2019-03-06 23:01:51 +00:00
|
|
|
package provisioner
|
2018-10-12 06:03:00 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-30 01:24:34 +00:00
|
|
|
"context"
|
2019-07-31 01:23:54 +00:00
|
|
|
"crypto"
|
2019-09-11 00:04:13 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2019-03-09 01:24:58 +00:00
|
|
|
"crypto/x509"
|
2019-12-20 21:30:05 +00:00
|
|
|
"net/http"
|
2019-03-09 01:24:58 +00:00
|
|
|
"strings"
|
2018-10-12 06:03:00 +00:00
|
|
|
"testing"
|
2019-03-08 03:30:17 +00:00
|
|
|
"time"
|
2018-10-12 06:03:00 +00:00
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-10-12 06:03:00 +00:00
|
|
|
"github.com/smallstep/assert"
|
2019-12-20 21:30:05 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2020-08-24 21:44:11 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2018-10-12 06:03:00 +00:00
|
|
|
)
|
|
|
|
|
2019-03-09 01:24:58 +00:00
|
|
|
func TestJWK_Getters(t *testing.T) {
|
|
|
|
p, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
if got := p.GetID(); got != p.Name+":"+p.Key.KeyID {
|
|
|
|
t.Errorf("JWK.GetID() = %v, want %v:%v", got, p.Name, p.Key.KeyID)
|
|
|
|
}
|
|
|
|
if got := p.GetName(); got != p.Name {
|
|
|
|
t.Errorf("JWK.GetName() = %v, want %v", got, p.Name)
|
|
|
|
}
|
|
|
|
if got := p.GetType(); got != TypeJWK {
|
|
|
|
t.Errorf("JWK.GetType() = %v, want %v", got, TypeJWK)
|
|
|
|
}
|
|
|
|
kid, key, ok := p.GetEncryptedKey()
|
|
|
|
if kid != p.Key.KeyID || key != p.EncryptedKey || ok == false {
|
|
|
|
t.Errorf("JWK.GetEncryptedKey() = (%v, %v, %v), want (%v, %v, %v)",
|
|
|
|
kid, key, ok, p.Key.KeyID, p.EncryptedKey, true)
|
|
|
|
}
|
|
|
|
p.EncryptedKey = ""
|
|
|
|
kid, key, ok = p.GetEncryptedKey()
|
|
|
|
if kid != p.Key.KeyID || key != "" || ok == true {
|
|
|
|
t.Errorf("JWK.GetEncryptedKey() = (%v, %v, %v), want (%v, %v, %v)",
|
|
|
|
kid, key, ok, p.Key.KeyID, "", false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJWK_Init(t *testing.T) {
|
2018-10-12 06:03:00 +00:00
|
|
|
type ProvisionerValidateTest struct {
|
2019-03-07 20:15:18 +00:00
|
|
|
p *JWK
|
2018-10-12 06:03:00 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) ProvisionerValidateTest{
|
2019-03-13 23:46:12 +00:00
|
|
|
"fail-empty": func(t *testing.T) ProvisionerValidateTest {
|
2018-10-12 06:03:00 +00:00
|
|
|
return ProvisionerValidateTest{
|
2019-03-07 20:15:18 +00:00
|
|
|
p: &JWK{},
|
2019-03-13 23:46:12 +00:00
|
|
|
err: errors.New("provisioner type cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail-empty-name": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
return ProvisionerValidateTest{
|
|
|
|
p: &JWK{
|
|
|
|
Type: "JWK",
|
|
|
|
},
|
2018-10-30 22:25:52 +00:00
|
|
|
err: errors.New("provisioner name cannot be empty"),
|
2018-10-12 06:03:00 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail-empty-type": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
return ProvisionerValidateTest{
|
2019-03-07 20:15:18 +00:00
|
|
|
p: &JWK{Name: "foo"},
|
2018-10-12 06:03:00 +00:00
|
|
|
err: errors.New("provisioner type cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail-empty-key": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
return ProvisionerValidateTest{
|
2019-03-07 20:15:18 +00:00
|
|
|
p: &JWK{Name: "foo", Type: "bar"},
|
2018-10-12 06:03:00 +00:00
|
|
|
err: errors.New("provisioner key cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
2019-03-19 22:27:41 +00:00
|
|
|
"fail-bad-claims": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
return ProvisionerValidateTest{
|
|
|
|
p: &JWK{Name: "foo", Type: "bar", Key: &jose.JSONWebKey{}, audiences: testAudiences, Claims: &Claims{DefaultTLSDur: &Duration{0}}},
|
|
|
|
err: errors.New("claims: DefaultTLSCertDuration must be greater than 0"),
|
|
|
|
}
|
|
|
|
},
|
2018-10-12 06:03:00 +00:00
|
|
|
"ok": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
return ProvisionerValidateTest{
|
2019-03-09 01:24:58 +00:00
|
|
|
p: &JWK{Name: "foo", Type: "bar", Key: &jose.JSONWebKey{}, audiences: testAudiences},
|
2018-10-12 06:03:00 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-03-08 03:30:17 +00:00
|
|
|
config := Config{
|
2019-03-09 01:24:58 +00:00
|
|
|
Claims: globalProvisionerClaims,
|
|
|
|
Audiences: testAudiences,
|
2019-03-08 03:30:17 +00:00
|
|
|
}
|
2018-10-12 06:03:00 +00:00
|
|
|
for name, get := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := get(t)
|
2019-03-08 03:30:17 +00:00
|
|
|
err := tc.p.Init(config)
|
2018-10-12 06:03:00 +00:00
|
|
|
if err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.Equals(t, tc.err.Error(), err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 01:24:58 +00:00
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
func TestJWK_authorizeToken(t *testing.T) {
|
2019-03-09 01:24:58 +00:00
|
|
|
p1, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p2, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
key1, err := decryptJSONWebKey(p1.EncryptedKey)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
key2, err := decryptJSONWebKey(p2.EncryptedKey)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
t1, err := generateSimpleToken(p1.Name, testAudiences.Sign[0], key1)
|
2019-03-09 01:24:58 +00:00
|
|
|
assert.FatalError(t, err)
|
2019-03-05 08:07:13 +00:00
|
|
|
t2, err := generateSimpleToken(p2.Name, testAudiences.Sign[1], key2)
|
2019-03-09 01:24:58 +00:00
|
|
|
assert.FatalError(t, err)
|
2019-03-05 08:07:13 +00:00
|
|
|
t3, err := generateToken("test.smallstep.com", p1.Name, testAudiences.Sign[0], "", []string{}, time.Now(), key1)
|
2019-03-09 01:24:58 +00:00
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
// Invalid tokens
|
|
|
|
parts := strings.Split(t1, ".")
|
2019-03-11 19:48:46 +00:00
|
|
|
key3, err := generateJSONWebKey()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
// missing key
|
2019-03-05 08:07:13 +00:00
|
|
|
failKey, err := generateSimpleToken(p1.Name, testAudiences.Sign[0], key3)
|
2019-03-11 19:48:46 +00:00
|
|
|
assert.FatalError(t, err)
|
2019-03-09 01:24:58 +00:00
|
|
|
// invalid token
|
|
|
|
failTok := "foo." + parts[1] + "." + parts[2]
|
|
|
|
// invalid claims
|
|
|
|
failClaims := parts[0] + ".foo." + parts[1]
|
|
|
|
// invalid issuer
|
2019-03-05 08:07:13 +00:00
|
|
|
failIss, err := generateSimpleToken("foobar", testAudiences.Sign[0], key1)
|
2019-03-09 01:24:58 +00:00
|
|
|
assert.FatalError(t, err)
|
|
|
|
// invalid audience
|
|
|
|
failAud, err := generateSimpleToken(p1.Name, "foobar", key1)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
// invalid signature
|
|
|
|
failSig := t1[0 : len(t1)-2]
|
|
|
|
// no subject
|
2019-03-05 08:07:13 +00:00
|
|
|
failSub, err := generateToken("", p1.Name, testAudiences.Sign[0], "", []string{"test.smallstep.com"}, time.Now(), key1)
|
2019-03-11 19:48:46 +00:00
|
|
|
assert.FatalError(t, err)
|
|
|
|
// expired
|
2019-03-05 08:07:13 +00:00
|
|
|
failExp, err := generateToken("subject", p1.Name, testAudiences.Sign[0], "", []string{"test.smallstep.com"}, time.Now().Add(-360*time.Second), key1)
|
2019-03-11 19:48:46 +00:00
|
|
|
assert.FatalError(t, err)
|
|
|
|
// not before
|
2019-03-05 08:07:13 +00:00
|
|
|
failNbf, err := generateToken("subject", p1.Name, testAudiences.Sign[0], "", []string{"test.smallstep.com"}, time.Now().Add(360*time.Second), key1)
|
2019-03-09 01:24:58 +00:00
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
// Remove encrypted key for p2
|
|
|
|
p2.EncryptedKey = ""
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
token string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
2019-03-05 08:07:13 +00:00
|
|
|
name string
|
|
|
|
prov *JWK
|
|
|
|
args args
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-03-05 08:07:13 +00:00
|
|
|
err error
|
2019-03-09 01:24:58 +00:00
|
|
|
}{
|
2019-12-20 21:30:05 +00:00
|
|
|
{"fail-token", p1, args{failTok}, http.StatusUnauthorized, errors.New("jwk.authorizeToken; error parsing jwk token")},
|
|
|
|
{"fail-key", p1, args{failKey}, http.StatusUnauthorized, errors.New("jwk.authorizeToken; error parsing jwk claims")},
|
|
|
|
{"fail-claims", p1, args{failClaims}, http.StatusUnauthorized, errors.New("jwk.authorizeToken; error parsing jwk claims")},
|
|
|
|
{"fail-signature", p1, args{failSig}, http.StatusUnauthorized, errors.New("jwk.authorizeToken; error parsing jwk claims: square/go-jose: error in cryptographic primitive")},
|
|
|
|
{"fail-issuer", p1, args{failIss}, http.StatusUnauthorized, errors.New("jwk.authorizeToken; invalid jwk claims: square/go-jose/jwt: validation failed, invalid issuer claim (iss)")},
|
|
|
|
{"fail-expired", p1, args{failExp}, http.StatusUnauthorized, errors.New("jwk.authorizeToken; invalid jwk claims: square/go-jose/jwt: validation failed, token is expired (exp)")},
|
|
|
|
{"fail-not-before", p1, args{failNbf}, http.StatusUnauthorized, errors.New("jwk.authorizeToken; invalid jwk claims: square/go-jose/jwt: validation failed, token not valid yet (nbf)")},
|
|
|
|
{"fail-audience", p1, args{failAud}, http.StatusUnauthorized, errors.New("jwk.authorizeToken; invalid jwk token audience claim (aud)")},
|
|
|
|
{"fail-subject", p1, args{failSub}, http.StatusUnauthorized, errors.New("jwk.authorizeToken; jwk token subject cannot be empty")},
|
|
|
|
{"ok", p1, args{t1}, http.StatusOK, nil},
|
|
|
|
{"ok-no-encrypted-key", p2, args{t2}, http.StatusOK, nil},
|
|
|
|
{"ok-no-sans", p1, args{t3}, http.StatusOK, nil},
|
2019-03-09 01:24:58 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-03-05 08:07:13 +00:00
|
|
|
if got, err := tt.prov.authorizeToken(tt.args.token, testAudiences.Sign); err != nil {
|
|
|
|
if assert.NotNil(t, tt.err) {
|
2019-12-20 21:30:05 +00:00
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tt.code)
|
2019-03-05 08:07:13 +00:00
|
|
|
assert.HasPrefix(t, err.Error(), tt.err.Error())
|
|
|
|
}
|
2019-03-09 01:24:58 +00:00
|
|
|
} else {
|
2019-03-05 08:07:13 +00:00
|
|
|
assert.Nil(t, tt.err)
|
2019-03-09 01:24:58 +00:00
|
|
|
assert.NotNil(t, got)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
func TestJWK_AuthorizeRevoke(t *testing.T) {
|
2019-03-09 01:24:58 +00:00
|
|
|
p1, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
2019-03-05 08:07:13 +00:00
|
|
|
key1, err := decryptJSONWebKey(p1.EncryptedKey)
|
2019-03-09 01:24:58 +00:00
|
|
|
assert.FatalError(t, err)
|
2019-03-05 08:07:13 +00:00
|
|
|
t1, err := generateSimpleToken(p1.Name, testAudiences.Revoke[0], key1)
|
2019-03-19 22:10:52 +00:00
|
|
|
assert.FatalError(t, err)
|
2019-03-05 08:07:13 +00:00
|
|
|
// invalid signature
|
|
|
|
failSig := t1[0 : len(t1)-2]
|
2019-03-09 01:24:58 +00:00
|
|
|
|
|
|
|
type args struct {
|
2019-03-05 08:07:13 +00:00
|
|
|
token string
|
2019-03-09 01:24:58 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
2019-03-05 08:07:13 +00:00
|
|
|
name string
|
|
|
|
prov *JWK
|
|
|
|
args args
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-03-05 08:07:13 +00:00
|
|
|
err error
|
2019-03-09 01:24:58 +00:00
|
|
|
}{
|
2019-12-20 21:30:05 +00:00
|
|
|
{"fail-signature", p1, args{failSig}, http.StatusUnauthorized, errors.New("jwk.AuthorizeRevoke: jwk.authorizeToken; error parsing jwk claims: square/go-jose: error in cryptographic primitive")},
|
|
|
|
{"ok", p1, args{t1}, http.StatusOK, nil},
|
2019-03-09 01:24:58 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-01-24 06:04:34 +00:00
|
|
|
if err := tt.prov.AuthorizeRevoke(context.Background(), tt.args.token); err != nil {
|
2019-03-05 08:07:13 +00:00
|
|
|
if assert.NotNil(t, tt.err) {
|
2019-12-20 21:30:05 +00:00
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tt.code)
|
2019-03-05 08:07:13 +00:00
|
|
|
assert.HasPrefix(t, err.Error(), tt.err.Error())
|
|
|
|
}
|
2019-03-09 01:24:58 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
func TestJWK_AuthorizeSign(t *testing.T) {
|
2019-03-09 01:24:58 +00:00
|
|
|
p1, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
key1, err := decryptJSONWebKey(p1.EncryptedKey)
|
|
|
|
assert.FatalError(t, err)
|
2019-03-05 08:07:13 +00:00
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
t1, err := generateToken("subject", p1.Name, testAudiences.Sign[0], "name@smallstep.com", []string{"127.0.0.1", "max@smallstep.com", "foo"}, time.Now(), key1)
|
2019-03-05 08:07:13 +00:00
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
t2, err := generateToken("subject", p1.Name, testAudiences.Sign[0], "name@smallstep.com", []string{}, time.Now(), key1)
|
2019-03-09 01:24:58 +00:00
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
// invalid signature
|
|
|
|
failSig := t1[0 : len(t1)-2]
|
|
|
|
|
2019-03-09 01:24:58 +00:00
|
|
|
type args struct {
|
|
|
|
token string
|
|
|
|
}
|
2019-03-05 08:07:13 +00:00
|
|
|
tests := []struct {
|
2020-06-25 06:25:15 +00:00
|
|
|
name string
|
|
|
|
prov *JWK
|
|
|
|
args args
|
|
|
|
code int
|
|
|
|
err error
|
|
|
|
sans []string
|
2019-03-05 08:07:13 +00:00
|
|
|
}{
|
2020-06-25 06:25:15 +00:00
|
|
|
{
|
|
|
|
name: "fail-signature",
|
|
|
|
prov: p1,
|
|
|
|
args: args{failSig},
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("jwk.AuthorizeSign: jwk.authorizeToken; error parsing jwk claims: square/go-jose: error in cryptographic primitive"),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok-sans",
|
|
|
|
prov: p1,
|
|
|
|
args: args{t1},
|
|
|
|
code: http.StatusOK,
|
|
|
|
err: nil,
|
|
|
|
sans: []string{"127.0.0.1", "max@smallstep.com", "foo"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok-no-sans",
|
|
|
|
prov: p1,
|
|
|
|
args: args{t2},
|
|
|
|
code: http.StatusOK,
|
|
|
|
err: nil,
|
|
|
|
sans: []string{"subject"},
|
|
|
|
},
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-07-30 01:24:34 +00:00
|
|
|
ctx := NewContextWithMethod(context.Background(), SignMethod)
|
|
|
|
if got, err := tt.prov.AuthorizeSign(ctx, tt.args.token); err != nil {
|
2019-03-05 08:07:13 +00:00
|
|
|
if assert.NotNil(t, tt.err) {
|
2019-12-20 21:30:05 +00:00
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tt.code)
|
2019-03-05 08:07:13 +00:00
|
|
|
assert.HasPrefix(t, err.Error(), tt.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.NotNil(t, got) {
|
2020-07-21 02:01:43 +00:00
|
|
|
assert.Len(t, 7, got)
|
2019-09-05 01:31:09 +00:00
|
|
|
for _, o := range got {
|
|
|
|
switch v := o.(type) {
|
2020-07-21 02:01:43 +00:00
|
|
|
case certificateOptionsFunc:
|
2019-09-05 01:31:09 +00:00
|
|
|
case *provisionerExtensionOption:
|
|
|
|
assert.Equals(t, v.Type, int(TypeJWK))
|
|
|
|
assert.Equals(t, v.Name, tt.prov.GetName())
|
|
|
|
assert.Equals(t, v.CredentialID, tt.prov.Key.KeyID)
|
|
|
|
assert.Len(t, 0, v.KeyValuePairs)
|
|
|
|
case profileDefaultDuration:
|
|
|
|
assert.Equals(t, time.Duration(v), tt.prov.claimer.DefaultTLSCertDuration())
|
|
|
|
case commonNameValidator:
|
|
|
|
assert.Equals(t, string(v), "subject")
|
|
|
|
case defaultPublicKeyValidator:
|
|
|
|
case *validityValidator:
|
|
|
|
assert.Equals(t, v.min, tt.prov.claimer.MinTLSCertDuration())
|
|
|
|
assert.Equals(t, v.max, tt.prov.claimer.MaxTLSCertDuration())
|
2020-06-25 06:25:15 +00:00
|
|
|
case defaultSANsValidator:
|
|
|
|
assert.Equals(t, []string(v), tt.sans)
|
2019-09-05 01:31:09 +00:00
|
|
|
default:
|
|
|
|
assert.FatalError(t, errors.Errorf("unexpected sign option of type %T", v))
|
|
|
|
}
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:48:06 +00:00
|
|
|
func TestJWK_AuthorizeRenew(t *testing.T) {
|
2019-03-05 08:07:13 +00:00
|
|
|
p1, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p2, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
// disable renewal
|
|
|
|
disable := true
|
|
|
|
p2.Claims = &Claims{DisableRenewal: &disable}
|
|
|
|
p2.claimer, err = NewClaimer(p2.Claims, globalProvisionerClaims)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
cert *x509.Certificate
|
|
|
|
}
|
2019-03-09 01:24:58 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
prov *JWK
|
|
|
|
args args
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-03-09 01:24:58 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-12-20 21:30:05 +00:00
|
|
|
{"ok", p1, args{nil}, http.StatusOK, false},
|
|
|
|
{"fail/renew-disabled", p2, args{nil}, http.StatusUnauthorized, true},
|
2019-03-09 01:24:58 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-01-24 06:04:34 +00:00
|
|
|
if err := tt.prov.AuthorizeRenew(context.Background(), tt.args.cert); (err != nil) != tt.wantErr {
|
2019-11-14 18:48:06 +00:00
|
|
|
t.Errorf("JWK.AuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
|
2019-12-20 21:30:05 +00:00
|
|
|
} else if err != nil {
|
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tt.code)
|
2019-03-09 01:24:58 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-07-31 01:23:54 +00:00
|
|
|
|
2019-11-14 23:26:37 +00:00
|
|
|
func TestJWK_AuthorizeSSHSign(t *testing.T) {
|
2019-08-01 00:04:17 +00:00
|
|
|
tm, fn := mockNow()
|
|
|
|
defer fn()
|
|
|
|
|
2019-07-31 01:23:54 +00:00
|
|
|
p1, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
p2, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
// disable sshCA
|
|
|
|
disable := false
|
|
|
|
p2.Claims = &Claims{EnableSSHCA: &disable}
|
|
|
|
p2.claimer, err = NewClaimer(p2.Claims, globalProvisionerClaims)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-07-31 01:23:54 +00:00
|
|
|
jwk, err := decryptJSONWebKey(p1.EncryptedKey)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-11-14 23:26:37 +00:00
|
|
|
iss, aud := p1.Name, testAudiences.SSHSign[0]
|
2019-07-31 01:23:54 +00:00
|
|
|
|
|
|
|
t1, err := generateSimpleSSHUserToken(iss, aud, jwk)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
t2, err := generateSimpleSSHHostToken(iss, aud, jwk)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
// invalid signature
|
|
|
|
failSig := t1[0 : len(t1)-2]
|
|
|
|
|
|
|
|
key, err := generateJSONWebKey()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
signer, err := generateJSONWebKey()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-09-11 00:04:13 +00:00
|
|
|
pub := key.Public().Key
|
|
|
|
rsa2048, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
rsa1024, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-08-01 00:04:17 +00:00
|
|
|
userDuration := p1.claimer.DefaultUserSSHCertDuration()
|
|
|
|
hostDuration := p1.claimer.DefaultHostSSHCertDuration()
|
2020-07-23 01:24:45 +00:00
|
|
|
expectedUserOptions := &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
CertType: "user", Principals: []string{"name"},
|
|
|
|
ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration)),
|
2019-07-31 01:23:54 +00:00
|
|
|
}
|
2020-07-23 01:24:45 +00:00
|
|
|
expectedHostOptions := &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
CertType: "host", Principals: []string{"smallstep.com"},
|
|
|
|
ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(hostDuration)),
|
|
|
|
}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
token string
|
2020-07-23 01:24:45 +00:00
|
|
|
sshOpts SignSSHOptions
|
2019-09-11 00:04:13 +00:00
|
|
|
key interface{}
|
2019-07-31 01:23:54 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
2019-08-01 00:04:17 +00:00
|
|
|
name string
|
|
|
|
prov *JWK
|
|
|
|
args args
|
2020-07-23 01:24:45 +00:00
|
|
|
expected *SignSSHOptions
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-08-01 00:04:17 +00:00
|
|
|
wantErr bool
|
|
|
|
wantSignErr bool
|
2019-07-31 01:23:54 +00:00
|
|
|
}{
|
2020-07-23 01:24:45 +00:00
|
|
|
{"user", p1, args{t1, SignSSHOptions{}, pub}, expectedUserOptions, http.StatusOK, false, false},
|
|
|
|
{"user-rsa2048", p1, args{t1, SignSSHOptions{}, rsa2048.Public()}, expectedUserOptions, http.StatusOK, false, false},
|
|
|
|
{"user-type", p1, args{t1, SignSSHOptions{CertType: "user"}, pub}, expectedUserOptions, http.StatusOK, false, false},
|
|
|
|
{"user-principals", p1, args{t1, SignSSHOptions{Principals: []string{"name"}}, pub}, expectedUserOptions, http.StatusOK, false, false},
|
|
|
|
{"user-options", p1, args{t1, SignSSHOptions{CertType: "user", Principals: []string{"name"}}, pub}, expectedUserOptions, http.StatusOK, false, false},
|
|
|
|
{"host", p1, args{t2, SignSSHOptions{}, pub}, expectedHostOptions, http.StatusOK, false, false},
|
|
|
|
{"host-type", p1, args{t2, SignSSHOptions{CertType: "host"}, pub}, expectedHostOptions, http.StatusOK, false, false},
|
|
|
|
{"host-principals", p1, args{t2, SignSSHOptions{Principals: []string{"smallstep.com"}}, pub}, expectedHostOptions, http.StatusOK, false, false},
|
|
|
|
{"host-options", p1, args{t2, SignSSHOptions{CertType: "host", Principals: []string{"smallstep.com"}}, pub}, expectedHostOptions, http.StatusOK, false, false},
|
|
|
|
{"fail-sshCA-disabled", p2, args{"foo", SignSSHOptions{}, pub}, expectedUserOptions, http.StatusUnauthorized, true, false},
|
|
|
|
{"fail-signature", p1, args{failSig, SignSSHOptions{}, pub}, nil, http.StatusUnauthorized, true, false},
|
|
|
|
{"rail-rsa1024", p1, args{t1, SignSSHOptions{}, rsa1024.Public()}, expectedUserOptions, http.StatusOK, false, true},
|
2019-07-31 01:23:54 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-20 21:30:05 +00:00
|
|
|
got, err := tt.prov.AuthorizeSSHSign(context.Background(), tt.args.token)
|
2019-08-01 00:04:17 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2019-11-14 23:26:37 +00:00
|
|
|
t.Errorf("JWK.AuthorizeSSHSign() error = %v, wantErr %v", err, tt.wantErr)
|
2019-08-01 00:04:17 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tt.code)
|
2019-08-01 00:04:17 +00:00
|
|
|
assert.Nil(t, got)
|
2019-07-31 01:23:54 +00:00
|
|
|
} else if assert.NotNil(t, got) {
|
2019-09-11 00:04:13 +00:00
|
|
|
cert, err := signSSHCertificate(tt.args.key, tt.args.sshOpts, got, signer.Key.(crypto.Signer))
|
2019-08-01 00:04:17 +00:00
|
|
|
if (err != nil) != tt.wantSignErr {
|
|
|
|
t.Errorf("SignSSH error = %v, wantSignErr %v", err, tt.wantSignErr)
|
2019-07-31 01:23:54 +00:00
|
|
|
} else {
|
2019-08-01 00:04:17 +00:00
|
|
|
if tt.wantSignErr {
|
|
|
|
assert.Nil(t, cert)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, validateSSHCertificate(cert, tt.expected))
|
|
|
|
}
|
2019-07-31 01:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestJWK_AuthorizeSign_SSHOptions(t *testing.T) {
|
2019-08-01 00:04:17 +00:00
|
|
|
tm, fn := mockNow()
|
|
|
|
defer fn()
|
|
|
|
|
2019-07-31 01:23:54 +00:00
|
|
|
p1, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
jwk, err := decryptJSONWebKey(p1.EncryptedKey)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-11-14 23:26:37 +00:00
|
|
|
sub, iss, aud, iat := "subject@smallstep.com", p1.Name, testAudiences.SSHSign[0], time.Now()
|
2019-07-31 01:23:54 +00:00
|
|
|
|
|
|
|
key, err := generateJSONWebKey()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
signer, err := generateJSONWebKey()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-08-01 00:04:17 +00:00
|
|
|
userDuration := p1.claimer.DefaultUserSSHCertDuration()
|
|
|
|
hostDuration := p1.claimer.DefaultHostSSHCertDuration()
|
2020-07-23 01:24:45 +00:00
|
|
|
expectedUserOptions := &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
CertType: "user", Principals: []string{"name"},
|
|
|
|
ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(userDuration)),
|
|
|
|
}
|
2020-07-23 01:24:45 +00:00
|
|
|
expectedHostOptions := &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
CertType: "host", Principals: []string{"smallstep.com"},
|
|
|
|
ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(hostDuration)),
|
|
|
|
}
|
2019-07-31 01:23:54 +00:00
|
|
|
type args struct {
|
|
|
|
sub, iss, aud string
|
|
|
|
iat time.Time
|
2020-07-23 01:24:45 +00:00
|
|
|
tokSSHOpts *SignSSHOptions
|
|
|
|
userSSHOpts *SignSSHOptions
|
2019-07-31 01:23:54 +00:00
|
|
|
jwk *jose.JSONWebKey
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
prov *JWK
|
|
|
|
args args
|
2020-07-23 01:24:45 +00:00
|
|
|
expected *SignSSHOptions
|
2019-07-31 01:23:54 +00:00
|
|
|
wantErr bool
|
|
|
|
wantSignErr bool
|
|
|
|
}{
|
2020-07-23 01:24:45 +00:00
|
|
|
{"ok-user", p1, args{sub, iss, aud, iat, &SignSSHOptions{CertType: "user", Principals: []string{"name"}}, &SignSSHOptions{}, jwk}, expectedUserOptions, false, false},
|
|
|
|
{"ok-host", p1, args{sub, iss, aud, iat, &SignSSHOptions{CertType: "host", Principals: []string{"smallstep.com"}}, &SignSSHOptions{}, jwk}, expectedHostOptions, false, false},
|
|
|
|
{"ok-user-validAfter", p1, args{sub, iss, aud, iat, &SignSSHOptions{
|
2019-07-31 01:23:54 +00:00
|
|
|
CertType: "user", Principals: []string{"name"},
|
2020-07-23 01:24:45 +00:00
|
|
|
}, &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
ValidAfter: NewTimeDuration(tm.Add(-time.Hour)),
|
2020-07-23 01:24:45 +00:00
|
|
|
}, jwk}, &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
CertType: "user", Principals: []string{"name"}, ValidAfter: NewTimeDuration(tm.Add(-time.Hour)), ValidBefore: NewTimeDuration(tm.Add(userDuration - time.Hour)),
|
|
|
|
}, false, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"ok-user-validBefore", p1, args{sub, iss, aud, iat, &SignSSHOptions{
|
2019-07-31 01:23:54 +00:00
|
|
|
CertType: "user", Principals: []string{"name"},
|
2020-07-23 01:24:45 +00:00
|
|
|
}, &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
ValidBefore: NewTimeDuration(tm.Add(time.Hour)),
|
2020-07-23 01:24:45 +00:00
|
|
|
}, jwk}, &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
CertType: "user", Principals: []string{"name"}, ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(time.Hour)),
|
|
|
|
}, false, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"ok-user-validAfter-validBefore", p1, args{sub, iss, aud, iat, &SignSSHOptions{
|
2019-07-31 01:23:54 +00:00
|
|
|
CertType: "user", Principals: []string{"name"},
|
2020-07-23 01:24:45 +00:00
|
|
|
}, &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
ValidAfter: NewTimeDuration(tm.Add(10 * time.Minute)), ValidBefore: NewTimeDuration(tm.Add(time.Hour)),
|
2020-07-23 01:24:45 +00:00
|
|
|
}, jwk}, &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
CertType: "user", Principals: []string{"name"}, ValidAfter: NewTimeDuration(tm.Add(10 * time.Minute)), ValidBefore: NewTimeDuration(tm.Add(time.Hour)),
|
|
|
|
}, false, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"ok-user-match", p1, args{sub, iss, aud, iat, &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
CertType: "user", Principals: []string{"name"}, ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(1 * time.Hour)),
|
2020-07-23 01:24:45 +00:00
|
|
|
}, &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
CertType: "user", Principals: []string{"name"}, ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(1 * time.Hour)),
|
2020-07-23 01:24:45 +00:00
|
|
|
}, jwk}, &SignSSHOptions{
|
2019-08-01 00:04:17 +00:00
|
|
|
CertType: "user", Principals: []string{"name"}, ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(time.Hour)),
|
|
|
|
}, false, false},
|
2020-07-23 01:24:45 +00:00
|
|
|
{"fail-certType", p1, args{sub, iss, aud, iat, &SignSSHOptions{CertType: "user", Principals: []string{"name"}}, &SignSSHOptions{CertType: "host"}, jwk}, nil, false, true},
|
|
|
|
{"fail-principals", p1, args{sub, iss, aud, iat, &SignSSHOptions{CertType: "user", Principals: []string{"name"}}, &SignSSHOptions{Principals: []string{"root"}}, jwk}, nil, false, true},
|
|
|
|
{"fail-validAfter", p1, args{sub, iss, aud, iat, &SignSSHOptions{CertType: "user", Principals: []string{"name"}, ValidAfter: NewTimeDuration(tm)}, &SignSSHOptions{ValidAfter: NewTimeDuration(tm.Add(time.Hour))}, jwk}, nil, false, true},
|
|
|
|
{"fail-validBefore", p1, args{sub, iss, aud, iat, &SignSSHOptions{CertType: "user", Principals: []string{"name"}, ValidBefore: NewTimeDuration(tm.Add(time.Hour))}, &SignSSHOptions{ValidBefore: NewTimeDuration(tm.Add(10 * time.Hour))}, jwk}, nil, false, true},
|
|
|
|
{"fail-subject", p1, args{"", iss, aud, iat, &SignSSHOptions{CertType: "user", Principals: []string{"name"}}, &SignSSHOptions{}, jwk}, nil, true, false},
|
|
|
|
{"fail-issuer", p1, args{sub, "invalid", aud, iat, &SignSSHOptions{CertType: "user", Principals: []string{"name"}}, &SignSSHOptions{}, jwk}, nil, true, false},
|
|
|
|
{"fail-audience", p1, args{sub, iss, "invalid", iat, &SignSSHOptions{CertType: "user", Principals: []string{"name"}}, &SignSSHOptions{}, jwk}, nil, true, false},
|
|
|
|
{"fail-expired", p1, args{sub, iss, aud, iat.Add(-6 * time.Minute), &SignSSHOptions{CertType: "user", Principals: []string{"name"}}, &SignSSHOptions{}, jwk}, nil, true, false},
|
|
|
|
{"fail-notBefore", p1, args{sub, iss, aud, iat.Add(5 * time.Minute), &SignSSHOptions{CertType: "user", Principals: []string{"name"}}, &SignSSHOptions{}, jwk}, nil, true, false},
|
2019-07-31 01:23:54 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
token, err := generateSSHToken(tt.args.sub, tt.args.iss, tt.args.aud, tt.args.iat, tt.args.tokSSHOpts, tt.args.jwk)
|
|
|
|
assert.FatalError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
if got, err := tt.prov.AuthorizeSSHSign(context.Background(), token); (err != nil) != tt.wantErr {
|
2019-11-14 23:26:37 +00:00
|
|
|
t.Errorf("JWK.AuthorizeSSHSign() error = %v, wantErr %v", err, tt.wantErr)
|
2019-07-31 01:23:54 +00:00
|
|
|
} else if !tt.wantErr && assert.NotNil(t, got) {
|
2020-07-23 01:24:45 +00:00
|
|
|
var opts SignSSHOptions
|
2019-07-31 01:23:54 +00:00
|
|
|
if tt.args.userSSHOpts != nil {
|
|
|
|
opts = *tt.args.userSSHOpts
|
|
|
|
}
|
2019-08-01 00:04:17 +00:00
|
|
|
cert, err := signSSHCertificate(key.Public().Key, opts, got, signer.Key.(crypto.Signer))
|
|
|
|
if (err != nil) != tt.wantSignErr {
|
2019-07-31 01:23:54 +00:00
|
|
|
t.Errorf("SignSSH error = %v, wantSignErr %v", err, tt.wantSignErr)
|
2019-08-01 00:04:17 +00:00
|
|
|
} else {
|
|
|
|
if tt.wantSignErr {
|
|
|
|
assert.Nil(t, cert)
|
2019-07-31 01:23:54 +00:00
|
|
|
} else {
|
2019-08-01 00:04:17 +00:00
|
|
|
assert.NoError(t, validateSSHCertificate(cert, tt.expected))
|
2019-07-31 01:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-12-20 21:30:05 +00:00
|
|
|
|
|
|
|
func TestJWK_AuthorizeSSHRevoke(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
p *JWK
|
|
|
|
token string
|
|
|
|
code int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
|
|
|
"fail/invalid-token": func(t *testing.T) test {
|
|
|
|
p, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("jwk.AuthorizeSSHRevoke: jwk.authorizeToken; error parsing jwk token"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
p, err := generateJWK()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
jwk, err := decryptJSONWebKey(p.EncryptedKey)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
tok, err := generateToken("subject", p.Name, testAudiences.SSHRevoke[0], "name@smallstep.com", []string{"127.0.0.1", "max@smallstep.com", "foo"}, time.Now(), jwk)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := tt(t)
|
|
|
|
if err := tc.p.AuthorizeSSHRevoke(context.Background(), tc.token); err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tc.code)
|
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|