Skip to content

Commit

Permalink
fix: remarks
Browse files Browse the repository at this point in the history
Signed-off-by: Bence Csati <[email protected]>
  • Loading branch information
csatib02 committed Apr 5, 2024
1 parent e4e21f6 commit a7464bd
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 36 deletions.
18 changes: 10 additions & 8 deletions env_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ var supportedProviders = []string{
// EnvStore is a helper for managing interactions between environment variables and providers,
// including tasks like extracting and converting provider-specific paths and secrets.
type EnvStore struct {
data map[string]string
data map[string]string
appConfig *common.Config
}

func NewEnvStore() *EnvStore {
func NewEnvStore(appConfig *common.Config) *EnvStore {
environ := make(map[string]string, len(os.Environ()))
for _, env := range os.Environ() {
split := strings.SplitN(env, "=", 2)
Expand All @@ -51,7 +52,8 @@ func NewEnvStore() *EnvStore {
}

return &EnvStore{
data: environ,
data: environ,
appConfig: appConfig,
}
}

Expand Down Expand Up @@ -83,7 +85,7 @@ func (s *EnvStore) GetProviderPaths() map[string][]string {
// LoadProviderSecrets creates a new provider for each detected provider using a specified config.
// It then asynchronously loads secrets using each provider and it's corresponding paths.
// The secrets from each provider are then placed into a map with the provider name as the key.
func (s *EnvStore) LoadProviderSecrets(providerPaths map[string][]string, appConfig *common.Config) (map[string][]provider.Secret, error) {
func (s *EnvStore) LoadProviderSecrets(providerPaths map[string][]string) (map[string][]provider.Secret, error) {
// At most, we will have one error per provider
errCh := make(chan error, len(supportedProviders))
providerSecrets := make(map[string][]provider.Secret)
Expand All @@ -93,7 +95,7 @@ func (s *EnvStore) LoadProviderSecrets(providerPaths map[string][]string, appCon
vaultPaths, ok := providerPaths[vault.ProviderName]
if ok {
var err error
providerSecrets[vault.ProviderName], err = s.workaroundForBao(vaultPaths, appConfig)
providerSecrets[vault.ProviderName], err = s.workaroundForBao(vaultPaths)
if err != nil {
return nil, fmt.Errorf("failed to workaround for bao: %w", err)
}
Expand All @@ -111,7 +113,7 @@ func (s *EnvStore) LoadProviderSecrets(providerPaths map[string][]string, appCon
go func(providerName string, paths []string, errCh chan<- error) {
defer wg.Done()

provider, err := newProvider(providerName, appConfig)
provider, err := newProvider(providerName, s.appConfig)
if err != nil {
errCh <- fmt.Errorf("failed to create provider %s: %w", providerName, err)
return
Expand Down Expand Up @@ -148,10 +150,10 @@ func (s *EnvStore) LoadProviderSecrets(providerPaths map[string][]string, appCon
}

// Workaround for openBao, essentially loading secretes from Vault first.
func (s *EnvStore) workaroundForBao(vaultPaths []string, appConfig *common.Config) ([]provider.Secret, error) {
func (s *EnvStore) workaroundForBao(vaultPaths []string) ([]provider.Secret, error) {
var secrets []provider.Secret

provider, err := newProvider(vault.ProviderName, appConfig)
provider, err := newProvider(vault.ProviderName, s.appConfig)
if err != nil {
return nil, fmt.Errorf("failed to create provider %s: %w", vault.ProviderName, err)
}
Expand Down
6 changes: 3 additions & 3 deletions env_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func TestEnvStore_GetProviderPaths(t *testing.T) {
os.Clearenv()
})

paths := NewEnvStore().GetProviderPaths()
paths := NewEnvStore(&common.Config{}).GetProviderPaths()

for key, expectedSlice := range ttp.wantPaths {
actualSlice, ok := paths[key]
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestEnvStore_LoadProviderSecrets(t *testing.T) {
t.Run(ttp.name, func(t *testing.T) {
createEnvsForProvider(ttp.addvault, secretFile)

providerSecrets, err := NewEnvStore().LoadProviderSecrets(ttp.providerPaths, &common.Config{})
providerSecrets, err := NewEnvStore(&common.Config{}).LoadProviderSecrets(ttp.providerPaths)
if err != nil {
assert.EqualError(t, ttp.err, err.Error(), "Unexpected error message")
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestEnvStore_ConvertProviderSecrets(t *testing.T) {
t.Run(ttp.name, func(t *testing.T) {
createEnvsForProvider(ttp.addvault, secretFile)

secretsEnv, err := NewEnvStore().ConvertProviderSecrets(ttp.providerSecrets)
secretsEnv, err := NewEnvStore(&common.Config{}).ConvertProviderSecrets(ttp.providerSecrets)
if err != nil {
assert.EqualError(t, ttp.err, err.Error(), "Unexpected error message")
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func main() {
}

// Fetch all provider secrets and assemble env variables using envstore
envStore := NewEnvStore()
envStore := NewEnvStore(config)

providerPaths := envStore.GetProviderPaths()

providerSecrets, err := envStore.LoadProviderSecrets(providerPaths, config)
providerSecrets, err := envStore.LoadProviderSecrets(providerPaths)
if err != nil {
slog.Error(fmt.Errorf("failed to extract secrets: %w", err).Error())
os.Exit(1)
Expand Down
5 changes: 2 additions & 3 deletions pkg/provider/bao/bao.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Bank-Vaults Maintainers
// Copyright © 2024 Bank-Vaults Maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,7 +27,6 @@ import (
bao "github.com/bank-vaults/vault-sdk/vault"

"github.com/bank-vaults/secret-init/pkg/common"
"github.com/bank-vaults/secret-init/pkg/internal/utils"
"github.com/bank-vaults/secret-init/pkg/provider"
)

Expand Down Expand Up @@ -69,7 +68,7 @@ func (s *sanitized) append(key string, value string) {
}

func NewProvider(config *Config, appConfig *common.Config) (*Provider, error) {
clientOptions := []bao.ClientOption{bao.ClientLogger(utils.ClientLogger{Logger: slog.Default()})}
clientOptions := []bao.ClientOption{bao.ClientLogger(clientLogger{slog.Default()})}
if config.TokenFile != "" {
clientOptions = append(clientOptions, bao.ClientToken(config.Token))
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/bao/bao_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Bank-Vaults Maintainers
// Copyright © 2024 Bank-Vaults Maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
59 changes: 59 additions & 0 deletions pkg/provider/bao/client_logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright © 2024 Bank-Vaults Maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package bao

import (
"log/slog"

bao "github.com/bank-vaults/vault-sdk/vault"
)

var _ bao.Logger = &clientLogger{}

type clientLogger struct {
logger *slog.Logger
}

func (l clientLogger) Trace(msg string, args ...map[string]interface{}) {
l.Debug(msg, args...)
}

func (l clientLogger) Debug(msg string, args ...map[string]interface{}) {
l.logger.Debug(msg, l.argsToAttrs(args...)...)
}

func (l clientLogger) Info(msg string, args ...map[string]interface{}) {
l.logger.Info(msg, l.argsToAttrs(args...)...)
}

func (l clientLogger) Warn(msg string, args ...map[string]interface{}) {
l.logger.Warn(msg, l.argsToAttrs(args...)...)
}

func (l clientLogger) Error(msg string, args ...map[string]interface{}) {
l.logger.Error(msg, l.argsToAttrs(args...)...)
}

func (clientLogger) argsToAttrs(args ...map[string]interface{}) []any {
var attrs []any

for _, arg := range args {
for key, value := range arg {
attrs = append(attrs, slog.Any(key, value))
}
}

return attrs
}
2 changes: 1 addition & 1 deletion pkg/provider/bao/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Bank-Vaults Maintainers
// Copyright © 2024 Bank-Vaults Maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/bao/config_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Bank-Vaults Maintainers
// Copyright © 2024 Bank-Vaults Maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion pkg/provider/bao/daemon_secret_renewer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2023 Bank-Vaults Maintainers
// Copyright © 2024 Bank-Vaults Maintainers
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,41 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package utils
package vault

import (
"log/slog"

"github.com/bank-vaults/vault-sdk/vault"
)

var _ vault.Logger = &ClientLogger{}
var _ vault.Logger = &clientLogger{}

type ClientLogger struct {
Logger *slog.Logger
type clientLogger struct {
logger *slog.Logger
}

func (l ClientLogger) Trace(msg string, args ...map[string]interface{}) {
func (l clientLogger) Trace(msg string, args ...map[string]interface{}) {
l.Debug(msg, args...)
}

func (l ClientLogger) Debug(msg string, args ...map[string]interface{}) {
l.Logger.Debug(msg, l.argsToAttrs(args...)...)
func (l clientLogger) Debug(msg string, args ...map[string]interface{}) {
l.logger.Debug(msg, l.argsToAttrs(args...)...)
}

func (l ClientLogger) Info(msg string, args ...map[string]interface{}) {
l.Logger.Info(msg, l.argsToAttrs(args...)...)
func (l clientLogger) Info(msg string, args ...map[string]interface{}) {
l.logger.Info(msg, l.argsToAttrs(args...)...)
}

func (l ClientLogger) Warn(msg string, args ...map[string]interface{}) {
l.Logger.Warn(msg, l.argsToAttrs(args...)...)
func (l clientLogger) Warn(msg string, args ...map[string]interface{}) {
l.logger.Warn(msg, l.argsToAttrs(args...)...)
}

func (l ClientLogger) Error(msg string, args ...map[string]interface{}) {
l.Logger.Error(msg, l.argsToAttrs(args...)...)
func (l clientLogger) Error(msg string, args ...map[string]interface{}) {
l.logger.Error(msg, l.argsToAttrs(args...)...)
}

func (ClientLogger) argsToAttrs(args ...map[string]interface{}) []any {
func (clientLogger) argsToAttrs(args ...map[string]interface{}) []any {
var attrs []any

for _, arg := range args {
Expand Down
3 changes: 1 addition & 2 deletions pkg/provider/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/bank-vaults/vault-sdk/vault"

"github.com/bank-vaults/secret-init/pkg/common"
"github.com/bank-vaults/secret-init/pkg/internal/utils"
"github.com/bank-vaults/secret-init/pkg/provider"
)

Expand Down Expand Up @@ -69,7 +68,7 @@ func (s *sanitized) append(key string, value string) {
}

func NewProvider(config *Config, appConfig *common.Config) (provider.Provider, error) {
clientOptions := []vault.ClientOption{vault.ClientLogger(utils.ClientLogger{Logger: slog.Default()})}
clientOptions := []vault.ClientOption{vault.ClientLogger(clientLogger{slog.Default()})}
if config.TokenFile != "" {
clientOptions = append(clientOptions, vault.ClientToken(config.Token))
} else {
Expand Down

0 comments on commit a7464bd

Please sign in to comment.