Skip to content

Commit

Permalink
Merge pull request #5 from scrambledeggs/fix/config
Browse files Browse the repository at this point in the history
Fix/config
  • Loading branch information
jlipa authored Sep 25, 2019
2 parents ec320ee + 2344946 commit 230e1d1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
18 changes: 15 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package config

import (
"errors"
"log"
"os"
"regexp"
"strings"

enc "github.com/scrambledeggs/booky-go-common/encryption"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

Expand All @@ -17,6 +17,8 @@ const (
ENV_STAGING string = "staging"
ENV_TEST string = "test"
ENV_PROD string = "production"

LOG_SECTION string = "COMMON/CONFIG"
)

type Config interface {
Expand Down Expand Up @@ -80,6 +82,7 @@ func (c *config) ApplyEnvConfig() error {
for k, v := range *c.configMap {
err := c.setKeyVarEnv(k, viper.GetString(prefix+v))
if err != nil {
log.WithFields(log.Fields{"-section": LOG_SECTION, "field": k}).Warn("Issue encountered while trying to set env variable.")
return err
}
}
Expand All @@ -97,9 +100,13 @@ func (c *config) setKeyVarEnv(key string, val string) error {
// Handle encrypted values
isEncrypted, _ := regexp.MatchString(`^ENC(.*)$`, val)
if isEncrypted {
trimmed := strings.TrimRight(strings.TrimLeft(val, "ENC("), ")")
trimmed := strings.TrimSuffix(strings.TrimPrefix(val, "ENC("), ")")
temp, err := enc.DecryptB64(trimmed, *c.cipherpass)
if err != nil {
log.WithFields(log.Fields{
"-section": LOG_SECTION,
"value": trimmed,
}).Info("Issue encountered while trying to decrypt a value.")
return err
}

Expand All @@ -125,7 +132,7 @@ func (c *config) checkEnvValidity() bool {
}
}

log.Print("Could not detect valid environment. Setting Config ENVIRONMENT to 'development'.")
log.WithFields(log.Fields{"-section": LOG_SECTION}).Info("Could not detect valid environment. Setting Config ENVIRONMENT to 'development'.")
defaultEnv := ENV_DEV
c.environment = &defaultEnv
return false
Expand All @@ -140,6 +147,11 @@ func setViperConfig(configFile string, configPath string, configType string) err

err := viper.ReadInConfig()
if err != nil {
log.WithFields(log.Fields{"-section": LOG_SECTION,
"config_file": configFile,
"config_path": configPath,
"config_type": configType,
}).Warn("Unable to read config file properly.")
return err
}

Expand Down
1 change: 1 addition & 0 deletions config/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.12

require (
github.com/scrambledeggs/booky-go-common/encryption v0.0.0-20190913074022-cfd0ef746a40
github.com/sirupsen/logrus v1.2.0
github.com/spf13/viper v1.4.0
github.com/stretchr/testify v1.4.0
)
2 changes: 2 additions & 0 deletions config/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -73,6 +74,7 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/scrambledeggs/booky-go-common/encryption v0.0.0-20190913074022-cfd0ef746a40 h1:/QHqHjI/AtdVvZ1/yFgRbRxeCvzoImQIPvnU/XJVOoQ=
github.com/scrambledeggs/booky-go-common/encryption v0.0.0-20190913074022-cfd0ef746a40/go.mod h1:5hz/zbUnPOoXJyvxiZh6L3MdI1817XAfVTYp8zYj6Mk=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
Expand Down Expand Up @@ -74,6 +75,7 @@ github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40T
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/scrambledeggs/booky-go-common/encryption v0.0.0-20190913074022-cfd0ef746a40 h1:/QHqHjI/AtdVvZ1/yFgRbRxeCvzoImQIPvnU/XJVOoQ=
github.com/scrambledeggs/booky-go-common/encryption v0.0.0-20190913074022-cfd0ef746a40/go.mod h1:5hz/zbUnPOoXJyvxiZh6L3MdI1817XAfVTYp8zYj6Mk=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
Expand Down

0 comments on commit 230e1d1

Please sign in to comment.