Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Update azure_storage_account table to include blob service logging details. Closes #79 #80

Merged
merged 2 commits into from
Apr 7, 2021
Merged
Changes from all commits
Commits
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
65 changes: 60 additions & 5 deletions azure/table_azure_storage_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-06-01/storage"
"github.com/Azure/go-autorest/autorest"
"github.com/tombuildsstuff/giovanni/storage/2018-11-09/queue/queues"
"github.com/tombuildsstuff/giovanni/storage/2019-12-12/blob/accounts"
"github.com/turbot/steampipe-plugin-sdk/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/plugin/transform"

Expand Down Expand Up @@ -122,6 +123,13 @@ func tableAzureStorageAccount(_ context.Context) *plugin.Table {
Hydrate: getAzureStorageAccountBlobProperties,
Transform: transform.FromField("BlobServicePropertiesProperties.RestorePolicy.Enabled"),
},
{
Name: "blob_service_logging",
Description: "Specifies the blob service properties for logging access.",
Type: proto.ColumnType_JSON,
Hydrate: getAzureStorageAccountBlobServiceLogging,
Transform: transform.FromValue(),
},
{
Name: "blob_soft_delete_enabled",
Description: "Specifies whether DeleteRetentionPolicy is enabled.",
Expand Down Expand Up @@ -230,7 +238,7 @@ func tableAzureStorageAccount(_ context.Context) *plugin.Table {
Description: "Indicates the number of days that metrics or logging data should be retained.",
Type: proto.ColumnType_INT,
Hydrate: getAzureStorageAccountQueueProperties,
Transform: transform.FromField("Logging.RetentionPolicy.Days"),
Transform: transform.FromField("Logging.RetentionPolicy.Days").Transform(transform.NullIfZeroValue),
},
{
Name: "logging_retention_enabled",
Expand Down Expand Up @@ -397,7 +405,7 @@ func tableAzureStorageAccount(_ context.Context) *plugin.Table {
}
}

//// FETCH FUNCTIONS ////
//// LIST FUNCTION

func listStorageAccounts(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
session, err := GetNewSession(ctx, d, "MANAGEMENT")
Expand Down Expand Up @@ -427,7 +435,7 @@ func listStorageAccounts(ctx context.Context, d *plugin.QueryData, _ *plugin.Hyd
return nil, err
}

//// HYDRATE FUNCTIONS ////
//// HYDRATE FUNCTIONS

func getStorageAccount(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getStorageAccount")
Expand Down Expand Up @@ -470,6 +478,53 @@ func getAzureStorageAccountBlobProperties(ctx context.Context, d *plugin.QueryDa
return op, nil
}

func getAzureStorageAccountBlobServiceLogging(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
accountData := h.Item.(*storageAccountInfo)

// Create session
session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}
subscriptionID := session.SubscriptionID

storageClient := storage.NewAccountsClient(subscriptionID)
storageClient.Authorizer = session.Authorizer

accountKeys, err := storageClient.ListKeys(ctx, *accountData.ResourceGroup, *accountData.Name, "")
if err != nil {
// storage.AccountsClient#ListKeys: Failure sending request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status=<nil> Code="ScopeLocked"
// Message="The scope '/subscriptions/********-****-****-****-************/resourceGroups/turbot_rg/providers/Microsoft.Storage/storageAccounts/delmett'
// cannot perform write operation because following scope(s) are locked: '/subscriptions/********-****-****-****-************/resourcegroups/turbot_rg/providers/Microsoft.Storage/storageAccounts/delmett'.
// Please remove the lock and try again."
if strings.Contains(err.Error(), "ScopeLocked") {
return nil, nil
}
return nil, err
}

if *accountKeys.Keys != nil || len(*accountKeys.Keys) > 0 {
key := (*accountKeys.Keys)[0]
storageAuth, err := autorest.NewSharedKeyAuthorizer(*accountData.Name, *key.Value, autorest.SharedKeyLite)
if err != nil {
return nil, err
}

client := accounts.New()
client.Client.Authorizer = storageAuth

resp, err := client.GetServiceProperties(ctx, *accountData.Name)
if err != nil {
if strings.Contains(err.Error(), "FeatureNotSupportedForAccount") {
return nil, nil
}
return nil, err
}
return resp.StorageServiceProperties.Logging, nil
}
return nil, nil
}

func getAzureStorageAccountFileProperties(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
accountData := h.Item.(*storageAccountInfo)

Expand Down Expand Up @@ -518,8 +573,8 @@ func getAzureStorageAccountQueueProperties(ctx context.Context, d *plugin.QueryD
accountKeys, err := storageClient.ListKeys(ctx, *accountData.ResourceGroup, *accountData.Name, "")
if err != nil {
// storage.AccountsClient#ListKeys: Failure sending request: StatusCode=409 -- Original Error: autorest/azure: Service returned an error. Status=<nil> Code="ScopeLocked"
// Message="The scope '/subscriptions/d7245080-b4ae-4fe5-b6fa-2e71b3dae6c8/resourceGroups/turbot_rg/providers/Microsoft.Storage/storageAccounts/delmett'
// cannot perform write operation because following scope(s) are locked: '/subscriptions/d7245080-b4ae-4fe5-b6fa-2e71b3dae6c8/resourcegroups/turbot_rg/providers/Microsoft.Storage/storageAccounts/delmett'.
// Message="The scope '/subscriptions/********-****-****-****-************/resourceGroups/turbot_rg/providers/Microsoft.Storage/storageAccounts/delmett'
// cannot perform write operation because following scope(s) are locked: '/subscriptions/********-****-****-****-************/resourcegroups/turbot_rg/providers/Microsoft.Storage/storageAccounts/delmett'.
// Please remove the lock and try again."
if strings.Contains(err.Error(), "ScopeLocked") {
return nil, nil
Expand Down