Skip to content
Draft
Show file tree
Hide file tree
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
58 changes: 50 additions & 8 deletions pkg/iac/adapters/arm/storage/adapt.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,8 @@ func adaptAccounts(deployment azure.Deployment) []storage.Account {
// The default interpretation is TLS 1.0 for this property.
MinimumTLSVersion: resource.Properties.GetMapValue("minimumTlsVersion").
AsStringValue("TLS1_0", resource.Properties.GetMetadata()),
Queues: queues,
BlobProperties: storage.BlobProperties{
Metadata: resource.Properties.GetMetadata(),
DeleteRetentionPolicy: storage.DeleteRetentionPolicy{
Metadata: resource.Properties.GetMetadata(),
Days: resource.Properties.GetMapValue("blobServices").GetMapValue("properties").GetMapValue("deleteRetentionPolicy").GetMapValue("days").AsIntValue(0, resource.Properties.GetMetadata()),
},
},
Queues: queues,
BlobProperties: adaptBlobProperties(resource),
AccountReplicationType: resource.Properties.GetMapValue("sku").GetMapValue("name").AsStringValue("", resource.Properties.GetMetadata()),
InfrastructureEncryptionEnabled: resource.Properties.GetMapValue("encryption").GetMapValue("requireInfrastructureEncryption").AsBoolValue(false, resource.Properties.GetMetadata()),
CustomerManagedKey: storage.CustomerManagedKey{
Expand Down Expand Up @@ -91,6 +85,54 @@ func adaptAccounts(deployment azure.Deployment) []storage.Account {
return accounts
}

func adaptBlobProperties(account azure.Resource) storage.BlobProperties {
service, ok := findBlobService(account)
if !ok {
return storage.BlobProperties{
Metadata: account.Properties.GetMetadata(),
DeleteRetentionPolicy: storage.DeleteRetentionPolicy{
Metadata: account.Properties.GetMetadata(),
Days: types.IntDefault(0, account.Properties.GetMetadata()),
},
}
}

policy := service.Properties.GetMapValue("deleteRetentionPolicy")
if policy.IsNull() {
return storage.BlobProperties{
Metadata: service.Metadata,
DeleteRetentionPolicy: storage.DeleteRetentionPolicy{
Metadata: service.Metadata,
Days: types.IntDefault(0, service.Metadata),
},
}
}

// With soft delete disabled, deleted blobs are removed immediately and days has no effect.
// See https://learn.microsoft.com/en-us/rest/api/storageservices/set-blob-service-properties#request-body
days := policy.GetMapValue("days").AsIntValue(0, policy.GetMetadata())
if enabled := policy.GetMapValue("enabled").AsBoolValue(false, policy.GetMetadata()); enabled.IsFalse() {
days = types.Int(0, enabled.GetMetadata())
}

return storage.BlobProperties{
Metadata: service.Metadata,
DeleteRetentionPolicy: storage.DeleteRetentionPolicy{
Metadata: policy.GetMetadata(),
Days: days,
},
}
}

func findBlobService(account azure.Resource) (azure.Resource, bool) {
for _, typ := range []string{"blobServices", "Microsoft.Storage/storageAccounts/blobServices"} {
if services := account.GetResourcesByType(typ); len(services) > 0 {
return services[0], true
}
}
return azure.Resource{}, false
}

func adaptNetworkRules(resource azure.Resource) []storage.NetworkRule {
defaultBypasses := []types.StringValue{types.StringDefault("AzureServices", resource.Metadata)}
acl := resource.Properties.GetMapValue("networkAcls")
Expand Down
122 changes: 122 additions & 0 deletions pkg/iac/adapters/arm/storage/adapt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,105 @@ func TestAdapt(t *testing.T) {
}},
},
},
{
name: "nested blob service with soft delete",
source: `{
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"properties": {},
"resources": [
{
"type": "blobServices",
"name": "default",
"properties": {
"deleteRetentionPolicy": {
"enabled": true,
"days": 7
}
}
}
]
}
]
}`,
expected: storage.Storage{
Accounts: []storage.Account{accountWithBlobRetentionDays(7)},
},
},
{
name: "nested blob service with full resource type",
source: `{
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"properties": {},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts/blobServices",
"name": "default",
"properties": {
"deleteRetentionPolicy": {
"enabled": true,
"days": 14
}
}
}
]
}
]
}`,
expected: storage.Storage{
Accounts: []storage.Account{accountWithBlobRetentionDays(14)},
},
},
{
name: "nested blob service with soft delete disabled",
source: `{
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"properties": {},
"resources": [
{
"type": "blobServices",
"name": "default",
"properties": {
"deleteRetentionPolicy": {
"enabled": false,
"days": 7
}
}
}
]
}
]
}`,
expected: storage.Storage{
Accounts: []storage.Account{accountWithBlobRetentionDays(0)},
},
},
{
name: "nested blob service without retention policy",
source: `{
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"properties": {},
"resources": [
{
"type": "blobServices",
"name": "default",
"properties": {}
}
]
}
]
}`,
expected: storage.Storage{
Accounts: []storage.Account{accountWithBlobRetentionDays(0)},
},
},
}

for _, tt := range tests {
Expand All @@ -100,3 +199,26 @@ func TestAdapt(t *testing.T) {
})
}
}

func accountWithBlobRetentionDays(days int) storage.Account {
return storage.Account{
MinimumTLSVersion: types.StringTest("TLS1_0"),
EnforceHTTPS: types.BoolTest(true),
NetworkRules: []storage.NetworkRule{{
Bypass: []types.StringValue{types.StringTest("AzureServices")},
AllowByDefault: types.BoolTest(true),
}},
PublicNetworkAccess: types.BoolTest(true),
AccountReplicationType: types.StringTest(""),
InfrastructureEncryptionEnabled: types.BoolTest(false),
BlobProperties: storage.BlobProperties{
DeleteRetentionPolicy: storage.DeleteRetentionPolicy{
Days: types.IntTest(days),
},
},
CustomerManagedKey: storage.CustomerManagedKey{
KeyVaultKeyId: types.StringTest(""),
UserAssignedIdentityId: types.StringTest(""),
},
}
}
Loading