Skip to content

Commit

Permalink
chore: handle errors
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Sep 30, 2022
1 parent fcab318 commit a0bbc42
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 43 deletions.
20 changes: 14 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

c "github.com/adikari/safebox/v2/config"
"github.com/adikari/safebox/v2/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -17,10 +18,11 @@ var (
)

var rootCmd = &cobra.Command{
Use: "safebox",
Short: "SafeBox is a secret manager CLI program",
Long: `A Fast and Flexible secret manager built with love by adikari in Go.`,
PersistentPreRun: prerun,
Use: "safebox",
Short: "SafeBox is a secret manager CLI program",
Long: `A Fast and Flexible secret manager built with love by adikari in Go.`,
PersistentPreRunE: prerun,
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
Expand All @@ -44,9 +46,15 @@ func Execute(version string) {
}
}

func prerun(cmd *cobra.Command, args []string) {
c, _ := loadConfig()
func prerun(cmd *cobra.Command, args []string) error {
c, err := loadConfig()

if err != nil {
return errors.Wrap(err, "failed to load config")
}

Config = c
return nil
}

func loadConfig() (*c.Config, error) {
Expand Down
14 changes: 9 additions & 5 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package cmd

import (
"log"

"github.com/pkg/errors"
"github.com/spf13/cobra"
)

Expand All @@ -19,8 +18,13 @@ func init() {
}

func execute(cmd *cobra.Command, args []string) error {
_, err := getStore()
log.Printf("%v", Config)
log.Printf("%v", err)
store, err := getStore()

if err != nil {
return errors.Wrap(err, "failed to instantiate store")
}

store.WriteMany(Config.Configs)

return nil
}
29 changes: 19 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package config

import (
"errors"
"fmt"
"io/ioutil"

"github.com/adikari/safebox/v2/store"
"gopkg.in/yaml.v2"
)

Expand All @@ -18,7 +18,7 @@ type rawConfig struct {
type Config struct {
Provider string
Service string
Config map[string]string
Configs []store.ConfigInput
}

type LoadParam struct {
Expand All @@ -30,15 +30,15 @@ func Load(param LoadParam) (*Config, error) {
yamlFile, err := ioutil.ReadFile(param.Path)

if err != nil {
return nil, errors.New("Could not find config file: " + param.Path)
return nil, fmt.Errorf("missing safebox config file %s", param.Path)
}

rc := rawConfig{}

err = yaml.Unmarshal(yamlFile, &rc)

if err != nil {
return nil, errors.New("Could not parse config file")
return nil, fmt.Errorf("could not parse safebox config file %s", param.Path)
}

c := Config{}
Expand All @@ -50,29 +50,38 @@ func Load(param LoadParam) (*Config, error) {
}

func parseConfig(rc rawConfig, c *Config, param LoadParam) {
c.Config = map[string]string{}
temp := map[string]string{}
c.Configs = []store.ConfigInput{}

defaultConfig := rc.Config["defaults"]
sharedConfig := rc.Config["shared"]
envConfig := rc.Config[param.Stage]

for key, value := range defaultConfig {
c.Config[formatKey(param.Stage, c.Service, key)] = value
temp[formatPath(param.Stage, c.Service, key)] = value
}

for key, value := range sharedConfig {
c.Config[formatSharedKey(param.Stage, key)] = value
temp[formatSharedPath(param.Stage, key)] = value
}

for key, value := range envConfig {
c.Config[formatKey(param.Stage, c.Service, key)] = value
temp[formatPath(param.Stage, c.Service, key)] = value
}

for key, value := range temp {
c.Configs = append(c.Configs, store.ConfigInput{
Key: key,
Value: value,
Secret: false,
})
}
}

func formatSharedKey(stage string, key string) string {
func formatSharedPath(stage string, key string) string {
return fmt.Sprintf("/%s/shared/%s", stage, key)
}

func formatKey(stage string, service string, key string) string {
func formatPath(stage string, service string, key string) string {
return fmt.Sprintf("/%s/%s/%s", stage, service, key)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.19

require (
github.com/aws/aws-sdk-go v1.44.107
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.5.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
32 changes: 23 additions & 9 deletions store/ssmstore.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package store

import (
"log"
"strconv"
"strings"

Expand All @@ -26,9 +27,14 @@ func NewSSMStore() (*SSMStore, error) {
}, nil
}

func (s *SSMStore) Write(input WriteConfigInput) error {
func (s *SSMStore) WriteMany(input []ConfigInput) error {
log.Printf("%v", input)
return nil
}

func (s *SSMStore) Write(input ConfigInput) error {
version := 1
current, err := s.Read(input.ConfigId)
current, err := s.Read(input.Key)

if err != nil && err != ConfigNotFoundError {
return err
Expand Down Expand Up @@ -60,14 +66,14 @@ func (s *SSMStore) Write(input WriteConfigInput) error {
return nil
}

func (s *SSMStore) Delete(id ConfigId) error {
_, err := s.Read(id)
func (s *SSMStore) Delete(key string) error {
_, err := s.Read(key)
if err != nil {
return err
}

deleteParameterInput := &ssm.DeleteParameterInput{
Name: aws.String(id.Key),
Name: aws.String(key),
}

_, err = s.svc.DeleteParameter(deleteParameterInput)
Expand All @@ -78,9 +84,17 @@ func (s *SSMStore) Delete(id ConfigId) error {
return nil
}

func (s *SSMStore) Read(id ConfigId) (Config, error) {
func (s *SSMStore) ReadMany(keys []string) ([]Config, error) {
return nil, nil
}

func (s *SSMStore) ReadAll() ([]Config, error) {
return nil, nil
}

func (s *SSMStore) Read(key string) (Config, error) {
getParametersInput := &ssm.GetParametersInput{
Names: []*string{aws.String(id.Key)},
Names: []*string{aws.String(key)},
WithDecryption: aws.Bool(true),
}

Expand All @@ -106,14 +120,14 @@ func (s *SSMStore) Read(id ConfigId) (Config, error) {
{
Key: aws.String("Path"),
Option: aws.String("OneLevel"),
Values: []*string{aws.String(basePath(id.Key))},
Values: []*string{aws.String(basePath(key))},
},
},
}

if err := s.svc.DescribeParametersPages(describeParametersInput, func(o *ssm.DescribeParametersOutput, lastPage bool) bool {
for _, param := range o.Parameters {
if *param.Name == id.Key {
if *param.Name == key {
parameter = param
return false
}
Expand Down
21 changes: 8 additions & 13 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ type Config struct {
Metadata
}

type ConfigId struct {
Path string
Key string
}

type WriteConfigInput struct {
ConfigId
type ConfigInput struct {
Key string
Value string
Secret bool
}
Expand All @@ -33,10 +28,10 @@ var (
)

type Store interface {
Write(input WriteConfigInput) error
// WriteMany(input []WriteConfigInput) error
Read(id ConfigId) (Config, error)
// ReadMany(id []ConfigId) ([]Config, error)
// ReadAll() ([]Config, error)
Delete(id ConfigId) error
Write(input ConfigInput) error
WriteMany(input []ConfigInput) error
Read(key string) (Config, error)
ReadMany(keys []string) ([]Config, error)
ReadAll() ([]Config, error)
Delete(key string) error
}

0 comments on commit a0bbc42

Please sign in to comment.