Skip to content

Commit

Permalink
#42 GetAll and SaveAll for sensitive dogu config
Browse files Browse the repository at this point in the history
also improve error handling
  • Loading branch information
jelemux committed Feb 20, 2024
1 parent 2def303 commit c09e684
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions pkg/adapter/config/etcdSensitiveDoguConfigRepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package config

import (
"context"
"fmt"
"errors"
"github.com/cloudogu/cesapp-lib/registry"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/common"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domain/ecosystem"
"github.com/cloudogu/k8s-blueprint-operator/pkg/domainservice"
)

type EtcdSensitiveDoguConfigRepository struct {
Expand All @@ -16,9 +17,26 @@ func NewEtcdSensitiveDoguConfigRepository(etcdStore etcdStore) *EtcdSensitiveDog
return &EtcdSensitiveDoguConfigRepository{etcdStore: etcdStore}
}

func (e EtcdSensitiveDoguConfigRepository) GetAllByKey(ctx context.Context, keys []common.SensitiveDoguConfigKey) (map[common.SimpleDoguName][]*ecosystem.SensitiveDoguConfigEntry, error) {
// TODO implement me
panic("implement me")
func (e EtcdSensitiveDoguConfigRepository) GetAllByKey(_ context.Context, keys []common.SensitiveDoguConfigKey) (map[common.SimpleDoguName][]*ecosystem.SensitiveDoguConfigEntry, error) {
var errs []error
var entriesByDogu map[common.SimpleDoguName][]*ecosystem.SensitiveDoguConfigEntry
for _, key := range keys {
entryRaw, err := e.etcdStore.DoguConfig(string(key.DoguName)).Get(key.Key)
if registry.IsKeyNotFoundError(err) {
errs = append(errs, domainservice.NewNotFoundError(err, "could not find %q in etcd", key))
continue
} else if err != nil {
errs = append(errs, domainservice.NewInternalError(err, "failed to get %q from etcd", key))
continue
}

entriesByDogu[key.DoguName] = append(entriesByDogu[key.DoguName], &ecosystem.SensitiveDoguConfigEntry{
Key: common.SensitiveDoguConfigKey{DoguConfigKey: common.DoguConfigKey{DoguName: key.DoguName, Key: key.Key}},
Value: common.EncryptedDoguConfigValue(entryRaw),
})
}

return entriesByDogu, errors.Join(errs...)
}

func (e EtcdSensitiveDoguConfigRepository) Save(_ context.Context, entry *ecosystem.SensitiveDoguConfigEntry) error {
Expand All @@ -27,23 +45,33 @@ func (e EtcdSensitiveDoguConfigRepository) Save(_ context.Context, entry *ecosys
strValue := string(entry.Value)
err := setEtcdKey(strKey, strValue, e.etcdStore.DoguConfig(strDoguName))
if err != nil {
return fmt.Errorf("failed to set encrypted config key %q with value %q for dogu %q: %w", strKey, strValue, strDoguName, err)
return domainservice.NewInternalError(err, "failed to set encrypted config key %q with value %q for dogu %q", strKey, strValue, strDoguName)
}

return nil
}

func (e EtcdSensitiveDoguConfigRepository) SaveAll(ctx context.Context, keys []*ecosystem.SensitiveDoguConfigEntry) error {
// TODO implement me
panic("implement me")
func (e EtcdSensitiveDoguConfigRepository) SaveAll(ctx context.Context, entries []*ecosystem.SensitiveDoguConfigEntry) error {
var errs []error
for _, entry := range entries {
err := e.Save(ctx, entry)
errs = append(errs, err)
}

err := errors.Join(errs...)
if err != nil {
return domainservice.NewInternalError(err, "failed to set given dogu config entries in etcd")
}

return nil
}

func (e EtcdSensitiveDoguConfigRepository) Delete(_ context.Context, key common.SensitiveDoguConfigKey) error {
strDoguName := string(key.DoguName)
strKey := key.Key
err := deleteEtcdKey(strKey, e.etcdStore.DoguConfig(strDoguName))
if err != nil && !registry.IsKeyNotFoundError(err) {
return fmt.Errorf("failed to delete encrypted config key %q for dogu %q: %w", strKey, strDoguName, err)
return domainservice.NewInternalError(err, "failed to delete encrypted config key %q for dogu %q", strKey, strDoguName)
}

return nil
Expand Down

0 comments on commit c09e684

Please sign in to comment.