| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711 |
- package api
- import (
- "bytes"
- "context"
- "crypto/x509"
- "encoding/base64"
- "encoding/json"
- "fmt"
- "io"
- "net/http/httptest"
- "net/url"
- "reflect"
- "testing"
- "time"
- "github.com/go-chi/chi"
- "github.com/pkg/errors"
- "github.com/smallstep/assert"
- "github.com/smallstep/certificates/acme"
- "go.step.sm/crypto/pemutil"
- )
- func TestNewOrderRequest_Validate(t *testing.T) {
- type test struct {
- nor *NewOrderRequest
- nbf, naf time.Time
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/no-identifiers": func(t *testing.T) test {
- return test{
- nor: &NewOrderRequest{},
- err: acme.NewError(acme.ErrorMalformedType, "identifiers list cannot be empty"),
- }
- },
- "fail/bad-identifier": func(t *testing.T) test {
- return test{
- nor: &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "dns", Value: "example.com"},
- {Type: "foo", Value: "bar.com"},
- },
- },
- err: acme.NewError(acme.ErrorMalformedType, "identifier type unsupported: foo"),
- }
- },
- "fail/bad-ip": func(t *testing.T) test {
- nbf := time.Now().UTC().Add(time.Minute)
- naf := time.Now().UTC().Add(5 * time.Minute)
- return test{
- nor: &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "ip", Value: "192.168.42.1000"},
- },
- NotAfter: naf,
- NotBefore: nbf,
- },
- nbf: nbf,
- naf: naf,
- err: acme.NewError(acme.ErrorMalformedType, "invalid IP address: %s", "192.168.42.1000"),
- }
- },
- "ok": func(t *testing.T) test {
- nbf := time.Now().UTC().Add(time.Minute)
- naf := time.Now().UTC().Add(5 * time.Minute)
- return test{
- nor: &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "dns", Value: "example.com"},
- {Type: "dns", Value: "bar.com"},
- },
- NotAfter: naf,
- NotBefore: nbf,
- },
- nbf: nbf,
- naf: naf,
- }
- },
- "ok/ipv4": func(t *testing.T) test {
- nbf := time.Now().UTC().Add(time.Minute)
- naf := time.Now().UTC().Add(5 * time.Minute)
- return test{
- nor: &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "ip", Value: "192.168.42.42"},
- },
- NotAfter: naf,
- NotBefore: nbf,
- },
- nbf: nbf,
- naf: naf,
- }
- },
- "ok/ipv6": func(t *testing.T) test {
- nbf := time.Now().UTC().Add(time.Minute)
- naf := time.Now().UTC().Add(5 * time.Minute)
- return test{
- nor: &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "ip", Value: "2001:db8::1"},
- },
- NotAfter: naf,
- NotBefore: nbf,
- },
- nbf: nbf,
- naf: naf,
- }
- },
- "ok/mixed-dns-and-ipv4": func(t *testing.T) test {
- nbf := time.Now().UTC().Add(time.Minute)
- naf := time.Now().UTC().Add(5 * time.Minute)
- return test{
- nor: &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "dns", Value: "example.com"},
- {Type: "ip", Value: "192.168.42.42"},
- },
- NotAfter: naf,
- NotBefore: nbf,
- },
- nbf: nbf,
- naf: naf,
- }
- },
- "ok/mixed-ipv4-and-ipv6": func(t *testing.T) test {
- nbf := time.Now().UTC().Add(time.Minute)
- naf := time.Now().UTC().Add(5 * time.Minute)
- return test{
- nor: &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "ip", Value: "192.168.42.42"},
- {Type: "ip", Value: "2001:db8::1"},
- },
- NotAfter: naf,
- NotBefore: nbf,
- },
- nbf: nbf,
- naf: naf,
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- if err := tc.nor.Validate(); err != nil {
- if assert.NotNil(t, err) {
- ae, ok := err.(*acme.Error)
- assert.True(t, ok)
- assert.HasPrefix(t, ae.Error(), tc.err.Error())
- assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
- assert.Equals(t, ae.Type, tc.err.Type)
- }
- } else {
- if assert.Nil(t, tc.err) {
- if tc.nbf.IsZero() {
- assert.True(t, tc.nor.NotBefore.Before(time.Now().Add(time.Minute)))
- assert.True(t, tc.nor.NotBefore.After(time.Now().Add(-time.Minute)))
- } else {
- assert.Equals(t, tc.nor.NotBefore, tc.nbf)
- }
- if tc.naf.IsZero() {
- assert.True(t, tc.nor.NotAfter.Before(time.Now().Add(24*time.Hour)))
- assert.True(t, tc.nor.NotAfter.After(time.Now().Add(24*time.Hour-time.Minute)))
- } else {
- assert.Equals(t, tc.nor.NotAfter, tc.naf)
- }
- }
- }
- })
- }
- }
- func TestFinalizeRequestValidate(t *testing.T) {
- _csr, err := pemutil.Read("../../authority/testdata/certs/foo.csr")
- assert.FatalError(t, err)
- csr, ok := _csr.(*x509.CertificateRequest)
- assert.Fatal(t, ok)
- type test struct {
- fr *FinalizeRequest
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/parse-csr-error": func(t *testing.T) test {
- return test{
- fr: &FinalizeRequest{},
- err: acme.NewError(acme.ErrorMalformedType, "unable to parse csr: asn1: syntax error: sequence truncated"),
- }
- },
- "fail/invalid-csr-signature": func(t *testing.T) test {
- b, err := pemutil.Read("../../authority/testdata/certs/badsig.csr")
- assert.FatalError(t, err)
- c, ok := b.(*x509.CertificateRequest)
- assert.Fatal(t, ok)
- return test{
- fr: &FinalizeRequest{
- CSR: base64.RawURLEncoding.EncodeToString(c.Raw),
- },
- err: acme.NewError(acme.ErrorMalformedType, "csr failed signature check: x509: ECDSA verification failure"),
- }
- },
- "ok": func(t *testing.T) test {
- return test{
- fr: &FinalizeRequest{
- CSR: base64.RawURLEncoding.EncodeToString(csr.Raw),
- },
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- if err := tc.fr.Validate(); err != nil {
- if assert.NotNil(t, err) {
- ae, ok := err.(*acme.Error)
- assert.True(t, ok)
- assert.HasPrefix(t, ae.Error(), tc.err.Error())
- assert.Equals(t, ae.StatusCode(), tc.err.StatusCode())
- assert.Equals(t, ae.Type, tc.err.Type)
- }
- } else {
- if assert.Nil(t, tc.err) {
- assert.Equals(t, tc.fr.csr.Raw, csr.Raw)
- }
- }
- })
- }
- }
- func TestHandler_GetOrder(t *testing.T) {
- prov := newProv()
- escProvName := url.PathEscape(prov.GetName())
- baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
- now := clock.Now()
- nbf := now
- naf := now.Add(24 * time.Hour)
- expiry := now.Add(-time.Hour)
- o := acme.Order{
- ID: "orderID",
- NotBefore: nbf,
- NotAfter: naf,
- Identifiers: []acme.Identifier{
- {
- Type: "dns",
- Value: "example.com",
- },
- {
- Type: "dns",
- Value: "*.smallstep.com",
- },
- },
- ExpiresAt: expiry,
- Status: acme.StatusInvalid,
- Error: acme.NewError(acme.ErrorMalformedType, "order has expired"),
- AuthorizationURLs: []string{
- fmt.Sprintf("%s/acme/%s/authz/foo", baseURL.String(), escProvName),
- fmt.Sprintf("%s/acme/%s/authz/bar", baseURL.String(), escProvName),
- fmt.Sprintf("%s/acme/%s/authz/baz", baseURL.String(), escProvName),
- },
- FinalizeURL: fmt.Sprintf("%s/acme/%s/order/orderID/finalize", baseURL.String(), escProvName),
- }
- // Request with chi context
- chiCtx := chi.NewRouteContext()
- chiCtx.URLParams.Add("ordID", o.ID)
- u := fmt.Sprintf("%s/acme/%s/order/%s",
- baseURL.String(), escProvName, o.ID)
- type test struct {
- db acme.DB
- ctx context.Context
- statusCode int
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/no-account": func(t *testing.T) test {
- return test{
- ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/nil-account": func(t *testing.T) test {
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, nil)
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/no-provisioner": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), accContextKey, acc)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("provisioner does not exist"),
- }
- },
- "fail/nil-provisioner": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
- ctx = context.WithValue(ctx, accContextKey, acc)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("provisioner does not exist"),
- }
- },
- "fail/db.GetOrder-error": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{
- MockError: acme.NewErrorISE("force"),
- },
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("force"),
- }
- },
- "fail/account-id-mismatch": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{
- MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
- return &acme.Order{AccountID: "foo"}, nil
- },
- },
- ctx: ctx,
- statusCode: 401,
- err: acme.NewError(acme.ErrorUnauthorizedType, "account id mismatch"),
- }
- },
- "fail/provisioner-id-mismatch": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{
- MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
- return &acme.Order{AccountID: "accountID", ProvisionerID: "bar"}, nil
- },
- },
- ctx: ctx,
- statusCode: 401,
- err: acme.NewError(acme.ErrorUnauthorizedType, "provisioner id mismatch"),
- }
- },
- "fail/order-update-error": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{
- MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
- return &acme.Order{
- AccountID: "accountID",
- ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
- ExpiresAt: clock.Now().Add(-time.Hour),
- Status: acme.StatusReady,
- }, nil
- },
- MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
- return acme.NewErrorISE("force")
- },
- },
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("force"),
- }
- },
- "ok": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- return test{
- db: &acme.MockDB{
- MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
- return &acme.Order{
- ID: "orderID",
- AccountID: "accountID",
- ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
- ExpiresAt: expiry,
- Status: acme.StatusReady,
- AuthorizationIDs: []string{"foo", "bar", "baz"},
- NotBefore: nbf,
- NotAfter: naf,
- Identifiers: []acme.Identifier{
- {
- Type: "dns",
- Value: "example.com",
- },
- {
- Type: "dns",
- Value: "*.smallstep.com",
- },
- },
- }, nil
- },
- MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
- return nil
- },
- },
- ctx: ctx,
- statusCode: 200,
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db}
- req := httptest.NewRequest("GET", u, nil)
- req = req.WithContext(tc.ctx)
- w := httptest.NewRecorder()
- h.GetOrder(w, req)
- res := w.Result()
- assert.Equals(t, res.StatusCode, tc.statusCode)
- body, err := io.ReadAll(res.Body)
- res.Body.Close()
- assert.FatalError(t, err)
- if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
- var ae acme.Error
- assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
- assert.Equals(t, ae.Type, tc.err.Type)
- assert.Equals(t, ae.Detail, tc.err.Detail)
- assert.Equals(t, ae.Identifier, tc.err.Identifier)
- assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
- assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
- } else {
- expB, err := json.Marshal(o)
- assert.FatalError(t, err)
- assert.Equals(t, bytes.TrimSpace(body), expB)
- assert.Equals(t, res.Header["Location"], []string{u})
- assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
- }
- })
- }
- }
- func TestHandler_newAuthorization(t *testing.T) {
- type test struct {
- az *acme.Authorization
- db acme.DB
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/error-db.CreateChallenge": func(t *testing.T) test {
- az := &acme.Authorization{
- AccountID: "accID",
- Identifier: acme.Identifier{
- Type: "dns",
- Value: "zap.internal",
- },
- }
- return test{
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- assert.Equals(t, ch.AccountID, az.AccountID)
- assert.Equals(t, ch.Type, acme.DNS01)
- assert.Equals(t, ch.Token, az.Token)
- assert.Equals(t, ch.Status, acme.StatusPending)
- assert.Equals(t, ch.Value, az.Identifier.Value)
- return errors.New("force")
- },
- },
- az: az,
- err: acme.NewErrorISE("error creating challenge: force"),
- }
- },
- "fail/error-db.CreateAuthorization": func(t *testing.T) test {
- az := &acme.Authorization{
- AccountID: "accID",
- Identifier: acme.Identifier{
- Type: "dns",
- Value: "zap.internal",
- },
- Status: acme.StatusPending,
- ExpiresAt: clock.Now(),
- }
- count := 0
- var ch1, ch2, ch3 **acme.Challenge
- return test{
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- switch count {
- case 0:
- ch.ID = "dns"
- assert.Equals(t, ch.Type, acme.DNS01)
- ch1 = &ch
- case 1:
- ch.ID = "http"
- assert.Equals(t, ch.Type, acme.HTTP01)
- ch2 = &ch
- case 2:
- ch.ID = "tls"
- assert.Equals(t, ch.Type, acme.TLSALPN01)
- ch3 = &ch
- default:
- assert.FatalError(t, errors.New("test logic error"))
- return errors.New("force")
- }
- count++
- assert.Equals(t, ch.AccountID, az.AccountID)
- assert.Equals(t, ch.Token, az.Token)
- assert.Equals(t, ch.Status, acme.StatusPending)
- assert.Equals(t, ch.Value, az.Identifier.Value)
- return nil
- },
- MockCreateAuthorization: func(ctx context.Context, _az *acme.Authorization) error {
- assert.Equals(t, _az.AccountID, az.AccountID)
- assert.Equals(t, _az.Token, az.Token)
- assert.Equals(t, _az.Status, acme.StatusPending)
- assert.Equals(t, _az.Identifier, az.Identifier)
- assert.Equals(t, _az.ExpiresAt, az.ExpiresAt)
- assert.Equals(t, _az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
- assert.Equals(t, _az.Wildcard, false)
- return errors.New("force")
- },
- },
- az: az,
- err: acme.NewErrorISE("error creating authorization: force"),
- }
- },
- "ok/no-wildcard": func(t *testing.T) test {
- az := &acme.Authorization{
- AccountID: "accID",
- Identifier: acme.Identifier{
- Type: "dns",
- Value: "zap.internal",
- },
- Status: acme.StatusPending,
- ExpiresAt: clock.Now(),
- }
- count := 0
- var ch1, ch2, ch3 **acme.Challenge
- return test{
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- switch count {
- case 0:
- ch.ID = "dns"
- assert.Equals(t, ch.Type, acme.DNS01)
- ch1 = &ch
- case 1:
- ch.ID = "http"
- assert.Equals(t, ch.Type, acme.HTTP01)
- ch2 = &ch
- case 2:
- ch.ID = "tls"
- assert.Equals(t, ch.Type, acme.TLSALPN01)
- ch3 = &ch
- default:
- assert.FatalError(t, errors.New("test logic error"))
- return errors.New("force")
- }
- count++
- assert.Equals(t, ch.AccountID, az.AccountID)
- assert.Equals(t, ch.Token, az.Token)
- assert.Equals(t, ch.Status, acme.StatusPending)
- assert.Equals(t, ch.Value, az.Identifier.Value)
- return nil
- },
- MockCreateAuthorization: func(ctx context.Context, _az *acme.Authorization) error {
- assert.Equals(t, _az.AccountID, az.AccountID)
- assert.Equals(t, _az.Token, az.Token)
- assert.Equals(t, _az.Status, acme.StatusPending)
- assert.Equals(t, _az.Identifier, az.Identifier)
- assert.Equals(t, _az.ExpiresAt, az.ExpiresAt)
- assert.Equals(t, _az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
- assert.Equals(t, _az.Wildcard, false)
- return nil
- },
- },
- az: az,
- }
- },
- "ok/wildcard": func(t *testing.T) test {
- az := &acme.Authorization{
- AccountID: "accID",
- Identifier: acme.Identifier{
- Type: "dns",
- Value: "*.zap.internal",
- },
- Status: acme.StatusPending,
- ExpiresAt: clock.Now(),
- }
- var ch1 **acme.Challenge
- return test{
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- ch.ID = "dns"
- assert.Equals(t, ch.Type, acme.DNS01)
- assert.Equals(t, ch.AccountID, az.AccountID)
- assert.Equals(t, ch.Token, az.Token)
- assert.Equals(t, ch.Status, acme.StatusPending)
- assert.Equals(t, ch.Value, "zap.internal")
- ch1 = &ch
- return nil
- },
- MockCreateAuthorization: func(ctx context.Context, _az *acme.Authorization) error {
- assert.Equals(t, _az.AccountID, az.AccountID)
- assert.Equals(t, _az.Token, az.Token)
- assert.Equals(t, _az.Status, acme.StatusPending)
- assert.Equals(t, _az.Identifier, acme.Identifier{
- Type: "dns",
- Value: "zap.internal",
- })
- assert.Equals(t, _az.ExpiresAt, az.ExpiresAt)
- assert.Equals(t, _az.Challenges, []*acme.Challenge{*ch1})
- assert.Equals(t, _az.Wildcard, true)
- return nil
- },
- },
- az: az,
- }
- },
- }
- for name, run := range tests {
- t.Run(name, func(t *testing.T) {
- tc := run(t)
- h := &Handler{db: tc.db}
- if err := h.newAuthorization(context.Background(), tc.az); err != nil {
- if assert.NotNil(t, tc.err) {
- switch k := err.(type) {
- case *acme.Error:
- assert.Equals(t, k.Type, tc.err.Type)
- assert.Equals(t, k.Detail, tc.err.Detail)
- assert.Equals(t, k.Status, tc.err.Status)
- assert.Equals(t, k.Err.Error(), tc.err.Err.Error())
- assert.Equals(t, k.Detail, tc.err.Detail)
- default:
- assert.FatalError(t, errors.New("unexpected error type"))
- }
- }
- } else {
- assert.Nil(t, tc.err)
- }
- })
- }
- }
- func TestHandler_NewOrder(t *testing.T) {
- // Request with chi context
- prov := newProv()
- escProvName := url.PathEscape(prov.GetName())
- baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
- u := fmt.Sprintf("%s/acme/%s/order/ordID",
- baseURL.String(), escProvName)
- type test struct {
- db acme.DB
- ctx context.Context
- nor *NewOrderRequest
- statusCode int
- vr func(t *testing.T, o *acme.Order)
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/no-account": func(t *testing.T) test {
- return test{
- ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/nil-account": func(t *testing.T) test {
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, nil)
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/no-provisioner": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), accContextKey, acc)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("provisioner does not exist"),
- }
- },
- "fail/nil-provisioner": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
- ctx = context.WithValue(ctx, accContextKey, acc)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("provisioner does not exist"),
- }
- },
- "fail/no-payload": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), accContextKey, acc)
- ctx = context.WithValue(ctx, provisionerContextKey, prov)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("payload does not exist"),
- }
- },
- "fail/nil-payload": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, nil)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("paylod does not exist"),
- }
- },
- "fail/unmarshal-payload-error": func(t *testing.T) test {
- acc := &acme.Account{ID: "accID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal new-order request payload: unexpected end of JSON input"),
- }
- },
- "fail/malformed-payload-error": func(t *testing.T) test {
- acc := &acme.Account{ID: "accID"}
- fr := &NewOrderRequest{}
- b, err := json.Marshal(fr)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorMalformedType, "identifiers list cannot be empty"),
- }
- },
- "fail/error-h.newAuthorization": func(t *testing.T) test {
- acc := &acme.Account{ID: "accID"}
- fr := &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "dns", Value: "zap.internal"},
- },
- }
- b, err := json.Marshal(fr)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- return test{
- ctx: ctx,
- statusCode: 500,
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- assert.Equals(t, ch.AccountID, "accID")
- assert.Equals(t, ch.Type, acme.DNS01)
- assert.NotEquals(t, ch.Token, "")
- assert.Equals(t, ch.Status, acme.StatusPending)
- assert.Equals(t, ch.Value, "zap.internal")
- return errors.New("force")
- },
- },
- err: acme.NewErrorISE("error creating challenge: force"),
- }
- },
- "fail/error-db.CreateOrder": func(t *testing.T) test {
- acc := &acme.Account{ID: "accID"}
- fr := &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "dns", Value: "zap.internal"},
- },
- }
- b, err := json.Marshal(fr)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- var (
- ch1, ch2, ch3 **acme.Challenge
- az1ID *string
- count = 0
- )
- return test{
- ctx: ctx,
- statusCode: 500,
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- switch count {
- case 0:
- ch.ID = "dns"
- assert.Equals(t, ch.Type, acme.DNS01)
- ch1 = &ch
- case 1:
- ch.ID = "http"
- assert.Equals(t, ch.Type, acme.HTTP01)
- ch2 = &ch
- case 2:
- ch.ID = "tls"
- assert.Equals(t, ch.Type, acme.TLSALPN01)
- ch3 = &ch
- default:
- assert.FatalError(t, errors.New("test logic error"))
- return errors.New("force")
- }
- count++
- assert.Equals(t, ch.AccountID, "accID")
- assert.NotEquals(t, ch.Token, "")
- assert.Equals(t, ch.Status, acme.StatusPending)
- assert.Equals(t, ch.Value, "zap.internal")
- return nil
- },
- MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
- az.ID = "az1ID"
- az1ID = &az.ID
- assert.Equals(t, az.AccountID, "accID")
- assert.NotEquals(t, az.Token, "")
- assert.Equals(t, az.Status, acme.StatusPending)
- assert.Equals(t, az.Identifier, fr.Identifiers[0])
- assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
- assert.Equals(t, az.Wildcard, false)
- return nil
- },
- MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
- assert.Equals(t, o.AccountID, "accID")
- assert.Equals(t, o.ProvisionerID, prov.GetID())
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, fr.Identifiers)
- assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
- return errors.New("force")
- },
- },
- err: acme.NewErrorISE("error creating order: force"),
- }
- },
- "ok/multiple-authz": func(t *testing.T) test {
- acc := &acme.Account{ID: "accID"}
- nor := &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "dns", Value: "zap.internal"},
- {Type: "dns", Value: "*.zar.internal"},
- },
- }
- b, err := json.Marshal(nor)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- var (
- ch1, ch2, ch3, ch4 **acme.Challenge
- az1ID, az2ID *string
- chCount, azCount = 0, 0
- )
- return test{
- ctx: ctx,
- statusCode: 201,
- nor: nor,
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- switch chCount {
- case 0:
- ch.ID = "dns"
- assert.Equals(t, ch.Type, acme.DNS01)
- assert.Equals(t, ch.Value, "zap.internal")
- ch1 = &ch
- case 1:
- ch.ID = "http"
- assert.Equals(t, ch.Type, acme.HTTP01)
- assert.Equals(t, ch.Value, "zap.internal")
- ch2 = &ch
- case 2:
- ch.ID = "tls"
- assert.Equals(t, ch.Type, acme.TLSALPN01)
- assert.Equals(t, ch.Value, "zap.internal")
- ch3 = &ch
- case 3:
- ch.ID = "dns"
- assert.Equals(t, ch.Type, acme.DNS01)
- assert.Equals(t, ch.Value, "zar.internal")
- ch4 = &ch
- default:
- assert.FatalError(t, errors.New("test logic error"))
- return errors.New("force")
- }
- chCount++
- assert.Equals(t, ch.AccountID, "accID")
- assert.NotEquals(t, ch.Token, "")
- assert.Equals(t, ch.Status, acme.StatusPending)
- return nil
- },
- MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
- switch azCount {
- case 0:
- az.ID = "az1ID"
- az1ID = &az.ID
- assert.Equals(t, az.Identifier, nor.Identifiers[0])
- assert.Equals(t, az.Wildcard, false)
- assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
- case 1:
- az.ID = "az2ID"
- az2ID = &az.ID
- assert.Equals(t, az.Identifier, acme.Identifier{
- Type: acme.DNS,
- Value: "zar.internal",
- })
- assert.Equals(t, az.Wildcard, true)
- assert.Equals(t, az.Challenges, []*acme.Challenge{*ch4})
- default:
- assert.FatalError(t, errors.New("test logic error"))
- return errors.New("force")
- }
- azCount++
- assert.Equals(t, az.AccountID, "accID")
- assert.NotEquals(t, az.Token, "")
- assert.Equals(t, az.Status, acme.StatusPending)
- return nil
- },
- MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
- o.ID = "ordID"
- assert.Equals(t, o.AccountID, "accID")
- assert.Equals(t, o.ProvisionerID, prov.GetID())
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, nor.Identifiers)
- assert.Equals(t, o.AuthorizationIDs, []string{*az1ID, *az2ID})
- return nil
- },
- },
- vr: func(t *testing.T, o *acme.Order) {
- now := clock.Now()
- testBufferDur := 5 * time.Second
- orderExpiry := now.Add(defaultOrderExpiry)
- expNbf := now.Add(-defaultOrderBackdate)
- expNaf := now.Add(prov.DefaultTLSCertDuration())
- assert.Equals(t, o.ID, "ordID")
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, nor.Identifiers)
- assert.Equals(t, o.AuthorizationURLs, []string{
- fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName),
- fmt.Sprintf("%s/acme/%s/authz/az2ID", baseURL.String(), escProvName),
- })
- assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
- assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
- assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
- assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
- assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
- assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
- },
- }
- },
- "ok/default-naf-nbf": func(t *testing.T) test {
- acc := &acme.Account{ID: "accID"}
- nor := &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "dns", Value: "zap.internal"},
- },
- }
- b, err := json.Marshal(nor)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- var (
- ch1, ch2, ch3 **acme.Challenge
- az1ID *string
- count = 0
- )
- return test{
- ctx: ctx,
- statusCode: 201,
- nor: nor,
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- switch count {
- case 0:
- ch.ID = "dns"
- assert.Equals(t, ch.Type, acme.DNS01)
- ch1 = &ch
- case 1:
- ch.ID = "http"
- assert.Equals(t, ch.Type, acme.HTTP01)
- ch2 = &ch
- case 2:
- ch.ID = "tls"
- assert.Equals(t, ch.Type, acme.TLSALPN01)
- ch3 = &ch
- default:
- assert.FatalError(t, errors.New("test logic error"))
- return errors.New("force")
- }
- count++
- assert.Equals(t, ch.AccountID, "accID")
- assert.NotEquals(t, ch.Token, "")
- assert.Equals(t, ch.Status, acme.StatusPending)
- assert.Equals(t, ch.Value, "zap.internal")
- return nil
- },
- MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
- az.ID = "az1ID"
- az1ID = &az.ID
- assert.Equals(t, az.AccountID, "accID")
- assert.NotEquals(t, az.Token, "")
- assert.Equals(t, az.Status, acme.StatusPending)
- assert.Equals(t, az.Identifier, nor.Identifiers[0])
- assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
- assert.Equals(t, az.Wildcard, false)
- return nil
- },
- MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
- o.ID = "ordID"
- assert.Equals(t, o.AccountID, "accID")
- assert.Equals(t, o.ProvisionerID, prov.GetID())
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, nor.Identifiers)
- assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
- return nil
- },
- },
- vr: func(t *testing.T, o *acme.Order) {
- now := clock.Now()
- testBufferDur := 5 * time.Second
- orderExpiry := now.Add(defaultOrderExpiry)
- expNbf := now.Add(-defaultOrderBackdate)
- expNaf := now.Add(prov.DefaultTLSCertDuration())
- assert.Equals(t, o.ID, "ordID")
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, nor.Identifiers)
- assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
- assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
- assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
- assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
- assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
- assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
- assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
- },
- }
- },
- "ok/nbf-no-naf": func(t *testing.T) test {
- now := clock.Now()
- expNbf := now.Add(10 * time.Minute)
- acc := &acme.Account{ID: "accID"}
- nor := &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "dns", Value: "zap.internal"},
- },
- NotBefore: expNbf,
- }
- b, err := json.Marshal(nor)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- var (
- ch1, ch2, ch3 **acme.Challenge
- az1ID *string
- count = 0
- )
- return test{
- ctx: ctx,
- statusCode: 201,
- nor: nor,
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- switch count {
- case 0:
- ch.ID = "dns"
- assert.Equals(t, ch.Type, acme.DNS01)
- ch1 = &ch
- case 1:
- ch.ID = "http"
- assert.Equals(t, ch.Type, acme.HTTP01)
- ch2 = &ch
- case 2:
- ch.ID = "tls"
- assert.Equals(t, ch.Type, acme.TLSALPN01)
- ch3 = &ch
- default:
- assert.FatalError(t, errors.New("test logic error"))
- return errors.New("force")
- }
- count++
- assert.Equals(t, ch.AccountID, "accID")
- assert.NotEquals(t, ch.Token, "")
- assert.Equals(t, ch.Status, acme.StatusPending)
- assert.Equals(t, ch.Value, "zap.internal")
- return nil
- },
- MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
- az.ID = "az1ID"
- az1ID = &az.ID
- assert.Equals(t, az.AccountID, "accID")
- assert.NotEquals(t, az.Token, "")
- assert.Equals(t, az.Status, acme.StatusPending)
- assert.Equals(t, az.Identifier, nor.Identifiers[0])
- assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
- assert.Equals(t, az.Wildcard, false)
- return nil
- },
- MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
- o.ID = "ordID"
- assert.Equals(t, o.AccountID, "accID")
- assert.Equals(t, o.ProvisionerID, prov.GetID())
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, nor.Identifiers)
- assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
- return nil
- },
- },
- vr: func(t *testing.T, o *acme.Order) {
- now := clock.Now()
- testBufferDur := 5 * time.Second
- orderExpiry := now.Add(defaultOrderExpiry)
- expNaf := expNbf.Add(prov.DefaultTLSCertDuration())
- assert.Equals(t, o.ID, "ordID")
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, nor.Identifiers)
- assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
- assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
- assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
- assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
- assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
- assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
- assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
- },
- }
- },
- "ok/naf-no-nbf": func(t *testing.T) test {
- now := clock.Now()
- expNaf := now.Add(15 * time.Minute)
- acc := &acme.Account{ID: "accID"}
- nor := &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "dns", Value: "zap.internal"},
- },
- NotAfter: expNaf,
- }
- b, err := json.Marshal(nor)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- var (
- ch1, ch2, ch3 **acme.Challenge
- az1ID *string
- count = 0
- )
- return test{
- ctx: ctx,
- statusCode: 201,
- nor: nor,
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- switch count {
- case 0:
- ch.ID = "dns"
- assert.Equals(t, ch.Type, acme.DNS01)
- ch1 = &ch
- case 1:
- ch.ID = "http"
- assert.Equals(t, ch.Type, acme.HTTP01)
- ch2 = &ch
- case 2:
- ch.ID = "tls"
- assert.Equals(t, ch.Type, acme.TLSALPN01)
- ch3 = &ch
- default:
- assert.FatalError(t, errors.New("test logic error"))
- return errors.New("force")
- }
- count++
- assert.Equals(t, ch.AccountID, "accID")
- assert.NotEquals(t, ch.Token, "")
- assert.Equals(t, ch.Status, acme.StatusPending)
- assert.Equals(t, ch.Value, "zap.internal")
- return nil
- },
- MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
- az.ID = "az1ID"
- az1ID = &az.ID
- assert.Equals(t, az.AccountID, "accID")
- assert.NotEquals(t, az.Token, "")
- assert.Equals(t, az.Status, acme.StatusPending)
- assert.Equals(t, az.Identifier, nor.Identifiers[0])
- assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
- assert.Equals(t, az.Wildcard, false)
- return nil
- },
- MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
- o.ID = "ordID"
- assert.Equals(t, o.AccountID, "accID")
- assert.Equals(t, o.ProvisionerID, prov.GetID())
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, nor.Identifiers)
- assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
- return nil
- },
- },
- vr: func(t *testing.T, o *acme.Order) {
- testBufferDur := 5 * time.Second
- orderExpiry := now.Add(defaultOrderExpiry)
- expNbf := now.Add(-defaultOrderBackdate)
- assert.Equals(t, o.ID, "ordID")
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, nor.Identifiers)
- assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
- assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
- assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
- assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
- assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
- assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
- assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
- },
- }
- },
- "ok/naf-nbf": func(t *testing.T) test {
- now := clock.Now()
- expNbf := now.Add(5 * time.Minute)
- expNaf := now.Add(15 * time.Minute)
- acc := &acme.Account{ID: "accID"}
- nor := &NewOrderRequest{
- Identifiers: []acme.Identifier{
- {Type: "dns", Value: "zap.internal"},
- },
- NotBefore: expNbf,
- NotAfter: expNaf,
- }
- b, err := json.Marshal(nor)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- var (
- ch1, ch2, ch3 **acme.Challenge
- az1ID *string
- count = 0
- )
- return test{
- ctx: ctx,
- statusCode: 201,
- nor: nor,
- db: &acme.MockDB{
- MockCreateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
- switch count {
- case 0:
- ch.ID = "dns"
- assert.Equals(t, ch.Type, acme.DNS01)
- ch1 = &ch
- case 1:
- ch.ID = "http"
- assert.Equals(t, ch.Type, acme.HTTP01)
- ch2 = &ch
- case 2:
- ch.ID = "tls"
- assert.Equals(t, ch.Type, acme.TLSALPN01)
- ch3 = &ch
- default:
- assert.FatalError(t, errors.New("test logic error"))
- return errors.New("force")
- }
- count++
- assert.Equals(t, ch.AccountID, "accID")
- assert.NotEquals(t, ch.Token, "")
- assert.Equals(t, ch.Status, acme.StatusPending)
- assert.Equals(t, ch.Value, "zap.internal")
- return nil
- },
- MockCreateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
- az.ID = "az1ID"
- az1ID = &az.ID
- assert.Equals(t, az.AccountID, "accID")
- assert.NotEquals(t, az.Token, "")
- assert.Equals(t, az.Status, acme.StatusPending)
- assert.Equals(t, az.Identifier, nor.Identifiers[0])
- assert.Equals(t, az.Challenges, []*acme.Challenge{*ch1, *ch2, *ch3})
- assert.Equals(t, az.Wildcard, false)
- return nil
- },
- MockCreateOrder: func(ctx context.Context, o *acme.Order) error {
- o.ID = "ordID"
- assert.Equals(t, o.AccountID, "accID")
- assert.Equals(t, o.ProvisionerID, prov.GetID())
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, nor.Identifiers)
- assert.Equals(t, o.AuthorizationIDs, []string{*az1ID})
- return nil
- },
- },
- vr: func(t *testing.T, o *acme.Order) {
- testBufferDur := 5 * time.Second
- orderExpiry := now.Add(defaultOrderExpiry)
- assert.Equals(t, o.ID, "ordID")
- assert.Equals(t, o.Status, acme.StatusPending)
- assert.Equals(t, o.Identifiers, nor.Identifiers)
- assert.Equals(t, o.AuthorizationURLs, []string{fmt.Sprintf("%s/acme/%s/authz/az1ID", baseURL.String(), escProvName)})
- assert.True(t, o.NotBefore.Add(-testBufferDur).Before(expNbf))
- assert.True(t, o.NotBefore.Add(testBufferDur).After(expNbf))
- assert.True(t, o.NotAfter.Add(-testBufferDur).Before(expNaf))
- assert.True(t, o.NotAfter.Add(testBufferDur).After(expNaf))
- assert.True(t, o.ExpiresAt.Add(-testBufferDur).Before(orderExpiry))
- assert.True(t, o.ExpiresAt.Add(testBufferDur).After(orderExpiry))
- },
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db}
- req := httptest.NewRequest("GET", u, nil)
- req = req.WithContext(tc.ctx)
- w := httptest.NewRecorder()
- h.NewOrder(w, req)
- res := w.Result()
- assert.Equals(t, res.StatusCode, tc.statusCode)
- body, err := io.ReadAll(res.Body)
- res.Body.Close()
- assert.FatalError(t, err)
- if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
- var ae acme.Error
- assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
- assert.Equals(t, ae.Type, tc.err.Type)
- assert.Equals(t, ae.Detail, tc.err.Detail)
- assert.Equals(t, ae.Identifier, tc.err.Identifier)
- assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
- assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
- } else {
- ro := new(acme.Order)
- assert.FatalError(t, json.Unmarshal(body, ro))
- if tc.vr != nil {
- tc.vr(t, ro)
- }
- assert.Equals(t, res.Header["Location"], []string{u})
- assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
- }
- })
- }
- }
- func TestHandler_FinalizeOrder(t *testing.T) {
- prov := newProv()
- escProvName := url.PathEscape(prov.GetName())
- baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
- now := clock.Now()
- nbf := now
- naf := now.Add(24 * time.Hour)
- o := acme.Order{
- ID: "orderID",
- NotBefore: nbf,
- NotAfter: naf,
- Identifiers: []acme.Identifier{
- {
- Type: "dns",
- Value: "example.com",
- },
- {
- Type: "dns",
- Value: "*.smallstep.com",
- },
- },
- ExpiresAt: naf,
- Status: acme.StatusValid,
- AuthorizationURLs: []string{
- fmt.Sprintf("%s/acme/%s/authz/foo", baseURL.String(), escProvName),
- fmt.Sprintf("%s/acme/%s/authz/bar", baseURL.String(), escProvName),
- fmt.Sprintf("%s/acme/%s/authz/baz", baseURL.String(), escProvName),
- },
- FinalizeURL: fmt.Sprintf("%s/acme/%s/order/orderID/finalize", baseURL.String(), escProvName),
- CertificateURL: fmt.Sprintf("%s/acme/%s/certificate/certID", baseURL.String(), escProvName),
- }
- // Request with chi context
- chiCtx := chi.NewRouteContext()
- chiCtx.URLParams.Add("ordID", o.ID)
- u := fmt.Sprintf("%s/acme/%s/order/%s",
- baseURL.String(), escProvName, o.ID)
- _csr, err := pemutil.Read("../../authority/testdata/certs/foo.csr")
- assert.FatalError(t, err)
- csr, ok := _csr.(*x509.CertificateRequest)
- assert.Fatal(t, ok)
- nor := &FinalizeRequest{
- CSR: base64.RawURLEncoding.EncodeToString(csr.Raw),
- }
- payloadBytes, err := json.Marshal(nor)
- assert.FatalError(t, err)
- type test struct {
- db acme.DB
- ctx context.Context
- statusCode int
- err *acme.Error
- }
- var tests = map[string]func(t *testing.T) test{
- "fail/no-account": func(t *testing.T) test {
- return test{
- ctx: context.WithValue(context.Background(), provisionerContextKey, prov),
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/nil-account": func(t *testing.T) test {
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, nil)
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
- }
- },
- "fail/no-provisioner": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), accContextKey, acc)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("provisioner does not exist"),
- }
- },
- "fail/nil-provisioner": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, nil)
- ctx = context.WithValue(ctx, accContextKey, acc)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("provisioner does not exist"),
- }
- },
- "fail/no-payload": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), accContextKey, acc)
- ctx = context.WithValue(ctx, provisionerContextKey, prov)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("payload does not exist"),
- }
- },
- "fail/nil-payload": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, nil)
- return test{
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("paylod does not exist"),
- }
- },
- "fail/unmarshal-payload-error": func(t *testing.T) test {
- acc := &acme.Account{ID: "accID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{})
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorMalformedType, "failed to unmarshal finalize-order request payload: unexpected end of JSON input"),
- }
- },
- "fail/malformed-payload-error": func(t *testing.T) test {
- acc := &acme.Account{ID: "accID"}
- fr := &FinalizeRequest{}
- b, err := json.Marshal(fr)
- assert.FatalError(t, err)
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: b})
- return test{
- ctx: ctx,
- statusCode: 400,
- err: acme.NewError(acme.ErrorMalformedType, "unable to parse csr: asn1: syntax error: sequence truncated"),
- }
- },
- "fail/db.GetOrder-error": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{
- MockError: acme.NewErrorISE("force"),
- },
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("force"),
- }
- },
- "fail/account-id-mismatch": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{
- MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
- return &acme.Order{AccountID: "foo"}, nil
- },
- },
- ctx: ctx,
- statusCode: 401,
- err: acme.NewError(acme.ErrorUnauthorizedType, "account id mismatch"),
- }
- },
- "fail/provisioner-id-mismatch": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{
- MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
- return &acme.Order{AccountID: "accountID", ProvisionerID: "bar"}, nil
- },
- },
- ctx: ctx,
- statusCode: 401,
- err: acme.NewError(acme.ErrorUnauthorizedType, "provisioner id mismatch"),
- }
- },
- "fail/order-finalize-error": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{
- MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
- return &acme.Order{
- AccountID: "accountID",
- ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
- ExpiresAt: clock.Now().Add(-time.Hour),
- Status: acme.StatusReady,
- }, nil
- },
- MockUpdateOrder: func(ctx context.Context, o *acme.Order) error {
- return acme.NewErrorISE("force")
- },
- },
- ctx: ctx,
- statusCode: 500,
- err: acme.NewErrorISE("force"),
- }
- },
- "ok": func(t *testing.T) test {
- acc := &acme.Account{ID: "accountID"}
- ctx := context.WithValue(context.Background(), provisionerContextKey, prov)
- ctx = context.WithValue(ctx, accContextKey, acc)
- ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{value: payloadBytes})
- ctx = context.WithValue(ctx, baseURLContextKey, baseURL)
- ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
- return test{
- db: &acme.MockDB{
- MockGetOrder: func(ctx context.Context, id string) (*acme.Order, error) {
- return &acme.Order{
- ID: "orderID",
- AccountID: "accountID",
- ProvisionerID: fmt.Sprintf("acme/%s", prov.GetName()),
- ExpiresAt: naf,
- Status: acme.StatusValid,
- AuthorizationIDs: []string{"foo", "bar", "baz"},
- NotBefore: nbf,
- NotAfter: naf,
- Identifiers: []acme.Identifier{
- {
- Type: "dns",
- Value: "example.com",
- },
- {
- Type: "dns",
- Value: "*.smallstep.com",
- },
- },
- CertificateID: "certID",
- }, nil
- },
- },
- ctx: ctx,
- statusCode: 200,
- }
- },
- }
- for name, run := range tests {
- tc := run(t)
- t.Run(name, func(t *testing.T) {
- h := &Handler{linker: NewLinker("dns", "acme"), db: tc.db}
- req := httptest.NewRequest("GET", u, nil)
- req = req.WithContext(tc.ctx)
- w := httptest.NewRecorder()
- h.FinalizeOrder(w, req)
- res := w.Result()
- assert.Equals(t, res.StatusCode, tc.statusCode)
- body, err := io.ReadAll(res.Body)
- res.Body.Close()
- assert.FatalError(t, err)
- if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
- var ae acme.Error
- assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
- assert.Equals(t, ae.Type, tc.err.Type)
- assert.Equals(t, ae.Detail, tc.err.Detail)
- assert.Equals(t, ae.Identifier, tc.err.Identifier)
- assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
- assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
- } else {
- expB, err := json.Marshal(o)
- assert.FatalError(t, err)
- ro := new(acme.Order)
- assert.FatalError(t, json.Unmarshal(body, ro))
- assert.Equals(t, bytes.TrimSpace(body), expB)
- assert.Equals(t, res.Header["Location"], []string{u})
- assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
- }
- })
- }
- }
- func TestHandler_challengeTypes(t *testing.T) {
- type args struct {
- az *acme.Authorization
- }
- tests := []struct {
- name string
- args args
- want []acme.ChallengeType
- }{
- {
- name: "ok/dns",
- args: args{
- az: &acme.Authorization{
- Identifier: acme.Identifier{Type: "dns", Value: "example.com"},
- Wildcard: false,
- },
- },
- want: []acme.ChallengeType{acme.DNS01, acme.HTTP01, acme.TLSALPN01},
- },
- {
- name: "ok/wildcard",
- args: args{
- az: &acme.Authorization{
- Identifier: acme.Identifier{Type: "dns", Value: "*.example.com"},
- Wildcard: true,
- },
- },
- want: []acme.ChallengeType{acme.DNS01},
- },
- {
- name: "ok/ip",
- args: args{
- az: &acme.Authorization{
- Identifier: acme.Identifier{Type: "ip", Value: "192.168.42.42"},
- Wildcard: false,
- },
- },
- want: []acme.ChallengeType{acme.HTTP01, acme.TLSALPN01},
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- if got := challengeTypes(tt.args.az); !reflect.DeepEqual(got, tt.want) {
- t.Errorf("Handler.challengeTypes() = %v, want %v", got, tt.want)
- }
- })
- }
- }
|