Skip to content

Commit 481b03c

Browse files
mbfrahrykatbyte
andauthored
Update azure backend storage sdk (#24669)
* update vendored azure sdk * vendor giovanni storage sdk * Add giovanni clients * go mod vendor * Swap to new storage sdk * workable tests * update .go-version to 1.14.2 * Tests working minus SAS * Add SAS Token support * Update vendor * Passing tests * Add date randomizer * Captalize RG * Remove random bits * Update client var name Co-authored-by: kt <kt@katbyte.me>
1 parent eead4c4 commit 481b03c

File tree

141 files changed

+7291
-8473
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+7291
-8473
lines changed

.go-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.14
1+
1.14.2

backend/remote-state/azure/arm_client.go

Lines changed: 63 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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

154188
func (c *ArmClient) configureClient(client *autorest.Client, auth autorest.Authorizer) {

backend/remote-state/azure/backend.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ type Backend struct {
149149
armClient *ArmClient
150150
containerName string
151151
keyName string
152+
accountName string
152153
}
153154

154155
type BackendConfig struct {
@@ -177,6 +178,7 @@ func (b *Backend) configure(ctx context.Context) error {
177178
// Grab the resource data
178179
data := schema.FromContextBackendConfig(ctx)
179180
b.containerName = data.Get("container_name").(string)
181+
b.accountName = data.Get("storage_account_name").(string)
180182
b.keyName = data.Get("key").(string)
181183

182184
// support for previously deprecated fields

backend/remote-state/azure/backend_state.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import (
66
"sort"
77
"strings"
88

9-
"github.com/Azure/azure-sdk-for-go/storage"
109
"github.com/hashicorp/terraform/backend"
1110
"github.com/hashicorp/terraform/state"
1211
"github.com/hashicorp/terraform/state/remote"
1312
"github.com/hashicorp/terraform/states"
13+
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/blobs"
14+
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/containers"
1415
)
1516

1617
const (
@@ -21,23 +22,22 @@ const (
2122

2223
func (b *Backend) Workspaces() ([]string, error) {
2324
prefix := b.keyName + keyEnvPrefix
24-
params := storage.ListBlobsParameters{
25-
Prefix: prefix,
25+
params := containers.ListBlobsInput{
26+
Prefix: &prefix,
2627
}
2728

2829
ctx := context.TODO()
29-
client, err := b.armClient.getBlobClient(ctx)
30+
client, err := b.armClient.getContainersClient(ctx)
3031
if err != nil {
3132
return nil, err
3233
}
33-
container := client.GetContainerReference(b.containerName)
34-
resp, err := container.ListBlobs(params)
34+
resp, err := client.ListBlobs(ctx, b.armClient.storageAccountName, b.containerName, params)
3535
if err != nil {
3636
return nil, err
3737
}
3838

3939
envs := map[string]struct{}{}
40-
for _, obj := range resp.Blobs {
40+
for _, obj := range resp.Blobs.Blobs {
4141
key := obj.Name
4242
if strings.HasPrefix(key, prefix) {
4343
name := strings.TrimPrefix(key, prefix)
@@ -69,11 +69,13 @@ func (b *Backend) DeleteWorkspace(name string) error {
6969
return err
7070
}
7171

72-
containerReference := client.GetContainerReference(b.containerName)
73-
blobReference := containerReference.GetBlobReference(b.path(name))
74-
options := &storage.DeleteBlobOptions{}
72+
if resp, err := client.Delete(ctx, b.armClient.storageAccountName, b.containerName, b.path(name), blobs.DeleteInput{}); err != nil {
73+
if resp.Response.StatusCode != 404 {
74+
return err
75+
}
76+
}
7577

76-
return blobReference.Delete(options)
78+
return nil
7779
}
7880

7981
func (b *Backend) StateMgr(name string) (state.State, error) {
@@ -84,9 +86,10 @@ func (b *Backend) StateMgr(name string) (state.State, error) {
8486
}
8587

8688
client := &RemoteClient{
87-
blobClient: *blobClient,
88-
containerName: b.containerName,
89-
keyName: b.path(name),
89+
giovanniBlobClient: *blobClient,
90+
containerName: b.containerName,
91+
keyName: b.path(name),
92+
accountName: b.accountName,
9093
}
9194

9295
stateMgr := &remote.State{Client: client}

0 commit comments

Comments
 (0)