2019-09-12 05:57:55 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
2020-01-06 22:21:13 +00:00
|
|
|
"reflect"
|
2019-09-12 05:57:55 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/assert"
|
2020-08-14 22:33:50 +00:00
|
|
|
"go.step.sm/crypto/keyutil"
|
2019-09-12 05:57:55 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
2020-01-04 02:22:02 +00:00
|
|
|
func TestSSHOptions_Type(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
CertType string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
want uint32
|
|
|
|
}{
|
|
|
|
{"user", fields{"user"}, 1},
|
|
|
|
{"host", fields{"host"}, 2},
|
|
|
|
{"empty", fields{""}, 0},
|
|
|
|
{"invalid", fields{"invalid"}, 0},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-07-23 01:24:45 +00:00
|
|
|
o := SignSSHOptions{
|
2020-01-04 02:22:02 +00:00
|
|
|
CertType: tt.fields.CertType,
|
|
|
|
}
|
|
|
|
if got := o.Type(); got != tt.want {
|
|
|
|
t.Errorf("SSHOptions.Type() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
func TestSSHOptions_Modify(t *testing.T) {
|
|
|
|
type test struct {
|
2020-07-31 01:44:52 +00:00
|
|
|
so SignSSHOptions
|
2019-12-20 21:30:05 +00:00
|
|
|
cert *ssh.Certificate
|
|
|
|
valid func(*ssh.Certificate)
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string](func() test){
|
|
|
|
"fail/unexpected-cert-type": func() test {
|
|
|
|
return test{
|
2020-07-31 01:44:52 +00:00
|
|
|
so: SignSSHOptions{CertType: "foo"},
|
2019-12-20 21:30:05 +00:00
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
err: errors.Errorf("ssh certificate has an unknown type - foo"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/validAfter-greater-validBefore": func() test {
|
|
|
|
return test{
|
2020-07-31 01:44:52 +00:00
|
|
|
so: SignSSHOptions{CertType: "user"},
|
2019-12-20 21:30:05 +00:00
|
|
|
cert: &ssh.Certificate{ValidAfter: uint64(15), ValidBefore: uint64(10)},
|
|
|
|
err: errors.Errorf("ssh certificate valid after cannot be greater than valid before"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/user-cert": func() test {
|
|
|
|
return test{
|
2020-07-31 01:44:52 +00:00
|
|
|
so: SignSSHOptions{CertType: "user"},
|
2019-12-20 21:30:05 +00:00
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Equals(t, cert.CertType, uint32(ssh.UserCert))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/host-cert": func() test {
|
|
|
|
return test{
|
2020-07-31 01:44:52 +00:00
|
|
|
so: SignSSHOptions{CertType: "host"},
|
2019-12-20 21:30:05 +00:00
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Equals(t, cert.CertType, uint32(ssh.HostCert))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func() test {
|
|
|
|
va := time.Now().Add(5 * time.Minute)
|
|
|
|
vb := time.Now().Add(1 * time.Hour)
|
2020-07-31 01:44:52 +00:00
|
|
|
so := SignSSHOptions{CertType: "host", KeyID: "foo", Principals: []string{"foo", "bar"},
|
2019-12-20 21:30:05 +00:00
|
|
|
ValidAfter: NewTimeDuration(va), ValidBefore: NewTimeDuration(vb)}
|
|
|
|
return test{
|
|
|
|
so: so,
|
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Equals(t, cert.CertType, uint32(ssh.HostCert))
|
|
|
|
assert.Equals(t, cert.KeyId, so.KeyID)
|
|
|
|
assert.Equals(t, cert.ValidPrincipals, so.Principals)
|
|
|
|
assert.Equals(t, cert.ValidAfter, uint64(so.ValidAfter.RelativeTime(time.Now()).Unix()))
|
|
|
|
assert.Equals(t, cert.ValidBefore, uint64(so.ValidBefore.RelativeTime(time.Now()).Unix()))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := run()
|
2020-07-31 01:44:52 +00:00
|
|
|
if err := tc.so.Modify(tc.cert, tc.so); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tc.err) {
|
|
|
|
tc.valid(tc.cert)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSSHOptions_Match(t *testing.T) {
|
|
|
|
type test struct {
|
2020-07-23 01:24:45 +00:00
|
|
|
so SignSSHOptions
|
|
|
|
cmp SignSSHOptions
|
2019-12-20 21:30:05 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string](func() test){
|
|
|
|
"fail/cert-type": func() test {
|
|
|
|
return test{
|
2020-07-23 01:24:45 +00:00
|
|
|
so: SignSSHOptions{CertType: "foo"},
|
|
|
|
cmp: SignSSHOptions{CertType: "bar"},
|
2019-12-20 21:30:05 +00:00
|
|
|
err: errors.Errorf("ssh certificate type does not match - got bar, want foo"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/pricipals": func() test {
|
|
|
|
return test{
|
2020-07-23 01:24:45 +00:00
|
|
|
so: SignSSHOptions{Principals: []string{"foo"}},
|
|
|
|
cmp: SignSSHOptions{Principals: []string{"bar"}},
|
2019-12-20 21:30:05 +00:00
|
|
|
err: errors.Errorf("ssh certificate principals does not match - got [bar], want [foo]"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/validAfter": func() test {
|
|
|
|
return test{
|
2020-07-23 01:24:45 +00:00
|
|
|
so: SignSSHOptions{ValidAfter: NewTimeDuration(time.Now().Add(1 * time.Minute))},
|
|
|
|
cmp: SignSSHOptions{ValidAfter: NewTimeDuration(time.Now().Add(5 * time.Minute))},
|
2019-12-20 21:30:05 +00:00
|
|
|
err: errors.Errorf("ssh certificate valid after does not match"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/validBefore": func() test {
|
|
|
|
return test{
|
2020-07-23 01:24:45 +00:00
|
|
|
so: SignSSHOptions{ValidBefore: NewTimeDuration(time.Now().Add(1 * time.Minute))},
|
|
|
|
cmp: SignSSHOptions{ValidBefore: NewTimeDuration(time.Now().Add(5 * time.Minute))},
|
2019-12-20 21:30:05 +00:00
|
|
|
err: errors.Errorf("ssh certificate valid before does not match"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/original-empty": func() test {
|
|
|
|
return test{
|
2020-07-23 01:24:45 +00:00
|
|
|
so: SignSSHOptions{},
|
|
|
|
cmp: SignSSHOptions{
|
2019-12-20 21:30:05 +00:00
|
|
|
CertType: "foo",
|
|
|
|
Principals: []string{"foo"},
|
|
|
|
ValidAfter: NewTimeDuration(time.Now().Add(1 * time.Minute)),
|
|
|
|
ValidBefore: NewTimeDuration(time.Now().Add(5 * time.Minute)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/cmp-empty": func() test {
|
|
|
|
return test{
|
2020-07-23 01:24:45 +00:00
|
|
|
cmp: SignSSHOptions{},
|
|
|
|
so: SignSSHOptions{
|
2019-12-20 21:30:05 +00:00
|
|
|
CertType: "foo",
|
|
|
|
Principals: []string{"foo"},
|
|
|
|
ValidAfter: NewTimeDuration(time.Now().Add(1 * time.Minute)),
|
|
|
|
ValidBefore: NewTimeDuration(time.Now().Add(5 * time.Minute)),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/equal": func() test {
|
|
|
|
n := time.Now()
|
|
|
|
va := NewTimeDuration(n.Add(1 * time.Minute))
|
|
|
|
vb := NewTimeDuration(n.Add(5 * time.Minute))
|
|
|
|
return test{
|
2020-07-23 01:24:45 +00:00
|
|
|
cmp: SignSSHOptions{
|
2019-12-20 21:30:05 +00:00
|
|
|
CertType: "foo",
|
|
|
|
Principals: []string{"foo"},
|
|
|
|
ValidAfter: va,
|
|
|
|
ValidBefore: vb,
|
|
|
|
},
|
2020-07-23 01:24:45 +00:00
|
|
|
so: SignSSHOptions{
|
2019-12-20 21:30:05 +00:00
|
|
|
CertType: "foo",
|
|
|
|
Principals: []string{"foo"},
|
|
|
|
ValidAfter: va,
|
|
|
|
ValidBefore: vb,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := run()
|
|
|
|
if err := tc.so.match(tc.cmp); err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_sshCertPrincipalsModifier_Modify(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
modifier sshCertPrincipalsModifier
|
|
|
|
cert *ssh.Certificate
|
|
|
|
expected []string
|
|
|
|
}
|
|
|
|
tests := map[string](func() test){
|
|
|
|
"ok": func() test {
|
|
|
|
a := []string{"foo", "bar"}
|
|
|
|
return test{
|
|
|
|
modifier: sshCertPrincipalsModifier(a),
|
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
expected: a,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := run()
|
2020-07-31 01:44:52 +00:00
|
|
|
if assert.Nil(t, tc.modifier.Modify(tc.cert, SignSSHOptions{})) {
|
2019-12-20 21:30:05 +00:00
|
|
|
assert.Equals(t, tc.cert.ValidPrincipals, tc.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_sshCertKeyIDModifier_Modify(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
modifier sshCertKeyIDModifier
|
|
|
|
cert *ssh.Certificate
|
|
|
|
expected string
|
|
|
|
}
|
|
|
|
tests := map[string](func() test){
|
|
|
|
"ok": func() test {
|
|
|
|
a := "foo"
|
|
|
|
return test{
|
|
|
|
modifier: sshCertKeyIDModifier(a),
|
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
expected: a,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := run()
|
2020-07-31 01:44:52 +00:00
|
|
|
if assert.Nil(t, tc.modifier.Modify(tc.cert, SignSSHOptions{})) {
|
2019-12-20 21:30:05 +00:00
|
|
|
assert.Equals(t, tc.cert.KeyId, tc.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_sshCertTypeModifier_Modify(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
modifier sshCertTypeModifier
|
|
|
|
cert *ssh.Certificate
|
|
|
|
expected uint32
|
|
|
|
}
|
|
|
|
tests := map[string](func() test){
|
|
|
|
"ok/user": func() test {
|
|
|
|
return test{
|
|
|
|
modifier: sshCertTypeModifier("user"),
|
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
expected: ssh.UserCert,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/host": func() test {
|
|
|
|
return test{
|
|
|
|
modifier: sshCertTypeModifier("host"),
|
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
expected: ssh.HostCert,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/default": func() test {
|
|
|
|
return test{
|
|
|
|
modifier: sshCertTypeModifier("foo"),
|
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
expected: 0,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := run()
|
2020-07-31 01:44:52 +00:00
|
|
|
if assert.Nil(t, tc.modifier.Modify(tc.cert, SignSSHOptions{})) {
|
2019-12-20 21:30:05 +00:00
|
|
|
assert.Equals(t, tc.cert.CertType, uint32(tc.expected))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_sshCertValidAfterModifier_Modify(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
modifier sshCertValidAfterModifier
|
|
|
|
cert *ssh.Certificate
|
|
|
|
expected uint64
|
|
|
|
}
|
|
|
|
tests := map[string](func() test){
|
|
|
|
"ok": func() test {
|
|
|
|
return test{
|
|
|
|
modifier: sshCertValidAfterModifier(15),
|
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
expected: 15,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := run()
|
2020-07-31 01:44:52 +00:00
|
|
|
if assert.Nil(t, tc.modifier.Modify(tc.cert, SignSSHOptions{})) {
|
2019-12-20 21:30:05 +00:00
|
|
|
assert.Equals(t, tc.cert.ValidAfter, tc.expected)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_sshCertDefaultsModifier_Modify(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
modifier sshCertDefaultsModifier
|
|
|
|
cert *ssh.Certificate
|
|
|
|
valid func(*ssh.Certificate)
|
|
|
|
}
|
|
|
|
tests := map[string](func() test){
|
|
|
|
"ok/changes": func() test {
|
|
|
|
n := time.Now()
|
|
|
|
va := NewTimeDuration(n.Add(1 * time.Minute))
|
|
|
|
vb := NewTimeDuration(n.Add(5 * time.Minute))
|
2020-07-23 01:24:45 +00:00
|
|
|
so := SignSSHOptions{
|
2019-12-20 21:30:05 +00:00
|
|
|
Principals: []string{"foo", "bar"},
|
|
|
|
CertType: "host",
|
|
|
|
ValidAfter: va,
|
|
|
|
ValidBefore: vb,
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
modifier: sshCertDefaultsModifier(so),
|
|
|
|
cert: new(ssh.Certificate),
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Equals(t, cert.ValidPrincipals, so.Principals)
|
|
|
|
assert.Equals(t, cert.CertType, uint32(ssh.HostCert))
|
|
|
|
assert.Equals(t, cert.ValidAfter, uint64(so.ValidAfter.RelativeTime(time.Now()).Unix()))
|
|
|
|
assert.Equals(t, cert.ValidBefore, uint64(so.ValidBefore.RelativeTime(time.Now()).Unix()))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/no-changes": func() test {
|
|
|
|
n := time.Now()
|
2020-07-23 01:24:45 +00:00
|
|
|
so := SignSSHOptions{
|
2019-12-20 21:30:05 +00:00
|
|
|
Principals: []string{"foo", "bar"},
|
|
|
|
CertType: "host",
|
|
|
|
ValidAfter: NewTimeDuration(n.Add(15 * time.Minute)),
|
|
|
|
ValidBefore: NewTimeDuration(n.Add(25 * time.Minute)),
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
modifier: sshCertDefaultsModifier(so),
|
|
|
|
cert: &ssh.Certificate{
|
|
|
|
CertType: uint32(ssh.UserCert),
|
|
|
|
ValidPrincipals: []string{"zap", "zoop"},
|
|
|
|
ValidAfter: 15,
|
|
|
|
ValidBefore: 25,
|
|
|
|
},
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Equals(t, cert.ValidPrincipals, []string{"zap", "zoop"})
|
|
|
|
assert.Equals(t, cert.CertType, uint32(ssh.UserCert))
|
|
|
|
assert.Equals(t, cert.ValidAfter, uint64(15))
|
|
|
|
assert.Equals(t, cert.ValidBefore, uint64(25))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := run()
|
2020-07-31 01:44:52 +00:00
|
|
|
if assert.Nil(t, tc.modifier.Modify(tc.cert, SignSSHOptions{})) {
|
2019-12-20 21:30:05 +00:00
|
|
|
tc.valid(tc.cert)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_sshDefaultExtensionModifier_Modify(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
modifier sshDefaultExtensionModifier
|
|
|
|
cert *ssh.Certificate
|
|
|
|
valid func(*ssh.Certificate)
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string](func() test){
|
|
|
|
"fail/unexpected-cert-type": func() test {
|
|
|
|
cert := &ssh.Certificate{CertType: 3}
|
|
|
|
return test{
|
|
|
|
modifier: sshDefaultExtensionModifier{},
|
|
|
|
cert: cert,
|
|
|
|
err: errors.New("ssh certificate type has not been set or is invalid"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/host": func() test {
|
|
|
|
cert := &ssh.Certificate{CertType: ssh.HostCert}
|
|
|
|
return test{
|
|
|
|
modifier: sshDefaultExtensionModifier{},
|
|
|
|
cert: cert,
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Len(t, 0, cert.Extensions)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/user/extensions-exists": func() test {
|
|
|
|
cert := &ssh.Certificate{CertType: ssh.UserCert, Permissions: ssh.Permissions{Extensions: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
}}}
|
|
|
|
return test{
|
|
|
|
modifier: sshDefaultExtensionModifier{},
|
|
|
|
cert: cert,
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
val, ok := cert.Extensions["foo"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "bar")
|
|
|
|
|
|
|
|
val, ok = cert.Extensions["permit-X11-forwarding"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "")
|
|
|
|
|
|
|
|
val, ok = cert.Extensions["permit-agent-forwarding"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "")
|
|
|
|
|
|
|
|
val, ok = cert.Extensions["permit-port-forwarding"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "")
|
|
|
|
|
|
|
|
val, ok = cert.Extensions["permit-pty"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "")
|
|
|
|
|
|
|
|
val, ok = cert.Extensions["permit-user-rc"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/user/no-extensions": func() test {
|
|
|
|
return test{
|
|
|
|
modifier: sshDefaultExtensionModifier{},
|
|
|
|
cert: &ssh.Certificate{CertType: ssh.UserCert},
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
_, ok := cert.Extensions["foo"]
|
|
|
|
assert.False(t, ok)
|
|
|
|
|
|
|
|
val, ok := cert.Extensions["permit-X11-forwarding"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "")
|
|
|
|
|
|
|
|
val, ok = cert.Extensions["permit-agent-forwarding"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "")
|
|
|
|
|
|
|
|
val, ok = cert.Extensions["permit-port-forwarding"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "")
|
|
|
|
|
|
|
|
val, ok = cert.Extensions["permit-pty"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "")
|
|
|
|
|
|
|
|
val, ok = cert.Extensions["permit-user-rc"]
|
|
|
|
assert.True(t, ok)
|
|
|
|
assert.Equals(t, val, "")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := run()
|
2020-07-31 01:44:52 +00:00
|
|
|
if err := tc.modifier.Modify(tc.cert, SignSSHOptions{}); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tc.err) {
|
|
|
|
tc.valid(tc.cert)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
func Test_sshCertDefaultValidator_Valid(t *testing.T) {
|
2020-08-14 22:33:50 +00:00
|
|
|
pub, _, err := keyutil.GenerateDefaultKeyPair()
|
2019-09-12 05:57:55 +00:00
|
|
|
assert.FatalError(t, err)
|
|
|
|
sshPub, err := ssh.NewPublicKey(pub)
|
|
|
|
assert.FatalError(t, err)
|
2020-01-24 06:04:34 +00:00
|
|
|
v := sshCertDefaultValidator{}
|
2019-09-12 05:57:55 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
cert *ssh.Certificate
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail/zero-nonce",
|
|
|
|
&ssh.Certificate{},
|
|
|
|
errors.New("ssh certificate nonce cannot be empty"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/nil-key",
|
|
|
|
&ssh.Certificate{Nonce: []byte("foo")},
|
|
|
|
errors.New("ssh certificate key cannot be nil"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/zero-serial",
|
|
|
|
&ssh.Certificate{Nonce: []byte("foo"), Key: sshPub},
|
|
|
|
errors.New("ssh certificate serial cannot be 0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/unexpected-cert-type",
|
|
|
|
// UserCert = 1, HostCert = 2
|
|
|
|
&ssh.Certificate{Nonce: []byte("foo"), Key: sshPub, CertType: 3, Serial: 1},
|
|
|
|
errors.New("ssh certificate has an unknown type: 3"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/empty-cert-key-id",
|
|
|
|
&ssh.Certificate{Nonce: []byte("foo"), Key: sshPub, Serial: 1, CertType: 1},
|
|
|
|
errors.New("ssh certificate key id cannot be empty"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/zero-validAfter",
|
|
|
|
&ssh.Certificate{
|
|
|
|
Nonce: []byte("foo"),
|
|
|
|
Key: sshPub,
|
|
|
|
Serial: 1,
|
|
|
|
CertType: 1,
|
|
|
|
KeyId: "foo",
|
|
|
|
ValidPrincipals: []string{"foo"},
|
|
|
|
ValidAfter: 0,
|
|
|
|
},
|
|
|
|
errors.New("ssh certificate validAfter cannot be 0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/validBefore-past",
|
|
|
|
&ssh.Certificate{
|
|
|
|
Nonce: []byte("foo"),
|
|
|
|
Key: sshPub,
|
|
|
|
Serial: 1,
|
|
|
|
CertType: 1,
|
|
|
|
KeyId: "foo",
|
|
|
|
ValidPrincipals: []string{"foo"},
|
|
|
|
ValidAfter: uint64(time.Now().Add(-10 * time.Minute).Unix()),
|
|
|
|
ValidBefore: uint64(time.Now().Add(-5 * time.Minute).Unix()),
|
|
|
|
},
|
|
|
|
errors.New("ssh certificate validBefore cannot be in the past"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/validAfter-after-validBefore",
|
|
|
|
&ssh.Certificate{
|
|
|
|
Nonce: []byte("foo"),
|
|
|
|
Key: sshPub,
|
|
|
|
Serial: 1,
|
|
|
|
CertType: 1,
|
|
|
|
KeyId: "foo",
|
|
|
|
ValidPrincipals: []string{"foo"},
|
|
|
|
ValidAfter: uint64(time.Now().Add(15 * time.Minute).Unix()),
|
|
|
|
ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
|
|
|
|
},
|
|
|
|
errors.New("ssh certificate validBefore cannot be before validAfter"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/nil-signature-key",
|
|
|
|
&ssh.Certificate{
|
|
|
|
Nonce: []byte("foo"),
|
|
|
|
Key: sshPub,
|
|
|
|
Serial: 1,
|
|
|
|
CertType: 1,
|
|
|
|
KeyId: "foo",
|
|
|
|
ValidPrincipals: []string{"foo"},
|
|
|
|
ValidAfter: uint64(time.Now().Unix()),
|
|
|
|
ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
|
|
|
|
Permissions: ssh.Permissions{
|
|
|
|
Extensions: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
errors.New("ssh certificate signature key cannot be nil"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/nil-signature",
|
|
|
|
&ssh.Certificate{
|
|
|
|
Nonce: []byte("foo"),
|
|
|
|
Key: sshPub,
|
|
|
|
Serial: 1,
|
|
|
|
CertType: 1,
|
|
|
|
KeyId: "foo",
|
|
|
|
ValidPrincipals: []string{"foo"},
|
|
|
|
ValidAfter: uint64(time.Now().Unix()),
|
|
|
|
ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
|
|
|
|
Permissions: ssh.Permissions{
|
|
|
|
Extensions: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
SignatureKey: sshPub,
|
|
|
|
},
|
|
|
|
errors.New("ssh certificate signature cannot be nil"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ok/userCert",
|
|
|
|
&ssh.Certificate{
|
|
|
|
Nonce: []byte("foo"),
|
|
|
|
Key: sshPub,
|
|
|
|
Serial: 1,
|
|
|
|
CertType: 1,
|
|
|
|
KeyId: "foo",
|
|
|
|
ValidPrincipals: []string{"foo"},
|
|
|
|
ValidAfter: uint64(time.Now().Unix()),
|
|
|
|
ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
|
|
|
|
Permissions: ssh.Permissions{
|
|
|
|
Extensions: map[string]string{"foo": "bar"},
|
|
|
|
},
|
|
|
|
SignatureKey: sshPub,
|
|
|
|
Signature: &ssh.Signature{},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ok/hostCert",
|
|
|
|
&ssh.Certificate{
|
|
|
|
Nonce: []byte("foo"),
|
|
|
|
Key: sshPub,
|
|
|
|
Serial: 1,
|
|
|
|
CertType: 2,
|
|
|
|
KeyId: "foo",
|
|
|
|
ValidPrincipals: []string{"foo"},
|
|
|
|
ValidAfter: uint64(time.Now().Unix()),
|
|
|
|
ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
|
2020-08-04 01:10:29 +00:00
|
|
|
SignatureKey: sshPub,
|
|
|
|
Signature: &ssh.Signature{},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ok/emptyPrincipals",
|
|
|
|
&ssh.Certificate{
|
|
|
|
Nonce: []byte("foo"),
|
|
|
|
Key: sshPub,
|
|
|
|
Serial: 1,
|
|
|
|
CertType: 1,
|
|
|
|
KeyId: "foo",
|
|
|
|
ValidPrincipals: []string{},
|
|
|
|
ValidAfter: uint64(time.Now().Unix()),
|
|
|
|
ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
|
|
|
|
SignatureKey: sshPub,
|
|
|
|
Signature: &ssh.Signature{},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ok/empty-extensions",
|
|
|
|
&ssh.Certificate{
|
|
|
|
Nonce: []byte("foo"),
|
|
|
|
Key: sshPub,
|
|
|
|
Serial: 1,
|
|
|
|
CertType: 1,
|
|
|
|
KeyId: "foo",
|
|
|
|
ValidPrincipals: []string{},
|
|
|
|
ValidAfter: uint64(time.Now().Unix()),
|
|
|
|
ValidBefore: uint64(time.Now().Add(10 * time.Minute).Unix()),
|
2019-09-12 05:57:55 +00:00
|
|
|
SignatureKey: sshPub,
|
|
|
|
Signature: &ssh.Signature{},
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-07-23 01:24:45 +00:00
|
|
|
if err := v.Valid(tt.cert, SignSSHOptions{}); err != nil {
|
2019-09-12 05:57:55 +00:00
|
|
|
if assert.NotNil(t, tt.err) {
|
|
|
|
assert.HasPrefix(t, err.Error(), tt.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tt.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 01:31:09 +00:00
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
func Test_sshCertValidityValidator(t *testing.T) {
|
2019-09-05 01:31:09 +00:00
|
|
|
p, err := generateX5C(nil)
|
|
|
|
assert.FatalError(t, err)
|
2020-01-24 06:04:34 +00:00
|
|
|
v := sshCertValidityValidator{p.claimer}
|
2019-09-05 01:31:09 +00:00
|
|
|
n := now()
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
cert *ssh.Certificate
|
2020-07-23 01:24:45 +00:00
|
|
|
opts SignSSHOptions
|
2019-09-05 01:31:09 +00:00
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"fail/validAfter-0",
|
|
|
|
&ssh.Certificate{CertType: ssh.UserCert},
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSHOptions{},
|
2019-09-05 01:31:09 +00:00
|
|
|
errors.New("ssh certificate validAfter cannot be 0"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/validBefore-in-past",
|
|
|
|
&ssh.Certificate{CertType: ssh.UserCert, ValidAfter: uint64(now().Unix()), ValidBefore: uint64(now().Add(-time.Minute).Unix())},
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSHOptions{},
|
2019-09-05 01:31:09 +00:00
|
|
|
errors.New("ssh certificate validBefore cannot be in the past"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/validBefore-before-validAfter",
|
|
|
|
&ssh.Certificate{CertType: ssh.UserCert, ValidAfter: uint64(now().Add(5 * time.Minute).Unix()), ValidBefore: uint64(now().Add(3 * time.Minute).Unix())},
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSHOptions{},
|
2019-09-05 01:31:09 +00:00
|
|
|
errors.New("ssh certificate validBefore cannot be before validAfter"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/cert-type-not-set",
|
|
|
|
&ssh.Certificate{ValidAfter: uint64(now().Unix()), ValidBefore: uint64(now().Add(10 * time.Minute).Unix())},
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSHOptions{},
|
2019-09-05 01:31:09 +00:00
|
|
|
errors.New("ssh certificate type has not been set"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/unexpected-cert-type",
|
|
|
|
&ssh.Certificate{
|
|
|
|
CertType: 3,
|
|
|
|
ValidAfter: uint64(now().Unix()),
|
|
|
|
ValidBefore: uint64(now().Add(10 * time.Minute).Unix()),
|
|
|
|
},
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSHOptions{},
|
2019-09-05 01:31:09 +00:00
|
|
|
errors.New("unknown ssh certificate type 3"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"fail/duration<min",
|
|
|
|
&ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
ValidAfter: uint64(n.Unix()),
|
|
|
|
ValidBefore: uint64(n.Add(4 * time.Minute).Unix()),
|
|
|
|
},
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSHOptions{Backdate: time.Second},
|
2019-09-05 01:31:09 +00:00
|
|
|
errors.New("requested duration of 4m0s is less than minimum accepted duration for selected provisioner of 5m0s"),
|
|
|
|
},
|
2020-01-24 21:42:00 +00:00
|
|
|
{
|
|
|
|
"ok/duration-exactly-min",
|
|
|
|
&ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
ValidAfter: uint64(n.Unix()),
|
|
|
|
ValidBefore: uint64(n.Add(5 * time.Minute).Unix()),
|
|
|
|
},
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSHOptions{Backdate: time.Second},
|
2020-01-24 21:42:00 +00:00
|
|
|
nil,
|
|
|
|
},
|
2019-09-05 01:31:09 +00:00
|
|
|
{
|
|
|
|
"fail/duration>max",
|
|
|
|
&ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
ValidAfter: uint64(n.Unix()),
|
|
|
|
ValidBefore: uint64(n.Add(48 * time.Hour).Unix()),
|
|
|
|
},
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSHOptions{Backdate: time.Second},
|
2020-01-24 21:42:00 +00:00
|
|
|
errors.New("requested duration of 48h0m0s is greater than maximum accepted duration for selected provisioner of 24h0m1s"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"ok/duration-exactly-max",
|
|
|
|
&ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
ValidAfter: uint64(n.Unix()),
|
|
|
|
ValidBefore: uint64(n.Add(24*time.Hour + time.Second).Unix()),
|
|
|
|
},
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSHOptions{Backdate: time.Second},
|
2020-01-24 21:42:00 +00:00
|
|
|
nil,
|
2019-09-05 01:31:09 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"ok",
|
|
|
|
&ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
ValidAfter: uint64(now().Unix()),
|
|
|
|
ValidBefore: uint64(now().Add(8 * time.Hour).Unix()),
|
|
|
|
},
|
2020-07-23 01:24:45 +00:00
|
|
|
SignSSHOptions{Backdate: time.Second},
|
2019-09-05 01:31:09 +00:00
|
|
|
nil,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-01-24 21:42:00 +00:00
|
|
|
if err := v.Valid(tt.cert, tt.opts); err != nil {
|
2019-09-05 01:31:09 +00:00
|
|
|
if assert.NotNil(t, tt.err) {
|
|
|
|
assert.HasPrefix(t, err.Error(), tt.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tt.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_sshValidityModifier(t *testing.T) {
|
2020-01-06 22:21:13 +00:00
|
|
|
n, fn := mockNow()
|
|
|
|
defer fn()
|
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
p, err := generateX5C(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
type test struct {
|
2020-01-04 02:22:02 +00:00
|
|
|
svm *sshLimitDuration
|
2019-09-05 01:31:09 +00:00
|
|
|
cert *ssh.Certificate
|
|
|
|
valid func(*ssh.Certificate)
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string]func() test{
|
|
|
|
"fail/type-not-set": func() test {
|
|
|
|
return test{
|
2020-01-04 02:22:02 +00:00
|
|
|
svm: &sshLimitDuration{Claimer: p.claimer, NotAfter: n.Add(6 * time.Hour)},
|
2019-09-05 01:31:09 +00:00
|
|
|
cert: &ssh.Certificate{
|
|
|
|
ValidAfter: uint64(n.Unix()),
|
|
|
|
ValidBefore: uint64(n.Add(8 * time.Hour).Unix()),
|
|
|
|
},
|
|
|
|
err: errors.New("ssh certificate type has not been set"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/type-not-recognized": func() test {
|
|
|
|
return test{
|
2020-01-04 02:22:02 +00:00
|
|
|
svm: &sshLimitDuration{Claimer: p.claimer, NotAfter: n.Add(6 * time.Hour)},
|
2019-09-05 01:31:09 +00:00
|
|
|
cert: &ssh.Certificate{
|
|
|
|
CertType: 4,
|
|
|
|
ValidAfter: uint64(n.Unix()),
|
|
|
|
ValidBefore: uint64(n.Add(8 * time.Hour).Unix()),
|
|
|
|
},
|
2020-01-04 02:22:02 +00:00
|
|
|
err: errors.New("ssh certificate has an unknown type: 4"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/requested-validAfter-after-limit": func() test {
|
|
|
|
return test{
|
2020-01-04 02:22:02 +00:00
|
|
|
svm: &sshLimitDuration{Claimer: p.claimer, NotAfter: n.Add(1 * time.Hour)},
|
2019-09-05 01:31:09 +00:00
|
|
|
cert: &ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
ValidAfter: uint64(n.Add(2 * time.Hour).Unix()),
|
|
|
|
ValidBefore: uint64(n.Add(8 * time.Hour).Unix()),
|
|
|
|
},
|
|
|
|
err: errors.Errorf("provisioning credential expiration ("),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/requested-validBefore-after-limit": func() test {
|
|
|
|
return test{
|
2020-01-04 02:22:02 +00:00
|
|
|
svm: &sshLimitDuration{Claimer: p.claimer, NotAfter: n.Add(1 * time.Hour)},
|
2019-09-05 01:31:09 +00:00
|
|
|
cert: &ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
ValidAfter: uint64(n.Unix()),
|
|
|
|
ValidBefore: uint64(n.Add(2 * time.Hour).Unix()),
|
|
|
|
},
|
|
|
|
err: errors.New("provisioning credential expiration ("),
|
|
|
|
}
|
|
|
|
},
|
2020-01-06 22:21:13 +00:00
|
|
|
"ok/no-limit": func() test {
|
|
|
|
va, vb := uint64(n.Unix()), uint64(n.Add(16*time.Hour).Unix())
|
|
|
|
return test{
|
|
|
|
svm: &sshLimitDuration{Claimer: p.claimer},
|
|
|
|
cert: &ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
},
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Equals(t, cert.ValidAfter, va)
|
|
|
|
assert.Equals(t, cert.ValidBefore, vb)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/defaults": func() test {
|
|
|
|
va, vb := uint64(n.Unix()), uint64(n.Add(16*time.Hour).Unix())
|
|
|
|
return test{
|
|
|
|
svm: &sshLimitDuration{Claimer: p.claimer},
|
|
|
|
cert: &ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
},
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Equals(t, cert.ValidAfter, va)
|
|
|
|
assert.Equals(t, cert.ValidBefore, vb)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2019-09-05 01:31:09 +00:00
|
|
|
"ok/valid-requested-validBefore": func() test {
|
|
|
|
va, vb := uint64(n.Unix()), uint64(n.Add(2*time.Hour).Unix())
|
|
|
|
return test{
|
2020-01-04 02:22:02 +00:00
|
|
|
svm: &sshLimitDuration{Claimer: p.claimer, NotAfter: n.Add(3 * time.Hour)},
|
2019-09-05 01:31:09 +00:00
|
|
|
cert: &ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
ValidAfter: va,
|
|
|
|
ValidBefore: vb,
|
|
|
|
},
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Equals(t, cert.ValidAfter, va)
|
|
|
|
assert.Equals(t, cert.ValidBefore, vb)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/empty-requested-validBefore-limit-after-default": func() test {
|
|
|
|
va := uint64(n.Unix())
|
|
|
|
return test{
|
2020-01-04 02:22:02 +00:00
|
|
|
svm: &sshLimitDuration{Claimer: p.claimer, NotAfter: n.Add(24 * time.Hour)},
|
2019-09-05 01:31:09 +00:00
|
|
|
cert: &ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
ValidAfter: va,
|
|
|
|
},
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Equals(t, cert.ValidAfter, va)
|
2020-01-04 02:22:02 +00:00
|
|
|
assert.Equals(t, cert.ValidBefore, uint64(n.Add(16*time.Hour).Unix()))
|
2019-09-05 01:31:09 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/empty-requested-validBefore-limit-before-default": func() test {
|
|
|
|
va := uint64(n.Unix())
|
|
|
|
return test{
|
2020-01-04 02:22:02 +00:00
|
|
|
svm: &sshLimitDuration{Claimer: p.claimer, NotAfter: n.Add(3 * time.Hour)},
|
2019-09-05 01:31:09 +00:00
|
|
|
cert: &ssh.Certificate{
|
|
|
|
CertType: 1,
|
|
|
|
ValidAfter: va,
|
|
|
|
},
|
|
|
|
valid: func(cert *ssh.Certificate) {
|
|
|
|
assert.Equals(t, cert.ValidAfter, va)
|
|
|
|
assert.Equals(t, cert.ValidBefore, uint64(n.Add(3*time.Hour).Unix()))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tt := run()
|
2020-07-31 01:44:52 +00:00
|
|
|
if err := tt.svm.Modify(tt.cert, SignSSHOptions{}); err != nil {
|
2019-09-05 01:31:09 +00:00
|
|
|
if assert.NotNil(t, tt.err) {
|
|
|
|
assert.HasPrefix(t, err.Error(), tt.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tt.err) {
|
|
|
|
tt.valid(tt.cert)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-04 02:22:02 +00:00
|
|
|
|
2020-01-06 22:21:13 +00:00
|
|
|
func Test_sshDefaultDuration_Option(t *testing.T) {
|
|
|
|
tm, fn := mockNow()
|
|
|
|
defer fn()
|
|
|
|
|
|
|
|
newClaimer := func(claims *Claims) *Claimer {
|
|
|
|
c, err := NewClaimer(claims, globalProvisionerClaims)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
unix := func(d time.Duration) uint64 {
|
|
|
|
return uint64(tm.Add(d).Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
Claimer *Claimer
|
|
|
|
}
|
|
|
|
type args struct {
|
2020-07-23 01:24:45 +00:00
|
|
|
o SignSSHOptions
|
2020-01-06 22:21:13 +00:00
|
|
|
cert *ssh.Certificate
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
want *ssh.Certificate
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2020-07-23 01:24:45 +00:00
|
|
|
{"user", fields{newClaimer(nil)}, args{SignSSHOptions{}, &ssh.Certificate{CertType: ssh.UserCert}},
|
2020-01-06 22:21:13 +00:00
|
|
|
&ssh.Certificate{CertType: ssh.UserCert, ValidAfter: unix(0), ValidBefore: unix(16 * time.Hour)}, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"host", fields{newClaimer(nil)}, args{SignSSHOptions{}, &ssh.Certificate{CertType: ssh.HostCert}},
|
2020-01-06 22:21:13 +00:00
|
|
|
&ssh.Certificate{CertType: ssh.HostCert, ValidAfter: unix(0), ValidBefore: unix(30 * 24 * time.Hour)}, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"user claim", fields{newClaimer(&Claims{DefaultUserSSHDur: &Duration{1 * time.Hour}})}, args{SignSSHOptions{}, &ssh.Certificate{CertType: ssh.UserCert}},
|
2020-01-06 22:21:13 +00:00
|
|
|
&ssh.Certificate{CertType: ssh.UserCert, ValidAfter: unix(0), ValidBefore: unix(1 * time.Hour)}, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"host claim", fields{newClaimer(&Claims{DefaultHostSSHDur: &Duration{1 * time.Hour}})}, args{SignSSHOptions{}, &ssh.Certificate{CertType: ssh.HostCert}},
|
2020-01-06 22:21:13 +00:00
|
|
|
&ssh.Certificate{CertType: ssh.HostCert, ValidAfter: unix(0), ValidBefore: unix(1 * time.Hour)}, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"user backdate", fields{newClaimer(nil)}, args{SignSSHOptions{Backdate: 1 * time.Minute}, &ssh.Certificate{CertType: ssh.UserCert}},
|
2020-01-06 22:21:13 +00:00
|
|
|
&ssh.Certificate{CertType: ssh.UserCert, ValidAfter: unix(-1 * time.Minute), ValidBefore: unix(16 * time.Hour)}, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"host backdate", fields{newClaimer(nil)}, args{SignSSHOptions{Backdate: 1 * time.Minute}, &ssh.Certificate{CertType: ssh.HostCert}},
|
2020-01-06 22:21:13 +00:00
|
|
|
&ssh.Certificate{CertType: ssh.HostCert, ValidAfter: unix(-1 * time.Minute), ValidBefore: unix(30 * 24 * time.Hour)}, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"user validAfter", fields{newClaimer(nil)}, args{SignSSHOptions{Backdate: 1 * time.Minute}, &ssh.Certificate{CertType: ssh.UserCert, ValidAfter: unix(1 * time.Hour)}},
|
2019-12-20 21:30:05 +00:00
|
|
|
&ssh.Certificate{CertType: ssh.UserCert, ValidAfter: unix(time.Hour), ValidBefore: unix(17 * time.Hour)}, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"user validBefore", fields{newClaimer(nil)}, args{SignSSHOptions{Backdate: 1 * time.Minute}, &ssh.Certificate{CertType: ssh.UserCert, ValidBefore: unix(1 * time.Hour)}},
|
2020-01-06 22:21:13 +00:00
|
|
|
&ssh.Certificate{CertType: ssh.UserCert, ValidAfter: unix(-1 * time.Minute), ValidBefore: unix(time.Hour)}, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"host validAfter validBefore", fields{newClaimer(nil)}, args{SignSSHOptions{Backdate: 1 * time.Minute}, &ssh.Certificate{CertType: ssh.HostCert, ValidAfter: unix(1 * time.Minute), ValidBefore: unix(2 * time.Minute)}},
|
2020-01-06 22:21:13 +00:00
|
|
|
&ssh.Certificate{CertType: ssh.HostCert, ValidAfter: unix(1 * time.Minute), ValidBefore: unix(2 * time.Minute)}, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"fail zero", fields{newClaimer(nil)}, args{SignSSHOptions{}, &ssh.Certificate{}}, &ssh.Certificate{}, true},
|
|
|
|
{"fail type", fields{newClaimer(nil)}, args{SignSSHOptions{}, &ssh.Certificate{CertType: 3}}, &ssh.Certificate{CertType: 3}, true},
|
2020-01-06 22:21:13 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
m := &sshDefaultDuration{
|
|
|
|
Claimer: tt.fields.Claimer,
|
|
|
|
}
|
2020-07-31 01:44:52 +00:00
|
|
|
if err := m.Modify(tt.args.cert, tt.args.o); (err != nil) != tt.wantErr {
|
2020-01-06 22:21:13 +00:00
|
|
|
t.Errorf("sshDefaultDuration.Option() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(tt.args.cert, tt.want) {
|
|
|
|
t.Errorf("sshDefaultDuration.Option() = %v, want %v", tt.args.cert, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|