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

Keyvault storage permissions #3081

Merged
merged 5 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
7 changes: 7 additions & 0 deletions azurerm/data_source_key_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ func dataSourceArmKeyVault() *schema.Resource {
Type: schema.TypeString,
},
},
"storage_permissions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
Expand Down
54 changes: 54 additions & 0 deletions azurerm/helpers/azure/key_vault_access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,33 @@ func SchemaKeyVaultSecretPermissions() *schema.Schema {
}
}

func SchemaKeyVaultStoragePermissions() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
string(keyvault.StoragePermissionsBackup),
string(keyvault.StoragePermissionsDelete),
string(keyvault.StoragePermissionsDeletesas),
string(keyvault.StoragePermissionsGet),
string(keyvault.StoragePermissionsGetsas),
string(keyvault.StoragePermissionsList),
string(keyvault.StoragePermissionsListsas),
string(keyvault.StoragePermissionsPurge),
string(keyvault.StoragePermissionsRecover),
string(keyvault.StoragePermissionsRegeneratekey),
string(keyvault.StoragePermissionsRestore),
string(keyvault.StoragePermissionsSet),
string(keyvault.StoragePermissionsSetsas),
string(keyvault.StoragePermissionsUpdate),
}, true),
DiffSuppressFunc: suppress.CaseDifference,
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
},
}
}

func ExpandKeyVaultAccessPolicies(input []interface{}) (*[]keyvault.AccessPolicyEntry, error) {
output := make([]keyvault.AccessPolicyEntry, 0)

Expand All @@ -96,12 +123,14 @@ func ExpandKeyVaultAccessPolicies(input []interface{}) (*[]keyvault.AccessPolicy
certificatePermissionsRaw := policyRaw["certificate_permissions"].([]interface{})
keyPermissionsRaw := policyRaw["key_permissions"].([]interface{})
secretPermissionsRaw := policyRaw["secret_permissions"].([]interface{})
storagePermissionsRaw := policyRaw["storage_permissions"].([]interface{})

policy := keyvault.AccessPolicyEntry{
Permissions: &keyvault.Permissions{
Certificates: ExpandCertificatePermissions(certificatePermissionsRaw),
Keys: ExpandKeyPermissions(keyPermissionsRaw),
Secrets: ExpandSecretPermissions(secretPermissionsRaw),
Storage: ExpandStoragePermissions(storagePermissionsRaw),
},
}

Expand Down Expand Up @@ -152,6 +181,9 @@ func FlattenKeyVaultAccessPolicies(policies *[]keyvault.AccessPolicyEntry) []map

secrets := FlattenSecretPermissions(permissions.Secrets)
policyRaw["secret_permissions"] = secrets

storage := FlattenStoragePermissions(permissions.Storage)
policyRaw["storage_permissions"] = storage
}

result = append(result, policyRaw)
Expand Down Expand Up @@ -224,3 +256,25 @@ func FlattenSecretPermissions(input *[]keyvault.SecretPermissions) []interface{}

return output
}

func ExpandStoragePermissions(input []interface{}) *[]keyvault.StoragePermissions {
output := make([]keyvault.StoragePermissions, 0)

for _, permission := range input {
output = append(output, keyvault.StoragePermissions(permission.(string)))
}

return &output
}

func FlattenStoragePermissions(input *[]keyvault.StoragePermissions) []interface{} {
output := make([]interface{}, 0)

if input != nil {
for _, storagePermission := range *input {
output = append(output, string(storagePermission))
}
}

return output
}
1 change: 1 addition & 0 deletions azurerm/resource_arm_key_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func resourceArmKeyVault() *schema.Resource {
"certificate_permissions": azure.SchemaKeyVaultCertificatePermissions(),
"key_permissions": azure.SchemaKeyVaultKeyPermissions(),
"secret_permissions": azure.SchemaKeyVaultSecretPermissions(),
"storage_permissions": azure.SchemaKeyVaultStoragePermissions(),
},
},
},
Expand Down
20 changes: 20 additions & 0 deletions azurerm/resource_arm_key_vault_certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ resource "azurerm_key_vault" "test" {
secret_permissions = [
"set",
]

storage_permissions = [
"set",
]
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -458,6 +462,10 @@ resource "azurerm_key_vault" "test" {
secret_permissions = [
"set",
]

storage_permissions = [
"set",
]
}
}

Expand Down Expand Up @@ -560,6 +568,10 @@ resource "azurerm_key_vault" "test" {
secret_permissions = [
"set",
]

storage_permissions = [
"set",
]
}
}

Expand Down Expand Up @@ -648,6 +660,10 @@ resource "azurerm_key_vault" "test" {
secret_permissions = [
"set",
]

storage_permissions = [
"set",
]
}
}

Expand Down Expand Up @@ -835,6 +851,10 @@ resource "azurerm_key_vault" "test" {
secret_permissions = [
"set",
]

storage_permissions = [
"set",
]
}
}

Expand Down
29 changes: 29 additions & 0 deletions azurerm/resource_arm_key_vault_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,35 @@ func migrateAzureRMKeyVaultStateV0toV1AccessPolicies(is *terraform.InstanceState
}
}

if v, ok := policy["storage_permissions"]; ok {
inputStoragePermissions := v.([]interface{})
outputStoragePermissions := make([]string, 0)
for _, p := range inputStoragePermissions {
permission := p.(string)
if strings.ToLower(permission) == "all" {
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsBackup))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsDelete))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsDeletesas))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsGet))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsGetsas))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsList))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsListsas))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsPurge))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsRecover))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsRegeneratekey))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsRestore))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsSet))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsSetsas))
outputStoragePermissions = append(outputStoragePermissions, string(keyvault.StoragePermissionsUpdate))
break
}
}

if len(outputStoragePermissions) > 0 {
policy["storage_permissions"] = outputStoragePermissions
}
}
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved

outputAccessPolicies = append(outputAccessPolicies, policy)
}

Expand Down
Loading