Skip to content

Commit

Permalink
NOISSUE - Fix issue cert response (#23)
Browse files Browse the repository at this point in the history
* Fix issue cert response

Signed-off-by: nyagamunene <[email protected]>

* Update sdk docs

Signed-off-by: nyagamunene <[email protected]>

* remove key from response

Signed-off-by: nyagamunene <[email protected]>

---------

Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene authored Sep 27, 2024
1 parent 0f074e3 commit 0ea242d
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 60 deletions.
11 changes: 9 additions & 2 deletions api/http/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,19 @@ func issueCertEndpoint(svc certs.Service) endpoint.Endpoint {
return issueCertRes{}, err
}

serialNumber, err := svc.IssueCert(ctx, req.entityID, req.TTL, req.IpAddrs, req.Options)
cert, err := svc.IssueCert(ctx, req.entityID, req.TTL, req.IpAddrs, req.Options)
if err != nil {
return issueCertRes{}, err
}

return issueCertRes{issued: true, SerialNumber: serialNumber}, nil
return issueCertRes{
SerialNumber: cert.SerialNumber,
Certificate: string(cert.Certificate),
ExpiryTime: cert.ExpiryTime,
EntityID: cert.EntityID,
Revoked: cert.Revoked,
issued: true,
}, nil
}
}

Expand Down
6 changes: 5 additions & 1 deletion api/http/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ func (res downloadCertRes) Empty() bool {
}

type issueCertRes struct {
SerialNumber string `json:"serial_number"`
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"`
issued bool
}

Expand Down
2 changes: 1 addition & 1 deletion api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (lm *loggingMiddleware) RetrieveCertDownloadToken(ctx context.Context, seri
return lm.svc.RetrieveCertDownloadToken(ctx, serialNumber)
}

func (lm *loggingMiddleware) IssueCert(ctx context.Context, entityID, ttl string, ipAddrs []string, options certs.SubjectOptions) (serialNumber string, err error) {
func (lm *loggingMiddleware) IssueCert(ctx context.Context, entityID, ttl string, ipAddrs []string, options certs.SubjectOptions) (cert certs.Certificate, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method issue_cert for took %s to complete", time.Since(begin))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (mm *metricsMiddleware) RetrieveCertDownloadToken(ctx context.Context, seri
return mm.svc.RetrieveCertDownloadToken(ctx, serialNumber)
}

func (mm *metricsMiddleware) IssueCert(ctx context.Context, entityID, ttl string, ipAddrs []string, options certs.SubjectOptions) (string, error) {
func (mm *metricsMiddleware) IssueCert(ctx context.Context, entityID, ttl string, ipAddrs []string, options certs.SubjectOptions) (certs.Certificate, error) {
defer func(begin time.Time) {
mm.counter.With("method", "issue_certificate").Add(1)
mm.latency.With("method", "issue_certificate").Observe(time.Since(begin).Seconds())
Expand Down
2 changes: 1 addition & 1 deletion certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type Service interface {
RetrieveCertDownloadToken(ctx context.Context, serialNumber string) (string, error)

// IssueCert issues a certificate from the database.
IssueCert(ctx context.Context, entityID, ttl string, ipAddrs []string, option SubjectOptions) (string, error)
IssueCert(ctx context.Context, entityID, ttl string, ipAddrs []string, option SubjectOptions) (Certificate, error)

// OCSP retrieves the OCSP response for a certificate.
OCSP(ctx context.Context, serialNumber string) (*Certificate, int, *x509.Certificate, error)
Expand Down
4 changes: 2 additions & 2 deletions cli/certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,12 @@ func NewCertsCmd() *cobra.Command {
}
}

serial, err := sdk.IssueCert(args[0], ttl, ipAddrs, option)
cert, err := sdk.IssueCert(args[0], ttl, ipAddrs, option)
if err != nil {
logErrorCmd(*cmd, err)
return
}
logJSONCmd(*cmd, serial)
logJSONCmd(*cmd, cert)
},
}

Expand Down
14 changes: 7 additions & 7 deletions cli/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ func TestIssueCertCmd(t *testing.T) {

ipAddrs := "[\"192.168.100.22\"]"

var sn sdk.SerialNumber
var cert sdk.Certificate
cases := []struct {
desc string
args []string
sdkErr errors.SDKError
errLogMessage string
logType outputLog
serial sdk.SerialNumber
cert sdk.Certificate
}{
{
desc: "issue cert successfully",
Expand All @@ -62,7 +62,7 @@ func TestIssueCertCmd(t *testing.T) {
ipAddrs,
},
logType: entityLog,
serial: sdk.SerialNumber{SerialNumber: serialNumber},
cert: sdk.Certificate{SerialNumber: serialNumber},
},
{
desc: "issue cert with invalid args",
Expand Down Expand Up @@ -92,19 +92,19 @@ func TestIssueCertCmd(t *testing.T) {
"{\"organization\":[\"organization_name\"]}",
},
logType: entityLog,
serial: sdk.SerialNumber{SerialNumber: serialNumber},
cert: sdk.Certificate{SerialNumber: serialNumber},
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
sdkCall := sdkMock.On("IssueCert", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.serial, tc.sdkErr)
sdkCall := sdkMock.On("IssueCert", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.cert, tc.sdkErr)
out := executeCommand(t, rootCmd, append([]string{issueCmd}, tc.args...)...)
switch tc.logType {
case entityLog:
err := json.Unmarshal([]byte(out), &sn)
err := json.Unmarshal([]byte(out), &cert)
assert.Nil(t, err)
assert.Equal(t, tc.serial, sn, fmt.Sprintf("%s unexpected response: expected: %v, got: %v", tc.desc, tc.serial, sn))
assert.Equal(t, tc.cert, cert, fmt.Sprintf("%s unexpected response: expected: %v, got: %v", tc.desc, tc.cert, cert))
case errLog:
assert.Equal(t, tc.errLogMessage, out, fmt.Sprintf("%s unexpected error response: expected %s got errLogMessage:%s", tc.desc, tc.errLogMessage, out))
case usageLog:
Expand Down
14 changes: 7 additions & 7 deletions mocks/service.go

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

16 changes: 9 additions & 7 deletions sdk/certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestIssueCert(t *testing.T) {
ttl string
ipAddrs []string
commonName string
svcresp string
svcresp certs.Certificate
svcerr error
err errors.SDKError
sdkCert sdk.Certificate
Expand All @@ -66,7 +66,9 @@ func TestIssueCert(t *testing.T) {
ttl: ttl,
ipAddrs: ipAddr,
commonName: commonName,
svcresp: serialNum,
svcresp: certs.Certificate{
SerialNumber: serialNum,
},
sdkCert: sdk.Certificate{
SerialNumber: serialNum,
},
Expand All @@ -79,7 +81,7 @@ func TestIssueCert(t *testing.T) {
ttl: ttl,
ipAddrs: ipAddr,
commonName: commonName,
svcresp: "",
svcresp: certs.Certificate{},
svcerr: certs.ErrCreateEntity,
err: errors.NewSDKErrorWithStatus(certs.ErrCreateEntity, http.StatusUnprocessableEntity),
},
Expand All @@ -89,7 +91,7 @@ func TestIssueCert(t *testing.T) {
ttl: ttl,
ipAddrs: ipAddr,
commonName: commonName,
svcresp: "",
svcresp: certs.Certificate{},
svcerr: certs.ErrMalformedEntity,
err: errors.NewSDKErrorWithStatus(certs.ErrMalformedEntity, http.StatusBadRequest),
},
Expand All @@ -98,7 +100,7 @@ func TestIssueCert(t *testing.T) {
entityID: id,
ttl: ttl,
commonName: commonName,
svcresp: serialNum,
svcresp: certs.Certificate{SerialNumber: serialNum},
sdkCert: sdk.Certificate{
SerialNumber: serialNum,
},
Expand All @@ -111,7 +113,7 @@ func TestIssueCert(t *testing.T) {
ttl: "",
ipAddrs: ipAddr,
commonName: commonName,
svcresp: serialNum,
svcresp: certs.Certificate{SerialNumber: serialNum},
sdkCert: sdk.Certificate{
SerialNumber: serialNum,
},
Expand All @@ -124,7 +126,7 @@ func TestIssueCert(t *testing.T) {
ttl: ttl,
ipAddrs: ipAddr,
commonName: "",
svcresp: "",
svcresp: certs.Certificate{},
svcerr: httpapi.ErrMissingCN,
err: errors.NewSDKErrorWithStatus(httpapi.ErrMissingCN, http.StatusBadRequest),
},
Expand Down
14 changes: 7 additions & 7 deletions sdk/mocks/sdk.go

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

26 changes: 11 additions & 15 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ type Options struct {
PostalCode []string `json:"postal_code"`
}

type SerialNumber struct {
SerialNumber string `json:"serial_number"`
}

type Token struct {
Token string `json:"token"`
}
Expand Down Expand Up @@ -113,9 +109,9 @@ type SDK interface {
// IssueCert issues a certificate for a thing required for mTLS.
//
// example:
// serial , _ := sdk.IssueCert("entityID", "10h", []string{"ipAddr1", "ipAddr2"}, sdk.Options{CommonName: "commonName"})
// fmt.Println(serial)
IssueCert(entityID, ttl string, ipAddrs []string, opts Options) (SerialNumber, errors.SDKError)
// cert , _ := sdk.IssueCert("entityID", "10h", []string{"ipAddr1", "ipAddr2"}, sdk.Options{CommonName: "commonName"})
// fmt.Println(cert)
IssueCert(entityID, ttl string, ipAddrs []string, opts Options) (Certificate, errors.SDKError)

// DownloadCert returns a certificate given certificate ID
//
Expand Down Expand Up @@ -167,32 +163,32 @@ type SDK interface {
OCSP(serialNumber string) (*ocsp.Response, errors.SDKError)
}

func (sdk mgSDK) IssueCert(entityID, ttl string, ipAddrs []string, opts Options) (SerialNumber, errors.SDKError) {
func (sdk mgSDK) IssueCert(entityID, ttl string, ipAddrs []string, opts Options) (Certificate, errors.SDKError) {
r := certReq{
IpAddrs: ipAddrs,
TTL: ttl,
Options: opts,
}
d, err := json.Marshal(r)
if err != nil {
return SerialNumber{}, errors.NewSDKError(err)
return Certificate{}, errors.NewSDKError(err)
}
url := fmt.Sprintf("%s/%s", issueCertEndpoint, entityID)

url, err = sdk.withQueryParams(sdk.certsURL, url, PageMetadata{CommonName: opts.CommonName})
if err != nil {
return SerialNumber{}, errors.NewSDKError(err)
return Certificate{}, errors.NewSDKError(err)
}
_, body, sdkerr := sdk.processRequest(http.MethodPost, url, d, nil, http.StatusCreated)
if sdkerr != nil {
return SerialNumber{}, sdkerr
return Certificate{}, sdkerr
}
var sn SerialNumber
if err := json.Unmarshal(body, &sn); err != nil {
return SerialNumber{}, errors.NewSDKError(err)
var cert Certificate
if err := json.Unmarshal(body, &cert); err != nil {
return Certificate{}, errors.NewSDKError(err)
}

return sn, nil
return cert, nil
}

func (sdk mgSDK) DownloadCert(token, serialNumber string) (CertificateBundle, errors.SDKError) {
Expand Down
Loading

0 comments on commit 0ea242d

Please sign in to comment.