Skip to content

Commit

Permalink
Allowing {name} to be replaced in key formats and labels.
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofferahl committed Jan 26, 2024
1 parent b6c53f2 commit 178b5a8
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 13 deletions.
3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@
- [x] Fix: Remove support for optional properties, define them in lower layer instead??? should be an error
- [x] Fix: Bad logging (racoon WARN[0000] dotenv file local.env was not found ... racoon DEBU[0000] dotenv file local.env loaded)
- [x] Feature: Allow prefix for dotenv output (could be used to do "export FOO=bar" or "MYSVC_FOO=bar")
- [x] Feature: Allow {name} to be replaced with the manifest name

- [ ] (in-progress) Initial round of real world testing

- [ ] Feature: Allow {name} to be replaced with the manifest name (make sure we support this everywhere)
- [ ] Feature: Add output type "combine", that combines aliased outputs
- [ ] Feature: Comments in dotenv output? To add generated by comment and possibly include property name and description?

- [ ] More tests on multiple levels and components (e2e, unit etc)
- [ ] Feature: Cleanup command to remove files specified in outputs
- [ ] Feature: Conditional outputs, based on same matching method as layers
Expand Down
10 changes: 5 additions & 5 deletions internal/api/encrypted.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ type EncryptedProperty struct {
Value *string `json:"value" yaml:"value"`
}

func NewEncryptedConfig(m config.Manifest, p config.OrderedParameterList, backend backend.Backend) *EncryptedConfig {
func NewEncryptedConfig(ctx config.AppContext, backend backend.Backend) *EncryptedConfig {
ec := EncryptedConfig{
backend: backend,
parameters: p,
parameters: ctx.Parameters,

Name: m.Name,
Name: ctx.Manifest.Name,
Labels: make(map[string]string),
Properties: make([]EncryptedProperty, 0),
}
for k, v := range m.Labels {
ec.Labels[k] = p.Replace(v)
for k, v := range ctx.Manifest.Labels {
ec.Labels[k] = ctx.Replace(v)
}
return &ec
}
Expand Down
4 changes: 2 additions & 2 deletions internal/command/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Export(metadata config.AppMetadata) *cli.Command {
return err
}

encconf := api.NewEncryptedConfig(m, ctx.Parameters, backend)
encconf := api.NewEncryptedConfig(ctx, backend)

visit := visitor.New(ctx)

Expand Down Expand Up @@ -173,7 +173,7 @@ func Export(metadata config.AppMetadata) *cli.Command {
continue
}

path = ctx.Parameters.Replace(path)
path = ctx.Replace(path)

filtered := []string{}
filteredValues := make(map[string]string)
Expand Down
8 changes: 8 additions & 0 deletions internal/config/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"context"
"strings"

"github.com/dotnetmentor/racoon/internal/environment"
"github.com/sirupsen/logrus"
Expand All @@ -28,6 +29,13 @@ type AppMetadata struct {
Date string
}

func (c AppContext) Replace(format string) string {
fv := format
fv = strings.ReplaceAll(fv, "{name}", c.Manifest.Name)
fv = c.Parameters.replace(fv)
return fv
}

func NewContext(metadata AppMetadata, paths ...string) (AppContext, error) {
l := logrus.New()
l.Formatter = &PrefixedTextFormatter{
Expand Down
10 changes: 9 additions & 1 deletion internal/config/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"regexp"
"strings"

"github.com/dotnetmentor/racoon/internal/utils"
)

type parameters map[string]string
Expand Down Expand Up @@ -40,10 +42,16 @@ func (p parameters) ValidateParams(pl ParameterConfigList) error {
}
}

reserved := []string{"name", "key"}

// validate parameters
for _, pc := range pl {
pv, ok := p[pc.Key]

if utils.StringSliceContains(reserved, pc.Key) {
return fmt.Errorf("parameter key \"%s\" is reserved and cannot be used", pc.Key)
}

if pc.Required {
if !ok {
return fmt.Errorf("required parameter must be set, parameter: %s", pc.Key)
Expand Down Expand Up @@ -79,7 +87,7 @@ func (p parameters) Ordered(pl ParameterConfigList) (ordered OrderedParameterLis
return ordered
}

func (op OrderedParameterList) Replace(s string) string {
func (op OrderedParameterList) replace(s string) string {
for _, p := range op {
s = strings.ReplaceAll(s, fmt.Sprintf("{%s}", p.Key), p.Value)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/store/awsParameterStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (s *AwsParameterStore) Read(ctx config.AppContext, layer api.Layer, key str
return api.NewValue(api.NewValueSource(layer, api.SourceTypeAwsParameterStore), "", "", missingKeyError(), sensitive || sourceConfig.ForceSensitive)
}

psk := awpParameterStoreKey(ctx.Parameters.Replace(pskf), key)
psk := awpParameterStoreKey(ctx.Replace(pskf), key)
ctx.Log.Debugf("reading %s from %s", psk, config.SourceTypeAwsParameterStore)
out, err := s.client.GetParameter(ctx.Context, &ssm.GetParameterInput{
Name: &psk,
Expand Down Expand Up @@ -101,7 +101,7 @@ func (s *AwsParameterStore) Write(ctx config.AppContext, key, value, description
})

for k, v := range ctx.Manifest.Labels {
fv := ctx.Parameters.Replace(v)
fv := ctx.Replace(v)
tags = append(tags, ssmtypes.Tag{
Key: aws.String(k),
Value: aws.String(fv),
Expand Down
2 changes: 1 addition & 1 deletion internal/store/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Environment struct {

func (s *Environment) Read(ctx config.AppContext, layer api.Layer, key string, sensitive bool, propertySource config.ValueFromEvnironment, sourceConfig config.EnvConfig) api.Value {
for _, dff := range sourceConfig.Dotfiles {
df := ctx.Parameters.Replace(dff)
df := ctx.Replace(dff)
if utils.StringSliceContains(s.dotfilesLoaded, df) {
continue
}
Expand Down

0 comments on commit 178b5a8

Please sign in to comment.