-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathauthorizedkeys_test.go
More file actions
108 lines (94 loc) · 3.63 KB
/
authorizedkeys_test.go
File metadata and controls
108 lines (94 loc) · 3.63 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
// Copyright 2024 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
"slices"
"strings"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
)
type AuthorizedKeysSerializationSuite struct {
SliceSerializationSuite
}
var _ = gc.Suite(&AuthorizedKeysSerializationSuite{})
func (s *AuthorizedKeysSerializationSuite) SetUpTest(c *gc.C) {
s.SliceSerializationSuite.SetUpTest(c)
s.importName = "users-authorized-keys"
s.sliceName = "users-authorized-keys"
s.importFunc = func(m map[string]any) (any, error) {
return importAuthorizedKeys(m)
}
s.testFields = func(m map[string]any) {
m["users-authorized-keys"] = []any{}
}
}
// TestNewUserAuthorizedKeys is testing that given a set of arguments we get
// back a [userAuthorizedKeys] struct that contains the information passed in
// via args.
func (s *AuthorizedKeysSerializationSuite) TestNewUserAuthorizedKeys(c *gc.C) {
args := UserAuthorizedKeysArgs{
Username: "tlm",
AuthorizedKeys: []string{
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII4GpCvqUUYUJlx6d1kpUO9k/t4VhSYsf0yE0/QTqDzC existing1",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJQJ9wv0uC3yytXM3d2sJJWvZLuISKo7ZHwafHVviwVe existing2",
},
}
uak := newUserAuthorizedKeys(args)
c.Check(uak.Username(), gc.Equals, "tlm")
c.Check(uak.AuthorizedKeys(), jc.DeepEquals, []string{
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII4GpCvqUUYUJlx6d1kpUO9k/t4VhSYsf0yE0/QTqDzC existing1",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJQJ9wv0uC3yytXM3d2sJJWvZLuISKo7ZHwafHVviwVe existing2",
})
}
// TestParsingSerializedData is asserting that we can marshal and unmarshal user
// authorized keys to from the go types to yaml and get back the data we expect.
// We give a two user example below to demonstrate a more complex scenario.
func (s *AuthorizedKeysSerializationSuite) TestParsingSerializedData(c *gc.C) {
initial := authorizedKeys{
Version: 1,
UserAuthorizedKeys_: []*userAuthorizedKeys{
newUserAuthorizedKeys(UserAuthorizedKeysArgs{
Username: "tlm",
AuthorizedKeys: []string{
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII4GpCvqUUYUJlx6d1kpUO9k/t4VhSYsf0yE0/QTqDzC existing1",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJQJ9wv0uC3yytXM3d2sJJWvZLuISKo7ZHwafHVviwVe existing2",
},
}),
newUserAuthorizedKeys(UserAuthorizedKeysArgs{
Username: "wallyworld",
AuthorizedKeys: []string{
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII4GpCvqUUYUJlx6d1kpUO9k/t4VhSYsf0yE0/QTqDzC existing1",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJQJ9wv0uC3yytXM3d2sJJWvZLuISKo7ZHwafHVviwVe existing2",
},
}),
},
}
bytes, err := yaml.Marshal(initial)
c.Assert(err, jc.ErrorIsNil)
var source map[string]any
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
usersAuthorizedKeys, err := importAuthorizedKeys(source)
c.Assert(err, jc.ErrorIsNil)
slices.SortFunc(usersAuthorizedKeys, func(a, b *userAuthorizedKeys) int {
return strings.Compare(a.Username(), b.Username())
})
c.Check(usersAuthorizedKeys, jc.DeepEquals, []*userAuthorizedKeys{
newUserAuthorizedKeys(UserAuthorizedKeysArgs{
Username: "tlm",
AuthorizedKeys: []string{
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII4GpCvqUUYUJlx6d1kpUO9k/t4VhSYsf0yE0/QTqDzC existing1",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJQJ9wv0uC3yytXM3d2sJJWvZLuISKo7ZHwafHVviwVe existing2",
},
}),
newUserAuthorizedKeys(UserAuthorizedKeysArgs{
Username: "wallyworld",
AuthorizedKeys: []string{
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAII4GpCvqUUYUJlx6d1kpUO9k/t4VhSYsf0yE0/QTqDzC existing1",
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJQJ9wv0uC3yytXM3d2sJJWvZLuISKo7ZHwafHVviwVe existing2",
},
}),
},
)
}