2019-10-18 20:52:28 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/x509"
|
2022-03-30 08:22:22 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-12-20 21:30:05 +00:00
|
|
|
"net/http"
|
2019-10-18 20:52:28 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-08-24 21:44:11 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2022-03-30 08:22:22 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/assert"
|
|
|
|
"github.com/smallstep/certificates/api/render"
|
2019-10-18 20:52:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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) {
|
2022-09-22 07:04:31 +00:00
|
|
|
var sc render.StatusCodedError
|
|
|
|
assert.Fatal(t, errors.As(err, &sc), "error does not implement StatusCodedError interface")
|
2019-12-20 21:30:05 +00:00
|
|
|
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 {
|
2022-09-22 07:04:31 +00:00
|
|
|
var sc render.StatusCodedError
|
|
|
|
assert.Fatal(t, errors.As(err, &sc), "error does not implement StatusCodedError interface")
|
2019-12-20 21:30:05 +00:00
|
|
|
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) {
|
2022-03-10 18:46:28 +00:00
|
|
|
now := time.Now().Truncate(time.Second)
|
2019-12-20 21:30:05 +00:00
|
|
|
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}
|
2022-03-10 02:43:45 +00:00
|
|
|
p.ctl.Claimer, err = NewClaimer(p.Claims, globalProvisionerClaims)
|
2019-12-20 21:30:05 +00:00
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
2022-03-10 02:43:45 +00:00
|
|
|
p: p,
|
|
|
|
cert: &x509.Certificate{
|
|
|
|
NotBefore: now,
|
|
|
|
NotAfter: now.Add(time.Hour),
|
|
|
|
},
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
2022-03-30 08:22:22 +00:00
|
|
|
err: fmt.Errorf("renew is disabled for provisioner '%s'", p.GetName()),
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
p, err := generateK8sSA(nil)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
2022-03-10 02:43:45 +00:00
|
|
|
p: p,
|
|
|
|
cert: &x509.Certificate{
|
|
|
|
NotBefore: now,
|
|
|
|
NotAfter: now.Add(time.Hour),
|
|
|
|
},
|
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.AuthorizeRenew(context.Background(), tc.cert); err != nil {
|
2022-09-22 07:04:31 +00:00
|
|
|
var sc render.StatusCodedError
|
|
|
|
assert.Fatal(t, errors.As(err, &sc), "error does not implement StatusCodedError interface")
|
2019-12-20 21:30:05 +00:00
|
|
|
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) {
|
2022-09-22 07:04:31 +00:00
|
|
|
var sc render.StatusCodedError
|
|
|
|
assert.Fatal(t, errors.As(err, &sc), "error does not implement StatusCodedError interface")
|
2019-12-20 21:30:05 +00:00
|
|
|
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) {
|
|
|
|
for _, o := range opts {
|
|
|
|
switch v := o.(type) {
|
2022-03-28 22:06:56 +00:00
|
|
|
case *K8sSA:
|
2020-07-21 02:01:43 +00:00
|
|
|
case certificateOptionsFunc:
|
2019-10-18 20:52:28 +00:00
|
|
|
case *provisionerExtensionOption:
|
2022-03-11 22:59:42 +00:00
|
|
|
assert.Equals(t, v.Type, TypeK8sSA)
|
2019-10-18 20:52:28 +00:00
|
|
|
assert.Equals(t, v.Name, tc.p.GetName())
|
|
|
|
assert.Equals(t, v.CredentialID, "")
|
|
|
|
assert.Len(t, 0, v.KeyValuePairs)
|
|
|
|
case profileDefaultDuration:
|
2022-03-10 02:43:45 +00:00
|
|
|
assert.Equals(t, time.Duration(v), tc.p.ctl.Claimer.DefaultTLSCertDuration())
|
2019-10-18 20:52:28 +00:00
|
|
|
case defaultPublicKeyValidator:
|
|
|
|
case *validityValidator:
|
2022-03-10 02:43:45 +00:00
|
|
|
assert.Equals(t, v.min, tc.p.ctl.Claimer.MinTLSCertDuration())
|
|
|
|
assert.Equals(t, v.max, tc.p.ctl.Claimer.MaxTLSCertDuration())
|
2022-01-03 11:25:24 +00:00
|
|
|
case *x509NamePolicyValidator:
|
|
|
|
assert.Equals(t, nil, v.policyEngine)
|
2019-10-18 20:52:28 +00:00
|
|
|
default:
|
2022-03-30 08:22:22 +00:00
|
|
|
assert.FatalError(t, fmt.Errorf("unexpected sign option of type %T", v))
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-08 14:01:56 +00:00
|
|
|
assert.Equals(t, 7, len(opts))
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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}
|
2022-03-10 02:43:45 +00:00
|
|
|
p.ctl.Claimer, err = NewClaimer(p.Claims, globalProvisionerClaims)
|
2019-12-20 21:30:05 +00:00
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
|
|
|
code: http.StatusUnauthorized,
|
2022-03-30 08:22:22 +00:00
|
|
|
err: fmt.Errorf("k8ssa.AuthorizeSSHSign; sshCA is disabled for k8sSA provisioner '%s'", p.GetName()),
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
},
|
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) {
|
2022-09-22 07:04:31 +00:00
|
|
|
var sc render.StatusCodedError
|
|
|
|
assert.Fatal(t, errors.As(err, &sc), "error does not implement StatusCodedError interface")
|
2019-12-20 21:30:05 +00:00
|
|
|
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) {
|
2022-05-19 01:51:36 +00:00
|
|
|
assert.Len(t, 8, opts)
|
2019-12-20 21:30:05 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
switch v := o.(type) {
|
2022-05-19 01:51:36 +00:00
|
|
|
case Interface:
|
2020-08-04 01:10:29 +00:00
|
|
|
case sshCertificateOptionsFunc:
|
|
|
|
case *sshCertOptionsRequireValidator:
|
|
|
|
assert.Equals(t, v, &sshCertOptionsRequireValidator{CertType: true, KeyID: true, Principals: true})
|
2020-01-24 06:04:34 +00:00
|
|
|
case *sshCertValidityValidator:
|
2022-03-10 02:43:45 +00:00
|
|
|
assert.Equals(t, v.Claimer, tc.p.ctl.Claimer)
|
2019-12-20 21:30:05 +00:00
|
|
|
case *sshDefaultPublicKeyValidator:
|
2020-01-24 06:04:34 +00:00
|
|
|
case *sshCertDefaultValidator:
|
2019-12-20 21:30:05 +00:00
|
|
|
case *sshDefaultDuration:
|
2022-03-10 02:43:45 +00:00
|
|
|
assert.Equals(t, v.Claimer, tc.p.ctl.Claimer)
|
2022-01-03 11:25:24 +00:00
|
|
|
case *sshNamePolicyValidator:
|
2022-02-01 13:58:13 +00:00
|
|
|
assert.Equals(t, nil, v.userPolicyEngine)
|
|
|
|
assert.Equals(t, nil, v.hostPolicyEngine)
|
2019-12-20 21:30:05 +00:00
|
|
|
default:
|
2022-03-30 08:22:22 +00:00
|
|
|
assert.FatalError(t, fmt.Errorf("unexpected sign option of type %T", v))
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|