Skip to content

Commit

Permalink
style: couple of stylistics changes (#3)
Browse files Browse the repository at this point in the history
- missing error checks
- change fields naming to follow go convention
- use time.Duration for the expires flag instead of string
- close request body
  • Loading branch information
jeremyletang committed Jul 20, 2021
1 parent f616c54 commit 2d3599e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 59 deletions.
16 changes: 10 additions & 6 deletions api/client/client.go
Expand Up @@ -31,9 +31,9 @@ type CreateOtsReq struct {
}

type CreateOtsRes struct {
Id string `json:"Id"`
ID string `json:"Id"`
ExpiresAt int64 `json:"ExpiresAt"`
ViewUrl *url.URL
ViewURL *url.URL
}

func CreateOts(encryptedBytes []byte, expiresIn uint32) (*CreateOtsRes, error) {
Expand All @@ -45,15 +45,19 @@ func CreateOts(encryptedBytes []byte, expiresIn uint32) (*CreateOtsRes, error) {
resBody := &CreateOtsRes{}

payloadBuf := new(bytes.Buffer)
json.NewEncoder(payloadBuf).Encode(reqBody)
err := json.NewEncoder(payloadBuf).Encode(reqBody)
if err != nil {
return nil, err
}

// TODO: Make part of config
res, err := http.Post("https://apiv2.beta.snipt.io/secrets", "application/json", payloadBuf)
if err != nil {
return nil, err
}
defer res.Body.Close()

err = decodeJson(res, &resBody)
err = decodeJSON(res, &resBody)
if err != nil {
return nil, err
}
Expand All @@ -63,12 +67,12 @@ func CreateOts(encryptedBytes []byte, expiresIn uint32) (*CreateOtsRes, error) {
return nil, err
}

resBody.ViewUrl = u
resBody.ViewURL = u

return resBody, nil
}

func decodeJson(res *http.Response, target interface{}) error {
func decodeJSON(res *http.Response, target interface{}) error {
statusOK := res.StatusCode >= 200 && res.StatusCode < 300

if !statusOK {
Expand Down
105 changes: 52 additions & 53 deletions cmd/new.go
Expand Up @@ -30,57 +30,57 @@ import (
"golang.org/x/crypto/ssh/terminal"
)

var expires string
const (
defaultExpiry = 24 * time.Hour
)

var (
expires time.Duration

// newCmd represents the new command
var newCmd = &cobra.Command{
Use: "new",
Short: "Create end-to-end encrypted secret",
Long: `
// newCmd represents the new command
newCmd = &cobra.Command{
Use: "new",
Short: "Create end-to-end encrypted secret",
Long: `
Encrypts a secret and makes it available for sharing via one-time URL.
The secret is stored encrypted for a specified duration which can range
from 5 minutes to 7 days (default is 72 hours). The secret gets deleted
from the server upon retrieval therefore can only be viewed once.
`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
duration, err := time.ParseDuration(expires)
if err != nil {
return err
}

if duration.Minutes() < 5 {
return errors.New("expiry must be at least 5 minutes")
}

if duration.Hours() > 168 {
return errors.New("expiry must be less than 7 days")
}

bytes, err := getInputBytes()
if err != nil {
return err
}

encryptedBytes, secretKey, err := encrypt.Bytes(bytes)
if err != nil {
return err
}

ots, err := client.CreateOts(encryptedBytes, uint32(duration.Seconds()))
if err != nil {
return err
}

expiresAt := time.Unix(ots.ExpiresAt, 0)

q := ots.ViewUrl.Query()
q.Set("p", base64.URLEncoding.EncodeToString(secretKey))
q.Set("ref", "cli")
ots.ViewUrl.RawQuery = q.Encode()

msg := fmt.Sprintf(`
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if expires.Minutes() < 5 {
return errors.New("expiry must be at least 5 minutes")
}

if expires.Hours() > 168 {
return errors.New("expiry must be less than 7 days")
}

bytes, err := getInputBytes()
if err != nil {
return err
}

encryptedBytes, secretKey, err := encrypt.Bytes(bytes)
if err != nil {
return err
}

ots, err := client.CreateOts(encryptedBytes, uint32(expires.Seconds()))
if err != nil {
return err
}

expiresAt := time.Unix(ots.ExpiresAt, 0)

q := ots.ViewURL.Query()
q.Set("p", base64.URLEncoding.EncodeToString(secretKey))
q.Set("ref", "cli")
ots.ViewURL.RawQuery = q.Encode()

fmt.Printf(`
Your secret is now available on the below URL.
%v
Expand All @@ -91,20 +91,19 @@ Please note that once retrieved, the secret will no longer
be available for viewing. If not viewed, the secret will
automatically expire at approximately %v.
`,
ots.ViewUrl,
expiresAt.Format("2 Jan 2006 15:04:05"),
)

fmt.Print(msg)
ots.ViewURL,
expiresAt.Format("2 Jan 2006 15:04:05"),
)

return nil
},
}
return nil
},
}
)

func init() {
rootCmd.AddCommand(newCmd)

newCmd.Flags().StringVarP(&expires, "expires", "x", "24h", "Secret will be deleted from the server after specified duration, supported units: s,m,h")
newCmd.Flags().DurationVarP(&expires, "expires", "x", defaultExpiry, "Secret will be deleted from the server after specified duration, supported units: s,m,h")
}

func getInputBytes() ([]byte, error) {
Expand Down

0 comments on commit 2d3599e

Please sign in to comment.