From 2f438d2b0b759cfd8c9c01fca139ee12c7e9b54f Mon Sep 17 00:00:00 2001 From: Joe Garcia Date: Thu, 30 May 2024 14:35:12 -0400 Subject: [PATCH] chore: Update conceal package to version 4.0.0 --- cmd/install.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/show.go | 28 ++++++++++++++++++ cmd/summon.go | 19 +++++++++++++ cmd/update.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 196 insertions(+) create mode 100644 cmd/install.go create mode 100644 cmd/show.go create mode 100644 cmd/summon.go create mode 100644 cmd/update.go diff --git a/cmd/install.go b/cmd/install.go new file mode 100644 index 0000000..3aedf23 --- /dev/null +++ b/cmd/install.go @@ -0,0 +1,72 @@ +package cmd + +import ( + "fmt" + "os" + "os/exec" + "path/filepath" + + "github.com/infamousjoeg/conceal/pkg/conceal" + "github.com/spf13/cobra" +) + +var installCmd = &cobra.Command{ + Use: "install", + Short: "Install Summon provider wrapper", + Long: `This command creates a wrapper script for using Conceal as a Summon provider. + + Example Usage: + $ conceal summon install`, + Run: func(cmd *cobra.Command, args []string) { + installWrapper() + }, +} + +func installWrapper() { + // Define the script content + scriptContent := `#!/bin/bash + + # Check if the correct number of arguments are provided + if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + exit 1 + fi + + # Call the conceal binary with the get argument and the provided secret ID + conceal summon show "$1" + ` + + // Find the full path of the summon executable + summonPath, err := exec.LookPath("summon") + if err != nil { + fmt.Fprintf(os.Stderr, "Error finding Summon: %v\n", err) + conceal.PrintInfo("Make sure Summon is installed and available in your PATH.") + os.Exit(1) + } + + // Get the directory where the summon executable is located + summonDir := filepath.Dir(summonPath) + providersPath := filepath.Join(summonDir, "Providers") + scriptFilePath := filepath.Join(providersPath, "conceal_summon") + + // Create the Providers directory + err = os.MkdirAll(providersPath, 0755) + if err != nil { + fmt.Fprintf(os.Stderr, "Error creating Providers directory: %v\n", err) + os.Exit(1) + } + + // Write the script content to the file + err = os.WriteFile(scriptFilePath, []byte(scriptContent), 0755) + if err != nil { + fmt.Fprintf(os.Stderr, "Error creating wrapper script: %v\n", err) + os.Exit(1) + } + + conceal.PrintSuccess("Wrapper script 'conceal_summon' created successfully.") + conceal.PrintInfo("To use: summon --provider conceal_summon ...") +} + +func init() { + summonCmd.AddCommand(installCmd) +} diff --git a/cmd/show.go b/cmd/show.go new file mode 100644 index 0000000..5c148f7 --- /dev/null +++ b/cmd/show.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "github.com/infamousjoeg/conceal/pkg/conceal" + "github.com/infamousjoeg/conceal/pkg/conceal/keychain" + "github.com/spf13/cobra" +) + +// getCmd represents the get command +var showCmd = &cobra.Command{ + Use: "show", + Short: "Retrieves and prints secret value to STDOUT", + Long: `Retrieves and prints secret value to STDOUT. This is mainly used by the Summon conceal-summon provider. + + Example Usage: + $ conceal summon show aws/access_key_id`, + Args: cobra.ExactArgs(1), + Run: func(cmd *cobra.Command, args []string) { + err := keychain.GetSecret(args[0], "stdout") + if err != nil { + conceal.PrintError("Failed to get secret value from keychain.") + } + }, +} + +func init() { + summonCmd.AddCommand(showCmd) +} diff --git a/cmd/summon.go b/cmd/summon.go new file mode 100644 index 0000000..5afe128 --- /dev/null +++ b/cmd/summon.go @@ -0,0 +1,19 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +// getCmd represents the get command +var summonCmd = &cobra.Command{ + Use: "summon", + Short: "Commands related to Summon integration", + Long: `This command group includes commands for integrating Conceal with Summon. + + Example Usage: + $ conceal summon install`, +} + +func init() { + rootCmd.AddCommand(summonCmd) +} diff --git a/cmd/update.go b/cmd/update.go new file mode 100644 index 0000000..88069ce --- /dev/null +++ b/cmd/update.go @@ -0,0 +1,77 @@ +package cmd + +import ( + "bufio" + "fmt" + "os" + "strings" + "syscall" + + "github.com/infamousjoeg/conceal/pkg/conceal" + "github.com/infamousjoeg/conceal/pkg/conceal/keychain" + "github.com/spf13/cobra" + "golang.org/x/term" +) + +// updateCmd represents the get command +var updateCmd = &cobra.Command{ + Use: "update", + Short: "Updates a secret value in the secret provider", + Long: `Updates a secret value within the secret. + + Example Usage: + $ conceal update + $ conceal update aws/access_key_id + $ echo "new_secret_value" | conceal update aws/access_key_id`, + Run: func(cmd *cobra.Command, args []string) { + // Check if secret name is empty + secretName := conceal.GetSecretName(args) + + // Check stdin for secret value + var byteSecretVal []byte + info, err := os.Stdin.Stat() + if err != nil { + conceal.PrintError("An error occurred while checking stdin. Exiting...") + } + + // Update secret value from STDIN + if (info.Mode() & os.ModeCharDevice) == 0 { + // Reading from STDIN + reader := bufio.NewReader(os.Stdin) + input, err := reader.ReadString('\n') + if err != nil { + conceal.PrintError("An error occurred while reading stdin. Exiting...") + } + byteSecretVal = []byte(strings.TrimSpace(input)) + } else { + // Get secret value from user + fmt.Println("Please enter the secret value: ") + byteSecretVal, err = term.ReadPassword(int(syscall.Stdin)) + if err != nil { + conceal.PrintError("An error occurred trying to read password. Exiting...") + } + } + + // Update secret and secret value in keychain + err = keychain.UpdateSecret(secretName, byteSecretVal) + if err != nil { + conceal.PrintError("Failed to update secret value in keychain.") + } + + conceal.PrintSuccess("Secret value updated successfully.") + }, +} + +func init() { + rootCmd.AddCommand(updateCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // getCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // getCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +}