This repository has been archived by the owner on Jul 21, 2020. It is now read-only.
forked from scaleway/terraform-provider-scaleway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_ssh_key.go
132 lines (110 loc) · 2.73 KB
/
resource_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
package scaleway
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
api "github.com/nicolai86/scaleway-sdk"
"golang.org/x/crypto/ssh"
)
func resourceScalewaySSHKey() *schema.Resource {
return &schema.Resource{
DeprecationMessage: "This resource is deprecated and will be removed in the next major version",
Create: resourceScalewaySSHKeyCreate,
Read: resourceScalewaySSHKeyRead,
Delete: resourceScalewaySSHKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ssh key",
},
},
}
}
func sshKeyFingerprint(key []byte) (string, error) {
pubkey, _, _, _, err := ssh.ParseAuthorizedKey(key)
if err != nil {
return "", err
}
return ssh.FingerprintLegacyMD5(pubkey), nil
}
func resourceScalewaySSHKeyCreate(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Meta).deprecatedClient
fingerprint, err := sshKeyFingerprint([]byte(d.Get("key").(string)))
if err != nil {
return err
}
user, err := scaleway.GetUser()
if err != nil {
return err
}
keys := []api.KeyDefinition{}
exists := false
for _, key := range user.SSHPublicKeys {
exists = exists || key.Key == d.Get("key").(string)
keys = append(keys, api.KeyDefinition{
Key: key.Key,
})
}
// remote already contains the key, nothing to do
if exists {
d.SetId(fingerprint)
return nil
}
_, err = scaleway.PatchUserSSHKey(user.ID, api.UserPatchSSHKeyDefinition{
SSHPublicKeys: append(keys, api.KeyDefinition{
Key: strings.TrimSpace(d.Get("key").(string)),
}),
})
if err != nil {
return err
}
d.SetId(fingerprint)
return nil
}
func resourceScalewaySSHKeyRead(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Meta).deprecatedClient
user, err := scaleway.GetUser()
if err != nil {
return err
}
exists := false
for _, key := range user.SSHPublicKeys {
exists = exists || strings.Contains(key.Fingerprint, d.Id())
if exists {
_ = d.Set("key", key.Key)
break
}
}
if !exists {
return fmt.Errorf("ssh key does not exist anymore")
}
return nil
}
func resourceScalewaySSHKeyDelete(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Meta).deprecatedClient
user, err := scaleway.GetUser()
if err != nil {
return err
}
keys := []api.KeyDefinition{}
for _, key := range user.SSHPublicKeys {
if !strings.Contains(key.Fingerprint, d.Id()) {
keys = append(keys, api.KeyDefinition{
Key: key.Key,
})
}
}
_, err = scaleway.PatchUserSSHKey(user.ID, api.UserPatchSSHKeyDefinition{
SSHPublicKeys: keys,
})
if err != nil {
return err
}
d.SetId("")
return nil
}