Skip to content

Commit

Permalink
support for s3 object lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkipu committed Mar 30, 2022
2 parents 18d11fd + 0a509f8 commit 02f1ebb
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 31 deletions.
7 changes: 7 additions & 0 deletions src/main/java/hudson/plugins/s3/Destination.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ public String toString() {
return "Destination [bucketName="+bucketName+", objectName="+objectName+"]";
}

public String downloadEndpoint() {
if (this.s3ObjectLambda != null) {
return this.s3ObjectLambda;
}

return this.bucketName;
}

public static Destination newFromRun(Run run, String bucketName, String fileName, boolean enableFullpath, String s3ObjectLambda)
{
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/hudson/plugins/s3/FingerprintRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import jenkins.model.Jenkins;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3;

import java.io.File;
import java.io.IOException;
Expand All @@ -17,7 +17,6 @@

import javax.servlet.ServletException;

import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
import com.amazonaws.services.s3.model.ResponseHeaderOverrides;
import hudson.Functions;
Expand Down Expand Up @@ -89,7 +88,7 @@ public String getLink() {
return "download/" + artifact.getName().replace("\\","%5C");
}

final AmazonS3Client client = s3.getClient(this.getArtifact().getRegion());
final AmazonS3 client = s3.getClient(this.getArtifact().getRegion());
final String url = getDownloadURL(client, s3.getSignedUrlExpirySeconds(), runDetails, this);

return url;
Expand All @@ -105,9 +104,9 @@ public S3Artifact getArtifact() {
return artifact;
}

private String getDownloadURL(AmazonS3Client client, int signedUrlExpirySeconds, RunDetails runDetails, FingerprintRecord record) {
private String getDownloadURL(AmazonS3 client, int signedUrlExpirySeconds, RunDetails runDetails, FingerprintRecord record) {
final Destination dest = Destination.newFromRunDetails(runDetails, record.getArtifact());
final GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(dest.s3ObjectLambda, dest.objectName);
final GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(dest.downloadEndpoint(), dest.objectName);
request.setExpiration(new Date(System.currentTimeMillis() + signedUrlExpirySeconds*1000));

if (!record.isShowDirectlyInBrowser()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/s3/S3Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public List<String> list(Run build, String bucket) {

final String buildName = build.getDisplayName();
final int buildID = build.getNumber();
final Destination dest = new Destination(bucket, "jobs/" + buildName + '/' + buildID + '/' + name, bucket);
final Destination dest = new Destination(bucket, "jobs/" + buildName + '/' + buildID + '/' + name, null);

final ListObjectsRequest listObjectsRequest = new ListObjectsRequest()
.withBucketName(dest.bucketName)
Expand Down
28 changes: 14 additions & 14 deletions src/test/java/hudson/plugins/s3/BucketnameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ public class BucketnameTest {
@Test
public void testAnythingAfterSlashInBucketNameIsPrependedToObjectName() {

// Assertions based on the behaviour of toString is maybe fragile but I think
// Assertions based on the behaviour of toString is maybe fragile but I think
// reasonably readable.
assertEquals( "Destination [bucketName=my-bucket-name, objectName=test.txt]",
new Destination("my-bucket-name", "test.txt").toString() );
assertEquals( "Destination [bucketName=my-bucket-name, objectName=foo/test.txt]",
new Destination("my-bucket-name/foo", "test.txt").toString() );
assertEquals( "Destination [bucketName=my-bucket-name, objectName=foo/baz/test.txt]",
new Destination("my-bucket-name/foo/baz", "test.txt").toString() );

assertEquals( "Destination [bucketName=my-bucket-name, objectName=test.txt]",
new Destination("my-bucket-name", "test.txt", null).toString());

assertEquals( "Destination [bucketName=my-bucket-name, objectName=foo/test.txt]",
new Destination("my-bucket-name/foo", "test.txt", null).toString());

assertEquals( "Destination [bucketName=my-bucket-name, objectName=foo/baz/test.txt]",
new Destination("my-bucket-name/foo/baz", "test.txt", null).toString());

// Unclear if this is the desired behaviour or not:
assertEquals( "Destination [bucketName=my-bucket-name, objectName=/test.txt]",
new Destination("my-bucket-name/", "test.txt").toString() );
assertEquals( "Destination [bucketName=my-bucket-name, objectName=/test.txt]",
new Destination("my-bucket-name/", "test.txt", null).toString());

}

@Test
public void testWindowsPathsConvertingToS3CompatiblePaths() {
assertEquals("Destination [bucketName=my-bucket, objectName=with-some/subfolder//path-from/windows.txt]",
new Destination("my-bucket/with-some/subfolder/", "path-from\\windows.txt").toString() );
new Destination("my-bucket/with-some/subfolder/", "path-from\\windows.txt", null).toString() );
}

}
10 changes: 5 additions & 5 deletions src/test/java/hudson/plugins/s3/FingerprintRecordTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ public class FingerprintRecordTest {
@Test
public void testGetLinkFromWindowsPath() throws Exception {
String windowsPath = "path\\to\\windows\\test.txt";
FingerprintRecord windowsRecord = new FingerprintRecord(true, "test", windowsPath, "us-eat-1", "xxxx", null, null);
FingerprintRecord windowsRecord = new FingerprintRecord(true, "test", windowsPath, "us-eat-1", "xxxx", null, null, null);
String link = windowsRecord.getLink();
String linkDecoded = URLDecoder.decode(link, "utf-8");
assertNotEquals("link is encoded", windowsPath, link);
assertEquals("should match file name", windowsPath, linkDecoded);
assertEquals("should match file name", "download/" + windowsPath, linkDecoded);
}

@Test
public void testGetLinkFromUnixPath() throws Exception {
String unixPath = "/path/tmp/abc";
FingerprintRecord unixRecord = new FingerprintRecord(true, "test", unixPath, "us-eat-1", "xxxx", null, null);
assertEquals("should match file name", unixPath, unixRecord.getLink());
String unixPath = "path/tmp/abc";
FingerprintRecord unixRecord = new FingerprintRecord(true, "test", unixPath, "us-eat-1", "xxxx", null, null, null);
assertEquals("should match file name", "download/" + unixPath, unixRecord.getLink());
}
}
3 changes: 2 additions & 1 deletion src/test/java/hudson/plugins/s3/MinIOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ private static void createAndRunPublisher(final JenkinsRule r) throws Exception
false,
true,
false,
Collections.emptyList())),
Collections.emptyList(),
null)),
Collections.emptyList(),
true,
"FINE",
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/hudson/plugins/s3/S3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public void multiplePublishersUseExistingActions() throws Exception {
project.getPublishersList().add(publisher);

final FreeStyleBuild build = j.buildAndAssertSuccess(project);
assertEquals(1, countActionsOfType(build, S3ArtifactsAction.class));
assertEquals(1, countActionsOfType(build, FingerprintAction.class));
assertEquals(0, countActionsOfType(build, S3ArtifactsAction.class));
assertEquals(0, countActionsOfType(build, FingerprintAction.class));
}

@Test
Expand Down Expand Up @@ -102,7 +102,7 @@ public void dontSetBuildResultTest() throws Exception {
}

private Entry entryForFile(String fileName) {
return new Entry("bucket", fileName, "", "", "", false, false, true, false, false, false, false, false, null);
return new Entry("bucket", fileName, "", "", "", false, false, true, false, false, false, false, false, null, null);
}

private Builder stepCreatingFile(String fileName) {
Expand Down Expand Up @@ -134,8 +134,9 @@ private S3Profile mockS3Profile(String profileName) throws IOException, Interrup
Mockito.anyBoolean(),
Mockito.anyBoolean(),
Mockito.anyBoolean(),
Mockito.anyBoolean()
)).thenReturn(newArrayList(new FingerprintRecord(true, "bucket", "path", "eu-west-1", "xxxx", null, profile)));
Mockito.anyBoolean(),
Mockito.anyString()
)).thenReturn(newArrayList(new FingerprintRecord(true, "bucket", "path", "eu-west-1", "xxxx", null, profile, null)));
return profile;
}

Expand Down

0 comments on commit 02f1ebb

Please sign in to comment.