-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathapistore.go
140 lines (122 loc) · 4.8 KB
/
apistore.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
package google
import (
"github.com/lytics/cloudstorage"
"google.golang.org/api/storage/v1"
)
// APIStore a google api store
type APIStore struct {
service *storage.Service
project string
}
// NewAPIStore create api store.
func NewAPIStore(conf *cloudstorage.Config) (*APIStore, error) {
googleClient, err := NewGoogleClient(conf)
if err != nil {
return nil, err
}
service, err := storage.New(googleClient.Client())
if err != nil {
return nil, err
}
return &APIStore{service: service, project: conf.Project}, nil
}
// BucketExists checks for the bucket name
func (c *APIStore) BucketExists(name string) bool {
b, err := c.service.Buckets.Get(name).Do()
if err != nil {
return false
}
return b.Id != ""
}
// CreateBucket creates a new bucket in GCS
func (c *APIStore) CreateBucket(name string) error {
return c.CreateBucketWithLocation(name, "")
}
// CreateBucketWithLocation creates a new bucket in GCS with the specified location
func (c *APIStore) CreateBucketWithLocation(name, location string) error {
bucket := &storage.Bucket{Name: name, Location: location}
_, err := c.service.Buckets.Insert(c.project, bucket).Do()
return err
}
// AddOwner adds entity as a owner of the object
func (c *APIStore) AddOwner(bucket, object, entity string) error {
ac := &storage.ObjectAccessControl{Entity: entity, Role: "OWNER"}
_, err := c.service.ObjectAccessControls.Insert(bucket, object, ac).Do()
return err
}
// AddReader adds enitty as a reader of the object
func (c *APIStore) AddReader(bucket, object, entity string) error {
ac := &storage.ObjectAccessControl{Entity: entity, Role: "READER"}
_, err := c.service.ObjectAccessControls.Insert(bucket, object, ac).Do()
return err
}
// AddBucketReader updates the bucket ACL to add entity as a reader on the bucket
// The bucket must be in fine-grained access control mode, or this will produce an error
func (c *APIStore) AddBucketReader(bucket, entity string) error {
ac := &storage.BucketAccessControl{Entity: entity, Role: "READER"}
_, err := c.service.BucketAccessControls.Insert(bucket, ac).Do()
return err
}
// AddBucketWriter updates the bucket ACL to add entity as a writer on the bucket
// The bucket must be in fine-grained access control mode, or this will produce an error
func (c *APIStore) AddBucketWriter(bucket, entity string) error {
ac := &storage.BucketAccessControl{Entity: entity, Role: "WRITER"}
_, err := c.service.BucketAccessControls.Insert(bucket, ac).Do()
return err
}
// SetBucketAgeLifecycle updates a bucket-level lifecycle policy for object age in days
func (c *APIStore) SetBucketAgeLifecycle(name string, days int64) error {
bucket := &storage.Bucket{Name: name}
bucket.Lifecycle = new(storage.BucketLifecycle)
action := &storage.BucketLifecycleRuleAction{Type: "Delete"}
condition := &storage.BucketLifecycleRuleCondition{Age: &days}
bucket.Lifecycle.Rule = make([]*storage.BucketLifecycleRule, 1)
bucket.Lifecycle.Rule[0] = &storage.BucketLifecycleRule{Action: action, Condition: condition}
_, err := c.service.Buckets.Patch(name, bucket).Do()
return err
}
// GrantObjectViewer updates the IAM policy on the bucket to grant member the roles/storage.objectViewer role
// The existing policy attributes on the bucket are preserved
func (c *APIStore) GrantObjectViewer(bucket, member string) error {
return c.grantRole(bucket, member, "roles/storage.objectViewer")
}
// GrantObjectCreator updates the IAM policy on the bucket to grant member the roles/storage.objectCreator role
// The existing policy attributes on the bucket are preserved
func (c *APIStore) GrantObjectCreator(bucket, member string) error {
return c.grantRole(bucket, member, "roles/storage.objectCreator")
}
// GrantObjectAdmin updates the IAM policy on the bucket to grant member the roles/storage.objectAdmin role
// The existing policy attributes on the bucket are preserved
func (c *APIStore) GrantObjectAdmin(bucket, member string) error {
return c.grantRole(bucket, member, "roles/storage.objectAdmin")
}
// grantRole updates the IAM policy for @bucket in order to rant @role to @member
// we have to retrieve the existing policy in order to modify it, per https://cloud.google.com/storage/docs/json_api/v1/buckets/setIamPolicy
func (c *APIStore) grantRole(bucket, member, role string) error {
existingPolicy, err := c.service.Buckets.GetIamPolicy(bucket).Do()
if err != nil {
return err
}
var added bool
for _, b := range existingPolicy.Bindings {
if b.Role == role {
for _, m := range b.Members {
if m == member {
// already granted
return nil
}
}
b.Members = append(b.Members, member)
added = true
break
}
}
if !added {
b := new(storage.PolicyBindings)
b.Role = role
b.Members = []string{member}
existingPolicy.Bindings = append(existingPolicy.Bindings, b)
}
_, err = c.service.Buckets.SetIamPolicy(bucket, existingPolicy).Do()
return err
}