Skip to content
New issue

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

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

Already on GitHub? # to your account

Storage Credentials #1004

Merged
merged 34 commits into from
May 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
0ed2519
Merge pull request #18 from Azure/master
WilliamMortlMicrosoft Apr 3, 2020
71a5ccc
Merge pull request #19 from Azure/master
WilliamMortlMicrosoft Apr 6, 2020
26aff09
Merge pull request #20 from Azure/master
WilliamMortlMicrosoft Apr 7, 2020
4869809
Merge pull request #21 from Azure/master
WilliamMortlMicrosoft Apr 8, 2020
48d9e77
Merge pull request #22 from Azure/master
WilliamMortlMicrosoft Apr 10, 2020
17b4a4a
Merge pull request #23 from Azure/master
WilliamMortlMicrosoft Apr 13, 2020
c9d6227
Merge pull request #24 from Azure/master
WilliamMortlMicrosoft Apr 13, 2020
e1a1e4a
Merge pull request #25 from Azure/master
WilliamMortlMicrosoft Apr 14, 2020
0fa4980
Merge pull request #26 from Azure/master
WilliamMortlMicrosoft Apr 15, 2020
afdab93
Merge pull request #27 from Azure/master
WilliamMortlMicrosoft Apr 16, 2020
5fe07e6
Merge pull request #28 from Azure/master
WilliamMortlMicrosoft Apr 19, 2020
ffae6f5
Merge pull request #29 from Azure/master
WilliamMortlMicrosoft Apr 20, 2020
0cd7941
Merge pull request #30 from Azure/master
WilliamMortlMicrosoft Apr 21, 2020
0d671f5
Merge pull request #31 from Azure/master
WilliamMortlMicrosoft Apr 22, 2020
3166ecd
Merge pull request #32 from Azure/master
WilliamMortlMicrosoft Apr 22, 2020
81d67f9
Merge pull request #33 from Azure/master
WilliamMortlMicrosoft Apr 23, 2020
4619ee6
Merge pull request #34 from Azure/master
WilliamMortlMicrosoft Apr 24, 2020
d6a8761
Merge pull request #35 from Azure/master
WilliamMortlMicrosoft Apr 26, 2020
93d690f
first
Apr 26, 2020
64ebc38
works
Apr 27, 2020
17456ab
Merge branch 'master' into newCredentials
WilliamMortlMicrosoft Apr 27, 2020
1ea5b9d
reconcile code
Apr 27, 2020
fe60343
lower case
Apr 27, 2020
e76a08e
Merge branch 'master' into newCredentials
frodopwns Apr 28, 2020
58b0b3a
Merge branch 'master' into newCredentials
jananivMS Apr 28, 2020
ee16ffc
Merge branch 'master' into newCredentials
WilliamMortlMicrosoft Apr 29, 2020
34fe0e1
janani feedback and merge fix
Apr 29, 2020
48eac2c
Merge branch 'master' into newCredentials
WilliamMortlMicrosoft Apr 30, 2020
9fd39e4
forgot controllers
Apr 30, 2020
cca7340
Merge branch 'master' into newCredentials
WilliamMortlMicrosoft Apr 30, 2020
71c625d
Merge branch 'master' into newCredentials
frodopwns Apr 30, 2020
fda2e61
janani feedback
Apr 30, 2020
69fdc10
Merge branch 'master' into newCredentials
jananivMS Apr 30, 2020
86e1a1d
Merge branch 'master' into newCredentials
WilliamMortlMicrosoft Apr 30, 2020
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
2 changes: 1 addition & 1 deletion controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ func setup() error {
err = (&StorageAccountReconciler{
Reconciler: &AsyncReconciler{
Client: k8sManager.GetClient(),
AzureClient: resourcemanagerstorageaccount.New(),
AzureClient: resourcemanagerstorageaccount.New(secretClient, k8sManager.GetScheme()),
Telemetry: telemetry.InitializeTelemetryDefault(
"StorageAccount",
ctrl.Log.WithName("controllers").WithName("StorageAccount"),
Expand Down
7 changes: 6 additions & 1 deletion docs/storage/storageaccount.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ A Storage Account needs the following fields to deploy, along with a location an

## Deploy, view and delete resources

You can follow the steps [here](/docs/customresource.md) to deploy, view and delete resources.
You can follow the steps [here](/docs/customresource.md) to deploy, view and delete resources.

## Secrets
After creating a storage account, the operator stores a JSON formatted secret with the following fields. For more details on where the secrets are stored, look [here](/docs/secrets.md).
* `key1` (depending on the number of keys, there could be up to keyn)
* `connectionString1` (depending on the number of keys, there could be up to connectionStringn)
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func main() {
err = (&controllers.StorageAccountReconciler{
Reconciler: &controllers.AsyncReconciler{
Client: mgr.GetClient(),
AzureClient: storageaccountManager.New(),
AzureClient: storageaccountManager.New(secretClient, scheme),
Telemetry: telemetry.InitializeTelemetryDefault(
"StorageAccount",
ctrl.Log.WithName("controllers").WithName("StorageAccount"),
Expand Down
10 changes: 7 additions & 3 deletions pkg/resourcemanager/storages/managers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ package storages
import (
"github.com/Azure/azure-service-operator/pkg/resourcemanager/storages/blobcontainer"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/storages/storageaccount"
"github.com/Azure/azure-service-operator/pkg/secrets"
"k8s.io/apimachinery/pkg/runtime"
)

type StorageManagers struct {
StorageAccount storageaccount.StorageManager
BlobContainer blobcontainer.BlobContainerManager
}

var AzureStorageManagers = StorageManagers{
StorageAccount: storageaccount.New(),
BlobContainer: blobcontainer.New(),
func NewAzureStorageManagers(secretClient secrets.SecretClient, scheme *runtime.Scheme) StorageManagers {
return StorageManagers{
StorageAccount: storageaccount.New(secretClient, scheme),
BlobContainer: blobcontainer.New(),
}
}
48 changes: 47 additions & 1 deletion pkg/resourcemanager/storages/storageaccount/storageaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,29 @@ package storageaccount
import (
"context"
"errors"
"fmt"
"net/http"
"strings"

"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage"
"github.com/Azure/azure-service-operator/api/v1alpha1"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
resourcemgrconfig "github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/iam"
"github.com/Azure/azure-service-operator/pkg/secrets"
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/to"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)

type azureStorageManager struct{}
const templateForConnectionString = "DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s;EndpointSuffix=%s"

type azureStorageManager struct {
SecretClient secrets.SecretClient
Scheme *runtime.Scheme
}

// ParseNetworkPolicy - helper function to parse network policies from Kubernetes spec
func ParseNetworkPolicy(ruleSet *v1alpha1.StorageNetworkRuleSet) storage.NetworkRuleSet {
Expand Down Expand Up @@ -183,3 +193,39 @@ func (_ *azureStorageManager) ListKeys(ctx context.Context, resourceGroupName st

return storagesClient.ListKeys(ctx, resourceGroupName, accountName, storage.Kerb)
}

// StoreSecrets upserts the secret information for this storage account
func (s *azureStorageManager) StoreSecrets(ctx context.Context, resourceGroupName string, accountName string, instance *v1alpha1.StorageAccount) error {

// get the keys
keyResult, err := s.ListKeys(ctx, resourceGroupName, accountName)
if err != nil {
return err
}
if keyResult.Keys == nil {
return fmt.Errorf("No keys were returned from ListKeys")
}
keys := *keyResult.Keys
storageEndpointSuffix := resourcemgrconfig.Environment().StorageEndpointSuffix

// build the connection string
data := map[string][]byte{
"StorageAccountName": []byte(accountName),
}
for i, key := range keys {
data[fmt.Sprintf("connectionString%v", i)] = []byte(fmt.Sprintf(templateForConnectionString, accountName, *key.Value, storageEndpointSuffix))
data[fmt.Sprintf("key%v", i)] = []byte(*key.Value)
}

// upsert
key := types.NamespacedName{
Name: fmt.Sprintf("storageaccount-%s-%s", resourceGroupName, accountName),
Namespace: instance.Namespace,
}
return s.SecretClient.Upsert(ctx,
key,
data,
secrets.WithOwner(instance),
secrets.WithScheme(s.Scheme),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ import (
"context"

"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage"
"github.com/Azure/azure-service-operator/api/v1alpha1"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/resourcemanager"
"github.com/Azure/azure-service-operator/pkg/secrets"
"github.com/Azure/go-autorest/autorest"
"k8s.io/apimachinery/pkg/runtime"
)

// New returns an instance of the Storage Account Client
func New() *azureStorageManager {
return &azureStorageManager{}
func New(secretClient secrets.SecretClient, scheme *runtime.Scheme) *azureStorageManager {
return &azureStorageManager{
SecretClient: secretClient,
Scheme: scheme,
}
}

type StorageManager interface {
Expand Down Expand Up @@ -42,5 +48,10 @@ type StorageManager interface {

ListKeys(ctx context.Context, groupName string, storageAccountName string) (result storage.AccountListKeysResult, err error)

StoreSecrets(ctx context.Context,
resourceGroupName string,
accountName string,
instance *v1alpha1.StorageAccount) error

resourcemanager.ARMClient
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ func (sa *azureStorageManager) Ensure(ctx context.Context, obj runtime.Object, o
}

if instance.Status.State == "Succeeded" {

// upsert
err = sa.StoreSecrets(ctx, groupName, name, instance)
if err != nil {
return false, err
}

// everything finished successfully!
instance.Status.Message = resourcemanager.SuccessMsg
instance.Status.Provisioned = true
instance.Status.Provisioning = false
Expand Down Expand Up @@ -166,6 +174,12 @@ func (sa *azureStorageManager) Delete(ctx context.Context, obj runtime.Object, o

name := instance.ObjectMeta.Name
groupName := instance.Spec.ResourceGroup
key := types.NamespacedName{
Name: fmt.Sprintf("storageaccount-%s-%s",
instance.Spec.ResourceGroup,
instance.Name),
Namespace: instance.Namespace,
}

_, err = sa.DeleteStorage(ctx, groupName, name)
if err != nil {
Expand All @@ -177,6 +191,8 @@ func (sa *azureStorageManager) Delete(ctx context.Context, obj runtime.Object, o
err = errhelp.NewAzureError(err)
if azerr, ok := err.(*errhelp.AzureError); ok {
if helpers.ContainsString(catch, azerr.Type) {
// Best case deletion of secrets
sa.SecretClient.Delete(ctx, key)
return false, nil
}
}
Expand All @@ -186,6 +202,8 @@ func (sa *azureStorageManager) Delete(ctx context.Context, obj runtime.Object, o
_, err = sa.GetStorage(ctx, groupName, name)
if err != nil {
if errhelp.IsStatusCode404(err) {
// Best case deletion of secrets
sa.SecretClient.Delete(ctx, key)
return false, nil
}
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/resourcemanager/storages/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type TestContext struct {
ResourceGroupName string
ResourceGroupLocation string
ResourceGroupManager resourcegroupsresourcemanager.ResourceGroupManager
StorageManagers StorageManagers
timeout time.Duration
retryInterval time.Duration
}
Expand Down Expand Up @@ -72,7 +71,6 @@ var _ = BeforeSuite(func() {
ResourceGroupName: resourceGroupName,
ResourceGroupLocation: resourceGroupLocation,
ResourceGroupManager: resourceGroupManager,
StorageManagers: AzureStorageManagers,
timeout: time.Second * 300,
retryInterval: time.Second * 1,
}
Expand Down