2019-10-18 20:52:28 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/x509"
|
2019-12-20 21:30:05 +00:00
|
|
|
"net/http"
|
2019-10-18 20:52:28 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/smallstep/assert"
|
2019-12-20 21:30:05 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2019-10-18 20:52:28 +00:00
|
|
|
"github.com/smallstep/cli/jose"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestK8sSA_Getters(t *testing.T) {
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
id := "k8ssa/" + p.Name
|
|
|
|
if got := p.GetID(); got != id {
|
|
|
|
t.Errorf("K8sSA.GetID() = %v, want %v", got, id)
|
|
|
|
}
|
|
|
|
if got := p.GetName(); got != p.Name {
|
|
|
|
t.Errorf("K8sSA.GetName() = %v, want %v", got, p.Name)
|
|
|
|
}
|
|
|
|
if got := p.GetType(); got != TypeK8sSA {
|
|
|
|
t.Errorf("K8sSA.GetType() = %v, want %v", got, TypeK8sSA)
|
|
|
|
}
|
|
|
|
kid, key, ok := p.GetEncryptedKey()
|
|
|
|
if kid != "" || key != "" || ok == true {
|
|
|
|
t.Errorf("K8sSA.GetEncryptedKey() = (%v, %v, %v), want (%v, %v, %v)",
|
|
|
|
kid, key, ok, "", "", false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestK8sSA_authorizeToken(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
p *K8sSA
|
|
|
|
token string
|
|
|
|
err error
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
|
|
|
"fail/bad-token": func(t *testing.T) test {
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("k8ssa.authorizeToken; error parsing k8sSA token"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/not-implemented": func(t *testing.T) test {
|
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateToken("", p.Name, testAudiences.Sign[0], "",
|
|
|
|
[]string{"test.smallstep.com"}, time.Now(), jwk)
|
|
|
|
p.pubKeys = nil
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
err: errors.New("k8ssa.authorizeToken; k8sSA TokenReview API integration not implemented"),
|
|
|
|
code: http.StatusUnauthorized,
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/error-validating-token": func(t *testing.T) test {
|
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateToken("", p.Name, testAudiences.Sign[0], "",
|
|
|
|
[]string{"test.smallstep.com"}, time.Now(), jwk)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
2019-12-20 21:30:05 +00:00
|
|
|
err: errors.New("k8ssa.authorizeToken; error validating k8sSA token and extracting claims"),
|
|
|
|
code: http.StatusUnauthorized,
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/invalid-issuer": func(t *testing.T) test {
|
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p, err := generateK8sSA(jwk.Public().Key)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
claims := getK8sSAPayload()
|
|
|
|
claims.Claims.Issuer = "invalid"
|
|
|
|
tok, err := generateK8sSAToken(jwk, claims)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("k8ssa.authorizeToken; invalid k8sSA token claims: square/go-jose/jwt: validation failed, invalid issuer claim (iss)"),
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p, err := generateK8sSA(jwk.Public().Key)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateK8sSAToken(jwk, nil)
|
|
|
|
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 claims, err := tc.p.authorizeToken(tc.token, testAudiences.Sign); err != nil {
|
|
|
|
if assert.NotNil(t, tc.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(), tc.code)
|
2019-10-18 20:52:28 +00:00
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tc.err) {
|
|
|
|
assert.NotNil(t, claims)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
func TestK8sSA_AuthorizeRevoke(t *testing.T) {
|
2019-10-18 20:52:28 +00:00
|
|
|
type test struct {
|
|
|
|
p *K8sSA
|
|
|
|
token string
|
|
|
|
err error
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
|
|
|
"fail/invalid-token": func(t *testing.T) test {
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("k8ssa.AuthorizeRevoke: k8ssa.authorizeToken; error parsing k8sSA token"),
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
},
|
2019-12-20 21:30:05 +00:00
|
|
|
"ok": func(t *testing.T) test {
|
2019-10-18 20:52:28 +00:00
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p, err := generateK8sSA(jwk.Public().Key)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateK8sSAToken(jwk, nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := tt(t)
|
|
|
|
if err := tc.p.AuthorizeRevoke(context.Background(), tc.token); err != nil {
|
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tc.code)
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestK8sSA_AuthorizeRenew(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
p *K8sSA
|
|
|
|
cert *x509.Certificate
|
|
|
|
err error
|
|
|
|
code int
|
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
|
|
|
"fail/renew-disabled": func(t *testing.T) test {
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
// disable renewal
|
|
|
|
disable := true
|
|
|
|
p.Claims = &Claims{DisableRenewal: &disable}
|
|
|
|
p.claimer, err = NewClaimer(p.Claims, globalProvisionerClaims)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
cert: &x509.Certificate{},
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.Errorf("k8ssa.AuthorizeRenew; renew is disabled for k8sSA provisioner %s", p.GetID()),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
cert: &x509.Certificate{},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := tt(t)
|
|
|
|
if err := tc.p.AuthorizeRenew(context.Background(), tc.cert); err != nil {
|
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tc.code)
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestK8sSA_AuthorizeSign(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
p *K8sSA
|
|
|
|
token string
|
|
|
|
code int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
|
|
|
"fail/invalid-token": func(t *testing.T) test {
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("k8ssa.AuthorizeSign: k8ssa.authorizeToken; error parsing k8sSA token"),
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p, err := generateK8sSA(jwk.Public().Key)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateK8sSAToken(jwk, nil)
|
|
|
|
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)
|
2019-12-20 21:30:05 +00:00
|
|
|
if opts, err := tc.p.AuthorizeSign(context.Background(), tc.token); err != nil {
|
2019-10-18 20:52:28 +00:00
|
|
|
if assert.NotNil(t, tc.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(), tc.code)
|
2019-10-18 20:52:28 +00:00
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tc.err) {
|
|
|
|
if assert.NotNil(t, opts) {
|
|
|
|
tot := 0
|
|
|
|
for _, o := range opts {
|
|
|
|
switch v := o.(type) {
|
|
|
|
case *provisionerExtensionOption:
|
|
|
|
assert.Equals(t, v.Type, int(TypeK8sSA))
|
|
|
|
assert.Equals(t, v.Name, tc.p.GetName())
|
|
|
|
assert.Equals(t, v.CredentialID, "")
|
|
|
|
assert.Len(t, 0, v.KeyValuePairs)
|
|
|
|
case profileDefaultDuration:
|
|
|
|
assert.Equals(t, time.Duration(v), tc.p.claimer.DefaultTLSCertDuration())
|
|
|
|
case defaultPublicKeyValidator:
|
|
|
|
case *validityValidator:
|
|
|
|
assert.Equals(t, v.min, tc.p.claimer.MinTLSCertDuration())
|
|
|
|
assert.Equals(t, v.max, tc.p.claimer.MaxTLSCertDuration())
|
|
|
|
default:
|
|
|
|
assert.FatalError(t, errors.Errorf("unexpected sign option of type %T", v))
|
|
|
|
}
|
|
|
|
tot++
|
|
|
|
}
|
|
|
|
assert.Equals(t, tot, 4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
func TestK8sSA_AuthorizeSSHSign(t *testing.T) {
|
2019-10-18 20:52:28 +00:00
|
|
|
type test struct {
|
|
|
|
p *K8sSA
|
|
|
|
token string
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-10-18 20:52:28 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
2019-12-20 21:30:05 +00:00
|
|
|
"fail/sshCA-disabled": func(t *testing.T) test {
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
// disable sshCA
|
|
|
|
disable := false
|
|
|
|
p.Claims = &Claims{EnableSSHCA: &disable}
|
|
|
|
p.claimer, err = NewClaimer(p.Claims, globalProvisionerClaims)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.Errorf("k8ssa.AuthorizeSSHSign; sshCA is disabled for k8sSA provisioner %s", p.GetID()),
|
|
|
|
}
|
|
|
|
},
|
2019-10-18 20:52:28 +00:00
|
|
|
"fail/invalid-token": func(t *testing.T) test {
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("k8ssa.AuthorizeSSHSign: k8ssa.authorizeToken; error parsing k8sSA token"),
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p, err := generateK8sSA(jwk.Public().Key)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateK8sSAToken(jwk, nil)
|
|
|
|
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)
|
2019-12-20 21:30:05 +00:00
|
|
|
if opts, err := tc.p.AuthorizeSSHSign(context.Background(), tc.token); err != nil {
|
2019-10-18 20:52:28 +00:00
|
|
|
if assert.NotNil(t, tc.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(), tc.code)
|
2019-10-18 20:52:28 +00:00
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
2019-12-20 21:30:05 +00:00
|
|
|
if assert.Nil(t, tc.err) {
|
|
|
|
if assert.NotNil(t, opts) {
|
|
|
|
tot := 0
|
|
|
|
for _, o := range opts {
|
|
|
|
switch v := o.(type) {
|
|
|
|
case sshCertDefaultsModifier:
|
|
|
|
assert.Equals(t, v.CertType, SSHUserCert)
|
|
|
|
case *sshDefaultExtensionModifier:
|
2020-01-24 06:04:34 +00:00
|
|
|
case *sshCertValidityValidator:
|
2019-12-20 21:30:05 +00:00
|
|
|
assert.Equals(t, v.Claimer, tc.p.claimer)
|
|
|
|
case *sshDefaultPublicKeyValidator:
|
2020-01-24 06:04:34 +00:00
|
|
|
case *sshCertDefaultValidator:
|
2019-12-20 21:30:05 +00:00
|
|
|
case *sshDefaultDuration:
|
|
|
|
assert.Equals(t, v.Claimer, tc.p.claimer)
|
|
|
|
default:
|
|
|
|
assert.FatalError(t, errors.Errorf("unexpected sign option of type %T", v))
|
|
|
|
}
|
|
|
|
tot++
|
|
|
|
}
|
|
|
|
assert.Equals(t, tot, 6)
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|