2019-09-05 01:31:09 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-10 02:43:45 +00:00
|
|
|
"crypto/x509"
|
2023-12-29 01:09:39 +00:00
|
|
|
"encoding/base64"
|
2022-03-30 08:22:22 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-12-20 21:30:05 +00:00
|
|
|
"net/http"
|
2024-07-24 19:13:57 +00:00
|
|
|
"strings"
|
2019-09-05 01:31:09 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-08-24 21:44:11 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2020-08-14 22:33:50 +00:00
|
|
|
"go.step.sm/crypto/pemutil"
|
2020-08-10 18:26:51 +00:00
|
|
|
"go.step.sm/crypto/randutil"
|
2023-07-20 20:03:45 +00:00
|
|
|
"go.step.sm/linkedca"
|
2022-03-30 08:22:22 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/certificates/api/render"
|
2024-07-24 19:13:57 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-09-05 01:31:09 +00:00
|
|
|
)
|
|
|
|
|
2024-07-24 19:13:57 +00:00
|
|
|
func assertHasPrefix(t *testing.T, s, p string) bool {
|
|
|
|
t.Helper()
|
|
|
|
return assert.True(t, strings.HasPrefix(s, p), "%q is not a prefix of %q", p, s)
|
|
|
|
}
|
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
func TestX5C_Getters(t *testing.T) {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
id := "x5c/" + p.Name
|
|
|
|
if got := p.GetID(); got != id {
|
|
|
|
t.Errorf("X5C.GetID() = %v, want %v:%v", got, p.Name, id)
|
|
|
|
}
|
|
|
|
if got := p.GetName(); got != p.Name {
|
|
|
|
t.Errorf("X5C.GetName() = %v, want %v", got, p.Name)
|
|
|
|
}
|
|
|
|
if got := p.GetType(); got != TypeX5C {
|
|
|
|
t.Errorf("X5C.GetType() = %v, want %v", got, TypeX5C)
|
|
|
|
}
|
|
|
|
kid, key, ok := p.GetEncryptedKey()
|
|
|
|
if kid != "" || key != "" || ok == true {
|
|
|
|
t.Errorf("X5C.GetEncryptedKey() = (%v, %v, %v), want (%v, %v, %v)",
|
|
|
|
kid, key, ok, "", "", false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestX5C_Init(t *testing.T) {
|
|
|
|
type ProvisionerValidateTest struct {
|
|
|
|
p *X5C
|
|
|
|
err error
|
|
|
|
extraValid func(*X5C) error
|
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) ProvisionerValidateTest{
|
|
|
|
"fail/empty": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
return ProvisionerValidateTest{
|
|
|
|
p: &X5C{},
|
|
|
|
err: errors.New("provisioner type cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/empty-name": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
return ProvisionerValidateTest{
|
|
|
|
p: &X5C{
|
|
|
|
Type: "X5C",
|
|
|
|
},
|
|
|
|
err: errors.New("provisioner name cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/empty-type": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
return ProvisionerValidateTest{
|
|
|
|
p: &X5C{Name: "foo"},
|
|
|
|
err: errors.New("provisioner type cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/empty-key": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
return ProvisionerValidateTest{
|
|
|
|
p: &X5C{Name: "foo", Type: "bar"},
|
|
|
|
err: errors.New("provisioner root(s) cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/no-valid-root-certs": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
return ProvisionerValidateTest{
|
2022-03-10 02:43:45 +00:00
|
|
|
p: &X5C{Name: "foo", Type: "bar", Roots: []byte("foo")},
|
2022-03-30 08:22:22 +00:00
|
|
|
err: errors.New("no x509 certificates found in roots attribute for provisioner 'foo'"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/invalid-duration": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
p.Claims = &Claims{DefaultTLSDur: &Duration{0}}
|
|
|
|
return ProvisionerValidateTest{
|
|
|
|
p: p,
|
2021-05-03 19:48:20 +00:00
|
|
|
err: errors.New("claims: MinTLSCertDuration must be greater than 0"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return ProvisionerValidateTest{
|
|
|
|
p: p,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/root-chain": func(t *testing.T) ProvisionerValidateTest {
|
|
|
|
p, err := generateX5C([]byte(`-----BEGIN CERTIFICATE-----
|
|
|
|
MIIBtjCCAVygAwIBAgIQNr+f4IkABY2n4wx4sLOMrTAKBggqhkjOPQQDAjAUMRIw
|
|
|
|
EAYDVQQDEwlyb290LXRlc3QwIBcNMTkxMDAyMDI0MDM0WhgPMjExOTA5MDgwMjQw
|
|
|
|
MzJaMBwxGjAYBgNVBAMTEWludGVybWVkaWF0ZS10ZXN0MFkwEwYHKoZIzj0CAQYI
|
|
|
|
KoZIzj0DAQcDQgAEflfRhPjgJXv4zsPWahXjM2UU61aRFErN0iw88ZPyxea22fxl
|
|
|
|
qN9ezntTXxzsS+mZiWapl8B40ACJgvP+WLQBHKOBhTCBgjAOBgNV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31:09 +00:00
|
|
|
return ProvisionerValidateTest{
|
|
|
|
p: p,
|
|
|
|
extraValid: func(p *X5C) error {
|
2022-08-23 19:43:48 +00:00
|
|
|
//nolint:staticcheck // We don't have a different way to
|
2022-03-21 23:53:57 +00:00
|
|
|
// check the number of certificates in the pool.
|
2019-09-05 01:31:09 +00:00
|
|
|
numCerts := len(p.rootPool.Subjects())
|
|
|
|
if numCerts != 2 {
|
2022-03-30 08:22:22 +00:00
|
|
|
return fmt.Errorf("unexpected number of certs: want 2, but got %d", numCerts)
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
config := Config{
|
|
|
|
Claims: globalProvisionerClaims,
|
|
|
|
Audiences: testAudiences,
|
|
|
|
}
|
|
|
|
for name, get := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := get(t)
|
|
|
|
err := tc.p.Init(config)
|
|
|
|
if err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.EqualError(t, tc.err, err.Error())
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tc.err) {
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, *tc.p.ctl.Audiences, config.Audiences.WithFragment(tc.p.GetID()))
|
2019-09-05 01:31:09 +00:00
|
|
|
if tc.extraValid != nil {
|
|
|
|
assert.Nil(t, tc.extraValid(tc.p))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestX5C_authorizeToken(t *testing.T) {
|
2019-12-20 21:30:05 +00:00
|
|
|
x5cCerts, err := pemutil.ReadCertificateBundle("./testdata/certs/x5c-leaf.crt")
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-24 21:44:11 +00:00
|
|
|
x5cJWK, err := jose.ReadKey("./testdata/secrets/x5c-leaf.key")
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
type test struct {
|
|
|
|
p *X5C
|
|
|
|
token string
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-09-05 01:31:09 +00:00
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
|
|
|
"fail/bad-token": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.authorizeToken; error parsing x5c token"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/invalid-cert-chain": func(t *testing.T) test {
|
|
|
|
certs, err := parseCerts([]byte(`-----BEGIN CERTIFICATE-----
|
|
|
|
MIIBpTCCAUugAwIBAgIRAOn2LHXjYyTXQ7PNjDTSKiIwCgYIKoZIzj0EAwIwHDEa
|
|
|
|
MBgGA1UEAxMRU21hbGxzdGVwIFJvb3QgQ0EwHhcNMTkwOTE0MDk1NTM2WhcNMjkw
|
|
|
|
OTExMDk1NTM2WjAkMSIwIAYDVQQDExlTbWFsbHN0ZXAgSW50ZXJtZWRpYXRlIENB
|
|
|
|
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE2Cs0TY0dLM4b2s+z8+cc3JJp/W5H
|
|
|
|
zQRvICX/1aJ4MuObNLcvoSguJwJEkYpGB5fhb0KvoL+ebHfEOywGNwrWkaNmMGQw
|
|
|
|
DgYDVR0PAQH/BAQDAgEGMBIGA1UdEwEB/wQIMAYBAf8CAQAwHQYDVR0OBBYEFNLJ
|
|
|
|
4ZXoX9cI6YkGPxgs2US3ssVzMB8GA1UdIwQYMBaAFGIwpqz85wL29aF47Vj9XSVM
|
|
|
|
P9K7MAoGCCqGSM49BAMCA0gAMEUCIQC5c1ldDcesDb31GlO5cEJvOcRrIrNtkk8m
|
|
|
|
a5wpg+9s6QIgHIW6L60F8klQX+EO3o0SBqLeNcaskA4oSZsKjEdpSGo=
|
|
|
|
-----END CERTIFICATE-----`))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
tok, err := generateToken("", p.Name, testAudiences.Sign[0], "",
|
|
|
|
[]string{"test.smallstep.com"}, time.Now(), jwk,
|
|
|
|
withX5CHdr(certs))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.authorizeToken; error verifying x5c certificate chain in token"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/doubled-up-self-signed-cert": func(t *testing.T) test {
|
|
|
|
certs, err := parseCerts([]byte(`-----BEGIN CERTIFICATE-----
|
|
|
|
MIIBgjCCASigAwIBAgIQIZiE9wpmSj6SMMDfHD17qjAKBggqhkjOPQQDAjAQMQ4w
|
|
|
|
DAYDVQQDEwVsZWFmMjAgFw0xOTEwMDIwMzEzNTlaGA8yMTE5MDkwODAzMTM1OVow
|
|
|
|
ED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wZ8G
|
|
|
|
EXAHTA9L
|
|
|
|
-----END CERTIFICATE-----`))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
tok, err := generateToken("", p.Name, testAudiences.Sign[0], "",
|
|
|
|
[]string{"test.smallstep.com"}, time.Now(), jwk,
|
|
|
|
withX5CHdr(certs))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.authorizeToken; error verifying x5c certificate chain in token"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/digital-signature-ext-required": func(t *testing.T) test {
|
|
|
|
certs, err := parseCerts([]byte(`-----BEGIN CERTIFICATE-----
|
|
|
|
MIIBuTCCAV+gAwIBAgIQeRJLdDMIdn/T2ORKxYABezAKBggqhkjOPQQDAjAcMRow
|
|
|
|
GAYDVQQDExFpbnRlcm1lZGlhdGUtdGVzdDAgFw0xOTEwMDIwMjQxMTRaGA8yMTE5
|
|
|
|
MDkwODAyNDExMlowFDESMBAGA1UEAxMJbGVhZi10ZXN0MFkwEwYHKoZIzj0CAQYI
|
|
|
|
KoZIzj0DAQcDQgAEDA1nGTOujobkcBWklyvymhWE5gQlvNLarVzhhhvPDw+MK2LX
|
|
|
|
yqkXrYZM10GrwQZuQ7ykHnjz00U/KXpPRQ7+0qOBiDCBhTAOBgNVHQ8BAf8EBAMC
|
|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E-----`))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
|
|
|
|
tok, err := generateToken("", p.Name, testAudiences.Sign[0], "",
|
|
|
|
[]string{"test.smallstep.com"}, time.Now(), jwk,
|
|
|
|
withX5CHdr(certs))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.authorizeToken; certificate used to sign x5c token cannot be used for digital signature"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/signature-does-not-match-x5c-pub-key": func(t *testing.T) test {
|
|
|
|
certs, err := parseCerts([]byte(`-----BEGIN CERTIFICATE-----
|
|
|
|
MIIBuDCCAV+gAwIBAgIQFdu723gqgGaTaqjf6ny88zAKBggqhkjOPQQDAjAcMRow
|
|
|
|
GAYDVQQDExFpbnRlcm1lZGlhdGUtdGVzdDAgFw0xOTEwMDIwMzE4NTNaGA8yMTE5
|
|
|
|
MDkwODAzMTg1MVowFDESMBAGA1UEAxMJbGVhZi10ZXN0MFkwEwYHKoZIzj0CAQYI
|
|
|
|
KoZIzj0DAQcDQgAEaV6807GhWEtMxA39zjuMVHAiN2/Ri5B1R1s+Y/8mlrKIvuvr
|
|
|
|
VpgSPXYruNRFduPWX564Abz/TDmb276JbKGeQqOBiDCBhTAOBgNVHQ8BAf8EBAMC
|
|
|
|
BaAwHQYDVR0lBBYw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 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
tok, err := generateToken("", "foobar", testAudiences.Sign[0], "",
|
|
|
|
[]string{"test.smallstep.com"}, time.Now(), jwk,
|
|
|
|
withX5CHdr(certs))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.authorizeToken; error parsing x5c claims"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/invalid-issuer": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
tok, err := generateToken("", "foobar", testAudiences.Sign[0], "",
|
2019-12-20 21:30:05 +00:00
|
|
|
[]string{"test.smallstep.com"}, time.Now(), x5cJWK,
|
|
|
|
withX5CHdr(x5cCerts))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.authorizeToken; invalid x5c claims"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/invalid-audience": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
tok, err := generateToken("", p.GetName(), "foobar", "",
|
2019-12-20 21:30:05 +00:00
|
|
|
[]string{"test.smallstep.com"}, time.Now(), x5cJWK,
|
|
|
|
withX5CHdr(x5cCerts))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.authorizeToken; x5c token has invalid audience claim (aud)"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/empty-subject": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
tok, err := generateToken("", p.GetName(), testAudiences.Sign[0], "",
|
2019-12-20 21:30:05 +00:00
|
|
|
[]string{"test.smallstep.com"}, time.Now(), x5cJWK,
|
|
|
|
withX5CHdr(x5cCerts))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.authorizeToken; x5c token subject cannot be empty"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
tok, err := generateToken("foo", p.GetName(), testAudiences.Sign[0], "",
|
2019-12-20 21:30:05 +00:00
|
|
|
[]string{"test.smallstep.com"}, time.Now(), x5cJWK,
|
|
|
|
withX5CHdr(x5cCerts))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
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-10-12 03:16:32 +00:00
|
|
|
var sc render.StatusCodedError
|
|
|
|
if assert.True(t, errors.As(err, &sc), "error does not implement StatusCodedError interface") {
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.code, sc.StatusCode())
|
2022-10-12 03:16:32 +00:00
|
|
|
}
|
2024-07-24 19:13:57 +00:00
|
|
|
assertHasPrefix(t, err.Error(), tc.err.Error())
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2024-07-24 19:13:57 +00:00
|
|
|
if assert.NoError(t, tc.err) {
|
2019-09-05 01:31:09 +00:00
|
|
|
assert.NotNil(t, claims)
|
|
|
|
assert.NotNil(t, claims.chains)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestX5C_AuthorizeSign(t *testing.T) {
|
2019-12-20 21:30:05 +00:00
|
|
|
certs, err := pemutil.ReadCertificateBundle("./testdata/certs/x5c-leaf.crt")
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-24 21:44:11 +00:00
|
|
|
jwk, err := jose.ReadKey("./testdata/secrets/x5c-leaf.key")
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
type test struct {
|
2023-12-29 01:09:39 +00:00
|
|
|
p *X5C
|
|
|
|
token string
|
|
|
|
code int
|
|
|
|
err error
|
|
|
|
sans []string
|
|
|
|
fingerprint string
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
|
|
|
"fail/invalid-token": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
2019-12-20 21:30:05 +00:00
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.AuthorizeSign: x5c.authorizeToken; error parsing x5c token"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/empty-sans": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
tok, err := generateToken("foo", p.GetName(), testAudiences.Sign[0], "",
|
|
|
|
[]string{}, time.Now(), jwk,
|
|
|
|
withX5CHdr(certs))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
2020-06-25 06:25:15 +00:00
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
sans: []string{"foo"},
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/multi-sans": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
tok, err := generateToken("foo", p.GetName(), testAudiences.Sign[0], "",
|
|
|
|
[]string{"127.0.0.1", "foo", "max@smallstep.com"}, time.Now(), jwk,
|
|
|
|
withX5CHdr(certs))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
2020-06-25 06:25:15 +00:00
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
sans: []string{"127.0.0.1", "foo", "max@smallstep.com"},
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
2023-12-29 01:09:39 +00:00
|
|
|
"ok/cnf": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-29 01:09:39 +00:00
|
|
|
|
|
|
|
x5c := make([]string, len(certs))
|
|
|
|
for i, cert := range certs {
|
|
|
|
x5c[i] = base64.StdEncoding.EncodeToString(cert.Raw)
|
|
|
|
}
|
|
|
|
extraHeaders := map[string]any{"x5c": x5c}
|
|
|
|
extraClaims := map[string]any{
|
|
|
|
"sans": []string{"127.0.0.1", "foo", "max@smallstep.com"},
|
2024-07-23 19:51:11 +00:00
|
|
|
"cnf": map[string]any{"x5rt#S256": "fingerprint"},
|
2023-12-29 01:09:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := generateCustomToken("foo", p.GetName(), testAudiences.Sign[0], jwk, extraHeaders, extraClaims)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-29 01:09:39 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
sans: []string{"127.0.0.1", "foo", "max@smallstep.com"},
|
|
|
|
fingerprint: "fingerprint",
|
|
|
|
}
|
|
|
|
},
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := tt(t)
|
2023-10-06 21:02:19 +00:00
|
|
|
ctx := NewContextWithMethod(context.Background(), SignIdentityMethod)
|
|
|
|
if opts, err := tc.p.AuthorizeSign(ctx, tc.token); err != nil {
|
2023-12-29 01:09:39 +00:00
|
|
|
if assert.NotNil(t, tc.err, err.Error()) {
|
2022-10-12 03:16:32 +00:00
|
|
|
var sc render.StatusCodedError
|
|
|
|
if assert.True(t, errors.As(err, &sc), "error does not implement StatusCodedError interface") {
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.code, sc.StatusCode())
|
2022-10-12 03:16:32 +00:00
|
|
|
}
|
2024-07-24 19:13:57 +00:00
|
|
|
assertHasPrefix(t, err.Error(), tc.err.Error())
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tc.err) {
|
|
|
|
if assert.NotNil(t, opts) {
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Len(t, opts, 11)
|
2019-09-05 01:31:09 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
switch v := o.(type) {
|
2022-03-28 22:06:56 +00:00
|
|
|
case *X5C:
|
2020-07-21 02:01:43 +00:00
|
|
|
case certificateOptionsFunc:
|
2019-09-05 01:31:09 +00:00
|
|
|
case *provisionerExtensionOption:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, TypeX5C, v.Type)
|
|
|
|
assert.Equal(t, tc.p.GetName(), v.Name)
|
|
|
|
assert.Equal(t, "", v.CredentialID)
|
|
|
|
assert.Len(t, v.KeyValuePairs, 0)
|
2019-09-05 01:31:09 +00:00
|
|
|
case profileLimitDuration:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.p.ctl.Claimer.DefaultTLSCertDuration(), v.def)
|
2022-03-10 02:43:45 +00:00
|
|
|
claims, err := tc.p.authorizeToken(tc.token, tc.p.ctl.Audiences.Sign)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, claims.chains[0][0].NotAfter, v.notAfter)
|
2019-09-05 01:31:09 +00:00
|
|
|
case commonNameValidator:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, "foo", string(v))
|
2019-09-05 01:31:09 +00:00
|
|
|
case defaultPublicKeyValidator:
|
2023-10-06 21:02:19 +00:00
|
|
|
case *defaultSANsValidator:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.sans, v.sans)
|
|
|
|
assert.Equal(t, SignIdentityMethod, MethodFromContext(v.ctx))
|
2019-09-05 01:31:09 +00:00
|
|
|
case *validityValidator:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.p.ctl.Claimer.MinTLSCertDuration(), v.min)
|
|
|
|
assert.Equal(t, tc.p.ctl.Claimer.MaxTLSCertDuration(), v.max)
|
2022-01-03 11:25:24 +00:00
|
|
|
case *x509NamePolicyValidator:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, nil, v.policyEngine)
|
2022-09-30 00:16:26 +00:00
|
|
|
case *WebhookController:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Len(t, v.webhooks, 0)
|
|
|
|
assert.Equal(t, linkedca.Webhook_X509, v.certType)
|
|
|
|
assert.Len(t, v.options, 2)
|
2024-07-23 18:48:46 +00:00
|
|
|
case csrFingerprintValidator:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.fingerprint, string(v))
|
2019-09-05 01:31:09 +00:00
|
|
|
default:
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, fmt.Errorf("unexpected sign option of type %T", v))
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
func TestX5C_AuthorizeRevoke(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
p *X5C
|
|
|
|
token string
|
|
|
|
code int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
|
|
|
"fail/invalid-token": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.AuthorizeRevoke: x5c.authorizeToken; error parsing x5c token"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
certs, err := pemutil.ReadCertificateBundle("./testdata/certs/x5c-leaf.crt")
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-24 21:44:11 +00:00
|
|
|
jwk, err := jose.ReadKey("./testdata/secrets/x5c-leaf.key")
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
tok, err := generateToken("foo", p.GetName(), testAudiences.Revoke[0], "",
|
|
|
|
[]string{"test.smallstep.com"}, time.Now(), jwk,
|
|
|
|
withX5CHdr(certs))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := tt(t)
|
2020-01-24 06:04:34 +00:00
|
|
|
if err := tc.p.AuthorizeRevoke(context.Background(), tc.token); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
if assert.NotNil(t, tc.err) {
|
2022-10-12 03:16:32 +00:00
|
|
|
var sc render.StatusCodedError
|
|
|
|
if assert.True(t, errors.As(err, &sc), "error does not implement StatusCodedError interface") {
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.code, sc.StatusCode())
|
2022-10-12 03:16:32 +00:00
|
|
|
}
|
2024-07-24 19:13:57 +00:00
|
|
|
assertHasPrefix(t, err.Error(), tc.err.Error())
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestX5C_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 *X5C
|
|
|
|
code int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
|
|
|
"fail/renew-disabled": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
// 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)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
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 := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := tt(t)
|
2022-03-10 02:43:45 +00:00
|
|
|
if err := tc.p.AuthorizeRenew(context.Background(), &x509.Certificate{
|
|
|
|
NotBefore: now,
|
|
|
|
NotAfter: now.Add(time.Hour),
|
|
|
|
}); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
if assert.NotNil(t, tc.err) {
|
2022-10-12 03:16:32 +00:00
|
|
|
var sc render.StatusCodedError
|
|
|
|
if assert.True(t, errors.As(err, &sc), "error does not implement StatusCodedError interface") {
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.code, sc.StatusCode())
|
2022-10-12 03:16:32 +00:00
|
|
|
}
|
2024-07-24 19:13:57 +00:00
|
|
|
assertHasPrefix(t, err.Error(), tc.err.Error())
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 18:48:06 +00:00
|
|
|
func TestX5C_AuthorizeSSHSign(t *testing.T) {
|
2019-12-20 21:30:05 +00:00
|
|
|
x5cCerts, err := pemutil.ReadCertificateBundle("./testdata/certs/x5c-leaf.crt")
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2020-08-24 21:44:11 +00:00
|
|
|
x5cJWK, err := jose.ReadKey("./testdata/secrets/x5c-leaf.key")
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
_, fn := mockNow()
|
|
|
|
defer fn()
|
|
|
|
type test struct {
|
2023-12-29 01:09:39 +00:00
|
|
|
p *X5C
|
|
|
|
token string
|
|
|
|
claims *x5cPayload
|
|
|
|
fingerprint string
|
|
|
|
count int
|
|
|
|
code int
|
|
|
|
err error
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
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 := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
// disable sshCA
|
|
|
|
enable := false
|
|
|
|
p.Claims = &Claims{EnableSSHCA: &enable}
|
2022-03-10 02:43:45 +00:00
|
|
|
p.ctl.Claimer, err = NewClaimer(p.Claims, globalProvisionerClaims)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
|
|
|
code: http.StatusUnauthorized,
|
2022-03-30 08:22:22 +00:00
|
|
|
err: fmt.Errorf("x5c.AuthorizeSSHSign; sshCA is disabled for x5c provisioner '%s'", p.GetName()),
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/invalid-token": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.AuthorizeSSHSign: x5c.authorizeToken; error parsing x5c token"),
|
|
|
|
}
|
|
|
|
},
|
2019-09-05 01:31:09 +00:00
|
|
|
"fail/no-Step-claim": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
tok, err := generateToken("foo", p.GetName(), testAudiences.SSHSign[0], "",
|
|
|
|
[]string{"test.smallstep.com"}, time.Now(), x5cJWK,
|
|
|
|
withX5CHdr(x5cCerts))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
2019-12-20 21:30:05 +00:00
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.AuthorizeSSHSign; x5c token must be an SSH provisioning token"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/no-SSH-subattribute-in-claims": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
|
|
|
|
id, err := randutil.ASCII(64)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
now := time.Now()
|
|
|
|
claims := &x5cPayload{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
ID: id,
|
|
|
|
Subject: "foo",
|
|
|
|
Issuer: p.GetName(),
|
|
|
|
IssuedAt: jose.NewNumericDate(now),
|
|
|
|
NotBefore: jose.NewNumericDate(now),
|
|
|
|
Expiry: jose.NewNumericDate(now.Add(5 * time.Minute)),
|
|
|
|
Audience: []string{testAudiences.SSHSign[0]},
|
|
|
|
},
|
|
|
|
Step: &stepPayload{},
|
|
|
|
}
|
|
|
|
tok, err := generateX5CSSHToken(x5cJWK, claims, withX5CHdr(x5cCerts))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
2019-12-20 21:30:05 +00:00
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("x5c.AuthorizeSSHSign; x5c token must be an SSH provisioning token"),
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/with-claims": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
|
|
|
|
id, err := randutil.ASCII(64)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
now := time.Now()
|
|
|
|
claims := &x5cPayload{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
ID: id,
|
|
|
|
Subject: "foo",
|
|
|
|
Issuer: p.GetName(),
|
|
|
|
IssuedAt: jose.NewNumericDate(now),
|
|
|
|
NotBefore: jose.NewNumericDate(now),
|
|
|
|
Expiry: jose.NewNumericDate(now.Add(5 * time.Minute)),
|
|
|
|
Audience: []string{testAudiences.SSHSign[0]},
|
|
|
|
},
|
2020-07-23 01:24:45 +00:00
|
|
|
Step: &stepPayload{SSH: &SignSSHOptions{
|
2023-12-29 01:09:39 +00:00
|
|
|
CertType: SSHUserCert,
|
2020-08-04 01:10:29 +00:00
|
|
|
KeyID: "foo",
|
2019-12-20 21:30:05 +00:00
|
|
|
Principals: []string{"max", "mariano", "alan"},
|
|
|
|
ValidAfter: TimeDuration{d: 5 * time.Minute},
|
|
|
|
ValidBefore: TimeDuration{d: 10 * time.Minute},
|
|
|
|
}},
|
|
|
|
}
|
|
|
|
tok, err := generateX5CSSHToken(x5cJWK, claims, withX5CHdr(x5cCerts))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
2019-12-20 21:30:05 +00:00
|
|
|
p: p,
|
|
|
|
claims: claims,
|
|
|
|
token: tok,
|
2023-12-29 01:09:39 +00:00
|
|
|
count: 12,
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/without-claims": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
|
|
|
|
id, err := randutil.ASCII(64)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-12-20 21:30:05 +00:00
|
|
|
now := time.Now()
|
|
|
|
claims := &x5cPayload{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
ID: id,
|
|
|
|
Subject: "foo",
|
|
|
|
Issuer: p.GetName(),
|
|
|
|
IssuedAt: jose.NewNumericDate(now),
|
|
|
|
NotBefore: jose.NewNumericDate(now),
|
|
|
|
Expiry: jose.NewNumericDate(now.Add(5 * time.Minute)),
|
|
|
|
Audience: []string{testAudiences.SSHSign[0]},
|
|
|
|
},
|
2020-07-23 01:24:45 +00:00
|
|
|
Step: &stepPayload{SSH: &SignSSHOptions{}},
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
tok, err := generateX5CSSHToken(x5cJWK, claims, withX5CHdr(x5cCerts))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-05 01:31:09 +00:00
|
|
|
return test{
|
2019-12-20 21:30:05 +00:00
|
|
|
p: p,
|
|
|
|
claims: claims,
|
|
|
|
token: tok,
|
2023-12-29 01:09:39 +00:00
|
|
|
count: 10,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/cnf": func(t *testing.T) test {
|
|
|
|
p, err := generateX5C(nil)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-29 01:09:39 +00:00
|
|
|
|
|
|
|
id, err := randutil.ASCII(64)
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-29 01:09:39 +00:00
|
|
|
now := time.Now()
|
|
|
|
claims := &x5cPayload{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
ID: id,
|
|
|
|
Subject: "foo",
|
|
|
|
Issuer: p.GetName(),
|
|
|
|
IssuedAt: jose.NewNumericDate(now),
|
|
|
|
NotBefore: jose.NewNumericDate(now),
|
|
|
|
Expiry: jose.NewNumericDate(now.Add(5 * time.Minute)),
|
|
|
|
Audience: []string{testAudiences.SSHSign[0]},
|
|
|
|
},
|
|
|
|
Step: &stepPayload{SSH: &SignSSHOptions{
|
|
|
|
CertType: SSHHostCert,
|
|
|
|
Principals: []string{"host.smallstep.com"},
|
|
|
|
}},
|
|
|
|
Confirmation: &cnfPayload{
|
2024-07-23 19:51:11 +00:00
|
|
|
Fingerprint: "fingerprint",
|
2023-12-29 01:09:39 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
tok, err := generateX5CSSHToken(x5cJWK, claims, withX5CHdr(x5cCerts))
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, err)
|
2023-12-29 01:09:39 +00:00
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
claims: claims,
|
|
|
|
token: tok,
|
|
|
|
fingerprint: "fingerprint",
|
2024-07-23 18:48:46 +00:00
|
|
|
count: 10,
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := tt(t)
|
2020-01-24 06:04:34 +00:00
|
|
|
if opts, err := tc.p.AuthorizeSSHSign(context.Background(), tc.token); err != nil {
|
2019-09-05 01:31:09 +00:00
|
|
|
if assert.NotNil(t, tc.err) {
|
2022-10-12 03:16:32 +00:00
|
|
|
var sc render.StatusCodedError
|
|
|
|
if assert.True(t, errors.As(err, &sc), "error does not implement StatusCodedError interface") {
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.code, sc.StatusCode())
|
2022-10-12 03:16:32 +00:00
|
|
|
}
|
2024-07-24 19:13:57 +00:00
|
|
|
assertHasPrefix(t, err.Error(), tc.err.Error())
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tc.err) {
|
|
|
|
if assert.NotNil(t, opts) {
|
|
|
|
tot := 0
|
2020-08-04 01:10:29 +00:00
|
|
|
firstValidator := true
|
2019-09-05 01:31:09 +00:00
|
|
|
nw := now()
|
|
|
|
for _, o := range opts {
|
|
|
|
switch v := o.(type) {
|
2022-05-19 01:51:36 +00:00
|
|
|
case Interface:
|
2020-01-24 06:04:34 +00:00
|
|
|
case sshCertOptionsValidator:
|
2019-09-05 01:31:09 +00:00
|
|
|
tc.claims.Step.SSH.ValidAfter.t = time.Time{}
|
|
|
|
tc.claims.Step.SSH.ValidBefore.t = time.Time{}
|
2020-08-04 01:10:29 +00:00
|
|
|
if firstValidator {
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, *tc.claims.Step.SSH, SignSSHOptions(v))
|
2020-08-04 01:10:29 +00:00
|
|
|
} else {
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, SignSSHOptions{KeyID: tc.claims.Subject}, SignSSHOptions(v))
|
2020-08-04 01:10:29 +00:00
|
|
|
}
|
|
|
|
firstValidator = false
|
2019-12-20 21:30:05 +00:00
|
|
|
case sshCertValidAfterModifier:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.claims.Step.SSH.ValidAfter.RelativeTime(nw).Unix(), int64(v))
|
2019-12-20 21:30:05 +00:00
|
|
|
case sshCertValidBeforeModifier:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.claims.Step.SSH.ValidBefore.RelativeTime(nw).Unix(), int64(v))
|
2020-01-04 02:22:02 +00:00
|
|
|
case *sshLimitDuration:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.p.ctl.Claimer, v.Claimer)
|
|
|
|
assert.Equal(t, x5cCerts[0].NotAfter, v.NotAfter)
|
2020-01-24 06:04:34 +00:00
|
|
|
case *sshCertValidityValidator:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.p.ctl.Claimer, v.Claimer)
|
2022-01-03 11:25:24 +00:00
|
|
|
case *sshNamePolicyValidator:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Nil(t, v.userPolicyEngine)
|
|
|
|
assert.Nil(t, v.hostPolicyEngine)
|
2020-08-04 01:10:29 +00:00
|
|
|
case *sshDefaultPublicKeyValidator, *sshCertDefaultValidator, sshCertificateOptionsFunc:
|
2022-09-30 00:16:26 +00:00
|
|
|
case *WebhookController:
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Len(t, v.webhooks, 0)
|
|
|
|
assert.Equal(t, linkedca.Webhook_SSH, v.certType)
|
|
|
|
assert.Len(t, v.options, 2)
|
2019-09-05 01:31:09 +00:00
|
|
|
default:
|
2024-07-24 19:13:57 +00:00
|
|
|
require.NoError(t, fmt.Errorf("unexpected sign option of type %T", v))
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
tot++
|
|
|
|
}
|
2024-07-24 19:13:57 +00:00
|
|
|
assert.Equal(t, tc.count, tot)
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|