Skip to content

Commit

Permalink
feat: support custom prefix (#11)
Browse files Browse the repository at this point in the history
* add custom prefix config

* feat: add support for custom prefix
  • Loading branch information
adikari authored Nov 22, 2022
1 parent b376f4f commit b4f1f9d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
16 changes: 13 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
type rawConfig struct {
Provider string
Service string
Prefix string

Config map[string]map[string]string
Secret map[string]map[string]string
Expand Down Expand Up @@ -62,7 +63,6 @@ func Load(param LoadConfigInput) (*Config, error) {
c.Service = rc.Service
c.Stage = param.Stage
c.Provider = rc.Provider
c.Prefix = getPrefix(param.Stage, c.Service)

if c.Provider == "" {
c.Provider = store.SsmProvider
Expand All @@ -74,6 +74,11 @@ func Load(param LoadConfigInput) (*Config, error) {
return nil, errors.Wrap(err, "failed to variables for interpolation")
}

c.Prefix, err = Interpolate(getPrefix(param.Stage, c.Service, rc.Prefix), variables)
if err != nil {
return nil, errors.Wrap(err, "failed to interpolate prefix")
}

for key, value := range rc.Config["defaults"] {
val, err := Interpolate(value, variables)

Expand Down Expand Up @@ -135,8 +140,13 @@ func formatPath(prefix string, key string) string {
return fmt.Sprintf("%s%s", prefix, key)
}

func getPrefix(stage string, service string) string {
return fmt.Sprintf("/%s/%s/", stage, service)
func getPrefix(stage string, service string, defaultPrefix string) string {
if defaultPrefix == "" {
return fmt.Sprintf("/%s/%s/", stage, service)
}

// TODO: validate prefix starts and ends with /
return defaultPrefix
}

func validateConfig(rc rawConfig) error {
Expand Down
23 changes: 23 additions & 0 deletions example/custom-prefix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
service: safebox
provider: ssm

prefix: "/{{.stage}}/custom/prefix/{{.service}}/"

cloudformation-stacks:
- "{{.stage}}-shared-infra-SharedInfraServerless"

config:
defaults:
DB_NAME: "database name updated"
CF_OUTPUT_API_ENDPOINT: "{{.internalDomainName}}"
AWS_ACCOUNT: "{{.account}}"

shared:
SHARED_KEY: "shared key"

secret:
defaults:
API_KEY: "key of the api endpoint"

shared:
APOLLO_KEY: "apollo key"
2 changes: 1 addition & 1 deletion npm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adikari/safebox",
"version": "1.1.9",
"version": "1.1.10",
"description": "A Fast and Flexible secret manager built with love by adikari in Go",
"main": "index.js",
"bin": "./run.js",
Expand Down

0 comments on commit b4f1f9d

Please sign in to comment.