Skip to content

Commit

Permalink
Support HTTPS protocol for PCS (#155)
Browse files Browse the repository at this point in the history
* index on master: ab83a3d Merge remote-tracking branch 'remotes/origin/5.0.1-beta-SNAPSHOT'

* Revert pom changes

* Merge branch '5.0.1-beta-SNAPSHOT' of https://github.com/hpsa/hp-application-automation-tools-plugin into development

# Conflicts:
#	.github/PULL_REQUEST_TEMPLATE.md

* Added some comments

* changes according to sonarlint

* changes for sonarlint

* Remove some comments

* Comments

*  * Fix for supporting SECURITY-170 changes
 * https://jenkins.io/blog/2016/05/11/security-update/
 * https://issues.jenkins-ci.org/browse/JENKINS-39654

* code changes for sonarlint
  • Loading branch information
bamh authored and YafimK committed Mar 22, 2017
1 parent 5c3e59f commit 1339c9c
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 50 deletions.
@@ -1,3 +1,6 @@
/*
* Takes all the parameter from the job in order to create a loadtest object
* */
package com.hp.application.automation.tools.model;

import java.util.Arrays;
Expand All @@ -24,11 +27,12 @@ public class PcModel {
private final String description;
private final boolean addRunToTrendReport;
private final String trendReportId;
private final boolean HTTPSProtocol;

@DataBoundConstructor
public PcModel(String pcServerName, String almUserName, String almPassword, String almDomain, String almProject,
String testId, String testInstanceId, String timeslotDurationHours, String timeslotDurationMinutes,
PostRunAction postRunAction, boolean vudsMode, String description, boolean addRunToTrendReport, String trendReportId) {
PostRunAction postRunAction, boolean vudsMode, String description, boolean addRunToTrendReport, String trendReportId, boolean HTTPSProtocol) {

this.pcServerName = pcServerName;
this.almUserName = almUserName;
Expand All @@ -42,6 +46,7 @@ public PcModel(String pcServerName, String almUserName, String almPassword, Stri
this.vudsMode = vudsMode;
this.description = description;
this.addRunToTrendReport = addRunToTrendReport;
this.HTTPSProtocol = HTTPSProtocol;
this.trendReportId = trendReportId;
}

Expand Down Expand Up @@ -107,6 +112,9 @@ public String getDescription() {
return this.description;
}

public boolean httpsProtocol(){
return this.HTTPSProtocol;
}


public static List<PostRunAction> getPostRunActions() {
Expand All @@ -128,7 +136,7 @@ public String runParamsToString() {
"TestInstanceID='%s', TimeslotDuration='%s', PostRunAction='%s'%s%s]",

almDomain, almProject, testId, testInstanceId,
timeslotDuration, postRunAction.getValue(), vudsModeString, trendString);
timeslotDuration, postRunAction.getValue(), vudsModeString, trendString,HTTPSProtocol);
}


Expand All @@ -139,4 +147,10 @@ public String getTrendReportId() {
public boolean isAddRunToTrendReport() {
return addRunToTrendReport;
}

public String isHTTPSProtocol(){
if (!HTTPSProtocol)
return "http";
return "https";
}
}
20 changes: 18 additions & 2 deletions src/main/java/com/hp/application/automation/tools/pc/PcClient.java
Expand Up @@ -20,6 +20,12 @@
* THE SOFTWARE.
*/


/*
* Implements the main method of loadtest
*
* */

package com.hp.application.automation.tools.pc;

import hudson.FilePath;
Expand Down Expand Up @@ -49,7 +55,7 @@ public class PcClient {

public PcClient(PcModel pcModel, PrintStream logger) {
model = pcModel;
restProxy = new PcRestProxy(model.getPcServerName(), model.getAlmDomain(), model.getAlmProject());
restProxy = new PcRestProxy(model.isHTTPSProtocol(),model.getPcServerName(), model.getAlmDomain(), model.getAlmProject(),logger);
this.logger = logger;
}

Expand All @@ -66,13 +72,24 @@ public boolean login() {
loggedIn = restProxy.authenticate(user, model.getAlmPassword().toString());
} catch (PcException e) {
logger.println(e.getMessage());
// stackTraceToString(e);
} catch (Exception e) {
logger.println(e);
// stackTraceToString(e);
}
logger.println(String.format("Login %s", loggedIn ? "succeeded" : "failed"));
return loggedIn;
}

// public void stackTraceToString(Throwable e) {
// StringBuilder sb = new StringBuilder();
// logger.println("DEBUGMSG - STACKTRACE");
// for (StackTraceElement element : e.getStackTrace()) {
// logger.println("DEBUGMSG - " + element.toString());
// }
// }


public boolean isLoggedIn() {

return loggedIn;
Expand Down Expand Up @@ -247,7 +264,6 @@ public void waitForRunToPublishOnTrendReport(int runId, String trendReportId) th
break;
}else{
Thread.sleep(5000);
logger.println("Publishing...");
counter++;
if(counter >= 120){
logger.println("Error: Publishing didn't ended after 10 minutes, aborting...");
Expand Down
Expand Up @@ -20,13 +20,17 @@
* THE SOFTWARE.
*/

/*
* Implements the REST API methods for executing the loadtest
* */
package com.hp.application.automation.tools.pc;

import com.hp.application.automation.tools.common.PcException;
import com.hp.application.automation.tools.model.TimeslotDuration;
import com.hp.application.automation.tools.rest.RESTConstants;
import com.hp.application.automation.tools.sse.sdk.Base64Encoder;
import org.apache.commons.io.IOUtils;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
Expand All @@ -45,19 +49,22 @@
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import java.io.PrintStream;


import static org.apache.commons.httpclient.HttpStatus.*;

public class PcRestProxy {
protected static final String BASE_PC_API_URL = "http://%s/LoadTest/rest";

protected static final String BASE_PC_API_URL = "%s://%s/LoadTest/rest";
protected static final String BASE_PC_API_AUTHENTICATION_URL = BASE_PC_API_URL + "/authentication-point";
protected static final String AUTHENTICATION_LOGIN_URL = BASE_PC_API_AUTHENTICATION_URL + "/authenticate";
protected static final String AUTHENTICATION_LOGOUT_URL = BASE_PC_API_AUTHENTICATION_URL + "/logout";
Expand All @@ -76,21 +83,26 @@ public class PcRestProxy {
private String pcServer;
private String domain;
private String project;
private String webProtocol;

private HttpClient client;
private HttpContext context;
private CookieStore cookieStore;

public PcRestProxy(String pcServerName, String almDomain, String almProject) {

// private PrintStream logger;

public PcRestProxy(String webProtocolName, String pcServerName, String almDomain, String almProject,PrintStream mainLogger) {

// logger = mainLogger;
pcServer = pcServerName;
domain = almDomain;
project = almProject;
baseURL = String.format(PC_API_RESOURCES_TEMPLATE, pcServer, domain, project);
webProtocol = webProtocolName;
baseURL = String.format(PC_API_RESOURCES_TEMPLATE, webProtocol,pcServer, domain, project);

PoolingClientConnectionManager cxMgr = new PoolingClientConnectionManager(SchemeRegistryFactory.createDefault());
cxMgr.setMaxTotal(100);
cxMgr.setDefaultMaxPerRoute(20);


client = new DefaultHttpClient(cxMgr);
context = new BasicHttpContext();
Expand All @@ -100,17 +112,17 @@ public PcRestProxy(String pcServerName, String almDomain, String almProject) {


public boolean authenticate(String userName, String password) throws PcException, ClientProtocolException, IOException {

String userNameAndPassword = userName + ":" + password;
String encodedCredentials = Base64Encoder.encode(userNameAndPassword.getBytes());
HttpGet authRequest = new HttpGet(String.format(AUTHENTICATION_LOGIN_URL, pcServer));
HttpGet authRequest = new HttpGet(String.format(AUTHENTICATION_LOGIN_URL,webProtocol, pcServer));
authRequest.addHeader("Authorization", String.format("Basic %s", encodedCredentials));
executeRequest(authRequest);
return true;
}

public PcRunResponse startRun(int testId, int testInstaceId, TimeslotDuration timeslotDuration,
String postRunAction, boolean vudsMode) throws PcException, ClientProtocolException, IOException {
// logger.println("Starting run");
HttpPost startRunRequest = new HttpPost(String.format(baseURL + "/%s", RUNS_RESOURCE_NAME));
startRunRequest.addHeader(RESTConstants.CONTENT_TYPE, CONTENT_TYPE_XML);
PcRunRequest runRequestData = new PcRunRequest(testId, testInstaceId, 0, timeslotDuration, postRunAction, vudsMode);
Expand Down Expand Up @@ -204,23 +216,32 @@ public PcRunEventLog getRunEventLog(int runId) throws PcException, ClientProtoco
}

public boolean logout() throws PcException, ClientProtocolException, IOException {
HttpGet logoutRequest = new HttpGet(String.format(AUTHENTICATION_LOGOUT_URL, pcServer));
HttpGet logoutRequest = new HttpGet(String.format(AUTHENTICATION_LOGOUT_URL, webProtocol,pcServer));
executeRequest(logoutRequest);
return true;
}

protected HttpResponse executeRequest(HttpRequestBase request) throws PcException, IOException {
HttpResponse response = client.execute(request,context);
// logger.println(String.format("DEBUGMSG - Request uri %s",request.getURI().toString()));
// logger.println(String.format("DEBUGMSG - Request Method %s",request.getMethod().toString()));
// // Print all headers
// logger.println("DEBUGMSG - Headers");
// List<Header> httpHeaders = Arrays.asList(request.getAllHeaders());
// for (Header header : httpHeaders) {
// logger.println("DEBUGMSG - " + header.getName() + " : " + header.getValue());
// }
HttpResponse response = client.execute(request,context);
if (!isOk(response)){
String message;
try {
String content = IOUtils.toString(response.getEntity().getContent());
// logger.println("DEBUGMSG - response content: " + content);
PcErrorResponse exception = PcErrorResponse.xmlToObject(content);
message = String.format("%s Error code: %s", exception.ExceptionMessage, exception.ErrorCode);
} catch (Exception ex) {
message = String.format("%s Error code: %s", exception.ExceptionMessage, exception.ErrorCode);
} catch (Exception ex) {
message = response.getStatusLine().toString();
}
throw new PcException(message);
throw new PcException("executeRequest exception: " + message);
}
return response;
}
Expand Down
@@ -0,0 +1,60 @@
package com.hp.application.automation.tools.run;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.EnvironmentContributor;
import hudson.model.ParameterValue;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.ParametersAction;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

/**
* Fix for supporting SECURITY-170 changes
* https://jenkins.io/blog/2016/05/11/security-update/
* https://issues.jenkins-ci.org/browse/JENKINS-39654
*
*/
public class AdditionalParametersAction extends ParametersAction{

private List<ParameterValue> parameters;

public AdditionalParametersAction(List<ParameterValue> cparameters){
this.parameters = Collections.unmodifiableList(cparameters);
}



@Override
public List<ParameterValue> getParameters() {
return Collections.unmodifiableList(parameters);
}

@Override
public ParameterValue getParameter(String name){
for (ParameterValue p : parameters) {
if (p == null)
continue;
if (p.getName().equals(name))
return p;
}
return null;
}

@Extension
public static final class AdditionalParametersActionEnvironmentContributor extends EnvironmentContributor {
@Override
public void buildEnvironmentFor(Run r, EnvVars envs, TaskListener listener)
throws IOException, InterruptedException {
AdditionalParametersAction action = r.getAction(AdditionalParametersAction.class);
if (action != null) {
for (ParameterValue p : action.getParameters()) {
envs.putIfNotNull(p.getName(), String.valueOf(p.getValue()));
}
}
}
}
}

0 comments on commit 1339c9c

Please sign in to comment.