-
Notifications
You must be signed in to change notification settings - Fork 10
/
bifrost.go
273 lines (243 loc) · 7.87 KB
/
bifrost.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
/*
package bifrost
provides a rainbow bridge for shipping files to any cloud storage service.
It's like bifrost from marvel comics, but for files.
*/
package bifrost
import (
"context"
"fmt"
"log"
"reflect"
"cloud.google.com/go/storage"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/opensaucerer/bifrost/gcs"
"github.com/opensaucerer/bifrost/pinata"
bs3 "github.com/opensaucerer/bifrost/s3"
bconfig "github.com/opensaucerer/bifrost/shared/config"
"github.com/opensaucerer/bifrost/shared/errors"
"github.com/opensaucerer/bifrost/shared/request"
"github.com/opensaucerer/bifrost/wasabi"
"google.golang.org/api/option"
awsv1 "github.com/aws/aws-sdk-go/aws"
credv1 "github.com/aws/aws-sdk-go/aws/credentials"
sessv1 "github.com/aws/aws-sdk-go/aws/session"
s3v1 "github.com/aws/aws-sdk-go/service/s3"
)
// NewRainbowBridge returns a new Rainbow Bridge for shipping files to your specified cloud storage service.
func NewRainbowBridge(bc *BridgeConfig) (RainbowBridge, error) {
// vefify that the config is valid
if bc == nil {
return nil, &errors.BifrostError{
Err: fmt.Errorf("config is nil"),
ErrorCode: errors.ErrInvalidConfig,
}
}
// verify that the config is a struct
t := reflect.TypeOf(bc)
if t.Elem().Kind() != reflect.Struct {
return nil, &errors.BifrostError{
Err: fmt.Errorf("invalid config type: %s", t.Elem().Kind()),
ErrorCode: errors.ErrInvalidConfig,
}
}
// verify that the config struct is of valid type
if t.Elem().Name() != bridgeConfigType {
return nil, &errors.BifrostError{
Err: fmt.Errorf("invalid config type: %s", t.Elem().Name()),
ErrorCode: errors.ErrInvalidConfig,
}
}
// verify that the config struct has a valid provider
if bc.Provider == "" {
return nil, &errors.BifrostError{
Err: fmt.Errorf("no provider specified"),
ErrorCode: errors.ErrInvalidProvider,
}
}
// verify that the provider is valid
if _, ok := providers[bc.Provider]; !ok {
return nil, &errors.BifrostError{
Err: fmt.Errorf("invalid provider: %s", bc.Provider),
ErrorCode: errors.ErrInvalidProvider,
}
}
// verify that the config struct has a valid bucket
if bc.DefaultBucket == "" {
// some provider might not require a bucket
// Just log a warning
if bc.EnableDebug {
// @TODO: create a logger
log.Printf(errors.WARN+"WARN: "+errors.NONE+"No bucket specified for provider %s. This might cause errors or require you to specify a bucket for each operation.", providers[bc.Provider])
}
}
// Create a new bridge based on the provider
switch bc.Provider {
case SimpleStorageService:
return newSimpleStorageService(bc)
case GoogleCloudStorage:
return newGoogleCloudStorage(bc)
case PinataCloud:
return newPinataCloud(bc)
case WasabiCloudStorage:
return newWasabiCloudStorage(bc)
default:
return nil, &errors.BifrostError{
Err: fmt.Errorf("invalid provider: %s", bc.Provider),
ErrorCode: errors.ErrBadRequest,
}
}
}
// newPinataCloud returns a new client for Pinata Cloud.
func newPinataCloud(bc *BridgeConfig) (RainbowBridge, error) {
// @TODO: add support for API key and API secret
if bc.PinataJWT == "" {
return nil, &errors.BifrostError{
Err: fmt.Errorf("pinata JWT is required"),
ErrorCode: errors.ErrUnauthorized,
}
}
var p = pinata.PinataCloud{
PinataJWT: bc.PinataJWT,
Provider: providers[bc.Provider],
DefaultTimeout: bc.DefaultTimeout,
PublicRead: bc.PublicRead,
UseAsync: bc.UseAsync,
EnableDebug: bc.EnableDebug,
Client: request.NewClient(bconfig.URLPinataAuth, bc.PinataJWT, bc.DefaultTimeout),
}
// authenticate with Pinata Cloud
if err := p.Preflight(); err != nil {
return nil, err
}
// return a new Pinata Cloud Storage Provider
return &p, nil
}
// newGoogleCloudStorage returns a new client for Google Cloud Storage.
func newGoogleCloudStorage(bc *BridgeConfig) (RainbowBridge, error) {
var client *storage.Client
var err error
if bc.CredentialsFile != "" {
// first attempt to authenticate with credentials file
client, err = storage.NewClient(context.Background(), option.WithCredentialsFile(bc.CredentialsFile))
if err != nil {
return nil, &errors.BifrostError{
Err: err,
ErrorCode: errors.ErrUnauthorized,
}
}
} else {
// if no credentials file is specified, attempt to authenticate without credentials file
client, err = storage.NewClient(context.Background())
if err != nil {
return nil, &errors.BifrostError{
Err: err,
ErrorCode: errors.ErrUnauthorized,
}
}
}
// return a new Google Cloud Storage client
return &gcs.GoogleCloudStorage{
Provider: providers[bc.Provider],
DefaultBucket: bc.DefaultBucket,
CredentialsFile: bc.CredentialsFile,
Project: bc.Project,
DefaultTimeout: bc.DefaultTimeout,
Client: client,
EnableDebug: bc.EnableDebug,
PublicRead: bc.PublicRead,
UseAsync: bc.UseAsync,
}, nil
}
// newSimpleStorageService returns a new client for AWS S3
func newSimpleStorageService(bc *BridgeConfig) (RainbowBridge, error) {
var client *awss3.Client
if bc.AccessKey != "" && bc.SecretKey != "" {
creds := credentials.NewStaticCredentialsProvider(bc.AccessKey, bc.SecretKey, "")
cfg, err := awsconfig.LoadDefaultConfig(context.Background(), awsconfig.WithCredentialsProvider(creds), awsconfig.WithRegion(bc.Region))
if err != nil {
return nil, &errors.BifrostError{
Err: err,
ErrorCode: errors.ErrUnauthorized,
}
}
client = awss3.NewFromConfig(cfg)
} else {
// Load AWS Shared Configuration
cfg, err := awsconfig.LoadDefaultConfig(context.TODO())
if err != nil {
return nil, &errors.BifrostError{
Err: err,
ErrorCode: errors.ErrUnauthorized,
}
}
client = awss3.NewFromConfig(cfg)
}
return &bs3.SimpleStorageService{
Provider: providers[bc.Provider],
DefaultBucket: bc.DefaultBucket,
Region: bc.Region,
DefaultTimeout: bc.DefaultTimeout,
PublicRead: bc.PublicRead,
SecretKey: bc.SecretKey,
AccessKey: bc.AccessKey,
Client: client,
EnableDebug: bc.EnableDebug,
UseAsync: bc.UseAsync,
}, nil
}
// newWasabiCloudStorage returns a new client for Wasabi Cloud Storage
func newWasabiCloudStorage(bc *BridgeConfig) (RainbowBridge, error) {
// Wasabi is an S3-compatible service, so we can use the same client
var client *s3v1.S3
if bc.AccessKey != "" && bc.SecretKey != "" {
s3Config := awsv1.Config{
Endpoint: awsv1.String(fmt.Sprintf(bconfig.URLWasabiEndpoint, bc.Region)),
Region: awsv1.String(bc.Region),
S3ForcePathStyle: awsv1.Bool(true),
Credentials: credv1.NewStaticCredentials(bc.AccessKey, bc.SecretKey, ""),
}
s3session, err := sessv1.NewSessionWithOptions(sessv1.Options{
Config: s3Config,
})
if err != nil {
return nil, &errors.BifrostError{
Err: err,
ErrorCode: errors.ErrUnauthorized,
}
}
client = s3v1.New(s3session)
} else {
// load default config
s3Config := awsv1.Config{
Endpoint: awsv1.String(fmt.Sprintf(bconfig.URLWasabiEndpoint, bc.Region)),
Region: awsv1.String(bc.Region),
S3ForcePathStyle: awsv1.Bool(true),
}
s3session, err := sessv1.NewSessionWithOptions(sessv1.Options{
Config: s3Config,
Profile: bconfig.WasabiCloudStorage,
})
if err != nil {
return nil, &errors.BifrostError{
Err: err,
ErrorCode: errors.ErrUnauthorized,
}
}
client = s3v1.New(s3session)
}
return &wasabi.WasabiCloudStorage{
Provider: providers[bc.Provider],
DefaultBucket: bc.DefaultBucket,
Region: bc.Region,
DefaultTimeout: bc.DefaultTimeout,
PublicRead: bc.PublicRead,
SecretKey: bc.SecretKey,
AccessKey: bc.AccessKey,
Client: client,
EnableDebug: bc.EnableDebug,
UseAsync: bc.UseAsync,
}, nil
}