From 1921e3f2cf6227adfee138dcc9a125ddf7478221 Mon Sep 17 00:00:00 2001 From: Kelly Gerritz Date: Mon, 5 Jun 2017 16:57:04 -0400 Subject: [PATCH] Adds --output-json for get command - Refactoring of test string constants [#145087513] Return json should be an application option for all commands Signed-off-by: Edie Beer --- commands/commands_suite_test.go | 21 +++++--- commands/generate_test.go | 14 ++--- commands/get_test.go | 14 ++--- commands/set.go | 3 +- commands/set_test.go | 92 ++++++++++++++++++++++++++++----- 5 files changed, 110 insertions(+), 34 deletions(-) diff --git a/commands/commands_suite_test.go b/commands/commands_suite_test.go index ece8ac29..b1e4a1ac 100644 --- a/commands/commands_suite_test.go +++ b/commands/commands_suite_test.go @@ -50,13 +50,20 @@ const RSA_CREDENTIAL_RESPONSE_YAML = "id: " + UUID + "\nname: %s\ntype: rsa\nval const USER_CREDENTIAL_RESPONSE_YAML = "id: " + UUID + "\nname: %s\ntype: user\nvalue:\n password: %s\n password_hash: %s\n username: %s\nversion_created_at: " + TIMESTAMP const USER_WITHOUT_USERNAME_CREDENTIAL_RESPONSE_YAML = "id: " + UUID + "\nname: %s\ntype: user\nvalue:\n password: %s\n password_hash: %s\n username: null\nversion_created_at: " + TIMESTAMP -var responseMyValuePotatoes = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-value", "value", "potatoes") -var responseMyPasswordPotatoes = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-password", "password", "potatoes") -var responseMyJson = fmt.Sprintf(JSON_CREDENTIAL_RESPONSE_YAML, "json-secret", " an:\n - array\n foo: bar\n nested:\n a: 1") -var responseMyCertificate = fmt.Sprintf(CERTIFICATE_CREDENTIAL_RESPONSE_YAML, "my-secret", "my-ca", "my-cert", "my-priv") -var responseMySSHFoo = fmt.Sprintf(SSH_CREDENTIAL_RESPONSE_YAML, "foo-ssh-key", "some-private-key", "some-public-key") -var responseMyRSAFoo = fmt.Sprintf(RSA_CREDENTIAL_RESPONSE_YAML, "foo-rsa-key", "some-private-key", "some-public-key") -var responseMyUsername = fmt.Sprintf(USER_CREDENTIAL_RESPONSE_YAML, "my-username-credential", "test-password", "passw0rd-H4$h", "my-username") +var responseMyValuePotatoesYaml = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-value", "value", "potatoes") +var responseMyValuePotatoesJson = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_JSON, "value", "my-value", "potatoes") +var responseMyPasswordPotatoesYaml = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-password", "password", "potatoes") +var responseMyPasswordPotatoesJson = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_JSON, "password", "my-password", "potatoes") +var responseMyJsonFormatYaml = fmt.Sprintf(JSON_CREDENTIAL_RESPONSE_YAML, "json-secret", " an:\n - array\n foo: bar\n nested:\n a: 1") +var responseMyJsonFormatJson = fmt.Sprintf(JSON_CREDENTIAL_RESPONSE_JSON, "json-secret", "{\"an\": [\"array\"], \"foo\": \"bar\", \"nested\": {\"a\": 1}}") +var responseMyCertificateYaml = fmt.Sprintf(CERTIFICATE_CREDENTIAL_RESPONSE_YAML, "my-secret", "my-ca", "my-cert", "my-priv") +var responseMyCertificateJson = fmt.Sprintf(CERTIFICATE_CREDENTIAL_RESPONSE_JSON, "my-secret", "my-ca", "my-cert", "my-priv") +var responseMySSHFooYaml = fmt.Sprintf(SSH_CREDENTIAL_RESPONSE_YAML, "foo-ssh-key", "some-private-key", "some-public-key") +var responseMySSHFooJson = fmt.Sprintf(RSA_SSH_CREDENTIAL_RESPONSE_JSON, "ssh", "foo-ssh-key", "some-public-key", "some-private-key") +var responseMyRSAFooYaml = fmt.Sprintf(RSA_CREDENTIAL_RESPONSE_YAML, "foo-rsa-key", "some-private-key", "some-public-key") +var responseMyRSAFooJson = fmt.Sprintf(RSA_SSH_CREDENTIAL_RESPONSE_JSON, "rsa", "foo-rsa-key", "some-public-key", "some-private-key") +var responseMyUsernameYaml = fmt.Sprintf(USER_CREDENTIAL_RESPONSE_YAML, "my-username-credential", "test-password", "passw0rd-H4$h", "my-username") +var responseMyUsernameJson = fmt.Sprintf(USER_CREDENTIAL_RESPONSE_JSON, "my-username-credential", "my-username", "test-password", "passw0rd-H4$h") var responseMySpecialCharacterValue = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-character-test", "value", `'{"password":"some-still-bad-password"}'`) var responseMySpecialCharacterPassword = fmt.Sprintf(STRING_CREDENTIAL_RESPONSE_YAML, "my-character-test", "password", `'{"password":"some-still-bad-password"}'`) var responseMySpecialCharacterJson = fmt.Sprintf(JSON_CREDENTIAL_RESPONSE_YAML, "my-character-test", " foo: b\"ar") diff --git a/commands/generate_test.go b/commands/generate_test.go index 55357236..8559c175 100644 --- a/commands/generate_test.go +++ b/commands/generate_test.go @@ -42,7 +42,7 @@ var _ = Describe("Generate", func() { session := runCommand("generate", "-n", "my-password", "-t", "password") Eventually(session).Should(Exit(0)) - Expect(session.Out).To(Say(responseMyPasswordPotatoes)) + Expect(session.Out).To(Say(responseMyPasswordPotatoesYaml)) }) It("can print the generated password secret as JSON", func() { @@ -68,7 +68,7 @@ var _ = Describe("Generate", func() { session := runCommand("generate", "-n", "my-password", "-t", "password") Eventually(session).Should(Exit(0)) - Expect(session.Out).To(Say(responseMyPasswordPotatoes)) + Expect(session.Out).To(Say(responseMyPasswordPotatoesYaml)) }) It("can print the secret as JSON", func() { @@ -135,7 +135,7 @@ var _ = Describe("Generate", func() { session := runCommand("generate", "-n", "foo-ssh-key", "-t", "ssh") Eventually(session).Should(Exit(0)) - Expect(session.Out).To(Say(responseMySSHFoo)) + Expect(session.Out).To(Say(responseMySSHFooYaml)) }) It("can print the SSH key as JSON", func() { @@ -183,7 +183,7 @@ var _ = Describe("Generate", func() { session := runCommand("generate", "-n", "foo-rsa-key", "-t", "rsa") Eventually(session).Should(Exit(0)) - Expect(session.Out).To(Say(responseMyRSAFoo)) + Expect(session.Out).To(Say(responseMyRSAFooYaml)) }) It("can print the RSA key as JSON", func() { @@ -225,7 +225,7 @@ var _ = Describe("Generate", func() { session := runCommand("generate", "-n", "my-secret", "-t", "certificate", "--common-name", "common.name.io") Eventually(session).Should(Exit(0)) - Expect(session.Out).To(Say(responseMyCertificate)) + Expect(session.Out).To(Say(responseMyCertificateYaml)) }) It("can print the certificate as JSON", func() { @@ -368,7 +368,7 @@ var _ = Describe("Generate", func() { session := runCommand("generate", "-n", name, "-t", "user") Eventually(session).Should(Exit(0)) - Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsername)) + Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsernameYaml)) }) It("should accept a statically provided username", func() { @@ -383,7 +383,7 @@ var _ = Describe("Generate", func() { session := runCommand("generate", "-n", name, "-t", "user", "-z", "my-username") Eventually(session).Should(Exit(0)) - Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsername)) + Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsernameYaml)) }) It("with with no-overwrite", func() { diff --git a/commands/get_test.go b/commands/get_test.go index 177865f7..bf3c7ac0 100644 --- a/commands/get_test.go +++ b/commands/get_test.go @@ -55,7 +55,7 @@ var _ = Describe("Get", func() { session := runCommand("get", "-n", "my-value") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyValuePotatoes)) + Eventually(session.Out).Should(Say(responseMyValuePotatoesYaml)) }) It("gets a password secret", func() { @@ -71,7 +71,7 @@ var _ = Describe("Get", func() { session := runCommand("get", "-n", "my-password") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyPasswordPotatoes)) + Eventually(session.Out).Should(Say(responseMyPasswordPotatoesYaml)) }) It("gets a json secret", func() { @@ -87,7 +87,7 @@ var _ = Describe("Get", func() { session := runCommand("get", "-n", "json-secret") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyJson)) + Eventually(session.Out).Should(Say(responseMyJsonFormatYaml)) }) It("gets a certificate secret", func() { @@ -103,7 +103,7 @@ var _ = Describe("Get", func() { session := runCommand("get", "-n", "my-secret") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyCertificate)) + Eventually(session.Out).Should(Say(responseMyCertificateYaml)) }) It("gets an rsa secret", func() { @@ -119,7 +119,7 @@ var _ = Describe("Get", func() { session := runCommand("get", "-n", "foo-rsa-key") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyRSAFoo)) + Eventually(session.Out).Should(Say(responseMyRSAFooYaml)) }) It("can output json", func() { @@ -157,7 +157,7 @@ var _ = Describe("Get", func() { session := runCommand("get", "-n", "my-username-credential") Eventually(session).Should(Exit(0)) - Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsername)) + Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsernameYaml)) }) It("gets a secret by ID", func() { @@ -173,7 +173,7 @@ var _ = Describe("Get", func() { session := runCommand("get", "--id", UUID) Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyPasswordPotatoes)) + Eventually(session.Out).Should(Say(responseMyPasswordPotatoesYaml)) }) It("does not use Printf on user-supplied data", func() { diff --git a/commands/set.go b/commands/set.go index c4b2e492..aa2e14c9 100644 --- a/commands/set.go +++ b/commands/set.go @@ -33,6 +33,7 @@ type SetCommand struct { PublicString string `short:"U" long:"public-string" description:"[SSH, RSA] Sets the public key from string input"` Username string `short:"z" long:"username" description:"[User] Sets the username value of the credential"` Password string `short:"w" long:"password" description:"[Password, User] Sets the password value of the credential"` + OutputJson bool `long:"output-json" description:"Return response in JSON format"` } func (cmd SetCommand) Execute([]string) error { @@ -62,7 +63,7 @@ func (cmd SetCommand) Execute([]string) error { if err != nil { return err } - models.Println(credential, false) + models.Println(credential, cmd.OutputJson) return nil } diff --git a/commands/set_test.go b/commands/set_test.go index 7782c66a..2e8b7543 100644 --- a/commands/set_test.go +++ b/commands/set_test.go @@ -42,7 +42,7 @@ var _ = Describe("Set", func() { session := runCommand("set", "-n", "my-value", "-v", "potatoes", "-t", "value") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyValuePotatoes)) + Eventually(session.Out).Should(Say(responseMyValuePotatoesYaml)) }) It("escapes special characters in the value", func() { @@ -53,6 +53,15 @@ var _ = Describe("Set", func() { Eventually(session).Should(Exit(0)) Eventually(session.Out).Should(Say(responseMySpecialCharacterValue)) }) + + It("puts a secret using explicit value type and returns in json format", func() { + setupPutValueServer("my-value", "value", "potatoes") + + session := runCommand("set", "-n", "my-value", "-v", "potatoes", "-t", "value", "--output-json") + + Eventually(session).Should(Exit(0)) + Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMyValuePotatoesJson)) + }) }) Describe("setting json secrets", func() { @@ -63,7 +72,7 @@ var _ = Describe("Set", func() { session := runCommand("set", "-n", "json-secret", "-v", jsonValue, "-t", "json") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyJson)) + Eventually(session.Out).Should(Say(responseMyJsonFormatYaml)) }) It("escapes special characters in the json", func() { @@ -74,6 +83,17 @@ var _ = Describe("Set", func() { Eventually(session).Should(Exit(0)) Eventually(session.Out).Should(Say(responseMySpecialCharacterJson)) }) + + It("puts a secret using explicit json type and returns in json format", func() { + jsonValue := `{"foo":"bar","nested":{"a":1},"an":["array"]}` + setupPutJsonServer("json-secret", jsonValue) + + session := runCommand("set", "-n", "json-secret", "-v", jsonValue, "-t", "json", "--output-json") + + Eventually(session).Should(Exit(0)) + Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMyJsonFormatJson)) + }) + }) Describe("setting SSH secrets", func() { @@ -83,7 +103,7 @@ var _ = Describe("Set", func() { session := runCommand("set", "-n", "foo-ssh-key", "-U", "some-public-key", "-P", "some-private-key", "-t", "ssh") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMySSHFoo)) + Eventually(session.Out).Should(Say(responseMySSHFooYaml)) }) It("puts a secret using values read from files", func() { @@ -100,7 +120,7 @@ var _ = Describe("Set", func() { os.RemoveAll(tempDir) Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMySSHFoo)) + Eventually(session.Out).Should(Say(responseMySSHFooYaml)) }) It("puts a secret specifying no-overwrite", func() { @@ -110,6 +130,15 @@ var _ = Describe("Set", func() { Eventually(session).Should(Exit(0)) }) + + It("puts a secret using explicit ssh type and returns in json format", func() { + setupPutRsaSshServer("foo-ssh-key", "ssh", "some-public-key", "some-private-key", true) + + session := runCommand("set", "-n", "foo-ssh-key", "-U", "some-public-key", "-P", "some-private-key", "-t", "ssh", "--output-json") + + Eventually(session).Should(Exit(0)) + Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMySSHFooJson)) + }) }) Describe("setting RSA secrets", func() { @@ -119,7 +148,7 @@ var _ = Describe("Set", func() { session := runCommand("set", "-n", "foo-rsa-key", "-U", "some-public-key", "-P", "some-private-key", "-t", "rsa") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyRSAFoo)) + Eventually(session.Out).Should(Say(responseMyRSAFooYaml)) }) It("puts a secret using values read from files", func() { @@ -136,7 +165,7 @@ var _ = Describe("Set", func() { os.RemoveAll(tempDir) Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyRSAFoo)) + Eventually(session.Out).Should(Say(responseMyRSAFooYaml)) }) It("puts a secret specifying no-overwrite", func() { @@ -146,6 +175,15 @@ var _ = Describe("Set", func() { Eventually(session).Should(Exit(0)) }) + + It("puts a secret using explicit rsa type and returns in json format", func() { + setupPutRsaSshServer("foo-rsa-key", "rsa", "some-public-key", "some-private-key", true) + + session := runCommand("set", "-n", "foo-rsa-key", "-U", "some-public-key", "-P", "some-private-key", "-t", "rsa", "--output-json") + + Eventually(session).Should(Exit(0)) + Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMyRSAFooJson)) + }) }) Describe("setting password secrets", func() { @@ -157,13 +195,13 @@ var _ = Describe("Set", func() { Eventually(session).Should(Exit(0)) }) - It("puts a secret using explicit password type", func() { + It("puts a secret using explicit password type and returns in yaml format", func() { setupPutValueServer("my-password", "password", "potatoes") session := runCommand("set", "-n", "my-password", "-w", "potatoes", "-t", "password") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyPasswordPotatoes)) + Eventually(session.Out).Should(Say(responseMyPasswordPotatoesYaml)) }) It("prompts for value if value is not provided", func() { @@ -172,7 +210,7 @@ var _ = Describe("Set", func() { session := runCommandWithStdin(strings.NewReader("potatoes\n"), "set", "-n", "my-password", "-t", "password") Eventually(session.Out).Should(Say("password:")) - Eventually(session.Wait("10s").Out).Should(Say(responseMyPasswordPotatoes)) + Eventually(session.Wait("10s").Out).Should(Say(responseMyPasswordPotatoesYaml)) Eventually(session).Should(Exit(0)) }) @@ -196,6 +234,15 @@ var _ = Describe("Set", func() { Eventually(session).Should(Exit(0)) Eventually(session.Out).Should(Say(responseMySpecialCharacterPassword)) }) + + It("puts a secret using explicit password type and returns in json format", func() { + setupPutValueServer("my-password", "password", "potatoes") + + session := runCommand("set", "-n", "my-password", "-w", "potatoes", "-t", "password", "--output-json") + + Eventually(session).Should(Exit(0)) + Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMyPasswordPotatoesJson)) + }) }) Describe("setting certificate secrets", func() { @@ -207,7 +254,7 @@ var _ = Describe("Set", func() { "--certificate-string", "my-cert", "--private-string", "my-priv") Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyCertificate)) + Eventually(session.Out).Should(Say(responseMyCertificateYaml)) }) It("puts a secret using explicit certificate type and string values with no-overwrite", func() { @@ -233,7 +280,7 @@ var _ = Describe("Set", func() { os.RemoveAll(tempDir) Eventually(session).Should(Exit(0)) - Eventually(session.Out).Should(Say(responseMyCertificate)) + Eventually(session.Out).Should(Say(responseMyCertificateYaml)) }) It("fails to put a secret when failing to read from a file", func() { @@ -247,6 +294,17 @@ var _ = Describe("Set", func() { testSetCertFileDuplicationFailure("--certificate-string", "my-cert") testSetCertFileDuplicationFailure("--private-string", "my-priv") }) + + It("puts a secret using explicit certificate type and string values in json format", func() { + setupPutCertificateServer("my-secret", "my-ca", "my-cert", "my-priv") + + session := runCommand("set", "-n", "my-secret", + "-t", "certificate", "--root-string", "my-ca", + "--certificate-string", "my-cert", "--private-string", "my-priv", "--output-json") + + Eventually(session).Should(Exit(0)) + Eventually(string(session.Out.Contents())).Should(MatchJSON(responseMyCertificateJson)) + }) }) Describe("setting User secrets", func() { @@ -256,7 +314,7 @@ var _ = Describe("Set", func() { session := runCommand("set", "-n", "my-username-credential", "-z", "my-username", "-w", "test-password", "-t", "user") Eventually(session).Should(Exit(0)) - Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsername)) + Expect(session.Out.Contents()).To(ContainSubstring(responseMyUsernameYaml)) }) It("puts a secret specifying no-overwrite", func() { @@ -290,6 +348,16 @@ var _ = Describe("Set", func() { Eventually(session.Wait("10s").Out.Contents()).Should(ContainSubstring(response)) Eventually(session).Should(Exit(0)) }) + + It("puts a secret using explicit user type in json format", func() { + setupPutUserServer("my-username-credential", `{"username": "my-username", "password": "test-password"}`, "my-username", "test-password", "passw0rd-H4$h", true) + + session := runCommand("set", "-n", "my-username-credential", "-z", "my-username", "-w", "test-password", "-t", "user", + "--output-json") + + Eventually(session).Should(Exit(0)) + Expect(string(session.Out.Contents())).Should(MatchJSON(responseMyUsernameJson)) + }) }) Describe("Help", func() {