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

Added new datasource for keyvault access policies #423

Merged
merged 2 commits into from
Oct 16, 2017
Merged
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
107 changes: 107 additions & 0 deletions azurerm/data_source_key_vault_access_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package azurerm

import (
"strings"

"github.com/Azure/azure-sdk-for-go/arm/keyvault"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func dataSourceArmKeyVaultAccessPolicy() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmKeyVaultAccessPolicyRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"Key Management",
"Secret Management",
"Certificate Management",
"Key & Secret Management",
"Key & Certificate Management",
"Secret & Certificate Management",
"Key, Secret, & Certificate Management",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor does the Portal have the second comma?

}, false),
},

// Computed
"certificate_permissions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"key_permissions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"secret_permissions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func dataSourceArmKeyVaultAccessPolicyRead(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
templateManagementPermissions := map[string][]string{
"key": {
string(keyvault.KeyPermissionsGet),
string(keyvault.KeyPermissionsList),
string(keyvault.KeyPermissionsUpdate),
string(keyvault.KeyPermissionsCreate),
string(keyvault.KeyPermissionsImport),
string(keyvault.KeyPermissionsDelete),
string(keyvault.KeyPermissionsRecover),
string(keyvault.KeyPermissionsBackup),
string(keyvault.KeyPermissionsRestore),
},
"secret": {
string(keyvault.SecretPermissionsGet),
string(keyvault.SecretPermissionsList),
string(keyvault.SecretPermissionsSet),
string(keyvault.SecretPermissionsDelete),
string(keyvault.SecretPermissionsRecover),
string(keyvault.SecretPermissionsBackup),
string(keyvault.SecretPermissionsRestore),
},
"certificate": {
string(keyvault.Get),
string(keyvault.List),
string(keyvault.Update),
string(keyvault.Create),
string(keyvault.Import),
string(keyvault.Delete),
string(keyvault.Managecontacts),
string(keyvault.Manageissuers),
string(keyvault.Getissuers),
string(keyvault.Listissuers),
string(keyvault.Setissuers),
string(keyvault.Deleteissuers),
},
}

d.SetId(name)

if strings.Contains(name, "Key") {
d.Set("key_permissions", templateManagementPermissions["key"])
}
if strings.Contains(name, "Secret") {
d.Set("secret_permissions", templateManagementPermissions["secret"])
}
if strings.Contains(name, "Certificate") {
d.Set("certificate_permissions", templateManagementPermissions["certificate"])
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 do we need to set an empty list for each of these values if it's not present?


return nil
}
142 changes: 142 additions & 0 deletions azurerm/data_source_key_vault_access_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccDataSourceAzureRMKeyVaultAccessPolicy_key(t *testing.T) {
dataSourceName := "data.azurerm_key_vault_access_policy.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceKeyVaultAccessPolicy("Key Management"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "key_permissions.#", "9"),
resource.TestCheckNoResourceAttr(dataSourceName, "secret_permissions"),
resource.TestCheckNoResourceAttr(dataSourceName, "certificate_permissions"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 should we be checking there's 0 here?

),
},
},
})
}

func TestAccDataSourceAzureRMKeyVaultAccessPolicy_secret(t *testing.T) {
dataSourceName := "data.azurerm_key_vault_access_policy.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceKeyVaultAccessPolicy("Secret Management"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr(dataSourceName, "key_permissions"),
resource.TestCheckResourceAttr(dataSourceName, "secret_permissions.#", "7"),
resource.TestCheckNoResourceAttr(dataSourceName, "certificate_permissions"),
),
},
},
})
}

func TestAccDataSourceAzureRMKeyVaultAccessPolicy_certificate(t *testing.T) {
dataSourceName := "data.azurerm_key_vault_access_policy.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceKeyVaultAccessPolicy("Certificate Management"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr(dataSourceName, "key_permissions"),
resource.TestCheckNoResourceAttr(dataSourceName, "secret_permissions"),
resource.TestCheckResourceAttr(dataSourceName, "certificate_permissions.#", "12"),
),
},
},
})
}

func TestAccDataSourceAzureRMKeyVaultAccessPolicy_keySecret(t *testing.T) {
dataSourceName := "data.azurerm_key_vault_access_policy.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceKeyVaultAccessPolicy("Key & Secret Management"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "key_permissions.#", "9"),
resource.TestCheckResourceAttr(dataSourceName, "secret_permissions.#", "7"),
resource.TestCheckNoResourceAttr(dataSourceName, "certificate_permissions"),
),
},
},
})
}

func TestAccDataSourceAzureRMKeyVaultAccessPolicy_keyCertificate(t *testing.T) {
dataSourceName := "data.azurerm_key_vault_access_policy.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceKeyVaultAccessPolicy("Key & Certificate Management"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "key_permissions.#", "9"),
resource.TestCheckNoResourceAttr(dataSourceName, "secret_permissions"),
resource.TestCheckResourceAttr(dataSourceName, "certificate_permissions.#", "12"),
),
},
},
})
}

func TestAccDataSourceAzureRMKeyVaultAccessPolicy_secretCertificate(t *testing.T) {
dataSourceName := "data.azurerm_key_vault_access_policy.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceKeyVaultAccessPolicy("Secret & Certificate Management"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckNoResourceAttr(dataSourceName, "key_permissions"),
resource.TestCheckResourceAttr(dataSourceName, "secret_permissions.#", "7"),
resource.TestCheckResourceAttr(dataSourceName, "certificate_permissions.#", "12"),
),
},
},
})
}

func TestAccDataSourceAzureRMKeyVaultAccessPolicy_keySecretCertificate(t *testing.T) {
dataSourceName := "data.azurerm_key_vault_access_policy.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceKeyVaultAccessPolicy("Key, Secret, & Certificate Management"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "key_permissions.#", "9"),
resource.TestCheckResourceAttr(dataSourceName, "secret_permissions.#", "7"),
resource.TestCheckResourceAttr(dataSourceName, "certificate_permissions.#", "12"),
),
},
},
})
}

func testAccDataSourceKeyVaultAccessPolicy(name string) string {
return fmt.Sprintf(`
data "azurerm_key_vault_access_policy" "test" {
name = "%s"
}
`, name)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(),
"azurerm_client_config": dataSourceArmClientConfig(),
"azurerm_image": dataSourceArmImage(),
"azurerm_key_vault_access_policy": dataSourceArmKeyVaultAccessPolicy(),
"azurerm_managed_disk": dataSourceArmManagedDisk(),
"azurerm_platform_image": dataSourceArmPlatformImage(),
"azurerm_public_ip": dataSourceArmPublicIP(),
Expand Down
13 changes: 9 additions & 4 deletions azurerm/resource_arm_key_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/satori/uuid"
uuid "github.com/satori/go.uuid"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -32,6 +32,8 @@ func resourceArmKeyVault() *schema.Resource {
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
MigrateState: resourceAzureRMKeyVaultMigrateState,
SchemaVersion: 1,

Schema: map[string]*schema.Schema{
"name": {
Expand Down Expand Up @@ -101,7 +103,6 @@ func resourceArmKeyVault() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
string(keyvault.All),
string(keyvault.Create),
string(keyvault.Delete),
string(keyvault.Deleteissuers),
Expand All @@ -124,7 +125,6 @@ func resourceArmKeyVault() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
string(keyvault.KeyPermissionsAll),
string(keyvault.KeyPermissionsBackup),
string(keyvault.KeyPermissionsCreate),
string(keyvault.KeyPermissionsDecrypt),
Expand All @@ -133,6 +133,8 @@ func resourceArmKeyVault() *schema.Resource {
string(keyvault.KeyPermissionsGet),
string(keyvault.KeyPermissionsImport),
string(keyvault.KeyPermissionsList),
string(keyvault.KeyPermissionsPurge),
string(keyvault.KeyPermissionsRecover),
string(keyvault.KeyPermissionsRestore),
string(keyvault.KeyPermissionsSign),
string(keyvault.KeyPermissionsUnwrapKey),
Expand All @@ -149,10 +151,13 @@ func resourceArmKeyVault() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
string(keyvault.SecretPermissionsAll),
string(keyvault.SecretPermissionsBackup),
string(keyvault.SecretPermissionsDelete),
string(keyvault.SecretPermissionsGet),
string(keyvault.SecretPermissionsList),
string(keyvault.SecretPermissionsPurge),
string(keyvault.SecretPermissionsRecover),
string(keyvault.SecretPermissionsRestore),
string(keyvault.SecretPermissionsSet),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
Expand Down
Loading