Skip to content

Commit

Permalink
Merge pull request #29 from Comcast/cleanup
Browse files Browse the repository at this point in the history
Cleanup and Backwards Compatibility
  • Loading branch information
lewg authored Dec 22, 2023
2 parents 02057b2 + b1d028d commit b18e486
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
buildenv
pkg/
buildenv-*.tar.gz
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ PROJECT_NAME := buildenv

all: clean build-deps build

test:
go test ./...

build-deps:
go install github.com/mitchellh/gox@latest

build:
build: test
CGO_ENABLED=0 gox -ldflags "-X main.version=$(VERSION)" -osarch="darwin/amd64 darwin/arm64 linux/386 linux/amd64 linux/arm linux/arm64 windows/386 windows/amd64" -output "pkg/{{.OS}}_{{.Arch}}/$(PROJECT_NAME)"
for pkg in $$(ls pkg/); do cp CONTRIBUTING.md CONTRIBUTORS.md LICENSE NOTICE pkg/$${pkg}; done
for pkg in $$(ls pkg/); do cd pkg/$${pkg}; tar cvzf "../../$(PROJECT_NAME)-$${pkg}-$(VERSION).tar.gz" $(PROJECT_NAME)*; cd ../..; done
for pkg in $$(ls pkg/); do cd pkg/$${pkg}; tar cvzf "../../$(PROJECT_NAME)-$${pkg}-$(VERSION).tar.gz" *; cd ../..; done

build-local: test
CGO_ENABLED=0 go build -ldflags "-X main.version=$(VERSION)" -o $(PROJECT_NAME)

clean:
rm -r buildenv
rm -f *.tar.gz
rm -rf pkg
12 changes: 11 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (

var cfgFile string

var Version string

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "buildenv",
Expand All @@ -38,6 +40,11 @@ Values can be specified in plain text, or set from a vault server.`,
// has an action associated with it:
Run: func(cmd *cobra.Command, args []string) {

version, _ := cmd.Flags().GetBool("version")
if version {
fmt.Printf("Version: %s\n", Version)
}

ctx := context.Background()
debug, _ := cmd.Flags().GetBool("debug")

Expand Down Expand Up @@ -98,7 +105,8 @@ Values can be specified in plain text, or set from a vault server.`,

// 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() {
func Execute(version string) {
Version = version
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
Expand All @@ -120,9 +128,11 @@ func init() {
rootCmd.Flags().StringP("datacenter", "d", "", "Datacenter (ndc_as_a, us-east-1 etc)")
rootCmd.Flags().StringP("variables_file", "f", "variables.yml", "Variables Source YAML file")

rootCmd.Flags().BoolP("skip-vault", "v", false, "Skip Vault and use only variables file")
rootCmd.Flags().BoolP("mlock", "m", false, "Will enable system mlock if set (prevent write to swap on linux)")
rootCmd.Flags().BoolP("comments", "c", false, "Comments will be included in output")
rootCmd.Flags().Bool("debug", false, "Turn on debugging output")
rootCmd.Flags().Bool("version", false, "Print the version number")

}

Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package main

import "github.com/Comcast/Buildenv-Tool/cmd"

var version string = "development"

func main() {
cmd.Execute()
cmd.Execute(version)
}
3 changes: 3 additions & 0 deletions no_secrets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
vars:
TEST: no secrets
25 changes: 18 additions & 7 deletions reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ type KVSecrets []KVSecretBlock
func (s KVSecretBlock) GetOutput(ctx context.Context, r *Reader) (OutputList, error) {
output := OutputList{}

// Initialize the Vault Client if Necessary
if r.client == nil {
err := r.InitVault()
if err != nil {
return nil, err
}
}

// The first thing we need to do is get the mount point for the KV engine
mountPoint, secretPath := r.MountAndPath(s.Path)
if mountPoint == "" {
Expand Down Expand Up @@ -170,16 +178,17 @@ type MountInfo struct {

type Mounts map[string]MountInfo

func NewReader() (*Reader, error) {
func (r *Reader) InitVault() error {
vaultClient, err := vault.New(vault.WithEnvironment())
if err != nil {
return nil, err
return err
}
r.client = vaultClient

// Get mount info
resp, err := vaultClient.System.MountsListSecretsEngines(context.Background())
if err != nil {
return nil, fmt.Errorf("failure reading secret mounts: %w", err)
return fmt.Errorf("failure reading secret mounts: %w", err)
}

mounts := Mounts{}
Expand All @@ -197,10 +206,12 @@ func NewReader() (*Reader, error) {
mounts[mount] = thisMount
}

return &Reader{
client: vaultClient,
mounts: mounts,
}, nil
r.mounts = mounts
return nil
}

func NewReader() (*Reader, error) {
return &Reader{}, nil
}

func (r *Reader) MountAndPath(path string) (string, string) {
Expand Down

0 comments on commit b18e486

Please sign in to comment.