forked from OffchainLabs/nitro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_cloud_storage_service.go
205 lines (178 loc) · 7.24 KB
/
google_cloud_storage_service.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
package das
import (
"context"
"fmt"
"io"
"math"
"sort"
"time"
googlestorage "cloud.google.com/go/storage"
"github.com/google/go-cmp/cmp"
flag "github.com/spf13/pflag"
"google.golang.org/api/option"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/offchainlabs/nitro/arbstate/daprovider"
"github.com/offchainlabs/nitro/das/dastree"
"github.com/offchainlabs/nitro/util/pretty"
)
type GoogleCloudStorageOperator interface {
Bucket(name string) *googlestorage.BucketHandle
Upload(ctx context.Context, bucket, objectPrefix string, value []byte) error
Download(ctx context.Context, bucket, objectPrefix string, key common.Hash) ([]byte, error)
Close(ctx context.Context) error
}
type GoogleCloudStorageClient struct {
client *googlestorage.Client
}
func (g *GoogleCloudStorageClient) Bucket(name string) *googlestorage.BucketHandle {
return g.client.Bucket(name)
}
func (g *GoogleCloudStorageClient) Upload(ctx context.Context, bucket, objectPrefix string, value []byte) error {
obj := g.client.Bucket(bucket).Object(objectPrefix + EncodeStorageServiceKey(dastree.Hash(value)))
w := obj.NewWriter(ctx)
if _, err := fmt.Fprintln(w, value); err != nil {
return err
}
return w.Close()
}
func (g *GoogleCloudStorageClient) Download(ctx context.Context, bucket, objectPrefix string, key common.Hash) ([]byte, error) {
obj := g.client.Bucket(bucket).Object(objectPrefix + EncodeStorageServiceKey(key))
reader, err := obj.NewReader(ctx)
if err != nil {
return nil, err
}
return io.ReadAll(reader)
}
func (g *GoogleCloudStorageClient) Close(ctx context.Context) error {
return g.client.Close()
}
type GoogleCloudStorageServiceConfig struct {
Enable bool `koanf:"enable"`
AccessToken string `koanf:"access-token"`
Bucket string `koanf:"bucket"`
ObjectPrefix string `koanf:"object-prefix"`
EnableExpiry bool `koanf:"enable-expiry"`
MaxRetention time.Duration `koanf:"max-retention"`
}
var DefaultGoogleCloudStorageServiceConfig = GoogleCloudStorageServiceConfig{}
func GoogleCloudConfigAddOptions(prefix string, f *flag.FlagSet) {
f.Bool(prefix+".enable", DefaultGoogleCloudStorageServiceConfig.Enable, "EXPERIMENTAL/unsupported - enable storage/retrieval of sequencer batch data from an Google Cloud Storage bucket")
f.String(prefix+".access-token", DefaultGoogleCloudStorageServiceConfig.AccessToken, "Google Cloud Storage access token")
f.String(prefix+".bucket", DefaultGoogleCloudStorageServiceConfig.Bucket, "Google Cloud Storage bucket")
f.String(prefix+".object-prefix", DefaultGoogleCloudStorageServiceConfig.ObjectPrefix, "prefix to add to Google Cloud Storage objects")
f.Bool(prefix+".enable-expiry", DefaultLocalFileStorageConfig.EnableExpiry, "enable expiry of batches")
f.Duration(prefix+".max-retention", DefaultLocalFileStorageConfig.MaxRetention, "store requests with expiry times farther in the future than max-retention will be rejected")
}
type GoogleCloudStorageService struct {
operator GoogleCloudStorageOperator
bucket string
objectPrefix string
enableExpiry bool
maxRetention time.Duration
}
func NewGoogleCloudStorageService(config GoogleCloudStorageServiceConfig) (StorageService, error) {
var client *googlestorage.Client
var err error
// Note that if the credentials are not specified, the client library will find credentials using ADC(Application Default Credentials)
// https://cloud.google.com/docs/authentication/provide-credentials-adc.
if config.AccessToken == "" {
client, err = googlestorage.NewClient(context.Background())
} else {
client, err = googlestorage.NewClient(context.Background(), option.WithCredentialsJSON([]byte(config.AccessToken)))
}
if err != nil {
return nil, fmt.Errorf("error creating Google Cloud Storage client: %w", err)
}
service := &GoogleCloudStorageService{
operator: &GoogleCloudStorageClient{client: client},
bucket: config.Bucket,
objectPrefix: config.ObjectPrefix,
enableExpiry: config.EnableExpiry,
maxRetention: config.MaxRetention,
}
if config.EnableExpiry {
lifecycleRule := googlestorage.LifecycleRule{
Action: googlestorage.LifecycleAction{Type: "Delete"},
Condition: googlestorage.LifecycleCondition{AgeInDays: int64(config.MaxRetention.Hours() / 24)}, // Objects older than 30 days
}
ctx := context.Background()
bucket := service.operator.Bucket(service.bucket)
// check if bucket exists (and others), and update expiration policy if enabled
attrs, err := bucket.Attrs(ctx)
if err != nil {
return nil, fmt.Errorf("error getting bucket attributes: %w", err)
}
attrs.Lifecycle.Rules = append(attrs.Lifecycle.Rules, lifecycleRule)
bucketAttrsToUpdate := googlestorage.BucketAttrsToUpdate{
Lifecycle: &attrs.Lifecycle,
}
if _, err := bucket.Update(ctx, bucketAttrsToUpdate); err != nil {
return nil, fmt.Errorf("failed to update bucket lifecycle: %w", err)
}
}
return service, nil
}
func (gcs *GoogleCloudStorageService) Put(ctx context.Context, data []byte, expiry uint64) error {
logPut("das.GoogleCloudStorageService.Store", data, expiry, gcs)
if expiry > math.MaxInt64 {
return fmt.Errorf("request expiry time (%v) exceeds max int64", expiry)
}
// #nosec G115
expiryTime := time.Unix(int64(expiry), 0)
currentTimePlusRetention := time.Now().Add(gcs.maxRetention)
if expiryTime.After(currentTimePlusRetention) {
return fmt.Errorf("requested expiry time (%v) exceeds current time plus maximum allowed retention period(%v)", expiryTime, currentTimePlusRetention)
}
if err := gcs.operator.Upload(ctx, gcs.bucket, gcs.objectPrefix, data); err != nil {
log.Error("das.GoogleCloudStorageService.Store", "err", err)
return err
}
return nil
}
func (gcs *GoogleCloudStorageService) GetByHash(ctx context.Context, key common.Hash) ([]byte, error) {
log.Trace("das.GoogleCloudStorageService.GetByHash", "key", pretty.PrettyHash(key), "this", gcs)
buf, err := gcs.operator.Download(ctx, gcs.bucket, gcs.objectPrefix, key)
if err != nil {
log.Error("das.GoogleCloudStorageService.GetByHash", "err", err)
return nil, err
}
return buf, nil
}
func (gcs *GoogleCloudStorageService) ExpirationPolicy(ctx context.Context) (daprovider.ExpirationPolicy, error) {
if gcs.enableExpiry {
return daprovider.KeepForever, nil
}
return daprovider.DiscardAfterDataTimeout, nil
}
func (gcs *GoogleCloudStorageService) Sync(ctx context.Context) error {
return nil
}
func (gcs *GoogleCloudStorageService) Close(ctx context.Context) error {
return gcs.operator.Close(ctx)
}
func (gcs *GoogleCloudStorageService) String() string {
return fmt.Sprintf("GoogleCloudStorageService(:%s)", gcs.bucket)
}
func (gcs *GoogleCloudStorageService) HealthCheck(ctx context.Context) error {
bucket := gcs.operator.Bucket(gcs.bucket)
// check if we have bucket permissions
permissions := []string{
"storage.buckets.get",
"storage.buckets.list",
"storage.objects.create",
"storage.objects.delete",
"storage.objects.list",
"storage.objects.get",
}
perms, err := bucket.IAM().TestPermissions(ctx, permissions)
if err != nil {
return fmt.Errorf("could not check permissions: %w", err)
}
sort.Strings(permissions)
sort.Strings(perms)
if !cmp.Equal(perms, permissions) {
return fmt.Errorf("permissions mismatch (-want +got):\n%s", cmp.Diff(permissions, perms))
}
return nil
}