Skip to content

Commit 9fdef64

Browse files
committed
Admin level API for provisioner mgmt v1
1 parent 7e82bd6 commit 9fdef64

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+4906
-3873
lines changed

.golangci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ linters-settings:
88
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
99
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
1010
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
11-
golint:
11+
revive:
1212
min-confidence: 0
1313
gocyclo:
1414
min-complexity: 10
@@ -44,7 +44,7 @@ linters:
4444
disable-all: true
4545
enable:
4646
- gofmt
47-
- golint
47+
- revive
4848
- govet
4949
- misspell
5050
- ineffassign

acme/api/middleware.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -288,13 +288,13 @@ func (h *Handler) lookupProvisioner(next nextHTTP) nextHTTP {
288288
return func(w http.ResponseWriter, r *http.Request) {
289289
ctx := r.Context()
290290

291-
name := chi.URLParam(r, "provisionerID")
292-
provID, err := url.PathUnescape(name)
291+
nameEscaped := chi.URLParam(r, "provisionerID")
292+
name, err := url.PathUnescape(nameEscaped)
293293
if err != nil {
294-
api.WriteError(w, acme.WrapErrorISE(err, "error url unescaping provisioner id '%s'", name))
294+
api.WriteError(w, acme.WrapErrorISE(err, "error url unescaping provisioner name '%s'", nameEscaped))
295295
return
296296
}
297-
p, err := h.ca.LoadProvisionerByID("acme/" + provID)
297+
p, err := h.ca.LoadProvisionerByName(name)
298298
if err != nil {
299299
api.WriteError(w, err)
300300
return

acme/common.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
// CertificateAuthority is the interface implemented by a CA authority.
1212
type CertificateAuthority interface {
1313
Sign(cr *x509.CertificateRequest, opts provisioner.SignOptions, signOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
14-
LoadProvisionerByID(string) (provisioner.Interface, error)
14+
LoadProvisionerByName(string) (provisioner.Interface, error)
1515
}
1616

1717
// Clock that returns time in UTC rounded to seconds.

acme/db/nosql/nosql.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ var (
2323

2424
// DB is a struct that implements the AcmeDB interface.
2525
type DB struct {
26-
db nosqlDB.DB
27-
authorityID string
26+
db nosqlDB.DB
2827
}
2928

3029
// New configures and returns a new ACME DB backend implemented using a nosql DB.
31-
func New(db nosqlDB.DB, authorityID string) (*DB, error) {
30+
func New(db nosqlDB.DB) (*DB, error) {
3231
tables := [][]byte{accountTable, accountByKeyIDTable, authzTable,
3332
challengeTable, nonceTable, orderTable, ordersByAccountIDTable, certTable}
3433
for _, b := range tables {
@@ -37,7 +36,7 @@ func New(db nosqlDB.DB, authorityID string) (*DB, error) {
3736
string(b))
3837
}
3938
}
40-
return &DB{db, authorityID}, nil
39+
return &DB{db}, nil
4140
}
4241

4342
// save writes the new data to the database, overwriting the old data if it

acme/order_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@ func TestOrder_UpdateStatus(t *testing.T) {
261261
}
262262

263263
type mockSignAuth struct {
264-
sign func(csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
265-
loadProvisionerByID func(string) (provisioner.Interface, error)
266-
ret1, ret2 interface{}
267-
err error
264+
sign func(csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error)
265+
loadProvisionerByName func(string) (provisioner.Interface, error)
266+
ret1, ret2 interface{}
267+
err error
268268
}
269269

270270
func (m *mockSignAuth) Sign(csr *x509.CertificateRequest, signOpts provisioner.SignOptions, extraOpts ...provisioner.SignOption) ([]*x509.Certificate, error) {
@@ -276,9 +276,9 @@ func (m *mockSignAuth) Sign(csr *x509.CertificateRequest, signOpts provisioner.S
276276
return []*x509.Certificate{m.ret1.(*x509.Certificate), m.ret2.(*x509.Certificate)}, m.err
277277
}
278278

279-
func (m *mockSignAuth) LoadProvisionerByID(id string) (provisioner.Interface, error) {
280-
if m.loadProvisionerByID != nil {
281-
return m.loadProvisionerByID(id)
279+
func (m *mockSignAuth) LoadProvisionerByName(name string) (provisioner.Interface, error) {
280+
if m.loadProvisionerByName != nil {
281+
return m.loadProvisionerByName(name)
282282
}
283283
return m.ret1.(provisioner.Interface), m.err
284284
}

api/api.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type Authority interface {
3939
Renew(peer *x509.Certificate) ([]*x509.Certificate, error)
4040
Rekey(peer *x509.Certificate, pk crypto.PublicKey) ([]*x509.Certificate, error)
4141
LoadProvisionerByCertificate(*x509.Certificate) (provisioner.Interface, error)
42-
LoadProvisionerByID(string) (provisioner.Interface, error)
42+
LoadProvisionerByName(string) (provisioner.Interface, error)
4343
GetProvisioners(cursor string, limit int) (provisioner.List, string, error)
4444
Revoke(context.Context, *authority.RevokeOptions) error
4545
GetEncryptedKey(kid string) (string, error)
@@ -418,7 +418,7 @@ func LogCertificate(w http.ResponseWriter, cert *x509.Certificate) {
418418
if len(val.CredentialID) > 0 {
419419
m["provisioner"] = fmt.Sprintf("%s (%s)", val.Name, val.CredentialID)
420420
} else {
421-
m["provisioner"] = fmt.Sprintf("%s", val.Name)
421+
m["provisioner"] = string(val.Name)
422422
}
423423
break
424424
}

api/api_test.go

+12-4
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ type mockProvisioner struct {
430430
ret1, ret2, ret3 interface{}
431431
err error
432432
getID func() string
433+
getIDForToken func() string
433434
getTokenID func(string) (string, error)
434435
getName func() string
435436
getType func() provisioner.Type
@@ -452,6 +453,13 @@ func (m *mockProvisioner) GetID() string {
452453
return m.ret1.(string)
453454
}
454455

456+
func (m *mockProvisioner) GetIDForToken() string {
457+
if m.getIDForToken != nil {
458+
return m.getIDForToken()
459+
}
460+
return m.ret1.(string)
461+
}
462+
455463
func (m *mockProvisioner) GetTokenID(token string) (string, error) {
456464
if m.getTokenID != nil {
457465
return m.getTokenID(token)
@@ -553,7 +561,7 @@ type mockAuthority struct {
553561
renew func(cert *x509.Certificate) ([]*x509.Certificate, error)
554562
rekey func(oldCert *x509.Certificate, pk crypto.PublicKey) ([]*x509.Certificate, error)
555563
loadProvisionerByCertificate func(cert *x509.Certificate) (provisioner.Interface, error)
556-
loadProvisionerByID func(provID string) (provisioner.Interface, error)
564+
loadProvisionerByName func(name string) (provisioner.Interface, error)
557565
getProvisioners func(nextCursor string, limit int) (provisioner.List, string, error)
558566
revoke func(context.Context, *authority.RevokeOptions) error
559567
getEncryptedKey func(kid string) (string, error)
@@ -633,9 +641,9 @@ func (m *mockAuthority) LoadProvisionerByCertificate(cert *x509.Certificate) (pr
633641
return m.ret1.(provisioner.Interface), m.err
634642
}
635643

636-
func (m *mockAuthority) LoadProvisionerByID(provID string) (provisioner.Interface, error) {
637-
if m.loadProvisionerByID != nil {
638-
return m.loadProvisionerByID(provID)
644+
func (m *mockAuthority) LoadProvisionerByName(name string) (provisioner.Interface, error) {
645+
if m.loadProvisionerByName != nil {
646+
return m.loadProvisionerByName(name)
639647
}
640648
return m.ret1.(provisioner.Interface), m.err
641649
}

api/errors.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
"github.com/pkg/errors"
1010
"github.com/smallstep/certificates/acme"
11-
"github.com/smallstep/certificates/authority/mgmt"
11+
"github.com/smallstep/certificates/authority/admin"
1212
"github.com/smallstep/certificates/errs"
1313
"github.com/smallstep/certificates/logging"
1414
"github.com/smallstep/certificates/scep"
@@ -20,8 +20,8 @@ func WriteError(w http.ResponseWriter, err error) {
2020
case *acme.Error:
2121
acme.WriteError(w, k)
2222
return
23-
case *mgmt.Error:
24-
mgmt.WriteError(w, k)
23+
case *admin.Error:
24+
admin.WriteError(w, k)
2525
return
2626
case *scep.Error:
2727
w.Header().Set("Content-Type", "text/plain")

api/utils.go

+36
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@ package api
33
import (
44
"encoding/json"
55
"io"
6+
"io/ioutil"
67
"log"
78
"net/http"
89

910
"github.com/smallstep/certificates/errs"
1011
"github.com/smallstep/certificates/logging"
12+
"google.golang.org/protobuf/encoding/protojson"
13+
"google.golang.org/protobuf/proto"
1114
)
1215

1316
// EnableLogger is an interface that enables response logging for an object.
@@ -64,6 +67,29 @@ func JSONStatus(w http.ResponseWriter, v interface{}, status int) {
6467
LogEnabledResponse(w, v)
6568
}
6669

70+
// ProtoJSON writes the passed value into the http.ResponseWriter.
71+
func ProtoJSON(w http.ResponseWriter, m proto.Message) {
72+
ProtoJSONStatus(w, m, http.StatusOK)
73+
}
74+
75+
// ProtoJSONStatus writes the given value into the http.ResponseWriter and the
76+
// given status is written as the status code of the response.
77+
func ProtoJSONStatus(w http.ResponseWriter, m proto.Message, status int) {
78+
w.Header().Set("Content-Type", "application/json")
79+
w.WriteHeader(status)
80+
81+
b, err := protojson.Marshal(m)
82+
if err != nil {
83+
LogError(w, err)
84+
return
85+
}
86+
if _, err := w.Write(b); err != nil {
87+
LogError(w, err)
88+
return
89+
}
90+
//LogEnabledResponse(w, v)
91+
}
92+
6793
// ReadJSON reads JSON from the request body and stores it in the value
6894
// pointed by v.
6995
func ReadJSON(r io.Reader, v interface{}) error {
@@ -72,3 +98,13 @@ func ReadJSON(r io.Reader, v interface{}) error {
7298
}
7399
return nil
74100
}
101+
102+
// ReadProtoJSON reads JSON from the request body and stores it in the value
103+
// pointed by v.
104+
func ReadProtoJSON(r io.Reader, m proto.Message) error {
105+
data, err := ioutil.ReadAll(r)
106+
if err != nil {
107+
return errs.Wrap(http.StatusBadRequest, err, "error reading request body")
108+
}
109+
return protojson.Unmarshal(data, m)
110+
}

0 commit comments

Comments
 (0)