Skip to content

Commit 777ad2d

Browse files
committed
1 parent 4bc27f6 commit 777ad2d

File tree

4 files changed

+75
-21
lines changed

4 files changed

+75
-21
lines changed

README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ This command requires a ``.sops.yaml`` configuration file. Below is an example:
967967
- vault_path: "sops/"
968968
vault_kv_mount_name: "secret/" # default
969969
vault_kv_version: 2 # default
970+
vault_path_omit_filename: false # default
970971
path_regex: vault/*
971972
972973
The above configuration will place all files under ``s3/*`` into the S3 bucket ``sops-secrets``,
@@ -982,14 +983,15 @@ Publishing to Vault
982983
983984
There are a few settings for Vault that you can place in your destination rules. The first
984985
is ``vault_path``, which is required. The others are optional, and they are
985-
``vault_address``, ``vault_kv_mount_name``, ``vault_kv_version``.
986+
``vault_address``, ``vault_kv_mount_name``, ``vault_kv_version``, ``vault_path_omit_filename``.
986987
987988
``sops`` uses the official Vault API provided by Hashicorp, which makes use of `environment
988989
variables <https://www.vaultproject.io/docs/commands/#environment-variables>`_ for
989990
configuring the client.
990991
991992
``vault_kv_mount_name`` is used if your Vault KV is mounted somewhere other than ``secret/``.
992993
``vault_kv_version`` supports ``1`` and ``2``, with ``2`` being the default.
994+
``vault_path_omit_filename`` set to ``true`` to omit filename from Vault path. ``false`` by default.
993995
994996
Below is an example of publishing to Vault (using token auth with a local dev instance of Vault).
995997

config/config.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,17 @@ type azureKVKey struct {
9090
}
9191

9292
type destinationRule struct {
93-
PathRegex string `yaml:"path_regex"`
94-
S3Bucket string `yaml:"s3_bucket"`
95-
S3Prefix string `yaml:"s3_prefix"`
96-
GCSBucket string `yaml:"gcs_bucket"`
97-
GCSPrefix string `yaml:"gcs_prefix"`
98-
VaultPath string `yaml:"vault_path"`
99-
VaultAddress string `yaml:"vault_address"`
100-
VaultKVMountName string `yaml:"vault_kv_mount_name"`
101-
VaultKVVersion int `yaml:"vault_kv_version"`
102-
RecreationRule creationRule `yaml:"recreation_rule,omitempty"`
93+
PathRegex string `yaml:"path_regex"`
94+
S3Bucket string `yaml:"s3_bucket"`
95+
S3Prefix string `yaml:"s3_prefix"`
96+
GCSBucket string `yaml:"gcs_bucket"`
97+
GCSPrefix string `yaml:"gcs_prefix"`
98+
VaultPath string `yaml:"vault_path"`
99+
VaultAddress string `yaml:"vault_address"`
100+
VaultKVMountName string `yaml:"vault_kv_mount_name"`
101+
VaultKVVersion int `yaml:"vault_kv_version"`
102+
VaultPathOmitFilename bool `yaml:"vault_path_omit_filename"`
103+
RecreationRule creationRule `yaml:"recreation_rule,omitempty"`
103104
}
104105

105106
type creationRule struct {
@@ -257,7 +258,7 @@ func parseDestinationRuleForFile(conf *configFile, filePath string, kmsEncryptio
257258
dest = publish.NewGCSDestination(dRule.GCSBucket, dRule.GCSPrefix)
258259
}
259260
if dRule.VaultPath != "" {
260-
dest = publish.NewVaultDestination(dRule.VaultAddress, dRule.VaultPath, dRule.VaultKVMountName, dRule.VaultKVVersion)
261+
dest = publish.NewVaultDestination(dRule.VaultAddress, dRule.VaultPath, dRule.VaultKVMountName, dRule.VaultKVVersion, dRule.VaultPathOmitFilename)
261262
}
262263
}
263264

@@ -271,6 +272,11 @@ func parseDestinationRuleForFile(conf *configFile, filePath string, kmsEncryptio
271272
}
272273

273274
func parseCreationRuleForFile(conf *configFile, filePath string, kmsEncryptionContext map[string]*string) (*Config, error) {
275+
// If config file doesn't contain CreationRules (it's empty or only contains DestionationRules), assume it does not exist
276+
if conf.CreationRules == nil {
277+
return nil, nil
278+
}
279+
274280
var rule *creationRule
275281

276282
for _, r := range conf.CreationRules {

config/config_test.go

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,25 @@ creation_rules:
139139
encrypted_suffix: _enc
140140
`)
141141

142-
var sampleInvalidConfig = []byte(`
142+
var sampleConfigWithNoMatchingRules = []byte(`
143143
creation_rules:
144+
- path_regex: notexisting
145+
pgp: bar
146+
`)
147+
148+
var sampleEmptyConfig = []byte(``)
149+
150+
var sampleConfigWithEmptyCreationRules = []byte(`
151+
creation_rules:
152+
`)
153+
154+
var sampleConfigWithOnlyDestinationRules = []byte(`
155+
destination_rules:
156+
- path_regex: ""
157+
s3_bucket: "foobar"
158+
s3_prefix: "test/"
159+
recreation_rule:
160+
pgp: newpgp
144161
`)
145162

146163
var sampleConfigWithDestinationRule = []byte(`
@@ -178,6 +195,9 @@ destination_rules:
178195
vault_kv_mount_name: "kv/"
179196
vault_kv_version: 1
180197
path_regex: "vault-v1/*"
198+
- vault_path: "omit/"
199+
vault_path_omit_filename: true
200+
path_regex: "vault-omit-filename/*"
181201
`)
182202

183203
func parseConfigFile(confBytes []byte, t *testing.T) *configFile {
@@ -248,11 +268,29 @@ func TestLoadConfigFileWithGroups(t *testing.T) {
248268
assert.Equal(t, expected, conf)
249269
}
250270

251-
func TestLoadInvalidConfigFile(t *testing.T) {
252-
_, err := parseCreationRuleForFile(parseConfigFile(sampleInvalidConfig, t), "foobar2000", nil)
271+
func TestLoadConfigFileWithNoMatchingRules(t *testing.T) {
272+
_, err := parseCreationRuleForFile(parseConfigFile(sampleConfigWithNoMatchingRules, t), "foobar2000", nil)
253273
assert.NotNil(t, err)
254274
}
255275

276+
func TestLoadEmptyConfigFile(t *testing.T) {
277+
conf, err := parseCreationRuleForFile(parseConfigFile(sampleEmptyConfig, t), "foobar2000", nil)
278+
assert.Nil(t, conf)
279+
assert.Nil(t, err)
280+
}
281+
282+
func TestLoadConfigFileWithEmptyCreationRules(t *testing.T) {
283+
conf, err := parseCreationRuleForFile(parseConfigFile(sampleConfigWithEmptyCreationRules, t), "foobar2000", nil)
284+
assert.Nil(t, conf)
285+
assert.Nil(t, err)
286+
}
287+
288+
func TestLoadConfigFileWithOnlyDestinationRules(t *testing.T) {
289+
conf, err := parseCreationRuleForFile(parseConfigFile(sampleConfigWithOnlyDestinationRules, t), "foobar2000", nil)
290+
assert.Nil(t, conf)
291+
assert.Nil(t, err)
292+
}
293+
256294
func TestKeyGroupsForFile(t *testing.T) {
257295
conf, err := parseCreationRuleForFile(parseConfigFile(sampleConfig, t), "foobar2000", nil)
258296
assert.Nil(t, err)
@@ -328,4 +366,8 @@ func TestLoadConfigFileWithVaultDestinationRules(t *testing.T) {
328366
assert.Nil(t, err)
329367
assert.NotNil(t, conf.Destination)
330368
assert.Equal(t, "http://127.0.0.1:8200/v1/kv/barfoo/barfoo", conf.Destination.Path("barfoo"))
369+
conf, err = parseDestinationRuleForFile(parseConfigFile(sampleConfigWithVaultDestinationRules, t), "vault-omit-filename/barfoo", nil)
370+
assert.Nil(t, err)
371+
assert.NotNil(t, conf.Destination)
372+
assert.Equal(t, "http://127.0.0.1:8200/v1/secret/data/omit/", conf.Destination.Path("omit"))
331373
}

publish/vault.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import (
88
)
99

1010
type VaultDestination struct {
11-
vaultAddress string
12-
vaultPath string
13-
kvMountName string
14-
kvVersion int
11+
vaultAddress string
12+
vaultPath string
13+
kvMountName string
14+
kvVersion int
15+
vaultPathOmitFilename bool
1516
}
1617

17-
func NewVaultDestination(vaultAddress, vaultPath, kvMountName string, kvVersion int) *VaultDestination {
18+
func NewVaultDestination(vaultAddress, vaultPath, kvMountName string, kvVersion int, vaultPathOmitFilename bool) *VaultDestination {
1819
if !strings.HasSuffix(vaultPath, "/") {
1920
vaultPath = vaultPath + "/"
2021
}
@@ -27,7 +28,7 @@ func NewVaultDestination(vaultAddress, vaultPath, kvMountName string, kvVersion
2728
if kvVersion != 1 && kvVersion != 2 {
2829
kvVersion = 2
2930
}
30-
return &VaultDestination{vaultAddress, vaultPath, kvMountName, kvVersion}
31+
return &VaultDestination{vaultAddress, vaultPath, kvMountName, kvVersion, vaultPathOmitFilename}
3132
}
3233

3334
func (vaultd *VaultDestination) getAddress() string {
@@ -42,6 +43,9 @@ func (vaultd *VaultDestination) Path(fileName string) string {
4243
}
4344

4445
func (vaultd *VaultDestination) secretsPath(fileName string) string {
46+
if vaultd.vaultPathOmitFilename {
47+
fileName = ""
48+
}
4549
if vaultd.kvVersion == 1 {
4650
return fmt.Sprintf("%s%s%s", vaultd.kvMountName, vaultd.vaultPath, fileName)
4751
}

0 commit comments

Comments
 (0)