diff --git a/src/main/java/edu/ohio/ais/rundeck/HttpBuilder.java b/src/main/java/edu/ohio/ais/rundeck/HttpBuilder.java index 8b5a895..16223e6 100644 --- a/src/main/java/edu/ohio/ais/rundeck/HttpBuilder.java +++ b/src/main/java/edu/ohio/ais/rundeck/HttpBuilder.java @@ -97,7 +97,8 @@ public CloseableHttpClient getHttpClient(Map 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() { @@ -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); } @@ -144,19 +143,19 @@ public void doRequest(Map 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() ){ @@ -170,7 +169,7 @@ public void doRequest(Map 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")); @@ -347,14 +346,14 @@ public String prettyPrint(HttpResponse response){ String getAuthHeader(PluginStepContext pluginStepContext, Map 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 ); @@ -368,7 +367,7 @@ String getAuthHeader(PluginStepContext pluginStepContext, Map 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", @@ -381,9 +380,12 @@ String getAuthHeader(PluginStepContext pluginStepContext, Map 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; @@ -491,4 +493,21 @@ static void propertyResolver(String pluginType, String property, Map options, String key) { + return getStringOption(options, key, null); + } + + static String getStringOption(Map options, String key, String defValue) { + return options.containsKey(key) && options.get(key) != null ? options.get(key).toString() : defValue; + } + + public static Integer getIntOption(Map 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 options, String key, Boolean defValue) { + return options.containsKey(key) && options.get(key) != null ? Boolean.parseBoolean(options.get(key).toString()) : defValue; + } + + } diff --git a/src/main/java/edu/ohio/ais/rundeck/HttpDescription.java b/src/main/java/edu/ohio/ais/rundeck/HttpDescription.java index 5cead91..de7ac43 100644 --- a/src/main/java/edu/ohio/ais/rundeck/HttpDescription.java +++ b/src/main/java/edu/ohio/ais/rundeck/HttpDescription.java @@ -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") diff --git a/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowNodeStepPlugin.java b/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowNodeStepPlugin.java index fada760..d11ab1b 100644 --- a/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowNodeStepPlugin.java +++ b/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowNodeStepPlugin.java @@ -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 { @@ -69,12 +71,11 @@ public void executeNodeStep(PluginStepContext context, Map 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); diff --git a/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowStepPlugin.java b/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowStepPlugin.java index bff7b7a..88fec4f 100644 --- a/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowStepPlugin.java +++ b/src/main/java/edu/ohio/ais/rundeck/HttpWorkflowStepPlugin.java @@ -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; /** @@ -77,11 +79,11 @@ public void executeStep(PluginStepContext pluginStepContext, Map ); // 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); diff --git a/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowNodeStepPluginTest.java b/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowNodeStepPluginTest.java index 5b0dfea..60453b7 100644 --- a/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowNodeStepPluginTest.java +++ b/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowNodeStepPluginTest.java @@ -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 ); } diff --git a/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowStepPluginTest.java b/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowStepPluginTest.java index ba7ccf1..88773a8 100644 --- a/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowStepPluginTest.java +++ b/src/test/java/edu/ohio/ais/rundeck/HttpWorkflowStepPluginTest.java @@ -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; @@ -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 { @@ -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()); + when(framework.getPropertyLookup()).thenReturn(frameworkProperties); + when(frameworkProperties.hasProperty(anyString())).thenReturn(true); + } @Test() @@ -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); }