-
Notifications
You must be signed in to change notification settings - Fork 195
/
bucket.go
247 lines (210 loc) · 7.46 KB
/
bucket.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
242
243
244
245
246
247
// Copyright 2023 Google LLC
//
// 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 mockstorage
import (
"context"
"fmt"
"net/http"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/httpmux"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/storage/v1"
"github.com/golang/protobuf/ptypes/empty"
)
type buckets struct {
*MockService
pb.UnimplementedBucketsServerServer
}
func (s *buckets) GetBucket(ctx context.Context, req *pb.GetBucketRequest) (*pb.Bucket, error) {
fqn := req.GetName()
obj := &pb.Bucket{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
}
ret := proto.Clone(obj).(*pb.Bucket)
projection := req.GetProjection()
if projection == "" {
projection = "noAcl"
}
switch projection {
case "full":
// full: Include all properties.
// TODO: Verify storage.buckets.getIamPolicy permission (one day!)
case "noAcl":
// noAcl: Omit owner, acl, and defaultObjectAcl properties.
ret.Acl = nil
ret.DefaultObjectAcl = nil
ret.Owner = nil
default:
return nil, status.Errorf(codes.InvalidArgument, "invalid projection: %s", projection)
}
httpmux.SetExpiresHeader(ctx, time.Now())
return ret, nil
}
func (s *buckets) InsertBucket(ctx context.Context, req *pb.InsertBucketRequest) (*pb.Bucket, error) {
fqn := req.GetBucket().GetName()
if req.GetProject() == "" {
return nil, status.Errorf(codes.InvalidArgument, "project is required")
}
project, err := s.Projects.GetProjectByID(req.GetProject())
if err != nil {
return nil, err
}
now := timestamppb.Now()
obj := proto.Clone(req.GetBucket()).(*pb.Bucket)
obj.Id = PtrTo(fqn)
obj.Kind = PtrTo("storage#bucket")
obj.Name = PtrTo(fqn)
obj.ProjectNumber = PtrTo(uint64(project.Number))
obj.Location = PtrTo("US")
obj.LocationType = PtrTo("multi-region")
obj.Rpo = PtrTo("DEFAULT")
obj.SelfLink = PtrTo(fmt.Sprintf("https://www.googleapis.com/storage/v1/b/%s", obj.GetName()))
obj.StorageClass = PtrTo("STANDARD")
obj.TimeCreated = now
obj.Updated = now
obj.Metageneration = PtrTo(int64(1))
obj.Etag = PtrTo(computeEtag(obj))
iamConfiguration := obj.IamConfiguration
if iamConfiguration == nil {
iamConfiguration = &pb.BucketIamConfiguration{}
obj.IamConfiguration = iamConfiguration
}
if iamConfiguration.PublicAccessPrevention == nil {
iamConfiguration.PublicAccessPrevention = PtrTo("inherited")
}
bucketPolicyOnly := iamConfiguration.BucketPolicyOnly
if bucketPolicyOnly == nil {
bucketPolicyOnly = &pb.BucketPolicyOnly{}
iamConfiguration.BucketPolicyOnly = bucketPolicyOnly
}
if bucketPolicyOnly.Enabled == nil {
bucketPolicyOnly.Enabled = PtrTo(false)
}
ubla := iamConfiguration.UniformBucketLevelAccess
if ubla == nil {
ubla = &pb.UniformBucketLevelAccess{}
iamConfiguration.UniformBucketLevelAccess = ubla
}
softDeletePolicy := obj.SoftDeletePolicy
if softDeletePolicy == nil {
softDeletePolicy = &pb.BucketSoftDeletePolicy{}
obj.SoftDeletePolicy = softDeletePolicy
}
if softDeletePolicy.RetentionDurationSeconds == nil {
defaultRetention := time.Hour * 7 * 24
softDeletePolicy.RetentionDurationSeconds = PtrTo(int64(defaultRetention.Seconds()))
}
softDeletePolicy.EffectiveTime = now
if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, err
}
return obj, nil
}
func (s *buckets) populateDefaults(ctx context.Context, project *projects.ProjectData, obj *pb.Bucket) error {
owner := obj.Owner
if owner == nil {
owner = &pb.BucketOwner{}
obj.Owner = owner
}
if owner.Entity == nil {
owner.Entity = PtrTo(fmt.Sprintf("project-owners-%d", project.Number))
}
if len(obj.Acl) == 0 {
obj.Acl = append(obj.Acl, buildBucketACL(ctx, project, obj, "OWNER", "owners"))
obj.Acl = append(obj.Acl, buildBucketACL(ctx, project, obj, "OWNER", "editors"))
obj.Acl = append(obj.Acl, buildBucketACL(ctx, project, obj, "READER", "viewers"))
}
if len(obj.DefaultObjectAcl) == 0 {
obj.DefaultObjectAcl = append(obj.DefaultObjectAcl, buildObjectACL(ctx, project, obj, "OWNER", "owners"))
obj.DefaultObjectAcl = append(obj.DefaultObjectAcl, buildObjectACL(ctx, project, obj, "OWNER", "editors"))
obj.DefaultObjectAcl = append(obj.DefaultObjectAcl, buildObjectACL(ctx, project, obj, "READER", "viewers"))
}
return nil
}
func buildBucketACL(ctx context.Context, project *projects.ProjectData, obj *pb.Bucket, role string, team string) *pb.BucketAccessControl {
acl := &pb.BucketAccessControl{}
acl.Bucket = PtrTo(obj.GetName())
acl.Entity = PtrTo(fmt.Sprintf("project-%s-%d", team, project.Number))
acl.Role = PtrTo(role)
acl.Id = PtrTo(acl.GetBucket() + "/" + acl.GetEntity())
acl.Kind = PtrTo("storage#bucketAccessControl")
acl.SelfLink = PtrTo(fmt.Sprintf("https://www.googleapis.com/storage/v1/b/%s/acl/%s", obj.GetName(), acl.GetEntity()))
acl.ProjectTeam = &pb.BucketAccessControlProjectTeam{
ProjectNumber: PtrTo(fmt.Sprintf("%d", project.Number)),
Team: PtrTo(team),
}
acl.Etag = PtrTo(computeEtag(obj))
return acl
}
func buildObjectACL(ctx context.Context, project *projects.ProjectData, obj *pb.Bucket, role string, team string) *pb.ObjectAccessControl {
acl := &pb.ObjectAccessControl{}
acl.Entity = PtrTo(fmt.Sprintf("project-%s-%d", team, project.Number))
acl.Role = PtrTo(role)
acl.Kind = PtrTo("storage#objectAccessControl")
acl.ProjectTeam = &pb.ObjectAccessControlProjectTeam{
ProjectNumber: PtrTo(fmt.Sprintf("%d", project.Number)),
Team: PtrTo(team),
}
acl.Etag = PtrTo(computeEtag(obj))
return acl
}
func (s *buckets) PatchBucket(ctx context.Context, req *pb.PatchBucketRequest) (*pb.Bucket, error) {
fqn := req.GetName()
obj := &pb.Bucket{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
}
project, err := s.Projects.GetProjectByNumber(fmt.Sprintf("%d", obj.GetProjectNumber()))
if err != nil {
return nil, err
}
if patch := req.Bucket; patch != nil {
if patch.Labels != nil {
obj.Labels = patch.Labels
}
if patch.Lifecycle != nil {
obj.Lifecycle = patch.Lifecycle
}
if patch.Versioning != nil {
obj.Versioning = patch.Versioning
}
}
// Remove empty lifecycle (no rules)
if obj.Lifecycle != nil && proto.Equal(obj.Lifecycle, &pb.BucketLifecycle{}) {
obj.Lifecycle = nil
}
obj.Metageneration = PtrTo(int64(obj.GetMetageneration() + 1))
if err := s.populateDefaults(ctx, project, obj); err != nil {
return nil, err
}
obj.Etag = PtrTo(computeEtag(obj))
if err := s.storage.Update(ctx, fqn, obj); err != nil {
return nil, err
}
return obj, nil
}
func (s *buckets) DeleteBucket(ctx context.Context, req *pb.DeleteBucketRequest) (*empty.Empty, error) {
fqn := req.GetName()
deletedObj := &pb.Bucket{}
if err := s.storage.Delete(ctx, fqn, deletedObj); err != nil {
return nil, err
}
httpmux.SetStatusCode(ctx, http.StatusNoContent)
return &empty.Empty{}, nil
}