Skip to content

Commit

Permalink
make azure storage account private
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinBisson committed Dec 5, 2024
1 parent 6ec3ad5 commit cbed879
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Secure Azure Storage Account by making them private.
- Update Kyverno PolicyException to v2beta1.

## [0.9.0] - 2024-10-03
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ module github.com/giantswarm/object-storage-operator

go 1.23

toolchain go1.23.4

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6 v6.1.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.6.0
github.com/aquilax/truncate v1.0.0
github.com/aws/aws-sdk-go-v2 v1.32.6
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.0 h1:+m0M/LFxN43KvUL
github.com/Azure/azure-sdk-for-go/sdk/azidentity/cache v0.3.0/go.mod h1:PwOyop78lveYMRs6oCxjiVyBdyCgIYH6XHIVZO9/SFQ=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 h1:ywEEhmNahHBihViHepv3xPBn1663uRv2t2q/ESv9seY=
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0/go.mod h1:iZDifYGJTIgIIkYRNWPENUnqx6bJ2xnSDFI2tjwZNuY=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3 v3.0.0 h1:Kb8eVvjdP6kZqYnER5w/PiGCFp91yVgaxve3d7kCEpY=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3 v3.0.0/go.mod h1:lYq15QkJyEsNegz5EhI/0SXQ6spvGfgwBH/Qyzkoc/s=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3 v3.1.0 h1:2qsIIvxVT+uE6yrNldntJKlLRgxGbZ85kgtz5SNBhMw=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/internal/v3 v3.1.0/go.mod h1:AW8VEadnhw9xox+VaVd9sP7NjzOAnaZBLRH6Tq3cJ38=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6 v6.1.0 h1:Fd+iaEa+JBwzYo6OTWYSNqyvlPSLciMGsmsnYCKcXM0=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6 v6.1.0/go.mod h1:ulHyBFJOI0ONiRL4vcJTmS7rx18jQQlEPmAgo80cRdM=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0 h1:Dd+RhdJn0OTtVGaeDLZpcumkIVCtA/3/Fo42+eoYvVM=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources v1.2.0/go.mod h1:5kakwfW5CjC9KK+Q4wjXAg+ShuIm2mBMua0ZFj2C8PE=
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage v1.6.0 h1:PiSrjRPpkQNjrM8H0WwKMnZUdu1RGMtd/LdGKUrOo+c=
Expand Down
8 changes: 8 additions & 0 deletions internal/pkg/service/objectstorage/cloud/azure/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,11 @@ func (c AzureCluster) GetCredentials() cluster.Credentials {
func (c AzureCluster) GetResourceGroup() string {
return c.Credentials.ResourceGroup
}

func (c AzureCluster) GetSubscriptionID() string {
return c.Credentials.SubscriptionID
}

func (c AzureCluster) GetVNetName() string {
return c.GetName() + "-vnet"
}
17 changes: 16 additions & 1 deletion internal/pkg/service/objectstorage/cloud/azure/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage"
"github.com/go-logr/logr"
"github.com/pkg/errors"
Expand Down Expand Up @@ -57,9 +58,23 @@ func (s AzureObjectStorageService) NewObjectStorageService(ctx context.Context,
return nil, errors.WithStack(err)
}

var networkClientFactory *armnetwork.ClientFactory
networkClientFactory, err = armnetwork.NewClientFactory(azureCredentials.SubscriptionID, cred, nil)
if err != nil {
return nil, errors.WithStack(err)
}

azurecluster, ok := cluster.(AzureCluster)
if !ok {
return nil, errors.New("Impossible to cast cluster into Azure cluster")
}
return NewAzureStorageService(storageClientFactory.NewAccountsClient(), storageClientFactory.NewBlobContainersClient(), storageClientFactory.NewManagementPoliciesClient(), logger, azurecluster, client), nil
return NewAzureStorageService(
storageClientFactory.NewAccountsClient(),
storageClientFactory.NewBlobContainersClient(),
storageClientFactory.NewManagementPoliciesClient(),
networkClientFactory.NewPrivateEndpointsClient(),
logger,
azurecluster,
client,
), nil
}
70 changes: 39 additions & 31 deletions internal/pkg/service/objectstorage/cloud/azure/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/storage/armstorage"
"github.com/go-logr/logr"
v1 "k8s.io/api/core/v1"
Expand All @@ -23,10 +24,10 @@ type AzureObjectStorageAdapter struct {
storageAccountClient *armstorage.AccountsClient
blobContainerClient *armstorage.BlobContainersClient
managementPoliciesClient *armstorage.ManagementPoliciesClient
privateEndpointsClient *armnetwork.PrivateEndpointsClient
logger logr.Logger
cluster AzureCluster
client client.Client
listStorageAccountName []string
}

// NewAzureStorageService creates a new instance of AzureObjectStorageAdapter.
Expand All @@ -35,16 +36,22 @@ type AzureObjectStorageAdapter struct {
// The logger is used for logging purposes.
// The cluster represents the Azure cluster.
// The client is the Kubernetes client used for interacting with the Kubernetes API.
// The listStorageAccountName is a list of storage account names.
func NewAzureStorageService(storageAccountClient *armstorage.AccountsClient, blobContainerClient *armstorage.BlobContainersClient, managementPoliciesClient *armstorage.ManagementPoliciesClient, logger logr.Logger, cluster AzureCluster, client client.Client) AzureObjectStorageAdapter {
func NewAzureStorageService(
storageAccountClient *armstorage.AccountsClient,
blobContainerClient *armstorage.BlobContainersClient,
managementPoliciesClient *armstorage.ManagementPoliciesClient,
privateEndpointsClient *armnetwork.PrivateEndpointsClient,
logger logr.Logger,
cluster AzureCluster,
client client.Client) AzureObjectStorageAdapter {
return AzureObjectStorageAdapter{
storageAccountClient: storageAccountClient,
blobContainerClient: blobContainerClient,
managementPoliciesClient: managementPoliciesClient,
privateEndpointsClient: privateEndpointsClient,
logger: logger,
cluster: cluster,
client: client,
listStorageAccountName: []string{},
}
}

Expand Down Expand Up @@ -98,40 +105,41 @@ func (s AzureObjectStorageAdapter) CreateBucket(ctx context.Context, bucket *v1a
if err != nil {
return fmt.Errorf("unable to retrieve access keys from storage account %s", storageAccountName)
}
// Then, we retrieve the Access Key for 'key1'
foundKey1 := false
for _, k := range listKeys.Keys {
if *k.KeyName == "key1" {
foundKey1 = true
// Finally, we create the Secret into the bucket namespace
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: bucket.Spec.Name,
Namespace: bucket.Namespace,
Labels: map[string]string{
"giantswarm.io/managed-by": "object-storage-operator",
},
Finalizers: []string{
v1alpha1.AzureSecretFinalizer,
},
},
Data: map[string][]byte{

secret := v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: bucket.Spec.Name,
Namespace: bucket.Namespace,
Labels: map[string]string{
"giantswarm.io/managed-by": "object-storage-operator",
},
Finalizers: []string{
v1alpha1.AzureSecretFinalizer,
},
},
}

_, err = controllerutil.CreateOrUpdate(ctx, s.client, &secret, func() error {
// Then, we retrieve the Access Key for 'key1'
for _, k := range listKeys.Keys {
if *k.KeyName == "key1" {
// Finally, we create the Secret into the bucket namespace
secret.Data = map[string][]byte{
"accountName": []byte(storageAccountName),
"accountKey": []byte(*k.Value),
},
}
return nil
}
err := s.client.Create(ctx, secret)
if err != nil {
return err
}
s.logger.Info(fmt.Sprintf("created secret %s", bucket.Spec.Name))
break
}
}
if !foundKey1 {

return fmt.Errorf("unable to retrieve access keys 'key1' from storage account %s", storageAccountName)
})

if err != nil {
return err
}

s.logger.Info(fmt.Sprintf("created secret %s", bucket.Spec.Name))
return nil
}

Expand Down

0 comments on commit cbed879

Please sign in to comment.