Skip to content
Snippets Groups Projects
Commit 03bef077 authored by Erik Hedenström's avatar Erik Hedenström
Browse files

initial functionality

parents
Branches
Tags
No related merge requests found
### Go template
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so
# Folders
_obj
_test
# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out
*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*
_testmain.go
*.exe
*.test
*.prof
vaultenv
\ No newline at end of file
LICENSE 0 → 100644
The MIT License (MIT)
Copyright © 2016 Erik Hedenström
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
package cmd
import (
"errors"
"gitlab.hedenstroem.com/go/vaultenv/vault"
"github.com/spf13/cobra"
)
var deleteCmd = &cobra.Command{
Use: "delete [flags] path key value",
Short: "delete Short",
Long: `delete Long`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 2 {
return errors.New("Expected 2 arguments")
}
data, err := vault.GetSecret(args[0])
if data != nil {
delete(data, args[1])
err = vault.PostSecret(args[0], data)
}
return
},
}
package cmd
import (
"encoding/json"
"errors"
"fmt"
"strings"
"gitlab.hedenstroem.com/go/vaultenv/vault"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var readCmd = &cobra.Command{
Use: "read [flags] path",
Short: "read Short",
Long: `read Long`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 1 {
return errors.New("Expected 1 argument")
}
data, err := vault.GetSecret(args[0])
if data != nil {
if viper.GetBool("shell") {
for k, v := range data {
fmt.Printf("%s=%s; export %s;\n", strings.ToUpper(k), v, strings.ToUpper(k))
}
} else {
b, _ := json.MarshalIndent(data, "", "\t")
fmt.Println(string(b))
}
}
return
},
}
package cmd
import (
"fmt"
"os"
"github.com/joho/godotenv"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var RootCmd = &cobra.Command{
Use: "vaultenv",
Short: "Root Short",
Long: `Root Long`,
}
func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(-1)
}
}
func init() {
cobra.OnInitialize(initEnv)
RootCmd.AddCommand(readCmd)
RootCmd.AddCommand(writeCmd)
RootCmd.AddCommand(deleteCmd)
RootCmd.PersistentFlags().StringP("addr", "a", "http://127.0.0.1:8200", "Address to the vault server")
RootCmd.PersistentFlags().StringP("token", "t", "", "Vault access token")
readCmd.Flags().BoolP("shell", "s", false, "Format output for eval(...)")
viper.SetEnvPrefix("VAULT")
viper.BindPFlag("ADDR", RootCmd.PersistentFlags().Lookup("addr"))
viper.BindPFlag("TOKEN", RootCmd.PersistentFlags().Lookup("token"))
viper.BindPFlag("shell", readCmd.Flags().Lookup("shell"))
}
func initEnv() {
_ = godotenv.Load(".env")
viper.AutomaticEnv()
}
package cmd
import (
"errors"
"github.com/spf13/cobra"
"gitlab.hedenstroem.com/go/vaultenv/vault"
)
var writeCmd = &cobra.Command{
Use: "write [flags] path key value",
Short: "write Short",
Long: `write Long`,
RunE: func(cmd *cobra.Command, args []string) (err error) {
if len(args) != 3 {
return errors.New("Expected 3 arguments")
}
data, err := vault.GetSecret(args[0])
if data != nil {
data[args[1]] = args[2]
err = vault.PostSecret(args[0], data)
}
return
},
}
main.go 0 → 100644
// Copyright © 2016 Erik Hedenström <erik@hedenstroem.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package main
import "gitlab.hedenstroem.com/go/vaultenv/cmd"
func main() {
cmd.Execute()
}
package vault
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"github.com/spf13/viper"
)
func GetSecret(path string) (data map[string]interface{}, err error) {
client := &http.Client{}
req, err := http.NewRequest("GET", viper.GetString("ADDR")+"/v1/"+path, nil)
if err != nil {
return
}
req.Header.Set("X-Vault-Token", viper.GetString("TOKEN"))
res, err := client.Do(req)
if err != nil {
return
}
if res.StatusCode == http.StatusOK {
var parsed map[string]interface{}
defer res.Body.Close()
json.NewDecoder(res.Body).Decode(&parsed)
data = parsed["data"].(map[string]interface{})
}
if res.StatusCode == http.StatusNotFound {
data = make(map[string]interface{})
}
return
}
func PostSecret(path string, data map[string]interface{}) (err error) {
b, err := json.MarshalIndent(data, "", "\t")
if err != nil {
return
}
client := &http.Client{}
var req *http.Request
if len(data) == 0 {
req, err = http.NewRequest("DELETE", viper.GetString("ADDR")+"/v1/"+path, nil)
} else {
req, err = http.NewRequest("POST", viper.GetString("ADDR")+"/v1/"+path, bytes.NewReader(b))
}
if err != nil {
return
}
req.Header.Set("X-Vault-Token", viper.GetString("TOKEN"))
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if res.StatusCode != http.StatusNoContent {
err = errors.New(res.Status)
}
return
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment