-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
sshkey.go
162 lines (135 loc) · 4.5 KB
/
sshkey.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
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package scalewaytasks
import (
"fmt"
"strings"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/pki"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/scaleway"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
iam "github.com/scaleway/scaleway-sdk-go/api/iam/v1alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
// +kops:fitask
type SSHKey struct {
ID *string
Name *string
Lifecycle fi.Lifecycle
PublicKey *fi.Resource
KeyPairFingerPrint *string
}
var _ fi.CompareWithID = &SSHKey{}
func (s *SSHKey) CompareWithID() *string {
return s.Name
}
func (s *SSHKey) Find(c *fi.CloudupContext) (*SSHKey, error) {
cloud := c.T.Cloud.(scaleway.ScwCloud)
keysResp, err := cloud.IamService().ListSSHKeys(&iam.ListSSHKeysRequest{
Name: s.Name,
}, scw.WithAllPages())
if err != nil {
return nil, fmt.Errorf("error listing SSH keys: %v", err)
}
if keysResp.TotalCount == 0 {
return nil, nil
}
if keysResp.TotalCount != 1 {
return nil, fmt.Errorf("found multiple SSH keys named %q", *s.Name)
}
klog.V(2).Infof("found matching SSH key named %q", *s.Name)
k := keysResp.SSHKeys[0]
sshKey := &SSHKey{
ID: fi.PtrTo(k.ID),
Name: fi.PtrTo(k.Name),
KeyPairFingerPrint: fi.PtrTo(k.Fingerprint),
Lifecycle: s.Lifecycle,
}
// Avoid spurious changes
if !strings.Contains(fi.ValueOf(sshKey.KeyPairFingerPrint), fi.ValueOf(s.KeyPairFingerPrint)) {
return nil, fmt.Errorf("computed SSH key fingerprint mismatch: %q %q", fi.ValueOf(s.KeyPairFingerPrint), fi.ValueOf(sshKey.KeyPairFingerPrint))
}
klog.V(2).Infof("SSH key fingerprints match; assuming public keys match")
sshKey.PublicKey = s.PublicKey
sshKey.KeyPairFingerPrint = s.KeyPairFingerPrint
return sshKey, nil
}
func (s *SSHKey) Run(c *fi.CloudupContext) error {
if s.KeyPairFingerPrint == nil && s.PublicKey != nil {
publicKey, err := fi.ResourceAsString(*s.PublicKey)
if err != nil {
return fmt.Errorf("error reading SSH public key: %w", err)
}
keyPairFingerPrint, err := pki.ComputeOpenSSHKeyFingerprint(publicKey)
if err != nil {
return fmt.Errorf("error computing key fingerprint for SSH key: %v", err)
}
klog.V(2).Infof("Computed SSH key fingerprint as %q", keyPairFingerPrint)
s.KeyPairFingerPrint = &keyPairFingerPrint
}
return fi.CloudupDefaultDeltaRunMethod(s, c)
}
func (s *SSHKey) CheckChanges(actual, expected, changes *SSHKey) error {
if actual != nil {
if changes.Name != nil {
return fi.CannotChangeField("Name")
}
}
return nil
}
func (*SSHKey) RenderScw(t *scaleway.ScwAPITarget, actual, expected, changes *SSHKey) error {
if actual != nil {
klog.Infof("Scaleway does not support changes to ssh keys for the moment")
return nil
}
cloud := t.Cloud.(scaleway.ScwCloud)
name := fi.ValueOf(expected.Name)
if name == "" {
return fi.RequiredField("Name")
}
klog.V(2).Infof("Creating keypair with name: %q", name)
keyArgs := &iam.CreateSSHKeyRequest{
Name: name,
}
if expected.PublicKey != nil {
d, err := fi.ResourceAsString(*expected.PublicKey)
if err != nil {
return fmt.Errorf("error rendering SSH public key: %w", err)
}
keyArgs.PublicKey = d
}
key, err := cloud.IamService().CreateSSHKey(keyArgs)
if err != nil {
return fmt.Errorf("error creating SSH keypair: %w", err)
}
expected.KeyPairFingerPrint = fi.PtrTo(key.Fingerprint)
klog.V(2).Infof("Created a new SSH keypair, id=%q fingerprint=%q", key.ID, key.Fingerprint)
return nil
}
type terraformSSHKey struct {
Name *string `cty:"name"`
PublicKey *string `cty:"public_key"`
}
func (_ *SSHKey) RenderTerraform(t *terraform.TerraformTarget, actual, expected, changes *SSHKey) error {
tfName := strings.ReplaceAll(fi.ValueOf(expected.Name), ".", "-")
publicKeyStr, err := fi.ResourceAsString(fi.ValueOf(expected.PublicKey))
if err != nil {
return err
}
tf := terraformSSHKey{
Name: expected.Name,
PublicKey: fi.PtrTo(publicKeyStr),
}
return t.RenderResource("scaleway_iam_ssh_key", tfName, tf)
}