Skip to content

Commit 2f376c7

Browse files
authored
Merge pull request #76 from alxndr13/feat-add-public-key-to-secrets
feat: save public key or its ID to secretFileData
2 parents 69d912a + 1dd130c commit 2f376c7

6 files changed

Lines changed: 54 additions & 12 deletions

File tree

secret.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package skipper
22

33
import (
44
"fmt"
5+
"os"
56
"path/filepath"
67
"regexp"
78
"strings"
@@ -36,10 +37,11 @@ func NewSecret(secretFile *SecretFile, driver string, alternative *Call, path []
3637
type SecretFileData struct {
3738
Data string `yaml:"data"`
3839
Type string `yaml:"type"`
40+
Key string `yaml:"key"`
3941
}
4042

4143
// NewSecretData constructs a [Data] map as it is required for secrets.
42-
func NewSecretData(data string, driver string) (*SecretFileData, error) {
44+
func NewSecretData(data string, driver string, key string) (*SecretFileData, error) {
4345
if data == "" {
4446
return nil, fmt.Errorf("secret data cannot be empty")
4547
}
@@ -50,6 +52,7 @@ func NewSecretData(data string, driver string) (*SecretFileData, error) {
5052
return &SecretFileData{
5153
Data: data,
5254
Type: driver,
55+
Key: key,
5356
}, nil
5457
}
5558

@@ -141,7 +144,7 @@ func (secret *Secret) attemptCreate(fs afero.Fs, secretPath string) error {
141144
}
142145

143146
// create new Data map which can then be written into the secret file
144-
secretFileData, err := NewSecretData(encryptedData, secret.Driver.Type())
147+
secretFileData, err := NewSecretData(encryptedData, secret.Driver.Type(), secret.Driver.GetKey())
145148
if err != nil {
146149
return fmt.Errorf("could not create NewSecretData: %w", err)
147150
}
@@ -229,7 +232,10 @@ func secretYamlFileLoader(secretFileList *[]*SecretFile) YamlFileLoaderFunc {
229232

230233
// Value returns the actual secret value.
231234
func (s *Secret) Value() (string, error) {
232-
return s.Driver.Decrypt(s.Data.Data)
235+
if s.Driver.GetKey() != s.Data.Key {
236+
fmt.Fprintf(os.Stderr, "key in secret file '%s' differs from the key in the inventory\n", s.Path())
237+
}
238+
return s.Driver.Decrypt(s.Data.Data, s.Data.Key)
233239
}
234240

235241
// FullName returns the full secret name as it would be expected to ocurr in a class/target.

secret/driver.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99

1010
type Driver interface {
1111
Type() string
12+
GetKey() string
1213
Encrypt(data string) (string, error)
13-
Decrypt(encrypted string) (string, error)
14+
// if key is set, use that one for decryption, otherwise use the key set in the driver (if available)
15+
Decrypt(encrypted string, key string) (string, error)
1416
}
1517

1618
type ConfigurableDriver interface {

secret/driver/aes.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ func (driver *Aes) Configure(config map[string]interface{}) error {
3636
return nil
3737
}
3838

39-
func (driver *Aes) Decrypt(encrypted string) (string, error) {
39+
func (driver *Aes) Decrypt(encrypted string, key string) (string, error) {
40+
// key is dismissed, as we always use the key in the driver config here
41+
4042
decrypted, err := driver.decrypt([]byte(driver.config.Key), encrypted)
4143
if err != nil {
4244
return "", err
@@ -107,3 +109,7 @@ func (driver *Aes) decrypt(key []byte, secure string) (decoded string, err error
107109
func (driver *Aes) Type() string {
108110
return "aes"
109111
}
112+
113+
func (driver *Aes) GetKey() string {
114+
return "aesIsSymmetricThereIsNoKey"
115+
}

secret/driver/azure.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type azureConfig struct {
2828
// IgnoreVersion will ignore any key version, even if given, and always use the latest version.
2929
IgnoreVersion bool `mapstructure:"ignore_version"`
3030
// The Azure Vault KeyId to use for encryption and decryption
31+
// KeyId is the full key string, example: https://secretkeyvault.vault.azure.net/keys/secrets-key/1111231232312111
3132
KeyId string `mapstructure:"key_id"`
3233

3334
VaultName string
@@ -90,7 +91,20 @@ func (driver *Azure) Encrypt(input string) (string, error) {
9091
return base64.RawStdEncoding.EncodeToString(res.Result), nil
9192
}
9293

93-
func (driver *Azure) Decrypt(input string) (string, error) {
94+
// Decrypt decrypts an input either using the key configured in the driver or if the key parameter isn't empty it will use that one.
95+
func (driver *Azure) Decrypt(input string, key string) (string, error) {
96+
var err error
97+
keyName := driver.config.KeyName
98+
keyVersion := driver.config.KeyVersion
99+
100+
// if we hand over a key to this func, make sure to use this key for the decryption, otherwise use the key configured in the driver
101+
if len(key) > 0 {
102+
_, keyName, keyVersion, err = parseAzureKeyVaultKeyId(key)
103+
if err != nil {
104+
return "", fmt.Errorf("the key we handed over to Decrypt() could not be parsed")
105+
}
106+
}
107+
94108
decoded, err := base64.RawStdEncoding.DecodeString(input)
95109
if err != nil {
96110
return "", err
@@ -101,11 +115,10 @@ func (driver *Azure) Decrypt(input string) (string, error) {
101115
Value: []byte(decoded),
102116
}
103117

104-
version := driver.config.KeyVersion
105118
if driver.config.IgnoreVersion {
106-
version = ""
119+
keyVersion = ""
107120
}
108-
res, err := driver.client.Decrypt(context.TODO(), driver.config.KeyName, version, encryptParams, nil)
121+
res, err := driver.client.Decrypt(context.TODO(), keyName, keyVersion, encryptParams, nil)
109122
if err != nil {
110123
return "", err
111124
}
@@ -157,3 +170,7 @@ func parseAzureKeyVaultKeyId(key string) (vaultName string, keyName string, keyV
157170
func (driver *Azure) Type() string {
158171
return "azurekv"
159172
}
173+
174+
func (driver Azure) GetKey() string {
175+
return driver.config.KeyId
176+
}

secret/driver/base64.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ func NewBase64() (*Base64, error) {
1717
return &driver, nil
1818
}
1919

20-
func (p *Base64) Decrypt(encrypted string) (string, error) {
20+
func (p *Base64) Decrypt(encrypted string, key string) (string, error) {
21+
// key is dismissed, as base64 isn't decrypting stuff
22+
2123
out, err := base64.StdEncoding.DecodeString(encrypted)
2224
if err != nil {
2325
return "", err
@@ -32,3 +34,7 @@ func (p *Base64) Encrypt(input string) (string, error) {
3234
func (p *Base64) Type() string {
3335
return "base64"
3436
}
37+
38+
func (p *Base64) GetKey() string {
39+
return "base64DoesNotHaveAKey"
40+
}

secret/driver/plain.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ func NewPlain() (*Plain, error) {
1414
}
1515

1616
// the plain driver does not do anything
17-
func (p *Plain) Decrypt(encrypted string) (string, error) {
17+
func (p *Plain) Decrypt(encrypted string, key string) (string, error) {
18+
// key is dismissed, as plain does not do anything
1819
return encrypted, nil
1920
}
2021

@@ -23,6 +24,10 @@ func (p *Plain) Encrypt(input string) (string, error) {
2324
return input, nil
2425
}
2526

26-
func (p *Plain) Type() string {
27+
func (p Plain) Type() string {
2728
return "plain"
2829
}
30+
31+
func (p Plain) GetKey() string {
32+
return "plainDoesntHaveAKey"
33+
}

0 commit comments

Comments
 (0)