Skip to content

Commit

Permalink
fixed k8s vault creation, new command to view k8s, root and self envs
Browse files Browse the repository at this point in the history
  • Loading branch information
shibme committed Jul 13, 2024
1 parent 86898d8 commit d6420c4
Show file tree
Hide file tree
Showing 23 changed files with 250 additions and 164 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,5 @@ Once shared, the other environments can access the vault using their respective

## Integrations
Some of the integrations that SLV currently supports are:
- [Kubernetes](/internal/k8s/README.md)
- [Kubernetes](/docs/KUBERNETES.md)
- [GitHub Actions](https://github.com/amagioss/slv-action)
34 changes: 25 additions & 9 deletions internal/k8s/README.md → docs/KUBERNETES.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# SLV - Kuberenetes Integration

SLV Kubernetes Integration helps in reconciling SLV vaults as kubernetes secrets into namespaces.
SLV can create SLV's kuberenetes compatible vaults with a `--k8s` flag. Doing this will create vaults that are technically custom resources based on SLV's [CRD](https://oss.amagi.com/slv/k8s/crd.yaml).
- SLV can create SLV's kuberenetes compatible vaults with a `--k8s` flag.
- Doing so creates vaults that are custom resources based on SLV's [CRD](https://oss.amagi.com/slv/k8s/crd.yaml).

```sh
slv vault new -v [vault-file.yaml] -k [public_key] --search [env_key_word] --k8s [k8s-secret-file-path | - | k8s-slv-object-name]
slv vault new -v [vault-file.yaml] -k [public_key] --search [env_key_word] --k8s [k8s-secret-file-path | - | k8s-slv-resource-name]
```

The `--k8s` flag takes in any of the following arguments and validates them in the following order
- An existing K8s Secret object stored as plaintext K8s Secret yaml file
- An input `-` signifies that you'd like to input the content of the K8s Secret object through stdin
- An existing K8s Secret resource stored as plaintext K8s Secret yaml file
- The value `-` signifies that you'd like to input the contents of the K8s Secret resource through stdin
- Name of the SLV's K8s resource which directly translates to the name of the K8s Secret. This creates an empty K8s compatible SLV vault file.

For example, to create an k8s SLV vault that would replicate a docker config Secret in k8s, you can do something like this:
Expand All @@ -27,18 +29,22 @@ To get started apply the SLV [CRD](https://oss.amagi.com/slv/k8s/crd.yaml) using
kubectl apply -f https://oss.amagi.com/slv/k8s/crd.yaml
```

SLV support two ways to reconcile SLV vaults as kuberenetes secrets:
SLV supports two ways to reconcile SLV vaults as kuberenetes secrets:
1. [Operator](#operator)
2. [Job](#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 objects.
SLV operator is a kubenetes controller that runs inside a given cluster to write secrets into given namespaces based on changes in SLV resources.

The following example shows how it is achieved using the operator.

- Create a namespace and add SLV environment secret key as a secret (recommended to use Access Binding using KMS for cloud environments)
- Create a namespace for the operator
```sh
kubectl create ns slv
```
- Add SLV environment secret key as a secret (recommended to use Access Binding with KMS for cloud environments)
```sh
# Note: You can skip this step if you wish SLV to automatically create a secret key for you (suitable for test environments).
# Disclaimer: The below secret key is only for demonstration purposes. Please avoid using it in production.
kubectl create secret generic slv -n slv --from-literal=SecretKey=SLV_ESK_AEAEKAAATI5CXB7QMFSUGY4RUT6UTUSK7SGMIECTJKRTQBFY6BN5ZV5M5XGF6DWLV2RVCJJSMXH43DJ6A5TK7Y6L6PYEMCDGQRBX46GUQPUIYUQ
```
Expand Down Expand Up @@ -67,11 +73,21 @@ kubectl apply -f pets.slv.yaml
```sh
kubectl get secret pets -o jsonpath='{.data.hi}' | base64 --decode
```
### Creating vaults shared with the deployed operator
- To show the public key of the operator
```sh
slv env show k8s
```
- Create a vault with the public key of the operator
```sh
slv vault new -v test.slv.yaml --env-k8s
```


## Job
SLV job is a one-time job that can reconcile any existing SLV objects as kubernetes secrets. This is useful in environments that can't afford to run a persistent operator or there aren't many secrets to deal with.
SLV job is a one-time job that can reconcile any existing SLV resources as kubernetes secrets. This is useful in environments that can't afford to run a persistent operator or there aren't many secrets to deal with.

The following example shows how SLV objects are reconciled to secrets using the job.
The following example shows how SLV resources are reconciled to secrets using the job.

- Create a namespace and add SLV environment secret key as a secret (recommended to use Access Binding using KMS for cloud environments)
```sh
Expand Down
18 changes: 8 additions & 10 deletions internal/cli/commands/cmdenv/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ var (
envAddCmd *cobra.Command
envListSearchCmd *cobra.Command
envDeleteCmd *cobra.Command
envSelfCmd *cobra.Command
envSelfSetCmd *cobra.Command
envK8sCmd *cobra.Command
envSetSCmd *cobra.Command
envSetSelfSCmd *cobra.Command
envShowCmd *cobra.Command
envShowRootCmd *cobra.Command
envShowSelfCmd *cobra.Command
envShowK8sCmd *cobra.Command
)

var (
Expand Down Expand Up @@ -51,21 +54,16 @@ var (
}

EnvSearchFlag = utils.FlagDef{
Name: "search",
Name: "env-search",
Shorthand: "s",
Usage: "Searches query to filter environments",
}

EnvSelfFlag = utils.FlagDef{
Name: "self",
Name: "env-self",
Usage: "References to the self environment (the current local environment where the command is executed)",
}

EnvK8sClusterFlag = utils.FlagDef{
Name: "k8s-cluster",
Usage: "References the accessible k8s cluster",
}

envDefFlag = utils.FlagDef{
Name: "env-def",
Shorthand: "e",
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/commands/cmdenv/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func envDeleteCommand() *cobra.Command {
}
if envs != nil {
for _, env := range envs {
utils.ShowEnv(*env, false, false)
ShowEnv(*env, false, false)
fmt.Println()
}
confirm, err := input.GetConfirmation("Are you sure you want to delete the above environment(s) [yes/no]: ", "yes")
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/commands/cmdenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func EnvCommand() *cobra.Command {
envCmd.AddCommand(envAddCommand())
envCmd.AddCommand(envListSearchCommand())
envCmd.AddCommand(envDeleteCommand())
envCmd.AddCommand(envSelfCommand())
envCmd.AddCommand(envK8sCommand())
envCmd.AddCommand(envSetCommand())
envCmd.AddCommand(envShowCommand())
envCmd.AddCommand(envShowK8sCommand())
return envCmd
}
50 changes: 0 additions & 50 deletions internal/cli/commands/cmdenv/k8s.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/cli/commands/cmdenv/kms.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func newKMSEnvCommand(kmsName, kmsProviderDesc string, keyIdFlag utils.FlagDef)
}
env.SetEmail(envEmail)
env.AddTags(envTags...)
utils.ShowEnv(*env, true, false)
ShowEnv(*env, true, false)
addToProfileFlag, _ := cmd.Flags().GetBool(envAddFlag.Name)
if addToProfileFlag {
profile, err := profiles.GetDefaultProfile()
Expand Down
6 changes: 3 additions & 3 deletions internal/cli/commands/cmdenv/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func envNewServiceCommand() *cobra.Command {
}
env.SetEmail(email)
env.AddTags(tags...)
utils.ShowEnv(*env, true, false)
ShowEnv(*env, true, false)
if secretKey != nil {
fmt.Println("\nSecret Key:\t", color.HiBlackString(secretKey.String()))
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func envNewUserCommand() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
selfEnv := environments.GetSelf()
if selfEnv != nil {
utils.ShowEnv(*selfEnv, true, true)
ShowEnv(*selfEnv, true, true)
confirmed, err := input.GetConfirmation("You are already registered as an environment, "+
"this will replace the existing environment. Proceed? (yes/no): ", "yes")
if err != nil {
Expand Down Expand Up @@ -128,7 +128,7 @@ func envNewUserCommand() *cobra.Command {
utils.ExitOnError(err)
}
secretBinding := env.SecretBinding
utils.ShowEnv(*env, true, true)
ShowEnv(*env, true, true)
addToProfileFlag, _ := cmd.Flags().GetBool(envAddFlag.Name)
if addToProfileFlag {
profile, err := profiles.GetDefaultProfile()
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/commands/cmdenv/searchlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func envListSearchCommand() *cobra.Command {
utils.ExitOnError(err)
}
for _, env := range envs {
utils.ShowEnv(*env, false, false)
ShowEnv(*env, false, false)
fmt.Println()
}
utils.SafeExit()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,27 @@ import (
"oss.amagi.com/slv/internal/core/input"
)

func envSelfCommand() *cobra.Command {
if envSelfCmd != nil {
return envSelfCmd
func envSetCommand() *cobra.Command {
if envSetSCmd != nil {
return envSetSCmd
}
envSelfCmd = &cobra.Command{
Use: "self",
Aliases: []string{"user", "me", "my", "current"},
Short: "Shows the current user environment if registered",
envSetSCmd = &cobra.Command{
Use: "set",
Aliases: []string{"put", "update"},
Short: "Set/update an environments",
Run: func(cmd *cobra.Command, args []string) {
env := environments.GetSelf()
if env == nil {
fmt.Println("No environment registered as self.")
} else {
utils.ShowEnv(*env, true, true)
}
utils.SafeExit()
cmd.Help()
},
}
envSelfCmd.AddCommand(envSelfSetCommand())
return envSelfCmd
envSetSCmd.AddCommand(envSetSelfCommand())
return envSetSCmd
}

func envSelfSetCommand() *cobra.Command {
if envSelfSetCmd != nil {
return envSelfSetCmd
func envSetSelfCommand() *cobra.Command {
if envSetSelfSCmd != nil {
return envSetSelfSCmd
}
envSelfSetCmd = &cobra.Command{
envSetSelfSCmd = &cobra.Command{
Use: "set",
Aliases: []string{"save", "put", "store", "s"},
Short: "Sets a given environment as self",
Expand All @@ -59,11 +53,11 @@ func envSelfSetCommand() *cobra.Command {
if err = env.MarkAsSelf(); err != nil {
utils.ExitOnError(err)
}
utils.ShowEnv(*env, true, true)
ShowEnv(*env, true, true)
fmt.Println(color.GreenString("Successfully registered self environment"))
},
}
envSelfSetCmd.Flags().StringP(envDefFlag.Name, envDefFlag.Shorthand, "", envDefFlag.Usage)
envSelfSetCmd.MarkFlagRequired(envDefFlag.Name)
return envSelfSetCmd
envSetSelfSCmd.Flags().StringP(envDefFlag.Name, envDefFlag.Shorthand, "", envDefFlag.Usage)
envSetSelfSCmd.MarkFlagRequired(envDefFlag.Name)
return envSetSelfSCmd
}
Loading

0 comments on commit d6420c4

Please sign in to comment.