-
Notifications
You must be signed in to change notification settings - Fork 48
/
s3.go
241 lines (208 loc) · 8.53 KB
/
s3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package s3
import (
"context"
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/go-logr/logr"
"github.com/google/uuid"
configv1 "github.com/openshift/api/config/v1"
veleroInstallCR "github.com/openshift/managed-velero-operator/api/v1alpha2"
storageBase "github.com/openshift/managed-velero-operator/pkg/storage/base"
storageConstants "github.com/openshift/managed-velero-operator/pkg/storage/constants"
"sigs.k8s.io/controller-runtime/pkg/client"
)
type S3 struct {
Region string
InfraName string
}
type driver struct {
storageBase.Driver
Config *S3
}
// NewDriver creates a new s3 storage driver
// Used during bootstrapping
func NewDriver(ctx context.Context, cfg *configv1.InfrastructureStatus, clnt client.Client) *driver {
drv := driver{
Config: &S3{
Region: cfg.PlatformStatus.AWS.Region,
InfraName: cfg.InfrastructureName,
},
}
drv.Context = ctx
drv.KubeClient = clnt
return &drv
}
// GetPlatformType returns the platform type of this driver
func (d *driver) GetPlatformType() configv1.PlatformType {
return configv1.AWSPlatformType
}
// CreateStorage attempts to create an s3 bucket
// and apply any provided tags
func (d *driver) CreateStorage(reqLogger logr.Logger, instance *veleroInstallCR.VeleroInstall) error {
var err error
// Create an S3 client based on the region we received
s3Client, err := NewS3Client(d.KubeClient, d.Config.Region)
if err != nil {
return err
}
bucketLog := reqLogger.WithValues("StorageBucket.Name", instance.Status.StorageBucket.Name, "StorageBucket.Region", d.Config.Region)
// This switch handles the provisioning steps/checks
switch {
// We don't yet have a bucket name selected
case instance.Status.StorageBucket.Name == "":
err = setInstanceBucketName(d, s3Client, reqLogger, instance)
if err != nil {
return err
}
// We have a bucket name, but haven't kicked off provisioning of the bucket yet
case instance.Status.StorageBucket.Name != "" && !instance.Status.StorageBucket.Provisioned:
bucketLog.Info("S3 bucket defined, but not provisioned")
// Create S3 bucket
bucketLog.Info("Creating S3 Bucket")
err = CreateBucket(s3Client, instance.Status.StorageBucket.Name)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeBucketAlreadyExists:
bucketLog.Info("Bucket exists, but is not owned by current user; retrying")
instance.Status.StorageBucket.Name = ""
return instance.StatusUpdate(reqLogger, d.KubeClient)
case s3.ErrCodeBucketAlreadyOwnedByYou:
bucketLog.Info("Bucket exists, and is owned by current user; continue")
default:
return fmt.Errorf("error occurred when creating bucket %v: %v", instance.Status.StorageBucket.Name, aerr.Error())
}
} else {
return fmt.Errorf("error occurred when creating bucket %v: %v", instance.Status.StorageBucket.Name, err.Error())
}
}
err = TagBucket(s3Client, instance.Status.StorageBucket.Name, storageConstants.DefaultVeleroBackupStorageLocation, d.Config.InfraName)
if err != nil {
return fmt.Errorf("error occurred when tagging bucket %v: %v", instance.Status.StorageBucket.Name, err.Error())
}
}
// Verify S3 bucket exists
bucketLog.Info("Verifing S3 Bucket exists")
exists, err := d.StorageExists(instance.Status.StorageBucket.Name)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
return fmt.Errorf("error occurred when verifying bucket %v: %v", instance.Status.StorageBucket.Name, aerr.Error())
}
return fmt.Errorf("error occurred when verifying bucket %v: %v", instance.Status.StorageBucket.Name, err.Error())
}
if !exists {
bucketLog.Error(nil, "S3 bucket doesn't appear to exist")
instance.Status.StorageBucket.Provisioned = false
return instance.StatusUpdate(reqLogger, d.KubeClient)
}
// Encrypt S3 bucket
bucketLog.Info("Enforcing S3 Bucket encryption")
err = EncryptBucket(s3Client, instance.Status.StorageBucket.Name)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
return fmt.Errorf("error occurred when encrypting bucket %v: %v", instance.Status.StorageBucket.Name, aerr.Error())
}
return fmt.Errorf("error occurred when encrypting bucket %v: %v", instance.Status.StorageBucket.Name, err.Error())
}
// Block public access to S3 bucket
bucketLog.Info("Enforcing S3 Bucket public access policy")
err = BlockBucketPublicAccess(s3Client, instance.Status.StorageBucket.Name)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
return fmt.Errorf("error occurred when blocking public access to bucket %v: %v", instance.Status.StorageBucket.Name, aerr.Error())
}
return fmt.Errorf("error occurred when blocking public access to bucket %v: %v", instance.Status.StorageBucket.Name, err.Error())
}
// Configure lifecycle rules on S3 bucket
bucketLog.Info("Enforcing S3 Bucket lifecycle rules on S3 Bucket")
err = SetBucketLifecycle(s3Client, instance.Status.StorageBucket.Name)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
return fmt.Errorf("error occurred when configuring lifecycle rules on bucket %v: %v", instance.Status.StorageBucket.Name, aerr.Error())
}
return fmt.Errorf("error occurred when configuring lifecycle rules on bucket %v: %v", instance.Status.StorageBucket.Name, err.Error())
}
// Make sure that tags are applied to buckets
bucketLog.Info("Enforcing S3 Bucket tags on S3 Bucket")
err = TagBucket(s3Client, instance.Status.StorageBucket.Name, storageConstants.DefaultVeleroBackupStorageLocation, d.Config.InfraName)
if err != nil {
return fmt.Errorf("error occurred when tagging bucket %v: %v", instance.Status.StorageBucket.Name, err.Error())
}
instance.Status.StorageBucket.Provisioned = true
instance.Status.StorageBucket.LastSyncTimestamp = &metav1.Time{
Time: time.Now(),
}
return instance.StatusUpdate(reqLogger, d.KubeClient)
}
// StorageExists checks that the bucket exists, and that we have access to it.
func (d *driver) StorageExists(bucketName string) (bool, error) {
//create an S3 Client
s3Client, err := NewS3Client(d.KubeClient, d.Config.Region)
if err != nil {
return false, err
}
input := &s3.HeadBucketInput{
Bucket: aws.String(bucketName),
}
_, err = s3Client.HeadBucket(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
// This is supposed to say "NoSuchBucket", but actually emits "NotFound"
// https://github.com/aws/aws-sdk-go/issues/2593
case s3.ErrCodeNoSuchBucket, "NotFound":
return false, nil
default:
return false, fmt.Errorf("unable to determine bucket %v status: %v", bucketName, aerr.Error())
}
} else {
return false, fmt.Errorf("unable to determine bucket %v status: %v", bucketName, aerr.Error())
}
}
return true, nil
}
//generateBucketName generates a proposed name for the S3 Bucket
func generateBucketName(prefix string) string {
id := uuid.New().String()
return prefix + id
}
// setInstanceBucketName generates a bucket name for the S3 Bucket, tests to
// confirm if the bucket is accessible and then updates the instance status
// with the name
func setInstanceBucketName(d *driver, s3Client Client, reqLogger logr.Logger, instance *veleroInstallCR.VeleroInstall) error {
bucketLog := reqLogger.WithValues("StorageBucket.Name", instance.Status.StorageBucket.Name, "StorageBucket.Region", d.Config.Region)
// Use an existing bucket, if it exists.
bucketLog.Info("No S3 bucket defined. Searching for existing bucket to use")
bucketlist, err := ListBucketsInRegion(s3Client, d.Config.Region)
if err != nil {
return err
}
bucketinfo, err := ListBucketTags(s3Client, bucketlist.Buckets)
if err != nil {
return err
}
existingBucket := FindMatchingTags(bucketinfo, d.Config.InfraName)
if existingBucket != "" {
bucketLog.Info("Recovered existing bucket", "StorageBucket.Name", existingBucket)
instance.Status.StorageBucket.Name = existingBucket
instance.Status.StorageBucket.Provisioned = true
return instance.StatusUpdate(reqLogger, d.KubeClient)
}
// Prepare to create a new bucket, if none exist.
proposedName := generateBucketName(storageConstants.StorageBucketPrefix)
proposedBucketExists, err := DoesBucketExist(s3Client, proposedName)
if err != nil {
return err
}
if proposedBucketExists {
return fmt.Errorf("proposed bucket %s already exists, retrying", proposedName)
}
bucketLog.Info("Setting proposed bucket name", "StorageBucket.Name", proposedName)
instance.Status.StorageBucket.Name = proposedName
instance.Status.StorageBucket.Provisioned = false
return instance.StatusUpdate(reqLogger, d.KubeClient)
}