github.com/mweagle/Sparta@v1.15.0/aws/cloudformation/resources/s3ArtifactPublisherResource.go (about) 1 package resources 2 3 import ( 4 "bytes" 5 "encoding/json" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/session" 9 "github.com/aws/aws-sdk-go/service/s3" 10 gocf "github.com/mweagle/go-cloudformation" 11 "github.com/sirupsen/logrus" 12 ) 13 14 // S3ArtifactPublisherResourceRequest is what the UserProperties 15 // should be set to in the CustomResource invocation 16 type S3ArtifactPublisherResourceRequest struct { 17 Bucket *gocf.StringExpr 18 Key *gocf.StringExpr 19 Body map[string]interface{} 20 } 21 22 // S3ArtifactPublisherResource is a simple POC showing how to create custom resources 23 type S3ArtifactPublisherResource struct { 24 gocf.CloudFormationCustomResource 25 S3ArtifactPublisherResourceRequest 26 } 27 28 // IAMPrivileges returns the IAM privs for this custom action 29 func (command *S3ArtifactPublisherResource) IAMPrivileges() []string { 30 return []string{"s3:PutObject", 31 "s3:DeleteObject"} 32 } 33 34 // Create implements the S3 create operation 35 func (command S3ArtifactPublisherResource) Create(awsSession *session.Session, 36 event *CloudFormationLambdaEvent, 37 logger *logrus.Logger) (map[string]interface{}, error) { 38 39 unmarshalErr := json.Unmarshal(event.ResourceProperties, &command) 40 if unmarshalErr != nil { 41 return nil, unmarshalErr 42 } 43 mapData, mapDataErr := json.Marshal(command.Body) 44 if mapDataErr != nil { 45 return nil, mapDataErr 46 } 47 itemInput := bytes.NewReader(mapData) 48 s3PutObjectParams := &s3.PutObjectInput{ 49 Body: itemInput, 50 Bucket: aws.String(command.Bucket.Literal), 51 Key: aws.String(command.Key.Literal), 52 } 53 s3Svc := s3.New(awsSession) 54 s3Response, s3ResponseErr := s3Svc.PutObject(s3PutObjectParams) 55 if s3ResponseErr != nil { 56 return nil, s3ResponseErr 57 } 58 return map[string]interface{}{ 59 "ObjectVersion": s3Response.VersionId, 60 }, nil 61 } 62 63 // Update implements the S3 update operation 64 func (command S3ArtifactPublisherResource) Update(awsSession *session.Session, 65 event *CloudFormationLambdaEvent, 66 logger *logrus.Logger) (map[string]interface{}, error) { 67 return command.Create(awsSession, event, logger) 68 } 69 70 // Delete implements the S3 delete operation 71 func (command S3ArtifactPublisherResource) Delete(awsSession *session.Session, 72 event *CloudFormationLambdaEvent, 73 logger *logrus.Logger) (map[string]interface{}, error) { 74 75 unmarshalErr := json.Unmarshal(event.ResourceProperties, &command) 76 if unmarshalErr != nil { 77 return nil, unmarshalErr 78 } 79 s3DeleteObjectParams := &s3.DeleteObjectInput{ 80 Bucket: aws.String(command.Bucket.Literal), 81 Key: aws.String(command.Key.Literal), 82 } 83 s3Svc := s3.New(awsSession) 84 _, s3ResponseErr := s3Svc.DeleteObject(s3DeleteObjectParams) 85 if s3ResponseErr != nil { 86 return nil, s3ResponseErr 87 } 88 logger.WithFields(logrus.Fields{ 89 "Bucket": command.Bucket.Literal, 90 "Key": command.Key.Literal, 91 }).Info("Object deleted") 92 return nil, nil 93 }