Skip to content

Commit

Permalink
Refactor PSC authentication to work with different PSC versions
Browse files Browse the repository at this point in the history
- Renamed inconsistent VCH related classes and methods

Change-Id: I977b875e4e9b60ef905144cdc8505fee7e8530f3
Reviewed-on: https://bellevue-ci.eng.vmware.com:8080/26018
Closures-Verified: jenkins <[email protected]>
Upgrade-Verified: jenkins <[email protected]>
Bellevue-Verified: jenkins <[email protected]>
CS-Verified: jenkins <[email protected]>
Reviewed-by: Stanislav Hadjiiski <[email protected]>
  • Loading branch information
Ivan Syarov committed Feb 9, 2018
1 parent f4e8423 commit a52aa3b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.vmware.admiral.test.util.AuthContext;
import com.vmware.admiral.test.util.SSHCommandExecutor.CommandResult;

public class CreateVCHRule implements TestRule {
public class CreateVchRule implements TestRule {

private final Logger LOG = Logger.getLogger(getClass().getName());

Expand All @@ -36,28 +36,28 @@ public class CreateVCHRule implements TestRule {

private final String[] hostsIps;

private VCHUtil vchUtil;
private VchUtil vchUtil;

public CreateVCHRule(AuthContext vicOvaAuthContext, AuthContext vcenterAuthContext,
public CreateVchRule(AuthContext vicOvaAuthContext, AuthContext vcenterAuthContext,
String namePrefix,
int hostsCount) {
this.hostsCount = hostsCount;
hostsIps = new String[hostsCount];
this.namePrefix = namePrefix;
vchUtil = new VCHUtil(vicOvaAuthContext, vcenterAuthContext);
vchUtil = new VchUtil(vicOvaAuthContext, vcenterAuthContext);
}

public CreateVCHRule datacenterName(String datacenterName) {
public CreateVchRule datacenterName(String datacenterName) {
this.datacenterName = datacenterName;
return this;
}

public CreateVCHRule dvsName(String dvsName) {
public CreateVchRule dvsName(String dvsName) {
this.dvsName = dvsName;
return this;
}

public CreateVCHRule datastoreName(String datastoreName) {
public CreateVchRule datastoreName(String datastoreName) {
this.datacenterName = datastoreName;
return this;
}
Expand Down Expand Up @@ -85,14 +85,14 @@ protected void createPortGroupsAndVchs(String testName) {
String portgroupName = testName + "-" + (i + 1);
createPortgroup(portgroupName);
String vchName = testName + "-" + (i + 1);
String ip = createVCH(vchName, portgroupName);
String ip = createVch(vchName, portgroupName);
hostsIps[i] = ip;
}
}

private void createPortgroup(String name) {
LOG.info("Creating portgroup with name: " + name);
CommandResult result = vchUtil.createDVSPortGroup(name, datacenterName, dvsName, 128);
CommandResult result = vchUtil.createDvsPortGroup(name, datacenterName, dvsName, 128);
if (result.getExitStatus() != 0) {
String error = String.format(
"Creating portgroup failed, command exit status [%d], command output:%n%s",
Expand All @@ -105,7 +105,7 @@ private void createPortgroup(String name) {

private void deletePortgroup(String name) {
LOG.info("Deleting portgroup with name: " + name);
CommandResult result = vchUtil.deleteDVSPortgroup(name, datacenterName, dvsName);
CommandResult result = vchUtil.deleteDvsPortgroup(name, datacenterName, dvsName);
if (result.getExitStatus() != 0) {
String error = String.format(
"Deleting portgroup failed, command exit status [%d], command output:%n%s",
Expand All @@ -116,7 +116,7 @@ private void deletePortgroup(String name) {
}
}

private String createVCH(String name, String portgroupName) {
private String createVch(String name, String portgroupName) {
LOG.info("Creating VCH with name: " + name);
CommandResult result = vchUtil.createVch(name, datastoreName, portgroupName);
if (result.getExitStatus() != 0) {
Expand Down Expand Up @@ -161,7 +161,7 @@ private void deleteAllHostsAndPortgroups(String testName) {
}
}

public CreateVCHRule keepOnSuccess() {
public CreateVchRule keepOnSuccess() {
this.keepOnSuccess = true;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,31 @@

public class IdentitySourceConfigurator {

private final String target;
private final String TARGET;
private final String USERNAME;
private final String PASSWORD;
private static final String ADD_AD_ENDPOINT = "/psc/mutation/add?propertyObjectType=com.vmware.vsphere.client.sso.admin.model.IdentitySourceLdapSpec";
private final List<Header> authHeaders;
private final HttpClient httpClient;
HttpClient client;

public IdentitySourceConfigurator(AuthContext vcenterAuthContext) {
this.target = "https://" + vcenterAuthContext.getTarget();
authHeaders = getAuthHeaders(target, vcenterAuthContext.getUsername(),
vcenterAuthContext.getPassword());
CookieStore cookies = new BasicCookieStore();
httpClient = HttpUtils.createUnsecureHttpClient(cookies, authHeaders);
if (!vcenterAuthContext.getTarget().startsWith("https://")) {
this.TARGET = "https://" + vcenterAuthContext.getTarget();
} else {
this.TARGET = vcenterAuthContext.getTarget();
}
this.USERNAME = vcenterAuthContext.getUsername();
this.PASSWORD = vcenterAuthContext.getPassword();
}

public void addIdentitySource(String specBody) {
if (Objects.isNull(client)) {
client = authenticateClient(TARGET, USERNAME, PASSWORD);
}
try {
StringEntity entity = new StringEntity(specBody);
HttpPost post = new HttpPost(target + ADD_AD_ENDPOINT);
HttpPost post = new HttpPost(TARGET + ADD_AD_ENDPOINT);
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
HttpResponse response = client.execute(post);
EntityUtils.consume(response.getEntity());
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Could not add identity source, response code was: "
Expand All @@ -72,7 +78,7 @@ public void addIdentitySource(String specBody) {
}
}

private List<Header> getAuthHeaders(String target, String username,
private HttpClient authenticateClient(String target, String username,
String password) {
String loginTarget = target + "/psc/login";
CookieStore cookies = new BasicCookieStore();
Expand All @@ -97,50 +103,51 @@ private List<Header> getAuthHeaders(String target, String username,
EntityUtils.consume(response.getEntity());
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException(
"Acquiring auth headers failed due to invalid credentials");
"Authenticating to the PSC failed due to invalid credentials");
}

Element samlPostForm = doc.getElementById("SamlPostForm");
String postTarget = samlPostForm.attr("action");
Elements elements = doc.getElementsByAttributeValue("name", "SAMLResponse");
if (elements.size() == 0) {
Elements samlElements = doc.getElementsByAttributeValue("name", "SAMLResponse");
if (samlElements.size() == 0) {
throw new RuntimeException(
"Could not get auth headers, probably the login sequence has changed");
"Authenticating to the PSC failed, probably the login sequence has changed");
}
String samlResponse = elements.get(0).attr("value");
String samlResponse = samlElements.get(0).attr("value");

params = new ArrayList<>();
params.add(new BasicNameValuePair("SAMLResponse", samlResponse));
params.add(new BasicNameValuePair("RelayState", "SessionId"));
post = new HttpPost(postTarget);
post.setEntity(new UrlEncodedFormEntity(params));
response = client.execute(post);
EntityUtils.consume(response.getEntity());

if (response.getStatusLine().getStatusCode() != 302) {
throw new RuntimeException(
"Authenticating to the PSC failed, probably the login sequence has changed");
}
get = new HttpGet(target + "/psc/");
response = client.execute(get);
EntityUtils.consume(response.getEntity());

String jSessionId = null;
String xsrfToken = null;
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException(
"Authenticating to the PSC failed, probably the login sequence has changed");
}
String token = null;
for (Cookie c : cookies.getCookies()) {
if (c.getName().equalsIgnoreCase("JSESSIONID")) {
jSessionId = c.getValue();
} else if (c.getName().equalsIgnoreCase("XSRF-TOKEN")) {
xsrfToken = c.getValue();
if (c.getName().equals("XSRF-TOKEN")) {
token = c.getValue();
break;
}
}
if (Objects.isNull(jSessionId)) {
throw new RuntimeException(
"Could not get session cookie, probably the login sequence has changed");
}
if (Objects.isNull(xsrfToken)) {
throw new RuntimeException(
"Could not get token header, probably the login sequence has changed");
if (!Objects.isNull(token)) {
// PSC 6.5
List<Header> headers = new ArrayList<>();
headers.add(new BasicHeader("X-XSRF-TOKEN", token));
return HttpUtils.createUnsecureHttpClient(cookies, headers);
} else {
return client;
}
List<Header> headers = new ArrayList<>();
headers.add(new BasicHeader("X-XSRF-TOKEN", xsrfToken));
headers.add(new BasicHeader("Cookie", "JSESSIONID=" + jSessionId));
return headers;
} catch (IOException e) {
throw new RuntimeException(
"Could not get auth headers", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.vmware.admiral.test.util.SSHCommandExecutor;
import com.vmware.admiral.test.util.SSHCommandExecutor.CommandResult;

public class VCHUtil {
public class VchUtil {

private static boolean isEnvironmentReady = false;

Expand All @@ -41,7 +41,7 @@ public class VCHUtil {

private AuthContext vcenterAuthContext;

public VCHUtil(AuthContext vicOvaAuthContext, AuthContext vcenterAuthContext) {
public VchUtil(AuthContext vicOvaAuthContext, AuthContext vcenterAuthContext) {
this.vcenterAuthContext = vcenterAuthContext;
this.executor = new SSHCommandExecutor(vicOvaAuthContext, 22);
if (!isEnvironmentReady) {
Expand Down Expand Up @@ -77,7 +77,7 @@ public CommandResult deleteVch(String vchName) {
return executor.execute(deleteVchCommand.toString(), 360);
}

public CommandResult createDVSPortGroup(String portgroupName, String datacenterName,
public CommandResult createDvsPortGroup(String portgroupName, String datacenterName,
String dvsName, int portsCount) {
StringBuilder createDVSPortgroup = new StringBuilder();
createDVSPortgroup.append("python " + SCRIPT_REMOTE_PATH)
Expand All @@ -92,7 +92,7 @@ public CommandResult createDVSPortGroup(String portgroupName, String datacenterN
return executor.execute(createDVSPortgroup.toString(), 360);
}

public CommandResult deleteDVSPortgroup(String portgroupNameName, String datacenterName,
public CommandResult deleteDvsPortgroup(String portgroupNameName, String datacenterName,
String dvsName) {
StringBuilder createDVSPortgroup = new StringBuilder();
createDVSPortgroup.append("python " + SCRIPT_REMOTE_PATH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import com.vmware.admiral.test.util.AuthContext;
import com.vmware.admiral.vic.test.ui.BaseTest;
import com.vmware.admiral.vic.test.ui.pages.hosts.AddContainerHostModalDialog;
import com.vmware.admiral.vic.test.ui.util.CreateVCHRule;
import com.vmware.admiral.vic.test.ui.util.CreateVchRule;

public class CreateAndProvisionVotingApp extends BaseTest {

Expand Down Expand Up @@ -73,7 +73,7 @@ public class CreateAndProvisionVotingApp extends BaseTest {
getDefaultAdminUsername(), getDefaultAdminPassword());

@Rule
public CreateVCHRule vchIps = new CreateVCHRule(vicOvaAuthContext, vcenterAuthContext,
public CreateVchRule vchIps = new CreateVchRule(vicOvaAuthContext, vcenterAuthContext,
"voting-app-test", 1);

@Test
Expand Down Expand Up @@ -301,28 +301,16 @@ public void createVotingAppTemplate() {
private void voteAndVerify(String votingTarget, String resultTarget) {
LOG.info("Opening voting app voting page");
open(votingTarget);
LOG.info("Voting six times for cats");
for (int i = 0; i < 6; i++) {
open(votingTarget);
$(By.cssSelector("#a")).click();
clearBrowserCache();
sleep(1000);
}
LOG.info("Voting two times for dogs");
for (int i = 0; i < 2; i++) {
open(votingTarget);
$(By.cssSelector("#b")).click();
clearBrowserCache();
sleep(1000);
}
// open the voting app result page
LOG.info("Voting for cats");
$(By.cssSelector("#a")).click();
clearBrowserCache();
sleep(1000);
LOG.info("Opening the votig app results page");
open(resultTarget);
// validate result is 75 to 25
LOG.info("Validating voting results");
$(By.cssSelector("#result>span")).shouldHave(Condition.exactText("8 votes"));
$(By.cssSelector(".choice.cats .stat.ng-binding")).shouldHave(Condition.exactText("75.0%"));
$(By.cssSelector(".choice.dogs .stat.ng-binding")).shouldHave(Condition.exactText("25.0%"));
$(By.cssSelector("#result>span")).shouldHave(Condition.exactText("1 vote"));
$(By.cssSelector(".choice.cats .stat.ng-binding"))
.shouldHave(Condition.exactText("100.0%"));
}

protected String extractAddressFromPortSetting(List<String> portSettings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import com.vmware.admiral.test.util.SSHCommandExecutor;
import com.vmware.admiral.test.util.SSHCommandExecutor.CommandResult;
import com.vmware.admiral.vic.test.ui.BaseTest;
import com.vmware.admiral.vic.test.ui.util.CreateVCHRule;
import com.vmware.admiral.vic.test.ui.util.CreateVchRule;

public class PushImageToHarborAndProvision extends BaseTest {

Expand All @@ -41,7 +41,7 @@ public class PushImageToHarborAndProvision extends BaseTest {
getDefaultAdminUsername(), getDefaultAdminPassword());

@Rule
public CreateVCHRule vchIps = new CreateVCHRule(vicOvaAuthContext, vcenterAuthContext,
public CreateVchRule vchIps = new CreateVchRule(vicOvaAuthContext, vcenterAuthContext,
"harbor-provisioning-test", 1);

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import com.vmware.admiral.test.ui.pages.volumes.VolumesPage;
import com.vmware.admiral.test.util.AuthContext;
import com.vmware.admiral.vic.test.ui.BaseTest;
import com.vmware.admiral.vic.test.ui.util.CreateVCHRule;
import com.vmware.admiral.vic.test.ui.util.CreateVchRule;

/**
* This test creates two projects, adds users with different roles to the projects, adds non-default
Expand Down Expand Up @@ -104,7 +104,7 @@ public class RBACAndItemsProjectAwareness extends BaseTest {
getDefaultAdminUsername(), getDefaultAdminPassword());

@Rule
public CreateVCHRule vchIps = new CreateVCHRule(vicOvaAuthContext, vcenterAuthContext,
public CreateVchRule vchIps = new CreateVchRule(vicOvaAuthContext, vcenterAuthContext,
"rbac-test", 2);

@Test
Expand Down

0 comments on commit a52aa3b

Please sign in to comment.