Skip to content

Commit 20d08da

Browse files
authored
Merge pull request #49 from silasdavis/js-tests
Add JS tests
2 parents 82a8cf4 + d4982f2 commit 20d08da

File tree

23 files changed

+2439
-246
lines changed

23 files changed

+2439
-246
lines changed

.circleci/config.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ jobs:
6363

6464
workflows:
6565
version: 2
66+
6667
test_and_release:
6768
jobs:
6869
- ensure_vendor:

Gopkg.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ test: check
140140

141141
.PHONY: test_js
142142
test_js: build install
143-
$(eval HID := $(shell HOARD_JSON_CONFIG=$$(hoard config -j memory) hoard -e &> /dev/null & echo $$!))
143+
$(eval HID := $(shell hoard config memory -s test:secret_pass | hoard -c- &> /dev/null & echo $$!))
144144
npm test
145145
kill ${HID}
146146

@@ -202,7 +202,7 @@ push_ci_image: build_ci_image
202202

203203
.PHONY: npm_install
204204
npm_install:
205-
@cd hoard-js && npm install
205+
@npm install
206206

207207
.PHONY: npm_publish
208208
npm_publish:

cmd/hoarctl/grants.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (client *Client) PutSeal(cmd *cli.Cmd) {
2323
if *key != "" {
2424
spec = grant.Spec{
2525
Plaintext: nil,
26-
Symmetric: &grant.SymmetricSpec{SecretID: *key},
26+
Symmetric: &grant.SymmetricSpec{PublicID: *key},
2727
}
2828
}
2929

@@ -55,7 +55,7 @@ func (client *Client) Seal(cmd *cli.Cmd) {
5555
if *key != "" {
5656
spec = grant.Spec{
5757
Plaintext: nil,
58-
Symmetric: &grant.SymmetricSpec{SecretID: *key},
58+
Symmetric: &grant.SymmetricSpec{PublicID: *key},
5959
}
6060
}
6161

@@ -85,7 +85,7 @@ func (client *Client) Reseal(cmd *cli.Cmd) {
8585
if *key != "" {
8686
next = grant.Spec{
8787
Plaintext: nil,
88-
Symmetric: &grant.SymmetricSpec{SecretID: *key},
88+
Symmetric: &grant.SymmetricSpec{PublicID: *key},
8989
}
9090
}
9191

cmd/hoard/main.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/monax/hoard/config/secrets"
89

@@ -35,7 +36,7 @@ func main() {
3536
"Whether to emit any operational logging")
3637

3738
configFileOpt := hoardApp.StringOpt("c config", "", "Path to "+
38-
"config file. If omitted default config is used.")
39+
"config file. If omitted default config is used. Use '-' to read config from STDIN.")
3940

4041
environmentOpt := hoardApp.BoolOpt("e env", false,
4142
fmt.Sprintf("Parse the contents of the environment variable %s as a complete JSON config",
@@ -111,16 +112,32 @@ func main() {
111112
initOpt := configCmd.BoolOpt("i init", false, "Write file to "+
112113
"XDG standard location")
113114

114-
arg := configCmd.StringArg("CONFIG", "", "Supported config to generate")
115-
configCmd.Spec = "[--json] | (([--output=<output file>] | [--init]) [--force])"
116-
configCmd.Spec += "CONFIG"
115+
secretsOpt := configCmd.StringsOpt("s secret", nil, "Pairs of PublicID and Passphrase to use as symmetric secrets in config")
116+
117+
arg := configCmd.StringArg("CONFIG", "", fmt.Sprintf("Config type to generate, one of: %s",
118+
strings.Join(configTypes(), ", ")))
119+
120+
configCmd.Spec = "[--json] | (([--output=<output file>] | [--init]) [--force]) CONFIG [--secret=<PublicID:Passphrase>...]"
117121

118122
configCmd.Action = func() {
119123
store, err := storage.GetDefaultConfig(*arg)
120124
if err != nil {
121125
fatalf("Error fetching default config for %v: %v", arg, err)
122126
}
123127
conf.Storage = store
128+
if len(*secretsOpt) > 0 {
129+
conf.Secrets = &secrets.SecretsConfig{
130+
Symmetric: make([]secrets.SymmetricSecret, len(*secretsOpt)),
131+
}
132+
for i, ss := range *secretsOpt {
133+
pair := strings.Split(ss, ":")
134+
if len(pair) != 2 {
135+
fatalf("got symmetric secret specification '%s' but must be specified as <PublicID:Passphrase>", ss)
136+
}
137+
conf.Secrets.Symmetric[i].PublicID = pair[0]
138+
conf.Secrets.Symmetric[i].Passphrase = pair[1]
139+
}
140+
}
124141
}
125142

126143
configCmd.After = func() {
@@ -175,3 +192,12 @@ func writeFile(filename string, data []byte, overwrite bool) error {
175192
}
176193
return fmt.Errorf("file '%s' already exists", filename)
177194
}
195+
196+
func configTypes() []string {
197+
storageTypes := storage.GetStorageTypes()
198+
configTypes := make([]string, len(storageTypes))
199+
for i, st := range storageTypes {
200+
configTypes[i] = string(st)
201+
}
202+
return configTypes
203+
}

config/secrets/secrets.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ import (
99
// and OpenPGP identifies an entity in the given keyring
1010
type SecretsConfig struct {
1111
Symmetric []SymmetricSecret
12-
OpenPGP OpenPGPSecret
12+
OpenPGP *OpenPGPSecret
1313
}
1414

1515
type SymmetricSecret struct {
16-
ID string
16+
// An identifier for this secret that will be stored in the clear with the grant
17+
PublicID string
1718
Passphrase string
1819
}
1920

2021
type OpenPGPSecret struct {
21-
ID string
22-
File string
23-
Data []byte
22+
// A private (though not secret) identifier that points to a PGP keyring that this instance of hoard
23+
// will use to provide PGP grants
24+
PrivateID string
25+
File string
26+
Data []byte
2427
}
2528

2629
type Manager struct {
@@ -48,7 +51,7 @@ func ProviderFromConfig(conf *SecretsConfig) SymmetricProvider {
4851
}
4952
secs := make(map[string][]byte, len(conf.Symmetric))
5053
for _, s := range conf.Symmetric {
51-
secs[s.ID] = []byte(s.Passphrase)
54+
secs[s.PublicID] = []byte(s.Passphrase)
5255
}
5356
return func(id string) []byte {
5457
return secs[id]
@@ -57,13 +60,13 @@ func ProviderFromConfig(conf *SecretsConfig) SymmetricProvider {
5760

5861
// OpenPGPFromConfig reads a given PGP keyring
5962
func OpenPGPFromConfig(conf *SecretsConfig) *OpenPGPSecret {
60-
if conf == nil || conf.OpenPGP.File == "" {
63+
if conf == nil || conf.OpenPGP == nil {
6164
return nil
6265
}
6366
keyRing, err := ioutil.ReadFile(conf.OpenPGP.File)
6467
if err != nil {
6568
return nil
6669
}
6770
conf.OpenPGP.Data = keyRing
68-
return &conf.OpenPGP
71+
return conf.OpenPGP
6972
}

config/source/source.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
const DefaultHoardConfigFileName = "hoard.toml"
1616
const DefaultJSONConfigEnvironmentVariable = "HOARD_JSON_CONFIG"
17+
const STDINFileIdentifier = "-"
1718

1819
type ConfigProvider interface {
1920
// Description of where this provider sources its config from
@@ -151,16 +152,26 @@ func Default() *configSource {
151152
}
152153

153154
func fromFile(configFile string) (*config.HoardConfig, error) {
154-
bs, err := ioutil.ReadFile(configFile)
155+
bs, err := readFile(configFile)
155156
if err != nil {
156157
return nil, fmt.Errorf("could not read config file '%s': %s",
157158
configFile, err)
158159
}
160+
if len(bs) == 0 {
161+
return nil, fmt.Errorf("empty config")
162+
}
159163

160164
tomlString := string(bs)
161165
return config.HoardConfigFromTOMLString(tomlString)
162166
}
163167

168+
func readFile(configFile string) ([]byte, error) {
169+
if configFile == STDINFileIdentifier {
170+
return ioutil.ReadAll(os.Stdin)
171+
}
172+
return ioutil.ReadFile(configFile)
173+
}
174+
164175
func writeLog(writer io.Writer, msg string) {
165176
if writer != nil {
166177
writer.Write(([]byte)(msg))

config/storage/storage.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,18 @@ func NewStorageConfig(storageType StorageType, addressEncoding string) *StorageC
4949
}
5050
}
5151

52-
func GetDefaultConfig(c string) (*StorageConfig, error) {
52+
func GetStorageTypes() []StorageType {
53+
return []StorageType{
54+
Memory,
55+
Filesystem,
56+
AWS,
57+
Azure,
58+
GCP,
59+
IPFS,
60+
}
61+
}
5362

63+
func GetDefaultConfig(c string) (*StorageConfig, error) {
5464
switch StorageType(c) {
5565
case Memory, Unspecified:
5666
return DefaultMemoryConfig(), nil

grant/grant.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Seal(secret secrets.Manager, ref *reference.Ref, spec *Spec) (*Grant, error
1515
case *PlaintextSpec:
1616
grt.EncryptedReference = PlaintextGrant(ref)
1717
case *SymmetricSpec:
18-
encRef, err := SymmetricGrant(ref, secret.Provider(s.SecretID))
18+
encRef, err := SymmetricGrant(ref, secret.Provider(s.PublicID))
1919
if err != nil {
2020
return nil, err
2121
}
@@ -38,7 +38,7 @@ func Unseal(secret secrets.Manager, grt *Grant) (*reference.Ref, error) {
3838
case *PlaintextSpec:
3939
return PlaintextReference(grt.EncryptedReference), nil
4040
case *SymmetricSpec:
41-
return SymmetricReference(grt.EncryptedReference, secret.Provider(s.SecretID))
41+
return SymmetricReference(grt.EncryptedReference, secret.Provider(s.PublicID))
4242
case *OpenPGPSpec:
4343
return OpenPGPReference(grt.EncryptedReference, secret.OpenPGP)
4444
}

0 commit comments

Comments
 (0)