@@ -24,12 +24,53 @@ type S3Client interface {
2424 DeleteObject (ctx context.Context , params * s3.DeleteObjectInput , optFns ... func (* s3.Options )) (* s3.DeleteObjectOutput , error )
2525}
2626
27+ type AwsS3EncryptionType string
28+
29+ const (
30+ ServerSideEncryptionAes256 AwsS3EncryptionType = "AES256"
31+ ServerSideEncryptionAwsKms AwsS3EncryptionType = "aws:kms"
32+ )
33+
2734type PlanStorageAWS struct {
28- Client S3Client
29- Bucket string
30- Context context.Context
35+ Client S3Client
36+ Bucket string
37+ Context context.Context
38+ EncryptionEnabled bool
39+ EncryptionType AwsS3EncryptionType
40+ KMSEncryptionId string
3141}
3242
43+ func NewAWSPlanStorage (bucketName string , encryptionEnabled bool , encryptionType string , KMSEncryptionId string ) (* PlanStorageAWS , error ) {
44+ if bucketName == "" {
45+ return nil , fmt .Errorf ("AWS_S3_BUCKET is not defined" )
46+ }
47+ ctx , client , err := GetAWSStorageClient ()
48+ if err != nil {
49+ return nil , fmt .Errorf ("could not retrieve aws storage client" )
50+ }
51+ planStorage := & PlanStorageAWS {
52+ Context : ctx ,
53+ Client : client ,
54+ Bucket : bucketName ,
55+ }
56+ if encryptionEnabled {
57+ planStorage .EncryptionEnabled = true
58+ if encryptionType == "AES256" {
59+ planStorage .EncryptionType = ServerSideEncryptionAes256
60+ } else if encryptionType == "KMS" {
61+ if KMSEncryptionId == "" {
62+ return nil , fmt .Errorf ("KMS encryption requested but no KMS key specified" )
63+ }
64+ planStorage .EncryptionType = ServerSideEncryptionAwsKms
65+ planStorage .KMSEncryptionId = KMSEncryptionId
66+ } else {
67+ return nil , fmt .Errorf ("unknown encryption type specified for aws plan bucket: %v" , encryptionType )
68+ }
69+ }
70+
71+ return planStorage , nil
72+
73+ }
3374func (psa * PlanStorageAWS ) PlanExists (artifactName , storedPlanFilePath string ) (bool , error ) {
3475 input := & s3.HeadObjectInput {
3576 Bucket : aws .String (psa .Bucket ),
@@ -59,6 +100,15 @@ func (psa *PlanStorageAWS) StorePlanFile(fileContents []byte, artifactName, file
59100 Bucket : aws .String (psa .Bucket ),
60101 Key : aws .String (fileName ),
61102 }
103+
104+ // support for encryption
105+ if psa .EncryptionEnabled {
106+ input .ServerSideEncryption = types .ServerSideEncryption (psa .EncryptionType )
107+ if psa .EncryptionType == ServerSideEncryptionAwsKms {
108+ input .SSEKMSKeyId = aws .String (psa .KMSEncryptionId )
109+ }
110+ }
111+
62112 _ , err := psa .Client .PutObject (psa .Context , input )
63113 if err != nil {
64114 log .Printf ("Failed to write file to bucket: %v" , err )
0 commit comments