-
Notifications
You must be signed in to change notification settings - Fork 44
/
resource_tailnet_key.go
259 lines (227 loc) · 8.21 KB
/
resource_tailnet_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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package tailscale
import (
"context"
"time"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/tailscale/tailscale-client-go/tailscale"
)
func resourceTailnetKey() *schema.Resource {
return &schema.Resource{
Description: "The tailnet_key resource allows you to create pre-authentication keys that can register new nodes without needing to sign in via a web browser. See https://tailscale.com/kb/1085/auth-keys for more information",
ReadContext: resourceTailnetKeyRead,
CreateContext: resourceTailnetKeyCreate,
DeleteContext: resourceTailnetKeyDelete,
UpdateContext: schema.NoopContext,
CustomizeDiff: resourceTailnetKeyDiff,
Schema: map[string]*schema.Schema{
"reusable": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicates if the key is reusable or single-use. Defaults to `false`.",
ForceNew: true,
},
"ephemeral": {
Type: schema.TypeBool,
Optional: true,
Description: "Indicates if the key is ephemeral. Defaults to `false`.",
ForceNew: true,
},
"tags": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "List of tags to apply to the machines authenticated by the key.",
ForceNew: true,
},
"preauthorized": {
Type: schema.TypeBool,
Optional: true,
Description: "Determines whether or not the machines authenticated by the key will be authorized for the tailnet by default. Defaults to `false`.",
ForceNew: true,
},
"key": {
Type: schema.TypeString,
Description: "The authentication key",
Computed: true,
Sensitive: true,
},
"expiry": {
Type: schema.TypeInt,
Optional: true,
Description: "The expiry of the key in seconds. Defaults to `7776000` (90 days).",
ForceNew: true,
},
"created_at": {
Type: schema.TypeString,
Description: "The creation timestamp of the key in RFC3339 format",
Computed: true,
},
"expires_at": {
Type: schema.TypeString,
Description: "The expiry timestamp of the key in RFC3339 format",
Computed: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "A description of the key consisting of alphanumeric characters. Defaults to `\"\"`.",
ForceNew: true,
ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics {
if len(i.(string)) > 50 {
return diagnosticsError(nil, "description must be 50 characters or less")
}
return nil
},
},
"invalid": {
Type: schema.TypeBool,
Description: "Indicates whether the key is invalid (e.g. expired, revoked or has been deleted).",
Computed: true,
},
"recreate_if_invalid": {
Type: schema.TypeString,
Optional: true,
Description: "Determines whether the key should be created again if it becomes invalid. By default, reusable keys will be recreated, but single-use keys will not. Possible values: 'always', 'never'.",
ValidateDiagFunc: func(i interface{}, p cty.Path) diag.Diagnostics {
switch i.(string) {
case "", "always", "never":
return nil
default:
return diagnosticsError(nil, "unexpected value of recreate_if_invalid: %s", i)
}
},
},
},
}
}
func resourceTailnetKeyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*tailscale.Client)
reusable := d.Get("reusable").(bool)
ephemeral := d.Get("ephemeral").(bool)
preauthorized := d.Get("preauthorized").(bool)
expiry, hasExpiry := d.GetOk("expiry")
description, hasDescription := d.GetOk("description")
var tags []string
for _, tag := range d.Get("tags").(*schema.Set).List() {
tags = append(tags, tag.(string))
}
var capabilities tailscale.KeyCapabilities
capabilities.Devices.Create.Reusable = reusable
capabilities.Devices.Create.Ephemeral = ephemeral
capabilities.Devices.Create.Tags = tags
capabilities.Devices.Create.Preauthorized = preauthorized
var opts []tailscale.CreateKeyOption
if hasExpiry {
opts = append(opts, tailscale.WithKeyExpiry(time.Duration(expiry.(int))*time.Second))
}
if hasDescription {
opts = append(opts, tailscale.WithKeyDescription(description.(string)))
}
key, err := client.CreateKey(ctx, capabilities, opts...)
if err != nil {
return diagnosticsError(err, "Failed to create key")
}
d.SetId(key.ID)
if err = d.Set("key", key.Key); err != nil {
return diagnosticsError(err, "Failed to set key")
}
if err = d.Set("created_at", key.Created.Format(time.RFC3339)); err != nil {
return diagnosticsError(err, "Failed to set created_at")
}
if err = d.Set("expires_at", key.Expires.Format(time.RFC3339)); err != nil {
return diagnosticsError(err, "Failed to set expires_at")
}
if err = d.Set("invalid", key.Invalid); err != nil {
return diagnosticsError(err, "Failed to set 'invalid'")
}
return resourceTailnetKeyRead(ctx, d, m)
}
func resourceTailnetKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*tailscale.Client)
err := client.DeleteKey(ctx, d.Id())
switch {
case tailscale.IsNotFound(err):
// Single-use keys may no longer be here, so we can ignore deletions that fail due to not-found errors.
return nil
case err != nil:
return diagnosticsError(err, "Failed to delete key")
default:
return nil
}
}
// shouldRecreateIfInvalid determines if a resource should be recreated when
// it's invalid, based on the values of `reusable` and `recreate_if_invalid` fields.
// By default, we automatically recreate reusable keys, but ignore invalid single-use
// keys, assuming they have successfully been used, and recreating them might trigger
// unnecessary updates of other Terraform resources that depend on the key.
func shouldRecreateIfInvalid(reusable bool, recreateIfInvalid string) bool {
if recreateIfInvalid == "always" {
return true
}
if recreateIfInvalid == "never" {
return false
}
return reusable
}
// resourceTailnetKeyDiff makes sure a resource is recreated when a `recreate_if_invalid`
// field changes in a way that requires it.
func resourceTailnetKeyDiff(ctx context.Context, d *schema.ResourceDiff, m interface{}) error {
old, new := d.GetChange("recreate_if_invalid")
if old == new {
return nil
}
recreateIfInvalid := shouldRecreateIfInvalid(d.Get("reusable").(bool), d.Get("recreate_if_invalid").(string))
if !recreateIfInvalid {
return nil
}
client := m.(*tailscale.Client)
key, err := client.GetKey(ctx, d.Id())
if tailscale.IsNotFound(err) || (err == nil && key.Invalid) {
d.ForceNew("recreate_if_invalid")
}
return nil
}
func resourceTailnetKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
recreateIfInvalid := shouldRecreateIfInvalid(d.Get("reusable").(bool), d.Get("recreate_if_invalid").(string))
client := m.(*tailscale.Client)
key, err := client.GetKey(ctx, d.Id())
switch {
case tailscale.IsNotFound(err):
if recreateIfInvalid {
d.SetId("")
}
return nil
case err != nil:
return diagnosticsError(err, "Failed to fetch key")
}
// The Tailscale API continues to return keys for some time after they've expired.
// Use `invalid` key property to determine if key should be recreated.
if key.Invalid && recreateIfInvalid {
d.SetId("")
return nil
}
d.SetId(key.ID)
if err = d.Set("reusable", key.Capabilities.Devices.Create.Reusable); err != nil {
return diagnosticsError(err, "Failed to set reusable")
}
if err = d.Set("ephemeral", key.Capabilities.Devices.Create.Ephemeral); err != nil {
return diagnosticsError(err, "failed to set ephemeral")
}
if err = d.Set("created_at", key.Created.Format(time.RFC3339)); err != nil {
return diagnosticsError(err, "Failed to set created_at")
}
if err = d.Set("expires_at", key.Expires.Format(time.RFC3339)); err != nil {
return diagnosticsError(err, "Failed to set expires_at")
}
if err = d.Set("description", key.Description); err != nil {
return diagnosticsError(err, "Failed to set description")
}
if err = d.Set("invalid", key.Invalid); err != nil {
return diagnosticsError(err, "Failed to set 'invalid'")
}
return nil
}