-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
binary_authorization_attestor KMS support (#2083)
Merged PR #2083.
- Loading branch information
1 parent
8961223
commit 1fd396d
Showing
13 changed files
with
447 additions
and
18 deletions.
There are no files selected for viewing
Submodule terraform
updated
from 86cbdc to 352be1
Submodule terraform-beta
updated
from e1fb65 to 7aebf0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
templates/terraform/examples/binary_authorization_attestor_kms.tf.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
resource "google_binary_authorization_attestor" "<%= ctx[:primary_resource_id] %>" { | ||
name = "<%= ctx[:vars]["attestor_name"] %>" | ||
attestation_authority_note { | ||
note_reference = "${google_container_analysis_note.note.name}" | ||
public_keys { | ||
id = "${data.google_kms_crypto_key_version.version.id}" | ||
pkix_public_key { | ||
public_key_pem = "${data.google_kms_crypto_key_version.version.public_key[0].pem}" | ||
signature_algorithm = "${data.google_kms_crypto_key_version.version.public_key[0].algorithm}" | ||
} | ||
} | ||
} | ||
} | ||
|
||
data "google_kms_crypto_key_version" "version" { | ||
crypto_key = "${google_kms_crypto_key.crypto-key.self_link}" | ||
} | ||
|
||
resource "google_container_analysis_note" "note" { | ||
name = "<%= ctx[:vars]["note_name"] %>" | ||
attestation_authority { | ||
hint { | ||
human_readable_name = "Attestor Note" | ||
} | ||
} | ||
} | ||
|
||
resource "google_kms_crypto_key" "crypto-key" { | ||
name = "<%= ctx[:vars]["key_name"] %>" | ||
key_ring = "${google_kms_key_ring.keyring.self_link}" | ||
purpose = "ASYMMETRIC_SIGN" | ||
|
||
version_template { | ||
algorithm = "RSA_SIGN_PKCS1_4096_SHA512" | ||
} | ||
|
||
lifecycle { | ||
prevent_destroy = true | ||
} | ||
} | ||
|
||
resource "google_kms_key_ring" "keyring" { | ||
name = "<%= ctx[:vars]["keyring_name"] %>" | ||
location = "global" | ||
} |
157 changes: 157 additions & 0 deletions
157
third_party/terraform/data_sources/data_source_google_kms_crypto_key_version.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceGoogleKmsCryptoKeyVersion() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGoogleKmsCryptoKeyVersionRead, | ||
Schema: map[string]*schema.Schema{ | ||
"crypto_key": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"version": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Default: 1, | ||
}, | ||
"algorithm": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"protection_level": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"state": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"public_key": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"algorithm": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"pem": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceGoogleKmsCryptoKeyVersionRead(d *schema.ResourceData, meta interface{}) error { | ||
config := meta.(*Config) | ||
|
||
url, err := replaceVars(d, config, "{{KmsBasePath}}{{crypto_key}}/cryptoKeyVersions/{{version}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Getting attributes for CryptoKeyVersion: %#v", url) | ||
res, err := sendRequest(config, "GET", url, nil) | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("KmsCryptoKeyVersion %q", d.Id())) | ||
} | ||
|
||
if err := d.Set("version", flattenKmsCryptoKeyVersionVersion(res["name"], d)); err != nil { | ||
return fmt.Errorf("Error reading CryptoKeyVersion: %s", err) | ||
} | ||
if err := d.Set("state", flattenKmsCryptoKeyVersionState(res["state"], d)); err != nil { | ||
return fmt.Errorf("Error reading CryptoKeyVersion: %s", err) | ||
} | ||
if err := d.Set("protection_level", flattenKmsCryptoKeyVersionProtectionLevel(res["protectionLevel"], d)); err != nil { | ||
return fmt.Errorf("Error reading CryptoKeyVersion: %s", err) | ||
} | ||
if err := d.Set("algorithm", flattenKmsCryptoKeyVersionAlgorithm(res["algorithm"], d)); err != nil { | ||
return fmt.Errorf("Error reading CryptoKeyVersion: %s", err) | ||
} | ||
|
||
url, err = replaceVars(d, config, "{{KmsBasePath}}{{crypto_key}}") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Getting purpose of CryptoKey: %#v", url) | ||
res, err = sendRequest(config, "GET", url, nil) | ||
if err != nil { | ||
return handleNotFoundError(err, d, fmt.Sprintf("KmsCryptoKey %q", d.Id())) | ||
} | ||
|
||
if res["purpose"] == "ASYMMETRIC_SIGN" || res["purpose"] == "ASYMMETRIC_DECRYPT" { | ||
url, err = replaceVars(d, config, "{{KmsBasePath}}{{crypto_key}}/cryptoKeyVersions/{{version}}/publicKey") | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("[DEBUG] Getting public key of CryptoKeyVersion: %#v", url) | ||
res, _ = sendRequest(config, "GET", url, nil) | ||
|
||
if err := d.Set("public_key", flattenKmsCryptoKeyVersionPublicKey(res, d)); err != nil { | ||
return fmt.Errorf("Error reading CryptoKeyVersion public key: %s", err) | ||
} | ||
} | ||
d.SetId(fmt.Sprintf("//cloudkms.googleapis.com/%s/cryptoKeyVersions/%d", d.Get("crypto_key"), d.Get("version"))) | ||
|
||
return nil | ||
} | ||
|
||
func flattenKmsCryptoKeyVersionVersion(v interface{}, d *schema.ResourceData) interface{} { | ||
parts := strings.Split(v.(string), "/") | ||
version := parts[len(parts)-1] | ||
// Handles the string fixed64 format | ||
if intVal, err := strconv.ParseInt(version, 10, 64); err == nil { | ||
return intVal | ||
} // let terraform core handle it if we can't convert the string to an int. | ||
return v | ||
} | ||
|
||
func flattenKmsCryptoKeyVersionState(v interface{}, d *schema.ResourceData) interface{} { | ||
return v | ||
} | ||
|
||
func flattenKmsCryptoKeyVersionProtectionLevel(v interface{}, d *schema.ResourceData) interface{} { | ||
return v | ||
} | ||
|
||
func flattenKmsCryptoKeyVersionAlgorithm(v interface{}, d *schema.ResourceData) interface{} { | ||
return v | ||
} | ||
|
||
func flattenKmsCryptoKeyVersionPublicKey(v interface{}, d *schema.ResourceData) interface{} { | ||
if v == nil { | ||
return nil | ||
} | ||
original := v.(map[string]interface{}) | ||
if len(original) == 0 { | ||
return nil | ||
} | ||
transformed := make(map[string]interface{}) | ||
transformed["pem"] = | ||
flattenKmsCryptoKeyVersionPublicKeyPem(original["pem"], d) | ||
transformed["algorithm"] = | ||
flattenKmsCryptoKeyVersionPublicKeyAlgorithm(original["algorithm"], d) | ||
return []interface{}{transformed} | ||
} | ||
func flattenKmsCryptoKeyVersionPublicKeyPem(v interface{}, d *schema.ResourceData) interface{} { | ||
return v | ||
} | ||
|
||
func flattenKmsCryptoKeyVersionPublicKeyAlgorithm(v interface{}, d *schema.ResourceData) interface{} { | ||
return v | ||
} |
47 changes: 47 additions & 0 deletions
47
third_party/terraform/tests/data_source_google_kms_crypto_key_version_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceGoogleKmsCryptoKeyVersion_basic(t *testing.T) { | ||
asymSignKey := BootstrapKMSKeyWithPurpose(t, "ASYMMETRIC_SIGN") | ||
asymDecrKey := BootstrapKMSKeyWithPurpose(t, "ASYMMETRIC_DECRYPT") | ||
symKey := BootstrapKMSKey(t) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceGoogleKmsCryptoKeyVersion_basic(asymSignKey.CryptoKey.Name), | ||
Check: resource.TestCheckResourceAttr("data.google_kms_crypto_key_version.version", "version", "1"), | ||
}, | ||
// Asymmetric keys should have a public key | ||
{ | ||
Config: testAccDataSourceGoogleKmsCryptoKeyVersion_basic(asymSignKey.CryptoKey.Name), | ||
Check: resource.TestCheckResourceAttr("data.google_kms_crypto_key_version.version", "public_key.#", "1"), | ||
}, | ||
{ | ||
Config: testAccDataSourceGoogleKmsCryptoKeyVersion_basic(asymDecrKey.CryptoKey.Name), | ||
Check: resource.TestCheckResourceAttr("data.google_kms_crypto_key_version.version", "public_key.#", "1"), | ||
}, | ||
// Symmetric key should have no public key | ||
{ | ||
Config: testAccDataSourceGoogleKmsCryptoKeyVersion_basic(symKey.CryptoKey.Name), | ||
Check: resource.TestCheckResourceAttr("data.google_kms_crypto_key_version.version", "public_key.#", "0"), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceGoogleKmsCryptoKeyVersion_basic(kmsKey string) string { | ||
return fmt.Sprintf(` | ||
data "google_kms_crypto_key_version" "version" { | ||
crypto_key = "%s" | ||
} | ||
`, kmsKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.