Skip to content

Commit

Permalink
Create new Storage class. (#45)
Browse files Browse the repository at this point in the history
* Create new Storage class.
* Create new Strings class.
Co-authored-by: Karl DeBisschop <[email protected]>
  • Loading branch information
kdebisschop authored Mar 10, 2020
1 parent da1038c commit 5f2b14b
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 229 deletions.
8 changes: 3 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,16 @@ configurations {
dependencies {
implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.10.1'

implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.14.4'
implementation group: 'com.squareup.okhttp3', name: 'okhttp', version: '3.13.1'

implementation (
'org.rundeck:rundeck-core:3.0.+',
'org.rundeck:rundeck-core:3.2.+',
)

testImplementation group: 'junit', name: 'junit', version:'4.12'

testImplementation (
'org.mockito:mockito-all:1.9.5',
'org.powermock:powermock-module-junit4:1.5',
'org.powermock:powermock-api-mockito:1.5'
'org.mockito:mockito-all:1.10.19'
)
}

Expand Down
22 changes: 10 additions & 12 deletions src/main/java/com/bioraft/rundeck/rancher/RancherAddService.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

import static com.bioraft.rundeck.rancher.Constants.*;
import static com.bioraft.rundeck.rancher.Errors.ErrorCause.*;
import static com.bioraft.rundeck.rancher.RancherShared.*;
import static com.dtolabs.rundeck.core.Constants.ERR_LEVEL;
import static com.dtolabs.rundeck.core.Constants.INFO_LEVEL;
import static com.dtolabs.rundeck.core.plugins.configuration.PropertyResolverFactory.FRAMEWORK_PREFIX;
Expand Down Expand Up @@ -126,15 +125,14 @@ public void executeStep(final PluginStepContext context, final Map<String, Objec
PluginLogger logger = context.getLogger();

String endpoint = cfgFromProjectOrFramework(framework, project, RANCHER_CONFIG_ENDPOINT);
String spec = endpoint + apiPath(environmentId, "/services");
String spec = endpoint + (new Strings()).apiPath(environmentId, "/services");

try {
Storage storage = new Storage(context.getExecutionContext());
String accessKeyPath = cfgFromRancherProjectOrFramework(framework, project, CONFIG_ACCESSKEY_PATH);
String accessKey = loadStoragePathData(context.getExecutionContext(), accessKeyPath);
client.setAccessKey(accessKey);
client.setAccessKey(storage.loadStoragePathData(accessKeyPath));
String secretKeyPath = cfgFromRancherProjectOrFramework(framework, project, CONFIG_SECRETKEY_PATH);
String secretKey = loadStoragePathData(context.getExecutionContext(), secretKeyPath);
client.setSecretKey(secretKey);
client.setSecretKey(storage.loadStoragePathData(secretKeyPath));
} catch (IOException e) {
throw new StepException("Could not get secret storage path", e, IO_EXCEPTION);
}
Expand All @@ -145,17 +143,17 @@ public void executeStep(final PluginStepContext context, final Map<String, Objec
mapBuilder.put("kind", "container");
mapBuilder.put("networkMode", "managed");

addJsonData(OPT_DATA_VOLUMES, ensureStringIsJsonArray(dataVolumes), mapBuilder);
addJsonData(OPT_ENV_VARS, ensureStringIsJsonObject(environment), mapBuilder);
addJsonData(OPT_LABELS, ensureStringIsJsonObject(labels), mapBuilder);
addJsonData(OPT_DATA_VOLUMES, (new Strings()).ensureStringIsJsonArray(dataVolumes), mapBuilder);
addJsonData(OPT_ENV_VARS, (new Strings()).ensureStringIsJsonObject(environment), mapBuilder);
addJsonData(OPT_LABELS, (new Strings()).ensureStringIsJsonObject(labels), mapBuilder);
addSecrets(mapBuilder);

JsonNode check;
String stackCheck;
String stackId;
try {
// First look for a stack with the designated ID.
stackCheck = endpoint + apiPath(environmentId, "/stacks/" + stackName);
stackCheck = endpoint + (new Strings()).apiPath(environmentId, "/stacks/" + stackName);
logger.log(INFO_LEVEL, "Looking for " + stackCheck);
check = client.get(stackCheck);
if (check.path("type").asText().equals("error")) {
Expand Down Expand Up @@ -197,7 +195,7 @@ private String cfgFromRancherProjectOrFramework(Framework framework, String proj

private String stackId(String stackName, String endpoint, PluginLogger logger) throws StepException {
try {
String stackCheck = endpoint + apiPath(environmentId, "/stacks?name=" + stackName);
String stackCheck = endpoint + (new Strings()).apiPath(environmentId, "/stacks?name=" + stackName);
logger.log(INFO_LEVEL, "Looking for " + stackCheck);
JsonNode check = client.get(stackCheck);
if (check.path("data").has(0)) {
Expand Down Expand Up @@ -231,7 +229,7 @@ private void addSecrets(ImmutableMap.Builder<String, Object> mapBuilder) {
// Add in the new or replacement secrets specified in the step.
List<Map<String, String>> secretsArray = new ArrayList<>();
for (String secretId : secrets.split("[,; ]+")) {
secretsArray.add(secretJsonMap(secretId));
secretsArray.add((new Strings()).secretJsonMap(secretId));
}
mapBuilder.put(OPT_SECRETS, (new ObjectMapper()).valueToTree(secretsArray));
}
Expand Down
29 changes: 3 additions & 26 deletions src/main/java/com/bioraft/rundeck/rancher/RancherFileCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.dtolabs.rundeck.core.plugins.configuration.Describable;
import com.dtolabs.rundeck.core.plugins.configuration.Description;
import com.dtolabs.rundeck.core.plugins.configuration.PropertyUtil;
import com.dtolabs.rundeck.core.storage.ResourceMeta;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription;
import com.dtolabs.rundeck.plugins.util.DescriptionBuilder;
Expand All @@ -49,7 +48,6 @@

import static com.bioraft.rundeck.rancher.Constants.*;
import static com.bioraft.rundeck.rancher.Errors.ErrorCause.*;
import static com.bioraft.rundeck.rancher.RancherShared.*;
import static com.dtolabs.rundeck.core.Constants.DEBUG_LEVEL;

/**
Expand Down Expand Up @@ -126,8 +124,9 @@ private String copyFile(final ExecutionContext context, final File scriptfile, f
String accessKey;
String secretKey;
try {
accessKey = this.loadStoragePathData(context, nodeAttributes.get(CONFIG_ACCESSKEY_PATH));
secretKey = this.loadStoragePathData(context, nodeAttributes.get(CONFIG_SECRETKEY_PATH));
Storage storage = new Storage(context);
accessKey = storage.loadStoragePathData(nodeAttributes.get(CONFIG_ACCESSKEY_PATH));
secretKey = storage.loadStoragePathData(nodeAttributes.get(CONFIG_SECRETKEY_PATH));
} catch (IOException e) {
throw new FileCopierException(e.getMessage(), AUTHENTICATION_FAILURE);
}
Expand Down Expand Up @@ -249,26 +248,4 @@ public void run() {
new BufferedReader(new InputStreamReader(inputStream)).lines().forEach(consumer);
}
}

/**
* Get a (secret) value from password storage.
*
* @param context The execution object that contains a reference to Storage.
* @param passwordStoragePath A path in Rundeck stage where value is stored.
*
* @return The specified password.
*
* @throws IOException When connection to Rundeck storage fails.
*/
private String loadStoragePathData(final ExecutionContext context, final String passwordStoragePath)
throws IOException {
if (null == passwordStoragePath) {
throw new IOException("Rancher access key and secret key must not be null");
}
ResourceMeta contents = context.getStorageTree().getResource(passwordStoragePath).getContents();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
contents.writeContent(byteArrayOutputStream);
return new String(byteArrayOutputStream.toByteArray());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static com.bioraft.rundeck.rancher.Constants.OPT_DATA_VOLUMES;
import static com.bioraft.rundeck.rancher.Constants.OPT_SECRETS;
import static com.bioraft.rundeck.rancher.Errors.ErrorCause.*;
import static com.bioraft.rundeck.rancher.RancherShared.*;

/**
* Workflow Node Step Plug-in to upgrade a service associated with a node.
Expand Down Expand Up @@ -139,7 +138,7 @@ public void setField(String field, String newData) throws NodeStepException {
} else {
objectNode = (ObjectNode) jsonNode;
}
JsonNode map = objectMapper.readTree(ensureStringIsJsonObject(newData));
JsonNode map = objectMapper.readTree((new Strings()).ensureStringIsJsonObject(newData));
Iterator<Map.Entry<String, JsonNode>> iterator = map.fields();
while (iterator.hasNext()) {
Map.Entry<String, JsonNode> entry = iterator.next();
Expand Down Expand Up @@ -173,7 +172,7 @@ public void removeField(String field, String remove) throws NodeStepException {
ObjectNode objectNode = (ObjectNode) launchConfigObject.get(field);
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode map = objectMapper.readTree(ensureStringIsJsonArray(remove));
JsonNode map = objectMapper.readTree((new Strings()).ensureStringIsJsonArray(remove));
Iterator<JsonNode> iterator = map.elements();
while (iterator.hasNext()) {
String entry = iterator.next().asText();
Expand Down Expand Up @@ -209,7 +208,7 @@ private void addSecrets(ObjectNode launchConfig) {

// Add in the new or replacement secrets specified in the step.
for (String secretId : secrets.split("[,; ]+")) {
secretsArray.add(buildSecret(secretId));
secretsArray.add((new Strings()).buildSecret(secretId));
logger.log(Constants.INFO_LEVEL, "Adding secret map to " + secretId);
}
}
Expand Down Expand Up @@ -238,16 +237,16 @@ private void setMountArray(String newData) throws NodeStepException {
Iterator<JsonNode> elements = launchConfigObject.get(OPT_DATA_VOLUMES).elements();
while (elements.hasNext()) {
String element = elements.next().asText();
hashMap.put(mountPoint(element), element);
hashMap.put((new Strings()).mountPoint(element), element);
}
}

// Copy new mounts into hash, possibly overwriting some vales.
ObjectMapper objectMapper = new ObjectMapper();
try {
JsonNode map = objectMapper.readTree(ensureStringIsJsonArray(newData));
JsonNode map = objectMapper.readTree((new Strings()).ensureStringIsJsonArray(newData));
Iterator<JsonNode> mounts = map.elements();
mounts.forEachRemaining(spec -> hashMap.put(mountPoint(spec.asText()), spec.asText()));
mounts.forEachRemaining(spec -> hashMap.put((new Strings()).mountPoint(spec.asText()), spec.asText()));

} catch (JsonProcessingException e) {
throw new NodeStepException("Could not parse JSON for " + OPT_DATA_VOLUMES + "\n" + newData, e, INVALID_CONFIGURATION, nodeName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

import static com.bioraft.rundeck.rancher.Constants.*;
import static com.bioraft.rundeck.rancher.Errors.ErrorCause.*;
import static com.bioraft.rundeck.rancher.RancherShared.loadStoragePathData;

/**
* Workflow Node Step Plug-in to manage a service associated with a node.
Expand Down Expand Up @@ -75,8 +74,9 @@ public void executeNodeStep(PluginStepContext ctx, Map<String, Object> cfg, INod
String accessKey;
String secretKey;
try {
accessKey = loadStoragePathData(executionContext, attributes.get(CONFIG_ACCESSKEY_PATH));
secretKey = loadStoragePathData(executionContext, attributes.get(CONFIG_SECRETKEY_PATH));
Storage storage = new Storage(executionContext);
accessKey = storage.loadStoragePathData(attributes.get(CONFIG_ACCESSKEY_PATH));
secretKey = storage.loadStoragePathData(attributes.get(CONFIG_SECRETKEY_PATH));
client.setAccessKey(accessKey);
client.setSecretKey(secretKey);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

import static com.bioraft.rundeck.rancher.Constants.*;
import static com.bioraft.rundeck.rancher.Errors.ErrorCause.*;
import static com.bioraft.rundeck.rancher.RancherShared.*;
import static com.dtolabs.rundeck.core.Constants.ERR_LEVEL;
import static com.dtolabs.rundeck.core.Constants.INFO_LEVEL;
import static org.apache.commons.lang.StringUtils.defaultString;
Expand Down Expand Up @@ -90,9 +89,10 @@ public void executeStep(final PluginStepContext context, final Map<String, Objec

String spec = endpoint + "/projects/" + environmentId + "/stacks/";
try {
String accessKey = loadStoragePathData(context.getExecutionContext(), accessKeyPath);
Storage storage = new Storage(context.getExecutionContext());
String accessKey = storage.loadStoragePathData(accessKeyPath);
client.setAccessKey(accessKey);
String secretKey = loadStoragePathData(context.getExecutionContext(), secretKeyPath);
String secretKey = storage.loadStoragePathData(secretKeyPath);
client.setSecretKey(secretKey);
} catch (IOException e) {
throw new StepException("Could not get secret storage path", e, IO_EXCEPTION);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.bioraft.rundeck.rancher;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
Expand All @@ -31,13 +30,11 @@
import com.dtolabs.rundeck.core.execution.workflow.steps.StepFailureReason;
import com.dtolabs.rundeck.core.plugins.Plugin;
import com.dtolabs.rundeck.core.plugins.configuration.*;
import com.dtolabs.rundeck.core.storage.ResourceMeta;
import com.dtolabs.rundeck.plugins.ServiceNameConstants;
import com.dtolabs.rundeck.plugins.util.DescriptionBuilder;

import static com.dtolabs.rundeck.core.Constants.DEBUG_LEVEL;
import static com.bioraft.rundeck.rancher.Constants.*;
import static com.bioraft.rundeck.rancher.RancherShared.*;

/**
* RancherNodeExecutorPlugin is a {@link NodeExecutor} plugin implementation for
Expand Down Expand Up @@ -85,8 +82,9 @@ public NodeExecutorResult executeCommand(final ExecutionContext context, final S
}

try {
accessKey = this.loadStoragePathData(context, nodeAttributes.get(CONFIG_ACCESSKEY_PATH));
secretKey = this.loadStoragePathData(context, nodeAttributes.get(CONFIG_SECRETKEY_PATH));
Storage storage = new Storage(context);
accessKey = storage.loadStoragePathData(nodeAttributes.get(CONFIG_ACCESSKEY_PATH));
secretKey = storage.loadStoragePathData(nodeAttributes.get(CONFIG_SECRETKEY_PATH));
} catch (IOException e) {
return NodeExecutorResultImpl.createFailure(StepFailureReason.IOFailure, e.getMessage(), node);
}
Expand Down Expand Up @@ -156,25 +154,4 @@ private String readLogFile(String file, String url) throws IOException, Interrup
RancherWebSocketListener.getFile(url, accessKey, secretKey, output, file);
return output.toString();
}

/**
* Get a (secret) value from password storage.
*
* @param context The execution object that contains a reference to Storage.
* @param passwordStoragePath A path in Rundeck stage where value is stored.
*
* @return The specified password.
*
* @throws IOException When connection to Rundeck storage fails.
*/
private String loadStoragePathData(final ExecutionContext context, final String passwordStoragePath)
throws IOException {
if (null == passwordStoragePath) {
return null;
}
ResourceMeta contents = context.getStorageTree().getResource(passwordStoragePath).getContents();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
contents.writeContent(byteArrayOutputStream);
return new String(byteArrayOutputStream.toByteArray());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.dtolabs.rundeck.plugins.util.DescriptionBuilder;

import static com.bioraft.rundeck.rancher.Constants.*;
import static com.bioraft.rundeck.rancher.RancherShared.*;

/**
* RancherResourceModelSourceFactory establishes parameters for Rancher node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@

import static com.bioraft.rundeck.rancher.Constants.*;
import static com.bioraft.rundeck.rancher.Errors.ErrorCause.*;
import static com.bioraft.rundeck.rancher.RancherShared.*;
import static com.dtolabs.rundeck.core.Constants.DEBUG_LEVEL;
import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.CODE_SYNTAX_MODE;
import static com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants.DISPLAY_TYPE_KEY;
Expand Down Expand Up @@ -123,8 +122,9 @@ public void executeNodeStep(PluginStepContext ctx, Map<String, Object> cfg, INod
String accessKey;
String secretKey;
try {
accessKey = loadStoragePathData(executionContext, attributes.get(CONFIG_ACCESSKEY_PATH));
secretKey = loadStoragePathData(executionContext, attributes.get(CONFIG_SECRETKEY_PATH));
Storage storage = new Storage(executionContext);
accessKey = storage.loadStoragePathData(attributes.get(CONFIG_ACCESSKEY_PATH));
secretKey = storage.loadStoragePathData(attributes.get(CONFIG_SECRETKEY_PATH));
} catch (IOException e) {
throw new NodeStepException("Could not get secret storage path", e, IO_EXCEPTION, this.nodeName);
}
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/bioraft/rundeck/rancher/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.bioraft.rundeck.rancher;

import com.dtolabs.rundeck.core.execution.ExecutionContext;
import com.dtolabs.rundeck.core.storage.ResourceMeta;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Storage {

private final ExecutionContext context;

public Storage(final ExecutionContext context) {
this.context = context;
}

/**
* Get a (secret) value from password storage.
*
* @param passwordStoragePath The path to look up in storage.
* @return The requested secret or password.
* @throws IOException When there is an IO Exception writing to stream.
*/
public String loadStoragePathData(final String passwordStoragePath) throws IOException {
if (null == passwordStoragePath) {
throw new IOException("Storage path is not defined.");
}
ResourceMeta contents = context.getStorageTree().getResource(passwordStoragePath).getContents();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
contents.writeContent(byteArrayOutputStream);
return new String(byteArrayOutputStream.toByteArray());
}

}
Loading

0 comments on commit 5f2b14b

Please sign in to comment.