Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kiriappeee committed Sep 4, 2018
0 parents commit 4cccaa0
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[prune]
go-tests = true
unused-packages = true

[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.3"
82 changes: 82 additions & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package cmd

import (
"fmt"
"log"
"sort"

"encoding/base64"

"github.com/bufferapp/kubesecret/core"
"github.com/spf13/cobra"
)

var namespace string
var secretKey string
var allKeys bool

var getCmd = &cobra.Command{
Use: "get",
Short: "Gets the keys and their values for a given secret",
Run: func(cmd *cobra.Command, args []string) {
s, err := core.GetSecretsByNamespaceAndName(namespace, args[0])
if err == nil {
keys := make([]string, 0, len(s.Data))
for k := range s.Data {
keys = append(keys, k)
}
sort.Strings(keys)

if secretKey == "" {
if allKeys {
fmt.Printf("List of keys with associated values\n\n")
for _, k := range keys {
value, err := base64.StdEncoding.DecodeString(s.Data[k])
if err != nil {
log.Fatalln(err)
} else {
fmt.Printf("%s: %s\n ------------ \n", k, value)
}
}
fmt.Printf("\n\n")
}
fmt.Printf("List of all keys for %s\n\n", args[0])
for _, k := range keys {
fmt.Printf("%s\n", k)
}
fmt.Printf("\n\nUse kubesecret get %s -k key --namespace %s to print the value for a specific key\n", args[0], namespace)
} else {
found := false
for k, v := range s.Data {
if found {
break
} else {
if k == secretKey {
found = true
decodedValue, err := base64.StdEncoding.DecodeString(v)
if err != nil {
log.Fatalln(err)
} else {
fmt.Printf("%s: %s\n", k, decodedValue)
}
}
}
}
if !found {
fmt.Println("Could not find the key specified")
}
}
} else {
fmt.Println(err)
}
},
Args: cobra.ExactArgs(1),
}

func init() {
rootCmd.AddCommand(getCmd)
getCmd.Flags().StringVarP(&namespace, "namespace", "n", "", "Namespace to search for secret in. (Required)")
getCmd.Flags().StringVarP(&secretKey, "key", "k", "", "Specific key value to get")
getCmd.Flags().BoolVar(&allKeys, "all-keys", false, "List the values of all the keys")
getCmd.MarkFlagRequired("namespace")
}
20 changes: 20 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "kubesecret",
Short: "Gets a specified secret for a given namespace",
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
71 changes: 71 additions & 0 deletions core/getsecret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package core

import (
"encoding/json"
"errors"
"log"
"os/exec"
"strings"
)

type metadata struct {
Name string `json:"Name"`
Namespace string `json:"namespace"`
}
type Secret struct {
APIVersion string `json:"apiVersion"`
Type string `json:"type"`
Kind string `json:"kind"`
Metadata metadata `json:"metadata"`
Data map[string]string `json:"data"`
}

type secrets struct {
APIVersion string `json:"apiVersion"`
Items []Secret `json:"Items"`
}

// GetSecrets Executes the kubectl command line
func GetSecrets(namespace string) {
log.Println("Executing kubectl")
out, _ := exec.Command("kubectl", "get", "secrets", "--namespace", "buffer", "-o", "json").CombinedOutput()

var dataRetrived map[string]interface{}
if err := json.Unmarshal(out, &dataRetrived); err != nil {
log.Fatal(err)
}
for k := range dataRetrived {
log.Println(k)
}

var secretsRetrieved secrets
if err := json.Unmarshal(out, &secretsRetrieved); err != nil {
log.Fatal(err)
}
log.Printf("Length of items: %d\n", len(secretsRetrieved.Items))
log.Println(secretsRetrieved.Items[0].APIVersion)
log.Println(len(secretsRetrieved.Items[0].Data))
log.Println(secretsRetrieved.Items[0].Metadata.Name)
for _, secretRetrieved := range secretsRetrieved.Items {
log.Println(secretRetrieved.Metadata.Name)
for k, v := range secretRetrieved.Data {
log.Printf("%s: %s\n", k, v)
}
}
}

// GetSecretsByNamespaceAndName returns a secret for a given namespace and secret name
func GetSecretsByNamespaceAndName(namespace, secretName string) (Secret, error) {
if namespace == "" || secretName == "" {
return Secret{}, errors.New("Namespace or secretName cannot be empty")
}
out, _ := exec.Command("kubectl", "get", "secret", "--namespace", namespace, secretName, "-o", "json").CombinedOutput()
var secretRetrieved Secret
if strings.Contains(string(out), "Error from server (NotFound)") {
return Secret{}, errors.New(string(out))
}
if err := json.Unmarshal(out, &secretRetrieved); err != nil {
return Secret{}, err
}
return secretRetrieved, nil
}
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/bufferapp/kubesecret/cmd"
)

func main() {
cmd.Execute()
}

0 comments on commit 4cccaa0

Please sign in to comment.