Skip to content

Commit

Permalink
adding some optimisation on secret key initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
shibme committed Jul 16, 2024
1 parent 5291bb9 commit ffcd237
Show file tree
Hide file tree
Showing 13 changed files with 45 additions and 32 deletions.
5 changes: 5 additions & 0 deletions docs/KUBERNETES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ SLV supports two ways to reconcile SLV vaults as kuberenetes secrets:
1. [Operator](#operator)
2. [Job](#job)

SLV is compiled as a single binary, meaning the same binary can act as the CLI, K8s Operator, and K8s Job. Set the respective values to the environment variable `SLV_MODE` to make the SLV container act as an operator or job.

- For Operator Mode set `SLV_MODE=k8s_operator`
- For Job Mode set `SLV_MODE=k8s_job`

## Operator
SLV operator is a kubenetes controller that runs inside a given cluster to write secrets into given namespaces based on changes in SLV resources.

Expand Down
4 changes: 2 additions & 2 deletions internal/cli/commands/cmdvault/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func vaultAccessAddCommand() *cobra.Command {
}
vault, err := getVault(vaultFile)
if err == nil {
err = vault.Unlock(*envSecretKey)
err = vault.Unlock(envSecretKey)
if err == nil {
for _, publicKey := range publicKeys {
if _, err = vault.Share(publicKey); err != nil {
Expand Down Expand Up @@ -113,7 +113,7 @@ func vaultAccessRemoveCommand() *cobra.Command {
if err == nil {
var envSecretKey *crypto.SecretKey
if envSecretKey, err = secretkey.Get(); err == nil {
err = vault.Unlock(*envSecretKey)
err = vault.Unlock(envSecretKey)
}
if err == nil {
if err = vault.Revoke(publicKeys, pq); err == nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/commands/cmdvault/deref.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func vaultDerefCommand() *cobra.Command {
if err != nil {
utils.ExitOnError(err)
}
err = vault.Unlock(*envSecretKey)
err = vault.Unlock(envSecretKey)
if err != nil {
utils.ExitOnError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/commands/cmdvault/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func vaultGetCommand() *cobra.Command {
if err != nil {
utils.ExitOnError(err)
}
err = vault.Unlock(*envSecretKey)
err = vault.Unlock(envSecretKey)
if err != nil {
utils.ExitOnError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/commands/cmdvault/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func vaultShellCommand() *cobra.Command {
if err != nil {
utils.ExitOnError(err)
}
err = vault.Unlock(*envSecretKey)
err = vault.Unlock(envSecretKey)
if err != nil {
utils.ExitOnError(err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/core/vaults/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (vlt *Vault) ListAccessors() ([]crypto.PublicKey, error) {
return accessors, nil
}

func (vlt *Vault) Unlock(secretKey crypto.SecretKey) error {
func (vlt *Vault) Unlock(secretKey *crypto.SecretKey) error {
if !vlt.IsLocked() {
return nil
}
Expand Down
7 changes: 6 additions & 1 deletion internal/k8s/api/v1/slv_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ var _ webhook.Validator = &SLV{}

func (r *SLV) validateSLV() error {
vault := r.Spec.Vault
if err := vault.Unlock(*utils.SecretKey()); err != nil {
secretKey, err := utils.SecretKey()
if err != nil {
slvlog.Error(err, "failed to get secret key", "name", r.Name)
return err
}
if err := vault.Unlock(secretKey); err != nil {
slvlog.Error(err, "failed to unlock vault", "name", r.Name)
return err
}
Expand Down
6 changes: 5 additions & 1 deletion internal/k8s/internal/controller/slv_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ func (r *SLVReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R
}

vault := slvObj.Spec.Vault
if err := vault.Unlock(*utils.SecretKey()); err != nil {
secretKey, err := utils.SecretKey()
if err != nil {
return r.returnError(ctx, &slvObj, &logger, err, "Failed to get secret key")
}
if err := vault.Unlock(secretKey); err != nil {
return r.returnError(ctx, &slvObj, &logger, err, "Failed to unlock vault")
}
slvSecretMap, err := vault.GetAllSecrets()
Expand Down
7 changes: 4 additions & 3 deletions internal/k8s/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

func Run() {
if err := utils.InitSecretKey(); err != nil {
panic(err.Error())
secretKey, err := utils.SecretKey()
if err != nil {
panic(err)
}

config, err := utils.GetKubeClientConfig()
Expand All @@ -25,7 +26,7 @@ func Run() {
panic(err)
}

if err = slvsToSecrets(clientset, utils.SecretKey(), slvObjs); err != nil {
if err = slvsToSecrets(clientset, secretKey, slvObjs); err != nil {
panic(err)
}
}
2 changes: 1 addition & 1 deletion internal/k8s/job/reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func listSLVs(cfg *rest.Config) ([]slvv1.SLV, error) {
}

func toSecret(clientset *kubernetes.Clientset, secretKey *crypto.SecretKey, slvObj slvv1.SLV) error {
if err := slvObj.Spec.Unlock(*secretKey); err != nil {
if err := slvObj.Spec.Unlock(secretKey); err != nil {
return err
}
slvSecretMap, err := slvObj.Spec.GetAllSecrets()
Expand Down
2 changes: 1 addition & 1 deletion internal/k8s/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func Run() {
setupLog.Info("initializing SLV operator...")
setupLog.Info(config.VersionInfo())

if err := utils.InitSecretKey(); err != nil {
if _, err := utils.SecretKey(); err != nil {
setupLog.Error(err, "unable to initialize SLV Environment Secret Key")
os.Exit(1)
}
Expand Down
34 changes: 16 additions & 18 deletions internal/k8s/utils/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,26 @@ import (
"oss.amagi.com/slv/internal/core/secretkey"
)

var sKey *crypto.SecretKey
var (
sKey *crypto.SecretKey
sKeyInitialized bool
)

func InitSecretKey() (err error) {
if sKey == nil {
sKey, _ = secretkey.Get()
func SecretKey() (secretKey *crypto.SecretKey, err error) {
if sKey == nil && !sKeyInitialized {
sKeyInitialized = true
sk, _ := secretkey.Get()
if clientset, _ := getKubeClientSet(); clientset != nil {
if sKey == nil {
if sKey, err = getSecretKeyFromCluster(clientset); err != nil && isEnvGenEnabled() {
sKey, err = crypto.NewSecretKey(environments.EnvironmentKey)
if sk == nil {
if sk, err = getSecretKeyFromCluster(clientset); err != nil && isEnvGenEnabled() {
sk, err = crypto.NewSecretKey(environments.EnvironmentKey)
}
}
if err == nil && sKey != nil {
if err == nil && sk != nil {
sKey = sk
var pkEC, pkPQ *crypto.PublicKey
if pkEC, err = sKey.PublicKey(false); err == nil {
if pkPQ, err = sKey.PublicKey(true); err == nil {
if pkEC, err = sk.PublicKey(false); err == nil {
if pkPQ, err = sk.PublicKey(true); err == nil {
var publicKeyEC, publicKeyPQ string
if publicKeyEC, err = pkEC.String(); err == nil {
if publicKeyPQ, err = pkPQ.String(); err == nil {
Expand All @@ -39,14 +44,7 @@ func InitSecretKey() (err error) {
sKey = nil
}
}
return err
}

func SecretKey() *crypto.SecretKey {
if err := InitSecretKey(); err != nil {
panic(err.Error())
}
return sKey
return sKey, err
}

func GetPublicKeyFromK8s(namespace string, pq bool) (string, error) {
Expand Down
2 changes: 1 addition & 1 deletion slv.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func getVaultUnlocked(vaultFile string) (*vaults.Vault, error) {
if err != nil {
return nil, err
}
if err = vault.Unlock(*secretKey); err != nil {
if err = vault.Unlock(secretKey); err != nil {
return nil, err
}
return vault, nil
Expand Down

0 comments on commit ffcd237

Please sign in to comment.