@@ -4,14 +4,14 @@ import (
44 "context"
55 "fmt"
66 "log"
7- "net/url"
87 "os"
9- "strings"
108 "time"
119
10+ "github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/blobs"
11+ "github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/containers"
12+
1213 "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/resources/mgmt/resources"
1314 armStorage "github.com/Azure/azure-sdk-for-go/profiles/2017-03-09/storage/mgmt/storage"
14- "github.com/Azure/azure-sdk-for-go/storage"
1515 "github.com/Azure/go-autorest/autorest"
1616 "github.com/Azure/go-autorest/autorest/azure"
1717 "github.com/hashicorp/go-azure-helpers/authentication"
@@ -23,6 +23,8 @@ type ArmClient struct {
2323 // These Clients are only initialized if an Access Key isn't provided
2424 groupsClient * resources.GroupsClient
2525 storageAccountsClient * armStorage.AccountsClient
26+ containersClient * containers.Client
27+ blobsClient * blobs.Client
2628
2729 accessKey string
2830 environment azure.Environment
@@ -106,49 +108,81 @@ func buildArmEnvironment(config BackendConfig) (*azure.Environment, error) {
106108 return authentication .DetermineEnvironment (config .Environment )
107109}
108110
109- func (c ArmClient ) getBlobClient (ctx context.Context ) (* storage. BlobStorageClient , error ) {
110- if c .accessKey != "" {
111- log .Printf ("[DEBUG] Building the Blob Client from an Access Token" )
112- storageClient , err := storage . NewBasicClientOnSovereignCloud (c .storageAccountName , c . accessKey , c . environment )
111+ func (c ArmClient ) getBlobClient (ctx context.Context ) (* blobs. Client , error ) {
112+ if c .sasToken != "" {
113+ log .Printf ("[DEBUG] Building the Blob Client from a SAS Token" )
114+ storageAuth , err := autorest . NewSASTokenAuthorizer (c .sasToken )
113115 if err != nil {
114- return nil , fmt .Errorf ("Error creating storage client for storage account %q : %s" , c . storageAccountName , err )
116+ return nil , fmt .Errorf ("Error building Authorizer : %+v" , err )
115117 }
116- client := storageClient .GetBlobService ()
117- return & client , nil
118+
119+ blobsClient := blobs .NewWithEnvironment (c .environment )
120+ c .configureClient (& blobsClient .Client , storageAuth )
121+ return & blobsClient , nil
118122 }
119123
120- if c . sasToken != "" {
121- log . Printf ( "[DEBUG] Building the Blob Client from a SAS Token" )
122- token := strings . TrimPrefix ( c . sasToken , "? " )
123- uri , err := url . ParseQuery ( token )
124+ accessKey := c . accessKey
125+ if accessKey == "" {
126+ log . Printf ( "[DEBUG] Building the Blob Client from an Access Token (using user credentials) " )
127+ keys , err := c . storageAccountsClient . ListKeys ( ctx , c . resourceGroupName , c . storageAccountName )
124128 if err != nil {
125- return nil , fmt .Errorf ("Error parsing SAS Token : %+v" , err )
129+ return nil , fmt .Errorf ("Error retrieving keys for Storage Account %q : %s" , c . storageAccountName , err )
126130 }
127131
128- storageClient := storage .NewAccountSASClient (c .storageAccountName , uri , c .environment )
129- client := storageClient .GetBlobService ()
130- return & client , nil
132+ if keys .Keys == nil {
133+ return nil , fmt .Errorf ("Nil key returned for storage account %q" , c .storageAccountName )
134+ }
135+
136+ accessKeys := * keys .Keys
137+ accessKey = * accessKeys [0 ].Value
131138 }
132139
133- log .Printf ("[DEBUG] Building the Blob Client from an Access Token (using user credentials)" )
134- keys , err := c .storageAccountsClient .ListKeys (ctx , c .resourceGroupName , c .storageAccountName )
140+ storageAuth , err := autorest .NewSharedKeyAuthorizer (c .storageAccountName , accessKey , autorest .SharedKey )
135141 if err != nil {
136- return nil , fmt .Errorf ("Error retrieving keys for Storage Account %q : %s" , c . storageAccountName , err )
142+ return nil , fmt .Errorf ("Error building Authorizer : %+v" , err )
137143 }
138144
139- if keys .Keys == nil {
140- return nil , fmt .Errorf ("Nil key returned for storage account %q" , c .storageAccountName )
145+ blobsClient := blobs .NewWithEnvironment (c .environment )
146+ c .configureClient (& blobsClient .Client , storageAuth )
147+ return & blobsClient , nil
148+ }
149+
150+ func (c ArmClient ) getContainersClient (ctx context.Context ) (* containers.Client , error ) {
151+ if c .sasToken != "" {
152+ log .Printf ("[DEBUG] Building the Container Client from a SAS Token" )
153+ storageAuth , err := autorest .NewSASTokenAuthorizer (c .sasToken )
154+ if err != nil {
155+ return nil , fmt .Errorf ("Error building Authorizer: %+v" , err )
156+ }
157+
158+ containersClient := containers .NewWithEnvironment (c .environment )
159+ c .configureClient (& containersClient .Client , storageAuth )
160+ return & containersClient , nil
141161 }
162+ accessKey := c .accessKey
163+ if accessKey == "" {
164+ log .Printf ("[DEBUG] Building the Container Client from an Access Token (using user credentials)" )
165+ keys , err := c .storageAccountsClient .ListKeys (ctx , c .resourceGroupName , c .storageAccountName )
166+ if err != nil {
167+ return nil , fmt .Errorf ("Error retrieving keys for Storage Account %q: %s" , c .storageAccountName , err )
168+ }
142169
143- accessKeys := * keys .Keys
144- accessKey := accessKeys [0 ].Value
170+ if keys .Keys == nil {
171+ return nil , fmt .Errorf ("Nil key returned for storage account %q" , c .storageAccountName )
172+ }
173+
174+ accessKeys := * keys .Keys
175+ accessKey = * accessKeys [0 ].Value
176+ }
145177
146- storageClient , err := storage . NewBasicClientOnSovereignCloud (c .storageAccountName , * accessKey , c . environment )
178+ storageAuth , err := autorest . NewSharedKeyAuthorizer (c .storageAccountName , accessKey , autorest . SharedKey )
147179 if err != nil {
148- return nil , fmt .Errorf ("Error creating storage client for storage account %q : %s" , c . storageAccountName , err )
180+ return nil , fmt .Errorf ("Error building Authorizer : %+v" , err )
149181 }
150- client := storageClient .GetBlobService ()
151- return & client , nil
182+
183+ containersClient := containers .NewWithEnvironment (c .environment )
184+ c .configureClient (& containersClient .Client , storageAuth )
185+ return & containersClient , nil
152186}
153187
154188func (c * ArmClient ) configureClient (client * autorest.Client , auth autorest.Authorizer ) {
0 commit comments