/
ssh_key.go
202 lines (176 loc) · 5.33 KB
/
ssh_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
package iam
import (
"bytes"
"context"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"golang.org/x/crypto/ssh"
)
func ResourceSSKKey() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIamSSKKeyCreate,
ReadContext: resourceIamSSHKeyRead,
UpdateContext: resourceIamSSKKeyUpdate,
DeleteContext: resourceIamSSKKeyDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The name of the iam SSH key",
},
"public_key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The public SSH key",
// We don't consider trailing \n as diff
DiffSuppressFunc: func(_, oldValue, newValue string, _ *schema.ResourceData) bool {
parsedOldValue, _, _, _, err := ssh.ParseAuthorizedKey([]byte(oldValue))
if err != nil {
return false
}
parsedNewValue, _, _, _, err := ssh.ParseAuthorizedKey([]byte(newValue))
if err != nil {
return false
}
marshalledOldValue := ssh.MarshalAuthorizedKey(parsedOldValue)
marshalledNewValue := ssh.MarshalAuthorizedKey(parsedNewValue)
areEqual := bytes.Equal(marshalledOldValue, marshalledNewValue)
return areEqual
},
},
"fingerprint": {
Type: schema.TypeString,
Computed: true,
Description: "The fingerprint of the iam SSH key",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time of the creation of the iam SSH Key",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time of the last update of the iam SSH Key",
},
"organization_id": account.OrganizationIDSchema(),
"project_id": account.ProjectIDSchema(),
"disabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "The SSH key status",
},
},
}
}
func resourceIamSSKKeyCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := NewAPI(m)
res, err := api.CreateSSHKey(&iam.CreateSSHKeyRequest{
Name: d.Get("name").(string),
PublicKey: strings.Trim(d.Get("public_key").(string), "\n"),
ProjectID: (d.Get("project_id")).(string),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
if _, disabledExists := d.GetOk("disabled"); disabledExists {
_, err = api.UpdateSSHKey(&iam.UpdateSSHKeyRequest{
SSHKeyID: d.Id(),
Disabled: types.ExpandBoolPtr(types.GetBool(d, "disabled")),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
}
d.SetId(res.ID)
return resourceIamSSHKeyRead(ctx, d, m)
}
func resourceIamSSHKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := NewAPI(m)
res, err := api.GetSSHKey(&iam.GetSSHKeyRequest{
SSHKeyID: d.Id(),
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
_ = d.Set("name", res.Name)
_ = d.Set("public_key", res.PublicKey)
_ = d.Set("fingerprint", res.Fingerprint)
_ = d.Set("created_at", types.FlattenTime(res.CreatedAt))
_ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt))
_ = d.Set("organization_id", res.OrganizationID)
_ = d.Set("project_id", res.ProjectID)
_ = d.Set("disabled", res.Disabled)
return nil
}
func resourceIamSSKKeyUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := NewAPI(m)
req := &iam.UpdateSSHKeyRequest{
SSHKeyID: d.Id(),
}
hasUpdated := false
if d.HasChange("name") {
req.Name = types.ExpandStringPtr(d.Get("name"))
hasUpdated = true
}
if d.HasChange("disabled") {
if _, disabledExists := d.GetOk("disabled"); !disabledExists {
_, err := api.UpdateSSHKey(&iam.UpdateSSHKeyRequest{
SSHKeyID: d.Id(),
Disabled: types.ExpandBoolPtr(false),
})
if err != nil {
return diag.FromErr(err)
}
} else {
_, err := api.UpdateSSHKey(&iam.UpdateSSHKeyRequest{
SSHKeyID: d.Id(),
Disabled: types.ExpandBoolPtr(types.GetBool(d, "disabled")),
})
if err != nil {
return diag.FromErr(err)
}
}
}
if hasUpdated {
_, err := api.UpdateSSHKey(req, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
}
return resourceIamSSHKeyRead(ctx, d, m)
}
func resourceIamSSKKeyDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api := NewAPI(m)
err := api.DeleteSSHKey(&iam.DeleteSSHKeyRequest{
SSHKeyID: d.Id(),
}, scw.WithContext(ctx))
if err != nil && !httperrors.Is404(err) {
return diag.FromErr(err)
}
return nil
}
func removePublicSSHKeyComment(publicKey string) string {
parts := strings.Split(publicKey, " ")
if len(parts) == 3 {
return strings.Join(parts[:2], " ")
}
return publicKey
}