-
Notifications
You must be signed in to change notification settings - Fork 125
/
azure_storage_client.go
346 lines (323 loc) · 9.29 KB
/
azure_storage_client.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// Copyright (c) 2021-2023 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/bloberror"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/container"
)
type snowflakeAzureClient struct {
}
type azureLocation struct {
containerName string
path string
}
type azureAPI interface {
UploadStream(ctx context.Context, body io.Reader, o *azblob.UploadStreamOptions) (azblob.UploadStreamResponse, error)
UploadFile(ctx context.Context, file *os.File, o *azblob.UploadFileOptions) (azblob.UploadFileResponse, error)
DownloadFile(ctx context.Context, file *os.File, o *blob.DownloadFileOptions) (int64, error)
DownloadStream(ctx context.Context, o *blob.DownloadStreamOptions) (azblob.DownloadStreamResponse, error)
GetProperties(ctx context.Context, o *blob.GetPropertiesOptions) (blob.GetPropertiesResponse, error)
}
func (util *snowflakeAzureClient) createClient(info *execResponseStageInfo, _ bool) (cloudClient, error) {
sasToken := info.Creds.AzureSasToken
u, err := url.Parse(fmt.Sprintf("https://%s.%s/%s%s", info.StorageAccount, info.EndPoint, info.Path, sasToken))
if err != nil {
return nil, err
}
client, err := azblob.NewClientWithNoCredential(u.String(), &azblob.ClientOptions{
ClientOptions: azcore.ClientOptions{
Retry: policy.RetryOptions{
MaxRetries: 60,
RetryDelay: 2 * time.Second,
},
Transport: &http.Client{
Transport: SnowflakeTransport,
},
},
})
if err != nil {
return nil, err
}
return client, nil
}
// cloudUtil implementation
func (util *snowflakeAzureClient) getFileHeader(meta *fileMetadata, filename string) (*fileHeader, error) {
client, ok := meta.client.(*azblob.Client)
if !ok {
return nil, fmt.Errorf("failed to parse client to azblob.Client")
}
azureLoc, err := util.extractContainerNameAndPath(meta.stageInfo.Location)
if err != nil {
return nil, err
}
path := azureLoc.path + strings.TrimLeft(filename, "/")
containerClient, err := createContainerClient(client.URL())
if err != nil {
return nil, &SnowflakeError{
Message: "failed to create container client",
}
}
var blobClient azureAPI
blobClient = containerClient.NewBlockBlobClient(path)
// for testing only
if meta.mockAzureClient != nil {
blobClient = meta.mockAzureClient
}
resp, err := blobClient.GetProperties(context.Background(), &blob.GetPropertiesOptions{
AccessConditions: &blob.AccessConditions{},
CPKInfo: &blob.CPKInfo{},
})
if err != nil {
var se *azcore.ResponseError
if errors.As(err, &se) {
if se.ErrorCode == string(bloberror.BlobNotFound) {
meta.resStatus = notFoundFile
return nil, fmt.Errorf("could not find file")
} else if se.StatusCode == 403 {
meta.resStatus = renewToken
return nil, fmt.Errorf("received 403, attempting to renew")
}
}
meta.resStatus = errStatus
return nil, err
}
meta.resStatus = uploaded
metadata := withLowerKeys(resp.Metadata)
var encData encryptionData
_, ok = metadata["encryptiondata"]
if ok {
if err = json.Unmarshal([]byte(*metadata["encryptiondata"]), &encData); err != nil {
return nil, err
}
}
matdesc, ok := metadata["matdesc"]
if !ok {
// matdesc is not in response, use empty string
matdesc = new(string)
}
encryptionMetadata := encryptMetadata{
encData.WrappedContentKey.EncryptionKey,
encData.ContentEncryptionIV,
*matdesc,
}
digest, ok := metadata["sfcdigest"]
if !ok {
// sfcdigest is not in response, use empty string
digest = new(string)
}
return &fileHeader{
*digest,
int64(len(metadata)),
&encryptionMetadata,
}, nil
}
// cloudUtil implementation
func (util *snowflakeAzureClient) uploadFile(
dataFile string,
meta *fileMetadata,
encryptMeta *encryptMetadata,
maxConcurrency int,
multiPartThreshold int64) error {
azureMeta := map[string]*string{
"sfcdigest": &meta.sha256Digest,
}
if encryptMeta != nil {
ed := &encryptionData{
EncryptionMode: "FullBlob",
WrappedContentKey: contentKey{
"symmKey1",
encryptMeta.key,
"AES_CBC_256",
},
EncryptionAgent: encryptionAgent{
"1.0",
"AES_CBC_128",
},
ContentEncryptionIV: encryptMeta.iv,
KeyWrappingMetadata: keyMetadata{
"Java 5.3.0",
},
}
metadata, err := json.Marshal(ed)
if err != nil {
return err
}
encryptionMetadata := string(metadata)
azureMeta["encryptiondata"] = &encryptionMetadata
azureMeta["matdesc"] = &encryptMeta.matdesc
}
azureLoc, err := util.extractContainerNameAndPath(meta.stageInfo.Location)
if err != nil {
return err
}
path := azureLoc.path + strings.TrimLeft(meta.dstFileName, "/")
client, ok := meta.client.(*azblob.Client)
if !ok {
return &SnowflakeError{
Message: "failed to cast to azure client",
}
}
containerClient, err := createContainerClient(client.URL())
if err != nil {
return &SnowflakeError{
Message: "failed to create container client",
}
}
var blobClient azureAPI
blobClient = containerClient.NewBlockBlobClient(path)
// for testing only
if meta.mockAzureClient != nil {
blobClient = meta.mockAzureClient
}
if meta.srcStream != nil {
uploadSrc := meta.srcStream
if meta.realSrcStream != nil {
uploadSrc = meta.realSrcStream
}
_, err = blobClient.UploadStream(context.Background(), uploadSrc, &azblob.UploadStreamOptions{
BlockSize: int64(uploadSrc.Len()),
Metadata: azureMeta,
})
} else {
var f *os.File
f, err = os.Open(dataFile)
if err != nil {
return err
}
defer f.Close()
contentType := "application/octet-stream"
contentEncoding := "utf-8"
blobOptions := &azblob.UploadFileOptions{
HTTPHeaders: &blob.HTTPHeaders{
BlobContentType: &contentType,
BlobContentEncoding: &contentEncoding,
},
Metadata: azureMeta,
Concurrency: uint16(maxConcurrency),
}
if meta.options.putAzureCallback != nil {
blobOptions.Progress = meta.options.putAzureCallback.call
}
_, err = blobClient.UploadFile(context.Background(), f, blobOptions)
}
if err != nil {
var se *azcore.ResponseError
if errors.As(err, &se) {
if se.StatusCode == 403 && util.detectAzureTokenExpireError(se.RawResponse) {
meta.resStatus = renewToken
} else {
meta.resStatus = needRetry
meta.lastError = err
}
return err
}
meta.resStatus = errStatus
return err
}
meta.dstFileSize = meta.uploadSize
meta.resStatus = uploaded
return nil
}
// cloudUtil implementation
func (util *snowflakeAzureClient) nativeDownloadFile(
meta *fileMetadata,
fullDstFileName string,
maxConcurrency int64) error {
azureLoc, err := util.extractContainerNameAndPath(meta.stageInfo.Location)
if err != nil {
return err
}
path := azureLoc.path + strings.TrimLeft(meta.srcFileName, "/")
client, ok := meta.client.(*azblob.Client)
if !ok {
return &SnowflakeError{
Message: "failed to cast to azure client",
}
}
containerClient, err := createContainerClient(client.URL())
if err != nil {
return &SnowflakeError{
Message: "failed to create container client",
}
}
var blobClient azureAPI
blobClient = containerClient.NewBlockBlobClient(path)
// for testing only
if meta.mockAzureClient != nil {
blobClient = meta.mockAzureClient
}
if meta.options.GetFileToStream {
blobDownloadResponse, err := blobClient.DownloadStream(context.Background(), &azblob.DownloadStreamOptions{})
if err != nil {
return err
}
retryReader := blobDownloadResponse.NewRetryReader(context.Background(), &azblob.RetryReaderOptions{})
defer retryReader.Close()
_, err = meta.dstStream.ReadFrom(retryReader)
if err != nil {
return err
}
} else {
f, err := os.OpenFile(fullDstFileName, os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
return err
}
defer f.Close()
_, err = blobClient.DownloadFile(
context.Background(), f, &azblob.DownloadFileOptions{
Concurrency: uint16(maxConcurrency)})
if err != nil {
return err
}
}
meta.resStatus = downloaded
return nil
}
func (util *snowflakeAzureClient) extractContainerNameAndPath(location string) (*azureLocation, error) {
stageLocation, err := expandUser(location)
if err != nil {
return nil, err
}
containerName := stageLocation
path := ""
if strings.Contains(stageLocation, "/") {
containerName = stageLocation[:strings.Index(stageLocation, "/")]
path = stageLocation[strings.Index(stageLocation, "/")+1:]
if path != "" && !strings.HasSuffix(path, "/") {
path += "/"
}
}
return &azureLocation{containerName, path}, nil
}
func (util *snowflakeAzureClient) detectAzureTokenExpireError(resp *http.Response) bool {
if resp.StatusCode != 403 {
return false
}
azureErr, err := io.ReadAll(resp.Body)
if err != nil {
return false
}
errStr := string(azureErr)
return strings.Contains(errStr, "Signature not valid in the specified time frame") ||
strings.Contains(errStr, "Server failed to authenticate the request")
}
func createContainerClient(clientURL string) (*container.Client, error) {
return container.NewClientWithNoCredential(clientURL, &container.ClientOptions{ClientOptions: azcore.ClientOptions{
Transport: &http.Client{
Transport: SnowflakeTransport,
},
}})
}