-
Notifications
You must be signed in to change notification settings - Fork 0
/
certificate_test.go
149 lines (130 loc) · 5.08 KB
/
certificate_test.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
package sshx
import (
"crypto/rand"
"fmt"
"io/fs"
"math/big"
"os"
"path/filepath"
"testing"
"time"
gocmp "github.com/google/go-cmp/cmp"
"golang.org/x/crypto/ssh"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)
func Test_NewCertificateFromOpenSSHAuthorizedKeyBytes(t *testing.T) {
t.Run("ok", func(t *testing.T) {
privKey := newTestPrivateKey(t)
sshCert := newTestSSHCertificate(t, privKey, privKey.Signer())
cert, err := NewCertificateFromOpenSSHAuthorizedKeyBytes(ssh.MarshalAuthorizedKey(sshCert))
assert.NilError(t, err)
assert.Check(t, cmpCertificate(cert, (*Certificate)(sshCert)))
})
t.Run("ko", func(t *testing.T) {
t.Run("not respecting authorized-key format", func(t *testing.T) {
cert, err := NewCertificateFromOpenSSHAuthorizedKeyBytes([]byte("42"))
assert.Check(t, cmp.Nil(cert))
assert.Assert(t, cmp.ErrorContains(err, "unable to parse ssh certificate from raw openssh certificate"))
})
t.Run("respecting authorized-key format but not a certificate", func(t *testing.T) {
privKey := newTestPrivateKey(t)
cert, err := NewCertificateFromOpenSSHAuthorizedKeyBytes(ssh.MarshalAuthorizedKey(privKey.PublicKey()))
assert.Check(t, cmp.Nil(cert))
assert.Assert(t, cmp.ErrorContains(err, "parsed authorized key is not a certificate"))
})
})
}
func Test_NewCertificateFromOpenSSHAuthorizedKeyFile(t *testing.T) {
t.Run("ok", func(t *testing.T) {
privKey := newTestPrivateKey(t)
sshCert := newTestSSHCertificate(t, privKey, privKey.Signer())
filePath := filepath.Join(t.TempDir(), "rsa-cert.pub")
assert.NilError(t, os.WriteFile(filePath, ssh.MarshalAuthorizedKey(sshCert), 0o400))
cert, err := NewCertificateFromOpenSSHAuthorizedKeyFile(filePath)
assert.NilError(t, err)
assert.Check(t, cmpCertificate(cert, (*Certificate)(sshCert)))
})
t.Run("ko", func(t *testing.T) {
t.Run("unable to read", func(t *testing.T) {
cert, err := NewCertificateFromOpenSSHAuthorizedKeyFile("notfound-cert.pub")
assert.Assert(t, cmp.ErrorContains(err, "unable to read"))
assert.ErrorIs(t, err, fs.ErrNotExist)
assert.Check(t, cmp.Nil(cert))
})
t.Run("invalid cert", func(t *testing.T) {
filePath := filepath.Join(t.TempDir(), "invalid.file")
assert.NilError(t, os.WriteFile(filePath, []byte("hello"), 0o400))
cert, err := NewCertificateFromOpenSSHAuthorizedKeyFile(filePath)
assert.Assert(t, cmp.ErrorContains(err, "unable to parse ssh certificate"))
assert.Check(t, cmp.Nil(cert))
})
})
}
func Test_Certificate_IsValid(t *testing.T) {
privKey := newTestPrivateKey(t)
now := time.Now()
t.Run("no validity constraints", func(t *testing.T) {
sshCert := newTestSSHCertificate(t, privKey, privKey.Signer(), func(cert *ssh.Certificate) {
cert.ValidAfter = 0
cert.ValidBefore = 0
})
cert := (*Certificate)(sshCert)
assert.NilError(t, cert.IsValid())
})
t.Run("validity constraints", func(t *testing.T) {
sshCert := newTestSSHCertificate(t, privKey, privKey.Signer(), func(cert *ssh.Certificate) {
cert.ValidBefore = uint64(now.Add(time.Hour).Unix())
cert.ValidAfter = uint64(now.Add(-time.Hour).Unix())
})
cert := (*Certificate)(sshCert)
assert.NilError(t, cert.IsValid())
})
t.Run("not yet valid", func(t *testing.T) {
sshCert := newTestSSHCertificate(t, privKey, privKey.Signer(), func(cert *ssh.Certificate) {
cert.ValidAfter = uint64(now.Add(time.Hour).Unix())
})
cert := (*Certificate)(sshCert)
err := cert.IsValid()
assert.Assert(t, cmp.ErrorContains(err, "certificate validity starts in"))
})
t.Run("expired", func(t *testing.T) {
sshCert := newTestSSHCertificate(t, privKey, privKey.Signer(), func(cert *ssh.Certificate) {
cert.ValidBefore = uint64(now.Add(-time.Hour).Unix())
})
cert := (*Certificate)(sshCert)
assert.Assert(t, cmp.ErrorContains(cert.IsValid(), "certificate validity expired"))
})
}
func newTestSSHCertificate(t *testing.T, privKey PrivateKey, signer ssh.Signer, setups ...func(cert *ssh.Certificate)) *ssh.Certificate {
cert := &ssh.Certificate{
Nonce: []byte{}, // To pass reflect.DeepEqual after marshal & parse, this must be non-nil.
Key: privKey.PublicKey(),
CertType: ssh.HostCert,
ValidPrincipals: []string{"foo", "bar"},
Permissions: ssh.Permissions{ // To pass reflect.DeepEqual after marshal & parse, this must be non-nil.
CriticalOptions: make(map[string]string),
Extensions: make(map[string]string),
},
Reserved: []byte{}, // To pass reflect.DeepEqual after marshal & parse, this must be non-nil.
SignatureKey: privKey.PublicKey(),
}
for _, setup := range setups {
setup(cert)
}
assert.NilError(t, cert.SignCert(rand.Reader, signer))
return cert
}
func cmpCertificate(x, y *Certificate) cmp.Comparison {
return func() cmp.Result {
// compare public keys first
if err := WrapSSHPublicKey(x.Key).Equal(WrapSSHPublicKey(y.Key)); err != nil {
return cmp.ResultFromError(fmt.Errorf("x.Key != y.Key: %v", err))
}
// then compare the rest
return cmp.DeepEqual(x, y,
gocmp.AllowUnexported(big.Int{}),
gocmp.FilterPath(func(path gocmp.Path) bool { return path.String() == "Key" }, gocmp.Ignore()),
)()
}
}