forked from hashicorp/terraform-provider-vault
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_gcp_secret_backend.go
More file actions
206 lines (181 loc) · 6.1 KB
/
Copy pathresource_gcp_secret_backend.go
File metadata and controls
206 lines (181 loc) · 6.1 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package vault
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/vault/api"
)
func gcpSecretBackendResource() *schema.Resource {
return &schema.Resource{
Create: gcpSecretBackendCreate,
Read: gcpSecretBackendRead,
Update: gcpSecretBackendUpdate,
Delete: gcpSecretBackendDelete,
Exists: gcpSecretBackendExists,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"path": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "gcp",
Description: "Path to mount the backend at.",
ValidateFunc: func(v interface{}, k string) (ws []string, errs []error) {
value := v.(string)
if strings.HasSuffix(value, "/") {
errs = append(errs, fmt.Errorf("path cannot end in '/'"))
}
return
},
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return old+"/" == new || new+"/" == old
},
},
"credentials": {
Type: schema.TypeString,
Optional: true,
Description: "JSON-encoded credentials to use to connect to GCP",
Sensitive: true,
// We rebuild the attached JSON string to a simple singleline
// string. This makes terraform not want to change when an extra
// space is included in the JSON string. It is also necesarry
// when disable_read is false for comparing values.
StateFunc: NormalizeDataJSON,
ValidateFunc: ValidateDataJSON,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Human-friendly description of the mount for the backend.",
},
"default_lease_ttl_seconds": {
Type: schema.TypeInt,
Optional: true,
Default: "3600",
Description: "Default lease duration for secrets in seconds",
},
"max_lease_ttl_seconds": {
Type: schema.TypeInt,
Optional: true,
Default: "86400",
Description: "Maximum possible lease duration for secrets in seconds",
},
},
}
}
func gcpSecretBackendCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
path := d.Get("path").(string)
description := d.Get("description").(string)
defaultTTL := d.Get("default_lease_ttl_seconds").(int)
maxTTL := d.Get("max_lease_ttl_seconds").(int)
credentials := d.Get("credentials").(string)
configPath := gcpSecretBackendConfigPath(path)
d.Partial(true)
log.Printf("[DEBUG] Mounting GCP backend at %q", path)
err := client.Sys().Mount(path, &api.MountInput{
Type: "gcp",
Description: description,
Config: api.MountConfigInput{
DefaultLeaseTTL: fmt.Sprintf("%ds", defaultTTL),
MaxLeaseTTL: fmt.Sprintf("%ds", maxTTL),
},
})
if err != nil {
return fmt.Errorf("error mounting to %q: %s", path, err)
}
log.Printf("[DEBUG] Mounted GCP backend at %q", path)
d.SetId(path)
d.SetPartial("path")
d.SetPartial("description")
d.SetPartial("default_lease_ttl_seconds")
d.SetPartial("max_lease_ttl_seconds")
log.Printf("[DEBUG] Writing GCP configuration to %q", configPath)
if credentials != "" {
data := map[string]interface{}{
"credentials": credentials,
}
if _, err := client.Logical().Write(configPath, data); err != nil {
return fmt.Errorf("error writing GCP configuration for %q: %s", path, err)
}
} else {
log.Printf("[DEBUG] No credentials configured")
}
log.Printf("[DEBUG] Wrote GCP configuration to %q", configPath)
d.Partial(false)
return gcpSecretBackendRead(d, meta)
}
func gcpSecretBackendRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
path := d.Id()
log.Printf("[DEBUG] Reading GCP backend mount %q from Vault", path)
mounts, err := client.Sys().ListMounts()
if err != nil {
return fmt.Errorf("error reading mount %q: %s", path, err)
}
log.Printf("[DEBUG] Read GCP backend mount %q from Vault", path)
// the API always returns the path with a trailing slash, so let's make
// sure we always specify it as a trailing slash.
mount, ok := mounts[strings.Trim(path, "/")+"/"]
if !ok {
log.Printf("[WARN] Mount %q not found, removing backend from state.", path)
d.SetId("")
return nil
}
d.Set("path", path)
d.Set("description", mount.Description)
d.Set("default_lease_ttl_seconds", mount.Config.DefaultLeaseTTL)
d.Set("max_lease_ttl_seconds", mount.Config.MaxLeaseTTL)
return nil
}
func gcpSecretBackendUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
path := d.Id()
d.Partial(true)
if d.HasChange("default_lease_ttl_seconds") || d.HasChange("max_lease_ttl_seconds") {
config := api.MountConfigInput{
DefaultLeaseTTL: fmt.Sprintf("%ds", d.Get("default_lease_ttl_seconds")),
MaxLeaseTTL: fmt.Sprintf("%ds", d.Get("max_lease_ttl_seconds")),
}
log.Printf("[DEBUG] Updating lease TTLs for %q", path)
err := client.Sys().TuneMount(path, config)
if err != nil {
return fmt.Errorf("error updating mount TTLs for %q: %s", path, err)
}
log.Printf("[DEBUG] Updated lease TTLs for %q", path)
d.SetPartial("default_lease_ttl_seconds")
d.SetPartial("max_lease_ttl_seconds")
}
d.Partial(false)
return gcpSecretBackendRead(d, meta)
}
func gcpSecretBackendDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
path := d.Id()
log.Printf("[DEBUG] Unmounting GCP backend %q", path)
err := client.Sys().Unmount(path)
if err != nil {
return fmt.Errorf("error unmounting GCP backend from %q: %s", path, err)
}
log.Printf("[DEBUG] Unmounted GCP backend %q", path)
return nil
}
func gcpSecretBackendExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*api.Client)
path := d.Id()
log.Printf("[DEBUG] Checking if GCP backend exists at %q", path)
mounts, err := client.Sys().ListMounts()
if err != nil {
return true, fmt.Errorf("error retrieving list of mounts: %s", err)
}
log.Printf("[DEBUG] Checked if GCP backend exists at %q", path)
_, ok := mounts[strings.Trim(path, "/")+"/"]
return ok, nil
}
func gcpSecretBackendConfigPath(backend string) string {
return strings.Trim(backend, "/") + "/config"
}