|
| 1 | +package bucket |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/aws/aws-sdk-go/aws" |
| 7 | + "github.com/aws/aws-sdk-go/aws/awserr" |
| 8 | + "github.com/aws/aws-sdk-go/aws/session" |
| 9 | + "github.com/aws/aws-sdk-go/service/s3" |
| 10 | + "github.com/aws/aws-sdk-go/service/s3/s3iface" |
| 11 | + "github.com/openshift/oadp-operator/api/v1alpha1" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 13 | +) |
| 14 | + |
| 15 | +type awsConfigMap map[awsConfigKey]string |
| 16 | +type awsConfigKey string |
| 17 | + |
| 18 | +// Config Keys for AWS |
| 19 | +const ( |
| 20 | + region awsConfigKey = "region" |
| 21 | + s3ForcePathStyle awsConfigKey = "s3ForcePathStyle" |
| 22 | + s3Url awsConfigKey = "s3Url" |
| 23 | + publicUrl awsConfigKey = "publicUrl" |
| 24 | + serverSideEncryption awsConfigKey = "serverSideEncryption" |
| 25 | + kmsKeyID awsConfigKey = "kmsKeyID" |
| 26 | + signatureVersion awsConfigKey = "signatureVersion" |
| 27 | + profile awsConfigKey = "profile" |
| 28 | + insecureSkipTLSVerify awsConfigKey = "insecureSkipTLSVerify" |
| 29 | +) |
| 30 | + |
| 31 | +type awsBucketClient struct { |
| 32 | + bucket v1alpha1.CloudStorage |
| 33 | + client client.Client |
| 34 | +} |
| 35 | + |
| 36 | +func (a awsBucketClient) Exists() (bool, error) { |
| 37 | + s3Client, err := a.getS3Client() |
| 38 | + if err != nil { |
| 39 | + return false, err |
| 40 | + } |
| 41 | + input := &s3.HeadBucketInput{ |
| 42 | + Bucket: aws.String(a.bucket.Spec.Name), |
| 43 | + } |
| 44 | + _, err = s3Client.HeadBucket(input) |
| 45 | + if err != nil { |
| 46 | + if aerr, ok := err.(awserr.Error); ok { |
| 47 | + switch aerr.Code() { |
| 48 | + // This is supposed to say "NoSuchBucket", but actually emits "NotFound" |
| 49 | + // https://github.com/aws/aws-sdk-go/issues/2593 |
| 50 | + case s3.ErrCodeNoSuchBucket, "NotFound": |
| 51 | + return false, nil |
| 52 | + default: |
| 53 | + // Return true, because we are unable to detemine if bucket exists or not |
| 54 | + return true, fmt.Errorf("unable to determine bucket %v status: %v", a.bucket.Spec.Name, aerr.Error()) |
| 55 | + } |
| 56 | + } else { |
| 57 | + // Return true, because we are unable to detemine if bucket exists or not |
| 58 | + return true, fmt.Errorf("unable to determine bucket %v status: %v", a.bucket.Spec.Name, aerr.Error()) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + err = a.tagBucket() |
| 63 | + if err != nil { |
| 64 | + return true, err |
| 65 | + } |
| 66 | + |
| 67 | + return true, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (a awsBucketClient) Create() (bool, error) { |
| 71 | + s3Client, err := a.getS3Client() |
| 72 | + if err != nil { |
| 73 | + return false, err |
| 74 | + } |
| 75 | + createBucketInput := &s3.CreateBucketInput{ |
| 76 | + ACL: aws.String(s3.BucketCannedACLPrivate), |
| 77 | + Bucket: aws.String(a.bucket.Spec.Name), |
| 78 | + } |
| 79 | + if a.bucket.Spec.Region != "us-east-1" { |
| 80 | + createBucketConfiguration := &s3.CreateBucketConfiguration{ |
| 81 | + LocationConstraint: &a.bucket.Spec.Region, |
| 82 | + } |
| 83 | + createBucketInput.SetCreateBucketConfiguration(createBucketConfiguration) |
| 84 | + } |
| 85 | + if err := createBucketInput.Validate(); err != nil { |
| 86 | + return false, fmt.Errorf("unable to validate %v bucket creation configuration: %v", a.bucket.Spec.Name, err) |
| 87 | + } |
| 88 | + |
| 89 | + _, err = s3Client.CreateBucket(createBucketInput) |
| 90 | + if err != nil { |
| 91 | + return false, err |
| 92 | + } |
| 93 | + |
| 94 | + // tag Bucket. |
| 95 | + err = a.tagBucket() |
| 96 | + if err != nil { |
| 97 | + return true, err |
| 98 | + } |
| 99 | + |
| 100 | + return true, nil |
| 101 | +} |
| 102 | + |
| 103 | +func (a awsBucketClient) tagBucket() error { |
| 104 | + s3Client, err := a.getS3Client() |
| 105 | + // Clear bucket tags. |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + deleteInput := &s3.DeleteBucketTaggingInput{Bucket: aws.String(a.bucket.Spec.Name)} |
| 110 | + _, err = s3Client.DeleteBucketTagging(deleteInput) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + input := CreateBucketTaggingInput(a.bucket.Spec.Name, a.bucket.Spec.Tags) |
| 115 | + |
| 116 | + _, err = s3Client.PutBucketTagging(input) |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +// CreateBucketTaggingInput creates an S3 PutBucketTaggingInput object, |
| 124 | +// which is used to associate a list of tags with a bucket. |
| 125 | +func CreateBucketTaggingInput(bucketname string, tags map[string]string) *s3.PutBucketTaggingInput { |
| 126 | + putInput := &s3.PutBucketTaggingInput{ |
| 127 | + Bucket: aws.String(bucketname), |
| 128 | + Tagging: &s3.Tagging{ |
| 129 | + TagSet: []*s3.Tag{}, |
| 130 | + }, |
| 131 | + } |
| 132 | + for key, value := range tags { |
| 133 | + newTag := s3.Tag{ |
| 134 | + Key: aws.String(key), |
| 135 | + Value: aws.String(value), |
| 136 | + } |
| 137 | + putInput.Tagging.TagSet = append(putInput.Tagging.TagSet, &newTag) |
| 138 | + } |
| 139 | + return putInput |
| 140 | +} |
| 141 | + |
| 142 | +func (a awsBucketClient) getS3Client() (s3iface.S3API, error) { |
| 143 | + awsConfig := &aws.Config{Region: &a.bucket.Spec.Region} |
| 144 | + cred, err := getCredentialFromSecret(a) |
| 145 | + if err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + |
| 149 | + opts := session.Options{ |
| 150 | + Config: *awsConfig, |
| 151 | + SharedConfigFiles: []string{cred}, |
| 152 | + } |
| 153 | + |
| 154 | + if a.bucket.Spec.EnableSharedConfig != nil && *a.bucket.Spec.EnableSharedConfig { |
| 155 | + opts.SharedConfigState = session.SharedConfigEnable |
| 156 | + } |
| 157 | + |
| 158 | + s, err := session.NewSessionWithOptions(opts) |
| 159 | + if err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + return s3.New(s), nil |
| 163 | +} |
| 164 | + |
| 165 | +func (a awsBucketClient) ForceCredentialRefresh() error { |
| 166 | + return nil |
| 167 | +} |
| 168 | + |
| 169 | +func (a awsBucketClient) Delete() (bool, error) { |
| 170 | + s3Client, err := a.getS3Client() |
| 171 | + if err != nil { |
| 172 | + return false, err |
| 173 | + } |
| 174 | + deleteBucketInput := &s3.DeleteBucketInput{ |
| 175 | + Bucket: aws.String(a.getCloudStorage().Spec.Name), |
| 176 | + } |
| 177 | + |
| 178 | + if err := deleteBucketInput.Validate(); err != nil { |
| 179 | + return false, fmt.Errorf("unable to validate %v bucket deletion configuration: %v", a.getCloudStorage().Spec.Name, err) |
| 180 | + } |
| 181 | + |
| 182 | + _, err = s3Client.DeleteBucket(deleteBucketInput) |
| 183 | + if err != nil { |
| 184 | + return false, err |
| 185 | + } |
| 186 | + |
| 187 | + return true, nil |
| 188 | +} |
| 189 | + |
| 190 | +func (a awsBucketClient) getClient() client.Client { |
| 191 | + return a.client |
| 192 | +} |
| 193 | + |
| 194 | +func (a awsBucketClient) getCloudStorage() v1alpha1.CloudStorage { |
| 195 | + return a.bucket |
| 196 | +} |
0 commit comments