Add config CLI command and encrypt/decrypt sub-commands (#77)

This commit is contained in:
Jeff Malnick authored and GitHub committed 2020-06-24 16:08:13 -04:00
1 parent 8de5371499
commit 8afcbd4c8e
12 files changed
+509 -49

No files matched your search

+22 -6
View File
@@ -2,6 +2,7 @@ package config
import (
"bytes"
"context"
"crypto/rand"
"encoding/base64"
"fmt"
@@ -113,19 +114,22 @@ func New() *Config {
}
// LoadFile loads the configuration from the given file.
func LoadFile(path string) (*Config, error) {
// Read the file
func LoadFile(path string, kms *configutil.KMS) (*Config, error) {
d, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
conf, err := Parse(string(d))
if err != nil {
return nil, err
raw := string(d)
if kms != nil {
raw, err = configDecrypt(raw, kms)
if err != nil {
return nil, err
}
}
return conf, nil
return Parse(raw)
}
func Parse(d string) (*Config, error) {
@@ -170,3 +174,15 @@ func (c *Config) Sanitized() map[string]interface{} {
return result
}
func configDecrypt(raw string, kms *configutil.KMS) (string, error) {
wrapper, err := configutil.ConfigureWrapper(kms, nil, nil, nil)
if err != nil {
return raw, err
}
wrapper.Init(context.Background())
defer wrapper.Finalize(context.Background())
return configutil.EncryptDecrypt(raw, true, true, wrapper)
}
+49
View File
@@ -1,6 +1,7 @@
package config
import (
"strings"
"testing"
"time"
@@ -76,3 +77,51 @@ func TestDevController(t *testing.T) {
assert.Equal(t, exp, actual)
}
func TestConfigDecrypt(t *testing.T) {
const (
clr = `
kms "aead" {
purpose = "config"
aead_type = "aes-gcm"
key = "c964AJj8VW8w4hKz/Jd8MvuLt0kkcjVuFqMiMvTvvN8="
}
kms "aead" {
purpose = "controller"
aead_type = "aes-gcm"
key ="eb78KqCwowELYnkOOko/XYz01q1ax3g76J1vCAvt5dQ="
}`
enc = `
kms "aead" {
purpose = "config"
aead_type = "aes-gcm"
key = "c964AJj8VW8w4hKz/Jd8MvuLt0kkcjVuFqMiMvTvvN8="
}
kms "aead" {
purpose = "controller"
aead_type = "aes-gcm"
key ="{{decrypt(Ckh57d4NA6nsnRKV6DiHTyfwLIakdhN8w7qdPJgo-KWnBdlEKv3NQkUFbouU0eorSGik1Qbca5xEy2NqYT9UYj_GUGo6hHz13MEqAA)}}"
}`
)
kmses, err := configutil.ParseKMSes(enc)
assert.NoError(t, err)
var kms = &configutil.KMS{}
for _, k := range kmses {
for _, p := range k.Purpose {
if p == "config" {
kms = k
}
}
}
got, err := configDecrypt(enc, kms)
assert.NoError(t, err)
assert.Equal(t, strings.TrimSpace(got), strings.TrimSpace(clr))
}