Skip to content

cdn bug fix #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 39 additions & 17 deletions storage/backend/azure/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,24 @@ const (

// Backend implements sotrage.Backend for Azure Blob Storage.
type Backend struct {
logger log.Logger
httpClient *http.Client
cfg Config
containerURL azblob.ContainerURL
sasToken string
// sharedKeyCredential *azblob.SharedKeyCredential
logger log.Logger
httpClient *http.Client
cfg Config
containerURL azblob.ContainerURL
sasToken string
sharedKeyCredential azblob.StorageAccountCredential
}

// New creates an AzureBlob backend.
func New(l log.Logger, c Config) (*Backend, error) {
var credential azblob.Credential

var err error

b := &Backend{
logger: l,
cfg: c,
httpClient: http.DefaultClient,
}
if c.AccountName == "" {
return nil, errors.New("azure account name is required")
}
Expand All @@ -57,6 +61,11 @@ func New(l log.Logger, c Config) (*Backend, error) {
if err != nil {
return nil, fmt.Errorf("azure, invalid credentials, %w", err)
}
var ok bool
b.sharedKeyCredential, ok = credential.(azblob.StorageAccountCredential)
if !ok {
return nil, errors.New("azure, invalid credentials")
}
}

// 3. Azurite has different URL pattern than production Azure Blob Storage.
Expand Down Expand Up @@ -98,13 +107,9 @@ func New(l log.Logger, c Config) (*Backend, error) {
level.Error(l).Log("msg", "container already exists", "err", err)
}
}

return &Backend{
logger: l,
cfg: c,
containerURL: containerURL,
httpClient: http.DefaultClient,
}, nil
b.containerURL = containerURL
b.sasToken = c.SASToken
return b, nil
}

// Get writes downloaded content to the given writer.
Expand Down Expand Up @@ -207,14 +212,31 @@ func (b *Backend) generateSASTokenWithCDN(containerName, blobPath string) (strin
containerName = strings.Replace(containerName, "\\", "/", -1) // Replace backslashes with forward slashes
blobPath = strings.Replace(blobPath, "\\", "/", -1) // Replace backslashes with forward slashes
}

parts := azblob.BlobURLParts{
Scheme: "https",
Host: b.cfg.CDNHost,
ContainerName: containerName,
BlobName: blobPath,
}
rawURL := parts.URL()
rawURL.RawQuery = b.sasToken
var rawURL url.URL
if b.sasToken == "" {
sasDefaultSignature := azblob.BlobSASSignatureValues{
Protocol: azblob.SASProtocolHTTPS,
ExpiryTime: time.Now().UTC().Add(12 * time.Hour),
ContainerName: containerName,
BlobName: blobPath,
Permissions: azblob.BlobSASPermissions{Read: true, List: true}.String(),
}
sasQueryParams, err := sasDefaultSignature.NewSASQueryParameters(b.sharedKeyCredential)
if err != nil {
return "", err
}
parts.SAS = sasQueryParams
rawURL = parts.URL()
} else {
rawURL = parts.URL()
rawURL.RawQuery = b.sasToken
}

return rawURL.String(), nil
}