Skip to content

Commit

Permalink
Merge pull request #5 from Shopify/prevent-fail-on-no-env
Browse files Browse the repository at this point in the history
prevents failure due to missing environment
  • Loading branch information
catherinejones authored Sep 17, 2018
2 parents 0980893 + 43cabd1 commit 0a9fb10
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 17 deletions.
36 changes: 36 additions & 0 deletions cmd/ejson2env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@ func TestLoadSecrets(t *testing.T) {

}

func TestLoadNoEnvSecrets(t *testing.T) {

rawValues, err := ReadSecrets("test2.ejson", "./key", TestKeyValue)
if nil != err {
t.Fatal(err)
}

_, err = ExtractEnv(rawValues)
if errNoEnv != err {
t.Fatal(err)
}

if isFailure(err) {
t.Fatalf("shouldn't have caused a failure: %s", err)
}

}

func TestLoadBadEnvSecrets(t *testing.T) {

rawValues, err := ReadSecrets("test3.ejson", "./key", TestKeyValue)
if nil != err {
t.Fatal(err)
}

_, err = ExtractEnv(rawValues)
if errEnvNotMap != err {
t.Fatal(err)
}

if isFailure(err) {
t.Fatalf("shouldn't have caused a failure: %s", err)
}

}

func TestInvalidEnvironments(t *testing.T) {
testGood := map[string]interface{}{
"environment": map[string]interface{}{
Expand Down
19 changes: 19 additions & 0 deletions cmd/ejson2env/key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"bytes"
"testing"
)

func TestReadKey(t *testing.T) {
buffer := bytes.NewBufferString(TestKeyValue)
value, err := readKey(buffer)

if nil != err {
t.Errorf("shouldn't have returned an error, return: %s", err)
}

if value != TestKeyValue {
t.Errorf("value does not match expected:\nvalue: \"%s\"\nexpected: \"%s\"", value, TestKeyValue)
}
}
17 changes: 0 additions & 17 deletions cmd/ejson2env/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,6 @@ func fail(err error) {
os.Exit(1)
}

// exportSecrets wraps the read, extract, and export steps. Returns
// an error if any step fails.
func exportSecrets(filename, keyDir, privateKey string) error {
secrets, err := ReadSecrets(filename, keyDir, privateKey)
if nil != err {
return (fmt.Errorf("could not load ejson file: %s", err))
}

envValues, err := ExtractEnv(secrets)
if nil != err {
return fmt.Errorf("could not load environment from file: %s", err)
}

ExportEnv(os.Stdout, envValues)
return nil
}

func main() {
app := cli.NewApp()
app.Usage = "get environment variables from ejson files"
Expand Down
25 changes: 25 additions & 0 deletions cmd/ejson2env/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package main
import (
"bytes"
"encoding/json"
"fmt"
"os"

"github.com/Shopify/ejson"
)
Expand All @@ -26,3 +28,26 @@ func ReadSecrets(filename, keyDir, privateKey string) (map[string]interface{}, e

return secrets, nil
}

// isFailure returns true if the passed error should prompt a
// failure.
func isFailure(err error) bool {
return (nil != err && errNoEnv != err && errEnvNotMap != err)
}

// exportSecrets wraps the read, extract, and export steps. Returns
// an error if any step fails.
func exportSecrets(filename, keyDir, privateKey string) error {
secrets, err := ReadSecrets(filename, keyDir, privateKey)
if nil != err {
return fmt.Errorf("could not load ejson file: %s", err)
}

envValues, err := ExtractEnv(secrets)
if isFailure(err) {
return fmt.Errorf("could not load environment from file: %s", err)
}

ExportEnv(os.Stdout, envValues)
return nil
}
18 changes: 18 additions & 0 deletions cmd/ejson2env/secrets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"strings"
"testing"
)

func TestReadSecrets(t *testing.T) {
var err error

_, err = ReadSecrets("bad.ejson", "./key", TestKeyValue)
if nil == err {
t.Fatal("failed to fail when loading a broken ejson file")
}
if !strings.Contains(err.Error(), "no such file or directory") {
t.Errorf("error should be \"no such file or directory\": %s", err)
}
}
3 changes: 3 additions & 0 deletions cmd/ejson2env/test2.ejson
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"_public_key": "795e671066eef17025c816b6d7c4f5c658b191cfaa31baca69963d761606415c"
}
4 changes: 4 additions & 0 deletions cmd/ejson2env/test3.ejson
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_public_key": "795e671066eef17025c816b6d7c4f5c658b191cfaa31baca69963d761606415c",
"environment": "EJ[1:vLCXRxBXJjdT+w8gSPlM45F3HdykWvUdNAeXLJ51GQ4=:BxQq4GdlfxXvCHs0xall8LKesFVjEStm:K2GhhrTy4Tvhb2w0d3RyR9Y/Gg==]"
}

0 comments on commit 0a9fb10

Please sign in to comment.