-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4cccaa0
Showing
6 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |