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

Add audit_policy detail in table azure_sql_database and azure_sql_server Closes #709 #711

Merged
merged 7 commits into from
Jan 8, 2024
131 changes: 131 additions & 0 deletions azure/table_azure_sql_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,13 @@ func tableAzureSqlDatabase(_ context.Context) *plugin.Table {
Hydrate: listSqlDatabaseVulnerabilityAssessmentScans,
Transform: transform.FromValue(),
},
{
Name: "audit_policy",
Description: "The database blob auditing policy.",
Type: proto.ColumnType_JSON,
Hydrate: getSqlDatabaseBlobAuditingPolicies,
Transform: transform.FromValue(),
},

// Steampipe standard columns
{
Expand Down Expand Up @@ -429,6 +436,130 @@ func getSqlDatabaseLongTermRetentionPolicies(ctx context.Context, d *plugin.Quer
return res[0], nil
}

func getSqlDatabaseBlobAuditingPolicies(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
database := h.Item.(sql.Database)
serverName := strings.Split(*database.ID, "/")[8]
databaseName := *database.Name
resourceGroupName := strings.Split(string(*database.ID), "/")[4]

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

client := sqlV5.NewDatabaseBlobAuditingPoliciesClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
client.Authorizer = session.Authorizer

op, err := client.ListByDatabase(ctx, resourceGroupName, serverName, databaseName)
if err != nil {
return nil, err
}

var blobPolicies []map[string]interface{}
for _, i := range op.Values() {
objectMap := make(map[string]interface{})
if i.ID != nil {
objectMap["id"] = i.ID
}
if i.Name != nil {
objectMap["name"] = i.Name
}
if i.Kind != nil {
objectMap["kind"] = i.Kind
}
if i.Type != nil {
objectMap["type"] = i.Type
}
if i.DatabaseBlobAuditingPolicyProperties != nil {
obMap := make(map[string]interface{})
if i.DatabaseBlobAuditingPolicyProperties.RetentionDays != nil {
obMap["retentionDays"] = i.DatabaseBlobAuditingPolicyProperties.RetentionDays
}
if i.DatabaseBlobAuditingPolicyProperties.AuditActionsAndGroups != nil {
obMap["AuditActionsAndGroups"] = i.DatabaseBlobAuditingPolicyProperties.AuditActionsAndGroups
}
if i.DatabaseBlobAuditingPolicyProperties.IsAzureMonitorTargetEnabled != nil {
obMap["isAzureMonitorTargetEnabled"] = i.DatabaseBlobAuditingPolicyProperties.IsAzureMonitorTargetEnabled
}
if i.DatabaseBlobAuditingPolicyProperties.IsStorageSecondaryKeyInUse != nil {
obMap["isStorageSecondaryKeyInUse"] = i.DatabaseBlobAuditingPolicyProperties.IsStorageSecondaryKeyInUse
}
if i.DatabaseBlobAuditingPolicyProperties.QueueDelayMs != nil {
obMap["queueDelayMs"] = i.DatabaseBlobAuditingPolicyProperties.QueueDelayMs
}
if i.DatabaseBlobAuditingPolicyProperties.State != "" {
obMap["state"] = i.DatabaseBlobAuditingPolicyProperties.State
}
if i.DatabaseBlobAuditingPolicyProperties.StorageEndpoint != nil {
obMap["storageEndpoint"] = i.DatabaseBlobAuditingPolicyProperties.StorageEndpoint
}
if i.DatabaseBlobAuditingPolicyProperties.StorageAccountAccessKey != nil {
obMap["storageAccountAccessKey"] = i.DatabaseBlobAuditingPolicyProperties.StorageAccountAccessKey
}
if i.DatabaseBlobAuditingPolicyProperties.StorageAccountSubscriptionID != nil {
obMap["storageAccountSubscriptionID"] = i.DatabaseBlobAuditingPolicyProperties.StorageAccountSubscriptionID
}
objectMap["databaseBlobAuditingPolicyProperties"] = obMap
}

blobPolicies = append(blobPolicies, objectMap)
}

if op.NotDone() {
for _, i := range op.Values() {
objectMap := make(map[string]interface{})
if i.ID != nil {
objectMap["id"] = i.ID
}
if i.Name != nil {
objectMap["name"] = i.Name
}
if i.Kind != nil {
objectMap["kind"] = i.Kind
}
if i.Type != nil {
objectMap["type"] = i.Type
}
if i.DatabaseBlobAuditingPolicyProperties != nil {
obMap := make(map[string]interface{})
if i.DatabaseBlobAuditingPolicyProperties.RetentionDays != nil {
obMap["retentionDays"] = i.DatabaseBlobAuditingPolicyProperties.RetentionDays
}
if i.DatabaseBlobAuditingPolicyProperties.AuditActionsAndGroups != nil {
obMap["AuditActionsAndGroups"] = i.DatabaseBlobAuditingPolicyProperties.AuditActionsAndGroups
}
if i.DatabaseBlobAuditingPolicyProperties.IsAzureMonitorTargetEnabled != nil {
obMap["isAzureMonitorTargetEnabled"] = i.DatabaseBlobAuditingPolicyProperties.IsAzureMonitorTargetEnabled
}
if i.DatabaseBlobAuditingPolicyProperties.IsStorageSecondaryKeyInUse != nil {
obMap["isStorageSecondaryKeyInUse"] = i.DatabaseBlobAuditingPolicyProperties.IsStorageSecondaryKeyInUse
}
if i.DatabaseBlobAuditingPolicyProperties.QueueDelayMs != nil {
obMap["queueDelayMs"] = i.DatabaseBlobAuditingPolicyProperties.QueueDelayMs
}
if i.DatabaseBlobAuditingPolicyProperties.State != "" {
obMap["state"] = i.DatabaseBlobAuditingPolicyProperties.State
}
if i.DatabaseBlobAuditingPolicyProperties.StorageEndpoint != nil {
obMap["storageEndpoint"] = i.DatabaseBlobAuditingPolicyProperties.StorageEndpoint
}
if i.DatabaseBlobAuditingPolicyProperties.StorageAccountAccessKey != nil {
obMap["storageAccountAccessKey"] = i.DatabaseBlobAuditingPolicyProperties.StorageAccountAccessKey
}
if i.DatabaseBlobAuditingPolicyProperties.StorageAccountSubscriptionID != nil {
obMap["storageAccountSubscriptionID"] = i.DatabaseBlobAuditingPolicyProperties.StorageAccountSubscriptionID
}
objectMap["databaseBlobAuditingPolicyProperties"] = obMap
}

blobPolicies = append(blobPolicies, objectMap)
}
}

return blobPolicies, nil
}

func listSqlDatabaseVulnerabilityAssessments(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
database := h.Item.(sql.Database)
serverName := strings.Split(*database.ID, "/")[8]
Expand Down
124 changes: 124 additions & 0 deletions azure/table_azure_sql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ func tableAzureSQLServer(_ context.Context) *plugin.Table {
Hydrate: listSQLServerVirtualNetworkRules,
Transform: transform.FromValue(),
},
{
Name: "audit_policy",
Description: "The SQL server blob auditing policy.",
Type: proto.ColumnType_JSON,
Hydrate: getSQLServerBlobAuditingPolicies,
Transform: transform.FromValue(),
},

// Steampipe standard columns
{
Expand Down Expand Up @@ -403,6 +410,123 @@ func getSQLServerSecurityAlertPolicy(ctx context.Context, d *plugin.QueryData, h
return securityAlertPolicies, nil
}

func getSQLServerBlobAuditingPolicies(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {

server := h.Item.(sqlv3.Server)

session, err := GetNewSession(ctx, d, "MANAGEMENT")
if err != nil {
return nil, err
}
subscriptionID := session.SubscriptionID
resourceGroupName := strings.Split(string(*server.ID), "/")[4]

client := sql.NewServerBlobAuditingPoliciesClientWithBaseURI(session.ResourceManagerEndpoint, subscriptionID)
client.Authorizer = session.Authorizer

op, err := client.ListByServer(ctx, resourceGroupName, *server.Name)
if err != nil {
return nil, err
}

var blobPolicies []map[string]interface{}
for _, i := range op.Values() {
objectMap := make(map[string]interface{})
if i.ID != nil {
objectMap["id"] = i.ID
}
if i.Name != nil {
objectMap["name"] = i.Name
}
if i.Type != nil {
objectMap["type"] = i.Type
}
if i.ServerBlobAuditingPolicyProperties != nil {
obMap := make(map[string]interface{})
if i.ServerBlobAuditingPolicyProperties.RetentionDays != nil {
obMap["retentionDays"] = i.ServerBlobAuditingPolicyProperties.RetentionDays
}
if i.ServerBlobAuditingPolicyProperties.AuditActionsAndGroups != nil {
obMap["AuditActionsAndGroups"] = i.ServerBlobAuditingPolicyProperties.AuditActionsAndGroups
}
if i.ServerBlobAuditingPolicyProperties.IsAzureMonitorTargetEnabled != nil {
obMap["isAzureMonitorTargetEnabled"] = i.ServerBlobAuditingPolicyProperties.IsAzureMonitorTargetEnabled
}
if i.ServerBlobAuditingPolicyProperties.IsStorageSecondaryKeyInUse != nil {
obMap["isStorageSecondaryKeyInUse"] = i.ServerBlobAuditingPolicyProperties.IsStorageSecondaryKeyInUse
}
if i.ServerBlobAuditingPolicyProperties.QueueDelayMs != nil {
obMap["queueDelayMs"] = i.ServerBlobAuditingPolicyProperties.QueueDelayMs
}
if i.ServerBlobAuditingPolicyProperties.State != "" {
obMap["state"] = i.ServerBlobAuditingPolicyProperties.State
}
if i.ServerBlobAuditingPolicyProperties.StorageEndpoint != nil {
obMap["storageEndpoint"] = i.ServerBlobAuditingPolicyProperties.StorageEndpoint
}
if i.ServerBlobAuditingPolicyProperties.StorageAccountAccessKey != nil {
obMap["storageAccountAccessKey"] = i.ServerBlobAuditingPolicyProperties.StorageAccountAccessKey
}
if i.ServerBlobAuditingPolicyProperties.StorageAccountSubscriptionID != nil {
obMap["storageAccountSubscriptionID"] = i.ServerBlobAuditingPolicyProperties.StorageAccountSubscriptionID
}
objectMap["serverBlobAuditingPolicyProperties"] = obMap
}

blobPolicies = append(blobPolicies, objectMap)
}

if op.NotDone() {
for _, i := range op.Values() {
objectMap := make(map[string]interface{})
if i.ID != nil {
objectMap["id"] = i.ID
}
if i.Name != nil {
objectMap["name"] = i.Name
}
if i.Type != nil {
objectMap["type"] = i.Type
}
if i.ServerBlobAuditingPolicyProperties != nil {
obMap := make(map[string]interface{})
if i.ServerBlobAuditingPolicyProperties.RetentionDays != nil {
obMap["retentionDays"] = i.ServerBlobAuditingPolicyProperties.RetentionDays
}
if i.ServerBlobAuditingPolicyProperties.AuditActionsAndGroups != nil {
obMap["AuditActionsAndGroups"] = i.ServerBlobAuditingPolicyProperties.AuditActionsAndGroups
}
if i.ServerBlobAuditingPolicyProperties.IsAzureMonitorTargetEnabled != nil {
obMap["isAzureMonitorTargetEnabled"] = i.ServerBlobAuditingPolicyProperties.IsAzureMonitorTargetEnabled
}
if i.ServerBlobAuditingPolicyProperties.IsStorageSecondaryKeyInUse != nil {
obMap["isStorageSecondaryKeyInUse"] = i.ServerBlobAuditingPolicyProperties.IsStorageSecondaryKeyInUse
}
if i.ServerBlobAuditingPolicyProperties.QueueDelayMs != nil {
obMap["queueDelayMs"] = i.ServerBlobAuditingPolicyProperties.QueueDelayMs
}
if i.ServerBlobAuditingPolicyProperties.State != "" {
obMap["state"] = i.ServerBlobAuditingPolicyProperties.State
}
if i.ServerBlobAuditingPolicyProperties.StorageEndpoint != nil {
obMap["storageEndpoint"] = i.ServerBlobAuditingPolicyProperties.StorageEndpoint
}
if i.ServerBlobAuditingPolicyProperties.StorageAccountAccessKey != nil {
obMap["storageAccountAccessKey"] = i.ServerBlobAuditingPolicyProperties.StorageAccountAccessKey
}
if i.ServerBlobAuditingPolicyProperties.StorageAccountSubscriptionID != nil {
obMap["storageAccountSubscriptionID"] = i.ServerBlobAuditingPolicyProperties.StorageAccountSubscriptionID
}
objectMap["serverBlobAuditingPolicyProperties"] = obMap
}

blobPolicies = append(blobPolicies, objectMap)
}
}

return blobPolicies, nil
}

func getSQLServerAzureADAdministrator(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
plugin.Logger(ctx).Trace("getSQLServerAzureADAdministrator")
server := h.Item.(sqlv3.Server)
Expand Down