Skip to content

Commit

Permalink
Update import path (#6)
Browse files Browse the repository at this point in the history
* Move serum package to top level

* Clean comment style

* Update readme

* Add test case
  • Loading branch information
hebime committed May 14, 2020
1 parent 4431e30 commit 1f30848
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ package main
import (
"os"

"wingocard/serum"
"wingocard/serum/secretprovider"
"github.com/wingocard/serum"
"github.com/wingocard/serum/secretprovider"
)

func main() {
Expand Down
24 changes: 12 additions & 12 deletions internal/envparser/envparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ func (o *osFS) Open(path string) (io.ReadCloser, error) {
return os.Open(path)
}

//EnvVars contains the plain text key value mappings as well as the encrypted secret key value mappings
//parsed from an env file
// EnvVars contains the plain text key value mappings as well as the encrypted secret key value mappings
// parsed from an env file
type EnvVars struct {
Plain map[string]string
Secrets map[string]string
}

//ParseFile parses a .env file at path and returns the key value
//mappings for plain text variables and secret variables
// ParseFile parses a .env file at path and returns the key value
// mappings for plain text variables and secret variables
func ParseFile(path string) (*EnvVars, error) {
return parseFile(&osFS{}, path)
}
Expand Down Expand Up @@ -73,38 +73,38 @@ func parseFile(fs fsWrapper, path string) (*EnvVars, error) {

func parseLine(envVars *EnvVars, l string) error {
l = strings.TrimSpace(l)
//ignore empty lines
// ignore empty lines
if l == "" {
return nil
}

//ignore commented line
//TODO: ignore inline comments
// ignore commented line
// TODO: ignore inline comments
if strings.HasPrefix(l, commentToken) {
return nil
}

//split line into two pieces (k,v) based on key value seperator
// split line into two pieces (k,v) based on key value seperator
splits := strings.SplitN(l, kvSeparator, 2)
if len(splits) != 2 {
return fmt.Errorf("invalid format %q", l)
}

//key is first index, value is second
// key is first index, value is second
k := strings.TrimSpace(splits[0])
v := strings.TrimSpace(splits[1])
if k == "" || v == emptySecret {
return fmt.Errorf("invalid format %q", l)
}

//check if value is encrypted secret
// check if value is encrypted secret
if secretRe.MatchString(v) {
//fill in secret value - replace template value with capture group "secretval"
// fill in secret value - replace template value with capture group "secretval"
envVars.Secrets[k] = secretRe.ReplaceAllString(v, "$secretval")
return nil
}

//not a secret, fill in plain text value
// not a secret, fill in plain text value
envVars.Plain[k] = v
return nil
}
10 changes: 10 additions & 0 deletions internal/envparser/envparser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ func TestParseFile(t *testing.T) {
"SECRET_PASSWORD": "is it the red or the white?",
},
},
{
name: "= in value",
envFile: `
EQUAL=1+1=3
`,
plain: map[string]string{
"EQUAL": "1+1=3",
},
secrets: map[string]string{},
},
}

for _, tc := range tt {
Expand Down
10 changes: 5 additions & 5 deletions secretprovider/gsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
secretmanagerpb "google.golang.org/genproto/googleapis/cloud/secretmanager/v1"
)

//GSManager is a secret provider that communicates with Google Cloud Platform's Secret Manager
//to decrypt secrets. Internally it uses the Google Cloud SDK.
// GSManager is a secret provider that communicates with Google Cloud Platform's Secret Manager
// to decrypt secrets. Internally it uses the Google Cloud SDK.
type GSManager struct {
smClient *secretmanager.Client
}

//NewGSManager return's an initialized GSManager using a new secret manager client.
// NewGSManager return's an initialized GSManager using a new secret manager client.
func NewGSManager() (*GSManager, error) {
c, err := secretmanager.NewClient(context.Background())
if err != nil {
Expand All @@ -24,7 +24,7 @@ func NewGSManager() (*GSManager, error) {
return &GSManager{smClient: c}, nil
}

//Decrypt will access the secret on GCP Secret Manager and return the plain text string.
// Decrypt will access the secret on GCP Secret Manager and return the plain text string.
func (g *GSManager) Decrypt(secret string) (string, error) {
req := &secretmanagerpb.AccessSecretVersionRequest{
Name: secret,
Expand All @@ -38,7 +38,7 @@ func (g *GSManager) Decrypt(secret string) (string, error) {
return result.Payload.String(), nil
}

//Close closes the connection to the secret manager API.
// Close closes the connection to the secret manager API.
func (g *GSManager) Close() error {
return g.smClient.Close()
}
18 changes: 9 additions & 9 deletions serum/serum.go → serum.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ import (
"github.com/wingocard/serum/secretprovider"
)

//Injector injects environment variables into the current running process. Key/value pairs can
//be read in from a .env file using the load method.
// Injector injects environment variables into the current running process. Key/value pairs can
// be read in from a .env file using the load method.
type Injector struct {
SecretProvider secretprovider.SecretProvider
envVars *envparser.EnvVars
}

//Inject will inject the loaded environment variables into the current running process' environment.
//Any secret values found will attempt to be decrypted using the provided secret provider.
//The presence of secrets with a nil SecretProvider will return an error.
// Inject will inject the loaded environment variables into the current running process' environment.
// Any secret values found will attempt to be decrypted using the provided secret provider.
// The presence of secrets with a nil SecretProvider will return an error.
func (in *Injector) Inject() error {
if len(in.envVars.Secrets) > 0 && in.SecretProvider == nil {
return fmt.Errorf("serum: error injecting env vars: secrets were loaded but the SecretProvider is nil")
}

//inject secrets
// inject secrets
for k, v := range in.envVars.Secrets {
decrypted, err := in.SecretProvider.Decrypt(v)
if err != nil {
Expand All @@ -35,7 +35,7 @@ func (in *Injector) Inject() error {
}
}

//inject plain text vars
// inject plain text vars
for k, v := range in.envVars.Plain {
if err := os.Setenv(k, v); err != nil {
return fmt.Errorf("serum: error setting env var %s: %s", k, err)
Expand All @@ -44,8 +44,8 @@ func (in *Injector) Inject() error {
return nil
}

//Load will parse a .env file for key/value pairs and prepair them to be injected using the
//Inject method.
// Load will parse a .env file for key/value pairs and prepair them to be injected using the
// Inject method.
func (in *Injector) Load(path string) error {
envVars, err := envparser.ParseFile(path)
if err != nil {
Expand Down
File renamed without changes.

0 comments on commit 1f30848

Please sign in to comment.