Skip to content

Commit

Permalink
Allow specifying a KMS Key ID for server-side encryption (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheImplementer authored Jun 14, 2024
1 parent d6952ef commit 6a6bbbc
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The AWS S3 build cache implementation has a few configuration options:
| `showStatisticsWhenSavingsExceeds` | Specifies minimum duration to trigger printing the stats, milliseconds | Yes | `100` |
| `showStatisticsWhenWasteExceeds` | Specifies minimum duration to trigger printing the stats, milliseconds | Yes | `100` |
| `showStatisticsWhenTransferExceeds` | Specifies minimum transfer size to trigger printing the stats, bytes | Yes | 10*1024*1024 |
| `kmsKeyId` | The ID of the KMS key to encrypt cache objects in S3. ([Using KMS Encryption](https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingKMSEncryption.html)) | no | |

Note: if both `awsAccessKeyId` and `awsSecretKey` are `nullOrBlank` (`null` or whitespace only), then anonymous credentials are used.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ open class AwsS3BuildCache : AbstractBuildCache() {
var region: String? = null
var bucket: String? = null
var prefix: String? = "cache/"
var kmsKeyId: String? = null
var maximumCachedObjectLength: Long = 50 * 1024 * 1024
var isReducedRedundancy = true
var endpoint: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class AwsS3BuildCacheService internal constructor(
s3Factory: () -> S3Client,
private val bucketName: String,
private val prefix: String?,
private val kmsKeyId: String?,
private val reducedRedundancy: Boolean,
private val maximumCachedObjectLength: Long,
private val showStatistics: Boolean,
Expand Down Expand Up @@ -245,6 +246,10 @@ class AwsS3BuildCacheService internal constructor(
{
it.bucket(bucketName)
it.key(bucketPath)
if (kmsKeyId != null) {
it.serverSideEncryption("aws:kms")
it.ssekmsKeyId(kmsKeyId)
}
it.contentLength(writer.size)
it.contentType(BUILD_CACHE_CONTENT_TYPE)
if (userMetadata != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class AwsS3BuildCacheServiceFactory : BuildCacheServiceFactory<AwsS3BuildCache>
describe("Bucket", config.bucket)
describe("Reduced Redundancy", config.isReducedRedundancy)
describe("Prefix", config.prefix)
describe("KMS Key ID", config.kmsKeyId)
describe("Endpoint", config.endpoint)
describe("Transfer Acceleration", config.transferAcceleration)
}
Expand All @@ -52,6 +53,7 @@ class AwsS3BuildCacheServiceFactory : BuildCacheServiceFactory<AwsS3BuildCache>
{ createS3Client(config) },
config.bucket!!,
config.prefix,
config.kmsKeyId,
config.isReducedRedundancy,
config.maximumCachedObjectLength,
config.showStatistics,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class RemoteCacheTest : BaseGradleTest() {
.withParameter("server.ssl.key-alias", "selfsigned")
.withParameter("server.ssl.key-password", "password")
.withParameter("server.ssl.key-store-password", "password")
.withParameter("com.adobe.testing.s3mock.domain.validKmsKeys", "arn:aws:kms:us-east-1:47110815:key/972393be-674f-4bdc-87ff-ea1b2588a1c6")
.withInitialBuckets(BUCKET_NAME)
.build()

Expand Down Expand Up @@ -130,6 +131,7 @@ class RemoteCacheTest : BaseGradleTest() {
region = 'eu-west-1'
bucket = '$BUCKET_NAME'
prefix = 'build-cache/'
kmsKeyId = '972393be-674f-4bdc-87ff-ea1b2588a1c6'
endpoint = 'localhost:${mockApp.port}'
// See https://github.com/adobe/S3Mock/issues/880
forcePathStyle = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,15 @@ class AwsS3BuildCacheServiceFactoryTest {
val service = subject.createBuildCacheService(conf, buildCacheDescriber)
Assertions.assertNotNull(service)
}

@Test
fun kmsKeyId() {
val conf = buildCache {
region = "us-west-1"
bucket = "my-bucket"
kmsKeyId = "972393be-674f-4bdc-87ff-ea1b2588a1c6"
}
val service = subject.createBuildCacheService(conf, buildCacheDescriber)
Assertions.assertNotNull(service)
}
}

0 comments on commit 6a6bbbc

Please sign in to comment.