2021-12-08 14:19:38 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-12-09 08:36:03 +00:00
|
|
|
"strings"
|
2021-12-08 14:19:38 +00:00
|
|
|
"testing"
|
2021-12-09 08:36:03 +00:00
|
|
|
"time"
|
2021-12-08 14:19:38 +00:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2021-12-09 08:36:03 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/google/go-cmp/cmp/cmpopts"
|
2021-12-08 14:19:38 +00:00
|
|
|
"github.com/smallstep/assert"
|
2021-12-09 08:36:03 +00:00
|
|
|
"github.com/smallstep/certificates/acme"
|
2021-12-08 14:19:38 +00:00
|
|
|
"github.com/smallstep/certificates/authority/admin"
|
|
|
|
"github.com/smallstep/certificates/authority/provisioner"
|
|
|
|
"go.step.sm/linkedca"
|
2021-12-09 08:36:03 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2021-12-08 14:19:38 +00:00
|
|
|
)
|
|
|
|
|
2021-12-09 08:36:03 +00:00
|
|
|
func readProtoJSON(r io.ReadCloser, m proto.Message) error {
|
|
|
|
defer r.Close()
|
|
|
|
data, err := io.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return protojson.Unmarshal(data, m)
|
|
|
|
}
|
|
|
|
|
2021-12-08 14:19:38 +00:00
|
|
|
func TestHandler_requireEABEnabled(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
ctx context.Context
|
|
|
|
db admin.DB
|
2021-12-22 14:30:40 +00:00
|
|
|
auth adminAuthority
|
2021-12-08 14:19:38 +00:00
|
|
|
next nextHTTP
|
|
|
|
err *admin.Error
|
|
|
|
statusCode int
|
|
|
|
}
|
|
|
|
var tests = map[string]func(t *testing.T) test{
|
|
|
|
"fail/h.provisionerHasEABEnabled": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2021-12-22 14:30:40 +00:00
|
|
|
auth := &mockAdminAuthority{
|
2021-12-08 14:19:38 +00:00
|
|
|
MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
|
|
|
|
assert.Equals(t, "provName", name)
|
|
|
|
return nil, errors.New("force")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := admin.NewErrorISE("error loading provisioner provName: force")
|
|
|
|
err.Message = "error loading provisioner provName: force"
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
auth: auth,
|
|
|
|
err: err,
|
|
|
|
statusCode: 500,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/eab-disabled": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2021-12-22 14:30:40 +00:00
|
|
|
auth := &mockAdminAuthority{
|
2021-12-08 14:19:38 +00:00
|
|
|
MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
|
|
|
|
assert.Equals(t, "provName", name)
|
|
|
|
return &provisioner.MockProvisioner{
|
|
|
|
MgetID: func() string {
|
|
|
|
return "provID"
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
db := &admin.MockDB{
|
|
|
|
MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
|
|
|
|
assert.Equals(t, "provID", id)
|
|
|
|
return &linkedca.Provisioner{
|
|
|
|
Id: "provID",
|
|
|
|
Name: "provName",
|
|
|
|
Details: &linkedca.ProvisionerDetails{
|
|
|
|
Data: &linkedca.ProvisionerDetails_ACME{
|
|
|
|
ACME: &linkedca.ACMEProvisioner{
|
|
|
|
RequireEab: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := admin.NewError(admin.ErrorBadRequestType, "ACME EAB not enabled for provisioner provName")
|
|
|
|
err.Message = "ACME EAB not enabled for provisioner provName"
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
auth: auth,
|
|
|
|
db: db,
|
|
|
|
err: err,
|
|
|
|
statusCode: 400,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/eab-enabled": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2021-12-22 14:30:40 +00:00
|
|
|
auth := &mockAdminAuthority{
|
2021-12-08 14:19:38 +00:00
|
|
|
MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
|
|
|
|
assert.Equals(t, "provName", name)
|
|
|
|
return &provisioner.MockProvisioner{
|
|
|
|
MgetID: func() string {
|
|
|
|
return "provID"
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
db := &admin.MockDB{
|
|
|
|
MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
|
|
|
|
assert.Equals(t, "provID", id)
|
|
|
|
return &linkedca.Provisioner{
|
|
|
|
Id: "provID",
|
|
|
|
Name: "provName",
|
|
|
|
Details: &linkedca.ProvisionerDetails{
|
|
|
|
Data: &linkedca.ProvisionerDetails_ACME{
|
|
|
|
ACME: &linkedca.ACMEProvisioner{
|
|
|
|
RequireEab: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
auth: auth,
|
|
|
|
db: db,
|
|
|
|
next: func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write(nil) // mock response with status 200
|
|
|
|
},
|
|
|
|
statusCode: 200,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, prep := range tests {
|
|
|
|
tc := prep(t)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
h := &Handler{
|
|
|
|
db: tc.db,
|
|
|
|
auth: tc.auth,
|
|
|
|
acmeDB: nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
req := httptest.NewRequest("GET", "/foo", nil) // chi routing is prepared in test setup
|
|
|
|
req = req.WithContext(tc.ctx)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
h.requireEABEnabled(tc.next)(w, req)
|
|
|
|
res := w.Result()
|
|
|
|
|
2021-12-09 14:24:14 +00:00
|
|
|
assert.Equals(t, tc.statusCode, res.StatusCode)
|
2021-12-08 14:19:38 +00:00
|
|
|
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
if res.StatusCode >= 400 {
|
|
|
|
err := admin.Error{}
|
|
|
|
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &err))
|
|
|
|
|
|
|
|
assert.Equals(t, tc.err.Type, err.Type)
|
|
|
|
assert.Equals(t, tc.err.Message, err.Message)
|
|
|
|
assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
|
|
|
|
assert.Equals(t, tc.err.Detail, err.Detail)
|
|
|
|
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandler_provisionerHasEABEnabled(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
db admin.DB
|
2021-12-22 14:30:40 +00:00
|
|
|
auth adminAuthority
|
2021-12-08 14:19:38 +00:00
|
|
|
provisionerName string
|
|
|
|
want bool
|
|
|
|
err *admin.Error
|
|
|
|
}
|
|
|
|
var tests = map[string]func(t *testing.T) test{
|
|
|
|
"fail/auth.LoadProvisionerByName": func(t *testing.T) test {
|
2021-12-22 14:30:40 +00:00
|
|
|
auth := &mockAdminAuthority{
|
2021-12-08 14:19:38 +00:00
|
|
|
MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
|
|
|
|
assert.Equals(t, "provName", name)
|
|
|
|
return nil, errors.New("force")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
auth: auth,
|
|
|
|
provisionerName: "provName",
|
|
|
|
want: false,
|
|
|
|
err: admin.WrapErrorISE(errors.New("force"), "error loading provisioner provName"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/db.GetProvisioner": func(t *testing.T) test {
|
2021-12-22 14:30:40 +00:00
|
|
|
auth := &mockAdminAuthority{
|
2021-12-08 14:19:38 +00:00
|
|
|
MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
|
|
|
|
assert.Equals(t, "provName", name)
|
|
|
|
return &provisioner.MockProvisioner{
|
|
|
|
MgetID: func() string {
|
|
|
|
return "provID"
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
db := &admin.MockDB{
|
|
|
|
MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
|
|
|
|
assert.Equals(t, "provID", id)
|
|
|
|
return nil, errors.New("force")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
auth: auth,
|
|
|
|
db: db,
|
|
|
|
provisionerName: "provName",
|
|
|
|
want: false,
|
|
|
|
err: admin.WrapErrorISE(errors.New("force"), "error loading provisioner provName"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/prov.GetDetails": func(t *testing.T) test {
|
2021-12-22 14:30:40 +00:00
|
|
|
auth := &mockAdminAuthority{
|
2021-12-08 14:19:38 +00:00
|
|
|
MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
|
|
|
|
assert.Equals(t, "provName", name)
|
|
|
|
return &provisioner.MockProvisioner{
|
|
|
|
MgetID: func() string {
|
|
|
|
return "provID"
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
db := &admin.MockDB{
|
|
|
|
MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
|
|
|
|
assert.Equals(t, "provID", id)
|
|
|
|
return &linkedca.Provisioner{
|
|
|
|
Id: "provID",
|
|
|
|
Name: "provName",
|
|
|
|
Details: nil,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
auth: auth,
|
|
|
|
db: db,
|
|
|
|
provisionerName: "provName",
|
|
|
|
want: false,
|
|
|
|
err: admin.WrapErrorISE(errors.New("force"), "error loading provisioner provName"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/details.GetACME": func(t *testing.T) test {
|
2021-12-22 14:30:40 +00:00
|
|
|
auth := &mockAdminAuthority{
|
2021-12-08 14:19:38 +00:00
|
|
|
MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
|
|
|
|
assert.Equals(t, "provName", name)
|
|
|
|
return &provisioner.MockProvisioner{
|
|
|
|
MgetID: func() string {
|
|
|
|
return "provID"
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
db := &admin.MockDB{
|
|
|
|
MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
|
|
|
|
assert.Equals(t, "provID", id)
|
|
|
|
return &linkedca.Provisioner{
|
|
|
|
Id: "provID",
|
|
|
|
Name: "provName",
|
|
|
|
Details: &linkedca.ProvisionerDetails{
|
|
|
|
Data: &linkedca.ProvisionerDetails_ACME{
|
|
|
|
ACME: nil,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
auth: auth,
|
|
|
|
db: db,
|
|
|
|
provisionerName: "provName",
|
|
|
|
want: false,
|
|
|
|
err: admin.WrapErrorISE(errors.New("force"), "error loading provisioner provName"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/eab-disabled": func(t *testing.T) test {
|
2021-12-22 14:30:40 +00:00
|
|
|
auth := &mockAdminAuthority{
|
2021-12-08 14:19:38 +00:00
|
|
|
MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
|
|
|
|
assert.Equals(t, "eab-disabled", name)
|
|
|
|
return &provisioner.MockProvisioner{
|
|
|
|
MgetID: func() string {
|
|
|
|
return "provID"
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
db := &admin.MockDB{
|
|
|
|
MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
|
|
|
|
assert.Equals(t, "provID", id)
|
|
|
|
return &linkedca.Provisioner{
|
|
|
|
Id: "provID",
|
|
|
|
Name: "eab-disabled",
|
|
|
|
Details: &linkedca.ProvisionerDetails{
|
|
|
|
Data: &linkedca.ProvisionerDetails_ACME{
|
|
|
|
ACME: &linkedca.ACMEProvisioner{
|
|
|
|
RequireEab: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
db: db,
|
|
|
|
auth: auth,
|
|
|
|
provisionerName: "eab-disabled",
|
|
|
|
want: false,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/eab-enabled": func(t *testing.T) test {
|
2021-12-22 14:30:40 +00:00
|
|
|
auth := &mockAdminAuthority{
|
2021-12-08 14:19:38 +00:00
|
|
|
MockLoadProvisionerByName: func(name string) (provisioner.Interface, error) {
|
|
|
|
assert.Equals(t, "eab-enabled", name)
|
|
|
|
return &provisioner.MockProvisioner{
|
|
|
|
MgetID: func() string {
|
|
|
|
return "provID"
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
db := &admin.MockDB{
|
|
|
|
MockGetProvisioner: func(ctx context.Context, id string) (*linkedca.Provisioner, error) {
|
|
|
|
assert.Equals(t, "provID", id)
|
|
|
|
return &linkedca.Provisioner{
|
|
|
|
Id: "provID",
|
|
|
|
Name: "eab-enabled",
|
|
|
|
Details: &linkedca.ProvisionerDetails{
|
|
|
|
Data: &linkedca.ProvisionerDetails_ACME{
|
|
|
|
ACME: &linkedca.ACMEProvisioner{
|
|
|
|
RequireEab: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
db: db,
|
|
|
|
auth: auth,
|
|
|
|
provisionerName: "eab-enabled",
|
|
|
|
want: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, prep := range tests {
|
|
|
|
tc := prep(t)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
h := &Handler{
|
|
|
|
db: tc.db,
|
|
|
|
auth: tc.auth,
|
|
|
|
acmeDB: nil,
|
|
|
|
}
|
2022-01-07 15:59:55 +00:00
|
|
|
got, prov, err := h.provisionerHasEABEnabled(context.TODO(), tc.provisionerName)
|
2021-12-08 14:19:38 +00:00
|
|
|
if (err != nil) != (tc.err != nil) {
|
|
|
|
t.Errorf("Handler.provisionerHasEABEnabled() error = %v, want err %v", err, tc.err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if tc.err != nil {
|
2022-01-07 15:59:55 +00:00
|
|
|
assert.Type(t, &linkedca.Provisioner{}, prov)
|
2021-12-22 14:30:40 +00:00
|
|
|
assert.Type(t, &admin.Error{}, err)
|
|
|
|
adminError, _ := err.(*admin.Error)
|
|
|
|
assert.Equals(t, tc.err.Type, adminError.Type)
|
|
|
|
assert.Equals(t, tc.err.Status, adminError.Status)
|
|
|
|
assert.Equals(t, tc.err.StatusCode(), adminError.StatusCode())
|
|
|
|
assert.Equals(t, tc.err.Message, adminError.Message)
|
|
|
|
assert.Equals(t, tc.err.Detail, adminError.Detail)
|
2021-12-08 14:19:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tc.want {
|
|
|
|
t.Errorf("Handler.provisionerHasEABEnabled() = %v, want %v", got, tc.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateExternalAccountKeyRequest_Validate(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
Reference string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2021-12-09 14:24:14 +00:00
|
|
|
{
|
|
|
|
name: "fail/reference-too-long",
|
|
|
|
fields: fields{
|
|
|
|
Reference: strings.Repeat("A", 257),
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2021-12-08 14:19:38 +00:00
|
|
|
{
|
|
|
|
name: "ok/empty-reference",
|
|
|
|
fields: fields{
|
|
|
|
Reference: "",
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ok",
|
|
|
|
fields: fields{
|
|
|
|
Reference: "my-eab-reference",
|
|
|
|
},
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
r := &CreateExternalAccountKeyRequest{
|
|
|
|
Reference: tt.fields.Reference,
|
|
|
|
}
|
|
|
|
if err := r.Validate(); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("CreateExternalAccountKeyRequest.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 08:36:03 +00:00
|
|
|
|
|
|
|
func TestHandler_CreateExternalAccountKey(t *testing.T) {
|
2022-01-07 15:59:55 +00:00
|
|
|
prov := &linkedca.Provisioner{
|
|
|
|
Id: "provID",
|
|
|
|
Name: "provName",
|
|
|
|
}
|
2021-12-09 08:36:03 +00:00
|
|
|
type test struct {
|
|
|
|
ctx context.Context
|
|
|
|
db acme.DB
|
|
|
|
body []byte
|
|
|
|
statusCode int
|
|
|
|
eak *linkedca.EABKey
|
|
|
|
err *admin.Error
|
|
|
|
}
|
|
|
|
var tests = map[string]func(t *testing.T) test{
|
|
|
|
"fail/ReadJSON": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
|
|
|
body := []byte("{!?}")
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
body: body,
|
|
|
|
statusCode: 400,
|
|
|
|
eak: nil,
|
|
|
|
err: &admin.Error{
|
|
|
|
Type: admin.ErrorBadRequestType.String(),
|
|
|
|
Status: 400,
|
|
|
|
Detail: "bad request",
|
|
|
|
Message: "error reading request body: error decoding json: invalid character '!' looking for beginning of object key string",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/validate": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
|
|
|
req := CreateExternalAccountKeyRequest{
|
|
|
|
Reference: strings.Repeat("A", 257),
|
|
|
|
}
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
body: body,
|
|
|
|
statusCode: 400,
|
|
|
|
eak: nil,
|
|
|
|
err: &admin.Error{
|
|
|
|
Type: admin.ErrorBadRequestType.String(),
|
|
|
|
Status: 400,
|
|
|
|
Detail: "bad request",
|
|
|
|
Message: "error validating request body: reference length 257 exceeds the maximum (256)",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/acmeDB.GetExternalAccountKeyByReference": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
req := CreateExternalAccountKeyRequest{
|
|
|
|
Reference: "an-external-key-reference",
|
|
|
|
}
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "an-external-key-reference", reference)
|
|
|
|
return nil, errors.New("force")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
db: db,
|
|
|
|
body: body,
|
|
|
|
statusCode: 500,
|
|
|
|
eak: nil,
|
|
|
|
err: &admin.Error{
|
|
|
|
Type: admin.ErrorServerInternalType.String(),
|
|
|
|
Status: 500,
|
|
|
|
Detail: "the server experienced an internal error",
|
|
|
|
Message: "could not lookup external account key by reference: force",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/reference-conflict-409": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
req := CreateExternalAccountKeyRequest{
|
|
|
|
Reference: "an-external-key-reference",
|
|
|
|
}
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "an-external-key-reference", reference)
|
|
|
|
past := time.Now().Add(-24 * time.Hour)
|
|
|
|
return &acme.ExternalAccountKey{
|
2022-01-07 15:59:55 +00:00
|
|
|
ID: "eakID",
|
|
|
|
ProvisionerID: "provID",
|
|
|
|
Reference: "an-external-key-reference",
|
|
|
|
KeyBytes: []byte{1, 3, 3, 7},
|
|
|
|
CreatedAt: past,
|
2021-12-09 08:36:03 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
db: db,
|
|
|
|
body: body,
|
|
|
|
statusCode: 409,
|
|
|
|
eak: nil,
|
|
|
|
err: &admin.Error{
|
|
|
|
Type: admin.ErrorBadRequestType.String(),
|
|
|
|
Status: 409,
|
|
|
|
Detail: "bad request",
|
2022-01-07 15:59:55 +00:00
|
|
|
Message: "an ACME EAB key for provisioner 'provName' with reference 'an-external-key-reference' already exists",
|
2021-12-09 08:36:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/acmeDB.CreateExternalAccountKey-no-reference": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
req := CreateExternalAccountKeyRequest{
|
|
|
|
Reference: "",
|
|
|
|
}
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockCreateExternalAccountKey: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "", reference)
|
|
|
|
return nil, errors.New("force")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
db: db,
|
|
|
|
body: body,
|
|
|
|
statusCode: 500,
|
|
|
|
err: &admin.Error{
|
|
|
|
Type: admin.ErrorServerInternalType.String(),
|
|
|
|
Status: 500,
|
|
|
|
Detail: "the server experienced an internal error",
|
|
|
|
Message: "error creating ACME EAB key for provisioner 'provName': force",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/acmeDB.CreateExternalAccountKey-with-reference": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
req := CreateExternalAccountKeyRequest{
|
|
|
|
Reference: "an-external-key-reference",
|
|
|
|
}
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "an-external-key-reference", reference)
|
|
|
|
return nil, acme.ErrNotFound // simulating not found; skipping 409 conflict
|
|
|
|
},
|
2022-01-07 15:59:55 +00:00
|
|
|
MockCreateExternalAccountKey: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "an-external-key-reference", reference)
|
|
|
|
return nil, errors.New("force")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
db: db,
|
|
|
|
body: body,
|
|
|
|
statusCode: 500,
|
|
|
|
err: &admin.Error{
|
|
|
|
Type: admin.ErrorServerInternalType.String(),
|
|
|
|
Status: 500,
|
|
|
|
Detail: "the server experienced an internal error",
|
|
|
|
Message: "error creating ACME EAB key for provisioner 'provName' and reference 'an-external-key-reference': force",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/no-reference": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
req := CreateExternalAccountKeyRequest{
|
|
|
|
Reference: "",
|
|
|
|
}
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
now := time.Now()
|
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockCreateExternalAccountKey: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "", reference)
|
|
|
|
return &acme.ExternalAccountKey{
|
2022-01-07 15:59:55 +00:00
|
|
|
ID: "eakID",
|
|
|
|
ProvisionerID: "provID",
|
|
|
|
Reference: "",
|
|
|
|
KeyBytes: []byte{1, 3, 3, 7},
|
|
|
|
CreatedAt: now,
|
2021-12-09 08:36:03 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
db: db,
|
|
|
|
body: body,
|
|
|
|
statusCode: 201,
|
|
|
|
eak: &linkedca.EABKey{
|
|
|
|
Id: "eakID",
|
|
|
|
Provisioner: "provName",
|
|
|
|
Reference: "",
|
|
|
|
HmacKey: []byte{1, 3, 3, 7},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/with-reference": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
req := CreateExternalAccountKeyRequest{
|
|
|
|
Reference: "an-external-key-reference",
|
|
|
|
}
|
|
|
|
body, err := json.Marshal(req)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
now := time.Now()
|
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "an-external-key-reference", reference)
|
|
|
|
return nil, acme.ErrNotFound // simulating not found; skipping 409 conflict
|
|
|
|
},
|
2022-01-07 15:59:55 +00:00
|
|
|
MockCreateExternalAccountKey: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "an-external-key-reference", reference)
|
|
|
|
return &acme.ExternalAccountKey{
|
2022-01-07 15:59:55 +00:00
|
|
|
ID: "eakID",
|
|
|
|
ProvisionerID: "provID",
|
|
|
|
Reference: "an-external-key-reference",
|
|
|
|
KeyBytes: []byte{1, 3, 3, 7},
|
|
|
|
CreatedAt: now,
|
2021-12-09 08:36:03 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
db: db,
|
|
|
|
body: body,
|
|
|
|
statusCode: 201,
|
|
|
|
eak: &linkedca.EABKey{
|
|
|
|
Id: "eakID",
|
|
|
|
Provisioner: "provName",
|
|
|
|
Reference: "an-external-key-reference",
|
|
|
|
HmacKey: []byte{1, 3, 3, 7},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, prep := range tests {
|
|
|
|
tc := prep(t)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
h := &Handler{
|
|
|
|
acmeDB: tc.db,
|
|
|
|
}
|
|
|
|
req := httptest.NewRequest("POST", "/foo", io.NopCloser(bytes.NewBuffer(tc.body))) // chi routing is prepared in test setup
|
|
|
|
req = req.WithContext(tc.ctx)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
h.CreateExternalAccountKey(w, req)
|
|
|
|
res := w.Result()
|
2021-12-09 14:24:14 +00:00
|
|
|
assert.Equals(t, tc.statusCode, res.StatusCode)
|
2021-12-09 08:36:03 +00:00
|
|
|
|
|
|
|
if res.StatusCode >= 400 {
|
|
|
|
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
adminErr := admin.Error{}
|
|
|
|
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
|
|
|
|
|
|
|
|
assert.Equals(t, tc.err.Type, adminErr.Type)
|
|
|
|
assert.Equals(t, tc.err.Message, adminErr.Message)
|
|
|
|
assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
|
|
|
|
assert.Equals(t, tc.err.Detail, adminErr.Detail)
|
|
|
|
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
|
2021-12-09 16:29:23 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-09 08:36:03 +00:00
|
|
|
|
2021-12-09 16:29:23 +00:00
|
|
|
eabKey := &linkedca.EABKey{}
|
|
|
|
err := readProtoJSON(res.Body, eabKey)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
|
2021-12-09 08:36:03 +00:00
|
|
|
|
2021-12-09 16:29:23 +00:00
|
|
|
opts := []cmp.Option{cmpopts.IgnoreUnexported(linkedca.EABKey{})}
|
|
|
|
if !cmp.Equal(tc.eak, eabKey, opts...) {
|
|
|
|
t.Errorf("h.CreateExternalAccountKey diff =\n%s", cmp.Diff(tc.eak, eabKey, opts...))
|
2021-12-09 08:36:03 +00:00
|
|
|
}
|
2021-12-09 16:29:23 +00:00
|
|
|
|
2021-12-09 08:36:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandler_DeleteExternalAccountKey(t *testing.T) {
|
2022-01-07 15:59:55 +00:00
|
|
|
prov := &linkedca.Provisioner{
|
|
|
|
Id: "provID",
|
|
|
|
Name: "provName",
|
|
|
|
}
|
2021-12-09 08:36:03 +00:00
|
|
|
type test struct {
|
|
|
|
ctx context.Context
|
|
|
|
db acme.DB
|
|
|
|
statusCode int
|
|
|
|
err *admin.Error
|
|
|
|
}
|
|
|
|
var tests = map[string]func(t *testing.T) test{
|
|
|
|
"fail/acmeDB.DeleteExternalAccountKey": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
chiCtx.URLParams.Add("id", "keyID")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockDeleteExternalAccountKey: func(ctx context.Context, provisionerID, keyID string) error {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "keyID", keyID)
|
|
|
|
return errors.New("force")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
db: db,
|
|
|
|
statusCode: 500,
|
|
|
|
err: &admin.Error{
|
|
|
|
Type: admin.ErrorServerInternalType.String(),
|
|
|
|
Status: 500,
|
|
|
|
Detail: "the server experienced an internal error",
|
|
|
|
Message: "error deleting ACME EAB Key 'keyID': force",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
chiCtx.URLParams.Add("id", "keyID")
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockDeleteExternalAccountKey: func(ctx context.Context, provisionerID, keyID string) error {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "keyID", keyID)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
db: db,
|
|
|
|
statusCode: 200,
|
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, prep := range tests {
|
|
|
|
tc := prep(t)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
h := &Handler{
|
|
|
|
acmeDB: tc.db,
|
|
|
|
}
|
|
|
|
req := httptest.NewRequest("DELETE", "/foo", nil) // chi routing is prepared in test setup
|
|
|
|
req = req.WithContext(tc.ctx)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
h.DeleteExternalAccountKey(w, req)
|
|
|
|
res := w.Result()
|
2021-12-09 14:24:14 +00:00
|
|
|
assert.Equals(t, tc.statusCode, res.StatusCode)
|
2021-12-09 08:36:03 +00:00
|
|
|
|
|
|
|
if res.StatusCode >= 400 {
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
adminErr := admin.Error{}
|
|
|
|
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
|
|
|
|
|
|
|
|
assert.Equals(t, tc.err.Type, adminErr.Type)
|
|
|
|
assert.Equals(t, tc.err.Message, adminErr.Message)
|
|
|
|
assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
|
|
|
|
assert.Equals(t, tc.err.Detail, adminErr.Detail)
|
|
|
|
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
|
2021-12-09 16:29:23 +00:00
|
|
|
return
|
2021-12-09 08:36:03 +00:00
|
|
|
}
|
2021-12-09 16:29:23 +00:00
|
|
|
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
response := DeleteResponse{}
|
|
|
|
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &response))
|
|
|
|
assert.Equals(t, "ok", response.Status)
|
|
|
|
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
|
|
|
|
|
2021-12-09 08:36:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHandler_GetExternalAccountKeys(t *testing.T) {
|
2022-01-07 15:59:55 +00:00
|
|
|
prov := &linkedca.Provisioner{
|
|
|
|
Id: "provID",
|
|
|
|
Name: "provName",
|
|
|
|
}
|
2021-12-09 08:36:03 +00:00
|
|
|
type test struct {
|
|
|
|
ctx context.Context
|
|
|
|
db acme.DB
|
|
|
|
statusCode int
|
|
|
|
req *http.Request
|
|
|
|
resp GetExternalAccountKeysResponse
|
|
|
|
err *admin.Error
|
|
|
|
}
|
|
|
|
var tests = map[string]func(t *testing.T) test{
|
|
|
|
"fail/acmeDB.GetExternalAccountKeyByReference": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
chiCtx.URLParams.Add("ref", "an-external-key-reference")
|
|
|
|
req := httptest.NewRequest("GET", "/foo", nil)
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "an-external-key-reference", reference)
|
|
|
|
return nil, errors.New("force")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 500,
|
|
|
|
req: req,
|
|
|
|
db: db,
|
|
|
|
err: &admin.Error{
|
|
|
|
Status: 500,
|
|
|
|
Type: admin.ErrorServerInternalType.String(),
|
|
|
|
Detail: "the server experienced an internal error",
|
|
|
|
Message: "error retrieving external account key with reference 'an-external-key-reference': force",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/acmeDB.GetExternalAccountKeys": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
req := httptest.NewRequest("GET", "/foo", nil)
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockGetExternalAccountKeys: func(ctx context.Context, provisionerID string) ([]*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2022-01-06 13:09:35 +00:00
|
|
|
return nil, errors.New("force")
|
2021-12-09 08:36:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 500,
|
|
|
|
req: req,
|
|
|
|
db: db,
|
|
|
|
err: &admin.Error{
|
|
|
|
Status: 500,
|
|
|
|
Type: admin.ErrorServerInternalType.String(),
|
|
|
|
Detail: "the server experienced an internal error",
|
|
|
|
Message: "error retrieving external account keys: force",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/reference-not-found": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
chiCtx.URLParams.Add("ref", "an-external-key-reference")
|
|
|
|
req := httptest.NewRequest("GET", "/foo", nil)
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "an-external-key-reference", reference)
|
|
|
|
return nil, nil // returning nil; no key found
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 200,
|
|
|
|
req: req,
|
|
|
|
resp: GetExternalAccountKeysResponse{
|
2022-01-06 13:09:35 +00:00
|
|
|
EAKs: []*linkedca.EABKey{},
|
2021-12-09 08:36:03 +00:00
|
|
|
},
|
|
|
|
db: db,
|
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/reference-found": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
chiCtx.URLParams.Add("ref", "an-external-key-reference")
|
|
|
|
req := httptest.NewRequest("GET", "/foo", nil)
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
createdAt := time.Now().Add(-24 * time.Hour)
|
|
|
|
var boundAt time.Time
|
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockGetExternalAccountKeyByReference: func(ctx context.Context, provisionerID, reference string) (*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
assert.Equals(t, "an-external-key-reference", reference)
|
|
|
|
return &acme.ExternalAccountKey{
|
2022-01-07 15:59:55 +00:00
|
|
|
ID: "eakID",
|
|
|
|
ProvisionerID: "provID",
|
|
|
|
Reference: "an-external-key-reference",
|
|
|
|
CreatedAt: createdAt,
|
2021-12-09 08:36:03 +00:00
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 200,
|
|
|
|
req: req,
|
|
|
|
resp: GetExternalAccountKeysResponse{
|
|
|
|
EAKs: []*linkedca.EABKey{
|
|
|
|
{
|
|
|
|
Id: "eakID",
|
|
|
|
Provisioner: "provName",
|
|
|
|
Reference: "an-external-key-reference",
|
|
|
|
CreatedAt: timestamppb.New(createdAt),
|
|
|
|
BoundAt: timestamppb.New(boundAt),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
db: db,
|
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok/multiple-keys": func(t *testing.T) test {
|
|
|
|
chiCtx := chi.NewRouteContext()
|
|
|
|
chiCtx.URLParams.Add("prov", "provName")
|
|
|
|
req := httptest.NewRequest("GET", "/foo", nil)
|
|
|
|
ctx := context.WithValue(context.Background(), chi.RouteCtxKey, chiCtx)
|
2022-01-07 15:59:55 +00:00
|
|
|
ctx = context.WithValue(ctx, provisionerContextKey, prov)
|
2021-12-09 08:36:03 +00:00
|
|
|
createdAt := time.Now().Add(-24 * time.Hour)
|
|
|
|
var boundAt time.Time
|
|
|
|
boundAtSet := time.Now().Add(-12 * time.Hour)
|
|
|
|
db := &acme.MockDB{
|
2022-01-07 15:59:55 +00:00
|
|
|
MockGetExternalAccountKeys: func(ctx context.Context, provisionerID string) ([]*acme.ExternalAccountKey, error) {
|
|
|
|
assert.Equals(t, "provID", provisionerID)
|
2021-12-09 08:36:03 +00:00
|
|
|
return []*acme.ExternalAccountKey{
|
|
|
|
{
|
2022-01-07 15:59:55 +00:00
|
|
|
ID: "eakID1",
|
|
|
|
ProvisionerID: "provID",
|
|
|
|
Reference: "some-external-key-reference",
|
|
|
|
KeyBytes: []byte{1, 3, 3, 7},
|
|
|
|
CreatedAt: createdAt,
|
2021-12-09 08:36:03 +00:00
|
|
|
},
|
|
|
|
{
|
2022-01-07 15:59:55 +00:00
|
|
|
ID: "eakID2",
|
|
|
|
ProvisionerID: "provID",
|
|
|
|
Reference: "some-other-external-key-reference",
|
|
|
|
KeyBytes: []byte{1, 3, 3, 7},
|
|
|
|
CreatedAt: createdAt.Add(1 * time.Hour),
|
2021-12-09 08:36:03 +00:00
|
|
|
},
|
|
|
|
{
|
2022-01-07 15:59:55 +00:00
|
|
|
ID: "eakID3",
|
|
|
|
ProvisionerID: "provID",
|
|
|
|
Reference: "another-external-key-reference",
|
|
|
|
KeyBytes: []byte{1, 3, 3, 7},
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
BoundAt: boundAtSet,
|
|
|
|
AccountID: "accountID",
|
2021-12-09 08:36:03 +00:00
|
|
|
},
|
2022-01-06 13:09:35 +00:00
|
|
|
}, nil
|
2021-12-09 08:36:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return test{
|
|
|
|
ctx: ctx,
|
|
|
|
statusCode: 200,
|
|
|
|
req: req,
|
|
|
|
resp: GetExternalAccountKeysResponse{
|
|
|
|
EAKs: []*linkedca.EABKey{
|
|
|
|
{
|
|
|
|
Id: "eakID1",
|
|
|
|
Provisioner: "provName",
|
|
|
|
Reference: "some-external-key-reference",
|
|
|
|
CreatedAt: timestamppb.New(createdAt),
|
|
|
|
BoundAt: timestamppb.New(boundAt),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "eakID2",
|
|
|
|
Provisioner: "provName",
|
|
|
|
Reference: "some-other-external-key-reference",
|
|
|
|
CreatedAt: timestamppb.New(createdAt.Add(1 * time.Hour)),
|
|
|
|
BoundAt: timestamppb.New(boundAt),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Id: "eakID3",
|
|
|
|
Provisioner: "provName",
|
|
|
|
Reference: "another-external-key-reference",
|
|
|
|
CreatedAt: timestamppb.New(createdAt),
|
|
|
|
BoundAt: timestamppb.New(boundAtSet),
|
|
|
|
Account: "accountID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
db: db,
|
|
|
|
err: nil,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, prep := range tests {
|
|
|
|
tc := prep(t)
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
h := &Handler{
|
|
|
|
acmeDB: tc.db,
|
|
|
|
}
|
|
|
|
req := tc.req.WithContext(tc.ctx)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
h.GetExternalAccountKeys(w, req)
|
|
|
|
res := w.Result()
|
2021-12-09 14:24:14 +00:00
|
|
|
assert.Equals(t, tc.statusCode, res.StatusCode)
|
2021-12-09 08:36:03 +00:00
|
|
|
|
|
|
|
if res.StatusCode >= 400 {
|
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
adminErr := admin.Error{}
|
|
|
|
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &adminErr))
|
|
|
|
|
|
|
|
assert.Equals(t, tc.err.Type, adminErr.Type)
|
|
|
|
assert.Equals(t, tc.err.Message, adminErr.Message)
|
|
|
|
assert.Equals(t, tc.err.StatusCode(), res.StatusCode)
|
|
|
|
assert.Equals(t, tc.err.Detail, adminErr.Detail)
|
|
|
|
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
|
2021-12-09 22:15:38 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-09 08:36:03 +00:00
|
|
|
|
2021-12-09 22:15:38 +00:00
|
|
|
body, err := io.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
assert.FatalError(t, err)
|
2021-12-09 08:36:03 +00:00
|
|
|
|
2021-12-09 22:15:38 +00:00
|
|
|
response := GetExternalAccountKeysResponse{}
|
|
|
|
assert.FatalError(t, json.Unmarshal(bytes.TrimSpace(body), &response))
|
2021-12-09 08:36:03 +00:00
|
|
|
|
2021-12-09 22:15:38 +00:00
|
|
|
assert.Equals(t, []string{"application/json"}, res.Header["Content-Type"])
|
|
|
|
|
|
|
|
opts := []cmp.Option{cmpopts.IgnoreUnexported(linkedca.EABKey{}, timestamppb.Timestamp{})}
|
|
|
|
if !cmp.Equal(tc.resp, response, opts...) {
|
|
|
|
t.Errorf("h.GetExternalAccountKeys diff =\n%s", cmp.Diff(tc.resp, response, opts...))
|
2021-12-09 08:36:03 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|