forked from scaleway/scaleway-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_init_ssh_test.go
More file actions
135 lines (122 loc) · 4.04 KB
/
custom_init_ssh_test.go
File metadata and controls
135 lines (122 loc) · 4.04 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
package init
import (
"os"
"path"
"path/filepath"
"testing"
"github.com/scaleway/scaleway-cli/v2/internal/core"
account "github.com/scaleway/scaleway-cli/v2/internal/namespaces/account/v2alpha1"
accountsdk "github.com/scaleway/scaleway-sdk-go/api/account/v2alpha1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
func setUpSSHKeyLocallyWithKeyName(key string, name string) core.BeforeFunc {
return func(ctx *core.BeforeFuncCtx) error {
homeDir := ctx.OverrideEnv["HOME"]
// TODO we persist the key as ~/.ssh/id_rsa.pub regardless of the type of key it is (rsa, ed25519)
keyPath := path.Join(homeDir, ".ssh", name)
ctx.Logger.Info("public key path set to: ", keyPath)
// Ensure the subfolders for the configuration files are all created
err := os.MkdirAll(filepath.Dir(keyPath), 0755)
if err != nil {
return err
}
// Write the configuration file
err = os.WriteFile(keyPath, []byte(key), 0600)
if err != nil {
return err
}
return nil
}
}
func removeSSHKeyFromAccount(publicSSHKey string) core.AfterFunc {
return func(ctx *core.AfterFuncCtx) error {
api := accountsdk.NewAPI(ctx.Client)
resp, err := api.ListSSHKeys(&accountsdk.ListSSHKeysRequest{},
scw.WithAllPages())
if err != nil {
return err
}
id := ""
for _, v := range resp.SSHKeys {
if v.PublicKey == publicSSHKey {
id = v.ID
}
}
if id != "" {
err = api.DeleteSSHKey(&accountsdk.DeleteSSHKeyRequest{SSHKeyID: id})
}
return err
}
}
// add an ssh key with a given meta key
func addSSHKeyToAccount(metaKey string, name string, key string) core.BeforeFunc {
return func(ctx *core.BeforeFuncCtx) error {
cmd := []string{
"scw", "account", "ssh-key", "add", "public-key=" + key, "name=" + name,
}
ctx.Meta[metaKey] = ctx.ExecuteCmd(cmd)
return nil
}
}
func Test_InitSSH(t *testing.T) {
defaultSettings := map[string]string{
"access-key": "{{ .AccessKey }}",
"secret-key": "{{ .SecretKey }}",
"send-telemetry": "false",
"remove-v1-config": "false",
"install-autocomplete": "false",
}
cmds := GetCommands()
cmds.Merge(account.GetCommands())
// We create a key in each tests to be able to run those tests in parallel
t.Run("KeyRegistered", func(t *testing.T) {
dummySSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAICd8ZxAm9mXQsRHhQ5iADEJuO+Ai8EbXMI7TIlsh9jbE foobar@foobar"
core.Test(&core.TestConfig{
Commands: cmds,
BeforeFunc: core.BeforeFuncCombine(
baseBeforeFunc(),
setUpSSHKeyLocallyWithKeyName(dummySSHKey, "id_rsa.pub"),
addSSHKeyToAccount("key", "test-cli-KeyRegistered", dummySSHKey),
),
Cmd: appendArgs("scw init with-ssh-key=true", defaultSettings),
Check: core.TestCheckGolden(),
AfterFunc: removeSSHKeyFromAccount(dummySSHKey),
TmpHomeDir: true,
})(t)
})
t.Run("KeyUnregistered", func(t *testing.T) {
dummySSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIQE67HxSRicWd4ol7ntM2jdeD/qEehPJxK/3thmMiZg foobar@foobar"
core.Test(&core.TestConfig{
Commands: cmds,
BeforeFunc: core.BeforeFuncCombine(
baseBeforeFunc(),
setUpSSHKeyLocallyWithKeyName(dummySSHKey, "id_rsa.pub"),
),
Cmd: appendArgs("scw init with-ssh-key=true", defaultSettings),
Check: core.TestCheckGolden(),
TmpHomeDir: true,
AfterFunc: removeSSHKeyFromAccount(dummySSHKey),
})(t)
})
t.Run("NoLocalKey", core.Test(&core.TestConfig{
Commands: cmds,
BeforeFunc: baseBeforeFunc(),
Cmd: appendArgs("scw init with-ssh-key=true", defaultSettings),
Check: core.TestCheckGolden(),
TmpHomeDir: true,
}))
t.Run("WithLocalEd25519Key", func(t *testing.T) {
dummySSHKey := "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIQE67HxSRicWd4ol7ntM2jdeD/qEehPJxK/3thmMiZg foobar@foobar"
core.Test(&core.TestConfig{
Commands: cmds,
BeforeFunc: core.BeforeFuncCombine(
baseBeforeFunc(),
setUpSSHKeyLocallyWithKeyName(dummySSHKey, "id_ed25519.pub"),
),
Cmd: appendArgs("scw init with-ssh-key=true", defaultSettings),
Check: core.TestCheckGolden(),
TmpHomeDir: true,
AfterFunc: removeSSHKeyFromAccount(dummySSHKey),
})(t)
})
}