Skip to content

Commit

Permalink
small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanmartins committed Nov 6, 2023
1 parent 11b8d83 commit 875370c
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 97 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.idea/
dist/
ksops
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ files:
## Release Process
Current version: `v1.0.6`
Current version: `v1.0.7`

To release a new version, install `goreleaser` and set your GitHub token:

Expand Down
93 changes: 0 additions & 93 deletions cmd/ksops.go

This file was deleted.

53 changes: 53 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package cmd

import (
"github.com/argyle-engineering/ksops/pkg"
"os"
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/fn/framework"
"sigs.k8s.io/kustomize/kyaml/kio"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ksops",
Short: "KSOPS is a flexible Kustomize KRM-based plugin for SOPS encrypted resources",
Long: `KSOPS is a flexible Kustomize KRM-based plugin for SOPS encrypted resources.
- Provides the ability to fail silently if the generator fails to decrypt files.
- Generates dummy secrets with the 'KSOPS_GENERATE_DUMMY_SECRETS' environment variable.`,
RunE: func(cmd *cobra.Command, args []string) error {

// No config is required
p := framework.SimpleProcessor{Config: nil, Filter: kio.FilterFunc(pkg.Ksops)}

// STDIN and STDOUT will be used if no reader or writer respectively is provided.
err := framework.Execute(p, nil)

return errors.Wrap(err)
},
Version: "v1.0.7",
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
rootCmd.SetVersionTemplate("{{.Version}}\n")
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ksops.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
2 changes: 1 addition & 1 deletion pkg/dummy/dummy.go → pkg/dummy.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dummy
package pkg

import (
"fmt"
Expand Down
81 changes: 81 additions & 0 deletions pkg/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package pkg

import (
"fmt"
"go.mozilla.org/sops/v3/cmd/sops/formats"
"go.mozilla.org/sops/v3/decrypt"
"os"
"sigs.k8s.io/kustomize/kyaml/yaml"
"strconv"
"strings"
)

var ksopsGenerateDummySecrets bool

func init() {
var err error
ke := os.Getenv("KSOPS_GENERATE_DUMMY_SECRETS")
if len(ke) == 0 { // env not set
ke = "false"
}

ksopsGenerateDummySecrets, err = strconv.ParseBool(ke)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error converting string to boolean, please use either false or true : %q\n", err)
os.Exit(1)
}
}

func Ksops(items []*yaml.RNode) ([]*yaml.RNode, error) {
var filteredItems []*yaml.RNode
for i := range items {
item := items[i]

// All other resources get passed along unmodified
if strings.ToLower(item.GetKind()) != "ksops" || strings.ToLower(item.GetApiVersion()) != "argyle.com/v1" {
filteredItems = append(filteredItems, item)
continue
}

// Get the spec yaml & unmarshal it
var spec Spec
err := yaml.Unmarshal([]byte(item.MustString()), &spec)
if err != nil {
return nil, fmt.Errorf("unable to parse ksops spec: %w\n", err)
}

// Generate secrets here
for _, file := range spec.Files {

var b, secret []byte

b, err = os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %w\n", file, err)
}

if ksopsGenerateDummySecrets {
secret, err = GenerateDummySecret(b)
if err != nil {
return nil, fmt.Errorf("failed generating dummy file %s: %w\n", file, err)
}
} else {
format := formats.FormatForPath(file)
secret, err = decrypt.DataWithFormat(b, format)
if err != nil && !spec.FailSilently {
return nil, fmt.Errorf("failed decrypting file %s: \n\n%w\n\n", file, err)
}
}

var node *yaml.RNode
node, err = yaml.Parse(string(secret))
if err != nil {
return nil, fmt.Errorf("failed parse secret into yaml file %s: %w\n", file, err)
}

filteredItems = append(filteredItems, node)
}

}
return filteredItems, nil
}
2 changes: 1 addition & 1 deletion pkg/schema/schema.go → pkg/schema.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package schema
package pkg

type Spec struct {
APIVersion string `yaml:"apiVersion"`
Expand Down

0 comments on commit 875370c

Please sign in to comment.