Skip to content

Commit

Permalink
rename signcsr to issuefromcsr
Browse files Browse the repository at this point in the history
Signed-off-by: nyagamunene <stevenyaga2014@gmail.com>
  • Loading branch information
nyagamunene committed Dec 2, 2024
1 parent 670bc52 commit 5ccc6f5
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 162 deletions.
13 changes: 6 additions & 7 deletions api/http/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,25 +328,24 @@ func createCSREndpoint(svc certs.Service) endpoint.Endpoint {
}
}

func signCSREndpoint(svc certs.Service) endpoint.Endpoint {
func issueFromCSREndpoint(svc certs.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(SignCSRReq)
req := request.(IssueFromCSRReq)
if err := req.validate(); err != nil {
return signCSRRes{signed: false}, err
return issueFromCSRRes{}, err
}

cert, err := svc.SignCSR(ctx, req.entityID, req.ttl, certs.CSR{CSR: []byte(req.CSR), PrivateKey: []byte(req.PrivateKey)})
cert, err := svc.IssueFromCSR(ctx, req.entityID, req.ttl, certs.CSR{CSR: []byte(req.CSR), PrivateKey: []byte(req.PrivateKey)})
if err != nil {
return signCSRRes{signed: false}, err
return issueFromCSRRes{}, err
}

return signCSRRes{
return issueFromCSRRes{
SerialNumber: cert.SerialNumber,
Certificate: string(cert.Certificate),
Revoked: cert.Revoked,
ExpiryTime: cert.ExpiryTime,
EntityID: cert.EntityID,
signed: true,
}, nil
}
}
4 changes: 2 additions & 2 deletions api/http/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ func (req createCSRReq) validate() error {
return nil
}

type SignCSRReq struct {
type IssueFromCSRReq struct {
entityID string
ttl string
CSR string `json:"csr"`
PrivateKey string `json:"private_key"`
}

func (req SignCSRReq) validate() error {
func (req IssueFromCSRReq) validate() error {
if req.entityID == "" {
return errors.Wrap(certs.ErrMalformedEntity, ErrMissingEntityID)
}
Expand Down
9 changes: 4 additions & 5 deletions api/http/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,23 +218,22 @@ func (res createCSRRes) Empty() bool {
return false
}

type signCSRRes struct {
type issueFromCSRRes struct {
SerialNumber string `json:"serial_number"`
Certificate string `json:"certificate,omitempty"`
Revoked bool `json:"revoked"`
ExpiryTime time.Time `json:"expiry_time"`
EntityID string `json:"entity_id"`
signed bool
}

func (res signCSRRes) Code() int {
func (res issueFromCSRRes) Code() int {
return http.StatusOK
}

func (res signCSRRes) Headers() map[string]string {
func (res issueFromCSRRes) Headers() map[string]string {
return map[string]string{}
}

func (res signCSRRes) Empty() bool {
func (res issueFromCSRRes) Empty() bool {
return false
}
10 changes: 5 additions & 5 deletions api/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ func MakeHandler(svc certs.Service, logger *slog.Logger, instanceID string) http
opts...,
), "create_csr").ServeHTTP)
r.Post("/{entityID}", otelhttp.NewHandler(kithttp.NewServer(
signCSREndpoint(svc),
decodeSignCSR,
issueFromCSREndpoint(svc),
decodeIssueFromCSR,
EncodeResponse,
opts...,
), "sign_csr").ServeHTTP)
), "issue_from_csr").ServeHTTP)
})
})

Expand Down Expand Up @@ -317,13 +317,13 @@ func decodeCreateCSR(_ context.Context, r *http.Request) (interface{}, error) {
return req, nil
}

func decodeSignCSR(_ context.Context, r *http.Request) (interface{}, error) {
func decodeIssueFromCSR(_ context.Context, r *http.Request) (interface{}, error) {
t, err := readStringQuery(r, ttl, "")
if err != nil {
return nil, err
}

req := SignCSRReq{
req := IssueFromCSRReq{
entityID: chi.URLParam(r, "entityID"),
ttl: t,
}
Expand Down
6 changes: 3 additions & 3 deletions api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ func (lm *loggingMiddleware) CreateCSR(ctx context.Context, metadata certs.CSRMe
return lm.svc.CreateCSR(ctx, metadata, privKey)
}

func (lm *loggingMiddleware) SignCSR(ctx context.Context, entityID, ttl string, csr certs.CSR) (c certs.Certificate, err error) {
func (lm *loggingMiddleware) IssueFromCSR(ctx context.Context, entityID, ttl string, csr certs.CSR) (c certs.Certificate, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method sign_csr took %s to complete", time.Since(begin))
message := fmt.Sprintf("Method issue_from_csr took %s to complete", time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(message)
}(time.Now())
return lm.svc.SignCSR(ctx, entityID, ttl, csr)
return lm.svc.IssueFromCSR(ctx, entityID, ttl, csr)
}
8 changes: 4 additions & 4 deletions api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ func (mm *metricsMiddleware) CreateCSR(ctx context.Context, metadata certs.CSRMe
return mm.svc.CreateCSR(ctx, metadata, privKey)
}

func (mm *metricsMiddleware) SignCSR(ctx context.Context, entityID, ttl string, csr certs.CSR) (certs.Certificate, error) {
func (mm *metricsMiddleware) IssueFromCSR(ctx context.Context, entityID, ttl string, csr certs.CSR) (certs.Certificate, error) {
defer func(begin time.Time) {
mm.counter.With("method", "sign_csr").Add(1)
mm.latency.With("method", "sign_csr").Observe(time.Since(begin).Seconds())
mm.counter.With("method", "issue_from_csr").Add(1)
mm.latency.With("method", "issue_from_csr").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.SignCSR(ctx, entityID, ttl, csr)
return mm.svc.IssueFromCSR(ctx, entityID, ttl, csr)
}
6 changes: 3 additions & 3 deletions certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ type Service interface {
// RemoveCert deletes a cert for a provided entityID.
RemoveCert(ctx context.Context, entityId string) error

// CreateCSR creates a new Certificate Signing Request
// CreateCSR creates a new Certificate Signing Request.
CreateCSR(ctx context.Context, metadata CSRMetadata, privKey any) (CSR, error)

// SignCSR parses and signs a CSR
SignCSR(ctx context.Context, entityID, ttl string, csr CSR) (Certificate, error)
// IssueFromCSR creates a certificate from a given CSR.
IssueFromCSR(ctx context.Context, entityID, ttl string, csr CSR) (Certificate, error)
}

type Repository interface {
Expand Down
10 changes: 5 additions & 5 deletions cli/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,9 @@ var cmdCerts = []cobra.Command{
},
},
{
Use: "sign <entity_id> <ttl> <path_to_csr> <private_key_path>",
Short: "Sign CSR",
Long: `Signs a CSR for a given csr.`,
Use: "issue-csr <entity_id> <ttl> <path_to_csr> <private_key_path>",
Short: "Issue from CSR",
Long: `issues a certificate for a given csr.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 4 {
logUsageCmd(*cmd, cmd.Use)
Expand All @@ -291,7 +291,7 @@ var cmdCerts = []cobra.Command{
return
}

cert, err := sdk.SignCSR(args[0], args[1], string(csrData), string(privData))
cert, err := sdk.IssueFromCSR(args[0], args[1], string(csrData), string(privData))
if err != nil {
logErrorCmd(*cmd, err)
return
Expand Down Expand Up @@ -341,7 +341,7 @@ func NewCertsCmd() *cobra.Command {
issueCmd.Flags().StringVar(&ttl, "ttl", "8760h", "certificate time to live in duration")

cmd := cobra.Command{
Use: "certs [issue | get | revoke | renew | ocsp | token | download | download-ca | download-ca | csr | sign]",
Use: "certs [issue | get | revoke | renew | ocsp | token | download | download-ca | download-ca | csr | issue-csr]",
Short: "Certificates management",
Long: `Certificates management: issue, get all, get by entity ID, revoke, renew, OCSP, token, download.`,
}
Expand Down
118 changes: 59 additions & 59 deletions mocks/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5ccc6f5

Please sign in to comment.