Skip to content

Commit

Permalink
don't disable logs for encrypted jobs known to user
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Jun 19, 2022
1 parent 102ddad commit 8907d1e
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 28 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/goreleaser-action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ jobs:
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ENCRYPTION_KEYS: ${{ secrets.ENCRYPTION_KEYS }}
CA_PATH_VALUE: ${{ secrets.PROMETHEUS_ROOT_CA }}
PROMETHEUS_BASIC_AUTH: ${{ secrets.PROMETHEUS_BASIC_AUTH }}
PROTECTED_KEYS: ${{ secrets.PROTECTED_KEYS }}
DEFAULT_CONFIG_VALUE: ${{ secrets.DEFAULT_CONFIG }}
DEFAULT_CONFIG_PATH: ${{ secrets.DEFAULT_CONFIG_PATH }}
4 changes: 1 addition & 3 deletions .github/workflows/ko-build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ jobs:
- run: KO_DOCKER_REPO=${DOCKER_REPO,,} ko publish --bare --tags ${{ github.ref_name }},latest --platform all .
env:
VERSION: ${{ github.ref_name }}-beta
ENCRYPTION_KEYS: ${{ secrets.ENCRYPTION_KEYS }}
CA_PATH_VALUE: ${{ secrets.PROMETHEUS_ROOT_CA }}
PROMETHEUS_BASIC_AUTH: ${{ secrets.PROMETHEUS_BASIC_AUTH }}
PROTECTED_KEYS: ${{ secrets.PROTECTED_KEYS }}
DEFAULT_CONFIG_VALUE: ${{ secrets.DEFAULT_CONFIG }}
DEFAULT_CONFIG_PATH: ${{ secrets.DEFAULT_CONFIG_PATH }}
4 changes: 1 addition & 3 deletions .github/workflows/ko-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ jobs:
- run: KO_DOCKER_REPO=${DOCKER_REPO,,} ko publish --bare --tags ${{ github.ref_name }},latest --platform all .
env:
VERSION: ${{ github.ref_name }}
ENCRYPTION_KEYS: ${{ secrets.ENCRYPTION_KEYS }}
CA_PATH_VALUE: ${{ secrets.PROMETHEUS_ROOT_CA }}
PROMETHEUS_BASIC_AUTH: ${{ secrets.PROMETHEUS_BASIC_AUTH }}
PROTECTED_KEYS: ${{ secrets.PROTECTED_KEYS }}
DEFAULT_CONFIG_VALUE: ${{ secrets.DEFAULT_CONFIG }}
DEFAULT_CONFIG_PATH: ${{ secrets.DEFAULT_CONFIG_PATH }}
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ builds:
- -s -w
- -extldflags "-static"
- -X github.com/Arriven/db1000n/src/utils/ota.Version={{ .Version }}
- -X github.com/Arriven/db1000n/src/utils.EncryptionKeys={{ .Env.ENCRYPTION_KEYS }}
- -X github.com/Arriven/db1000n/src/utils.ProtectedKeys={{ .Env.PROTECTED_KEYS }}
- -X github.com/Arriven/db1000n/src/job/config.DefaultConfig={{ .Env.DEFAULT_CONFIG_VALUE }}
- -X github.com/Arriven/db1000n/src/job.DefaultConfigPathCSV={{ .Env.DEFAULT_CONFIG_PATH }}
archives:
Expand Down
2 changes: 1 addition & 1 deletion .ko.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ builds:
- -s -w
- -extldflags "-static"
- -X github.com/Arriven/db1000n/src/utils/ota.Version={{ .Env.VERSION }}
- -X github.com/Arriven/db1000n/src/utils.EncryptionKeys={{ .Env.ENCRYPTION_KEYS }}
- -X github.com/Arriven/db1000n/src/utils.ProtectedKeys={{ .Env.PROTECTED_KEYS }}
- -X github.com/Arriven/db1000n/src/job/config.DefaultConfig={{ .Env.DEFAULT_CONFIG_VALUE }}
- -X github.com/Arriven/db1000n/src/job.DefaultConfigPathCSV={{ .Env.DEFAULT_CONFIG_PATH }}
6 changes: 3 additions & 3 deletions src/job/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type MultiConfig struct {

type RawMultiConfig struct {
Body []byte
Encrypted bool
Protected bool
lastModified string
etag string
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func fetchAndDecrypt(logger *zap.Logger, path string, lastKnownConfig *RawMultiC
return nil, fmt.Errorf("encryption disabled")
}

decryptedConfig, err := utils.Decrypt(config.Body)
decryptedConfig, protected, err := utils.Decrypt(config.Body)
if err != nil {
logger.Warn("can't decrypt config", zap.Error(err))

Expand All @@ -103,7 +103,7 @@ func fetchAndDecrypt(logger *zap.Logger, path string, lastKnownConfig *RawMultiC
logger.Info("decrypted config")

config.Body = decryptedConfig
config.Encrypted = true
config.Protected = protected
}

return config, nil
Expand Down
4 changes: 2 additions & 2 deletions src/job/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func (r *Runner) Run(ctx context.Context, logger *zap.Logger) {

metric = &metrics.Metrics{} // clear info about previous targets and avoid old jobs from dumping old info to new metrics

if rawConfig.Encrypted {
logger.Info("config is encrypted, disabling logs")
if rawConfig.Protected {
logger.Info("config is protected, disabling logs")

cancel = r.runJobs(ctx, cfg, nil, zap.NewNop())
} else {
Expand Down
8 changes: 6 additions & 2 deletions src/job/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func encryptedJob(ctx context.Context, args config.Args, globalConfig *GlobalCon
return nil, err
}

decrypted, err := utils.Decrypt(decoded)
decrypted, protected, err := utils.Decrypt(decoded)
if err != nil {
return nil, err
}
Expand All @@ -255,5 +255,9 @@ func encryptedJob(ctx context.Context, args config.Args, globalConfig *GlobalCon
return nil, fmt.Errorf("unknown job %q", jobCfg.Type)
}

return job(ctx, jobCfg.Args, globalConfig, nil, zap.NewNop())
if protected {
return job(ctx, jobCfg.Args, globalConfig, nil, zap.NewNop())
}

return job(ctx, jobCfg.Args, globalConfig, a, logger)
}
29 changes: 21 additions & 8 deletions src/utils/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
// EncryptionKeys random 32 byte key encoded into base64 string. Used by default for configs
var EncryptionKeys = `/45pB920B6DFNwCB/n4rYUio3AVMawrdtrFnjTSIzL4=`

var ProtectedKeys = ``

// decryption takes a bunch of RAM to generate scrypt identity
// we don't do decryption in hot paths so it's better to only allow one thread doing decryption at a time to avoi OOM
var decryptMutex sync.Mutex
Expand All @@ -25,8 +27,13 @@ const (
keySeparator = `&`
)

type encryptionKey struct {
key string
protected bool //indicates that the content encrypted by this key shouldn't be logged anywhere
}

// GetEncryptionKeys returns list of encryption keys from ENCRYPTION_KEYS env variable name or default value
func GetEncryptionKeys() ([]string, error) {
func GetEncryptionKeys() ([]encryptionKey, error) {
keysString := GetEnvStringDefault(encryptionKeyEnvName, EncryptionKeys)
if keysString != EncryptionKeys {
// if user specified own keys, add default at end to be sure that it always used too
Expand All @@ -37,11 +44,17 @@ func GetEncryptionKeys() ([]string, error) {
// +1 to allocate for case if no separator and list contains key itself
// otherwise we just allocate +1 struct for string slice that stores just 2 int fields
// that is not a lot
output := make([]string, 0, strings.Count(keysString, keySeparator)+1)
output := make([]encryptionKey, 0, strings.Count(keysString, keySeparator)+strings.Count(ProtectedKeys, keySeparator)+1)

for _, key := range strings.Split(keysString, keySeparator) {
if key != "" {
output = append(output, key)
output = append(output, encryptionKey{key: key})
}
}

for _, key := range strings.Split(ProtectedKeys, keySeparator) {
if key != "" {
output = append(output, encryptionKey{key: key, protected: true})
}
}

Expand All @@ -54,28 +67,28 @@ func IsEncrypted(cfg []byte) bool {
}

// Decrypt decrypts config using EncryptionKeys
func Decrypt(cfg []byte) (result []byte, err error) {
func Decrypt(cfg []byte) (result []byte, protected bool, err error) {
keys, err := GetEncryptionKeys()
if err != nil {
return nil, err
return nil, false, err
}

decryptMutex.Lock()
defer decryptMutex.Unlock()

// iterate over all keys and return on first success decryption
for _, key := range keys {
result, err = decrypt(cfg, key)
result, err = decrypt(cfg, key.key)
runtime.GC() // force GC to decrease memory usage

if err != nil {
continue
}

return result, nil
return result, key.protected, nil
}

return nil, err
return nil, false, err
}

func decrypt(cfg []byte, key string) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/crypto_noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ func IsEncrypted(cfg []byte) bool {
}

// Decrypt decrypts config using EncryptionKeys
func Decrypt(cfg []byte) ([]byte, error) {
return nil, fmt.Errorf("encryption not supported")
func Decrypt(cfg []byte) ([]byte, bool, error) {
return nil, false, fmt.Errorf("encryption not supported")
}

0 comments on commit 8907d1e

Please sign in to comment.