Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from semaphoreci/create-secrets-with-env-vars
Pass list of env vars when creating secrets
- Loading branch information
Showing
5 changed files
with
150 additions
and
113 deletions.
There are no files selected for viewing
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
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
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,92 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
client "github.com/semaphoreci/cli/api/client" | ||
models "github.com/semaphoreci/cli/api/models" | ||
"github.com/semaphoreci/cli/cmd/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCreateSecretCmd() *cobra.Command { | ||
cmd := &cobra.Command{} | ||
|
||
cmd.Use = "secret [NAME]" | ||
cmd.Short = "Create a secret." | ||
cmd.Long = `` | ||
cmd.Aliases = []string{"secrets"} | ||
cmd.Args = cobra.ExactArgs(1) | ||
|
||
cmd.Flags().StringArrayP( | ||
"file", | ||
"f", | ||
[]string{}, | ||
"File mapping <local-path>:<mount-path>, used to create a secret with file", | ||
) | ||
|
||
cmd.Flags().StringArrayP( | ||
"env", | ||
"e", | ||
[]string{}, | ||
"Environment Variables", | ||
) | ||
|
||
cmd.Run = func(cmd *cobra.Command, args []string) { | ||
name := args[0] | ||
|
||
fileFlags, err := cmd.Flags().GetStringArray("file") | ||
utils.Check(err) | ||
|
||
var files []models.SecretV1BetaFile | ||
for _, fileFlag := range fileFlags { | ||
matchFormat, err := regexp.MatchString(`^[^: ]+:[^: ]+$`, fileFlag) | ||
utils.Check(err) | ||
|
||
if !matchFormat { | ||
utils.Fail("The format of --file flag must be: <local-path>:<semaphore-path>") | ||
} | ||
|
||
flagPaths := strings.Split(fileFlag, ":") | ||
|
||
file := models.SecretV1BetaFile{} | ||
file.Path = flagPaths[1] | ||
file.Content = encodeFromFileAt(flagPaths[0]) | ||
files = append(files, file) | ||
} | ||
|
||
envFlags, err := cmd.Flags().GetStringArray("env") | ||
utils.Check(err) | ||
|
||
var envVars []models.SecretV1BetaEnvVar | ||
for _, envFlag := range envFlags { | ||
matchFormat, err := regexp.MatchString(`^.+=.+$`, envFlag) | ||
utils.Check(err) | ||
|
||
if !matchFormat { | ||
utils.Fail("The format of -e flag must be: <NAME>=<VALUE>") | ||
} | ||
|
||
parts := strings.SplitN(envFlag, "=", 2) | ||
|
||
envVars = append(envVars, models.SecretV1BetaEnvVar{ | ||
Name: parts[0], | ||
Value: parts[1], | ||
}) | ||
} | ||
|
||
secret := models.NewSecretV1Beta(name, envVars, files) | ||
|
||
c := client.NewSecretV1BetaApi() | ||
|
||
_, err = c.CreateSecret(&secret) | ||
|
||
utils.Check(err) | ||
|
||
fmt.Printf("Secret '%s' created.\n", secret.Metadata.Name) | ||
} | ||
|
||
return cmd | ||
} |
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,54 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"testing" | ||
|
||
assert "github.com/stretchr/testify/assert" | ||
httpmock "gopkg.in/jarcoal/httpmock.v1" | ||
) | ||
|
||
func Test__CreateSecret__WithSubcommand__Response200(t *testing.T) { | ||
httpmock.Activate() | ||
defer httpmock.DeactivateAndReset() | ||
|
||
content1 := "This is some docker config" | ||
content2 := "This is some gcloud config" | ||
|
||
ioutil.WriteFile("/tmp/docker", []byte(content1), 0644) | ||
ioutil.WriteFile("/tmp/gcloud", []byte(content2), 0644) | ||
|
||
received := "" | ||
|
||
httpmock.RegisterResponder("POST", "https://org.semaphoretext.xyz/api/v1beta/secrets", | ||
func(req *http.Request) (*http.Response, error) { | ||
body, _ := ioutil.ReadAll(req.Body) | ||
|
||
received = string(body) | ||
|
||
return httpmock.NewStringResponse(200, received), nil | ||
}, | ||
) | ||
|
||
RootCmd.SetArgs([]string{ | ||
"create", | ||
"secret", | ||
"-e", "FOO=BAR", | ||
"-e", "ZEZ=Hello World", | ||
"--file", "/tmp/docker:.config/docker", | ||
"--file", "/tmp/gcloud:.config/gcloud", | ||
"abc", | ||
}) | ||
|
||
RootCmd.Execute() | ||
|
||
file1 := base64.StdEncoding.EncodeToString([]byte(content1)) | ||
file2 := base64.StdEncoding.EncodeToString([]byte(content2)) | ||
|
||
expected := fmt.Sprintf(`{"apiVersion":"v1beta","kind":"Secret","metadata":{"name":"abc"},"data":{"env_vars":[{"name":"FOO","value":"BAR"},{"name":"ZEZ","value":"Hello World"}],"files":[{"path":".config/docker","content":"%s"},{"path":".config/gcloud","content":"%s"}]}}`, file1, file2) | ||
|
||
assert.Equal(t, received, expected) | ||
} |
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