Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RUN-301 Encode user metadata to URL format to avoid error on upload request to S3 log storage #42

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/main/java/org/rundeck/plugins/S3LogFileStoragePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.dtolabs.utils.Streams;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -62,6 +64,9 @@ public class S3LogFileStoragePlugin implements ExecutionFileStoragePlugin, AWSCr
@PluginProperty(title = "Bucket name", required = true, description = "Bucket to store files in")
private String bucket;

@PluginProperty(title = "Encode user metadata", required = false, description = "Encode user metadata to URL format", defaultValue = "false")
private boolean encodeUserMetadata = false;

@PluginProperty(
title = "Path",
required = true,
Expand Down Expand Up @@ -410,7 +415,7 @@ protected ObjectMetadata createObjectMetadata(long length, Date lastModified) {
for (String s : STORED_META) {
Object v = context.get(s);
if (null != v) {
metadata.addUserMetadata(metaKey(s), v.toString());
metadata.addUserMetadata(metaKey(s), isEncodeUserMetadata() ? encodeStringToURLRequest(v.toString()) : v.toString());
}
}
metadata.setLastModified(lastModified);
Expand All @@ -422,6 +427,16 @@ protected String metaKey(final String s) {
return _PREFIX_META + s;
}

private String encodeStringToURLRequest(String value){
try {
return URLEncoder.encode(value, StandardCharsets.UTF_8.toString());
} catch (UnsupportedEncodingException e) {
logger.log(Level.WARNING, e.getMessage(), e);
}

return value;
}


public boolean retrieve(final String filetype, OutputStream stream)
throws IOException, ExecutionFileStorageException
Expand Down Expand Up @@ -567,4 +582,12 @@ public String getExpandedPath() {
public AmazonS3 getAmazonS3() {
return amazonS3;
}

public boolean isEncodeUserMetadata() {
return encodeUserMetadata;
}

public void setEncodeUserMetadata(boolean encodeUserMetadata) {
this.encodeUserMetadata = encodeUserMetadata;
}
}
28 changes: 28 additions & 0 deletions src/test/java/org/rundeck/plugins/S3LogFileStoragePluginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import org.junit.runners.JUnit4;

import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
Expand Down Expand Up @@ -728,6 +730,32 @@ public void storeMetadata() throws IOException, ExecutionFileStorageException {
Assert.assertEquals(testContext().get("serverUUID"), userMetadata.get("rundeck.serverUUID"));
}

@Test
public void storeEncodedMetadata() throws IOException, ExecutionFileStorageException {
testPlugin testPlugin = new S3LogFileStoragePluginTest.testPlugin();
testPlugin.setAWSAccessKeyId("blah");
testPlugin.setAWSSecretKey("blah");
testPlugin.setBucket("testBucket");
testPlugin.setEncodeUserMetadata(true);
testPlugin.initialize(testContext());

testPlugin.getTestS3().putObject = new PutObjectResult();
Date lastModified = new Date();
int length = 123;
boolean result = false;
result = testPlugin.store(DEFAULT_FILETYPE, null, length, lastModified);
Assert.assertTrue(result);
Assert.assertEquals(length, testPlugin.getTestS3().putObjectRequest.getMetadata().getContentLength());
Assert.assertEquals(lastModified, testPlugin.getTestS3().putObjectRequest.getMetadata().getLastModified());
Map<String, String> userMetadata = testPlugin.getTestS3().putObjectRequest.getMetadata().getUserMetadata();
Assert.assertEquals(5, userMetadata.size());
Assert.assertEquals(URLEncoder.encode((String)testContext().get("execid"), StandardCharsets.UTF_8.toString()), userMetadata.get("rundeck.execid"));
Assert.assertEquals(URLEncoder.encode((String)testContext().get("project"), StandardCharsets.UTF_8.toString()), userMetadata.get("rundeck.project"));
Assert.assertEquals(URLEncoder.encode((String)testContext().get("url"), StandardCharsets.UTF_8.toString()), userMetadata.get("rundeck.url"));
Assert.assertEquals(URLEncoder.encode((String)testContext().get("serverUrl"), StandardCharsets.UTF_8.toString()), userMetadata.get("rundeck.serverUrl"));
Assert.assertEquals(URLEncoder.encode((String)testContext().get("serverUUID"), StandardCharsets.UTF_8.toString()), userMetadata.get("rundeck.serverUUID"));
}

class testOutputStream extends OutputStream {
boolean wasWrite = false;
boolean writeIOException = false;
Expand Down