-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
storage.go
96 lines (77 loc) · 2.42 KB
/
storage.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
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gce
import (
"context"
"fmt"
storage "google.golang.org/api/storage/v1"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/acls"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi/cloudup"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/util/pkg/vfs"
)
// gcsAclStrategy is the AclStrategy for objects written to google cloud storage
type gcsAclStrategy struct{}
var _ acls.ACLStrategy = &gcsAclStrategy{}
// GetACL returns the ACL to use if this is a google cloud storage path
func (s *gcsAclStrategy) GetACL(ctx context.Context, p vfs.Path, cluster *kops.Cluster) (vfs.ACL, error) {
if cluster.Spec.GetCloudProvider() != kops.CloudProviderGCE {
return nil, nil
}
gcsPath, ok := p.(*vfs.GSPath)
if !ok {
return nil, nil
}
bucketName := gcsPath.Bucket()
client, err := gcsPath.Client(ctx)
if err != nil {
return nil, err
}
// TODO: Cache?
bucket, err := client.Buckets.Get(bucketName).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("error querying bucket %q: %v", bucketName, err)
}
bucketPolicyOnly := false
if bucket.IamConfiguration != nil && bucket.IamConfiguration.BucketPolicyOnly != nil {
bucketPolicyOnly = bucket.IamConfiguration.BucketPolicyOnly.Enabled
}
if bucketPolicyOnly {
klog.V(8).Infof("bucket gs://%s has bucket-policy only; won't try to set ACLs", bucketName)
return nil, nil
}
// TODO: Cache?
cloud, err := cloudup.BuildCloud(cluster)
if err != nil {
return nil, err
}
serviceAccount, err := cloud.(gce.GCECloud).ServiceAccount()
if err != nil {
return nil, err
}
var acls []*storage.ObjectAccessControl
acls = append(acls, bucket.DefaultObjectAcl...)
acls = append(acls, &storage.ObjectAccessControl{
Email: serviceAccount,
Entity: "user-" + serviceAccount,
Role: "READER",
})
return &vfs.GSAcl{
Acl: acls,
}, nil
}
func Register() {
acls.RegisterPlugin("k8s.io/kops/acl/gce", &gcsAclStrategy{})
}