Skip to content
Open
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
22 changes: 21 additions & 1 deletion src/main/java/hudson/plugins/s3/S3BucketPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@
import org.kohsuke.stapler.interceptor.RequirePOST;
import software.amazon.awssdk.core.exception.SdkException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.model.ChecksumAlgorithm;

import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
Expand All @@ -69,6 +71,7 @@
private boolean dontWaitForConcurrentBuildCompletion;
private boolean dontSetBuildResultOnFailure;
private int uploadTimeout = 30; // default 30 mins
private ChecksumAlgorithm checksumAlgorithm = ChecksumAlgorithm.CRC32; // SDK's default

/**
* In-memory representation of console log level.
Expand Down Expand Up @@ -248,6 +251,23 @@
this.uploadTimeout = Math.max(uploadTimeout, Uploads.MIN_UPLOAD_TIMEOUT);
}

@DataBoundSetter
public void setChecksumAlgorithm(String checksumAlgorithm) {
if (checksumAlgorithm == null || checksumAlgorithm.isBlank()) {
this.checksumAlgorithm = ChecksumAlgorithm.CRC32;
return;
}
String normalized = checksumAlgorithm.toUpperCase(Locale.ROOT);
ChecksumAlgorithm algo = ChecksumAlgorithm.fromValue(normalized);
if (algo == ChecksumAlgorithm.UNKNOWN_TO_SDK_VERSION) {
throw new IllegalArgumentException("Unsupported checksum algorithm: " + checksumAlgorithm);
} else if (algo == ChecksumAlgorithm.CRC64_NVME) {
throw new UnsupportedOperationException("Checksum algorithm '" + checksumAlgorithm + "' requires AWS CRT dependency, which is currently unavailable."
+ "\nUse another algorithm (CRC32, CRC32C, SHA1, SHA256).");
}
this.checksumAlgorithm = algo;
}

Check warning on line 269 in src/main/java/hudson/plugins/s3/S3BucketPublisher.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 256-269 are not covered by tests

private void log(final PrintStream logger, final String message) {
log(Level.INFO, logger, message);
}
Expand Down Expand Up @@ -333,7 +353,7 @@
final Map<String, String> escapedMetadata = buildMetadata(envVars, entry);

final List<FingerprintRecord> records = Lists.newArrayList();
final List<FingerprintRecord> fingerprints = profile.upload(run, bucket, paths, filenames, escapedMetadata, storageClass, selRegion, entry.uploadFromSlave, entry.managedArtifacts, entry.useServerSideEncryption, entry.gzipFiles, uploadTimeout);
final List<FingerprintRecord> fingerprints = profile.upload(run, bucket, paths, filenames, escapedMetadata, storageClass, selRegion, entry.uploadFromSlave, entry.managedArtifacts, entry.useServerSideEncryption, entry.gzipFiles, checksumAlgorithm, uploadTimeout);

for (FingerprintRecord fingerprintRecord : fingerprints) {
records.add(fingerprintRecord);
Expand Down
22 changes: 20 additions & 2 deletions src/main/java/hudson/plugins/s3/S3Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.ChecksumAlgorithm;
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.ListObjectsV2Request;
Expand Down Expand Up @@ -163,30 +164,47 @@
final boolean useServerSideEncryption,
final boolean gzipFiles,
final int uploadTimeout) throws IOException, InterruptedException {
return upload(run, bucketName, filePaths, fileNames, userMetadata, storageClass, selregion,
uploadFromSlave, managedArtifacts, useServerSideEncryption, gzipFiles, null, uploadTimeout);
}

public List<FingerprintRecord> upload(Run<?, ?> run,
final String bucketName,
final List<FilePath> filePaths,
final List<String> fileNames,
final Map<String, String> userMetadata,
final String storageClass,
final String selregion,
final boolean uploadFromSlave,
final boolean managedArtifacts,
final boolean useServerSideEncryption,
final boolean gzipFiles,
final ChecksumAlgorithm checksumAlgorithm,
final int uploadTimeout) throws IOException, InterruptedException {
final List<FingerprintRecord> fingerprints = new ArrayList<>(fileNames.size());

try {
for (int i = 0; i < fileNames.size(); i++) {
final FilePath filePath = filePaths.get(i);
final String fileName = fileNames.get(i);

final Destination dest;
final boolean produced;
if (managedArtifacts) {
dest = Destination.newFromRun(run, bucketName, fileName, true);
produced = run.getTimeInMillis() <= filePath.lastModified() + 2000;
} else {
dest = new Destination(bucketName, fileName);
produced = false;
}

final MasterSlaveCallable<String> upload;
if (gzipFiles) {
upload = new S3GzipCallable(accessKey, secretKey, useRole, dest, userMetadata,
storageClass, selregion, useServerSideEncryption, getProxy(), usePathStyle);
storageClass, selregion, useServerSideEncryption, getProxy(), usePathStyle, checksumAlgorithm);

Check warning on line 204 in src/main/java/hudson/plugins/s3/S3Profile.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 167-204 are not covered by tests
} else {
upload = new S3UploadCallable(accessKey, secretKey, useRole, dest, userMetadata,
storageClass, selregion, useServerSideEncryption, getProxy(), usePathStyle);
storageClass, selregion, useServerSideEncryption, getProxy(), usePathStyle, checksumAlgorithm);
}

final FingerprintRecord fingerprintRecord = repeat(maxUploadRetries, uploadRetryTime, dest, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import hudson.plugins.s3.Destination;
import hudson.remoting.VirtualChannel;
import hudson.util.Secret;
import software.amazon.awssdk.services.s3.model.ChecksumAlgorithm;
import software.amazon.awssdk.services.s3.model.PutObjectRequest;

import java.io.File;
Expand All @@ -15,6 +16,7 @@
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;

public abstract class S3BaseUploadCallable extends S3Callable<String> {
Expand All @@ -23,16 +25,23 @@
private final String storageClass;
private final Map<String, String> userMetadata;
private final boolean useServerSideEncryption;

private final ChecksumAlgorithm checksumAlgorithm;

public S3BaseUploadCallable(String accessKey, Secret secretKey, boolean useRole,
Destination dest, Map<String, String> userMetadata, String storageClass, String selregion,
boolean useServerSideEncryption, ProxyConfiguration proxy, boolean usePathStyle) {
this(accessKey, secretKey, useRole, dest, userMetadata, storageClass, selregion, useServerSideEncryption, proxy, usePathStyle, null);
}

Check warning on line 34 in src/main/java/hudson/plugins/s3/callable/S3BaseUploadCallable.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 33-34 are not covered by tests

public S3BaseUploadCallable(String accessKey, Secret secretKey, boolean useRole,
Destination dest, Map<String, String> userMetadata, String storageClass, String selregion,
boolean useServerSideEncryption, ProxyConfiguration proxy, boolean usePathStyle, ChecksumAlgorithm checksumAlgorithm) {
super(accessKey, secretKey, useRole, selregion, proxy, usePathStyle);
this.dest = dest;
this.storageClass = storageClass;
this.userMetadata = userMetadata;
this.useServerSideEncryption = useServerSideEncryption;
this.checksumAlgorithm = checksumAlgorithm;
}

/**
Expand All @@ -59,6 +68,7 @@
if (useServerSideEncryption) {
metadata.sseCustomerAlgorithm("AES256");
}
metadata.checksumAlgorithm(Objects.requireNonNullElse(checksumAlgorithm, ChecksumAlgorithm.CRC32));

Check warning on line 71 in src/main/java/hudson/plugins/s3/callable/S3BaseUploadCallable.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 71 is not covered by tests
};
Uploads.Metadata metadata = new Uploads.Metadata(builder);
metadata.setContentLength(contentLength);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/hudson/plugins/s3/callable/S3GzipCallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import hudson.plugins.s3.Uploads;
import hudson.util.Secret;
import org.apache.commons.io.IOUtils;
import software.amazon.awssdk.services.s3.model.ChecksumAlgorithm;
import software.amazon.awssdk.transfer.s3.model.Upload;
import software.amazon.awssdk.transfer.s3.progress.TransferListener;

Expand All @@ -23,7 +24,11 @@

public final class S3GzipCallable extends S3BaseUploadCallable implements MasterSlaveCallable<String> {
public S3GzipCallable(String accessKey, Secret secretKey, boolean useRole, Destination dest, Map<String, String> userMetadata, String storageClass, String selregion, boolean useServerSideEncryption, ProxyConfiguration proxy, boolean usePathStyle) {
super(accessKey, secretKey, useRole, dest, userMetadata, storageClass, selregion, useServerSideEncryption, proxy, usePathStyle);
this(accessKey, secretKey, useRole, dest, userMetadata, storageClass, selregion, useServerSideEncryption, proxy, usePathStyle, null);
}

public S3GzipCallable(String accessKey, Secret secretKey, boolean useRole, Destination dest, Map<String, String> userMetadata, String storageClass, String selregion, boolean useServerSideEncryption, ProxyConfiguration proxy, boolean usePathStyle, ChecksumAlgorithm checksumAlgorithm) {
super(accessKey, secretKey, useRole, dest, userMetadata, storageClass, selregion, useServerSideEncryption, proxy, usePathStyle, checksumAlgorithm);

Check warning on line 31 in src/main/java/hudson/plugins/s3/callable/S3GzipCallable.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 27-31 are not covered by tests
}

// Return a File containing the gzipped contents of the input file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hudson.plugins.s3.MD5;
import hudson.plugins.s3.Uploads;
import hudson.util.Secret;
import software.amazon.awssdk.services.s3.model.ChecksumAlgorithm;

import java.io.IOException;
import java.util.Map;
Expand All @@ -14,7 +15,11 @@
private static final long serialVersionUID = 1L;

public S3UploadCallable(String accessKey, Secret secretKey, boolean useRole, Destination dest, Map<String, String> userMetadata, String storageClass, String selregion, boolean useServerSideEncryption, ProxyConfiguration proxy, boolean usePathStyle) {
super(accessKey, secretKey, useRole, dest, userMetadata, storageClass, selregion, useServerSideEncryption, proxy, usePathStyle);
this(accessKey, secretKey, useRole, dest, userMetadata, storageClass, selregion, useServerSideEncryption, proxy, usePathStyle, null);
}

Check warning on line 19 in src/main/java/hudson/plugins/s3/callable/S3UploadCallable.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 18-19 are not covered by tests

public S3UploadCallable(String accessKey, Secret secretKey, boolean useRole, Destination dest, Map<String, String> userMetadata, String storageClass, String selregion, boolean useServerSideEncryption, ProxyConfiguration proxy, boolean usePathStyle, ChecksumAlgorithm checksumAlgorithm) {
super(accessKey, secretKey, useRole, dest, userMetadata, storageClass, selregion, useServerSideEncryption, proxy, usePathStyle, checksumAlgorithm);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/hudson/plugins/s3/S3Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.mockito.Mockito;
import software.amazon.awssdk.services.s3.model.ChecksumAlgorithm;

import java.io.IOException;
import java.util.Collections;
Expand Down Expand Up @@ -133,6 +134,7 @@ private S3Profile mockS3Profile(String profileName) throws IOException, Interrup
Mockito.anyBoolean(),
Mockito.anyBoolean(),
Mockito.anyBoolean(),
Mockito.any(ChecksumAlgorithm.class),
Mockito.anyInt()
)).thenReturn(newArrayList(new FingerprintRecord(true, "bucket", "path", "eu-west-1", "xxxx")));
return profile;
Expand Down
Loading