2017-03-30 16:33:54 +02:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2018-11-21 22:06:03 +01:00
|
|
|
"context"
|
|
|
|
"os"
|
2017-03-30 16:33:54 +02:00
|
|
|
"testing"
|
|
|
|
|
2021-05-17 17:42:17 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/backend"
|
2021-01-20 19:18:02 +01:00
|
|
|
"github.com/hashicorp/terraform/internal/legacy/helper/acctest"
|
2021-05-17 21:43:35 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/states/remote"
|
2020-05-20 17:29:02 +02:00
|
|
|
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/blob/blobs"
|
2017-03-30 16:33:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRemoteClient_impl(t *testing.T) {
|
|
|
|
var _ remote.Client = new(RemoteClient)
|
|
|
|
var _ remote.ClientLocker = new(RemoteClient)
|
|
|
|
}
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
func TestRemoteClientAccessKeyBasic(t *testing.T) {
|
|
|
|
testAccAzureBackend(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-21 22:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-30 16:33:54 +02:00
|
|
|
"storage_account_name": res.storageAccountName,
|
2018-11-21 22:06:03 +01:00
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"access_key": res.storageAccountAccessKey,
|
2018-11-22 18:02:33 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-03-21 02:43:02 +01:00
|
|
|
})).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-10-01 23:35:23 +02:00
|
|
|
state, err := b.StateMgr(backend.DefaultStateName)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
|
|
|
}
|
|
|
|
|
2018-11-22 16:52:27 +01:00
|
|
|
func TestRemoteClientManagedServiceIdentityBasic(t *testing.T) {
|
|
|
|
testAccAzureBackendRunningInAzure(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-22 16:52:27 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"resource_group_name": res.resourceGroup,
|
|
|
|
"use_msi": true,
|
2018-11-26 11:19:43 +01:00
|
|
|
"subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"tenant_id": os.Getenv("ARM_TENANT_ID"),
|
2018-11-22 16:52:27 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-22 16:52:27 +01:00
|
|
|
})).(*Backend)
|
|
|
|
|
|
|
|
state, err := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
|
|
|
}
|
|
|
|
|
2018-11-22 18:02:33 +01:00
|
|
|
func TestRemoteClientSasTokenBasic(t *testing.T) {
|
2018-11-21 22:06:03 +01:00
|
|
|
testAccAzureBackend(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-21 22:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
2018-11-22 18:02:33 +01:00
|
|
|
|
|
|
|
sasToken, err := buildSasToken(res.storageAccountName, res.storageAccountAccessKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error building SAS Token: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"sas_token": *sasToken,
|
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-22 18:02:33 +01:00
|
|
|
})).(*Backend)
|
|
|
|
|
|
|
|
state, err := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteClientServicePrincipalBasic(t *testing.T) {
|
|
|
|
testAccAzureBackend(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-21 22:06:03 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-22 18:02:33 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
2018-11-21 22:06:03 +01:00
|
|
|
|
|
|
|
b := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"resource_group_name": res.resourceGroup,
|
2018-11-26 11:19:43 +01:00
|
|
|
"subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"client_secret": os.Getenv("ARM_CLIENT_SECRET"),
|
2018-11-21 22:06:03 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-21 22:06:03 +01:00
|
|
|
})).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
state, err := b.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.TestClient(t, state.(*remote.State).Client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteClientAccessKeyLocks(t *testing.T) {
|
|
|
|
testAccAzureBackend(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-21 22:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-30 16:33:54 +02:00
|
|
|
"storage_account_name": res.storageAccountName,
|
2018-11-21 22:06:03 +01:00
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"access_key": res.storageAccountAccessKey,
|
2018-11-22 18:02:33 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-03-21 02:43:02 +01:00
|
|
|
})).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-03-21 02:43:02 +01:00
|
|
|
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
2017-03-30 16:33:54 +02:00
|
|
|
"storage_account_name": res.storageAccountName,
|
2018-11-21 22:06:03 +01:00
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"access_key": res.storageAccountAccessKey,
|
2018-11-22 18:02:33 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-21 22:06:03 +01:00
|
|
|
})).(*Backend)
|
|
|
|
|
|
|
|
s1, err := b1.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s2, err := b2.StateMgr(backend.DefaultStateName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.TestRemoteLocks(t, s1.(*remote.State).Client, s2.(*remote.State).Client)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoteClientServicePrincipalLocks(t *testing.T) {
|
|
|
|
testAccAzureBackend(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-21 22:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b1 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"resource_group_name": res.resourceGroup,
|
2018-11-26 11:19:43 +01:00
|
|
|
"subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"client_secret": os.Getenv("ARM_CLIENT_SECRET"),
|
2018-11-21 22:06:03 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-11-21 22:06:03 +01:00
|
|
|
})).(*Backend)
|
|
|
|
|
|
|
|
b2 := backend.TestBackendConfig(t, New(), backend.TestWrapConfig(map[string]interface{}{
|
|
|
|
"storage_account_name": res.storageAccountName,
|
|
|
|
"container_name": res.storageContainerName,
|
|
|
|
"key": res.storageKeyName,
|
|
|
|
"resource_group_name": res.resourceGroup,
|
2018-11-26 11:19:43 +01:00
|
|
|
"subscription_id": os.Getenv("ARM_SUBSCRIPTION_ID"),
|
|
|
|
"tenant_id": os.Getenv("ARM_TENANT_ID"),
|
|
|
|
"client_id": os.Getenv("ARM_CLIENT_ID"),
|
|
|
|
"client_secret": os.Getenv("ARM_CLIENT_SECRET"),
|
2018-11-21 22:06:03 +01:00
|
|
|
"environment": os.Getenv("ARM_ENVIRONMENT"),
|
2018-11-26 14:42:16 +01:00
|
|
|
"endpoint": os.Getenv("ARM_ENDPOINT"),
|
2018-03-21 02:43:02 +01:00
|
|
|
})).(*Backend)
|
2017-03-30 16:33:54 +02:00
|
|
|
|
2018-10-01 23:35:23 +02:00
|
|
|
s1, err := b1.StateMgr(backend.DefaultStateName)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-10-01 23:35:23 +02:00
|
|
|
s2, err := b2.StateMgr(backend.DefaultStateName)
|
2017-03-30 16:33:54 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
remote.TestRemoteLocks(t, s1.(*remote.State).Client, s2.(*remote.State).Client)
|
|
|
|
}
|
2017-09-04 13:04:43 +02:00
|
|
|
|
|
|
|
func TestPutMaintainsMetaData(t *testing.T) {
|
2018-11-21 22:06:03 +01:00
|
|
|
testAccAzureBackend(t)
|
|
|
|
rs := acctest.RandString(4)
|
|
|
|
res := testResourceNames(rs, "testState")
|
|
|
|
armClient := buildTestClient(t, res)
|
|
|
|
|
|
|
|
ctx := context.TODO()
|
|
|
|
err := armClient.buildTestResources(ctx, &res)
|
2018-11-22 18:02:33 +01:00
|
|
|
defer armClient.destroyTestResources(ctx, res)
|
2018-11-21 22:06:03 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error creating Test Resources: %q", err)
|
|
|
|
}
|
2017-09-04 13:04:43 +02:00
|
|
|
|
|
|
|
headerName := "acceptancetest"
|
|
|
|
expectedValue := "f3b56bad-33ad-4b93-a600-7a66e9cbd1eb"
|
|
|
|
|
2018-11-21 22:06:03 +01:00
|
|
|
client, err := armClient.getBlobClient(ctx)
|
2017-09-04 13:04:43 +02:00
|
|
|
if err != nil {
|
2018-11-21 22:06:03 +01:00
|
|
|
t.Fatalf("Error building Blob Client: %+v", err)
|
2017-09-04 13:04:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
_, err = client.PutBlockBlob(ctx, res.storageAccountName, res.storageContainerName, res.storageKeyName, blobs.PutBlockBlobInput{})
|
2017-09-04 13:04:43 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error Creating Block Blob: %+v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
blobReference, err := client.GetProperties(ctx, res.storageAccountName, res.storageContainerName, res.storageKeyName, blobs.GetPropertiesInput{})
|
2017-09-04 13:04:43 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error loading MetaData: %+v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
blobReference.MetaData[headerName] = expectedValue
|
|
|
|
opts := blobs.SetMetaDataInput{
|
|
|
|
MetaData: blobReference.MetaData,
|
|
|
|
}
|
|
|
|
_, err = client.SetMetaData(ctx, res.storageAccountName, res.storageContainerName, res.storageKeyName, opts)
|
2017-09-04 13:04:43 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error setting MetaData: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the metadata using the Backend
|
|
|
|
remoteClient := RemoteClient{
|
2018-11-21 22:06:03 +01:00
|
|
|
keyName: res.storageKeyName,
|
|
|
|
containerName: res.storageContainerName,
|
2020-05-20 17:29:02 +02:00
|
|
|
accountName: res.storageAccountName,
|
2018-11-21 22:06:03 +01:00
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
giovanniBlobClient: *client,
|
2017-09-04 13:04:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bytes := []byte(acctest.RandString(20))
|
|
|
|
err = remoteClient.Put(bytes)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error putting data: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify it still exists
|
2020-05-20 17:29:02 +02:00
|
|
|
blobReference, err = client.GetProperties(ctx, res.storageAccountName, res.storageContainerName, res.storageKeyName, blobs.GetPropertiesInput{})
|
2017-09-04 13:04:43 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error loading MetaData: %+v", err)
|
|
|
|
}
|
|
|
|
|
2020-05-20 17:29:02 +02:00
|
|
|
if blobReference.MetaData[headerName] != expectedValue {
|
|
|
|
t.Fatalf("%q was not set to %q in the MetaData: %+v", headerName, expectedValue, blobReference.MetaData)
|
2017-09-04 13:04:43 +02:00
|
|
|
}
|
|
|
|
}
|