Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement secret encryption #375

Merged
merged 9 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
go-version-file: "go.mod"

- name: Test
run: go test ./... -coverprofile=coverage.out
run: make test-ci

- name: Send coverage
uses: shogo82148/actions-goveralls@v1
Expand Down
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@ tidy:
go mod tidy

# Run tests
test: tidy lint
test: tidy lint test-ci
test-ci:
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
@sed -i '/pkg\/mocks/d' coverage.out
@sed -i '/pkg\/export\/archive.go/d' coverage.out
@sed -i '/pkg\/export\/export.go/d' coverage.out
@sed -i '/cmd/d' coverage.out
@sed -i '/uor/d' coverage.out
@sed -i '/log/d' coverage.out
go tool cover -func coverage.out

release: semver goreleaser
@version=$$($(LOCALBIN)/semver); \
Expand Down
24 changes: 24 additions & 0 deletions Secret.argocd-secret.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
apiVersion: v1
data:
admin.password: AES#Nd1MC5tFFj9msuJ3FKLUQk400YTyn3faLbKanMh8pIgtDlwsmD0GkS9gfsicHdOmxmIgLPjSqJVGEGzK0Oq3IDVKTxAuf4fy4Fm/2/zPk3PAnGux/lfJPwJmbi+m8G2PkjFTSV1CpSn874pC
admin.passwordMtime: AES#Nd1MC5tFFj9msuJ3E4zfU0kJpIv1iTfqMY6BrtJ/o8g3DVdlmUs75O1BCwZAlifnBld5jtAQQ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Secret
metadata:
annotations:
meta.helm.sh/release-name: argocd
meta.helm.sh/release-namespace: argocd
labels:
app.kubernetes.io/component: server
app.kubernetes.io/instance: argocd
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: argocd-secret
app.kubernetes.io/part-of: argocd
app.kubernetes.io/version: v2.9.5
argocd.argoproj.io/instance: argocd
helm.sh/chart: argo-cd-5.53.10
name: argocd-secret
namespace: argocd
type: Opaque
44 changes: 44 additions & 0 deletions cmd/decrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cmd

import (
"fmt"
"os"

"github.com/bakito/kubexporter/pkg/types"
"github.com/spf13/cobra"
"golang.org/x/term"
)

// decrypt
var (
resourceFiles []string
aesKey string

decrypt = &cobra.Command{
Use: "decrypt",
Aliases: []string{"uor"},
Short: "Decrypt secrets in exported resource files",
RunE: func(cmd *cobra.Command, args []string) error {
if k, ok := os.LookupEnv(types.EnvAesKey); ok {
aesKey = k
}

if aesKey == "" {
fmt.Println("Please the aes key: ")
key, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return err
}
aesKey = string(key)
}

return types.Decrypt(printFlags, aesKey, resourceFiles...)
},
}
)

func init() {
rootCmd.AddCommand(decrypt)
decrypt.PersistentFlags().StringArrayVar(&resourceFiles, "file", nil, "resource files to decrypt")
decrypt.PersistentFlags().StringVar(&aesKey, "aes-key", "", "the decryption key")
}
8 changes: 8 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ func readConfig(cmd *cobra.Command, configFlags *genericclioptions.ConfigFlags,
return nil, err
}

if err := config.Encrypted.Setup(); err != nil {
return nil, err
}

config.Encrypted.KindFields = config.Masked.KindFields.Diff(config.Encrypted.KindFields)

return config, nil
}

Expand Down Expand Up @@ -132,4 +138,6 @@ func init() {
klog.InitFlags(fs)
_ = fs.Parse([]string{"-logtostderr=false"})
klog.SetOutput(io.Discard)

printFlags.AddFlags(decrypt)
}
8 changes: 7 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@ asLists: false
#queryPageSize: 1000
clearTarget: true
worker: 5
encrypted:
aesKey: "12345678901234567890123456789012"
kindFields:
Secret:
- [ data ]
- [ stringData ]
masked:
replacement: '***'
checksum: md5
kindFields:
Secret:
- [ data ]
- [ stringData ]
considerOwnerReferences: true
excluded:
kinds:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/vardius/worker-pool/v2 v2.1.0
github.com/vbauerster/mpb/v5 v5.4.0
go.uber.org/mock v0.4.0
golang.org/x/term v0.17.0
k8s.io/api v0.29.2
k8s.io/apimachinery v0.29.2
k8s.io/cli-runtime v0.29.2
Expand Down Expand Up @@ -410,7 +411,6 @@ require (
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.18.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions pkg/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ func (e *exporter) writeIntro() {
e.l.Printf(" considering owner references 👑\n")
}

if len(e.config.Masked.KindFields) > 0 {
e.l.Printf(" masked fields 🤿 %v\n", e.config.Masked.KindFields)
}
if len(e.config.Encrypted.KindFields) > 0 {
e.l.Printf(" encrypted fields 🔒 %v\n", e.config.Encrypted.KindFields)
}

if e.config.AsLists {
e.l.Printf(" as lists 📦\n")
} else if e.config.QueryPageSize != 0 {
Expand Down
7 changes: 5 additions & 2 deletions pkg/export/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/bakito/kubexporter/pkg/client"
"github.com/bakito/kubexporter/pkg/log"
"github.com/bakito/kubexporter/pkg/types"
"github.com/bakito/kubexporter/pkg/utils"
"github.com/vbauerster/mpb/v5"
"github.com/vbauerster/mpb/v5/decor"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -250,6 +251,7 @@ func (w *worker) exportLists(res *types.GroupResource, ul *unstructured.Unstruct
if !w.config.IsInstanceExcluded(res, u) {
w.config.FilterFields(res, u)
w.config.MaskFields(res, u)
w.config.EncryptFields(res, u)
w.config.SortSliceFields(res, u)

if _, ok := perNs[u.GetNamespace()]; !ok {
Expand Down Expand Up @@ -279,7 +281,7 @@ func (w *worker) exportLists(res *types.GroupResource, ul *unstructured.Unstruct
continue
}

err = w.config.PrintObj(usl, f)
err = utils.PrintObj(w.config.PrintFlags, usl, f)
if err != nil {
res.Error = err.Error()
continue
Expand All @@ -306,6 +308,7 @@ func (w *worker) exportSingleResources(res *types.GroupResource, ul *unstructure
w.stats.addNamespace(u.GetNamespace())
w.config.FilterFields(res, u)
w.config.MaskFields(res, u)
w.config.EncryptFields(res, u)
w.config.SortSliceFields(res, u)
us := &u

Expand All @@ -329,7 +332,7 @@ func (w *worker) exportSingleResources(res *types.GroupResource, ul *unstructure
continue
}

err = w.config.PrintObj(us, f)
err = utils.PrintObj(w.config.PrintFlags, us, f)
if err != nil {
res.Error = err.Error()
continue
Expand Down
Loading