From e49f2baefaf460ebe676768c54c802af20a19c3e Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Thu, 15 Mar 2018 19:35:45 +0100 Subject: [PATCH 1/9] Added Encrypt/Decrypt into the client --- config/client/client.go | 47 +++++++++++++--- config/client/client_test.go | 100 +++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 6 deletions(-) diff --git a/config/client/client.go b/config/client/client.go index 59c3a8a..2992ce9 100644 --- a/config/client/client.go +++ b/config/client/client.go @@ -26,8 +26,10 @@ func ParseExtension(str string) (Extension, error) { } const ( - configPathFmt = "%s/%s/%s-%s.%s" - configFilePathFmt = "%s/%s/%s/%s/%s" + configPathFmt = "/%s/%s-%s.%s" + configFilePathFmt = "/%s/%s/%s/%s" + encryptPath = "/encrypt" + decryptPath = "/decrypt" ) const ( @@ -56,6 +58,12 @@ type Client interface { //FetchAsProperties queries the remote configuration service and returns the result as a Properties string FetchAsProperties() (string, error) + + //Encrypt encrypts the value server side and returns result + Encrypt(value string) (string, error) + + //Decrypt decrypts the value server side and returns result + Decrypt(value string) (string, error) } //Config needed to fetch a remote configuration @@ -75,6 +83,15 @@ func NewClient(c Config) Client { client := &client{ config: &c, } + + resty.SetHostURL(c.URI) + resty.OnAfterResponse(func(client *resty.Client, response *resty.Response) error { + if response.StatusCode() >= 300 || response.StatusCode() < 200 { + return fmt.Errorf("unexpected response code '%s'", response.Status()) + } + return nil + }) + return client } @@ -107,14 +124,32 @@ func (c *client) FetchAsYAML() (string, error) { //FetchAs queries the remote configuration service and returns the result in specified format func (c *client) FetchAs(extension Extension) (string, error) { - resp, err := resty.R().Get(c.formatURI(extension)) + resp, err := resty.R().Get(c.formatValuesURI(extension)) + return resp.String(), err +} + +//Encrypt encrypts the value server side and returns result +func (c *client) Encrypt(value string) (string, error) { + resp, err := resty.R(). + SetHeader("Content-Type", "text/plain"). + SetBody(value). + Post(encryptPath) + return resp.String(), err +} + +//Decrypt decrypts the value server side and returns result +func (c *client) Decrypt(value string) (string, error) { + resp, err := resty.R(). + SetHeader("Content-Type", "text/plain"). + SetBody(value). + Post(decryptPath) return resp.String(), err } -func (c *client) formatURI(extension Extension) string { - return fmt.Sprintf(configPathFmt, c.config.URI, c.config.Label, c.config.Application, c.config.Profile, extension) +func (c *client) formatValuesURI(extension Extension) string { + return fmt.Sprintf(configPathFmt, c.config.Label, c.config.Application, c.config.Profile, extension) } func (c *client) formatFileURI(source string) string { - return fmt.Sprintf(configFilePathFmt, c.config.URI, c.config.Application, c.config.Profile, c.config.Label, source) + return fmt.Sprintf(configFilePathFmt, c.config.Application, c.config.Profile, c.config.Label, source) } diff --git a/config/client/client_test.go b/config/client/client_test.go index d46f5a6..01e462f 100644 --- a/config/client/client_test.go +++ b/config/client/client_test.go @@ -1,6 +1,7 @@ package client import ( + "bytes" "fmt" "net/http" "net/http/httptest" @@ -34,6 +35,41 @@ func TestNewClient(t *testing.T) { assertString(t, "Incorrect Label", tp.label, c.Config().Label) } +func TestErrorResponse(t *testing.T) { + var tp = struct { + application string + profile string + label string + URI string + testContent string + fileName string + }{ + "service", + "profile", + "master", + "/service/profile/master/File", + "test", + "File", + } + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assertString(t, "Incorrect URI call", tp.URI, r.RequestURI) + w.WriteHeader(406) + })) + defer ts.Close() + + _, err := NewClient(Config{ + URI: ts.URL, + Application: tp.application, + Profile: tp.profile, + Label: tp.label, + }).FetchFile(tp.fileName) + + if err == nil { + t.Error("FetchFile should have failed") + } +} + func TestClient_FetchFile(t *testing.T) { var tp = struct { application string @@ -108,6 +144,70 @@ func TestClient_FetchAsYAML(t *testing.T) { assertString(t, "Content mismatch", tp.testContent, cont) } +func TestClient_Encrypt(t *testing.T) { + var tp = struct { + URI string + testContent string + }{ + "/encrypt", + "test", + } + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assertString(t, "Incorrect Method", "POST", r.Method) + assertString(t, "Incorrect URI call", tp.URI, r.RequestURI) + + buf := new(bytes.Buffer) + buf.ReadFrom(r.Body) + + assertString(t, "Incorrect Content received", tp.testContent, buf.String()) + fmt.Fprintln(w, tp.testContent) + })) + defer ts.Close() + + cont, err := NewClient(Config{ + URI: ts.URL, + }).Encrypt(tp.testContent) + + if err != nil { + t.Error("Encrypt failed with: ", err) + } + + assertString(t, "Content mismatch", tp.testContent, cont) +} + +func TestClient_Decrypt(t *testing.T) { + var tp = struct { + URI string + testContent string + }{ + "/decrypt", + "test", + } + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + assertString(t, "Incorrect Method", "POST", r.Method) + assertString(t, "Incorrect URI call", tp.URI, r.RequestURI) + + buf := new(bytes.Buffer) + buf.ReadFrom(r.Body) + + assertString(t, "Incorrect Content received", tp.testContent, buf.String()) + fmt.Fprintln(w, tp.testContent) + })) + defer ts.Close() + + cont, err := NewClient(Config{ + URI: ts.URL, + }).Decrypt(tp.testContent) + + if err != nil { + t.Error("Decrypt failed with: ", err) + } + + assertString(t, "Content mismatch", tp.testContent, cont) +} + func assertString(t *testing.T, message string, expected string, got string) { expected = trimString(expected) got = trimString(got) From b0b2646746c23d21f2a7a9889ea0586a8fc96b44 Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Fri, 16 Mar 2018 09:48:36 +0100 Subject: [PATCH 2/9] Test utils package --- config/internal/testutil.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 config/internal/testutil.go diff --git a/config/internal/testutil.go b/config/internal/testutil.go new file mode 100644 index 0000000..bfac657 --- /dev/null +++ b/config/internal/testutil.go @@ -0,0 +1,19 @@ +package testutil + +import ( + "strings" + "testing" +) + +//AssertString asserts string values and prints the expected and received values if failed +func AssertString(t *testing.T, message string, expected string, got string) { + expected = trimString(expected) + got = trimString(got) + if expected != got { + t.Errorf("%s: expected %s but got %s instead", message, expected, got) + } +} + +func trimString(s string) string { + return strings.TrimRight(s, "\n") +} From 875b78c86f2f38dcaade28aa930b8370daec1ebd Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Fri, 16 Mar 2018 09:49:14 +0100 Subject: [PATCH 3/9] Updated test with test package --- config/client/client_test.go | 48 ++++++++++++++---------------------- 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/config/client/client_test.go b/config/client/client_test.go index 01e462f..42214fe 100644 --- a/config/client/client_test.go +++ b/config/client/client_test.go @@ -3,9 +3,9 @@ package client import ( "bytes" "fmt" + "github.com/WanderaOrg/scccmd/config/internal" "net/http" "net/http/httptest" - "strings" "testing" ) @@ -29,10 +29,10 @@ func TestNewClient(t *testing.T) { Label: tp.label, }) - assertString(t, "Incorrect URI", tp.URI, c.Config().URI) - assertString(t, "Incorrect Application", tp.application, c.Config().Application) - assertString(t, "Incorrect Profile", tp.profile, c.Config().Profile) - assertString(t, "Incorrect Label", tp.label, c.Config().Label) + testutil.AssertString(t, "Incorrect URI", tp.URI, c.Config().URI) + testutil.AssertString(t, "Incorrect Application", tp.application, c.Config().Application) + testutil.AssertString(t, "Incorrect Profile", tp.profile, c.Config().Profile) + testutil.AssertString(t, "Incorrect Label", tp.label, c.Config().Label) } func TestErrorResponse(t *testing.T) { @@ -53,7 +53,7 @@ func TestErrorResponse(t *testing.T) { } ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assertString(t, "Incorrect URI call", tp.URI, r.RequestURI) + testutil.AssertString(t, "Incorrect URI call", tp.URI, r.RequestURI) w.WriteHeader(406) })) defer ts.Close() @@ -88,7 +88,7 @@ func TestClient_FetchFile(t *testing.T) { } ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assertString(t, "Incorrect URI call", tp.URI, r.RequestURI) + testutil.AssertString(t, "Incorrect URI call", tp.URI, r.RequestURI) fmt.Fprintln(w, tp.testContent) })) defer ts.Close() @@ -104,7 +104,7 @@ func TestClient_FetchFile(t *testing.T) { t.Error("FetchFile failed with: ", err) } - assertString(t, "Content mismatch", tp.testContent, string(cont)) + testutil.AssertString(t, "Content mismatch", tp.testContent, string(cont)) } func TestClient_FetchAsYAML(t *testing.T) { @@ -125,7 +125,7 @@ func TestClient_FetchAsYAML(t *testing.T) { } ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assertString(t, "Incorrect URI call", tp.URI, r.RequestURI) + testutil.AssertString(t, "Incorrect URI call", tp.URI, r.RequestURI) fmt.Fprintln(w, tp.testContent) })) defer ts.Close() @@ -141,7 +141,7 @@ func TestClient_FetchAsYAML(t *testing.T) { t.Error("FetchFile failed with: ", err) } - assertString(t, "Content mismatch", tp.testContent, cont) + testutil.AssertString(t, "Content mismatch", tp.testContent, cont) } func TestClient_Encrypt(t *testing.T) { @@ -154,13 +154,13 @@ func TestClient_Encrypt(t *testing.T) { } ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assertString(t, "Incorrect Method", "POST", r.Method) - assertString(t, "Incorrect URI call", tp.URI, r.RequestURI) + testutil.AssertString(t, "Incorrect Method", "POST", r.Method) + testutil.AssertString(t, "Incorrect URI call", tp.URI, r.RequestURI) buf := new(bytes.Buffer) buf.ReadFrom(r.Body) - assertString(t, "Incorrect Content received", tp.testContent, buf.String()) + testutil.AssertString(t, "Incorrect Content received", tp.testContent, buf.String()) fmt.Fprintln(w, tp.testContent) })) defer ts.Close() @@ -173,7 +173,7 @@ func TestClient_Encrypt(t *testing.T) { t.Error("Encrypt failed with: ", err) } - assertString(t, "Content mismatch", tp.testContent, cont) + testutil.AssertString(t, "Content mismatch", tp.testContent, cont) } func TestClient_Decrypt(t *testing.T) { @@ -186,13 +186,13 @@ func TestClient_Decrypt(t *testing.T) { } ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - assertString(t, "Incorrect Method", "POST", r.Method) - assertString(t, "Incorrect URI call", tp.URI, r.RequestURI) + testutil.AssertString(t, "Incorrect Method", "POST", r.Method) + testutil.AssertString(t, "Incorrect URI call", tp.URI, r.RequestURI) buf := new(bytes.Buffer) buf.ReadFrom(r.Body) - assertString(t, "Incorrect Content received", tp.testContent, buf.String()) + testutil.AssertString(t, "Incorrect Content received", tp.testContent, buf.String()) fmt.Fprintln(w, tp.testContent) })) defer ts.Close() @@ -205,17 +205,5 @@ func TestClient_Decrypt(t *testing.T) { t.Error("Decrypt failed with: ", err) } - assertString(t, "Content mismatch", tp.testContent, cont) -} - -func assertString(t *testing.T, message string, expected string, got string) { - expected = trimString(expected) - got = trimString(got) - if expected != got { - t.Errorf("%s: expected %s but got %s instead", message, expected, got) - } -} - -func trimString(s string) string { - return strings.TrimRight(s, "\n") + testutil.AssertString(t, "Content mismatch", tp.testContent, cont) } From 1b7d70fc1e23470aa07ce496d88b5e2169bc920a Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Fri, 16 Mar 2018 09:49:25 +0100 Subject: [PATCH 4/9] Decrypt command --- config/cmd/decrypt.go | 48 ++++++++++++++++++++++++++++++ config/cmd/decrypt_test.go | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 config/cmd/decrypt.go create mode 100644 config/cmd/decrypt_test.go diff --git a/config/cmd/decrypt.go b/config/cmd/decrypt.go new file mode 100644 index 0000000..3167bb2 --- /dev/null +++ b/config/cmd/decrypt.go @@ -0,0 +1,48 @@ +package cmd + +import ( + "fmt" + "github.com/WanderaOrg/scccmd/config/client" + "github.com/spf13/cobra" + "io/ioutil" + "os" +) + +var dp = struct { + source string + value string +}{} + +var decryptCmd = &cobra.Command{ + Use: "decrypt", + Short: "Decrypt the value server-side and prints the response", + RunE: func(cmd *cobra.Command, args []string) error { + return executeDecrypt(args) + }, +} + +func executeDecrypt(args []string) error { + if dp.value == "" { + bytes, err := ioutil.ReadAll(os.Stdin) + + dp.value = string(bytes) + if err != nil { + return err + } + } + + if res, err := client.NewClient(client.Config{ + URI: dp.source, + }).Decrypt(dp.value); err == nil { + fmt.Println(res) + return nil + } else { + return err + } +} + +func init() { + decryptCmd.Flags().StringVarP(&dp.source, "source", "s", "", "address of the config server") + decryptCmd.Flags().StringVar(&dp.value, "value", "", "value to decrypt *WARNING* unsafe use standard-in instead") + decryptCmd.MarkFlagRequired("source") +} diff --git a/config/cmd/decrypt_test.go b/config/cmd/decrypt_test.go new file mode 100644 index 0000000..b6766a8 --- /dev/null +++ b/config/cmd/decrypt_test.go @@ -0,0 +1,60 @@ +package cmd + +import ( + "bytes" + "fmt" + "github.com/WanderaOrg/scccmd/config/internal" + "net/http" + "net/http/httptest" + "testing" +) + +func TestExecuteDecrypt(t *testing.T) { + var tp = struct { + URI string + testContent string + }{ + "/decrypt", + "test", + } + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + testutil.AssertString(t, "Incorrect Method", "POST", r.Method) + testutil.AssertString(t, "Incorrect URI call", tp.URI, r.RequestURI) + + buf := new(bytes.Buffer) + buf.ReadFrom(r.Body) + + testutil.AssertString(t, "Incorrect Content received", tp.testContent, buf.String()) + fmt.Fprintln(w, tp.testContent) + })) + defer ts.Close() + + dp.source = ts.URL + dp.value = tp.testContent + err := executeDecrypt(nil) + + if err != nil { + t.Error("Decrypt failed with: ", err) + } +} + +func ExampleExecuteDecrypt() { + var tp = struct { + URI string + testContent string + }{ + "/decrypt", + "test", + } + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, tp.testContent) + })) + defer ts.Close() + + dp.source = ts.URL + dp.value = tp.testContent + executeDecrypt(nil) + //Output: test +} From afaaffcdf00101ff1b289a10c41073965a138205 Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Fri, 16 Mar 2018 09:49:38 +0100 Subject: [PATCH 5/9] Encrypt command --- config/cmd/encrypt.go | 48 ++++++++++++++++++++++++++++++ config/cmd/encrypt_test.go | 60 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 config/cmd/encrypt.go create mode 100644 config/cmd/encrypt_test.go diff --git a/config/cmd/encrypt.go b/config/cmd/encrypt.go new file mode 100644 index 0000000..9a42738 --- /dev/null +++ b/config/cmd/encrypt.go @@ -0,0 +1,48 @@ +package cmd + +import ( + "fmt" + "github.com/WanderaOrg/scccmd/config/client" + "github.com/spf13/cobra" + "io/ioutil" + "os" +) + +var ep = struct { + source string + value string +}{} + +var encryptCmd = &cobra.Command{ + Use: "encrypt", + Short: "Encrypt the value server-side and prints the response", + RunE: func(cmd *cobra.Command, args []string) error { + return executeEncrypt(args) + }, +} + +func executeEncrypt(args []string) error { + if ep.value == "" { + bytes, err := ioutil.ReadAll(os.Stdin) + + ep.value = string(bytes) + if err != nil { + return err + } + } + + if res, err := client.NewClient(client.Config{ + URI: ep.source, + }).Encrypt(ep.value); err == nil { + fmt.Println(res) + return nil + } else { + return err + } +} + +func init() { + encryptCmd.Flags().StringVarP(&ep.source, "source", "s", "", "address of the config server") + encryptCmd.Flags().StringVar(&ep.value, "value", "", "value to encrypt *WARNING* unsafe use standard-in instead") + encryptCmd.MarkFlagRequired("source") +} diff --git a/config/cmd/encrypt_test.go b/config/cmd/encrypt_test.go new file mode 100644 index 0000000..d318f36 --- /dev/null +++ b/config/cmd/encrypt_test.go @@ -0,0 +1,60 @@ +package cmd + +import ( + "bytes" + "fmt" + "github.com/WanderaOrg/scccmd/config/internal" + "net/http" + "net/http/httptest" + "testing" +) + +func TestExecuteEncrypt(t *testing.T) { + var tp = struct { + URI string + testContent string + }{ + "/encrypt", + "test", + } + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + testutil.AssertString(t, "Incorrect Method", "POST", r.Method) + testutil.AssertString(t, "Incorrect URI call", tp.URI, r.RequestURI) + + buf := new(bytes.Buffer) + buf.ReadFrom(r.Body) + + testutil.AssertString(t, "Incorrect Content received", tp.testContent, buf.String()) + fmt.Fprintln(w, tp.testContent) + })) + defer ts.Close() + + ep.source = ts.URL + ep.value = tp.testContent + err := executeEncrypt(nil) + + if err != nil { + t.Error("Encrypt failed with: ", err) + } +} + +func ExampleExecuteEncrypt() { + var tp = struct { + URI string + testContent string + }{ + "/encrypt", + "test", + } + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, tp.testContent) + })) + defer ts.Close() + + ep.source = ts.URL + ep.value = tp.testContent + executeEncrypt(nil) + //Output: test +} From dd76910ae2ff0828230f06b92971a52dbc0ca09e Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Fri, 16 Mar 2018 09:50:09 +0100 Subject: [PATCH 6/9] Integrated encrypt/decrypt commands --- config/cmd/root.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/cmd/root.go b/config/cmd/root.go index e32a2cd..33830da 100644 --- a/config/cmd/root.go +++ b/config/cmd/root.go @@ -20,6 +20,8 @@ func init() { rootCmd.AddCommand(getCmd) rootCmd.AddCommand(genDocCmd) rootCmd.AddCommand(initializerCmd) + rootCmd.AddCommand(encryptCmd) + rootCmd.AddCommand(decryptCmd) } //Execute run root command (main entrypoint) From 2c830b006e5fe54ad4671b539c6f4027ebf06984 Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Fri, 16 Mar 2018 09:50:34 +0100 Subject: [PATCH 7/9] Updated documentation --- docs/config.md | 2 ++ docs/config_decrypt.md | 30 ++++++++++++++++++++++++++++++ docs/config_encrypt.md | 30 ++++++++++++++++++++++++++++++ docs/config_initializer.md | 11 +++++------ 4 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 docs/config_decrypt.md create mode 100644 docs/config_encrypt.md diff --git a/docs/config.md b/docs/config.md index 477b991..3daeffa 100644 --- a/docs/config.md +++ b/docs/config.md @@ -16,6 +16,8 @@ Tool currently provides functionality t get (download) config file from server. ``` ### SEE ALSO +* [config decrypt](config_decrypt.md) - Decrypt the value server-side and prints the response +* [config encrypt](config_encrypt.md) - Encrypt the value server-side and prints the response * [config gendoc](config_gendoc.md) - Generates documentation for this tool in Markdown format * [config get](config_get.md) - Get the config from the given config server * [config initializer](config_initializer.md) - Runs K8s initializer for injecting config from Cloud Config Server diff --git a/docs/config_decrypt.md b/docs/config_decrypt.md new file mode 100644 index 0000000..d4b21f9 --- /dev/null +++ b/docs/config_decrypt.md @@ -0,0 +1,30 @@ +## config decrypt + +Decrypt the value server-side and prints the response + +### Synopsis + + +Decrypt the value server-side and prints the response + +``` +config decrypt [flags] +``` + +### Options + +``` + -h, --help help for decrypt + -s, --source string address of the config server + --value string value to decrypt *WARNING* unsafe use standard-in instead +``` + +### Options inherited from parent commands + +``` + -v, --verbose verbose output +``` + +### SEE ALSO +* [config](config.md) - Spring Cloud Config management tool + diff --git a/docs/config_encrypt.md b/docs/config_encrypt.md new file mode 100644 index 0000000..2d6f540 --- /dev/null +++ b/docs/config_encrypt.md @@ -0,0 +1,30 @@ +## config encrypt + +Encrypt the value server-side and prints the response + +### Synopsis + + +Encrypt the value server-side and prints the response + +``` +config encrypt [flags] +``` + +### Options + +``` + -h, --help help for encrypt + -s, --source string address of the config server + --value string value to encrypt *WARNING* unsafe use standard-in instead +``` + +### Options inherited from parent commands + +``` + -v, --verbose verbose output +``` + +### SEE ALSO +* [config](config.md) - Spring Cloud Config management tool + diff --git a/docs/config_initializer.md b/docs/config_initializer.md index 2fec52b..f09952c 100644 --- a/docs/config_initializer.md +++ b/docs/config_initializer.md @@ -14,12 +14,11 @@ config initializer [flags] ### Options ``` - -c, --configmap string The config initializer configuration configmap (default "initializer-config") - -h, --help help for initializer - -i, --initializer-name string The initializer name (default "config.initializer.kubernetes.io") - --kubeconfig If kubeconfig should b e used for connecting to the cluster, mainly for debugging purposes, when false command autodiscover configuration from within the cluster - -n, --namespace string The configuration namespace (default "default") - -e, --watched-namespace string The initializer watched namespace (default all) + -c, --configmap string The config initializer configuration configmap (default "initializer-config") + -h, --help help for initializer + -i, --initializer-name string The initializer name (default "config.initializer.kubernetes.io") + --kubeconfig If kubeconfig should b e used for connecting to the cluster, mainly for debugging purposes, when false command autodiscover configuration from within the cluster + -n, --namespace string The configuration namespace (default "default") ``` ### Options inherited from parent commands From 66f95766c902fdb7fedef0ab3c4643849edcc651 Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Fri, 16 Mar 2018 10:06:58 +0100 Subject: [PATCH 8/9] Fixed linter warnings --- config/cmd/decrypt.go | 11 ++++++----- config/cmd/encrypt.go | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/config/cmd/decrypt.go b/config/cmd/decrypt.go index 3167bb2..2761d9f 100644 --- a/config/cmd/decrypt.go +++ b/config/cmd/decrypt.go @@ -31,14 +31,15 @@ func executeDecrypt(args []string) error { } } - if res, err := client.NewClient(client.Config{ + res, err := client.NewClient(client.Config{ URI: dp.source, - }).Decrypt(dp.value); err == nil { + }).Decrypt(dp.value) + + if err == nil { fmt.Println(res) - return nil - } else { - return err } + + return err } func init() { diff --git a/config/cmd/encrypt.go b/config/cmd/encrypt.go index 9a42738..5aea6ee 100644 --- a/config/cmd/encrypt.go +++ b/config/cmd/encrypt.go @@ -31,14 +31,15 @@ func executeEncrypt(args []string) error { } } - if res, err := client.NewClient(client.Config{ + res, err := client.NewClient(client.Config{ URI: ep.source, - }).Encrypt(ep.value); err == nil { + }).Encrypt(ep.value) + + if err == nil { fmt.Println(res) - return nil - } else { - return err } + + return err } func init() { From e880a2c2acdddc02093c1132974836ed8adaeca0 Mon Sep 17 00:00:00 2001 From: "jakub.coufal" Date: Fri, 16 Mar 2018 10:10:25 +0100 Subject: [PATCH 9/9] Cache dependencies between builds --- .travis.yml | 5 +++++ config/cmd/decrypt.go | 5 +++-- config/cmd/decrypt_test.go | 4 ++-- config/cmd/encrypt.go | 5 +++-- config/cmd/encrypt_test.go | 4 ++-- config/cmd/get.go | 10 ++++++---- config/cmd/get_test.go | 6 +++--- 7 files changed, 24 insertions(+), 15 deletions(-) diff --git a/.travis.yml b/.travis.yml index b7d90d8..35701fc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -21,6 +21,11 @@ matrix: # tests pass on the stable versions of Go. fast_finish: true +cache: + directories: + - $GOPATH/pkg + - $GOPATH/bin + env: - DEP_VERSION="0.4.1" diff --git a/config/cmd/decrypt.go b/config/cmd/decrypt.go index 2761d9f..706fa4a 100644 --- a/config/cmd/decrypt.go +++ b/config/cmd/decrypt.go @@ -17,11 +17,12 @@ var decryptCmd = &cobra.Command{ Use: "decrypt", Short: "Decrypt the value server-side and prints the response", RunE: func(cmd *cobra.Command, args []string) error { - return executeDecrypt(args) + return ExecuteDecrypt(args) }, } -func executeDecrypt(args []string) error { +//ExecuteDecrypt runs decrypt cmd +func ExecuteDecrypt(args []string) error { if dp.value == "" { bytes, err := ioutil.ReadAll(os.Stdin) diff --git a/config/cmd/decrypt_test.go b/config/cmd/decrypt_test.go index b6766a8..e027f21 100644 --- a/config/cmd/decrypt_test.go +++ b/config/cmd/decrypt_test.go @@ -32,7 +32,7 @@ func TestExecuteDecrypt(t *testing.T) { dp.source = ts.URL dp.value = tp.testContent - err := executeDecrypt(nil) + err := ExecuteDecrypt(nil) if err != nil { t.Error("Decrypt failed with: ", err) @@ -55,6 +55,6 @@ func ExampleExecuteDecrypt() { dp.source = ts.URL dp.value = tp.testContent - executeDecrypt(nil) + ExecuteDecrypt(nil) //Output: test } diff --git a/config/cmd/encrypt.go b/config/cmd/encrypt.go index 5aea6ee..7be1458 100644 --- a/config/cmd/encrypt.go +++ b/config/cmd/encrypt.go @@ -17,11 +17,12 @@ var encryptCmd = &cobra.Command{ Use: "encrypt", Short: "Encrypt the value server-side and prints the response", RunE: func(cmd *cobra.Command, args []string) error { - return executeEncrypt(args) + return ExecuteEncrypt(args) }, } -func executeEncrypt(args []string) error { +//ExecuteEncrypt runs encrypt cmd +func ExecuteEncrypt(args []string) error { if ep.value == "" { bytes, err := ioutil.ReadAll(os.Stdin) diff --git a/config/cmd/encrypt_test.go b/config/cmd/encrypt_test.go index d318f36..3d7f1d5 100644 --- a/config/cmd/encrypt_test.go +++ b/config/cmd/encrypt_test.go @@ -32,7 +32,7 @@ func TestExecuteEncrypt(t *testing.T) { ep.source = ts.URL ep.value = tp.testContent - err := executeEncrypt(nil) + err := ExecuteEncrypt(nil) if err != nil { t.Error("Encrypt failed with: ", err) @@ -55,6 +55,6 @@ func ExampleExecuteEncrypt() { ep.source = ts.URL ep.value = tp.testContent - executeEncrypt(nil) + ExecuteEncrypt(nil) //Output: test } diff --git a/config/cmd/get.go b/config/cmd/get.go index abbcb31..29749ad 100644 --- a/config/cmd/get.go +++ b/config/cmd/get.go @@ -26,7 +26,7 @@ var getValuesCmd = &cobra.Command{ Use: "values", Short: "Get the config values in specified format from the given config server", RunE: func(cmd *cobra.Command, args []string) error { - return executeGetValues(args) + return ExecuteGetValues(args) }, } @@ -34,11 +34,12 @@ var getFilesCmd = &cobra.Command{ Use: "files", Short: "Get the config files from the given config server", RunE: func(cmd *cobra.Command, args []string) error { - return executeGetFiles(args) + return ExecuteGetFiles(args) }, } -func executeGetValues(args []string) error { +//ExecuteGetValues runs get values cmd +func ExecuteGetValues(args []string) error { ext, err := client.ParseExtension(gp.format) if err != nil { @@ -73,7 +74,8 @@ func executeGetValues(args []string) error { return nil } -func executeGetFiles(args []string) error { +//ExecuteGetFiles runs get files cmd +func ExecuteGetFiles(args []string) error { for _, mapping := range gp.fileMappings.Mappings() { resp, err := client. NewClient(client.Config{URI: gp.source, Profile: gp.profile, Application: gp.application, Label: gp.label}). diff --git a/config/cmd/get_test.go b/config/cmd/get_test.go index 64bbe63..153065a 100644 --- a/config/cmd/get_test.go +++ b/config/cmd/get_test.go @@ -11,7 +11,7 @@ import ( ) func TestNoArgExecute(t *testing.T) { - err := executeGetFiles(nil) + err := ExecuteGetFiles(nil) if err != nil { t.Error("Execute failed with: ", err) } @@ -68,7 +68,7 @@ func TestExecuteGetFiles(t *testing.T) { gp.fileMappings = FileMappings{mappings: make([]FileMapping, 1)} gp.fileMappings.mappings[0] = FileMapping{source: tp.srcFileName, destination: tp.destFileName} - if err := executeGetFiles(nil); err != nil { + if err := ExecuteGetFiles(nil); err != nil { t.Error("Execute failed with: ", err) } @@ -131,7 +131,7 @@ func TestExecuteGetValues(t *testing.T) { gp.source = ts.URL gp.destination = tp.destFileName - if err := executeGetValues(nil); err != nil { + if err := ExecuteGetValues(nil); err != nil { t.Error("Execute failed with: ", err) }