Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmanuelosunamoreno committed Aug 13, 2024
1 parent adc37c9 commit c03beb3
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 29 deletions.
55 changes: 37 additions & 18 deletions src/main/java/edu/ohio/ais/rundeck/HttpBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public CloseableHttpClient getHttpClient(Map<String, Object> options) throws Gen
httpClientBuilder.disableAuthCaching();
httpClientBuilder.disableAutomaticRetries();

if(options.containsKey("sslVerify") && !Boolean.parseBoolean(options.get("sslVerify").toString())) {

if(!getBooleanOption(options, "sslVerify", true)) {
log.log(5,"Disabling all SSL certificate verification.");
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(null, new TrustStrategy() {
Expand All @@ -110,17 +111,15 @@ public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws Ce
httpClientBuilder.setSSLHostnameVerifier(new NoopHostnameVerifier());
httpClientBuilder.setSSLContext(sslContextBuilder.build());
}
if(options.get("useSystemProxySettings").equals("true") && !Boolean.parseBoolean(options.get("proxySettings").toString())) {

if(getBooleanOption(options, "useSystemProxySettings", false) && !getBooleanOption(options, "proxySettings", false)) {
log.log(5, "Using proxy settings set on system");

httpClientBuilder.setRoutePlanner(new SystemDefaultRoutePlanner(ProxySelector.getDefault()));

HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort")), "http");
httpClientBuilder.setProxy(proxy);
}
if(options.containsKey("proxySettings") && Boolean.parseBoolean(options.get("proxySettings").toString())){
if(getBooleanOption(options, "proxySettings", false)){
log.log(5, "proxy IP set in job: " + options.get("proxyIP").toString());

HttpHost proxy = new HttpHost(options.get("proxyIP").toString(), Integer.valueOf((String)options.get("proxyPort")), "http");
HttpHost proxy = new HttpHost(options.get("proxyIP").toString(), Integer.parseInt((String)options.get("proxyPort")), "http");
httpClientBuilder.setProxy(proxy);
}

Expand All @@ -144,19 +143,19 @@ public void doRequest(Map<String, Object> options, HttpUriRequest request, Integ
try {
response = this.getHttpClient(options).execute(request);

if(options.containsKey("printResponseCode") && Boolean.parseBoolean(options.get("printResponseCode").toString())) {
if(getBooleanOption(options,"printResponseCode",false)) {
String responseCode = response.getStatusLine().toString();
log.log(2, "Response Code: " + responseCode);
}

//print the response content
if(options.containsKey("printResponse") && Boolean.parseBoolean(options.get("printResponse").toString())) {
if(getBooleanOption(options,"printResponse",false)) {
output = getOutputForResponse(this.prettyPrint(response));
//print response
log.log(2, output);
}

if(options.containsKey("printResponseToFile") && Boolean.parseBoolean(options.get("printResponseToFile").toString())){
if(getBooleanOption(options,"printResponseToFile",false)){
File file = new File(options.get("file").toString());
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
if( output.isEmpty() ){
Expand All @@ -170,7 +169,7 @@ public void doRequest(Map<String, Object> options, HttpUriRequest request, Integ
}

//check response status
if(options.containsKey("checkResponseCode") && Boolean.parseBoolean(options.get("checkResponseCode").toString())) {
if(getBooleanOption(options,"checkResponseCode",false)) {

if(options.containsKey("responseCode")){
int responseCode = Integer.valueOf( (String) options.get("responseCode"));
Expand Down Expand Up @@ -347,14 +346,14 @@ public String prettyPrint(HttpResponse response){


String getAuthHeader(PluginStepContext pluginStepContext, Map<String, Object> options) throws StepException {
String authentication = options.containsKey("authentication") ? options.get("authentication").toString() : AUTH_NONE;
String authentication = getStringOption(options, "authentication",AUTH_NONE);
//moving the password to the key storage
String password=null;
String authHeader = null;


if(options.containsKey("password") ){
String passwordRaw = options.containsKey("password") ? options.get("password").toString() : null;
String passwordRaw = getStringOption(options, "password");
//to avid the test error add a try-catch
//if it didn't find the key path, it will use the password directly
byte[] content = SecretBundleUtil.getStoragePassword(pluginStepContext.getExecutionContext(),passwordRaw );
Expand All @@ -368,7 +367,7 @@ String getAuthHeader(PluginStepContext pluginStepContext, Map<String, Object> o

if(authentication.equals(AUTH_BASIC)) {
// Setup the authentication header for BASIC
String username = options.containsKey("username") ? options.get("username").toString() : null;
String username = getStringOption(options, "username");

if(username == null || password == null) {
throw new StepException("Username and password not provided for BASIC Authentication",
Expand All @@ -381,9 +380,12 @@ String getAuthHeader(PluginStepContext pluginStepContext, Map<String, Object> o
authHeader = "Basic " + com.dtolabs.rundeck.core.utils.Base64.encode(authHeader);
} else if (authentication.equals(AUTH_OAUTH2)) {
// Get an OAuth token and setup the auth header for OAuth
String tokenEndpoint = options.containsKey("oauthTokenEndpoint") ? options.get("oauthTokenEndpoint").toString() : null;
String validateEndpoint = options.containsKey("oauthValidateEndpoint") ? options.get("oauthValidateEndpoint").toString() : null;
String clientId = options.containsKey("username") ? options.get("username").toString() : null;
String tokenEndpoint = getStringOption(options, "oauthTokenEndpoint");
String validateEndpoint = getStringOption(options, "oauthValidateEndpoint");
String clientId = getStringOption(options, "username");



String clientSecret = password;


Expand Down Expand Up @@ -491,4 +493,21 @@ static void propertyResolver(String pluginType, String property, Map<String,Obje
}
}

static String getStringOption(Map<String, Object> options, String key) {
return getStringOption(options, key, null);
}

static String getStringOption(Map<String, Object> options, String key, String defValue) {
return options.containsKey(key) && options.get(key) != null ? options.get(key).toString() : defValue;
}

public static Integer getIntOption(Map<String, Object> options, String key, Integer defValue) {
return options.containsKey(key) && options.get(key) != null ? Integer.parseInt(options.get(key).toString()) : defValue;
}

public static Boolean getBooleanOption(Map<String, Object> options, String key, Boolean defValue) {
return options.containsKey(key) && options.get(key) != null ? Boolean.parseBoolean(options.get(key).toString()) : defValue;
}


}
1 change: 1 addition & 0 deletions src/main/java/edu/ohio/ais/rundeck/HttpDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public Description getDescription() {
.renderingOption(StringRenderingConstants.GROUP_NAME,"Print")
.build())
.property(PropertyBuilder.builder()
.title("System Proxy Settings")
.booleanType("useSystemProxySettings")
.description("Choose whether to use proxy settings set on the JVM.")
.defaultValue("false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.*;

import static edu.ohio.ais.rundeck.HttpBuilder.propertyResolver;
import static edu.ohio.ais.rundeck.HttpBuilder.getIntOption;
import static edu.ohio.ais.rundeck.HttpBuilder.getStringOption;

@Plugin(name = HttpWorkflowNodeStepPlugin.SERVICE_PROVIDER_NAME, service = ServiceNameConstants.WorkflowNodeStep)
public class HttpWorkflowNodeStepPlugin implements NodeStepPlugin, Describable, ProxySecretBundleCreator {
Expand Down Expand Up @@ -69,12 +71,11 @@ public void executeNodeStep(PluginStepContext context, Map<String, Object> confi
);

// Parse out the options
String remoteUrl = configuration.containsKey("remoteUrl") ? configuration.get("remoteUrl").toString() : null;
String method = configuration.containsKey("method") ? configuration.get("method").toString() : null;

Integer timeout = configuration.containsKey("timeout") ? Integer.parseInt(configuration.get("timeout").toString()) : DEFAULT_TIMEOUT;
String headers = configuration.containsKey("headers") ? configuration.get("headers").toString() : null;
String body = configuration.containsKey("body") ? configuration.get("body").toString() : null;
String remoteUrl = getStringOption(configuration, "remoteUrl");
String method = getStringOption(configuration, "method");
Integer timeout =getIntOption(configuration,"timeout", DEFAULT_TIMEOUT);
String headers = getStringOption(configuration, "headers");
String body = getStringOption(configuration, "body");

log.log(5, "remoteUrl: " + remoteUrl);
log.log(5, "method: " + method);
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/edu/ohio/ais/rundeck/HttpWorkflowStepPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.util.*;

import static edu.ohio.ais.rundeck.HttpBuilder.propertyResolver;
import static edu.ohio.ais.rundeck.HttpBuilder.getIntOption;
import static edu.ohio.ais.rundeck.HttpBuilder.getStringOption;


/**
Expand Down Expand Up @@ -77,11 +79,11 @@ public void executeStep(PluginStepContext pluginStepContext, Map<String, Object>
);

// Parse out the options
String remoteUrl = options.containsKey("remoteUrl") ? options.get("remoteUrl").toString() : null;
String method = options.containsKey("method") ? options.get("method").toString() : null;
Integer timeout = options.containsKey("timeout") ? Integer.parseInt(options.get("timeout").toString()) : DEFAULT_TIMEOUT;
String headers = options.containsKey("headers") ? options.get("headers").toString() : null;
String body = options.containsKey("body") ? options.get("body").toString() : null;
String remoteUrl = getStringOption(options, "remoteUrl");
String method = getStringOption(options, "method");
Integer timeout =getIntOption(options,"timeout", DEFAULT_TIMEOUT);
String headers = getStringOption(options, "headers");
String body = getStringOption(options, "body");

if(remoteUrl == null || method == null) {
throw new StepException("Remote URL and Method are required.", StepFailureReason.ConfigurationFailure);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ public void canHandleAuthenticationRequired() throws NodeStepException {

options.put("remoteUrl", OAuthClientTest.BASE_URI + ERROR_URL_401);
options.put("method", "GET");
options.put("authentication", HttpBuilder.AUTH_BASIC);

this.plugin.executeNodeStep(pluginContext, options, node );
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/edu/ohio/ais/rundeck/HttpWorkflowStepPluginTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package edu.ohio.ais.rundeck;

import com.dtolabs.rundeck.core.common.Framework;
import com.dtolabs.rundeck.core.common.FrameworkProject;
import com.dtolabs.rundeck.core.common.FrameworkProjectMgr;
import com.dtolabs.rundeck.core.execution.ExecutionContext;
import com.dtolabs.rundeck.core.execution.workflow.steps.PluginStepContextImpl;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepException;
import com.dtolabs.rundeck.core.execution.workflow.steps.StepFailureReason;
import com.dtolabs.rundeck.core.plugins.configuration.Description;
import com.dtolabs.rundeck.core.utils.IPropertyLookup;
import com.dtolabs.rundeck.plugins.PluginLogger;
import com.dtolabs.rundeck.plugins.step.PluginStepContext;
import com.github.tomakehurst.wiremock.client.WireMock;
Expand All @@ -20,6 +24,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

public class HttpWorkflowStepPluginTest {
Expand Down Expand Up @@ -163,6 +168,19 @@ public void setUp() {
dataContext =new HashMap<>();
Mockito.when(pluginContext.getDataContext()).thenReturn(dataContext);

// Mock the necessary objects
Framework framework = Mockito.mock(Framework.class);
FrameworkProjectMgr frameworkProjectMgr = Mockito.mock(FrameworkProjectMgr.class);
FrameworkProject frameworkProject = Mockito.mock(FrameworkProject.class);
IPropertyLookup frameworkProperties = Mockito.mock(IPropertyLookup.class);

when(pluginContext.getFramework()).thenReturn(framework);
when(framework.getFrameworkProjectMgr()).thenReturn(frameworkProjectMgr);
when(frameworkProjectMgr.getFrameworkProject(anyString())).thenReturn(frameworkProject);
when(frameworkProject.getProperties()).thenReturn(new HashMap<String, String>());
when(framework.getPropertyLookup()).thenReturn(frameworkProperties);
when(frameworkProperties.hasProperty(anyString())).thenReturn(true);

}

@Test()
Expand Down Expand Up @@ -293,6 +311,7 @@ public void canHandleAuthenticationRequired() throws StepException {

options.put("remoteUrl", OAuthClientTest.BASE_URI + ERROR_URL_401);
options.put("method", "GET");
options.put("authentication", HttpBuilder.AUTH_BASIC);

this.plugin.executeStep(pluginContext, options);
}
Expand Down

0 comments on commit c03beb3

Please sign in to comment.