-
Notifications
You must be signed in to change notification settings - Fork 111
/
gcs.go
331 lines (279 loc) · 9.99 KB
/
gcs.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package gcs
import (
"context"
"errors"
"fmt"
"math"
"net/http"
"github.com/bmatcuk/doublestar/v4"
"github.com/c2h5oh/datasize"
"github.com/mitchellh/mapstructure"
"github.com/rilldata/rill/runtime/drivers"
rillblob "github.com/rilldata/rill/runtime/drivers/blob"
"github.com/rilldata/rill/runtime/pkg/activity"
"github.com/rilldata/rill/runtime/pkg/gcputil"
"github.com/rilldata/rill/runtime/pkg/globutil"
"go.uber.org/zap"
"gocloud.dev/blob/gcsblob"
"gocloud.dev/gcp"
"golang.org/x/oauth2"
"google.golang.org/api/googleapi"
)
const defaultPageSize = 20
func init() {
drivers.Register("gcs", driver{})
drivers.RegisterAsConnector("gcs", driver{})
}
var spec = drivers.Spec{
DisplayName: "Google Cloud Storage",
Description: "Connect to Google Cloud Storage.",
DocsURL: "https://docs.rilldata.com/reference/connectors/gcs",
ConfigProperties: []*drivers.PropertySpec{
{
Key: "google_application_credentials",
Type: drivers.FilePropertyType,
Hint: "Enter path of file to load from.",
},
},
SourceProperties: []*drivers.PropertySpec{
{
Key: "path",
Type: drivers.StringPropertyType,
DisplayName: "GS URI",
Description: "Path to file on the disk.",
Placeholder: "gs://bucket-name/path/to/file.csv",
Required: true,
Hint: "Glob patterns are supported",
},
{
Key: "gcp.credentials",
Type: drivers.InformationalPropertyType,
DisplayName: "GCP credentials",
Description: "GCP credentials inferred from your local environment.",
Hint: "Set your local credentials: <code>gcloud auth application-default login</code> Click to learn more.",
DocsURL: "https://docs.rilldata.com/reference/connectors/gcs#local-credentials",
},
},
ImplementsObjectStore: true,
}
type driver struct{}
type configProperties struct {
SecretJSON string `mapstructure:"google_application_credentials"`
AllowHostAccess bool `mapstructure:"allow_host_access"`
}
func (d driver) Open(instanceID string, config map[string]any, client *activity.Client, logger *zap.Logger) (drivers.Handle, error) {
if instanceID == "" {
return nil, errors.New("gcs driver can't be shared")
}
conf := &configProperties{}
err := mapstructure.WeakDecode(config, conf)
if err != nil {
return nil, err
}
conn := &Connection{
config: conf,
logger: logger,
}
return conn, nil
}
func (d driver) Spec() drivers.Spec {
return spec
}
func (d driver) HasAnonymousSourceAccess(ctx context.Context, src map[string]any, logger *zap.Logger) (bool, error) {
conf, err := parseSourceProperties(src)
if err != nil {
return false, fmt.Errorf("failed to parse config: %w", err)
}
client := gcp.NewAnonymousHTTPClient(gcp.DefaultTransport())
bucketObj, err := gcsblob.OpenBucket(ctx, client, conf.url.Host, nil)
if err != nil {
return false, fmt.Errorf("failed to open bucket %q, %w", conf.url.Host, err)
}
return bucketObj.IsAccessible(ctx)
}
func (d driver) TertiarySourceConnectors(ctx context.Context, src map[string]any, logger *zap.Logger) ([]string, error) {
return nil, nil
}
type sourceProperties struct {
Path string `mapstructure:"path"`
URI string `mapstructure:"uri"`
Extract map[string]any `mapstructure:"extract"`
GlobMaxTotalSize int64 `mapstructure:"glob.max_total_size"`
GlobMaxObjectsMatched int `mapstructure:"glob.max_objects_matched"`
GlobMaxObjectsListed int64 `mapstructure:"glob.max_objects_listed"`
GlobPageSize int `mapstructure:"glob.page_size"`
BatchSize string `mapstructure:"batch_size"`
url *globutil.URL
extractPolicy *rillblob.ExtractPolicy
}
func parseSourceProperties(props map[string]any) (*sourceProperties, error) {
conf := &sourceProperties{}
err := mapstructure.WeakDecode(props, conf)
if err != nil {
return nil, err
}
// Backwards compatibility for "uri" renamed to "path"
if conf.URI != "" {
conf.Path = conf.URI
}
if !doublestar.ValidatePattern(conf.Path) {
// ideally this should be validated at much earlier stage
// keeping it here to have gcs specific validations
return nil, fmt.Errorf("glob pattern %s is invalid", conf.Path)
}
url, err := globutil.ParseBucketURL(conf.Path)
if err != nil {
return nil, fmt.Errorf("failed to parse path %q, %w", conf.Path, err)
}
conf.url = url
if url.Scheme != "gs" {
return nil, fmt.Errorf("invalid gcs path %q, should start with gs://", conf.Path)
}
conf.extractPolicy, err = rillblob.ParseExtractPolicy(conf.Extract)
if err != nil {
return nil, fmt.Errorf("failed to parse extract config: %w", err)
}
return conf, nil
}
type Connection struct {
config *configProperties
logger *zap.Logger
}
var _ drivers.Handle = &Connection{}
// Driver implements drivers.Connection.
func (c *Connection) Driver() string {
return "gcs"
}
// Config implements drivers.Connection.
func (c *Connection) Config() map[string]any {
m := make(map[string]any, 0)
_ = mapstructure.Decode(c.config, m)
return m
}
// Close implements drivers.Connection.
func (c *Connection) Close() error {
return nil
}
// AsRegistry implements drivers.Connection.
func (c *Connection) AsRegistry() (drivers.RegistryStore, bool) {
return nil, false
}
// AsCatalogStore implements drivers.Connection.
func (c *Connection) AsCatalogStore(instanceID string) (drivers.CatalogStore, bool) {
return nil, false
}
// AsRepoStore implements drivers.Connection.
func (c *Connection) AsRepoStore(instanceID string) (drivers.RepoStore, bool) {
return nil, false
}
// AsAdmin implements drivers.Handle.
func (c *Connection) AsAdmin(instanceID string) (drivers.AdminService, bool) {
return nil, false
}
// AsAI implements drivers.Handle.
func (c *Connection) AsAI(instanceID string) (drivers.AIService, bool) {
return nil, false
}
// AsOLAP implements drivers.Connection.
func (c *Connection) AsOLAP(instanceID string) (drivers.OLAPStore, bool) {
return nil, false
}
// Migrate implements drivers.Connection.
func (c *Connection) Migrate(ctx context.Context) (err error) {
return nil
}
// MigrationStatus implements drivers.Connection.
func (c *Connection) MigrationStatus(ctx context.Context) (current, desired int, err error) {
return 0, 0, nil
}
// AsObjectStore implements drivers.Connection.
func (c *Connection) AsObjectStore() (drivers.ObjectStore, bool) {
return c, true
}
// AsModelExecutor implements drivers.Handle.
func (c *Connection) AsModelExecutor(instanceID string, opts *drivers.ModelExecutorOptions) (drivers.ModelExecutor, bool) {
return nil, false
}
// AsModelManager implements drivers.Handle.
func (c *Connection) AsModelManager(instanceID string) (drivers.ModelManager, bool) {
return nil, false
}
// AsTransporter implements drivers.Connection.
func (c *Connection) AsTransporter(from, to drivers.Handle) (drivers.Transporter, bool) {
return nil, false
}
func (c *Connection) AsFileStore() (drivers.FileStore, bool) {
return nil, false
}
// AsSQLStore implements drivers.Connection.
func (c *Connection) AsSQLStore() (drivers.SQLStore, bool) {
return nil, false
}
// AsNotifier implements drivers.Connection.
func (c *Connection) AsNotifier(properties map[string]any) (drivers.Notifier, error) {
return nil, drivers.ErrNotNotifier
}
// DownloadFiles returns a file iterator over objects stored in gcs.
// The credential json is read from config google_application_credentials.
// Additionally in case `allow_host_credentials` is true it looks for "Application Default Credentials" as well
func (c *Connection) DownloadFiles(ctx context.Context, props map[string]any) (drivers.FileIterator, error) {
conf, err := parseSourceProperties(props)
if err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
}
client, err := c.createClient(ctx)
if err != nil {
return nil, err
}
bucketObj, err := gcsblob.OpenBucket(ctx, client, conf.url.Host, nil)
if err != nil {
return nil, fmt.Errorf("failed to open bucket %q, %w", conf.url.Host, err)
}
var batchSize datasize.ByteSize
if conf.BatchSize == "-1" {
batchSize = math.MaxInt64 // download everything in one batch
} else {
batchSize, err = datasize.ParseString(conf.BatchSize)
if err != nil {
return nil, err
}
}
// prepare fetch configs
opts := rillblob.Options{
GlobMaxTotalSize: conf.GlobMaxTotalSize,
GlobMaxObjectsMatched: conf.GlobMaxObjectsMatched,
GlobMaxObjectsListed: conf.GlobMaxObjectsListed,
GlobPageSize: conf.GlobPageSize,
GlobPattern: conf.url.Path,
ExtractPolicy: conf.extractPolicy,
BatchSizeBytes: int64(batchSize.Bytes()),
KeepFilesUntilClose: conf.BatchSize == "-1",
}
iter, err := rillblob.NewIterator(ctx, bucketObj, opts, c.logger)
if err != nil {
apiError := &googleapi.Error{}
// in cases when no creds are passed
if errors.As(err, &apiError) && apiError.Code == http.StatusUnauthorized {
return nil, drivers.NewPermissionDeniedError(fmt.Sprintf("can't access remote err: %v", apiError))
}
// StatusUnauthorized when incorrect key is passsed
// StatusBadRequest when key doesn't have a valid credentials file
retrieveError := &oauth2.RetrieveError{}
if errors.As(err, &retrieveError) && (retrieveError.Response.StatusCode == http.StatusUnauthorized || retrieveError.Response.StatusCode == http.StatusBadRequest) {
return nil, drivers.NewPermissionDeniedError(fmt.Sprintf("can't access remote err: %v", retrieveError))
}
}
return iter, err
}
func (c *Connection) createClient(ctx context.Context) (*gcp.HTTPClient, error) {
creds, err := gcputil.Credentials(ctx, c.config.SecretJSON, c.config.AllowHostAccess)
if err != nil {
if !errors.Is(err, gcputil.ErrNoCredentials) {
return nil, err
}
// no credentials set, we try with a anonymous client in case user is trying to access public buckets
return gcp.NewAnonymousHTTPClient(gcp.DefaultTransport()), nil
}
// the token source returned from credentials works for all kind of credentials like serviceAccountKey, credentialsKey etc.
return gcp.NewHTTPClient(gcp.DefaultTransport(), gcp.CredentialsTokenSource(creds))
}