forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_google_service_account_key.go
160 lines (142 loc) · 4.52 KB
/
resource_google_service_account_key.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/encryption"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"google.golang.org/api/iam/v1"
)
func resourceGoogleServiceAccountKey() *schema.Resource {
return &schema.Resource{
Create: resourceGoogleServiceAccountKeyCreate,
Read: resourceGoogleServiceAccountKeyRead,
Delete: resourceGoogleServiceAccountKeyDelete,
Schema: map[string]*schema.Schema{
// Required
"service_account_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
// Optional
"key_algorithm": &schema.Schema{
Type: schema.TypeString,
Default: "KEY_ALG_RSA_2048",
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"KEY_ALG_UNSPECIFIED", "KEY_ALG_RSA_1024", "KEY_ALG_RSA_2048"}, false),
},
"pgp_key": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"private_key_type": &schema.Schema{
Type: schema.TypeString,
Default: "TYPE_GOOGLE_CREDENTIALS_FILE",
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"TYPE_UNSPECIFIED", "TYPE_PKCS12_FILE", "TYPE_GOOGLE_CREDENTIALS_FILE"}, false),
},
"public_key_type": &schema.Schema{
Type: schema.TypeString,
Default: "TYPE_X509_PEM_FILE",
Optional: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"TYPE_NONE", "TYPE_X509_PEM_FILE", "TYPE_RAW_PUBLIC_KEY"}, false),
},
// Computed
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
"public_key": {
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
"private_key": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"valid_after": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"valid_before": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"private_key_encrypted": {
Type: schema.TypeString,
Computed: true,
},
"private_key_fingerprint": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceGoogleServiceAccountKeyCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceAccountName, err := serviceAccountFQN(d.Get("service_account_id").(string), d, config)
if err != nil {
return err
}
r := &iam.CreateServiceAccountKeyRequest{
KeyAlgorithm: d.Get("key_algorithm").(string),
PrivateKeyType: d.Get("private_key_type").(string),
}
sak, err := config.clientIAM.Projects.ServiceAccounts.Keys.Create(serviceAccountName, r).Do()
if err != nil {
return fmt.Errorf("Error creating service account key: %s", err)
}
d.SetId(sak.Name)
// Data only available on create.
d.Set("valid_after", sak.ValidAfterTime)
d.Set("valid_before", sak.ValidBeforeTime)
if v, ok := d.GetOk("pgp_key"); ok {
encryptionKey, err := encryption.RetrieveGPGKey(v.(string))
if err != nil {
return err
}
fingerprint, encrypted, err := encryption.EncryptValue(encryptionKey, sak.PrivateKeyData, "Google Service Account Key")
if err != nil {
return err
}
d.Set("private_key_encrypted", encrypted)
d.Set("private_key_fingerprint", fingerprint)
} else {
d.Set("private_key", sak.PrivateKeyData)
}
err = serviceAccountKeyWaitTime(config.clientIAM.Projects.ServiceAccounts.Keys, d.Id(), d.Get("public_key_type").(string), "Creating Service account key", 4)
if err != nil {
return err
}
return resourceGoogleServiceAccountKeyRead(d, meta)
}
func resourceGoogleServiceAccountKeyRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
publicKeyType := d.Get("public_key_type").(string)
// Confirm the service account key exists
sak, err := config.clientIAM.Projects.ServiceAccounts.Keys.Get(d.Id()).PublicKeyType(publicKeyType).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Service Account Key %q", d.Id()))
}
d.Set("name", sak.Name)
d.Set("key_algorithm", sak.KeyAlgorithm)
d.Set("public_key", sak.PublicKeyData)
return nil
}
func resourceGoogleServiceAccountKeyDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
_, err := config.clientIAM.Projects.ServiceAccounts.Keys.Delete(d.Id()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Service Account Key %q", d.Id()))
}
d.SetId("")
return nil
}