-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathutil.go
402 lines (352 loc) · 11.9 KB
/
util.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package common
import (
"context"
"errors"
"fmt"
"net"
"net/url"
"strings"
"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/azcore/to"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob"
blobservice "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/service"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azdatalake"
datalake "github.com/Azure/azure-sdk-for-go/sdk/storage/azdatalake/service"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/directory"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/file"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/fileerror"
fileservice "github.com/Azure/azure-sdk-for-go/sdk/storage/azfile/service"
)
var AzcopyJobPlanFolder string
var AzcopyCurrentJobLogger ILoggerResetable
// isIPEndpointStyle checks if URL's host is IP, in this case the storage account endpoint will be composed as:
// http(s)://IP(:port)/storageaccount/container/...
// As url's Host property, host could be both host or host:port
func isIPEndpointStyle(host string) bool {
if host == "" {
return false
}
if h, _, err := net.SplitHostPort(host); err == nil {
host = h
}
// For IPv6, there could be case where SplitHostPort fails for cannot finding port.
// In this case, eliminate the '[' and ']' in the URL.
// For details about IPv6 URL, please refer to https://tools.ietf.org/html/rfc2732
if host[0] == '[' && host[len(host)-1] == ']' {
host = host[1 : len(host)-1]
}
return net.ParseIP(host) != nil
}
// SplitContainerNameFromPath returns blob/file/dir path excluding container.
// Ex. For input https://account1.blob.core.windows.net/container1/a/b/c/d
// a/b/c/d is returned.
func SplitContainerNameFromPath(u string) (container string, filepath string, err error) {
uri, err := url.Parse(u)
if err != nil {
return "", "", err
}
if uri.Path == "" {
return "", "", nil
}
path := uri.Path
if path[0] == '/' {
path = path[1:]
}
if isIPEndpointStyle(uri.Host) {
if accountEndIndex := strings.Index(path, "/"); accountEndIndex == -1 {
// Slash not found; path has account name & no container name or blob
return "", "", nil
} else {
path = path[accountEndIndex+1:] // path refers to portion after the account name now (container & blob names)
}
}
containerEndIndex := strings.Index(path, "/") // Find the next slash (if it exists)
if containerEndIndex == -1 { // Slash not found; path has container name & no blob name
return path, "", nil
}
return path[:containerEndIndex], path[containerEndIndex+1:], nil
}
func VerifyIsURLResolvable(url_string string) error {
/* This function is disabled. But we should still fix this after fixing the below stuff.
* We can take this up after migration to new SDK. The pipeline infra may not be same then.
* 1. At someplaces we use Blob SDK directly to create pipeline - ex getBlobCredentialType()
* We should create pipeline through helper functions create<Blob/File/blobfs>pipeline, where we
* handle errors appropriately.
* 2. We should either do a http.Get or net.Dial instead of lookIP. If we are behind a proxy, we may
* not resolve this IP. #2144
* 3. DNS errors may by temporary, we should try for a minute before we give up.
*/
return nil
/*
url, err := url.Parse(url_string)
if (err != nil) {
return err
}
_, err = net.LookupIP(url.Host)
return err
*/
}
type FileClientOptions struct {
AllowTrailingDot bool
AllowSourceTrailingDot bool
}
// GetServiceClientForLocation returns service client for the resourceURL. It strips the
// container and file related details before creating the client. locationSpecificOptions
// are required currently only for files.
func GetServiceClientForLocation(loc Location,
resource ResourceString,
credType CredentialType,
cred azcore.TokenCredential,
policyOptions *azcore.ClientOptions,
locationSpecificOptions any,
) (*ServiceClient, error) {
ret := &ServiceClient{}
resourceURL, err := resource.String()
if err != nil {
return nil, fmt.Errorf("failed to get resource string: %w", err)
}
switch loc {
case ELocation.BlobFS(), ELocation.Blob(): // Since we always may need to interact with DFS while working with Blob, we should just attach both.
datalakeURLParts, err := azdatalake.ParseURL(resourceURL)
if err != nil {
return nil, err
}
datalakeURLParts.FileSystemName = ""
datalakeURLParts.PathName = ""
resourceURL = datalakeURLParts.String()
var o *datalake.ClientOptions
var dsc *datalake.Client
if policyOptions != nil {
o = &datalake.ClientOptions{ClientOptions: *policyOptions}
}
if credType.IsAzureOAuth() {
dsc, err = datalake.NewClient(resourceURL, cred, o)
} else if credType.IsSharedKey() {
var sharedKeyCred *azdatalake.SharedKeyCredential
sharedKeyCred, err = GetDatalakeSharedKeyCredential()
if err != nil {
return nil, err
}
dsc, err = datalake.NewClientWithSharedKeyCredential(resourceURL, sharedKeyCred, o)
} else {
dsc, err = datalake.NewClientWithNoCredential(resourceURL, o)
}
if err != nil {
return nil, err
}
ret.dsc = dsc
blobURLParts, err := blob.ParseURL(resourceURL)
if err != nil {
return nil, err
}
blobURLParts.ContainerName = ""
blobURLParts.BlobName = ""
// In case we are creating a blob client for a datalake target, correct the endpoint
blobURLParts.Host = strings.Replace(blobURLParts.Host, ".dfs", ".blob", 1)
resourceURL = blobURLParts.String()
var bso *blobservice.ClientOptions
var bsc *blobservice.Client
if policyOptions != nil {
bso = &blobservice.ClientOptions{ClientOptions: *policyOptions}
}
if credType.IsAzureOAuth() {
bsc, err = blobservice.NewClient(resourceURL, cred, bso)
} else if credType.IsSharedKey() {
var sharedKeyCred *blob.SharedKeyCredential
sharedKeyCred, err = GetBlobSharedKeyCredential()
if err != nil {
return nil, err
}
bsc, err = blobservice.NewClientWithSharedKeyCredential(resourceURL, sharedKeyCred, bso)
} else {
bsc, err = blobservice.NewClientWithNoCredential(resourceURL, bso)
}
if err != nil {
return nil, err
}
ret.bsc = bsc
return ret, nil
case ELocation.File():
fileURLParts, err := file.ParseURL(resourceURL)
if err != nil {
return nil, err
}
fileURLParts.ShareName = ""
fileURLParts.DirectoryOrFilePath = ""
resourceURL = fileURLParts.String()
var o *fileservice.ClientOptions
var fsc *fileservice.Client
if policyOptions != nil {
o = &fileservice.ClientOptions{ClientOptions: *policyOptions}
}
if locationSpecificOptions != nil {
o.AllowTrailingDot = &locationSpecificOptions.(*FileClientOptions).AllowTrailingDot
o.AllowSourceTrailingDot = &locationSpecificOptions.(*FileClientOptions).AllowSourceTrailingDot
}
if cred != nil {
o.FileRequestIntent = to.Ptr(fileservice.ShareTokenIntentBackup)
fsc, err = fileservice.NewClient(resourceURL, cred, o)
} else {
fsc, err = fileservice.NewClientWithNoCredential(resourceURL, o)
}
if err != nil {
return nil, err
}
ret.fsc = fsc
return ret, nil
default:
return nil, nil
}
}
// NewScopedCredential takes in a credInfo object and returns ScopedCredential
// if credentialType is either MDOAuth or oAuth. For anything else,
// nil is returned
func NewScopedCredential(cred azcore.TokenCredential, credType CredentialType) *ScopedCredential {
var scope string
if !credType.IsAzureOAuth() {
return nil
} else if credType == ECredentialType.MDOAuthToken() {
scope = ManagedDiskScope
} else if credType == ECredentialType.OAuthToken() {
scope = StorageScope
}
return &ScopedCredential{cred: cred, scopes: []string{scope}}
}
type ScopedCredential struct {
cred azcore.TokenCredential
scopes []string
}
func (s *ScopedCredential) GetToken(ctx context.Context, _ policy.TokenRequestOptions) (azcore.AccessToken, error) {
return s.cred.GetToken(ctx, policy.TokenRequestOptions{Scopes: s.scopes, EnableCAE: true})
}
type ServiceClient struct {
fsc *fileservice.Client
bsc *blobservice.Client
dsc *datalake.Client
}
func (s *ServiceClient) BlobServiceClient() (*blobservice.Client, error) {
if s.bsc == nil {
return nil, ErrInvalidClient("Blob Service")
}
return s.bsc, nil
}
func (s *ServiceClient) FileServiceClient() (*fileservice.Client, error) {
if s.fsc == nil {
return nil, ErrInvalidClient("File Service")
}
return s.fsc, nil
}
func (s *ServiceClient) DatalakeServiceClient() (*datalake.Client, error) {
if s.dsc == nil {
return nil, ErrInvalidClient("Datalake Service")
}
return s.dsc, nil
}
// This is currently used only in testcases
func NewServiceClient(bsc *blobservice.Client,
fsc *fileservice.Client,
dsc *datalake.Client) *ServiceClient {
return &ServiceClient{
bsc: bsc,
fsc: fsc,
dsc: dsc,
}
}
// Metadata utility functions to work around GoLang's metadata capitalization
func TryAddMetadata(metadata Metadata, key, value string) {
if _, ok := metadata[key]; ok {
return // Don't overwrite the user's metadata
}
if key != "" {
capitalizedKey := strings.ToUpper(string(key[0])) + key[1:]
if _, ok := metadata[capitalizedKey]; ok {
return
}
}
v := value
metadata[key] = &v
}
func TryReadMetadata(metadata Metadata, key string) (*string, bool) {
if v, ok := metadata[key]; ok {
return v, true
}
if key != "" {
capitalizedKey := strings.ToUpper(string(key[0])) + key[1:]
if v, ok := metadata[capitalizedKey]; ok {
return v, true
}
}
return nil, false
}
type FileClientStub interface {
URL() string
}
// DoWithOverrideReadOnlyOnAzureFiles performs the given action,
// and forces it to happen even if the target is read only.
// NOTE that all SMB attributes (and other headers?) on the target will be lost,
// so only use this if you don't need them any more
// (e.g. you are about to delete the resource, or you are going to reset the attributes/headers)
func DoWithOverrideReadOnlyOnAzureFiles(ctx context.Context, action func() (interface{}, error), targetFileOrDir FileClientStub, enableForcing bool) error {
// try the action
_, err := action()
if fileerror.HasCode(err, fileerror.ParentNotFound, fileerror.ShareNotFound) {
return err
}
failedAsReadOnly := false
if fileerror.HasCode(err, fileerror.ReadOnlyAttribute) {
failedAsReadOnly = true
}
if !failedAsReadOnly {
return err
}
// did fail as readonly, but forcing is not enabled
if !enableForcing {
return errors.New("target is readonly. To force the action to proceed, add --force-if-read-only to the command line")
}
// did fail as readonly, and forcing is enabled
if f, ok := targetFileOrDir.(*file.Client); ok {
h := file.HTTPHeaders{}
_, err = f.SetHTTPHeaders(ctx, &file.SetHTTPHeadersOptions{
HTTPHeaders: &h,
SMBProperties: &file.SMBProperties{
// clear the attributes
Attributes: &file.NTFSFileAttributes{None: true},
},
})
} else if d, ok := targetFileOrDir.(*directory.Client); ok {
// this code path probably isn't used, since ReadOnly (in Windows file systems at least)
// only applies to the files in a folder, not to the folder itself. But we'll leave the code here, for now.
_, err = d.SetProperties(ctx, &directory.SetPropertiesOptions{
FileSMBProperties: &file.SMBProperties{
// clear the attributes
Attributes: &file.NTFSFileAttributes{None: true},
},
})
} else {
err = errors.New("cannot remove read-only attribute from unknown target type")
}
if err != nil {
return err
}
// retry the action
_, err = action()
return err
}
// @brief Checks if the container name provided is a system container or not
func IsSystemContainer(containerName string) bool {
// Decode the container name in case it's URL-encoded
decodedName, err := url.QueryUnescape(containerName)
if err != nil {
// If decoding fails, it's unlikely the name matches a system container
return false
}
// define the system variables for the system containers
systemContainers := []string{"$blobchangefeed", "$logs"}
for _, sys := range systemContainers {
if decodedName == sys {
return true
}
}
return false
}