Skip to content

Commit

Permalink
initial commit (#1708)
Browse files Browse the repository at this point in the history
Signed-off-by: rodneyosodo <socials@rodneyosodo.com>

Signed-off-by: rodneyosodo <socials@rodneyosodo.com>
Co-authored-by: rodneyosodo <socials@rodneyosodo.com>
  • Loading branch information
rodneyosodo and rodneyosodo authored Jan 24, 2023
1 parent d008ae5 commit 5f9b3c9
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
1 change: 1 addition & 0 deletions api/openapi/certs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ components:
format: uuid
ttl:
type: string
example: "10h"

responses:
ServiceError:
Expand Down
10 changes: 9 additions & 1 deletion certs/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package api

import "github.com/mainflux/mainflux/internal/apiutil"
import (
"time"

"github.com/mainflux/mainflux/internal/apiutil"
)

const maxLimitSize = 100

Expand All @@ -26,6 +30,10 @@ func (req addCertsReq) validate() error {
return apiutil.ErrMissingCertData
}

if _, err := time.ParseDuration(req.TTL); err != nil {
return apiutil.ErrInvalidCertData
}

return nil
}

Expand Down
1 change: 1 addition & 0 deletions certs/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func encodeError(_ context.Context, err error, w http.ResponseWriter) {
case errors.Contains(err, errors.ErrMalformedEntity),
err == apiutil.ErrMissingID,
err == apiutil.ErrMissingCertData,
err == apiutil.ErrInvalidCertData,
err == apiutil.ErrLimitSize:
w.WriteHeader(http.StatusBadRequest)
case errors.Contains(err, errors.ErrConflict):
Expand Down
3 changes: 1 addition & 2 deletions certs/pki/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package pki

import (
"encoding/json"
"fmt"
"time"

"github.com/hashicorp/vault/api"
Expand Down Expand Up @@ -101,7 +100,7 @@ func NewVaultClient(token, host, path, role string) (Agent, error) {
func (p *pkiAgent) IssueCert(cn, ttl string) (Cert, error) {
cReq := certReq{
CommonName: cn,
TTL: fmt.Sprintf("%sh", ttl),
TTL: ttl,
}

var certIssueReq map[string]interface{}
Expand Down
11 changes: 4 additions & 7 deletions cli/certs.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cli

import (
"strconv"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -45,10 +43,10 @@ var cmdCerts = []cobra.Command{

// NewCertsCmd returns certificate command.
func NewCertsCmd() *cobra.Command {
var ttl uint32
var ttl string

issueCmd := cobra.Command{
Use: "issue <thing_id> <user_auth_token> [--ttl=8760]",
Use: "issue <thing_id> <user_auth_token> [--ttl=8760h]",
Short: "Issue certificate",
Long: `Issues new certificate for a thing`,
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -58,9 +56,8 @@ func NewCertsCmd() *cobra.Command {
}

thingID := args[0]
valid := strconv.FormatUint(uint64(ttl), 10)

c, err := sdk.IssueCert(thingID, valid, args[1])
c, err := sdk.IssueCert(thingID, ttl, args[1])
if err != nil {
logError(err)
return
Expand All @@ -69,7 +66,7 @@ func NewCertsCmd() *cobra.Command {
},
}

issueCmd.Flags().Uint32Var(&ttl, "ttl", 8760, "certificate time to live in hours")
issueCmd.Flags().StringVar(&ttl, "ttl", "8760h", "certificate time to live in duration")

cmd := cobra.Command{
Use: "certs [issue | get | revoke ]",
Expand Down
3 changes: 3 additions & 0 deletions internal/apiutil/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ var (
// ErrMissingCertData indicates missing cert data (ttl).
ErrMissingCertData = errors.New("missing certificate data")

// ErrInvalidCertData indicates invalid cert data (ttl).
ErrInvalidCertData = errors.New("invalid certificate data")

// ErrInvalidTopic indicates an invalid subscription topic.
ErrInvalidTopic = errors.New("invalid Subscription topic")

Expand Down

0 comments on commit 5f9b3c9

Please sign in to comment.