Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Couple of stylistics changes #3

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions api/client/client.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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