@@ -12,6 +12,7 @@ import (
1212 "time"
1313
1414 "github.com/aws/aws-sdk-go-v2/service/s3"
15+ "github.com/aws/aws-sdk-go-v2/service/s3/types"
1516 "github.com/stretchr/testify/require"
1617
1718 "github.com/renderinc/render-auditlogs/pkg/auditlogs"
@@ -108,4 +109,100 @@ func TestUploadAuditLogs(t *testing.T) {
108109 require .Contains (t , err .Error (), "error uploading to S3" )
109110 require .Empty (t , s3URI )
110111 })
112+
113+ t .Run ("uses default SSE-S3 encryption when KMS not enabled" , func (t * testing.T ) {
114+ t .Parallel ()
115+ s3Client := & mockS3Client {
116+ putObjectFunc : func (ctx context.Context , params * s3.PutObjectInput , optFns ... func (* s3.Options )) (* s3.PutObjectOutput , error ) {
117+ require .Equal (t , "test-bucket" , * params .Bucket )
118+ require .Equal (t , types .ServerSideEncryptionAes256 , params .ServerSideEncryption )
119+ require .Nil (t , params .SSEKMSKeyId )
120+ require .Nil (t , params .BucketKeyEnabled )
121+ return & s3.PutObjectOutput {}, nil
122+ },
123+ }
124+
125+ uploader , err := aws .NewUploaderWithOptions (ctx , s3Client , "test-bucket" , "test-region" , aws.UploaderOptions {
126+ UseKMS : false ,
127+ })
128+ require .NoError (t , err )
129+
130+ s3URI , err := uploader .UploadAuditLogs (ctx , auditlogs .WorkspaceAuditLog , "workspace-123" , testData )
131+
132+ require .NoError (t , err )
133+ require .NotEmpty (t , s3URI )
134+ })
135+
136+ t .Run ("uses KMS encryption without specific key ID" , func (t * testing.T ) {
137+ t .Parallel ()
138+ s3Client := & mockS3Client {
139+ putObjectFunc : func (ctx context.Context , params * s3.PutObjectInput , optFns ... func (* s3.Options )) (* s3.PutObjectOutput , error ) {
140+ require .Equal (t , "test-bucket" , * params .Bucket )
141+ require .Equal (t , types .ServerSideEncryptionAwsKms , params .ServerSideEncryption )
142+ require .Nil (t , params .SSEKMSKeyId )
143+ require .Nil (t , params .BucketKeyEnabled )
144+ return & s3.PutObjectOutput {}, nil
145+ },
146+ }
147+
148+ uploader , err := aws .NewUploaderWithOptions (ctx , s3Client , "test-bucket" , "test-region" , aws.UploaderOptions {
149+ UseKMS : true ,
150+ })
151+ require .NoError (t , err )
152+
153+ s3URI , err := uploader .UploadAuditLogs (ctx , auditlogs .WorkspaceAuditLog , "workspace-123" , testData )
154+
155+ require .NoError (t , err )
156+ require .NotEmpty (t , s3URI )
157+ })
158+
159+ t .Run ("uses KMS encryption with specific key ID" , func (t * testing.T ) {
160+ t .Parallel ()
161+ s3Client := & mockS3Client {
162+ putObjectFunc : func (ctx context.Context , params * s3.PutObjectInput , optFns ... func (* s3.Options )) (* s3.PutObjectOutput , error ) {
163+ require .Equal (t , "test-bucket" , * params .Bucket )
164+ require .Equal (t , types .ServerSideEncryptionAwsKms , params .ServerSideEncryption )
165+ require .NotNil (t , params .SSEKMSKeyId )
166+ require .Equal (t , "arn:aws:kms:us-west-2:123456789012:key/12345678-1234-1234-1234-123456789012" , * params .SSEKMSKeyId )
167+ require .Nil (t , params .BucketKeyEnabled )
168+ return & s3.PutObjectOutput {}, nil
169+ },
170+ }
171+
172+ uploader , err := aws .NewUploaderWithOptions (ctx , s3Client , "test-bucket" , "test-region" , aws.UploaderOptions {
173+ UseKMS : true ,
174+ KMSKeyID : "arn:aws:kms:us-west-2:123456789012:key/12345678-1234-1234-1234-123456789012" ,
175+ })
176+ require .NoError (t , err )
177+
178+ s3URI , err := uploader .UploadAuditLogs (ctx , auditlogs .WorkspaceAuditLog , "workspace-123" , testData )
179+
180+ require .NoError (t , err )
181+ require .NotEmpty (t , s3URI )
182+ })
183+
184+ t .Run ("uses KMS encryption with bucket key enabled" , func (t * testing.T ) {
185+ t .Parallel ()
186+ s3Client := & mockS3Client {
187+ putObjectFunc : func (ctx context.Context , params * s3.PutObjectInput , optFns ... func (* s3.Options )) (* s3.PutObjectOutput , error ) {
188+ require .Equal (t , "test-bucket" , * params .Bucket )
189+ require .Equal (t , types .ServerSideEncryptionAwsKms , params .ServerSideEncryption )
190+ require .Nil (t , params .SSEKMSKeyId )
191+ require .NotNil (t , params .BucketKeyEnabled )
192+ require .True (t , * params .BucketKeyEnabled )
193+ return & s3.PutObjectOutput {}, nil
194+ },
195+ }
196+
197+ uploader , err := aws .NewUploaderWithOptions (ctx , s3Client , "test-bucket" , "test-region" , aws.UploaderOptions {
198+ UseKMS : true ,
199+ BucketKeyEnabled : true ,
200+ })
201+ require .NoError (t , err )
202+
203+ s3URI , err := uploader .UploadAuditLogs (ctx , auditlogs .WorkspaceAuditLog , "workspace-123" , testData )
204+
205+ require .NoError (t , err )
206+ require .NotEmpty (t , s3URI )
207+ })
111208}
0 commit comments