Skip to content

Commit

Permalink
rebase and refactor the codes
Browse files Browse the repository at this point in the history
  • Loading branch information
njuCZ committed May 6, 2021
1 parent 4b4ae5a commit ee064ef
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@ import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault"
"github.com/Azure/azure-sdk-for-go/services/keyvault/v7.1/keyvault"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/keyvault/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/keyvault/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmKeyVaultKeyDecrypt() *schema.Resource {
func dataSourceKeyVaultKeyDecrypt() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmKeyVaultKeyDecryptRead,
Read: dataSourceKeyVaultKeyDecryptRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(5 * time.Minute),
Expand All @@ -25,7 +26,7 @@ func dataSourceArmKeyVaultKeyDecrypt() *schema.Resource {
"key_vault_key_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateKeyVaultChildId,
ValidateFunc: validate.NestedItemId,
},

"payload": {
Expand All @@ -51,14 +52,14 @@ func dataSourceArmKeyVaultKeyDecrypt() *schema.Resource {
}
}

func dataSourceArmKeyVaultKeyDecryptRead(d *schema.ResourceData, meta interface{}) error {
func dataSourceKeyVaultKeyDecryptRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).KeyVault.ManagementClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

payload := d.Get("payload").(string)
keyVaultKeyIdRaw := d.Get("key_vault_key_id").(string)
keyVaultKeyId, err := azure.ParseKeyVaultChildID(keyVaultKeyIdRaw)
keyVaultKeyId, err := parse.ParseNestedItemID(keyVaultKeyIdRaw)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
package tests
package keyvault_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/provider"
)

func TestAccDataSourceAzureRMKeyVaultKeyDecrypt_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_key_vault_key_decrypt", "test")
func TestAccDataSourceKeyVaultKeyDecrypt_basic(t *testing.T) {
plaintext := "testData"
data := acceptance.BuildTestData(t, "data.azurerm_key_vault_key_decrypt", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
PreCheck: func() { acceptance.PreCheck(t) },
ProviderFactories: map[string]terraform.ResourceProviderFactory{
"azurerm": func() (terraform.ResourceProvider, error) {
azurerm := provider.TestAzureProvider()
return azurerm, nil
},
},
Steps: []resource.TestStep{
{
Config: testAccDataSourceKeyVaultKeyDecrypt_basic(data, plaintext),
Expand All @@ -26,7 +34,7 @@ func TestAccDataSourceAzureRMKeyVaultKeyDecrypt_basic(t *testing.T) {
}

func testAccDataSourceKeyVaultKeyDecrypt_basic(data acceptance.TestData, plaintext string) string {
t := testAccAzureRMKeyVaultKeyEncrypt_basic(data, plaintext)
t := testAccKeyVaultKeyEncrypt_basic(data, plaintext)
return fmt.Sprintf(`
%s
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package keyvault

import (
"crypto/sha1"
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/keyvault/2016-10-01/keyvault"
"github.com/Azure/azure-sdk-for-go/services/keyvault/v7.1/keyvault"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/keyvault/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/keyvault/validate"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand All @@ -29,11 +31,18 @@ func resourceArmKeyVaultKeyEncrypt() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"key_vault_key_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateKeyVaultChildId,
ValidateFunc: validate.NestedItemId,
},

"plaintext": {
Expand Down Expand Up @@ -67,9 +76,10 @@ func resourceArmKeyVaultKeyEncryptCreate(d *schema.ResourceData, meta interface{
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
plaintext := d.Get("plaintext").(string)
keyVaultKeyIdRaw := d.Get("key_vault_key_id").(string)
keyVaultKeyId, err := azure.ParseKeyVaultChildID(keyVaultKeyIdRaw)
keyVaultKeyId, err := parse.ParseNestedItemID(keyVaultKeyIdRaw)
if err != nil {
return err
}
Expand All @@ -83,7 +93,9 @@ func resourceArmKeyVaultKeyEncryptCreate(d *schema.ResourceData, meta interface{
return fmt.Errorf("failed to encrypt '%s' using key %s: %+v", plaintext, keyVaultKeyIdRaw, err)
}

d.SetId(time.Now().UTC().String())
id := fmt.Sprintf("azurerm_keyvault_encrypted_data/%s/%s", name, sha1.Sum([]byte(plaintext)))
d.SetId(id)

d.Set("key_vault_key_id", result.Kid)
d.Set("cipher_text", result.Result)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,64 @@
package tests
package keyvault_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/provider"
)

func TestAccAzureRMKeyVaultKeyEncrypt_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_key_vault_key_encrypt", "test")
func TestAccKeyVaultKeyEncrypt_basic(t *testing.T) {
plaintext := "testData"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
data := acceptance.BuildTestData(t, "azurerm_key_vault_key_encrypt", "test")

testCase := resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
ProviderFactories: map[string]terraform.ResourceProviderFactory{
"azurerm": func() (terraform.ResourceProvider, error) {
azurerm := provider.TestAzureProvider()
return azurerm, nil
},
},
Steps: []resource.TestStep{
{
Config: testAccAzureRMKeyVaultKeyEncrypt_basic(data, plaintext),
Config: testAccKeyVaultKeyEncrypt_basic(data, plaintext),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(data.ResourceName, "cipher_text"),
),
},
},
})
}

resource.ParallelTest(t, testCase)
}

func testAccAzureRMKeyVaultKeyEncrypt_basic(data acceptance.TestData, plaintext string) string {
t := testAccAzureRMKeyVaultKeyEncrypt_template(data)
func testAccKeyVaultKeyEncrypt_basic(data acceptance.TestData, plaintext string) string {
t := testAccKeyVaultKeyEncrypt_template(data)
return fmt.Sprintf(`
provider "azurerm" {
features {
key_vault {
purge_soft_delete_on_destroy = false
}
}
}
%s
resource "azurerm_key_vault_key_encrypt" "test" {
name = "acctest_encrypt%d"
key_vault_key_id = azurerm_key_vault_key.test.id
algorithm = "RSA1_5"
plaintext = "%s"
}
`, t, plaintext)
`, t, data.RandomInteger, plaintext)
}

func testAccAzureRMKeyVaultKeyEncrypt_template(data acceptance.TestData) string {
func testAccKeyVaultKeyEncrypt_template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
data "azurerm_client_config" "current" {
}
Expand Down
2 changes: 1 addition & 1 deletion azurerm/internal/services/keyvault/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r Registration) SupportedDataSources() map[string]*schema.Resource {
"azurerm_key_vault_certificate_data": dataSourceKeyVaultCertificateData(),
"azurerm_key_vault_certificate_issuer": dataSourceKeyVaultCertificateIssuer(),
"azurerm_key_vault_key": dataSourceKeyVaultKey(),
"azurerm_key_vault_key_decrypt": dataSourceArmKeyVaultKeyDecrypt(),
"azurerm_key_vault_key_decrypt": dataSourceKeyVaultKeyDecrypt(),
"azurerm_key_vault_managed_hardware_security_module": dataSourceKeyVaultManagedHardwareSecurityModule(),
"azurerm_key_vault_secret": dataSourceKeyVaultSecret(),
"azurerm_key_vault": dataSourceKeyVault(),
Expand Down
4 changes: 2 additions & 2 deletions website/docs/d/key_vault_key_decrypt.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ subcategory: "Key Vault"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_key_vault_key_decrypt"
description: |-
Decrypt data encrypted with the Key Vault Key.
Decrypt data encrypted with a Key Vault Key.
---

# Data Source: azurerm_key_vault_key_decrypt

Use this data source to decrypt data encrypted with the Key Vault Key.
Use this data source to decrypt data encrypted with a Key Vault Key.

## Example Usage

Expand Down
7 changes: 5 additions & 2 deletions website/docs/r/key_vault_key_encrypt.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ subcategory: "Key Vault"
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_key_vault_key_encrypt"
description: |-
Encrypt data with the Key Vault Key.
Encrypt data with a Key Vault Key.
---

# azurerm_key_vault_key_encrypt

Use this resource to encrypt data with the Key Vault Key.
Use this resource to encrypt data with a Key Vault Key.

## Example Usage

```hcl
resource "azurerm_key_vault_key_encrypt" "example" {
name = "example_encrypt"
key_vault_key_id = azurerm_key_vault_key.example.id
plaintext = "testData"
algorithm = "RSA1_5"
Expand All @@ -29,6 +30,8 @@ output "encrypted_data" {

The following arguments are supported:

* `name` - (Required) Specifies the name of this resource. Changing this forces a new resource to be created.

* `key_vault_key_id` - (Required) Specifies the ID of the Key Vault key which is used to encrypt. Changing this forces a new resource to be created.

* `plaintext` - (Required) Specifies the Data to be encrypted. Changing this forces a new resource to be created.
Expand Down

0 comments on commit ee064ef

Please sign in to comment.