2019-05-27 00:41:10 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-05-03 00:35:35 +00:00
|
|
|
"crypto/tls"
|
2021-03-11 07:05:46 +00:00
|
|
|
"crypto/x509"
|
2019-05-27 00:41:10 +00:00
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
2021-11-12 23:46:34 +00:00
|
|
|
"io"
|
2021-03-11 21:10:14 +00:00
|
|
|
"net/http"
|
2019-05-27 00:41:10 +00:00
|
|
|
"net/http/httptest"
|
2020-05-07 03:18:12 +00:00
|
|
|
"net/url"
|
2019-05-27 00:41:10 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2021-12-08 14:19:38 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2021-03-11 21:10:14 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-11-07 14:35:42 +00:00
|
|
|
|
2021-03-11 21:10:14 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2020-08-14 22:33:50 +00:00
|
|
|
"go.step.sm/crypto/pemutil"
|
2022-11-07 14:35:42 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/assert"
|
|
|
|
"github.com/smallstep/certificates/acme"
|
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
2019-05-27 00:41:10 +00:00
|
|
|
)
|
|
|
|
|
2022-05-03 00:35:35 +00:00
|
|
|
type mockClient struct {
|
|
|
|
get func(url string) (*http.Response, error)
|
|
|
|
lookupTxt func(name string) ([]string, error)
|
|
|
|
tlsDial func(network, addr string, config *tls.Config) (*tls.Conn, error)
|
|
|
|
}
|
|
|
|
|
2022-05-03 01:47:47 +00:00
|
|
|
func (m *mockClient) Get(u string) (*http.Response, error) { return m.get(u) }
|
2022-05-03 00:35:35 +00:00
|
|
|
func (m *mockClient) LookupTxt(name string) ([]string, error) { return m.lookupTxt(name) }
|
|
|
|
func (m *mockClient) TLSDial(network, addr string, config *tls.Config) (*tls.Conn, error) {
|
|
|
|
return m.tlsDial(network, addr, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mockMustAuthority(t *testing.T, a acme.CertificateAuthority) {
|
|
|
|
t.Helper()
|
|
|
|
fn := mustAuthority
|
|
|
|
t.Cleanup(func() {
|
|
|
|
mustAuthority = fn
|
|
|
|
})
|
|
|
|
mustAuthority = func(ctx context.Context) acme.CertificateAuthority {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
func TestHandler_GetNonce(t *testing.T) {
|
2019-05-27 00:41:10 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
statusCode int
|
|
|
|
}{
|
|
|
|
{"GET", 204},
|
|
|
|
{"HEAD", 200},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request with chi context
|
|
|
|
req := httptest.NewRequest("GET", "http://ca.smallstep.com/nonce", nil)
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2022-04-28 02:08:16 +00:00
|
|
|
// h := &Handler{}
|
2019-05-27 00:41:10 +00:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req.Method = tt.name
|
2022-04-28 02:08:16 +00:00
|
|
|
GetNonce(w, req)
|
2019-05-27 00:41:10 +00:00
|
|
|
res := w.Result()
|
|
|
|
|
|
|
|
if res.StatusCode != tt.statusCode {
|
|
|
|
t.Errorf("Handler.GetNonce StatusCode = %d, wants %d", res.StatusCode, tt.statusCode)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
func TestHandler_GetDirectory(t *testing.T) {
|
2022-05-03 00:35:35 +00:00
|
|
|
linker := acme.NewLinker("ca.smallstep.com", "acme")
|
2022-04-28 02:08:16 +00:00
|
|
|
_ = linker
|
2019-05-27 00:41:10 +00:00
|
|
|
type test struct {
|
2021-12-08 14:19:38 +00:00
|
|
|
ctx context.Context
|
2019-05-27 00:41:10 +00:00
|
|
|
statusCode int
|
2021-12-08 14:19:38 +00:00
|
|
|
dir Directory
|
2021-03-09 06:35:57 +00:00
|
|
|
err *acme.Error
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
var tests = map[string]func(t *testing.T) test{
|
2021-12-08 14:19:38 +00:00
|
|
|
"fail/no-provisioner": func(t *testing.T) test {
|
|
|
|
return test{
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx: context.Background(),
|
2021-12-08 14:19:38 +00:00
|
|
|
statusCode: 500,
|
2022-05-03 00:35:35 +00:00
|
|
|
err: acme.NewErrorISE("provisioner is not in context"),
|
2021-12-08 14:19:38 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/different-provisioner": func(t *testing.T) test {
|
2022-05-06 20:21:41 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), &fakeProvisioner{})
|
2021-12-08 14:19:38 +00:00
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 500,
|
|
|
|
err: acme.NewErrorISE("provisioner in context is not an ACME provisioner"),
|
|
|
|
}
|
|
|
|
},
|
2019-05-27 00:41:10 +00:00
|
|
|
"ok": func(t *testing.T) test {
|
2021-12-08 14:19:38 +00:00
|
|
|
prov := newProv()
|
|
|
|
provName := url.PathEscape(prov.GetName())
|
|
|
|
baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-12-08 14:19:38 +00:00
|
|
|
expDir := Directory{
|
|
|
|
NewNonce: fmt.Sprintf("%s/acme/%s/new-nonce", baseURL.String(), provName),
|
|
|
|
NewAccount: fmt.Sprintf("%s/acme/%s/new-account", baseURL.String(), provName),
|
|
|
|
NewOrder: fmt.Sprintf("%s/acme/%s/new-order", baseURL.String(), provName),
|
|
|
|
RevokeCert: fmt.Sprintf("%s/acme/%s/revoke-cert", baseURL.String(), provName),
|
|
|
|
KeyChange: fmt.Sprintf("%s/acme/%s/key-change", baseURL.String(), provName),
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
dir: expDir,
|
|
|
|
statusCode: 200,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/eab-required": func(t *testing.T) test {
|
|
|
|
prov := newACMEProv(t)
|
|
|
|
prov.RequireEAB = true
|
|
|
|
provName := url.PathEscape(prov.GetName())
|
|
|
|
baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-12-08 14:19:38 +00:00
|
|
|
expDir := Directory{
|
|
|
|
NewNonce: fmt.Sprintf("%s/acme/%s/new-nonce", baseURL.String(), provName),
|
|
|
|
NewAccount: fmt.Sprintf("%s/acme/%s/new-account", baseURL.String(), provName),
|
|
|
|
NewOrder: fmt.Sprintf("%s/acme/%s/new-order", baseURL.String(), provName),
|
|
|
|
RevokeCert: fmt.Sprintf("%s/acme/%s/revoke-cert", baseURL.String(), provName),
|
|
|
|
KeyChange: fmt.Sprintf("%s/acme/%s/key-change", baseURL.String(), provName),
|
2022-10-24 12:14:28 +00:00
|
|
|
Meta: &Meta{
|
2021-12-08 14:19:38 +00:00
|
|
|
ExternalAccountRequired: true,
|
|
|
|
},
|
|
|
|
}
|
2019-05-27 00:41:10 +00:00
|
|
|
return test{
|
2021-12-08 14:19:38 +00:00
|
|
|
ctx: ctx,
|
|
|
|
dir: expDir,
|
2019-05-27 00:41:10 +00:00
|
|
|
statusCode: 200,
|
|
|
|
}
|
|
|
|
},
|
2022-11-07 14:35:42 +00:00
|
|
|
"ok/full-meta": func(t *testing.T) test {
|
|
|
|
prov := newACMEProv(t)
|
|
|
|
prov.TermsOfService = "https://terms.ca.local/"
|
|
|
|
prov.Website = "https://ca.local/"
|
|
|
|
prov.CaaIdentities = []string{"ca.local"}
|
|
|
|
prov.RequireEAB = true
|
|
|
|
provName := url.PathEscape(prov.GetName())
|
|
|
|
baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
|
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
|
|
|
expDir := Directory{
|
|
|
|
NewNonce: fmt.Sprintf("%s/acme/%s/new-nonce", baseURL.String(), provName),
|
|
|
|
NewAccount: fmt.Sprintf("%s/acme/%s/new-account", baseURL.String(), provName),
|
|
|
|
NewOrder: fmt.Sprintf("%s/acme/%s/new-order", baseURL.String(), provName),
|
|
|
|
RevokeCert: fmt.Sprintf("%s/acme/%s/revoke-cert", baseURL.String(), provName),
|
|
|
|
KeyChange: fmt.Sprintf("%s/acme/%s/key-change", baseURL.String(), provName),
|
|
|
|
Meta: &Meta{
|
|
|
|
TermsOfService: "https://terms.ca.local/",
|
|
|
|
Website: "https://ca.local/",
|
|
|
|
CaaIdentities: []string{"ca.local"},
|
|
|
|
ExternalAccountRequired: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
dir: expDir,
|
|
|
|
statusCode: 200,
|
|
|
|
}
|
|
|
|
},
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
tc := run(t)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewLinkerContext(tc.ctx, acme.NewLinker("test.ca.smallstep.com", "acme"))
|
2020-05-07 03:18:12 +00:00
|
|
|
req := httptest.NewRequest("GET", "/foo/bar", nil)
|
2022-05-03 00:35:35 +00:00
|
|
|
req = req.WithContext(ctx)
|
2019-05-27 00:41:10 +00:00
|
|
|
w := httptest.NewRecorder()
|
2022-04-28 02:08:16 +00:00
|
|
|
GetDirectory(w, req)
|
2019-05-27 00:41:10 +00:00
|
|
|
res := w.Result()
|
|
|
|
|
|
|
|
assert.Equals(t, res.StatusCode, tc.statusCode)
|
|
|
|
|
2021-11-12 23:46:34 +00:00
|
|
|
body, err := io.ReadAll(res.Body)
|
2019-05-27 00:41:10 +00:00
|
|
|
res.Body.Close()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
|
|
|
|
var ae acme.Error
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
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)
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
|
|
|
|
} else {
|
2021-03-09 06:35:57 +00:00
|
|
|
var dir Directory
|
2019-05-27 00:41:10 +00:00
|
|
|
json.Unmarshal(bytes.TrimSpace(body), &dir)
|
2021-12-08 14:19:38 +00:00
|
|
|
if !cmp.Equal(tc.dir, dir) {
|
|
|
|
t.Errorf("GetDirectory() diff =\n%s", cmp.Diff(tc.dir, dir))
|
|
|
|
}
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:59:02 +00:00
|
|
|
func TestHandler_GetAuthorization(t *testing.T) {
|
2019-05-27 00:41:10 +00:00
|
|
|
expiry := time.Now().UTC().Add(6 * time.Hour)
|
2021-03-09 06:35:57 +00:00
|
|
|
az := acme.Authorization{
|
2021-03-11 07:59:02 +00:00
|
|
|
ID: "authzID",
|
|
|
|
AccountID: "accID",
|
2019-05-27 00:41:10 +00:00
|
|
|
Identifier: acme.Identifier{
|
|
|
|
Type: "dns",
|
|
|
|
Value: "example.com",
|
|
|
|
},
|
2021-03-19 21:37:45 +00:00
|
|
|
Status: "pending",
|
|
|
|
ExpiresAt: expiry,
|
|
|
|
Wildcard: false,
|
2019-05-27 00:41:10 +00:00
|
|
|
Challenges: []*acme.Challenge{
|
|
|
|
{
|
2021-03-25 07:23:57 +00:00
|
|
|
Type: "http-01",
|
|
|
|
Status: "pending",
|
|
|
|
Token: "tok2",
|
|
|
|
URL: "https://ca.smallstep.com/acme/challenge/chHTTPID",
|
|
|
|
ID: "chHTTP01ID",
|
2019-05-27 00:41:10 +00:00
|
|
|
},
|
|
|
|
{
|
2021-03-25 07:23:57 +00:00
|
|
|
Type: "dns-01",
|
|
|
|
Status: "pending",
|
|
|
|
Token: "tok2",
|
|
|
|
URL: "https://ca.smallstep.com/acme/challenge/chDNSID",
|
|
|
|
ID: "chDNSID",
|
2019-05-27 00:41:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
prov := newProv()
|
2020-05-07 03:18:12 +00:00
|
|
|
provName := url.PathEscape(prov.GetName())
|
|
|
|
baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
|
2019-05-27 00:41:10 +00:00
|
|
|
|
|
|
|
// Request with chi context
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("authzID", az.ID)
|
2021-10-08 18:59:57 +00:00
|
|
|
u := fmt.Sprintf("%s/acme/%s/authz/%s",
|
2020-05-07 03:18:12 +00:00
|
|
|
baseURL.String(), provName, az.ID)
|
2019-05-27 00:41:10 +00:00
|
|
|
|
|
|
|
type test struct {
|
2021-03-09 06:35:57 +00:00
|
|
|
db acme.DB
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx context.Context
|
|
|
|
statusCode int
|
2021-03-09 06:35:57 +00:00
|
|
|
err *acme.Error
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
var tests = map[string]func(t *testing.T) test{
|
|
|
|
"fail/no-account": func(t *testing.T) test {
|
|
|
|
return test{
|
2021-03-09 06:35:57 +00:00
|
|
|
db: &acme.MockDB{},
|
|
|
|
ctx: context.Background(),
|
2020-02-02 01:35:41 +00:00
|
|
|
statusCode: 400,
|
2021-03-09 06:35:57 +00:00
|
|
|
err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/nil-account": func(t *testing.T) test {
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx = context.WithValue(ctx, accContextKey, nil)
|
2019-05-27 00:41:10 +00:00
|
|
|
return test{
|
2021-03-09 06:35:57 +00:00
|
|
|
db: &acme.MockDB{},
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx: ctx,
|
2020-02-02 01:35:41 +00:00
|
|
|
statusCode: 400,
|
2021-03-09 06:35:57 +00:00
|
|
|
err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
},
|
2021-03-11 07:59:02 +00:00
|
|
|
"fail/db.GetAuthorization-error": func(t *testing.T) test {
|
2019-05-27 00:41:10 +00:00
|
|
|
acc := &acme.Account{ID: "accID"}
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx := context.WithValue(context.Background(), accContextKey, acc)
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
2021-03-09 06:35:57 +00:00
|
|
|
db: &acme.MockDB{
|
|
|
|
MockError: acme.NewErrorISE("force"),
|
2019-05-27 00:41:10 +00:00
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 500,
|
2021-03-09 06:35:57 +00:00
|
|
|
err: acme.NewErrorISE("force"),
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
},
|
2021-03-11 07:59:02 +00:00
|
|
|
"fail/account-id-mismatch": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
|
|
|
ctx := context.WithValue(context.Background(), accContextKey, acc)
|
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetAuthorization: func(ctx context.Context, id string) (*acme.Authorization, error) {
|
|
|
|
assert.Equals(t, id, az.ID)
|
|
|
|
return &acme.Authorization{
|
|
|
|
AccountID: "foo",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 401,
|
|
|
|
err: acme.NewError(acme.ErrorUnauthorizedType, "account id mismatch"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/db.UpdateAuthorization-error": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
|
|
|
ctx := context.WithValue(context.Background(), accContextKey, acc)
|
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetAuthorization: func(ctx context.Context, id string) (*acme.Authorization, error) {
|
|
|
|
assert.Equals(t, id, az.ID)
|
|
|
|
return &acme.Authorization{
|
|
|
|
AccountID: "accID",
|
|
|
|
Status: acme.StatusPending,
|
2021-03-19 21:37:45 +00:00
|
|
|
ExpiresAt: time.Now().Add(-1 * time.Hour),
|
2021-03-11 07:59:02 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
MockUpdateAuthorization: func(ctx context.Context, az *acme.Authorization) error {
|
|
|
|
assert.Equals(t, az.Status, acme.StatusInvalid)
|
|
|
|
return acme.NewErrorISE("force")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 500,
|
|
|
|
err: acme.NewErrorISE("force"),
|
|
|
|
}
|
|
|
|
},
|
2019-05-27 00:41:10 +00:00
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx = context.WithValue(ctx, accContextKey, acc)
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
2021-03-09 06:35:57 +00:00
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetAuthorization: func(ctx context.Context, id string) (*acme.Authorization, error) {
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.Equals(t, id, az.ID)
|
|
|
|
return &az, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 200,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
tc := run(t)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewContext(tc.ctx, tc.db, nil, acme.NewLinker("test.ca.smallstep.com", "acme"), nil)
|
2020-05-07 03:18:12 +00:00
|
|
|
req := httptest.NewRequest("GET", "/foo/bar", nil)
|
2022-05-03 00:35:35 +00:00
|
|
|
req = req.WithContext(ctx)
|
2019-05-27 00:41:10 +00:00
|
|
|
w := httptest.NewRecorder()
|
2022-04-28 02:08:16 +00:00
|
|
|
GetAuthorization(w, req)
|
2019-05-27 00:41:10 +00:00
|
|
|
res := w.Result()
|
|
|
|
|
|
|
|
assert.Equals(t, res.StatusCode, tc.statusCode)
|
|
|
|
|
2021-11-12 23:46:34 +00:00
|
|
|
body, err := io.ReadAll(res.Body)
|
2019-05-27 00:41:10 +00:00
|
|
|
res.Body.Close()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
|
|
|
|
var ae acme.Error
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
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)
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
|
|
|
|
} else {
|
|
|
|
//var gotAz acme.Authz
|
|
|
|
//assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &gotAz))
|
|
|
|
expB, err := json.Marshal(az)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
assert.Equals(t, bytes.TrimSpace(body), expB)
|
2021-10-08 18:59:57 +00:00
|
|
|
assert.Equals(t, res.Header["Location"], []string{u})
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
func TestHandler_GetCertificate(t *testing.T) {
|
2019-05-27 00:41:10 +00:00
|
|
|
leaf, err := pemutil.ReadCertificate("../../authority/testdata/certs/foo.crt")
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
inter, err := pemutil.ReadCertificate("../../authority/testdata/certs/intermediate_ca.crt")
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
root, err := pemutil.ReadCertificate("../../authority/testdata/certs/root_ca.crt")
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
certBytes := append(pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: leaf.Raw,
|
|
|
|
}), pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: inter.Raw,
|
|
|
|
})...)
|
|
|
|
certBytes = append(certBytes, pem.EncodeToMemory(&pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: root.Raw,
|
|
|
|
})...)
|
|
|
|
certID := "certID"
|
|
|
|
|
|
|
|
prov := newProv()
|
2020-05-07 03:18:12 +00:00
|
|
|
provName := url.PathEscape(prov.GetName())
|
|
|
|
baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
|
2019-05-27 00:41:10 +00:00
|
|
|
// Request with chi context
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("certID", certID)
|
2021-10-08 18:59:57 +00:00
|
|
|
u := fmt.Sprintf("%s/acme/%s/certificate/%s",
|
2020-05-07 03:18:12 +00:00
|
|
|
baseURL.String(), provName, certID)
|
2019-05-27 00:41:10 +00:00
|
|
|
|
|
|
|
type test struct {
|
2021-03-09 06:35:57 +00:00
|
|
|
db acme.DB
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx context.Context
|
|
|
|
statusCode int
|
2021-03-09 06:35:57 +00:00
|
|
|
err *acme.Error
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
var tests = map[string]func(t *testing.T) test{
|
|
|
|
"fail/no-account": func(t *testing.T) test {
|
|
|
|
return test{
|
2021-03-09 06:35:57 +00:00
|
|
|
db: &acme.MockDB{},
|
|
|
|
ctx: context.Background(),
|
2020-02-02 01:35:41 +00:00
|
|
|
statusCode: 400,
|
2021-03-09 06:35:57 +00:00
|
|
|
err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/nil-account": func(t *testing.T) test {
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx := context.WithValue(context.Background(), accContextKey, nil)
|
2019-05-27 00:41:10 +00:00
|
|
|
return test{
|
2021-03-09 06:35:57 +00:00
|
|
|
db: &acme.MockDB{},
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx: ctx,
|
2020-02-02 01:35:41 +00:00
|
|
|
statusCode: 400,
|
2021-03-09 06:35:57 +00:00
|
|
|
err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
},
|
2021-03-11 07:05:46 +00:00
|
|
|
"fail/db.GetCertificate-error": func(t *testing.T) test {
|
2019-05-27 00:41:10 +00:00
|
|
|
acc := &acme.Account{ID: "accID"}
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx := context.WithValue(context.Background(), accContextKey, acc)
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
2021-03-09 06:35:57 +00:00
|
|
|
db: &acme.MockDB{
|
|
|
|
MockError: acme.NewErrorISE("force"),
|
2019-05-27 00:41:10 +00:00
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 500,
|
2021-03-09 06:35:57 +00:00
|
|
|
err: acme.NewErrorISE("force"),
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
},
|
2021-03-11 07:05:46 +00:00
|
|
|
"fail/account-id-mismatch": func(t *testing.T) test {
|
2020-08-12 22:50:45 +00:00
|
|
|
acc := &acme.Account{ID: "accID"}
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx := context.WithValue(context.Background(), accContextKey, acc)
|
2020-08-12 22:50:45 +00:00
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
2021-03-09 06:35:57 +00:00
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetCertificate: func(ctx context.Context, id string) (*acme.Certificate, error) {
|
2020-08-12 22:50:45 +00:00
|
|
|
assert.Equals(t, id, certID)
|
2021-03-11 07:05:46 +00:00
|
|
|
return &acme.Certificate{AccountID: "foo"}, nil
|
2020-08-12 22:50:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ctx: ctx,
|
2021-03-11 07:05:46 +00:00
|
|
|
statusCode: 401,
|
|
|
|
err: acme.NewError(acme.ErrorUnauthorizedType, "account id mismatch"),
|
2020-08-12 22:50:45 +00:00
|
|
|
}
|
|
|
|
},
|
2019-05-27 00:41:10 +00:00
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx := context.WithValue(context.Background(), accContextKey, acc)
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
2021-03-09 06:35:57 +00:00
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetCertificate: func(ctx context.Context, id string) (*acme.Certificate, error) {
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.Equals(t, id, certID)
|
2021-03-11 07:05:46 +00:00
|
|
|
return &acme.Certificate{
|
|
|
|
AccountID: "accID",
|
|
|
|
OrderID: "ordID",
|
|
|
|
Leaf: leaf,
|
|
|
|
Intermediates: []*x509.Certificate{inter, root},
|
|
|
|
ID: id,
|
|
|
|
}, nil
|
2019-05-27 00:41:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 200,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
tc := run(t)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewDatabaseContext(tc.ctx, tc.db)
|
2021-10-08 18:59:57 +00:00
|
|
|
req := httptest.NewRequest("GET", u, nil)
|
2022-05-03 00:35:35 +00:00
|
|
|
req = req.WithContext(ctx)
|
2019-05-27 00:41:10 +00:00
|
|
|
w := httptest.NewRecorder()
|
2022-04-28 02:08:16 +00:00
|
|
|
GetCertificate(w, req)
|
2019-05-27 00:41:10 +00:00
|
|
|
res := w.Result()
|
|
|
|
|
|
|
|
assert.Equals(t, res.StatusCode, tc.statusCode)
|
|
|
|
|
2021-11-12 23:46:34 +00:00
|
|
|
body, err := io.ReadAll(res.Body)
|
2019-05-27 00:41:10 +00:00
|
|
|
res.Body.Close()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
|
|
|
|
var ae acme.Error
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
assert.Equals(t, ae.Type, tc.err.Type)
|
|
|
|
assert.HasPrefix(t, ae.Detail, tc.err.Detail)
|
|
|
|
assert.Equals(t, ae.Identifier, tc.err.Identifier)
|
|
|
|
assert.Equals(t, ae.Subproblems, tc.err.Subproblems)
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
|
|
|
|
} else {
|
|
|
|
assert.Equals(t, bytes.TrimSpace(body), bytes.TrimSpace(certBytes))
|
2022-11-09 18:57:50 +00:00
|
|
|
assert.Equals(t, res.Header["Content-Type"], []string{"application/pem-certificate-chain"})
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:59:02 +00:00
|
|
|
func TestHandler_GetChallenge(t *testing.T) {
|
2019-05-27 00:41:10 +00:00
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("chID", "chID")
|
2021-03-11 21:10:14 +00:00
|
|
|
chiCtx.URLParams.Add("authzID", "authzID")
|
2019-05-27 00:41:10 +00:00
|
|
|
prov := newProv()
|
2020-05-07 03:18:12 +00:00
|
|
|
provName := url.PathEscape(prov.GetName())
|
2021-03-11 21:10:14 +00:00
|
|
|
|
2020-05-07 03:18:12 +00:00
|
|
|
baseURL := &url.URL{Scheme: "https", Host: "test.ca.smallstep.com"}
|
2021-03-11 21:10:14 +00:00
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
u := fmt.Sprintf("%s/acme/%s/challenge/%s/%s",
|
2021-03-11 21:10:14 +00:00
|
|
|
baseURL.String(), provName, "authzID", "chID")
|
2019-05-27 00:41:10 +00:00
|
|
|
|
|
|
|
type test struct {
|
2021-03-09 06:35:57 +00:00
|
|
|
db acme.DB
|
2022-05-03 00:35:35 +00:00
|
|
|
vc acme.Client
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx context.Context
|
|
|
|
statusCode int
|
2021-03-11 21:10:14 +00:00
|
|
|
ch *acme.Challenge
|
2021-03-09 06:35:57 +00:00
|
|
|
err *acme.Error
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
var tests = map[string]func(t *testing.T) test{
|
|
|
|
"fail/no-account": func(t *testing.T) test {
|
|
|
|
return test{
|
2022-05-03 00:35:35 +00:00
|
|
|
db: &acme.MockDB{},
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx: context.Background(),
|
2020-02-02 01:35:41 +00:00
|
|
|
statusCode: 400,
|
2021-03-09 06:35:57 +00:00
|
|
|
err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/nil-account": func(t *testing.T) test {
|
|
|
|
return test{
|
2022-05-03 00:35:35 +00:00
|
|
|
db: &acme.MockDB{},
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx: context.WithValue(context.Background(), accContextKey, nil),
|
2020-02-02 01:35:41 +00:00
|
|
|
statusCode: 400,
|
2021-03-09 06:35:57 +00:00
|
|
|
err: acme.NewError(acme.ErrorAccountDoesNotExistType, "account does not exist"),
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/no-payload": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx := context.WithValue(context.Background(), accContextKey, acc)
|
2019-05-27 00:41:10 +00:00
|
|
|
return test{
|
2022-05-03 00:35:35 +00:00
|
|
|
db: &acme.MockDB{},
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx: ctx,
|
2021-03-25 03:46:02 +00:00
|
|
|
statusCode: 500,
|
|
|
|
err: acme.NewErrorISE("payload expected in request context"),
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/nil-payload": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-03-09 06:35:57 +00:00
|
|
|
ctx = context.WithValue(ctx, accContextKey, acc)
|
|
|
|
ctx = context.WithValue(ctx, payloadContextKey, nil)
|
2019-05-27 00:41:10 +00:00
|
|
|
return test{
|
2022-05-03 00:35:35 +00:00
|
|
|
db: &acme.MockDB{},
|
2019-05-27 00:41:10 +00:00
|
|
|
ctx: ctx,
|
2021-03-25 03:46:02 +00:00
|
|
|
statusCode: 500,
|
|
|
|
err: acme.NewErrorISE("payload expected in request context"),
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
},
|
2021-03-11 21:10:14 +00:00
|
|
|
"fail/db.GetChallenge-error": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-03-11 21:10:14 +00:00
|
|
|
ctx = context.WithValue(ctx, accContextKey, acc)
|
|
|
|
ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{isEmptyJSON: true})
|
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetChallenge: func(ctx context.Context, chID, azID string) (*acme.Challenge, error) {
|
|
|
|
assert.Equals(t, chID, "chID")
|
|
|
|
assert.Equals(t, azID, "authzID")
|
|
|
|
return nil, acme.NewErrorISE("force")
|
2019-05-27 00:41:10 +00:00
|
|
|
},
|
2021-03-11 21:10:14 +00:00
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 500,
|
|
|
|
err: acme.NewErrorISE("force"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/account-id-mismatch": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-03-11 21:10:14 +00:00
|
|
|
ctx = context.WithValue(ctx, accContextKey, acc)
|
|
|
|
ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{isEmptyJSON: true})
|
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetChallenge: func(ctx context.Context, chID, azID string) (*acme.Challenge, error) {
|
|
|
|
assert.Equals(t, chID, "chID")
|
|
|
|
assert.Equals(t, azID, "authzID")
|
|
|
|
return &acme.Challenge{AccountID: "foo"}, nil
|
2021-03-11 07:59:02 +00:00
|
|
|
},
|
2021-03-11 21:10:14 +00:00
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 401,
|
|
|
|
err: acme.NewError(acme.ErrorUnauthorizedType, "accout id mismatch"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/no-jwk": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-03-11 21:10:14 +00:00
|
|
|
ctx = context.WithValue(ctx, accContextKey, acc)
|
|
|
|
ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{isEmptyJSON: true})
|
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetChallenge: func(ctx context.Context, chID, azID string) (*acme.Challenge, error) {
|
|
|
|
assert.Equals(t, chID, "chID")
|
|
|
|
assert.Equals(t, azID, "authzID")
|
|
|
|
return &acme.Challenge{AccountID: "accID"}, nil
|
2021-03-11 07:59:02 +00:00
|
|
|
},
|
2021-03-11 21:10:14 +00:00
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 500,
|
|
|
|
err: acme.NewErrorISE("missing jwk"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/nil-jwk": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-03-11 21:10:14 +00:00
|
|
|
ctx = context.WithValue(ctx, accContextKey, acc)
|
|
|
|
ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{isEmptyJSON: true})
|
|
|
|
ctx = context.WithValue(ctx, jwkContextKey, nil)
|
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetChallenge: func(ctx context.Context, chID, azID string) (*acme.Challenge, error) {
|
|
|
|
assert.Equals(t, chID, "chID")
|
|
|
|
assert.Equals(t, azID, "authzID")
|
|
|
|
return &acme.Challenge{AccountID: "accID"}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 500,
|
|
|
|
err: acme.NewErrorISE("nil jwk"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/validate-challenge-error": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-03-11 21:10:14 +00:00
|
|
|
ctx = context.WithValue(ctx, accContextKey, acc)
|
|
|
|
ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{isEmptyJSON: true})
|
|
|
|
_jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
_pub := _jwk.Public()
|
|
|
|
ctx = context.WithValue(ctx, jwkContextKey, &_pub)
|
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetChallenge: func(ctx context.Context, chID, azID string) (*acme.Challenge, error) {
|
|
|
|
assert.Equals(t, chID, "chID")
|
|
|
|
assert.Equals(t, azID, "authzID")
|
|
|
|
return &acme.Challenge{
|
|
|
|
Status: acme.StatusPending,
|
2021-06-18 10:39:36 +00:00
|
|
|
Type: acme.HTTP01,
|
2021-03-11 21:10:14 +00:00
|
|
|
AccountID: "accID",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
MockUpdateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
|
|
|
|
assert.Equals(t, ch.Status, acme.StatusPending)
|
2021-06-18 10:39:36 +00:00
|
|
|
assert.Equals(t, ch.Type, acme.HTTP01)
|
2021-03-11 21:10:14 +00:00
|
|
|
assert.Equals(t, ch.AccountID, "accID")
|
2021-03-29 19:04:14 +00:00
|
|
|
assert.Equals(t, ch.AuthorizationID, "authzID")
|
2021-03-11 21:10:14 +00:00
|
|
|
assert.HasSuffix(t, ch.Error.Type, acme.ErrorConnectionType.String())
|
|
|
|
return acme.NewErrorISE("force")
|
|
|
|
},
|
|
|
|
},
|
2022-05-03 00:35:35 +00:00
|
|
|
vc: &mockClient{
|
|
|
|
get: func(string) (*http.Response, error) {
|
2021-03-11 21:10:14 +00:00
|
|
|
return nil, errors.New("force")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 500,
|
|
|
|
err: acme.NewErrorISE("force"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
acc := &acme.Account{ID: "accID"}
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewProvisionerContext(context.Background(), prov)
|
2021-03-11 21:10:14 +00:00
|
|
|
ctx = context.WithValue(ctx, accContextKey, acc)
|
|
|
|
ctx = context.WithValue(ctx, payloadContextKey, &payloadInfo{isEmptyJSON: true})
|
|
|
|
_jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
_pub := _jwk.Public()
|
|
|
|
ctx = context.WithValue(ctx, jwkContextKey, &_pub)
|
|
|
|
ctx = context.WithValue(ctx, chi.RouteCtxKey, chiCtx)
|
|
|
|
return test{
|
|
|
|
db: &acme.MockDB{
|
|
|
|
MockGetChallenge: func(ctx context.Context, chID, azID string) (*acme.Challenge, error) {
|
|
|
|
assert.Equals(t, chID, "chID")
|
|
|
|
assert.Equals(t, azID, "authzID")
|
|
|
|
return &acme.Challenge{
|
|
|
|
ID: "chID",
|
|
|
|
Status: acme.StatusPending,
|
2021-06-18 10:39:36 +00:00
|
|
|
Type: acme.HTTP01,
|
2021-03-11 21:10:14 +00:00
|
|
|
AccountID: "accID",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
MockUpdateChallenge: func(ctx context.Context, ch *acme.Challenge) error {
|
|
|
|
assert.Equals(t, ch.Status, acme.StatusPending)
|
2021-06-18 10:39:36 +00:00
|
|
|
assert.Equals(t, ch.Type, acme.HTTP01)
|
2021-03-11 21:10:14 +00:00
|
|
|
assert.Equals(t, ch.AccountID, "accID")
|
2021-03-29 19:04:14 +00:00
|
|
|
assert.Equals(t, ch.AuthorizationID, "authzID")
|
2021-03-11 21:10:14 +00:00
|
|
|
assert.HasSuffix(t, ch.Error.Type, acme.ErrorConnectionType.String())
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ch: &acme.Challenge{
|
2021-03-29 19:04:14 +00:00
|
|
|
ID: "chID",
|
|
|
|
Status: acme.StatusPending,
|
|
|
|
AuthorizationID: "authzID",
|
2021-06-18 10:39:36 +00:00
|
|
|
Type: acme.HTTP01,
|
2021-03-29 19:04:14 +00:00
|
|
|
AccountID: "accID",
|
2021-10-08 18:59:57 +00:00
|
|
|
URL: u,
|
2021-03-29 19:04:14 +00:00
|
|
|
Error: acme.NewError(acme.ErrorConnectionType, "force"),
|
2021-03-11 21:10:14 +00:00
|
|
|
},
|
2022-05-03 00:35:35 +00:00
|
|
|
vc: &mockClient{
|
|
|
|
get: func(string) (*http.Response, error) {
|
2021-03-11 21:10:14 +00:00
|
|
|
return nil, errors.New("force")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 200,
|
|
|
|
}
|
|
|
|
},
|
2019-05-27 00:41:10 +00:00
|
|
|
}
|
|
|
|
for name, run := range tests {
|
|
|
|
tc := run(t)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2022-05-03 00:35:35 +00:00
|
|
|
ctx := acme.NewContext(tc.ctx, tc.db, nil, acme.NewLinker("test.ca.smallstep.com", "acme"), nil)
|
2021-10-08 18:59:57 +00:00
|
|
|
req := httptest.NewRequest("GET", u, nil)
|
2022-05-03 00:35:35 +00:00
|
|
|
req = req.WithContext(ctx)
|
2019-05-27 00:41:10 +00:00
|
|
|
w := httptest.NewRecorder()
|
2022-04-28 02:08:16 +00:00
|
|
|
GetChallenge(w, req)
|
2019-05-27 00:41:10 +00:00
|
|
|
res := w.Result()
|
|
|
|
|
|
|
|
assert.Equals(t, res.StatusCode, tc.statusCode)
|
|
|
|
|
2021-11-12 23:46:34 +00:00
|
|
|
body, err := io.ReadAll(res.Body)
|
2019-05-27 00:41:10 +00:00
|
|
|
res.Body.Close()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
if res.StatusCode >= 400 && assert.NotNil(t, tc.err) {
|
|
|
|
var ae acme.Error
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &ae))
|
|
|
|
|
2021-03-09 06:35:57 +00:00
|
|
|
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)
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.Equals(t, res.Header["Content-Type"], []string{"application/problem+json"})
|
|
|
|
} else {
|
|
|
|
expB, err := json.Marshal(tc.ch)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
assert.Equals(t, bytes.TrimSpace(body), expB)
|
2021-03-25 07:23:57 +00:00
|
|
|
assert.Equals(t, res.Header["Link"], []string{fmt.Sprintf("<%s/acme/%s/authz/%s>;rel=\"up\"", baseURL, provName, "authzID")})
|
2021-10-08 18:59:57 +00:00
|
|
|
assert.Equals(t, res.Header["Location"], []string{u})
|
2019-05-27 00:41:10 +00:00
|
|
|
assert.Equals(t, res.Header["Content-Type"], []string{"application/json"})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 14:35:42 +00:00
|
|
|
|
|
|
|
func Test_createMetaObject(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
p *provisioner.ACME
|
|
|
|
want *Meta
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no-meta",
|
|
|
|
p: &provisioner.ACME{
|
|
|
|
Type: "ACME",
|
|
|
|
Name: "acme",
|
|
|
|
},
|
|
|
|
want: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "terms-of-service",
|
|
|
|
p: &provisioner.ACME{
|
|
|
|
Type: "ACME",
|
|
|
|
Name: "acme",
|
|
|
|
TermsOfService: "https://terms.ca.local",
|
|
|
|
},
|
|
|
|
want: &Meta{
|
|
|
|
TermsOfService: "https://terms.ca.local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "website",
|
|
|
|
p: &provisioner.ACME{
|
|
|
|
Type: "ACME",
|
|
|
|
Name: "acme",
|
|
|
|
Website: "https://ca.local",
|
|
|
|
},
|
|
|
|
want: &Meta{
|
|
|
|
Website: "https://ca.local",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "caa",
|
|
|
|
p: &provisioner.ACME{
|
|
|
|
Type: "ACME",
|
|
|
|
Name: "acme",
|
|
|
|
CaaIdentities: []string{"ca.local", "ca.remote"},
|
|
|
|
},
|
|
|
|
want: &Meta{
|
|
|
|
CaaIdentities: []string{"ca.local", "ca.remote"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "require-eab",
|
|
|
|
p: &provisioner.ACME{
|
|
|
|
Type: "ACME",
|
|
|
|
Name: "acme",
|
|
|
|
RequireEAB: true,
|
|
|
|
},
|
|
|
|
want: &Meta{
|
|
|
|
ExternalAccountRequired: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "full-meta",
|
|
|
|
p: &provisioner.ACME{
|
|
|
|
Type: "ACME",
|
|
|
|
Name: "acme",
|
|
|
|
TermsOfService: "https://terms.ca.local",
|
|
|
|
Website: "https://ca.local",
|
|
|
|
CaaIdentities: []string{"ca.local", "ca.remote"},
|
|
|
|
RequireEAB: true,
|
|
|
|
},
|
|
|
|
want: &Meta{
|
|
|
|
TermsOfService: "https://terms.ca.local",
|
|
|
|
Website: "https://ca.local",
|
|
|
|
CaaIdentities: []string{"ca.local", "ca.remote"},
|
|
|
|
ExternalAccountRequired: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got := createMetaObject(tt.p)
|
|
|
|
if !cmp.Equal(tt.want, got) {
|
|
|
|
t.Errorf("createMetaObject() diff =\n%s", cmp.Diff(tt.want, got))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|