| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996 |
- package stepcas
- import (
- "bytes"
- "context"
- "crypto"
- "crypto/ed25519"
- "crypto/rand"
- "crypto/x509"
- "encoding/json"
- "encoding/pem"
- "fmt"
- "net/http"
- "net/http/httptest"
- "net/url"
- "os"
- "path/filepath"
- "reflect"
- "testing"
- "time"
- "github.com/smallstep/certificates/api"
- "github.com/smallstep/certificates/authority/provisioner"
- "github.com/smallstep/certificates/ca"
- "github.com/smallstep/certificates/cas/apiv1"
- "github.com/stretchr/testify/require"
- "go.step.sm/crypto/jose"
- "go.step.sm/crypto/pemutil"
- "go.step.sm/crypto/randutil"
- "go.step.sm/crypto/x509util"
- )
- var (
- testRootCrt *x509.Certificate
- testRootKey crypto.Signer
- testRootPath, testRootKeyPath string
- testRootFingerprint string
- testIssCrt *x509.Certificate
- testIssKey crypto.Signer
- testIssPath, testIssKeyPath string
- testX5CCrt *x509.Certificate
- testX5CKey crypto.Signer
- testX5CPath, testX5CKeyPath string
- testPassword, testEncryptedKeyPath string
- testKeyID, testEncryptedJWKKey string
- testCR *x509.CertificateRequest
- testCrt *x509.Certificate
- testKey crypto.Signer
- testFailCR *x509.CertificateRequest
- )
- func mustSignCertificate(subject string, sans []string, template string, parent *x509.Certificate, signer crypto.Signer) (*x509.Certificate, crypto.Signer) {
- pub, priv, err := ed25519.GenerateKey(rand.Reader)
- if err != nil {
- panic(err)
- }
- cr, err := x509util.CreateCertificateRequest(subject, sans, priv)
- if err != nil {
- panic(err)
- }
- cert, err := x509util.NewCertificate(cr, x509util.WithTemplate(template, x509util.CreateTemplateData(subject, sans)))
- if err != nil {
- panic(err)
- }
- crt := cert.GetCertificate()
- crt.NotBefore = time.Now()
- crt.NotAfter = crt.NotBefore.Add(time.Hour)
- if parent == nil {
- parent = crt
- }
- if signer == nil {
- signer = priv
- }
- if crt, err = x509util.CreateCertificate(crt, parent, pub, signer); err != nil {
- panic(err)
- }
- return crt, priv
- }
- func mustSerializeCrt(filename string, certs ...*x509.Certificate) {
- buf := new(bytes.Buffer)
- for _, c := range certs {
- if err := pem.Encode(buf, &pem.Block{
- Type: "CERTIFICATE",
- Bytes: c.Raw,
- }); err != nil {
- panic(err)
- }
- }
- if err := os.WriteFile(filename, buf.Bytes(), 0600); err != nil {
- panic(err)
- }
- }
- func mustSerializeKey(filename string, key crypto.Signer) {
- b, err := x509.MarshalPKCS8PrivateKey(key)
- if err != nil {
- panic(err)
- }
- b = pem.EncodeToMemory(&pem.Block{
- Type: "PRIVATE KEY",
- Bytes: b,
- })
- if err := os.WriteFile(filename, b, 0600); err != nil {
- panic(err)
- }
- }
- func mustEncryptKey(filename string, key crypto.Signer) {
- _, err := pemutil.Serialize(key,
- pemutil.ToFile(filename, 0600),
- pemutil.WithPKCS8(true),
- pemutil.WithPassword([]byte(testPassword)))
- if err != nil {
- panic(err)
- }
- }
- func testCAHelper(t *testing.T) (*url.URL, *ca.Client) {
- t.Helper()
- writeJSON := func(w http.ResponseWriter, v interface{}) {
- _ = json.NewEncoder(w).Encode(v)
- }
- parseJSON := func(r *http.Request, v interface{}) {
- _ = json.NewDecoder(r.Body).Decode(v)
- }
- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- switch r.RequestURI {
- case "/root/" + testRootFingerprint:
- w.WriteHeader(http.StatusOK)
- writeJSON(w, api.RootResponse{
- RootPEM: api.NewCertificate(testRootCrt),
- })
- case "/sign":
- var msg api.SignRequest
- parseJSON(r, &msg)
- if msg.CsrPEM.DNSNames[0] == "fail.doe.org" {
- w.WriteHeader(http.StatusBadRequest)
- fmt.Fprintf(w, `{"error":"fail","message":"fail"}`)
- return
- }
- w.WriteHeader(http.StatusOK)
- writeJSON(w, api.SignResponse{
- CertChainPEM: []api.Certificate{api.NewCertificate(testCrt), api.NewCertificate(testIssCrt)},
- })
- case "/renew":
- if r.Header.Get("Authorization") == "Bearer fail" {
- w.WriteHeader(http.StatusBadRequest)
- fmt.Fprintf(w, `{"error":"fail","message":"fail"}`)
- return
- }
- w.WriteHeader(http.StatusOK)
- writeJSON(w, api.SignResponse{
- CertChainPEM: []api.Certificate{api.NewCertificate(testCrt), api.NewCertificate(testIssCrt)},
- })
- case "/revoke":
- var msg api.RevokeRequest
- parseJSON(r, &msg)
- if msg.Serial == "fail" {
- w.WriteHeader(http.StatusBadRequest)
- fmt.Fprintf(w, `{"error":"fail","message":"fail"}`)
- return
- }
- w.WriteHeader(http.StatusOK)
- writeJSON(w, api.RevokeResponse{
- Status: "ok",
- })
- case "/provisioners":
- w.WriteHeader(http.StatusOK)
- writeJSON(w, api.ProvisionersResponse{
- NextCursor: "cursor",
- Provisioners: []provisioner.Interface{
- &provisioner.JWK{
- Type: "JWK",
- Name: "ra@doe.org",
- Key: &jose.JSONWebKey{KeyID: testKeyID, Key: testX5CKey.Public()},
- EncryptedKey: testEncryptedJWKKey,
- },
- &provisioner.JWK{
- Type: "JWK",
- Name: "empty@doe.org",
- Key: &jose.JSONWebKey{KeyID: testKeyID, Key: testX5CKey.Public()},
- },
- },
- })
- case "/provisioners?cursor=cursor":
- w.WriteHeader(http.StatusOK)
- writeJSON(w, api.ProvisionersResponse{})
- default:
- w.WriteHeader(http.StatusNotFound)
- fmt.Fprintf(w, `{"error":"not found"}`)
- }
- }))
- t.Cleanup(func() {
- srv.Close()
- })
- u, err := url.Parse(srv.URL)
- if err != nil {
- srv.Close()
- t.Fatal(err)
- }
- client, err := ca.NewClient(srv.URL, ca.WithTransport(http.DefaultTransport))
- if err != nil {
- srv.Close()
- t.Fatal(err)
- }
- return u, client
- }
- func testX5CIssuer(t *testing.T, caURL *url.URL, password string) *x5cIssuer {
- t.Helper()
- key, givenPassword := testX5CKeyPath, password
- if password != "" {
- key = testEncryptedKeyPath
- password = testPassword
- }
- x5c, err := newX5CIssuer(caURL, &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: key,
- Password: password,
- })
- if err != nil {
- t.Fatal(err)
- }
- x5c.password = givenPassword
- return x5c
- }
- func testJWKIssuer(t *testing.T, caURL *url.URL, password string) *jwkIssuer {
- t.Helper()
- client, err := ca.NewClient(caURL.String(), ca.WithTransport(http.DefaultTransport))
- if err != nil {
- t.Fatal(err)
- }
- key := testX5CKeyPath
- if password != "" {
- key = testEncryptedKeyPath
- password = testPassword
- }
- jwk, err := newJWKIssuer(context.TODO(), caURL, client, &apiv1.CertificateIssuer{
- Type: "jwk",
- Provisioner: "ra@doe.org",
- Key: key,
- Password: password,
- })
- if err != nil {
- t.Fatal(err)
- }
- return jwk
- }
- func TestMain(m *testing.M) {
- testRootCrt, testRootKey = mustSignCertificate("Test Root Certificate", nil, x509util.DefaultRootTemplate, nil, nil)
- testIssCrt, testIssKey = mustSignCertificate("Test Intermediate Certificate", nil, x509util.DefaultIntermediateTemplate, testRootCrt, testRootKey)
- testX5CCrt, testX5CKey = mustSignCertificate("Test X5C Certificate", nil, x509util.DefaultLeafTemplate, testIssCrt, testIssKey)
- testRootFingerprint = x509util.Fingerprint(testRootCrt)
- // Final certificate.
- var err error
- sans := []string{"doe.org", "jane@doe.org", "127.0.0.1", "::1", "localhost", "uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6;name=value"}
- testCrt, testKey = mustSignCertificate("Test Certificate", sans, x509util.DefaultLeafTemplate, testIssCrt, testIssKey)
- testCR, err = x509util.CreateCertificateRequest("Test Certificate", sans, testKey)
- if err != nil {
- panic(err)
- }
- // CR used in errors.
- testFailCR, err = x509util.CreateCertificateRequest("", []string{"fail.doe.org"}, testKey)
- if err != nil {
- panic(err)
- }
- // Password used to encrypt the key.
- testPassword = randutil.Hex(32)
- // Encrypted JWK key used when the key is downloaded from the CA.
- jwe, err := jose.EncryptJWK(&jose.JSONWebKey{Key: testX5CKey}, []byte(testPassword))
- if err != nil {
- panic(err)
- }
- testEncryptedJWKKey, err = jwe.CompactSerialize()
- if err != nil {
- panic(err)
- }
- testKeyID, err = jose.Thumbprint(&jose.JSONWebKey{Key: testX5CKey})
- if err != nil {
- panic(err)
- }
- // Create test files.
- path, err := os.MkdirTemp(os.TempDir(), "stepcas")
- if err != nil {
- panic(err)
- }
- testRootPath = filepath.Join(path, "root_ca.crt")
- testRootKeyPath = filepath.Join(path, "root_ca.key")
- mustSerializeCrt(testRootPath, testRootCrt)
- mustSerializeKey(testRootKeyPath, testRootKey)
- testIssPath = filepath.Join(path, "intermediate_ca.crt")
- testIssKeyPath = filepath.Join(path, "intermediate_ca.key")
- mustSerializeCrt(testIssPath, testIssCrt)
- mustSerializeKey(testIssKeyPath, testIssKey)
- testX5CPath = filepath.Join(path, "x5c.crt")
- testX5CKeyPath = filepath.Join(path, "x5c.key")
- mustSerializeCrt(testX5CPath, testX5CCrt, testIssCrt)
- mustSerializeKey(testX5CKeyPath, testX5CKey)
- testEncryptedKeyPath = filepath.Join(path, "x5c.enc.key")
- mustEncryptKey(testEncryptedKeyPath, testX5CKey)
- code := m.Run()
- if err := os.RemoveAll(path); err != nil {
- panic(err)
- }
- os.Exit(code)
- }
- func Test_init(t *testing.T) {
- caURL, _ := testCAHelper(t)
- fn, ok := apiv1.LoadCertificateAuthorityServiceNewFunc(apiv1.StepCAS)
- if !ok {
- t.Errorf("apiv1.Register() ok = %v, want true", ok)
- return
- }
- fn(context.Background(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: testX5CKeyPath,
- },
- })
- }
- func TestNew(t *testing.T) {
- caURL, client := testCAHelper(t)
- signer, err := newJWKSignerFromEncryptedKey(testKeyID, testEncryptedJWKKey, testPassword)
- if err != nil {
- t.Fatal(err)
- }
- type args struct {
- ctx context.Context
- opts apiv1.Options
- }
- tests := []struct {
- name string
- args args
- want *StepCAS
- wantErr bool
- }{
- {"ok", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: testX5CKeyPath,
- },
- }}, &StepCAS{
- iss: &x5cIssuer{
- caURL: caURL,
- certFile: testX5CPath,
- keyFile: testX5CKeyPath,
- issuer: "X5C",
- },
- client: client,
- fingerprint: testRootFingerprint,
- }, false},
- {"ok jwk", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "jwk",
- Provisioner: "ra@doe.org",
- Key: testX5CKeyPath,
- },
- }}, &StepCAS{
- iss: &jwkIssuer{
- caURL: caURL,
- issuer: "ra@doe.org",
- signer: signer,
- },
- client: client,
- fingerprint: testRootFingerprint,
- }, false},
- {"ok jwk provisioners", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "jwk",
- Provisioner: "ra@doe.org",
- Password: testPassword,
- },
- }}, &StepCAS{
- iss: &jwkIssuer{
- caURL: caURL,
- issuer: "ra@doe.org",
- signer: signer,
- },
- client: client,
- fingerprint: testRootFingerprint,
- }, false},
- {"ok ca getter", args{context.TODO(), apiv1.Options{
- IsCAGetter: true,
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "jwk",
- Provisioner: "ra@doe.org",
- },
- }}, &StepCAS{
- iss: nil,
- client: client,
- fingerprint: testRootFingerprint,
- }, false},
- {"fail authority", args{context.TODO(), apiv1.Options{
- CertificateAuthority: "",
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- {"fail fingerprint", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: "",
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- {"fail type", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- {"fail provisioner", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "",
- Certificate: testX5CPath,
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- {"fail provisioner jwk", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "jwk",
- Provisioner: "",
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- {"fail provisioner not found", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "jwk",
- Provisioner: "notfound@doe.org",
- Password: testPassword,
- },
- }}, nil, true},
- {"fail invalid password", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "jwk",
- Provisioner: "ra@doe.org",
- Password: "bad-password",
- },
- }}, nil, true},
- {"fail no key", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "jwk",
- Provisioner: "empty@doe.org",
- Password: testPassword,
- },
- }}, nil, true},
- {"fail certificate", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: "",
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- {"fail key", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: "",
- },
- }}, nil, true},
- {"fail key jwk", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "jwk",
- Provisioner: "ra@smallstep.com",
- Key: "",
- },
- }}, nil, true},
- {"bad authority", args{context.TODO(), apiv1.Options{
- CertificateAuthority: "https://foobar",
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- {"fail parse url", args{context.TODO(), apiv1.Options{
- CertificateAuthority: "::failparse",
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- {"fail new client", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: "foobar",
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- {"fail new x5c issuer", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "x5c",
- Provisioner: "X5C",
- Certificate: testX5CPath + ".missing",
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- {"fail new jwk issuer", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "jwk",
- Provisioner: "ra@doe.org",
- Key: testX5CKeyPath + ".missing",
- },
- }}, nil, true},
- {"bad issuer", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: nil}}, nil, true},
- {"bad issuer type", args{context.TODO(), apiv1.Options{
- CertificateAuthority: caURL.String(),
- CertificateAuthorityFingerprint: testRootFingerprint,
- CertificateIssuer: &apiv1.CertificateIssuer{
- Type: "fail",
- Provisioner: "X5C",
- Certificate: testX5CPath,
- Key: testX5CKeyPath,
- },
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- got, err := New(tt.args.ctx, tt.args.opts)
- if (err != nil) != tt.wantErr {
- t.Errorf("New() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- // We cannot compare neither the client nor the signer.
- if got != nil && tt.want != nil {
- got.client = tt.want.client
- if jwk, ok := got.iss.(*jwkIssuer); ok {
- jwk.signer = signer
- }
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("New() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestStepCAS_Type(t *testing.T) {
- tests := []struct {
- name string
- want apiv1.Type
- }{
- {"ok", apiv1.StepCAS},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- c := &StepCAS{}
- if got := c.Type(); got != tt.want {
- t.Errorf("StepCAS.Type() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestStepCAS_CreateCertificate(t *testing.T) {
- caURL, client := testCAHelper(t)
- x5c := testX5CIssuer(t, caURL, "")
- jwk := testJWKIssuer(t, caURL, "")
- x5cEnc := testX5CIssuer(t, caURL, testPassword)
- jwkEnc := testJWKIssuer(t, caURL, testPassword)
- x5cBad := testX5CIssuer(t, caURL, "bad-password")
- testTemplate := &x509.Certificate{
- Subject: testCR.Subject,
- DNSNames: testCR.DNSNames,
- EmailAddresses: testCR.EmailAddresses,
- IPAddresses: testCR.IPAddresses,
- URIs: testCR.URIs,
- }
- testOtherCR, err := x509util.CreateCertificateRequest("Test Certificate", []string{"test.example.com"}, testKey)
- require.NoError(t, err)
- type fields struct {
- iss stepIssuer
- client *ca.Client
- fingerprint string
- }
- type args struct {
- req *apiv1.CreateCertificateRequest
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *apiv1.CreateCertificateResponse
- wantErr bool
- }{
- {"ok", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testCR,
- Template: testTemplate,
- Lifetime: time.Hour,
- }}, &apiv1.CreateCertificateResponse{
- Certificate: testCrt,
- CertificateChain: []*x509.Certificate{testIssCrt},
- }, false},
- {"ok with different CSR", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testOtherCR,
- Template: testTemplate,
- Lifetime: time.Hour,
- }}, &apiv1.CreateCertificateResponse{
- Certificate: testCrt,
- CertificateChain: []*x509.Certificate{testIssCrt},
- }, false},
- {"ok with password", fields{x5cEnc, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testCR,
- Template: testTemplate,
- Lifetime: time.Hour,
- }}, &apiv1.CreateCertificateResponse{
- Certificate: testCrt,
- CertificateChain: []*x509.Certificate{testIssCrt},
- }, false},
- {"ok jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testCR,
- Template: testTemplate,
- Lifetime: time.Hour,
- }}, &apiv1.CreateCertificateResponse{
- Certificate: testCrt,
- CertificateChain: []*x509.Certificate{testIssCrt},
- }, false},
- {"ok jwk with password", fields{jwkEnc, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testCR,
- Template: testTemplate,
- Lifetime: time.Hour,
- }}, &apiv1.CreateCertificateResponse{
- Certificate: testCrt,
- CertificateChain: []*x509.Certificate{testIssCrt},
- }, false},
- {"ok with provisioner", fields{jwk, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testCR,
- Template: testTemplate,
- Lifetime: time.Hour,
- Provisioner: &apiv1.ProvisionerInfo{ID: "provisioner-id", Type: "ACME"},
- }}, &apiv1.CreateCertificateResponse{
- Certificate: testCrt,
- CertificateChain: []*x509.Certificate{testIssCrt},
- }, false},
- {"ok with server cert", fields{jwk, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testCR,
- Template: testTemplate,
- Lifetime: time.Hour,
- IsCAServerCert: true,
- }}, &apiv1.CreateCertificateResponse{
- Certificate: testCrt,
- CertificateChain: []*x509.Certificate{testIssCrt},
- }, false},
- {"fail CSR", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: nil,
- Template: testTemplate,
- Lifetime: time.Hour,
- }}, nil, true},
- {"fail Template", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testCR,
- Template: nil,
- Lifetime: time.Hour,
- }}, nil, true},
- {"fail lifetime", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testCR,
- Lifetime: 0,
- }}, nil, true},
- {"fail sign token", fields{mockErrIssuer{}, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testCR,
- Lifetime: time.Hour,
- }}, nil, true},
- {"fail client sign", fields{x5c, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testFailCR,
- Lifetime: time.Hour,
- }}, nil, true},
- {"fail password", fields{x5cBad, client, testRootFingerprint}, args{&apiv1.CreateCertificateRequest{
- CSR: testCR,
- Lifetime: time.Hour,
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- s := &StepCAS{
- iss: tt.fields.iss,
- client: tt.fields.client,
- authorityID: "authority-id",
- fingerprint: tt.fields.fingerprint,
- }
- got, err := s.CreateCertificate(tt.args.req)
- if (err != nil) != tt.wantErr {
- t.Errorf("StepCAS.CreateCertificate() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("StepCAS.CreateCertificate() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestStepCAS_RenewCertificate(t *testing.T) {
- caURL, client := testCAHelper(t)
- jwk := testJWKIssuer(t, caURL, "")
- tokenIssuer := testX5CIssuer(t, caURL, "")
- token, err := tokenIssuer.SignToken("test", []string{"test.example.com"}, nil)
- if err != nil {
- t.Fatal(err)
- }
- type fields struct {
- iss stepIssuer
- client *ca.Client
- fingerprint string
- }
- type args struct {
- req *apiv1.RenewCertificateRequest
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *apiv1.RenewCertificateResponse
- wantErr bool
- }{
- {"ok", fields{jwk, client, testRootFingerprint}, args{&apiv1.RenewCertificateRequest{
- Template: &x509.Certificate{},
- Backdate: time.Minute,
- Lifetime: time.Hour,
- Token: token,
- }}, &apiv1.RenewCertificateResponse{
- Certificate: testCrt,
- CertificateChain: []*x509.Certificate{testIssCrt},
- }, false},
- {"fail no token", fields{jwk, client, testRootFingerprint}, args{&apiv1.RenewCertificateRequest{
- Template: &x509.Certificate{},
- Backdate: time.Minute,
- Lifetime: time.Hour,
- }}, nil, true},
- {"fail bad token", fields{jwk, client, testRootFingerprint}, args{&apiv1.RenewCertificateRequest{
- Template: &x509.Certificate{},
- Backdate: time.Minute,
- Lifetime: time.Hour,
- Token: "fail",
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- s := &StepCAS{
- iss: tt.fields.iss,
- client: tt.fields.client,
- fingerprint: tt.fields.fingerprint,
- }
- got, err := s.RenewCertificate(tt.args.req)
- if (err != nil) != tt.wantErr {
- t.Errorf("StepCAS.RenewCertificate() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Error(reflect.DeepEqual(got.Certificate, tt.want.Certificate))
- t.Error(reflect.DeepEqual(got.CertificateChain, tt.want.CertificateChain))
- t.Errorf("StepCAS.RenewCertificate() = %v, want %v", got.Certificate.Subject, tt.want.Certificate.Subject)
- }
- })
- }
- }
- func TestStepCAS_RevokeCertificate(t *testing.T) {
- caURL, client := testCAHelper(t)
- x5c := testX5CIssuer(t, caURL, "")
- jwk := testJWKIssuer(t, caURL, "")
- x5cEnc := testX5CIssuer(t, caURL, testPassword)
- jwkEnc := testJWKIssuer(t, caURL, testPassword)
- x5cBad := testX5CIssuer(t, caURL, "bad-password")
- type fields struct {
- iss stepIssuer
- client *ca.Client
- fingerprint string
- }
- type args struct {
- req *apiv1.RevokeCertificateRequest
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *apiv1.RevokeCertificateResponse
- wantErr bool
- }{
- {"ok serial number", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "ok",
- Certificate: nil,
- }}, &apiv1.RevokeCertificateResponse{}, false},
- {"ok certificate", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "",
- Certificate: testCrt,
- }}, &apiv1.RevokeCertificateResponse{
- Certificate: testCrt,
- }, false},
- {"ok both", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "ok",
- Certificate: testCrt,
- }}, &apiv1.RevokeCertificateResponse{
- Certificate: testCrt,
- }, false},
- {"ok with password", fields{x5cEnc, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "ok",
- Certificate: nil,
- }}, &apiv1.RevokeCertificateResponse{}, false},
- {"ok serial number jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "ok",
- Certificate: nil,
- }}, &apiv1.RevokeCertificateResponse{}, false},
- {"ok certificate jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "",
- Certificate: testCrt,
- }}, &apiv1.RevokeCertificateResponse{
- Certificate: testCrt,
- }, false},
- {"ok both jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "ok",
- Certificate: testCrt,
- }}, &apiv1.RevokeCertificateResponse{
- Certificate: testCrt,
- }, false},
- {"ok jwk with password", fields{jwkEnc, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "ok",
- Certificate: nil,
- }}, &apiv1.RevokeCertificateResponse{}, false},
- {"fail request", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "",
- Certificate: nil,
- }}, nil, true},
- {"fail revoke token", fields{mockErrIssuer{}, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "ok",
- }}, nil, true},
- {"fail client revoke", fields{x5c, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "fail",
- }}, nil, true},
- {"fail password", fields{x5cBad, client, testRootFingerprint}, args{&apiv1.RevokeCertificateRequest{
- SerialNumber: "ok",
- Certificate: nil,
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- s := &StepCAS{
- iss: tt.fields.iss,
- client: tt.fields.client,
- fingerprint: tt.fields.fingerprint,
- }
- got, err := s.RevokeCertificate(tt.args.req)
- if (err != nil) != tt.wantErr {
- t.Errorf("StepCAS.RevokeCertificate() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("StepCAS.RevokeCertificate() = %v, want %v", got, tt.want)
- }
- })
- }
- }
- func TestStepCAS_GetCertificateAuthority(t *testing.T) {
- caURL, client := testCAHelper(t)
- x5c := testX5CIssuer(t, caURL, "")
- jwk := testJWKIssuer(t, caURL, "")
- type fields struct {
- iss stepIssuer
- client *ca.Client
- fingerprint string
- }
- type args struct {
- req *apiv1.GetCertificateAuthorityRequest
- }
- tests := []struct {
- name string
- fields fields
- args args
- want *apiv1.GetCertificateAuthorityResponse
- wantErr bool
- }{
- {"ok", fields{x5c, client, testRootFingerprint}, args{&apiv1.GetCertificateAuthorityRequest{
- Name: caURL.String(),
- }}, &apiv1.GetCertificateAuthorityResponse{
- RootCertificate: testRootCrt,
- }, false},
- {"ok jwk", fields{jwk, client, testRootFingerprint}, args{&apiv1.GetCertificateAuthorityRequest{
- Name: caURL.String(),
- }}, &apiv1.GetCertificateAuthorityResponse{
- RootCertificate: testRootCrt,
- }, false},
- {"fail fingerprint", fields{x5c, client, "fail"}, args{&apiv1.GetCertificateAuthorityRequest{
- Name: caURL.String(),
- }}, nil, true},
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- s := &StepCAS{
- iss: tt.fields.iss,
- client: tt.fields.client,
- fingerprint: tt.fields.fingerprint,
- }
- got, err := s.GetCertificateAuthority(tt.args.req)
- if (err != nil) != tt.wantErr {
- t.Errorf("StepCAS.GetCertificateAuthority() error = %v, wantErr %v", err, tt.wantErr)
- return
- }
- if !reflect.DeepEqual(got, tt.want) {
- t.Errorf("StepCAS.GetCertificateAuthority() = %v, want %v", got, tt.want)
- }
- })
- }
- }
|