Skip to content

Commit a1a08c5

Browse files
committed
master key option, fix wait bug
1 parent 90ac1e1 commit a1a08c5

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

Readme.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func main() {
9494
}
9595
```
9696

97-
The `LoadEnv` function will load the credentials from the encrypted file `{environment.enc}`, decrypt it with the key file `{environment.key}`, and then unmarshal the result into the given config object. The example above uses a `struct`, but the object can be of type `struct` or `map[string]string`.
97+
The `LoadEnv` function will load the credentials from the encrypted file `{environment.enc}`, decrypt it with the key file `{environment.key}` or the environment variable `SICHER_MASTER_KEY`, and then unmarshal the result into the given config object. The example above uses a `struct`, but the object can be of type `struct` or `map[string]string`.
9898

9999
**_LoadEnv Parameters:_**
100100

@@ -103,6 +103,14 @@ The `LoadEnv` function will load the credentials from the encrypted file `{envir
103103
| prefix | the prefix of the environment variables | string |
104104
| config | the config object | struct or map |
105105

106+
The key also be loaded from the environment variable `SICHER_MASTER_KEY`. In production, storing the key in the environment variable is recommended.
107+
108+
To use the key from environment variable:
109+
110+
```shell
111+
env SICHER_MASTER_KEY=`{YOUR_KEY_HERE}` sicher edit
112+
```
113+
106114
All env files should be in the format like the example below:
107115

108116
For `dotenv`:
@@ -143,3 +151,7 @@ If object is a map, the keys are the environment variables and the values are th
143151
- Enable support for nested yaml env files
144152
- Add support for other types of encryption
145153
- test for Edit
154+
155+
### License
156+
157+
MIT License

dev.enc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
d23c446cbf6dda61365112d6cd815426e433a23c07aaa562d84bf7c4527adeac4a8966358df27d2fb8d2b43cc548240f6a1169a45ea477742d97f6ff==--==7007112fa552cbfe5457b407
1+
cb3272c8a4437c24b70296e697b551a9aa972e244d682af20fd5d2d1a4f32c9b1a059a48caa0d8c08e52e59c4660f88ae4309d5a0e3e5e198a81974c7e4918fd7fdbfcf32e72f3d9af4a9fc1fe7ea56b9f7d2b315b4fbfe47d9d5fc698510322a9==--==090e8f04181739a0d57068d8

sicher.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
var delimiter = "==--=="
2222
var defaultEnv = "dev"
2323
var DefaultEnvStyle = DOTENV
24+
var masterKey = "SICHER_MASTER_KEY"
2425
var (
2526
execCmd = exec.Command
2627
stdIn io.ReadWriter = os.Stdin
@@ -153,16 +154,22 @@ func (s *sicher) Edit(editor ...string) error {
153154
return fmt.Errorf("invalid Command: Select one of vim, vi, code or nano as editor, or leave as empty")
154155
}
155156

157+
var cmdArgs []string
156158
// waitOpt is needed to enable vscode to wait for the editor to close before continuing
157159
var waitOpt string
158160
if editorName == "code" {
159161
waitOpt = "--wait"
162+
cmdArgs = append(cmdArgs, waitOpt)
160163
}
161164

162-
// read the encryption key
165+
// read the encryption key. if key not in file, try getting from env
163166
key, err := os.ReadFile(fmt.Sprintf("%s%s.key", s.Path, s.Environment))
164167
if err != nil {
165-
return fmt.Errorf("encryption key(%s.key) is not available. Create one by running the cli with init flag", s.Environment)
168+
if os.Getenv(masterKey) != "" {
169+
key = []byte(os.Getenv(masterKey))
170+
} else {
171+
return fmt.Errorf("encryption key(%s.key) is not available. Create one by running the cli with init flag", s.Environment)
172+
}
166173
}
167174
strKey := string(key)
168175

@@ -208,7 +215,8 @@ func (s *sicher) Edit(editor ...string) error {
208215
}
209216

210217
//open decrypted file with editor
211-
cmd := execCmd(editorName, waitOpt, filePath)
218+
cmdArgs = append(cmdArgs, filePath)
219+
cmd := execCmd(editorName, cmdArgs...)
212220
cmd.Stdin = stdIn
213221
cmd.Stdout = stdOut
214222
cmd.Stderr = stdErr

sicher_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ func TestEditSuccess(t *testing.T) {
8888

8989
if !bytes.Contains(buf.Bytes(), []byte("TESTKEY=loremipsum")) {
9090
t.Errorf("Expected credential file to be opened and contain TESTKEY=loremipsum; got %s", buf.String())
91+
return
9192
}
9293
if !bytes.Contains(buf.Bytes(), []byte("File encrypted and saved")) {
9394
t.Errorf("Expected file to be saved and message to be displayed, got %s", buf.String())
95+
return
9496
}
9597

9698
// get path to the gitignore file and cleanup
@@ -102,6 +104,7 @@ func TestEditSuccess(t *testing.T) {
102104
os.Remove(gitPath)
103105
})
104106
}
107+
105108
func TestEditFail(t *testing.T) {
106109
oldExecCmd := execCmd
107110
defer func() { execCmd = oldExecCmd }()

0 commit comments

Comments
 (0)