Skip to content

Commit

Permalink
Explicitly mention ChinaCloud for Azure
Browse files Browse the repository at this point in the history
  • Loading branch information
itaiad200 committed Mar 7, 2024
1 parent 48ecdfa commit beac0e1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
10 changes: 8 additions & 2 deletions pkg/block/azure/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ const (
// more the 5000 different accounts, which is highly unlikely
udcCacheSize = 5000

BlobEndpointFormat = "https://%s.blob.core.windows.net/"
BlobEndpointGeneralFormat = "https://%s.blob.core.windows.net/"
BlobEndpointChinaCloudFormat = "https://%s.blob.core.chinacloudapi.cn/"
)

type Adapter struct {
clientCache *ClientCache
preSignedExpiry time.Duration
disablePreSigned bool
disablePreSignedUI bool
chinaCloud bool
}

func NewAdapter(ctx context.Context, params params.Azure) (*Adapter, error) {
Expand All @@ -54,6 +56,7 @@ func NewAdapter(ctx context.Context, params params.Azure) (*Adapter, error) {
preSignedExpiry: preSignedExpiry,
disablePreSigned: params.DisablePreSigned,
disablePreSignedUI: params.DisablePreSignedUI,
chinaCloud: params.ChinaCloud,
}, nil
}

Expand All @@ -62,6 +65,7 @@ type BlobURLInfo struct {
ContainerURL string
ContainerName string
BlobURL string
Host string
}

type PrefixURLInfo struct {
Expand Down Expand Up @@ -106,6 +110,7 @@ func ResolveBlobURLInfoFromURL(pathURL *url.URL) (BlobURLInfo, error) {
ContainerURL: fmt.Sprintf("%s://%s/%s", pathURL.Scheme, pathURL.Host, pathParts[0]),
ContainerName: pathParts[0],
BlobURL: strings.Join(pathParts[1:], "/"),
Host: pathURL.Host,
}, nil
}

Expand All @@ -131,6 +136,7 @@ func resolveBlobURLInfo(obj block.ObjectPointer) (BlobURLInfo, error) {
ContainerURL: qp.ContainerURL,
ContainerName: qp.ContainerName,
BlobURL: qp.BlobURL + "/" + key,
Host: parsedNamespace.Host,
}
if qp.BlobURL == "" {
info.BlobURL = key
Expand Down Expand Up @@ -266,7 +272,7 @@ func (a *Adapter) getPreSignedURL(ctx context.Context, obj block.ObjectPointer,
}

// format blob URL with signed SAS query params
accountEndpoint := fmt.Sprintf(BlobEndpointFormat, qualifiedKey.StorageAccountName)
accountEndpoint := buildAccountEndpoint(qualifiedKey.StorageAccountName, a.chinaCloud)
u, err := url.JoinPath(accountEndpoint, qualifiedKey.ContainerName, qualifiedKey.BlobURL)
if err != nil {
return "", err
Expand Down
10 changes: 9 additions & 1 deletion pkg/block/azure/client_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func BuildAzureServiceClient(params params.Azure) (*service.Client, error) {
if params.TestEndpointURL != "" { // For testing purposes - override default url template
url = params.TestEndpointURL
} else {
url = fmt.Sprintf(BlobEndpointFormat, params.StorageAccount)
url = buildAccountEndpoint(params.StorageAccount, params.ChinaCloud)
}

options := service.ClientOptions{ClientOptions: azcore.ClientOptions{Retry: policy.RetryOptions{TryTimeout: params.TryTimeout}}}
Expand All @@ -142,3 +142,11 @@ func BuildAzureServiceClient(params params.Azure) (*service.Client, error) {
}
return service.NewClient(url, defaultCreds, &options)
}

func buildAccountEndpoint(storageAccount string, chinaCloud bool) string {
format := BlobEndpointGeneralFormat
if chinaCloud {
format = BlobEndpointChinaCloudFormat
}
return fmt.Sprintf(format, storageAccount)
}
2 changes: 2 additions & 0 deletions pkg/block/params/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ type Azure struct {
PreSignedExpiry time.Duration
DisablePreSigned bool
DisablePreSignedUI bool
// Azure China Cloud has different endpoints, different services and it's isolated from Azure Global Cloud
ChinaCloud bool
// TestEndpointURL - For testing purposes, provide a custom URL to override the default URL template
TestEndpointURL string
}
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ type Config struct {
PreSignedExpiry time.Duration `mapstructure:"pre_signed_expiry"`
DisablePreSigned bool `mapstructure:"disable_pre_signed"`
DisablePreSignedUI bool `mapstructure:"disable_pre_signed_ui"`
ChinaCloud bool `mapstructure:"china_cloud"`
// TestEndpointURL for testing purposes
TestEndpointURL string `mapstructure:"test_endpoint_url"`
} `mapstructure:"azure"`
Expand Down Expand Up @@ -503,6 +504,7 @@ func (c *Config) BlockstoreAzureParams() (blockparams.Azure, error) {
TestEndpointURL: c.Blockstore.Azure.TestEndpointURL,
DisablePreSigned: c.Blockstore.Azure.DisablePreSigned,
DisablePreSignedUI: c.Blockstore.Azure.DisablePreSignedUI,
ChinaCloud: c.Blockstore.Azure.ChinaCloud,
}, nil
}

Expand Down

0 comments on commit beac0e1

Please sign in to comment.