Skip to content

Commit d110e65

Browse files
Enable Uploading Serviceaccount keys (#3849) (#2368)
* enable uploading service account keys * remove service account key Signed-off-by: Modular Magician <[email protected]>
1 parent e0bda86 commit d110e65

File tree

5 files changed

+85
-7
lines changed

5 files changed

+85
-7
lines changed

.changelog/3849.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
iam: Added `public_key_type` field to `google_service_account_key `
3+
```

google-beta/resource_google_service_account_key.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ func resourceGoogleServiceAccountKey() *schema.Resource {
5252
ForceNew: true,
5353
ValidateFunc: validation.StringInSlice([]string{"TYPE_NONE", "TYPE_X509_PEM_FILE", "TYPE_RAW_PUBLIC_KEY"}, false),
5454
},
55+
"public_key_data": {
56+
Type: schema.TypeString,
57+
Optional: true,
58+
ForceNew: true,
59+
ConflictsWith: []string{"key_algorithm", "private_key_type"},
60+
Description: `A field that allows clients to upload their own public key. If set, use this public key data to create a service account key for given service account. Please note, the expected format for this field is a base64 encoded X509_PEM.`,
61+
},
5562
// Computed
5663
"name": {
5764
Type: schema.TypeString,
@@ -103,14 +110,25 @@ func resourceGoogleServiceAccountKeyCreate(d *schema.ResourceData, meta interfac
103110
return err
104111
}
105112

106-
r := &iam.CreateServiceAccountKeyRequest{
107-
KeyAlgorithm: d.Get("key_algorithm").(string),
108-
PrivateKeyType: d.Get("private_key_type").(string),
109-
}
113+
var sak *iam.ServiceAccountKey
110114

111-
sak, err := config.clientIAM.Projects.ServiceAccounts.Keys.Create(serviceAccountName, r).Do()
112-
if err != nil {
113-
return fmt.Errorf("Error creating service account key: %s", err)
115+
if d.Get("public_key_data").(string) != "" {
116+
ru := &iam.UploadServiceAccountKeyRequest{
117+
PublicKeyData: d.Get("public_key_data").(string),
118+
}
119+
sak, err = config.clientIAM.Projects.ServiceAccounts.Keys.Upload(serviceAccountName, ru).Do()
120+
if err != nil {
121+
return fmt.Errorf("Error creating service account key: %s", err)
122+
}
123+
} else {
124+
rc := &iam.CreateServiceAccountKeyRequest{
125+
KeyAlgorithm: d.Get("key_algorithm").(string),
126+
PrivateKeyType: d.Get("private_key_type").(string),
127+
}
128+
sak, err = config.clientIAM.Projects.ServiceAccounts.Keys.Create(serviceAccountName, rc).Do()
129+
if err != nil {
130+
return fmt.Errorf("Error creating service account key: %s", err)
131+
}
114132
}
115133

116134
d.SetId(sak.Name)

google-beta/resource_google_service_account_key_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,30 @@ func TestAccServiceAccountKey_fromEmail(t *testing.T) {
5757
})
5858
}
5959

60+
func TestAccServiceAccountKey_fromCertificate(t *testing.T) {
61+
t.Parallel()
62+
63+
resourceName := "google_service_account_key.acceptance"
64+
accountID := "a" + randString(t, 10)
65+
displayName := "Terraform Test"
66+
vcrTest(t, resource.TestCase{
67+
PreCheck: func() { testAccPreCheck(t) },
68+
Providers: testAccProviders,
69+
Steps: []resource.TestStep{
70+
{
71+
Config: testAccServiceAccountKey_fromCertificate(accountID, displayName),
72+
Check: resource.ComposeTestCheckFunc(
73+
testAccCheckGoogleServiceAccountKeyExists(t, resourceName),
74+
resource.TestCheckResourceAttrSet(resourceName, "public_key"),
75+
resource.TestCheckResourceAttrSet(resourceName, "valid_after"),
76+
resource.TestCheckResourceAttrSet(resourceName, "valid_before"),
77+
resource.TestCheckResourceAttrSet(resourceName, "public_key"),
78+
),
79+
},
80+
},
81+
})
82+
}
83+
6084
func testAccCheckGoogleServiceAccountKeyExists(t *testing.T, r string) resource.TestCheckFunc {
6185
return func(s *terraform.State) error {
6286

@@ -106,3 +130,17 @@ resource "google_service_account_key" "acceptance" {
106130
}
107131
`, account, name)
108132
}
133+
134+
func testAccServiceAccountKey_fromCertificate(account, name string) string {
135+
return fmt.Sprintf(`
136+
resource "google_service_account" "acceptance" {
137+
account_id = "%s"
138+
display_name = "%s"
139+
}
140+
141+
resource "google_service_account_key" "acceptance" {
142+
service_account_id = google_service_account.acceptance.email
143+
public_key_data = filebase64("test-fixtures/serviceaccount/public_key.pem")
144+
}
145+
`, account, name)
146+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIICnjCCAYYCCQD6STTBmcOGNTANBgkqhkiG9w0BAQsFADARMQ8wDQYDVQQDDAZ1
3+
bnVzZWQwHhcNMjAwODEwMTExNzU0WhcNMzAwODA4MTExNzU0WjARMQ8wDQYDVQQD
4+
DAZ1bnVzZWQwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDAGCUQbs0l
5+
yyalBW4rBgWvU2awMXiVyQMOhWMQVMd99CgtY4Rzktj7qWnPiKe/daegyz40FXuq
6+
2Is8RThit4hx0RrdRFm8XXYpJjhHbIpCD/e5ukVMLNDIBqiMuFQI9naKcppuzOtL
7+
htj3zOQ54qXwe183lrg60RHoVR95Z1QqnCGkZcyECGJMuQBEaYyTnzf/nFba05uP
8+
LcZS1RHtdu5xfdDCrS9vDYA7R/3tvQ2erRvETSUFpMyIOxSMgZEBKhDhVfYqVh5T
9+
gSo3fJ5oXHozdqno2nf+MkE71moP4LbwqUGrSWK19kLcOGnGxWzLwcJWDTDlnU1S
10+
MC1y1T7GG+4dAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAKZXsIoQ7CZhtb7GL7m6
11+
tVO/Q4WuL2D3sL0EYHpHWMUDFZ9aXeiNEaTJLYeaAkVQ80y+i1D2xaK42S/m94sd
12+
mq4UKy0sRN25brVXFGjhBNwk2iJlWPj9/ibttMLKMT2nxPWS+YQOCZXg5B60wUFD
13+
mmKkdsbZmrLe2VX2lHGvWuZF2ZFpx9wKcrLmQBhQ/1tZV7k8bf/JiWlGkQqDzwBZ
14+
m+xUNAUpu32QQwkNGUNte562KK9nzsbVD0qDBFcmh3sEirOgiU4ezEWdmbFhtcfH
15+
Q1lTZZ1oD38RmMNPnJUHY+b7W57TrsYO5inFjBwjYJ4plTUG12RSZ8nPz6whZTK6
16+
Gys=
17+
-----END CERTIFICATE-----

website/docs/r/google_service_account_key.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Valid values are listed at
6464

6565
* `private_key_type` (Optional) The output format of the private key. TYPE_GOOGLE_CREDENTIALS_FILE is the default output format.
6666

67+
* `public_key_data` (Optional) Public key data to create a service account key for given service account. The expected format for this field is a base64 encoded X509_PEM and it conflicts with `public_key_type` and `private_key_type`.
68+
6769
## Attributes Reference
6870

6971
The following attributes are exported in addition to the arguments listed above:

0 commit comments

Comments
 (0)