Skip to content

Commit 2023477

Browse files
add support for sas token
1 parent 9c5d51b commit 2023477

File tree

3 files changed

+42
-32
lines changed

3 files changed

+42
-32
lines changed

main.go

+6
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,11 @@ func main() {
421421
Usage: "Azure Blob Storage Account Name",
422422
EnvVars: []string{"PLUGIN_ACCOUNT_NAME", "AZURE_ACCOUNT_NAME"},
423423
},
424+
&cli.StringFlag{
425+
Name: "azure.sas-token",
426+
Usage: "Azure Blob Storage SAS Token",
427+
EnvVars: []string{"AZURE_SAS_TOKEN"},
428+
},
424429
&cli.StringFlag{
425430
Name: "azure.account-key",
426431
Usage: "Azure Blob Storage Account Key",
@@ -587,6 +592,7 @@ func run(c *cli.Context) error {
587592
},
588593
Azure: azure.Config{
589594
AccountName: c.String("azure.account-name"),
595+
SASToken: c.String("azure.sas-token"),
590596
AccountKey: c.String("azure.account-key"),
591597
ContainerName: c.String("azure.container-name"),
592598
BlobStorageURL: c.String("azure.blob-storage-url"),

storage/backend/azure/azure.go

+35-32
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,40 @@ const (
2323
// DefaultBlobMaxRetryRequests Default value for Azure Blob Storage Max Retry Requests.
2424
DefaultBlobMaxRetryRequests = 4
2525

26-
defaultBufferSize = 3 * 1024 * 1024
26+
defaultBufferSize = 4 * 1024 * 1024
2727
defaultMaxBuffers = 4
2828
)
2929

3030
// Backend implements sotrage.Backend for Azure Blob Storage.
3131
type Backend struct {
32-
logger log.Logger
33-
httpClient *http.Client
34-
cfg Config
35-
containerURL azblob.ContainerURL
36-
sharedKeyCredential *azblob.SharedKeyCredential
32+
logger log.Logger
33+
httpClient *http.Client
34+
cfg Config
35+
containerURL azblob.ContainerURL
36+
sasToken string
37+
// sharedKeyCredential *azblob.SharedKeyCredential
3738
}
3839

3940
// New creates an AzureBlob backend.
4041
func New(l log.Logger, c Config) (*Backend, error) {
41-
// 1. From the Azure portal, get your storage account name and key and set environment variables.
42-
if c.AccountName == "" || c.AccountKey == "" {
43-
return nil, errors.New("either the AZURE_ACCOUNT_NAME or AZURE_ACCOUNT_KEY environment variable is not set")
44-
}
42+
var credential azblob.Credential
43+
44+
var err error
4545

46+
if c.AccountName == "" {
47+
return nil, errors.New("azure account name is required")
48+
}
4649
// 2. Create a default request pipeline using your storage account name and account key.
47-
credential, err := azblob.NewSharedKeyCredential(c.AccountName, c.AccountKey)
48-
if err != nil {
49-
return nil, fmt.Errorf("azure, invalid credentials, %w", err)
50+
if c.SASToken != "" {
51+
level.Info(l).Log("msg", "using token for cache operation")
52+
credential = azblob.NewAnonymousCredential()
53+
} else if c.AccountKey == "" {
54+
return nil, errors.New("azure account key is required")
55+
} else if c.AccountKey != "" {
56+
credential, err = azblob.NewSharedKeyCredential(c.AccountName, c.AccountKey)
57+
if err != nil {
58+
return nil, fmt.Errorf("azure, invalid credentials, %w", err)
59+
}
5060
}
5161

5262
// 3. Azurite has different URL pattern than production Azure Blob Storage.
@@ -57,6 +67,10 @@ func New(l log.Logger, c Config) (*Backend, error) {
5767
blobURL, err = url.Parse(fmt.Sprintf("https://%s.%s/%s", c.AccountName, c.BlobStorageURL, c.ContainerName))
5868
}
5969

70+
if c.SASToken != "" {
71+
blobURL.RawQuery = c.SASToken
72+
}
73+
6074
if err != nil {
6175
level.Error(l).Log("msg", "can't create url with : "+err.Error())
6276
}
@@ -85,9 +99,11 @@ func New(l log.Logger, c Config) (*Backend, error) {
8599
}
86100
}
87101

88-
return &Backend{logger: l, cfg: c, containerURL: containerURL,
89-
httpClient: http.DefaultClient,
90-
sharedKeyCredential: credential,
102+
return &Backend{
103+
logger: l,
104+
cfg: c,
105+
containerURL: containerURL,
106+
httpClient: http.DefaultClient,
91107
}, nil
92108
}
93109

@@ -127,8 +143,8 @@ func (b *Backend) Get(ctx context.Context, p string, w io.Writer) error {
127143

128144
} else {
129145
blobURL := b.containerURL.NewBlockBlobURL(p)
130-
// nolint: lll
131-
resp, err := blobURL.Download(ctx, 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{})
146+
resp, err := blobURL.Download(ctx, 0, azblob.CountToEnd,
147+
azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{})
132148
if err != nil {
133149
errCh <- fmt.Errorf("get the object, %w", err)
134150

@@ -187,31 +203,18 @@ func (b *Backend) Exists(ctx context.Context, p string) (bool, error) {
187203

188204
// Exists checks if path already exists.
189205
func (b *Backend) generateSASTokenWithCDN(containerName, blobPath string) (string, error) {
190-
191206
if runtime.GOOS == "windows" {
192207
containerName = strings.Replace(containerName, "\\", "/", -1) // Replace backslashes with forward slashes
193208
blobPath = strings.Replace(blobPath, "\\", "/", -1) // Replace backslashes with forward slashes
194209
}
195210

196-
sasDefaultSignature := azblob.BlobSASSignatureValues{
197-
Protocol: azblob.SASProtocolHTTPS,
198-
ExpiryTime: time.Now().UTC().Add(12 * time.Hour),
199-
ContainerName: containerName,
200-
BlobName: blobPath,
201-
Permissions: azblob.BlobSASPermissions{Read: true, List: true}.String(),
202-
}
203-
sasQueryParams, err := sasDefaultSignature.NewSASQueryParameters(b.sharedKeyCredential)
204-
if err != nil {
205-
return "", err
206-
}
207211
parts := azblob.BlobURLParts{
208212
Scheme: "https",
209213
Host: b.cfg.CDNHost,
210214
ContainerName: containerName,
211215
BlobName: blobPath,
212-
SAS: sasQueryParams,
213216
}
214-
215217
rawURL := parts.URL()
218+
rawURL.RawQuery = b.sasToken
216219
return rawURL.String(), nil
217220
}

storage/backend/azure/config.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import "time"
66
type Config struct {
77
AccountName string
88
AccountKey string
9+
SASToken string
910
ContainerName string
1011
BlobStorageURL string
1112
CDNHost string

0 commit comments

Comments
 (0)