Skip to content

Commit

Permalink
- add func ReplaceInFile
Browse files Browse the repository at this point in the history
  • Loading branch information
ijustfool committed Oct 21, 2019
1 parent 20b193d commit b25ea50
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module github.com/ijustfool/docker-secrets

require github.com/mitchellh/mapstructure v1.1.2
require (
github.com/mitchellh/mapstructure v1.1.2
github.com/yosuke-furukawa/json5 v0.1.1
)

go 1.13
28 changes: 25 additions & 3 deletions secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package secrets

import (
"fmt"
"github.com/mitchellh/mapstructure"
"io/ioutil"
"os"

"github.com/mitchellh/mapstructure"
"path"
"regexp"
"strings"
)

// DockerSecrets contains secrets
Expand Down Expand Up @@ -49,6 +51,26 @@ func (ds *DockerSecrets) Unmarshal(output interface{}) error {
return decode(ds.secrets, defaultDecoderConfig(output))
}

// ReplaceInFile another way to get secrets from a config file
func ReplaceInFile(b []byte) (result []byte, err error) {
var secretContent []byte
configContent := string(b)
re := regexp.MustCompile(`%docker-secret:([a-zA-Z0-9_\-\/]+)%`)
for _, captureGroups := range re.FindAllStringSubmatch(configContent, -1) {
dockerSecretSuggestion := captureGroups[0]
secretPath := captureGroups[1]
filePath := path.Join(secretPath)
secretContent, err = ioutil.ReadFile(filePath)
if err != nil {
return
}
dockerSecretValue := strings.TrimSpace(string(secretContent))
configContent = strings.ReplaceAll(configContent, dockerSecretSuggestion, dockerSecretValue)
}
result = []byte(configContent)
return
}

// defaultDecoderConfig returns default mapsstructure.DecoderConfig
func defaultDecoderConfig(output interface{}) *mapstructure.DecoderConfig {
return &mapstructure.DecoderConfig{
Expand Down Expand Up @@ -99,7 +121,7 @@ func (ds *DockerSecrets) read(file string) error {
if err != nil {
return err
}
ds.secrets[file] = string(buf)
ds.secrets[file] = strings.TrimSpace(string(buf))
return nil
}

Expand Down
30 changes: 30 additions & 0 deletions secrets_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package secrets_test

import (
"encoding/json"
"io/ioutil"
"testing"

"github.com/ijustfool/docker-secrets"
Expand Down Expand Up @@ -58,6 +60,34 @@ func TestDockerSecrets_Unmarshal(t *testing.T) {
}
}

func TestReplaceInFile(t *testing.T) {
b, err := ioutil.ReadFile(secretDir + "/config.json")
if err != nil {
t.Errorf("ReplaceInFile(): %v", err)
return
}

b, err = secrets.ReplaceInFile(b)
if err != nil {
t.Errorf("ReplaceInFile(): %v", err)
return
}

testSecrets := testSecrets{}
err = json.Unmarshal(b, &testSecrets)
if err != nil {
t.Errorf("ReplaceInFile(): %v", err)
return
}

if testSecrets.User != userVal {
t.Errorf("testSecrets.User = `%v`, expected: `%v`", testSecrets.User, userVal)
}
if testSecrets.Password != passVal {
t.Errorf("testSecrets.Password = `%v`, expected: `%v`", testSecrets.Password, passVal)
}
}

func checkKey(t *testing.T, dockerSecrets *secrets.DockerSecrets, key, expectedValue string) {
value, err := dockerSecrets.Get(key)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions test-data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"user": "%docker-secret:test-data/user%",
"password": "%docker-secret:test-data/pass%"
}

0 comments on commit b25ea50

Please sign in to comment.