From be74f4d34c4bd2c4d02b493ff9b0a1e40da4018c Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:27:50 +0200 Subject: [PATCH 01/45] Fix: Add import & recursive support to raw fetching --- cli/packages/api/api.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/cli/packages/api/api.go b/cli/packages/api/api.go index d45a42db4..01f29a03a 100644 --- a/cli/packages/api/api.go +++ b/cli/packages/api/api.go @@ -512,16 +512,23 @@ func CallUniversalAuthRefreshAccessToken(httpClient *resty.Client, request Unive func CallGetRawSecretsV3(httpClient *resty.Client, request GetRawSecretsV3Request) (GetRawSecretsV3Response, error) { var getRawSecretsV3Response GetRawSecretsV3Response - response, err := httpClient. + req := httpClient. R(). SetResult(&getRawSecretsV3Response). SetHeader("User-Agent", USER_AGENT). SetBody(request). SetQueryParam("workspaceId", request.WorkspaceId). SetQueryParam("environment", request.Environment). - SetQueryParam("secretPath", request.SecretPath). - SetQueryParam("include_imports", "false"). - Get(fmt.Sprintf("%v/v3/secrets/raw", config.INFISICAL_URL)) + SetQueryParam("secretPath", request.SecretPath) + + if request.IncludeImport { + req.SetQueryParam("include_imports", "true") + } + if request.Recursive { + req.SetQueryParam("recursive", "true") + } + + response, err := req.Get(fmt.Sprintf("%v/v3/secrets/raw", config.INFISICAL_URL)) if err != nil { return GetRawSecretsV3Response{}, fmt.Errorf("CallGetRawSecretsV3: Unable to complete api request [err=%w]", err) From 7581b33b3bdb4bad5465f331a58fc7898c2e2cc6 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:28:01 +0200 Subject: [PATCH 02/45] Fix: Add import support for raw fetching --- cli/packages/api/model.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cli/packages/api/model.go b/cli/packages/api/model.go index 0a5bfee6d..56b9807f7 100644 --- a/cli/packages/api/model.go +++ b/cli/packages/api/model.go @@ -371,6 +371,22 @@ type ImportedSecretV3 struct { Secrets []EncryptedSecretV3 `json:"secrets"` } +type ImportedRawSecretV3 struct { + SecretPath string `json:"secretPath"` + Environment string `json:"environment"` + FolderId string `json:"folderId"` + Secrets []struct { + ID string `json:"id"` + Workspace string `json:"workspace"` + Environment string `json:"environment"` + Version int `json:"version"` + Type string `json:"type"` + SecretKey string `json:"secretKey"` + SecretValue string `json:"secretValue"` + SecretComment string `json:"secretComment"` + } `json:"secrets"` +} + type GetEncryptedSecretsV3Response struct { Secrets []EncryptedSecretV3 `json:"secrets"` ImportedSecrets []ImportedSecretV3 `json:"imports,omitempty"` @@ -542,6 +558,6 @@ type GetRawSecretsV3Response struct { SecretValue string `json:"secretValue"` SecretComment string `json:"secretComment"` } `json:"secrets"` - Imports []any `json:"imports"` + Imports []ImportedRawSecretV3 `json:"imports"` ETag string } From 6b473d2b361a7d4b99e0097f8cf5d85876973724 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:28:13 +0200 Subject: [PATCH 03/45] Feat: Integration tests --- cli/packages/cmd/export.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cli/packages/cmd/export.go b/cli/packages/cmd/export.go index 4e08e5fa8..26c7d0044 100644 --- a/cli/packages/cmd/export.go +++ b/cli/packages/cmd/export.go @@ -117,7 +117,10 @@ var exportCmd = &cobra.Command{ secrets = util.ExpandSecrets(secrets, authParams, "") } + secrets = util.FilterSecretsByTag(secrets, tagSlugs) + util.HandleSendTestSecrets(cmd, secrets) + output, err = formatEnvs(secrets, format) if err != nil { util.HandleError(err) From 7ab8db047112af9da935a894ad46f34ef8ca2edc Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:28:18 +0200 Subject: [PATCH 04/45] Feat: Integration tests --- cli/packages/cmd/root.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cli/packages/cmd/root.go b/cli/packages/cmd/root.go index 06846260f..7fe172c07 100644 --- a/cli/packages/cmd/root.go +++ b/cli/packages/cmd/root.go @@ -87,3 +87,7 @@ func initLog() { zerolog.SetGlobalLevel(zerolog.InfoLevel) } } + +func NewRootCmd() *cobra.Command { + return rootCmd +} From 8bf09789d6626404529385841bcf0231c17a85c9 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:29:05 +0200 Subject: [PATCH 05/45] Feat: Integration tests --- cli/packages/cmd/secrets.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index 7ba6e5411..c3228587e 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -39,6 +39,7 @@ var secretsCmd = &cobra.Command{ } token, err := util.GetInfisicalToken(cmd) + if err != nil { util.HandleError(err, "Unable to parse flag") } @@ -116,6 +117,7 @@ var secretsCmd = &cobra.Command{ secrets = util.ExpandSecrets(secrets, authParams, "") } + util.HandleSendTestSecrets(cmd, secrets) visualize.PrintAllSecretDetails(secrets) Telemetry.CaptureEvent("cli-command:secrets", posthog.NewProperties().Set("secretCount", len(secrets)).Set("version", util.CLI_VERSION)) }, @@ -505,6 +507,8 @@ func getSecretsByNames(cmd *cobra.Command, args []string) { } } + util.HandleSendTestSecrets(cmd, requestedSecrets) + if showOnlyValue && len(requestedSecrets) > 1 { util.PrintErrorMessageAndExit("--raw-value only works with one secret.") } From 12d5fb1043ab7e7cb6a72bf18aec33e45e34f5c7 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:29:18 +0200 Subject: [PATCH 06/45] Fix: Add support for imported secrets with raw fetching --- cli/packages/util/secrets.go | 42 ++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/cli/packages/util/secrets.go b/cli/packages/util/secrets.go index 59d80ea77..bedd77572 100644 --- a/cli/packages/util/secrets.go +++ b/cli/packages/util/secrets.go @@ -186,12 +186,12 @@ func GetPlainTextSecretsViaMachineIdentity(accessToken string, workspaceId strin plainTextSecrets = append(plainTextSecrets, models.SingleEnvironmentVariable{Key: secret.SecretKey, Value: secret.SecretValue, Type: secret.Type, WorkspaceId: secret.Workspace}) } - // if includeImports { - // plainTextSecrets, err = InjectImportedSecret(plainTextWorkspaceKey, plainTextSecrets, encryptedSecrets.ImportedSecrets) - // if err != nil { - // return nil, err - // } - // } + if includeImports { + plainTextSecrets, err = InjectRawImportedSecret(plainTextSecrets, rawSecrets.Imports) + if err != nil { + return models.PlaintextSecretResult{}, err + } + } return models.PlaintextSecretResult{ Secrets: plainTextSecrets, @@ -252,6 +252,36 @@ func InjectImportedSecret(plainTextWorkspaceKey []byte, secrets []models.SingleE return secrets, nil } +func InjectRawImportedSecret(secrets []models.SingleEnvironmentVariable, importedSecrets []api.ImportedRawSecretV3) ([]models.SingleEnvironmentVariable, error) { + if importedSecrets == nil { + return secrets, nil + } + + hasOverriden := make(map[string]bool) + for _, sec := range secrets { + hasOverriden[sec.Key] = true + } + + for i := len(importedSecrets) - 1; i >= 0; i-- { + importSec := importedSecrets[i] + plainTextImportedSecrets := importSec.Secrets + + for _, sec := range plainTextImportedSecrets { + if _, ok := hasOverriden[sec.SecretKey]; !ok { + secrets = append(secrets, models.SingleEnvironmentVariable{ + Key: sec.SecretKey, + WorkspaceId: sec.Workspace, + Value: sec.SecretValue, + Type: sec.Type, + ID: sec.ID, + }) + hasOverriden[sec.SecretKey] = true + } + } + } + return secrets, nil +} + func FilterSecretsByTag(plainTextSecrets []models.SingleEnvironmentVariable, tagSlugs string) []models.SingleEnvironmentVariable { if tagSlugs == "" { return plainTextSecrets From bcb3eaab74cd3363603bf0809ff28b24f3a7c1c1 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:29:31 +0200 Subject: [PATCH 07/45] Feat: Integration tests --- cli/packages/util/tests.go | 26 +++++++++ cli/test/export_test.go | 82 ++++++++++++++++++++++++++++ cli/test/helper.go | 26 +++++++++ cli/test/root_test.go | 107 +++++++++++++++++++++++++++++++++++++ cli/test/secrets_test.go | 93 ++++++++++++++++++++++++++++++++ 5 files changed, 334 insertions(+) create mode 100644 cli/packages/util/tests.go create mode 100644 cli/test/export_test.go create mode 100644 cli/test/helper.go create mode 100644 cli/test/root_test.go create mode 100644 cli/test/secrets_test.go diff --git a/cli/packages/util/tests.go b/cli/packages/util/tests.go new file mode 100644 index 000000000..816df573e --- /dev/null +++ b/cli/packages/util/tests.go @@ -0,0 +1,26 @@ +package util + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/Infisical/infisical-merge/packages/models" + "github.com/spf13/cobra" +) + +func HandleSendTestSecrets(cmd *cobra.Command, secrets []models.SingleEnvironmentVariable) { + isTestMode := os.Getenv("TEST_MODE") + + if isTestMode != "true" { + return + } + + jsonOut, err := json.Marshal(secrets) + if err != nil { + HandleError(err, "Unable to marshal secrets") + } + + fmt.Fprint(cmd.OutOrStdout(), string(jsonOut)) + +} diff --git a/cli/test/export_test.go b/cli/test/export_test.go new file mode 100644 index 000000000..8c4b49ef1 --- /dev/null +++ b/cli/test/export_test.go @@ -0,0 +1,82 @@ +package tests + +import ( + "bytes" + "encoding/json" + "fmt" + "testing" + + "github.com/Infisical/infisical-merge/packages/cmd" + "github.com/Infisical/infisical-merge/packages/models" + "github.com/stretchr/testify/assert" +) + +func ExportSecrets(t *testing.T, authToken string, projectId string, envSlug string) { + + rootCommand := cmd.NewRootCmd() + + commandOutput := new(bytes.Buffer) + rootCommand.SetOut(commandOutput) + rootCommand.SetErr(commandOutput) + + args := []string{ + "export", + } + + args = append(args, fmt.Sprintf("--token=%s", authToken)) + args = append(args, fmt.Sprintf("--projectId=%s", projectId)) + + rootCommand.SetArgs(args) + rootCommand.Execute() + + var secrets []models.SingleEnvironmentVariable + + json.Unmarshal(commandOutput.Bytes(), &secrets) + + expectedLength := len(ALL_SECRETS) - 1 // -1 because the default path is "/", and the secret in /folder will not be found. + + assert.Len(t, secrets, expectedLength) + + for _, secret := range secrets { + if secret.Key == "FOLDER-SECRET-1" { + continue + } + assert.Contains(t, ALL_SECRET_KEYS, secret.Key) + assert.Contains(t, ALL_SECRET_VALUES, secret.Value) + } + +} + +func ExportSecretsWithoutImports(t *testing.T, authToken string, projectId string, envSlug string) { + + rootCommand := cmd.NewRootCmd() + + commandOutput := new(bytes.Buffer) + rootCommand.SetOut(commandOutput) + rootCommand.SetErr(commandOutput) + + args := []string{ + "export", + } + + args = append(args, fmt.Sprintf("--token=%s", authToken)) + args = append(args, fmt.Sprintf("--projectId=%s", projectId)) + args = append(args, "--include-imports=false") + + rootCommand.SetArgs(args) + rootCommand.Execute() + + var secrets []models.SingleEnvironmentVariable + + json.Unmarshal(commandOutput.Bytes(), &secrets) + + assert.Len(t, secrets, len(DEV_SECRETS)) + + allDevSecretKeys, allDevSecretValues := getSecretKeysAndValues(DEV_SECRETS) + + for _, secret := range secrets { + assert.Contains(t, allDevSecretKeys, secret.Key) + assert.Contains(t, allDevSecretValues, secret.Value) + } + +} diff --git a/cli/test/helper.go b/cli/test/helper.go new file mode 100644 index 000000000..912a3a71d --- /dev/null +++ b/cli/test/helper.go @@ -0,0 +1,26 @@ +package tests + +type Secret struct { + Key string + Value string +} + +func Map[T, U any](ts []T, f func(T) U) []U { + us := make([]U, len(ts)) + for i := range ts { + us[i] = f(ts[i]) + } + return us +} + +func getSecretKeysAndValues(secrets []Secret) (keys []string, values []string) { + secretKeys := []string{} + secretValues := []string{} + + for _, secret := range secrets { + secretKeys = append(secretKeys, secret.Key) + secretValues = append(secretValues, secret.Value) + } + + return secretKeys, secretValues +} diff --git a/cli/test/root_test.go b/cli/test/root_test.go new file mode 100644 index 000000000..2fbb788fe --- /dev/null +++ b/cli/test/root_test.go @@ -0,0 +1,107 @@ +package tests + +import ( + "os" + "testing" + + "github.com/Infisical/infisical-merge/packages/util" +) + +var DEV_SECRETS = []Secret{ + { + Key: "TEST-SECRET-1", + Value: "test-value-1", + }, + { + Key: "TEST-SECRET-2", + Value: "test-value-2", + }, + { + Key: "TEST-SECRET-3", + Value: "test-value-3", + }, +} + +var DEV_FOLDER_SECRETS = []Secret{ + { + Key: "FOLDER-SECRET-1", + Value: "folder-value-1", + }, +} + +var STAGING_SECRETS = []Secret{ + { + Key: "STAGING-SECRET-1", + Value: "staging-value-1", + }, + { + Key: "STAGING-SECRET-2", + Value: "staging-value-2", + }, +} + +// Initialize the combined secrets array +var ALL_SECRETS = []Secret{} +var ALL_SECRET_KEYS = []string{} +var ALL_SECRET_VALUES = []string{} + +type Credentials struct { + ClientID string + ClientSecret string + ServiceToken string + ProjectID string + EnvSlug string +} + +var creds = Credentials{ + ClientID: os.Getenv("CLI_TESTS_UA_CLIENT_ID"), + ClientSecret: os.Getenv("CLI_TESTS_UA_CLIENT_SECRET"), + ServiceToken: os.Getenv("CLI_TESTS_SERVICE_TOKEN"), + ProjectID: os.Getenv("CLI_TESTS_PROJECT_ID"), + EnvSlug: os.Getenv("CLI_TESTS_ENV_SLUG"), +} + +func initialize() { + if creds.ClientID == "" || creds.ClientSecret == "" || creds.ServiceToken == "" || creds.ProjectID == "" || creds.EnvSlug == "" { + panic("Missing required environment variables") + } + + ALL_SECRETS = append(ALL_SECRETS, DEV_SECRETS...) + ALL_SECRETS = append(ALL_SECRETS, DEV_FOLDER_SECRETS...) + ALL_SECRETS = append(ALL_SECRETS, STAGING_SECRETS...) + + for _, secret := range ALL_SECRETS { + ALL_SECRET_KEYS = append(ALL_SECRET_KEYS, secret.Key) + ALL_SECRET_VALUES = append(ALL_SECRET_VALUES, secret.Value) + } +} + +func Test_RunTests(t *testing.T) { + initialize() + + res, err := util.UniversalAuthLogin(creds.ClientID, creds.ClientSecret) + if err != nil { + t.Errorf("Error: %v", err) + } + universalAuthAccessToken := res.AccessToken + + t.Run("Export secrets", func(t *testing.T) { + ExportSecrets(t, universalAuthAccessToken, creds.ProjectID, creds.EnvSlug) + ExportSecrets(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) + }) + + t.Run("Export secrets (without imports)", func(t *testing.T) { + ExportSecretsWithoutImports(t, universalAuthAccessToken, creds.ProjectID, creds.EnvSlug) + ExportSecretsWithoutImports(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) + }) + + t.Run("List Secrets (with imports and recursive)", func(t *testing.T) { + ListSecretsWithImportsAndRecursive(t, universalAuthAccessToken, creds.ProjectID, creds.EnvSlug) + ListSecretsWithImportsAndRecursive(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) + }) + + t.Run("Get Secrets by Names", func(t *testing.T) { + GetSecretsByNames(t, universalAuthAccessToken, creds.ProjectID, creds.EnvSlug) + GetSecretsByNames(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) + }) +} diff --git a/cli/test/secrets_test.go b/cli/test/secrets_test.go new file mode 100644 index 000000000..b346a3e69 --- /dev/null +++ b/cli/test/secrets_test.go @@ -0,0 +1,93 @@ +package tests + +import ( + "bytes" + "encoding/json" + "fmt" + "testing" + + "github.com/Infisical/infisical-merge/packages/cmd" + "github.com/Infisical/infisical-merge/packages/models" + "github.com/stretchr/testify/assert" +) + +func ListSecretsWithImportsAndRecursive(t *testing.T, authToken string, projectId string, envSlug string) { + + rootCommand := cmd.NewRootCmd() + + commandOutput := new(bytes.Buffer) + rootCommand.SetOut(commandOutput) + rootCommand.SetErr(commandOutput) + + args := []string{ + "secrets", + } + args = append(args, fmt.Sprintf("--token=%s", authToken)) + args = append(args, fmt.Sprintf("--projectId=%s", projectId)) + args = append(args, fmt.Sprintf("--env=%s", envSlug)) + args = append(args, "--include-imports=true") + args = append(args, "--recursive=true") + + rootCommand.SetArgs(args) + rootCommand.Execute() + + var secrets []models.SingleEnvironmentVariable + + json.Unmarshal(commandOutput.Bytes(), &secrets) + + if len(secrets) == 0 { + t.Errorf("No secrets found") + } + + secretKeys := []string{} + secretValues := []string{} + + for _, secret := range secrets { + secretKeys = append(secretKeys, secret.Key) + secretValues = append(secretValues, secret.Value) + } + + // Secrets can have different order and potentially more secrets. but the secrets should at least contain the above secrets. + for _, key := range ALL_SECRET_KEYS { + assert.Contains(t, secretKeys, key) + } + for _, value := range ALL_SECRET_VALUES { + assert.Contains(t, secretValues, value) + } +} + +func GetSecretsByNames(t *testing.T, authToken string, projectId string, envSlug string) { + + rootCommand := cmd.NewRootCmd() + + commandOutput := new(bytes.Buffer) + rootCommand.SetOut(commandOutput) + rootCommand.SetErr(commandOutput) + + args := []string{ + "secrets", + "get", + } + + args = append(args, ALL_SECRET_KEYS...) + args = append(args, fmt.Sprintf("--token=%s", authToken)) + args = append(args, fmt.Sprintf("--projectId=%s", projectId)) + args = append(args, fmt.Sprintf("--env=%s", envSlug)) + + rootCommand.SetArgs(args) + rootCommand.Execute() + + var secrets []models.SingleEnvironmentVariable + + json.Unmarshal(commandOutput.Bytes(), &secrets) + + assert.Len(t, secrets, len(ALL_SECRETS)) + + for _, secret := range secrets { + assert.Contains(t, ALL_SECRET_KEYS, secret.Key) + + if secret.Key == "FOLDER-SECRET-1" { + assert.Equal(t, secret.Value, "*not found*") // Should not be found because recursive isn't enabled in this test, and the default path is "/" + } + } +} From cb6cbafcaedc4da9ddcd7384908ab42bf5d11286 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Wed, 17 Apr 2024 15:17:08 +0200 Subject: [PATCH 08/45] Fix: JSON error check --- cli/test/export_test.go | 10 ++++++++-- cli/test/secrets_test.go | 10 ++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cli/test/export_test.go b/cli/test/export_test.go index 8c4b49ef1..dd156fc39 100644 --- a/cli/test/export_test.go +++ b/cli/test/export_test.go @@ -31,7 +31,10 @@ func ExportSecrets(t *testing.T, authToken string, projectId string, envSlug str var secrets []models.SingleEnvironmentVariable - json.Unmarshal(commandOutput.Bytes(), &secrets) + err := json.Unmarshal(commandOutput.Bytes(), &secrets) + if err != nil { + t.Errorf("Error: %v", err) + } expectedLength := len(ALL_SECRETS) - 1 // -1 because the default path is "/", and the secret in /folder will not be found. @@ -68,7 +71,10 @@ func ExportSecretsWithoutImports(t *testing.T, authToken string, projectId strin var secrets []models.SingleEnvironmentVariable - json.Unmarshal(commandOutput.Bytes(), &secrets) + err := json.Unmarshal(commandOutput.Bytes(), &secrets) + if err != nil { + t.Errorf("Error: %v", err) + } assert.Len(t, secrets, len(DEV_SECRETS)) diff --git a/cli/test/secrets_test.go b/cli/test/secrets_test.go index b346a3e69..39bfd1c07 100644 --- a/cli/test/secrets_test.go +++ b/cli/test/secrets_test.go @@ -33,7 +33,10 @@ func ListSecretsWithImportsAndRecursive(t *testing.T, authToken string, projectI var secrets []models.SingleEnvironmentVariable - json.Unmarshal(commandOutput.Bytes(), &secrets) + err := json.Unmarshal(commandOutput.Bytes(), &secrets) + if err != nil { + t.Errorf("Error: %v", err) + } if len(secrets) == 0 { t.Errorf("No secrets found") @@ -79,7 +82,10 @@ func GetSecretsByNames(t *testing.T, authToken string, projectId string, envSlug var secrets []models.SingleEnvironmentVariable - json.Unmarshal(commandOutput.Bytes(), &secrets) + err := json.Unmarshal(commandOutput.Bytes(), &secrets) + if err != nil { + t.Errorf("Error: %v", err) + } assert.Len(t, secrets, len(ALL_SECRETS)) From d31d98b5e083d449b7f62f11ee8781e862c0ad0b Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:58:59 +0200 Subject: [PATCH 09/45] Feat: CLI Integration tests --- cli/packages/cmd/login.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cli/packages/cmd/login.go b/cli/packages/cmd/login.go index bbb2c3a05..a6401e3d0 100644 --- a/cli/packages/cmd/login.go +++ b/cli/packages/cmd/login.go @@ -191,6 +191,10 @@ var loginCmd = &cobra.Command{ util.HandleError(err) } + if util.IS_TEST_MODE { + util.HandleSendUniversalAuthToken(cmd, res.AccessToken) + return + } if plainOutput { fmt.Println(res.AccessToken) return From b79ce8a8807ec6471c2950d2dd6b661999ea63c1 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:59:13 +0200 Subject: [PATCH 10/45] Feat: Cli integration tests -- login --- cli/test/login_test.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 cli/test/login_test.go diff --git a/cli/test/login_test.go b/cli/test/login_test.go new file mode 100644 index 000000000..1d598153a --- /dev/null +++ b/cli/test/login_test.go @@ -0,0 +1,43 @@ +package tests + +import ( + "bytes" + "fmt" + "regexp" + "testing" + + "github.com/Infisical/infisical-merge/packages/cmd" + "github.com/stretchr/testify/assert" +) + +func UALoginCmd(t *testing.T) { + jwtPattern := `^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$` + + rootCommand := cmd.NewRootCmd() + + commandOutput := new(bytes.Buffer) + errorOutput := new(bytes.Buffer) + rootCommand.SetOut(commandOutput) + rootCommand.SetErr(errorOutput) + + args := []string{ + "login", + } + + args = append(args, fmt.Sprintf("--method=%s", "universal-auth")) + args = append(args, fmt.Sprintf("--client-id=%s", creds.ClientID)) + args = append(args, fmt.Sprintf("--client-secret=%s", creds.ClientSecret)) + + rootCommand.SetArgs(args) + rootCommand.Execute() + + token := commandOutput.String() + + // We do a match and compare it against true, instead of using assert.Regexp. + // If the assertion fails, we would be able to see the potential token that was generated in the output console, which would be bad if running in a CI/CD pipeline. + match, err := regexp.MatchString(jwtPattern, token) + assert.Nil(t, err) + assert.True(t, match, "The token does not match the pattern") + + creds.UAAccessToken = token +} From 46a91515b1199837159dd557c02f3883c5ded9f1 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:59:21 +0200 Subject: [PATCH 11/45] Fix: Use login UA token --- cli/test/root_test.go | 50 +++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/cli/test/root_test.go b/cli/test/root_test.go index 2fbb788fe..e5e9d7684 100644 --- a/cli/test/root_test.go +++ b/cli/test/root_test.go @@ -3,8 +3,6 @@ package tests import ( "os" "testing" - - "github.com/Infisical/infisical-merge/packages/util" ) var DEV_SECRETS = []Secret{ @@ -46,19 +44,21 @@ var ALL_SECRET_KEYS = []string{} var ALL_SECRET_VALUES = []string{} type Credentials struct { - ClientID string - ClientSecret string - ServiceToken string - ProjectID string - EnvSlug string + ClientID string + ClientSecret string + UAAccessToken string + ServiceToken string + ProjectID string + EnvSlug string } var creds = Credentials{ - ClientID: os.Getenv("CLI_TESTS_UA_CLIENT_ID"), - ClientSecret: os.Getenv("CLI_TESTS_UA_CLIENT_SECRET"), - ServiceToken: os.Getenv("CLI_TESTS_SERVICE_TOKEN"), - ProjectID: os.Getenv("CLI_TESTS_PROJECT_ID"), - EnvSlug: os.Getenv("CLI_TESTS_ENV_SLUG"), + UAAccessToken: "", + ClientID: os.Getenv("CLI_TESTS_UA_CLIENT_ID"), + ClientSecret: os.Getenv("CLI_TESTS_UA_CLIENT_SECRET"), + ServiceToken: os.Getenv("CLI_TESTS_SERVICE_TOKEN"), + ProjectID: os.Getenv("CLI_TESTS_PROJECT_ID"), + EnvSlug: os.Getenv("CLI_TESTS_ENV_SLUG"), } func initialize() { @@ -79,29 +79,37 @@ func initialize() { func Test_RunTests(t *testing.T) { initialize() - res, err := util.UniversalAuthLogin(creds.ClientID, creds.ClientSecret) - if err != nil { - t.Errorf("Error: %v", err) - } - universalAuthAccessToken := res.AccessToken + t.Run("User login command", func(t *testing.T) { + UALoginCmd(t) + }) + + t.Run("Run command", func(t *testing.T) { + RunCmd(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) + RunCmd(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) + }) + + t.Run("Run Command (without imports, with recursive)", func(t *testing.T) { + RunCmdWithoutImportsAndWithRecursive(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) + RunCmdWithoutImportsAndWithRecursive(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) + }) t.Run("Export secrets", func(t *testing.T) { - ExportSecrets(t, universalAuthAccessToken, creds.ProjectID, creds.EnvSlug) + ExportSecrets(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) ExportSecrets(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) }) t.Run("Export secrets (without imports)", func(t *testing.T) { - ExportSecretsWithoutImports(t, universalAuthAccessToken, creds.ProjectID, creds.EnvSlug) + ExportSecretsWithoutImports(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) ExportSecretsWithoutImports(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) }) t.Run("List Secrets (with imports and recursive)", func(t *testing.T) { - ListSecretsWithImportsAndRecursive(t, universalAuthAccessToken, creds.ProjectID, creds.EnvSlug) + ListSecretsWithImportsAndRecursive(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) ListSecretsWithImportsAndRecursive(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) }) t.Run("Get Secrets by Names", func(t *testing.T) { - GetSecretsByNames(t, universalAuthAccessToken, creds.ProjectID, creds.EnvSlug) + GetSecretsByNames(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) GetSecretsByNames(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) }) } From ad6d18a9056331802783904434b63438481235bc Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:59:26 +0200 Subject: [PATCH 12/45] Feat: Cli integration tests -- run cmd --- cli/test/run_test.go | 137 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 cli/test/run_test.go diff --git a/cli/test/run_test.go b/cli/test/run_test.go new file mode 100644 index 000000000..8ea26e38d --- /dev/null +++ b/cli/test/run_test.go @@ -0,0 +1,137 @@ +package tests + +import ( + "bytes" + "fmt" + "slices" + "strings" + "testing" + + "github.com/Infisical/infisical-merge/packages/cmd" + "github.com/stretchr/testify/assert" +) + +func RunCmd(t *testing.T, authToken string, projectId string, envSlug string) { + + rootCommand := cmd.NewRootCmd() + + commandOutput := new(bytes.Buffer) + errorOutput := new(bytes.Buffer) + rootCommand.SetOut(commandOutput) + rootCommand.SetErr(errorOutput) + + args := []string{ + "run", + } + + args = append(args, fmt.Sprintf("--token=%s", authToken)) + args = append(args, fmt.Sprintf("--projectId=%s", projectId)) + args = append(args, fmt.Sprintf("--env=%s", envSlug)) + args = append(args, "--", "echo", "TEST_COMMAND_BEING_EXECUTED") + + rootCommand.SetArgs(args) + rootCommand.Execute() + + var secrets []Secret + + stringSecrets := commandOutput.String() + arraySecrets := strings.Split(stringSecrets, "\n") + + for idx, secret := range arraySecrets { + if idx == len(arraySecrets)-1 && secret == "" { + continue + } + + secretParts := strings.Split(secret, "=") + + if len(secretParts) != 2 { + t.Errorf("Error: secret at index %d is not formatted correctly", idx) + } + + newSecret := Secret{ + Key: secretParts[0], + Value: secretParts[1], + } + + // make sure the new secret key is at least one of the expected keys + if !slices.Contains(ALL_SECRET_KEYS, newSecret.Key) { + continue + } + + secrets = append(secrets, newSecret) + } + + expectedLength := len(DEV_SECRETS) + len(STAGING_SECRETS) + + assert.Len(t, secrets, expectedLength) + + for _, secret := range secrets { + assert.Contains(t, ALL_SECRET_KEYS, secret.Key) + assert.Contains(t, ALL_SECRET_VALUES, secret.Value) + } +} + +func RunCmdWithoutImportsAndWithRecursive(t *testing.T, authToken string, projectId string, envSlug string) { + + rootCommand := cmd.NewRootCmd() + + commandOutput := new(bytes.Buffer) + errorOutput := new(bytes.Buffer) + rootCommand.SetOut(commandOutput) + rootCommand.SetErr(errorOutput) + + args := []string{ + "run", + } + + args = append(args, fmt.Sprintf("--token=%s", authToken)) + args = append(args, fmt.Sprintf("--projectId=%s", projectId)) + args = append(args, fmt.Sprintf("--env=%s", envSlug)) + args = append(args, "--include-imports=false") + args = append(args, "--recursive") + args = append(args, "--", "echo", "TEST_COMMAND_BEING_EXECUTED_RECURSIVE") + + rootCommand.SetArgs(args) + rootCommand.Execute() + + var secrets []Secret + + stringSecrets := commandOutput.String() + arraySecrets := strings.Split(stringSecrets, "\n") + + for idx, secret := range arraySecrets { + if idx == len(arraySecrets)-1 && secret == "" { + continue + } + + secretParts := strings.Split(secret, "=") + + if len(secretParts) != 2 { + t.Errorf("Error: secret at index %d is not formatted correctly", idx) + } + + newSecret := Secret{ + Key: secretParts[0], + Value: secretParts[1], + } + + // make sure the new secret key is at least one of the expected keys + if !slices.Contains(ALL_SECRET_KEYS, newSecret.Key) { + continue + } + + secrets = append(secrets, newSecret) + } + + nestedDevSecrets := append(DEV_FOLDER_SECRETS, DEV_SECRETS...) + nestedDevSecretsKeys := Map(nestedDevSecrets, func(secret Secret) string { return secret.Key }) + nestedDevSecretsValues := Map(nestedDevSecrets, func(secret Secret) string { return secret.Value }) + + expectedLength := len(nestedDevSecrets) + assert.Len(t, secrets, expectedLength) + + for _, secret := range secrets { + assert.Contains(t, nestedDevSecretsKeys, secret.Key) + assert.Contains(t, nestedDevSecretsValues, secret.Value) + } +} From b598dd3d477ff1d95f8f9eaa079c5a18cbeecb0a Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 14:59:41 +0200 Subject: [PATCH 13/45] Feat: Cli integration tests -- exports --- cli/test/export_test.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/cli/test/export_test.go b/cli/test/export_test.go index dd156fc39..a0f68fa7a 100644 --- a/cli/test/export_test.go +++ b/cli/test/export_test.go @@ -16,8 +16,9 @@ func ExportSecrets(t *testing.T, authToken string, projectId string, envSlug str rootCommand := cmd.NewRootCmd() commandOutput := new(bytes.Buffer) + errorOutput := new(bytes.Buffer) rootCommand.SetOut(commandOutput) - rootCommand.SetErr(commandOutput) + rootCommand.SetErr(errorOutput) args := []string{ "export", @@ -25,6 +26,7 @@ func ExportSecrets(t *testing.T, authToken string, projectId string, envSlug str args = append(args, fmt.Sprintf("--token=%s", authToken)) args = append(args, fmt.Sprintf("--projectId=%s", projectId)) + args = append(args, fmt.Sprintf("--env=%s", envSlug)) rootCommand.SetArgs(args) rootCommand.Execute() @@ -36,7 +38,7 @@ func ExportSecrets(t *testing.T, authToken string, projectId string, envSlug str t.Errorf("Error: %v", err) } - expectedLength := len(ALL_SECRETS) - 1 // -1 because the default path is "/", and the secret in /folder will not be found. + expectedLength := len(DEV_SECRETS) + len(STAGING_SECRETS) assert.Len(t, secrets, expectedLength) @@ -47,7 +49,6 @@ func ExportSecrets(t *testing.T, authToken string, projectId string, envSlug str assert.Contains(t, ALL_SECRET_KEYS, secret.Key) assert.Contains(t, ALL_SECRET_VALUES, secret.Value) } - } func ExportSecretsWithoutImports(t *testing.T, authToken string, projectId string, envSlug string) { @@ -64,6 +65,7 @@ func ExportSecretsWithoutImports(t *testing.T, authToken string, projectId strin args = append(args, fmt.Sprintf("--token=%s", authToken)) args = append(args, fmt.Sprintf("--projectId=%s", projectId)) + args = append(args, fmt.Sprintf("--env=%s", envSlug)) args = append(args, "--include-imports=false") rootCommand.SetArgs(args) From 943945f6d78bcc3ab48a78b6a330e73736e19c2b Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 15:01:28 +0200 Subject: [PATCH 14/45] Feat: Make run testable --- cli/packages/cmd/run.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 04fe2588b..dc123235a 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -174,6 +174,7 @@ var runCmd = &cobra.Command{ } log.Debug().Msgf("injecting the following environment variables into shell: %v", env) + util.HandleSendTestEnvVars(cmd, env) Telemetry.CaptureEvent("cli-command:run", posthog.NewProperties(). @@ -310,7 +311,10 @@ func execCmd(cmd *exec.Cmd) error { return fmt.Errorf("failed to wait for command termination: %v", err) } - waitStatus := cmd.ProcessState.Sys().(syscall.WaitStatus) - os.Exit(waitStatus.ExitStatus()) + if !util.IS_TEST_MODE { + waitStatus := cmd.ProcessState.Sys().(syscall.WaitStatus) + os.Exit(waitStatus.ExitStatus()) + } + return nil } From 22c589e2cf84cee9a99c7e0829e6a7e4445780ea Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 15:01:31 +0200 Subject: [PATCH 15/45] Update tests.go --- cli/packages/util/tests.go | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/cli/packages/util/tests.go b/cli/packages/util/tests.go index 816df573e..989376e58 100644 --- a/cli/packages/util/tests.go +++ b/cli/packages/util/tests.go @@ -9,10 +9,11 @@ import ( "github.com/spf13/cobra" ) -func HandleSendTestSecrets(cmd *cobra.Command, secrets []models.SingleEnvironmentVariable) { - isTestMode := os.Getenv("TEST_MODE") +var IS_TEST_MODE = os.Getenv("TEST_MODE") == "true" - if isTestMode != "true" { +func HandleSendTestSecrets(cmd *cobra.Command, secrets []models.SingleEnvironmentVariable) { + + if !IS_TEST_MODE { return } @@ -22,5 +23,28 @@ func HandleSendTestSecrets(cmd *cobra.Command, secrets []models.SingleEnvironmen } fmt.Fprint(cmd.OutOrStdout(), string(jsonOut)) - +} + +func HandleSendTestEnvVars(cmd *cobra.Command, envs []string) { + + if !IS_TEST_MODE { + return + } + + stringEnvVars := "" + + for _, env := range envs { + stringEnvVars += env + "\n" + } + + fmt.Fprint(cmd.OutOrStdout(), string(stringEnvVars)) +} + +func HandleSendUniversalAuthToken(cmd *cobra.Command, token string) { + + if !IS_TEST_MODE { + return + } + + fmt.Fprint(cmd.OutOrStdout(), token) } From ba42aca069b9d5a4192a2371ea9d6958b6cc8a94 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 15:13:58 +0200 Subject: [PATCH 16/45] Workflow --- .github/workflows/run-cli-tests.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 .github/workflows/run-cli-tests.yml diff --git a/.github/workflows/run-cli-tests.yml b/.github/workflows/run-cli-tests.yml new file mode 100644 index 000000000..aa3edaff6 --- /dev/null +++ b/.github/workflows/run-cli-tests.yml @@ -0,0 +1,22 @@ +name: Go CLI Tests +on: [push] # Test + +jobs: + test: + defaults: + run: + working-directory: ./cli + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Setup Go + uses: actions/setup-go@v4 + with: + go-version: "1.21.x" + - name: Install dependencies + run: go get . + - name: Build + run: go build -v ./... + - name: Test with the Go CLI + run: TEST_MODE=true go test -v -count=1 ./test From 3f68807179e14c483f6f3fd0c8d79242bfdc5477 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 17:07:37 +0200 Subject: [PATCH 17/45] Update run-cli-tests.yml --- .github/workflows/run-cli-tests.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/run-cli-tests.yml b/.github/workflows/run-cli-tests.yml index aa3edaff6..1076228cc 100644 --- a/.github/workflows/run-cli-tests.yml +++ b/.github/workflows/run-cli-tests.yml @@ -19,4 +19,11 @@ jobs: - name: Build run: go build -v ./... - name: Test with the Go CLI + env: + CLI_TESTS_UA_CLIENT_ID: ${{ secrets.CLI_TESTS_UA_CLIENT_ID }} + CLI_TESTS_UA_CLIENT_SECRET: ${{ secrets.CLI_TESTS_UA_CLIENT_SECRET }} + CLI_TESTS_SERVICE_TOKEN: ${{ secrets.CLI_TESTS_SERVICE_TOKEN }} + CLI_TESTS_PROJECT_ID: ${{ secrets.CLI_TESTS_PROJECT_ID }} + CLI_TESTS_ENV_SLUG: ${{ secrets.CLI_TESTS_ENV_SLUG }} + run: TEST_MODE=true go test -v -count=1 ./test From e453ddf937b3930f83e8f4e2b43bb04778f5b457 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 18:04:29 +0200 Subject: [PATCH 18/45] Update secrets.go --- cli/packages/cmd/secrets.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index c3228587e..95ac6c5bb 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -39,7 +39,6 @@ var secretsCmd = &cobra.Command{ } token, err := util.GetInfisicalToken(cmd) - if err != nil { util.HandleError(err, "Unable to parse flag") } From 4050e56e60da308a0a09ca0c2f8535c1e6e0e8f2 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Thu, 18 Apr 2024 23:29:11 +0200 Subject: [PATCH 19/45] Feat: CLI Integration tests --- .../workflows/release_build_infisical_cli.yml | 112 +++++++++--------- .github/workflows/run-cli-tests.yml | 9 +- 2 files changed, 66 insertions(+), 55 deletions(-) diff --git a/.github/workflows/release_build_infisical_cli.yml b/.github/workflows/release_build_infisical_cli.yml index d01d56198..40137a455 100644 --- a/.github/workflows/release_build_infisical_cli.yml +++ b/.github/workflows/release_build_infisical_cli.yml @@ -1,60 +1,64 @@ name: Build and release CLI on: - push: - # run only against tags - tags: - - "infisical-cli/v*.*.*" + push: + # run only against tags + tags: + - "infisical-cli/v*.*.*" permissions: - contents: write - # packages: write - # issues: write - + contents: write + # packages: write + # issues: write jobs: - goreleaser: - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - name: 🐋 Login to Docker Hub - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: 🔧 Set up Docker Buildx - uses: docker/setup-buildx-action@v2 - - run: git fetch --force --tags - - run: echo "Ref name ${{github.ref_name}}" - - uses: actions/setup-go@v3 - with: - go-version: ">=1.19.3" - cache: true - cache-dependency-path: cli/go.sum - - name: libssl1.1 => libssl1.0-dev for OSXCross - run: | - echo 'deb http://security.ubuntu.com/ubuntu bionic-security main' | sudo tee -a /etc/apt/sources.list - sudo apt update && apt-cache policy libssl1.0-dev - sudo apt-get install libssl1.0-dev - - name: OSXCross for CGO Support - run: | - mkdir ../../osxcross - git clone https://github.com/plentico/osxcross-target.git ../../osxcross/target - - uses: goreleaser/goreleaser-action@v4 - with: - distribution: goreleaser-pro - version: latest - args: release --clean - env: - GITHUB_TOKEN: ${{ secrets.GO_RELEASER_GITHUB_TOKEN }} - POSTHOG_API_KEY_FOR_CLI: ${{ secrets.POSTHOG_API_KEY_FOR_CLI }} - FURY_TOKEN: ${{ secrets.FURYPUSHTOKEN }} - AUR_KEY: ${{ secrets.AUR_KEY }} - GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }} - - uses: actions/setup-python@v4 - - run: pip install --upgrade cloudsmith-cli - - name: Publish to CloudSmith - run: sh cli/upload_to_cloudsmith.sh - env: - CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }} + cli-integration-tests: + name: Run tests before deployment + uses: ./.github/workflows/run-cli-tests.yml + + goreleaser: + runs-on: ubuntu-20.04 + needs: [cli-integration-tests] + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - name: 🐋 Login to Docker Hub + uses: docker/login-action@v2 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: 🔧 Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + - run: git fetch --force --tags + - run: echo "Ref name ${{github.ref_name}}" + - uses: actions/setup-go@v3 + with: + go-version: ">=1.19.3" + cache: true + cache-dependency-path: cli/go.sum + - name: libssl1.1 => libssl1.0-dev for OSXCross + run: | + echo 'deb http://security.ubuntu.com/ubuntu bionic-security main' | sudo tee -a /etc/apt/sources.list + sudo apt update && apt-cache policy libssl1.0-dev + sudo apt-get install libssl1.0-dev + - name: OSXCross for CGO Support + run: | + mkdir ../../osxcross + git clone https://github.com/plentico/osxcross-target.git ../../osxcross/target + - uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser-pro + version: latest + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GO_RELEASER_GITHUB_TOKEN }} + POSTHOG_API_KEY_FOR_CLI: ${{ secrets.POSTHOG_API_KEY_FOR_CLI }} + FURY_TOKEN: ${{ secrets.FURYPUSHTOKEN }} + AUR_KEY: ${{ secrets.AUR_KEY }} + GORELEASER_KEY: ${{ secrets.GORELEASER_KEY }} + - uses: actions/setup-python@v4 + - run: pip install --upgrade cloudsmith-cli + - name: Publish to CloudSmith + run: sh cli/upload_to_cloudsmith.sh + env: + CLOUDSMITH_API_KEY: ${{ secrets.CLOUDSMITH_API_KEY }} diff --git a/.github/workflows/run-cli-tests.yml b/.github/workflows/run-cli-tests.yml index 1076228cc..9cd5d1519 100644 --- a/.github/workflows/run-cli-tests.yml +++ b/.github/workflows/run-cli-tests.yml @@ -1,5 +1,12 @@ name: Go CLI Tests -on: [push] # Test + +on: + pull_request: + types: [opened, synchronize] + paths: + - "cli/**" + + workflow_call: jobs: test: From 0e3cc4fdeb209ff5b7ed14e3fd165eb378c9f233 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:02:40 +0200 Subject: [PATCH 20/45] Correct snapshots --- .../test-TestServiceToken_ExportSecretsWithImports | 5 +++++ .../test-TestServiceToken_ExportSecretsWithoutImports | 3 +++ .../test-TestServiceToken_GetSecretsByNameRecursive | 7 +++++++ .../test-TestServiceToken_GetSecretsByNameWithImports | 7 +++++++ ...TestServiceToken_GetSecretsByNameWithNotFoundSecret | 8 ++++++++ .../test-TestServiceToken_RunCmdRecursiveAndImports | 2 ++ .../.snapshots/test-TestServiceToken_RunCmdWithImports | 2 ++ .../test-TestServiceToken_RunCmdWithoutImports | 2 ++ ...stServiceToken_SecretsGetWithImportsAndRecursiveCmd | 10 ++++++++++ ...oken_SecretsGetWithoutImportsAndWithoutRecursiveCmd | 7 +++++++ .../test-TestUniversalAuth_ExportSecretsWithImports | 5 +++++ .../test-TestUniversalAuth_ExportSecretsWithoutImports | 3 +++ .../test-TestUniversalAuth_GetSecretsByNameRecursive | 7 +++++++ .../test-TestUniversalAuth_GetSecretsByNameWithImports | 7 +++++++ ...estUniversalAuth_GetSecretsByNameWithNotFoundSecret | 8 ++++++++ .../test-TestUniversalAuth_RunCmdRecursiveAndImports | 2 ++ .../test-TestUniversalAuth_RunCmdWithImports | 2 ++ .../test-TestUniversalAuth_RunCmdWithoutImports | 2 ++ ...tUniversalAuth_SecretsGetWithImportsAndRecursiveCmd | 10 ++++++++++ ...Auth_SecretsGetWithoutImportsAndWithoutRecursiveCmd | 7 +++++++ .../test-TestUniversalAuth_SecretsGetWrongEnvironment | 4 ++++ 21 files changed, 110 insertions(+) create mode 100644 cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports create mode 100644 cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithoutImports create mode 100644 cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameRecursive create mode 100644 cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameWithImports create mode 100644 cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameWithNotFoundSecret create mode 100644 cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports create mode 100644 cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports create mode 100644 cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports create mode 100644 cli/test/.snapshots/test-TestServiceToken_SecretsGetWithImportsAndRecursiveCmd create mode 100644 cli/test/.snapshots/test-TestServiceToken_SecretsGetWithoutImportsAndWithoutRecursiveCmd create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithImports create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameRecursive create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameWithImports create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameWithNotFoundSecret create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWithImportsAndRecursiveCmd create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWithoutImportsAndWithoutRecursiveCmd create mode 100644 cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWrongEnvironment diff --git a/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports b/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports new file mode 100644 index 000000000..022400543 --- /dev/null +++ b/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports @@ -0,0 +1,5 @@ +STAGING-SECRET-1='staging-value-1' +TEST-SECRET-1='test-value-1' +TEST-SECRET-3='test-value-3' +TEST-SECRET-2='test-value-2' +STAGING-SECRET-2='staging-value-2' diff --git a/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithoutImports b/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithoutImports new file mode 100644 index 000000000..6cc3ca7c4 --- /dev/null +++ b/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithoutImports @@ -0,0 +1,3 @@ +TEST-SECRET-1='test-value-1' +TEST-SECRET-3='test-value-3' +TEST-SECRET-2='test-value-2' diff --git a/cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameRecursive b/cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameRecursive new file mode 100644 index 000000000..f2f26ae19 --- /dev/null +++ b/cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameRecursive @@ -0,0 +1,7 @@ +┌─────────────────┬────────────────┬─────────────┐ +│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │ +├─────────────────┼────────────────┼─────────────┤ +│ TEST-SECRET-1 │ test-value-1 │ shared │ +│ TEST-SECRET-2 │ test-value-2 │ shared │ +│ FOLDER-SECRET-1 │ folder-value-1 │ shared │ +└─────────────────┴────────────────┴─────────────┘ diff --git a/cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameWithImports b/cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameWithImports new file mode 100644 index 000000000..ff488466d --- /dev/null +++ b/cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameWithImports @@ -0,0 +1,7 @@ +┌──────────────────┬─────────────────┬─────────────┐ +│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │ +├──────────────────┼─────────────────┼─────────────┤ +│ TEST-SECRET-1 │ test-value-1 │ shared │ +│ STAGING-SECRET-2 │ staging-value-2 │ shared │ +│ FOLDER-SECRET-1 │ folder-value-1 │ shared │ +└──────────────────┴─────────────────┴─────────────┘ diff --git a/cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameWithNotFoundSecret b/cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameWithNotFoundSecret new file mode 100644 index 000000000..afe3bffa8 --- /dev/null +++ b/cli/test/.snapshots/test-TestServiceToken_GetSecretsByNameWithNotFoundSecret @@ -0,0 +1,8 @@ +┌─────────────────┬────────────────┬─────────────┐ +│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │ +├─────────────────┼────────────────┼─────────────┤ +│ TEST-SECRET-1 │ test-value-1 │ shared │ +│ TEST-SECRET-2 │ test-value-2 │ shared │ +│ FOLDER-SECRET-1 │ folder-value-1 │ shared │ +│ DOES-NOT-EXIST │ *not found* │ *not found* │ +└─────────────────┴────────────────┴─────────────┘ diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports new file mode 100644 index 000000000..7bf32be6c --- /dev/null +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports @@ -0,0 +1,2 @@ +2:53PM INF Injecting 6 Infisical secrets into your application process +hello world diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports new file mode 100644 index 000000000..cc3d8ae47 --- /dev/null +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports @@ -0,0 +1,2 @@ +2:53PM INF Injecting 5 Infisical secrets into your application process +hello world diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports new file mode 100644 index 000000000..9e9929a52 --- /dev/null +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports @@ -0,0 +1,2 @@ +2:53PM INF Injecting 3 Infisical secrets into your application process +hello world diff --git a/cli/test/.snapshots/test-TestServiceToken_SecretsGetWithImportsAndRecursiveCmd b/cli/test/.snapshots/test-TestServiceToken_SecretsGetWithImportsAndRecursiveCmd new file mode 100644 index 000000000..dd2bc317c --- /dev/null +++ b/cli/test/.snapshots/test-TestServiceToken_SecretsGetWithImportsAndRecursiveCmd @@ -0,0 +1,10 @@ +┌──────────────────┬─────────────────┬─────────────┐ +│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │ +├──────────────────┼─────────────────┼─────────────┤ +│ FOLDER-SECRET-1 │ folder-value-1 │ shared │ +│ STAGING-SECRET-1 │ staging-value-1 │ shared │ +│ STAGING-SECRET-2 │ staging-value-2 │ shared │ +│ TEST-SECRET-1 │ test-value-1 │ shared │ +│ TEST-SECRET-2 │ test-value-2 │ shared │ +│ TEST-SECRET-3 │ test-value-3 │ shared │ +└──────────────────┴─────────────────┴─────────────┘ diff --git a/cli/test/.snapshots/test-TestServiceToken_SecretsGetWithoutImportsAndWithoutRecursiveCmd b/cli/test/.snapshots/test-TestServiceToken_SecretsGetWithoutImportsAndWithoutRecursiveCmd new file mode 100644 index 000000000..260607e97 --- /dev/null +++ b/cli/test/.snapshots/test-TestServiceToken_SecretsGetWithoutImportsAndWithoutRecursiveCmd @@ -0,0 +1,7 @@ +┌───────────────┬──────────────┬─────────────┐ +│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │ +├───────────────┼──────────────┼─────────────┤ +│ TEST-SECRET-1 │ test-value-1 │ shared │ +│ TEST-SECRET-2 │ test-value-2 │ shared │ +│ TEST-SECRET-3 │ test-value-3 │ shared │ +└───────────────┴──────────────┴─────────────┘ diff --git a/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithImports b/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithImports new file mode 100644 index 000000000..1b59a0f22 --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithImports @@ -0,0 +1,5 @@ +TEST-SECRET-1='test-value-1' +TEST-SECRET-3='test-value-3' +TEST-SECRET-2='test-value-2' +STAGING-SECRET-2='staging-value-2' +STAGING-SECRET-1='staging-value-1' diff --git a/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports b/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports new file mode 100644 index 000000000..6cc3ca7c4 --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports @@ -0,0 +1,3 @@ +TEST-SECRET-1='test-value-1' +TEST-SECRET-3='test-value-3' +TEST-SECRET-2='test-value-2' diff --git a/cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameRecursive b/cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameRecursive new file mode 100644 index 000000000..f2f26ae19 --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameRecursive @@ -0,0 +1,7 @@ +┌─────────────────┬────────────────┬─────────────┐ +│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │ +├─────────────────┼────────────────┼─────────────┤ +│ TEST-SECRET-1 │ test-value-1 │ shared │ +│ TEST-SECRET-2 │ test-value-2 │ shared │ +│ FOLDER-SECRET-1 │ folder-value-1 │ shared │ +└─────────────────┴────────────────┴─────────────┘ diff --git a/cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameWithImports b/cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameWithImports new file mode 100644 index 000000000..ff488466d --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameWithImports @@ -0,0 +1,7 @@ +┌──────────────────┬─────────────────┬─────────────┐ +│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │ +├──────────────────┼─────────────────┼─────────────┤ +│ TEST-SECRET-1 │ test-value-1 │ shared │ +│ STAGING-SECRET-2 │ staging-value-2 │ shared │ +│ FOLDER-SECRET-1 │ folder-value-1 │ shared │ +└──────────────────┴─────────────────┴─────────────┘ diff --git a/cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameWithNotFoundSecret b/cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameWithNotFoundSecret new file mode 100644 index 000000000..afe3bffa8 --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_GetSecretsByNameWithNotFoundSecret @@ -0,0 +1,8 @@ +┌─────────────────┬────────────────┬─────────────┐ +│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │ +├─────────────────┼────────────────┼─────────────┤ +│ TEST-SECRET-1 │ test-value-1 │ shared │ +│ TEST-SECRET-2 │ test-value-2 │ shared │ +│ FOLDER-SECRET-1 │ folder-value-1 │ shared │ +│ DOES-NOT-EXIST │ *not found* │ *not found* │ +└─────────────────┴────────────────┴─────────────┘ diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports new file mode 100644 index 000000000..7bf32be6c --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports @@ -0,0 +1,2 @@ +2:53PM INF Injecting 6 Infisical secrets into your application process +hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports new file mode 100644 index 000000000..cc3d8ae47 --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports @@ -0,0 +1,2 @@ +2:53PM INF Injecting 5 Infisical secrets into your application process +hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports new file mode 100644 index 000000000..9e9929a52 --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports @@ -0,0 +1,2 @@ +2:53PM INF Injecting 3 Infisical secrets into your application process +hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWithImportsAndRecursiveCmd b/cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWithImportsAndRecursiveCmd new file mode 100644 index 000000000..dd2bc317c --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWithImportsAndRecursiveCmd @@ -0,0 +1,10 @@ +┌──────────────────┬─────────────────┬─────────────┐ +│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │ +├──────────────────┼─────────────────┼─────────────┤ +│ FOLDER-SECRET-1 │ folder-value-1 │ shared │ +│ STAGING-SECRET-1 │ staging-value-1 │ shared │ +│ STAGING-SECRET-2 │ staging-value-2 │ shared │ +│ TEST-SECRET-1 │ test-value-1 │ shared │ +│ TEST-SECRET-2 │ test-value-2 │ shared │ +│ TEST-SECRET-3 │ test-value-3 │ shared │ +└──────────────────┴─────────────────┴─────────────┘ diff --git a/cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWithoutImportsAndWithoutRecursiveCmd b/cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWithoutImportsAndWithoutRecursiveCmd new file mode 100644 index 000000000..260607e97 --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWithoutImportsAndWithoutRecursiveCmd @@ -0,0 +1,7 @@ +┌───────────────┬──────────────┬─────────────┐ +│ SECRET NAME │ SECRET VALUE │ SECRET TYPE │ +├───────────────┼──────────────┼─────────────┤ +│ TEST-SECRET-1 │ test-value-1 │ shared │ +│ TEST-SECRET-2 │ test-value-2 │ shared │ +│ TEST-SECRET-3 │ test-value-3 │ shared │ +└───────────────┴──────────────┴─────────────┘ diff --git a/cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWrongEnvironment b/cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWrongEnvironment new file mode 100644 index 000000000..c3811bd22 --- /dev/null +++ b/cli/test/.snapshots/test-TestUniversalAuth_SecretsGetWrongEnvironment @@ -0,0 +1,4 @@ +error: CallGetRawSecretsV3: Unsuccessful response [GET https://app.infisical.com/api/v3/secrets/raw?environment=invalid-env&include_imports=true&recursive=true&secretPath=%2F&workspaceId=bef697d4-849b-4a75-b284-0922f87f8ba2] [status-code=500] [response={"statusCode":500,"error":"Internal Server Error","message":"'invalid-env' environment not found in project with ID bef697d4-849b-4a75-b284-0922f87f8ba2"}] + + +If this issue continues, get support at https://infisical.com/slack From d4d3c2b10f855d303e547e153215956302c60a6e Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:02:44 +0200 Subject: [PATCH 21/45] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4a12c15f2..f97955983 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,4 @@ yarn-error.log* frontend-build *.tgz +cli/infisical-merge From ae63898d5e42de876060d1407bae10e8bccd7057 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:02:51 +0200 Subject: [PATCH 22/45] Install cupaloy --- cli/go.mod | 1 + cli/go.sum | 3 +++ 2 files changed, 4 insertions(+) diff --git a/cli/go.mod b/cli/go.mod index d3b8eff0c..833745eff 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -29,6 +29,7 @@ require ( require ( github.com/alessio/shellescape v1.4.1 // indirect github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef // indirect + github.com/bradleyjkemp/cupaloy/v2 v2.8.0 // indirect github.com/chzyer/readline v1.5.1 // indirect github.com/danieljoos/wincred v1.2.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/cli/go.sum b/cli/go.sum index a73ac5185..353579136 100644 --- a/cli/go.sum +++ b/cli/go.sum @@ -51,6 +51,8 @@ github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef h1:46PFijGL github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM= +github.com/bradleyjkemp/cupaloy/v2 v2.8.0 h1:any4BmKE+jGIaMpnU8YgH/I2LPiLBufr6oMMlVBbn9M= +github.com/bradleyjkemp/cupaloy/v2 v2.8.0/go.mod h1:bm7JXdkRd4BHJk9HpwqAI8BoAY1lps46Enkdqw6aRX0= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8= github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs= @@ -324,6 +326,7 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An github.com/spf13/viper v1.8.1 h1:Kq1fyeebqsBfbjZj4EL7gj2IO0mMaiyjYUWcUsl2O44= github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= From 7a20251261d2619138754bf5669911816dd644a2 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:10:55 +0200 Subject: [PATCH 23/45] Fix: Returning keys in a reproducible manner --- cli/packages/cmd/secrets.go | 3 +++ cli/packages/util/helper.go | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index 95ac6c5bb..b333931a3 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -116,6 +116,9 @@ var secretsCmd = &cobra.Command{ secrets = util.ExpandSecrets(secrets, authParams, "") } + // convert secrets to the same order as the keys + secrets = util.SortSecretsByKeys(secrets) + util.HandleSendTestSecrets(cmd, secrets) visualize.PrintAllSecretDetails(secrets) Telemetry.CaptureEvent("cli-command:secrets", posthog.NewProperties().Set("secretCount", len(secrets)).Set("version", util.CLI_VERSION)) diff --git a/cli/packages/util/helper.go b/cli/packages/util/helper.go index e5b8ab6d9..33e22e040 100644 --- a/cli/packages/util/helper.go +++ b/cli/packages/util/helper.go @@ -53,6 +53,19 @@ func GetBase64DecodedSymmetricEncryptionDetails(key string, cipher string, IV st }, nil } +func SortSecretsByKeys(secrets []models.SingleEnvironmentVariable) []models.SingleEnvironmentVariable { + // sort secrets by key + for i := 0; i < len(secrets); i++ { + for j := i + 1; j < len(secrets); j++ { + if secrets[i].Key > secrets[j].Key { + secrets[i], secrets[j] = secrets[j], secrets[i] + } + } + } + + return secrets +} + func IsSecretEnvironmentValid(env string) bool { if env == "prod" || env == "dev" || env == "test" || env == "staging" { return true From 580b86cde8160e1d4ec042e5ca1ed0b3ce4c12a8 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:11:10 +0200 Subject: [PATCH 24/45] Fix: Refactor teests to use cupaloy --- cli/test/export_test.go | 122 +++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 65 deletions(-) diff --git a/cli/test/export_test.go b/cli/test/export_test.go index a0f68fa7a..d3069701b 100644 --- a/cli/test/export_test.go +++ b/cli/test/export_test.go @@ -1,90 +1,82 @@ package tests import ( - "bytes" - "encoding/json" "fmt" "testing" - "github.com/Infisical/infisical-merge/packages/cmd" - "github.com/Infisical/infisical-merge/packages/models" - "github.com/stretchr/testify/assert" + "github.com/bradleyjkemp/cupaloy/v2" ) -func ExportSecrets(t *testing.T, authToken string, projectId string, envSlug string) { +func TestUniversalAuth_ExportSecretsWithImports(t *testing.T) { + MachineIdentityLoginCmd(t) + SetupCli(t) - rootCommand := cmd.NewRootCmd() + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "export", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent") - commandOutput := new(bytes.Buffer) - errorOutput := new(bytes.Buffer) - rootCommand.SetOut(commandOutput) - rootCommand.SetErr(errorOutput) + fmt.Printf("output: %v\n", output) - args := []string{ - "export", - } - - args = append(args, fmt.Sprintf("--token=%s", authToken)) - args = append(args, fmt.Sprintf("--projectId=%s", projectId)) - args = append(args, fmt.Sprintf("--env=%s", envSlug)) - - rootCommand.SetArgs(args) - rootCommand.Execute() - - var secrets []models.SingleEnvironmentVariable - - err := json.Unmarshal(commandOutput.Bytes(), &secrets) if err != nil { - t.Errorf("Error: %v", err) + t.Fatalf("error running CLI command: %v", err) } - expectedLength := len(DEV_SECRETS) + len(STAGING_SECRETS) - - assert.Len(t, secrets, expectedLength) - - for _, secret := range secrets { - if secret.Key == "FOLDER-SECRET-1" { - continue - } - assert.Contains(t, ALL_SECRET_KEYS, secret.Key) - assert.Contains(t, ALL_SECRET_VALUES, secret.Value) + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) } } -func ExportSecretsWithoutImports(t *testing.T, authToken string, projectId string, envSlug string) { +func TestServiceToken_ExportSecretsWithImports(t *testing.T) { + SetupCli(t) - rootCommand := cmd.NewRootCmd() + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "export", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent") - commandOutput := new(bytes.Buffer) - rootCommand.SetOut(commandOutput) - rootCommand.SetErr(commandOutput) + fmt.Printf("output: %v\n", output) - args := []string{ - "export", - } - - args = append(args, fmt.Sprintf("--token=%s", authToken)) - args = append(args, fmt.Sprintf("--projectId=%s", projectId)) - args = append(args, fmt.Sprintf("--env=%s", envSlug)) - args = append(args, "--include-imports=false") - - rootCommand.SetArgs(args) - rootCommand.Execute() - - var secrets []models.SingleEnvironmentVariable - - err := json.Unmarshal(commandOutput.Bytes(), &secrets) if err != nil { - t.Errorf("Error: %v", err) + t.Fatalf("error running CLI command: %v", err) } - assert.Len(t, secrets, len(DEV_SECRETS)) - - allDevSecretKeys, allDevSecretValues := getSecretKeysAndValues(DEV_SECRETS) - - for _, secret := range secrets { - assert.Contains(t, allDevSecretKeys, secret.Key) - assert.Contains(t, allDevSecretValues, secret.Value) + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestUniversalAuth_ExportSecretsWithoutImports(t *testing.T) { + MachineIdentityLoginCmd(t) + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "export", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--include-imports=false") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestServiceToken_ExportSecretsWithoutImports(t *testing.T) { + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "export", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--include-imports=false") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) } - } From 3bcf406688b3e35ee7a5e504158431444e3e93b6 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:11:20 +0200 Subject: [PATCH 25/45] Fix: Refactor --- cli/test/helper.go | 68 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/cli/test/helper.go b/cli/test/helper.go index 912a3a71d..995367c4b 100644 --- a/cli/test/helper.go +++ b/cli/test/helper.go @@ -1,26 +1,64 @@ package tests -type Secret struct { - Key string - Value string +import ( + "fmt" + "os" + "os/exec" + "strings" + "testing" +) + +const ( + CLI_NAME = "infisical-merge" +) + +var ( + FORMATTED_CLI_NAME = fmt.Sprintf("./%s", CLI_NAME) +) + +type Credentials struct { + ClientID string + ClientSecret string + UAAccessToken string + ServiceToken string + ProjectID string + EnvSlug string } -func Map[T, U any](ts []T, f func(T) U) []U { - us := make([]U, len(ts)) - for i := range ts { - us[i] = f(ts[i]) +var creds = Credentials{ + UAAccessToken: "", + ClientID: os.Getenv("CLI_TESTS_UA_CLIENT_ID"), + ClientSecret: os.Getenv("CLI_TESTS_UA_CLIENT_SECRET"), + ServiceToken: os.Getenv("CLI_TESTS_SERVICE_TOKEN"), + ProjectID: os.Getenv("CLI_TESTS_PROJECT_ID"), + EnvSlug: os.Getenv("CLI_TESTS_ENV_SLUG"), +} + +func ExecuteCliCommand(command string, args ...string) (string, error) { + cmd := exec.Command(command, args...) + output, err := cmd.CombinedOutput() + if err != nil { + return strings.TrimSpace(string(output)), err } - return us + return strings.TrimSpace(string(output)), nil } -func getSecretKeysAndValues(secrets []Secret) (keys []string, values []string) { - secretKeys := []string{} - secretValues := []string{} +func SetupCli(t *testing.T) { - for _, secret := range secrets { - secretKeys = append(secretKeys, secret.Key) - secretValues = append(secretValues, secret.Value) + if creds.ClientID == "" || creds.ClientSecret == "" || creds.ServiceToken == "" || creds.ProjectID == "" || creds.EnvSlug == "" { + panic("Missing required environment variables") + } + + // check if the CLI is already built, if not build it + alreadyBuilt := false + if _, err := os.Stat(FORMATTED_CLI_NAME); err == nil { + alreadyBuilt = true + } + + if !alreadyBuilt { + if err := exec.Command("go", "build", "../.").Run(); err != nil { + t.Fatal(err) + } } - return secretKeys, secretValues } From 89ff6a6c9399cab2f60f4bc8624c972b996d5ed9 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:11:25 +0200 Subject: [PATCH 26/45] Update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f97955983..b04860071 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,4 @@ frontend-build *.tgz cli/infisical-merge +cli/test/infisical-merge From 2b215a510c7ab0620ea3c301b161b12925ab6f72 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:11:39 +0200 Subject: [PATCH 27/45] Fix: Integrated UA login test --- cli/test/login.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 cli/test/login.go diff --git a/cli/test/login.go b/cli/test/login.go new file mode 100644 index 000000000..0f4591413 --- /dev/null +++ b/cli/test/login.go @@ -0,0 +1,29 @@ +package tests + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func MachineIdentityLoginCmd(t *testing.T) { + SetupCli(t) + + if creds.UAAccessToken != "" { + return + } + + jwtPattern := `^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$` + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "login", "--method=universal-auth", "--client-id", creds.ClientID, "--client-secret", creds.ClientSecret, "--plain", "--silent") + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + assert.Regexp(t, jwtPattern, output) + + creds.UAAccessToken = output + + // We can't use snapshot testing here because the output will be different every time +} From 9387d9aaacef84882c1f8b3e6f94804981743422 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:11:58 +0200 Subject: [PATCH 28/45] Rename --- cli/test/login.go | 29 ----------------------------- cli/test/login_test.go | 40 +++++++++++++--------------------------- 2 files changed, 13 insertions(+), 56 deletions(-) delete mode 100644 cli/test/login.go diff --git a/cli/test/login.go b/cli/test/login.go deleted file mode 100644 index 0f4591413..000000000 --- a/cli/test/login.go +++ /dev/null @@ -1,29 +0,0 @@ -package tests - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func MachineIdentityLoginCmd(t *testing.T) { - SetupCli(t) - - if creds.UAAccessToken != "" { - return - } - - jwtPattern := `^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$` - - output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "login", "--method=universal-auth", "--client-id", creds.ClientID, "--client-secret", creds.ClientSecret, "--plain", "--silent") - - if err != nil { - t.Fatalf("error running CLI command: %v", err) - } - - assert.Regexp(t, jwtPattern, output) - - creds.UAAccessToken = output - - // We can't use snapshot testing here because the output will be different every time -} diff --git a/cli/test/login_test.go b/cli/test/login_test.go index 1d598153a..0f4591413 100644 --- a/cli/test/login_test.go +++ b/cli/test/login_test.go @@ -1,43 +1,29 @@ package tests import ( - "bytes" - "fmt" - "regexp" "testing" - "github.com/Infisical/infisical-merge/packages/cmd" "github.com/stretchr/testify/assert" ) -func UALoginCmd(t *testing.T) { - jwtPattern := `^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$` +func MachineIdentityLoginCmd(t *testing.T) { + SetupCli(t) - rootCommand := cmd.NewRootCmd() - - commandOutput := new(bytes.Buffer) - errorOutput := new(bytes.Buffer) - rootCommand.SetOut(commandOutput) - rootCommand.SetErr(errorOutput) - - args := []string{ - "login", + if creds.UAAccessToken != "" { + return } - args = append(args, fmt.Sprintf("--method=%s", "universal-auth")) - args = append(args, fmt.Sprintf("--client-id=%s", creds.ClientID)) - args = append(args, fmt.Sprintf("--client-secret=%s", creds.ClientSecret)) + jwtPattern := `^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$` - rootCommand.SetArgs(args) - rootCommand.Execute() + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "login", "--method=universal-auth", "--client-id", creds.ClientID, "--client-secret", creds.ClientSecret, "--plain", "--silent") - token := commandOutput.String() + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } - // We do a match and compare it against true, instead of using assert.Regexp. - // If the assertion fails, we would be able to see the potential token that was generated in the output console, which would be bad if running in a CI/CD pipeline. - match, err := regexp.MatchString(jwtPattern, token) - assert.Nil(t, err) - assert.True(t, match, "The token does not match the pattern") + assert.Regexp(t, jwtPattern, output) - creds.UAAccessToken = token + creds.UAAccessToken = output + + // We can't use snapshot testing here because the output will be different every time } From afee47ab4520db21fe65315ce28ab8dca6380e55 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:12:02 +0200 Subject: [PATCH 29/45] Delete root_test.go --- cli/test/root_test.go | 115 ------------------------------------------ 1 file changed, 115 deletions(-) delete mode 100644 cli/test/root_test.go diff --git a/cli/test/root_test.go b/cli/test/root_test.go deleted file mode 100644 index e5e9d7684..000000000 --- a/cli/test/root_test.go +++ /dev/null @@ -1,115 +0,0 @@ -package tests - -import ( - "os" - "testing" -) - -var DEV_SECRETS = []Secret{ - { - Key: "TEST-SECRET-1", - Value: "test-value-1", - }, - { - Key: "TEST-SECRET-2", - Value: "test-value-2", - }, - { - Key: "TEST-SECRET-3", - Value: "test-value-3", - }, -} - -var DEV_FOLDER_SECRETS = []Secret{ - { - Key: "FOLDER-SECRET-1", - Value: "folder-value-1", - }, -} - -var STAGING_SECRETS = []Secret{ - { - Key: "STAGING-SECRET-1", - Value: "staging-value-1", - }, - { - Key: "STAGING-SECRET-2", - Value: "staging-value-2", - }, -} - -// Initialize the combined secrets array -var ALL_SECRETS = []Secret{} -var ALL_SECRET_KEYS = []string{} -var ALL_SECRET_VALUES = []string{} - -type Credentials struct { - ClientID string - ClientSecret string - UAAccessToken string - ServiceToken string - ProjectID string - EnvSlug string -} - -var creds = Credentials{ - UAAccessToken: "", - ClientID: os.Getenv("CLI_TESTS_UA_CLIENT_ID"), - ClientSecret: os.Getenv("CLI_TESTS_UA_CLIENT_SECRET"), - ServiceToken: os.Getenv("CLI_TESTS_SERVICE_TOKEN"), - ProjectID: os.Getenv("CLI_TESTS_PROJECT_ID"), - EnvSlug: os.Getenv("CLI_TESTS_ENV_SLUG"), -} - -func initialize() { - if creds.ClientID == "" || creds.ClientSecret == "" || creds.ServiceToken == "" || creds.ProjectID == "" || creds.EnvSlug == "" { - panic("Missing required environment variables") - } - - ALL_SECRETS = append(ALL_SECRETS, DEV_SECRETS...) - ALL_SECRETS = append(ALL_SECRETS, DEV_FOLDER_SECRETS...) - ALL_SECRETS = append(ALL_SECRETS, STAGING_SECRETS...) - - for _, secret := range ALL_SECRETS { - ALL_SECRET_KEYS = append(ALL_SECRET_KEYS, secret.Key) - ALL_SECRET_VALUES = append(ALL_SECRET_VALUES, secret.Value) - } -} - -func Test_RunTests(t *testing.T) { - initialize() - - t.Run("User login command", func(t *testing.T) { - UALoginCmd(t) - }) - - t.Run("Run command", func(t *testing.T) { - RunCmd(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) - RunCmd(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) - }) - - t.Run("Run Command (without imports, with recursive)", func(t *testing.T) { - RunCmdWithoutImportsAndWithRecursive(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) - RunCmdWithoutImportsAndWithRecursive(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) - }) - - t.Run("Export secrets", func(t *testing.T) { - ExportSecrets(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) - ExportSecrets(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) - }) - - t.Run("Export secrets (without imports)", func(t *testing.T) { - ExportSecretsWithoutImports(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) - ExportSecretsWithoutImports(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) - }) - - t.Run("List Secrets (with imports and recursive)", func(t *testing.T) { - ListSecretsWithImportsAndRecursive(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) - ListSecretsWithImportsAndRecursive(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) - }) - - t.Run("Get Secrets by Names", func(t *testing.T) { - GetSecretsByNames(t, creds.UAAccessToken, creds.ProjectID, creds.EnvSlug) - GetSecretsByNames(t, creds.ServiceToken, creds.ProjectID, creds.EnvSlug) - }) -} From 55aea364da9fa6ede43cfc7bf13f994f78ea1a1d Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:12:09 +0200 Subject: [PATCH 30/45] Fix: Refactor teests to use cupaloy --- cli/test/run_test.go | 199 +++++++++++++++++++------------------------ 1 file changed, 90 insertions(+), 109 deletions(-) diff --git a/cli/test/run_test.go b/cli/test/run_test.go index 8ea26e38d..b9518b835 100644 --- a/cli/test/run_test.go +++ b/cli/test/run_test.go @@ -1,137 +1,118 @@ package tests import ( - "bytes" "fmt" - "slices" - "strings" "testing" - "github.com/Infisical/infisical-merge/packages/cmd" - "github.com/stretchr/testify/assert" + "github.com/bradleyjkemp/cupaloy/v2" ) -func RunCmd(t *testing.T, authToken string, projectId string, envSlug string) { +func TestServiceToken_RunCmdRecursiveAndImports(t *testing.T) { + SetupCli(t) - rootCommand := cmd.NewRootCmd() + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent", "--", "echo", "hello world") - commandOutput := new(bytes.Buffer) - errorOutput := new(bytes.Buffer) - rootCommand.SetOut(commandOutput) - rootCommand.SetErr(errorOutput) + fmt.Printf("output: %v\n", output) - args := []string{ - "run", + if err != nil { + t.Fatalf("error running CLI command: %v", err) } - args = append(args, fmt.Sprintf("--token=%s", authToken)) - args = append(args, fmt.Sprintf("--projectId=%s", projectId)) - args = append(args, fmt.Sprintf("--env=%s", envSlug)) - args = append(args, "--", "echo", "TEST_COMMAND_BEING_EXECUTED") + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} +func TestServiceToken_RunCmdWithImports(t *testing.T) { + SetupCli(t) - rootCommand.SetArgs(args) - rootCommand.Execute() + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--", "echo", "hello world") - var secrets []Secret + fmt.Printf("output: %v\n", output) - stringSecrets := commandOutput.String() - arraySecrets := strings.Split(stringSecrets, "\n") - - for idx, secret := range arraySecrets { - if idx == len(arraySecrets)-1 && secret == "" { - continue - } - - secretParts := strings.Split(secret, "=") - - if len(secretParts) != 2 { - t.Errorf("Error: secret at index %d is not formatted correctly", idx) - } - - newSecret := Secret{ - Key: secretParts[0], - Value: secretParts[1], - } - - // make sure the new secret key is at least one of the expected keys - if !slices.Contains(ALL_SECRET_KEYS, newSecret.Key) { - continue - } - - secrets = append(secrets, newSecret) + if err != nil { + t.Fatalf("error running CLI command: %v", err) } - expectedLength := len(DEV_SECRETS) + len(STAGING_SECRETS) - - assert.Len(t, secrets, expectedLength) - - for _, secret := range secrets { - assert.Contains(t, ALL_SECRET_KEYS, secret.Key) - assert.Contains(t, ALL_SECRET_VALUES, secret.Value) + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) } } -func RunCmdWithoutImportsAndWithRecursive(t *testing.T, authToken string, projectId string, envSlug string) { +func TestUniversalAuth_RunCmdRecursiveAndImports(t *testing.T) { + MachineIdentityLoginCmd(t) + SetupCli(t) - rootCommand := cmd.NewRootCmd() + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent", "--", "echo", "hello world") - commandOutput := new(bytes.Buffer) - errorOutput := new(bytes.Buffer) - rootCommand.SetOut(commandOutput) - rootCommand.SetErr(errorOutput) + fmt.Printf("output: %v\n", output) - args := []string{ - "run", + if err != nil { + t.Fatalf("error running CLI command: %v", err) } - args = append(args, fmt.Sprintf("--token=%s", authToken)) - args = append(args, fmt.Sprintf("--projectId=%s", projectId)) - args = append(args, fmt.Sprintf("--env=%s", envSlug)) - args = append(args, "--include-imports=false") - args = append(args, "--recursive") - args = append(args, "--", "echo", "TEST_COMMAND_BEING_EXECUTED_RECURSIVE") - - rootCommand.SetArgs(args) - rootCommand.Execute() - - var secrets []Secret - - stringSecrets := commandOutput.String() - arraySecrets := strings.Split(stringSecrets, "\n") - - for idx, secret := range arraySecrets { - if idx == len(arraySecrets)-1 && secret == "" { - continue - } - - secretParts := strings.Split(secret, "=") - - if len(secretParts) != 2 { - t.Errorf("Error: secret at index %d is not formatted correctly", idx) - } - - newSecret := Secret{ - Key: secretParts[0], - Value: secretParts[1], - } - - // make sure the new secret key is at least one of the expected keys - if !slices.Contains(ALL_SECRET_KEYS, newSecret.Key) { - continue - } - - secrets = append(secrets, newSecret) - } - - nestedDevSecrets := append(DEV_FOLDER_SECRETS, DEV_SECRETS...) - nestedDevSecretsKeys := Map(nestedDevSecrets, func(secret Secret) string { return secret.Key }) - nestedDevSecretsValues := Map(nestedDevSecrets, func(secret Secret) string { return secret.Value }) - - expectedLength := len(nestedDevSecrets) - assert.Len(t, secrets, expectedLength) - - for _, secret := range secrets { - assert.Contains(t, nestedDevSecretsKeys, secret.Key) - assert.Contains(t, nestedDevSecretsValues, secret.Value) + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestUniversalAuth_RunCmdWithImports(t *testing.T) { + MachineIdentityLoginCmd(t) + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--", "echo", "hello world") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestUniversalAuth_RunCmdWithoutImports(t *testing.T) { + MachineIdentityLoginCmd(t) + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--include-imports=false", "--", "echo", "hello world") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestServiceToken_RunCmdWithoutImports(t *testing.T) { + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--include-imports=false", "--", "echo", "hello world") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) } } From 1dd0167ac8ea6e3ef2aab9122ef79632e33b146e Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:12:18 +0200 Subject: [PATCH 31/45] Feat: CLI Integration Tests --- cli/test/secrets_by_name_test.go | 119 +++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 cli/test/secrets_by_name_test.go diff --git a/cli/test/secrets_by_name_test.go b/cli/test/secrets_by_name_test.go new file mode 100644 index 000000000..bd86749b6 --- /dev/null +++ b/cli/test/secrets_by_name_test.go @@ -0,0 +1,119 @@ +package tests + +import ( + "fmt" + "testing" + + "github.com/bradleyjkemp/cupaloy/v2" +) + +func TestServiceToken_GetSecretsByNameRecursive(t *testing.T) { + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "TEST-SECRET-2", "FOLDER-SECRET-1", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestServiceToken_GetSecretsByNameWithNotFoundSecret(t *testing.T) { + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "TEST-SECRET-2", "FOLDER-SECRET-1", "DOES-NOT-EXIST", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestServiceToken_GetSecretsByNameWithImports(t *testing.T) { + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "STAGING-SECRET-2", "FOLDER-SECRET-1", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestUniversalAuth_GetSecretsByNameRecursive(t *testing.T) { + MachineIdentityLoginCmd(t) + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "TEST-SECRET-2", "FOLDER-SECRET-1", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestUniversalAuth_GetSecretsByNameWithNotFoundSecret(t *testing.T) { + MachineIdentityLoginCmd(t) + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "TEST-SECRET-2", "FOLDER-SECRET-1", "DOES-NOT-EXIST", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestUniversalAuth_GetSecretsByNameWithImports(t *testing.T) { + MachineIdentityLoginCmd(t) + SetupCli(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "STAGING-SECRET-2", "FOLDER-SECRET-1", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} From a0aa06e2f5e0541ec00081997fcb950273397403 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:12:21 +0200 Subject: [PATCH 32/45] Fix: Refactor teests to use cupaloy --- cli/test/secrets_test.go | 147 +++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 74 deletions(-) diff --git a/cli/test/secrets_test.go b/cli/test/secrets_test.go index 39bfd1c07..aca5f806b 100644 --- a/cli/test/secrets_test.go +++ b/cli/test/secrets_test.go @@ -1,99 +1,98 @@ package tests import ( - "bytes" - "encoding/json" "fmt" "testing" - "github.com/Infisical/infisical-merge/packages/cmd" - "github.com/Infisical/infisical-merge/packages/models" - "github.com/stretchr/testify/assert" + "github.com/bradleyjkemp/cupaloy/v2" ) -func ListSecretsWithImportsAndRecursive(t *testing.T, authToken string, projectId string, envSlug string) { +func TestServiceToken_SecretsGetWithImportsAndRecursiveCmd(t *testing.T) { + SetupCli(t) - rootCommand := cmd.NewRootCmd() + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") - commandOutput := new(bytes.Buffer) - rootCommand.SetOut(commandOutput) - rootCommand.SetErr(commandOutput) + fmt.Printf("output: %v\n", output) - args := []string{ - "secrets", - } - args = append(args, fmt.Sprintf("--token=%s", authToken)) - args = append(args, fmt.Sprintf("--projectId=%s", projectId)) - args = append(args, fmt.Sprintf("--env=%s", envSlug)) - args = append(args, "--include-imports=true") - args = append(args, "--recursive=true") - - rootCommand.SetArgs(args) - rootCommand.Execute() - - var secrets []models.SingleEnvironmentVariable - - err := json.Unmarshal(commandOutput.Bytes(), &secrets) if err != nil { - t.Errorf("Error: %v", err) + t.Fatalf("error running CLI command: %v", err) } - if len(secrets) == 0 { - t.Errorf("No secrets found") - } - - secretKeys := []string{} - secretValues := []string{} - - for _, secret := range secrets { - secretKeys = append(secretKeys, secret.Key) - secretValues = append(secretValues, secret.Value) - } - - // Secrets can have different order and potentially more secrets. but the secrets should at least contain the above secrets. - for _, key := range ALL_SECRET_KEYS { - assert.Contains(t, secretKeys, key) - } - for _, value := range ALL_SECRET_VALUES { - assert.Contains(t, secretValues, value) + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) } } -func GetSecretsByNames(t *testing.T, authToken string, projectId string, envSlug string) { +func TestServiceToken_SecretsGetWithoutImportsAndWithoutRecursiveCmd(t *testing.T) { + SetupCli(t) - rootCommand := cmd.NewRootCmd() + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--include-imports=false", "--silent") - commandOutput := new(bytes.Buffer) - rootCommand.SetOut(commandOutput) - rootCommand.SetErr(commandOutput) + fmt.Printf("output: %v\n", output) - args := []string{ - "secrets", - "get", - } - - args = append(args, ALL_SECRET_KEYS...) - args = append(args, fmt.Sprintf("--token=%s", authToken)) - args = append(args, fmt.Sprintf("--projectId=%s", projectId)) - args = append(args, fmt.Sprintf("--env=%s", envSlug)) - - rootCommand.SetArgs(args) - rootCommand.Execute() - - var secrets []models.SingleEnvironmentVariable - - err := json.Unmarshal(commandOutput.Bytes(), &secrets) if err != nil { - t.Errorf("Error: %v", err) + t.Fatalf("error running CLI command: %v", err) } - assert.Len(t, secrets, len(ALL_SECRETS)) - - for _, secret := range secrets { - assert.Contains(t, ALL_SECRET_KEYS, secret.Key) - - if secret.Key == "FOLDER-SECRET-1" { - assert.Equal(t, secret.Value, "*not found*") // Should not be found because recursive isn't enabled in this test, and the default path is "/" - } + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) } } + +func TestUniversalAuth_SecretsGetWithImportsAndRecursiveCmd(t *testing.T) { + SetupCli(t) + MachineIdentityLoginCmd(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestUniversalAuth_SecretsGetWithoutImportsAndWithoutRecursiveCmd(t *testing.T) { + SetupCli(t) + MachineIdentityLoginCmd(t) + + output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--include-imports=false", "--silent") + + fmt.Printf("output: %v\n", output) + + if err != nil { + t.Fatalf("error running CLI command: %v", err) + } + + // Use cupaloy to snapshot test the output + err = cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } +} + +func TestUniversalAuth_SecretsGetWrongEnvironment(t *testing.T) { + SetupCli(t) + MachineIdentityLoginCmd(t) + + output, _ := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", "invalid-env", "--recursive", "--silent") + + fmt.Printf("output: %v\n", output) + + // Use cupaloy to snapshot test the output + err := cupaloy.Snapshot(output) + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + +} From 8c7d329f8f14b08403d0715815bef6f3a9cf59fa Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:18:23 +0200 Subject: [PATCH 33/45] Fix: Snapshot output order --- .../.snapshots/test-TestServiceToken_ExportSecretsWithImports | 4 ++-- .../test-TestServiceToken_RunCmdRecursiveAndImports | 2 +- cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports | 2 +- .../.snapshots/test-TestServiceToken_RunCmdWithoutImports | 2 +- .../test-TestUniversalAuth_ExportSecretsWithoutImports | 2 +- .../test-TestUniversalAuth_RunCmdRecursiveAndImports | 2 +- cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports | 2 +- .../.snapshots/test-TestUniversalAuth_RunCmdWithoutImports | 2 +- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports b/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports index 022400543..542fbdd45 100644 --- a/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports +++ b/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports @@ -1,5 +1,5 @@ +TEST-SECRET-2='test-value-2' +STAGING-SECRET-2='staging-value-2' STAGING-SECRET-1='staging-value-1' TEST-SECRET-1='test-value-1' TEST-SECRET-3='test-value-3' -TEST-SECRET-2='test-value-2' -STAGING-SECRET-2='staging-value-2' diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports index 7bf32be6c..8676c410c 100644 --- a/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports @@ -1,2 +1,2 @@ -2:53PM INF Injecting 6 Infisical secrets into your application process +3:16PM INF Injecting 6 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports index cc3d8ae47..fc4019ddd 100644 --- a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports @@ -1,2 +1,2 @@ -2:53PM INF Injecting 5 Infisical secrets into your application process +3:16PM INF Injecting 5 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports index 9e9929a52..89508e72f 100644 --- a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports @@ -1,2 +1,2 @@ -2:53PM INF Injecting 3 Infisical secrets into your application process +3:16PM INF Injecting 3 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports b/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports index 6cc3ca7c4..33462eb0b 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports @@ -1,3 +1,3 @@ -TEST-SECRET-1='test-value-1' TEST-SECRET-3='test-value-3' TEST-SECRET-2='test-value-2' +TEST-SECRET-1='test-value-1' diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports index 7bf32be6c..8676c410c 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports @@ -1,2 +1,2 @@ -2:53PM INF Injecting 6 Infisical secrets into your application process +3:16PM INF Injecting 6 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports index cc3d8ae47..fc4019ddd 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports @@ -1,2 +1,2 @@ -2:53PM INF Injecting 5 Infisical secrets into your application process +3:16PM INF Injecting 5 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports index 9e9929a52..89508e72f 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports @@ -1,2 +1,2 @@ -2:53PM INF Injecting 3 Infisical secrets into your application process +3:16PM INF Injecting 3 Infisical secrets into your application process hello world From 2ed9aa888eca7d59d48952b2f7300ecbf0037767 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:18:30 +0200 Subject: [PATCH 34/45] Fix: Secrets order --- cli/packages/cmd/export.go | 1 + 1 file changed, 1 insertion(+) diff --git a/cli/packages/cmd/export.go b/cli/packages/cmd/export.go index 26c7d0044..86e9d8e97 100644 --- a/cli/packages/cmd/export.go +++ b/cli/packages/cmd/export.go @@ -119,6 +119,7 @@ var exportCmd = &cobra.Command{ } secrets = util.FilterSecretsByTag(secrets, tagSlugs) + secrets = util.SortSecretsByKeys(secrets) util.HandleSendTestSecrets(cmd, secrets) output, err = formatEnvs(secrets, format) From ca0062f04962448783925e6690df9126d5e7101e Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:18:32 +0200 Subject: [PATCH 35/45] Update run-cli-tests.yml --- .github/workflows/run-cli-tests.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/run-cli-tests.yml b/.github/workflows/run-cli-tests.yml index 9cd5d1519..e3237a34e 100644 --- a/.github/workflows/run-cli-tests.yml +++ b/.github/workflows/run-cli-tests.yml @@ -23,8 +23,6 @@ jobs: go-version: "1.21.x" - name: Install dependencies run: go get . - - name: Build - run: go build -v ./... - name: Test with the Go CLI env: CLI_TESTS_UA_CLIENT_ID: ${{ secrets.CLI_TESTS_UA_CLIENT_ID }} From 144a5636093b4b72ca896e97bb3b4aea3525c306 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:21:19 +0200 Subject: [PATCH 36/45] Fix: Fixed snapshots order --- .../test-TestServiceToken_ExportSecretsWithImports | 4 ++-- .../test-TestServiceToken_ExportSecretsWithoutImports | 2 +- .../test-TestServiceToken_RunCmdRecursiveAndImports | 2 +- .../.snapshots/test-TestServiceToken_RunCmdWithImports | 2 +- .../.snapshots/test-TestServiceToken_RunCmdWithoutImports | 2 +- .../test-TestUniversalAuth_ExportSecretsWithImports | 8 ++++---- .../test-TestUniversalAuth_ExportSecretsWithoutImports | 4 ++-- .../test-TestUniversalAuth_RunCmdRecursiveAndImports | 2 +- .../.snapshots/test-TestUniversalAuth_RunCmdWithImports | 2 +- .../test-TestUniversalAuth_RunCmdWithoutImports | 2 +- 10 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports b/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports index 542fbdd45..679cd91ad 100644 --- a/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports +++ b/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithImports @@ -1,5 +1,5 @@ -TEST-SECRET-2='test-value-2' -STAGING-SECRET-2='staging-value-2' STAGING-SECRET-1='staging-value-1' +STAGING-SECRET-2='staging-value-2' TEST-SECRET-1='test-value-1' +TEST-SECRET-2='test-value-2' TEST-SECRET-3='test-value-3' diff --git a/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithoutImports b/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithoutImports index 6cc3ca7c4..c803e591e 100644 --- a/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithoutImports +++ b/cli/test/.snapshots/test-TestServiceToken_ExportSecretsWithoutImports @@ -1,3 +1,3 @@ TEST-SECRET-1='test-value-1' -TEST-SECRET-3='test-value-3' TEST-SECRET-2='test-value-2' +TEST-SECRET-3='test-value-3' diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports index 8676c410c..0040acd4e 100644 --- a/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports @@ -1,2 +1,2 @@ -3:16PM INF Injecting 6 Infisical secrets into your application process +3:20PM INF Injecting 6 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports index fc4019ddd..9bd775eaa 100644 --- a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports @@ -1,2 +1,2 @@ -3:16PM INF Injecting 5 Infisical secrets into your application process +3:20PM INF Injecting 5 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports index 89508e72f..c54b77867 100644 --- a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports @@ -1,2 +1,2 @@ -3:16PM INF Injecting 3 Infisical secrets into your application process +3:20PM INF Injecting 3 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithImports b/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithImports index 1b59a0f22..679cd91ad 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithImports @@ -1,5 +1,5 @@ -TEST-SECRET-1='test-value-1' -TEST-SECRET-3='test-value-3' -TEST-SECRET-2='test-value-2' -STAGING-SECRET-2='staging-value-2' STAGING-SECRET-1='staging-value-1' +STAGING-SECRET-2='staging-value-2' +TEST-SECRET-1='test-value-1' +TEST-SECRET-2='test-value-2' +TEST-SECRET-3='test-value-3' diff --git a/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports b/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports index 33462eb0b..c803e591e 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_ExportSecretsWithoutImports @@ -1,3 +1,3 @@ -TEST-SECRET-3='test-value-3' -TEST-SECRET-2='test-value-2' TEST-SECRET-1='test-value-1' +TEST-SECRET-2='test-value-2' +TEST-SECRET-3='test-value-3' diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports index 8676c410c..0040acd4e 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports @@ -1,2 +1,2 @@ -3:16PM INF Injecting 6 Infisical secrets into your application process +3:20PM INF Injecting 6 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports index fc4019ddd..9bd775eaa 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports @@ -1,2 +1,2 @@ -3:16PM INF Injecting 5 Infisical secrets into your application process +3:20PM INF Injecting 5 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports index 89508e72f..c54b77867 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports @@ -1,2 +1,2 @@ -3:16PM INF Injecting 3 Infisical secrets into your application process +3:20PM INF Injecting 3 Infisical secrets into your application process hello world From 9cf1dd38a69087df4cc81dd75059abbeff86b35d Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:27:22 +0200 Subject: [PATCH 37/45] Fix: Run CMD snapshot fix --- .../.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports | 2 +- cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports | 2 +- cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports | 2 +- .../.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports | 2 +- cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports | 2 +- cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports index 0040acd4e..4106549cd 100644 --- a/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdRecursiveAndImports @@ -1,2 +1,2 @@ -3:20PM INF Injecting 6 Infisical secrets into your application process + Injecting 6 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports index 9bd775eaa..86475d3e5 100644 --- a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithImports @@ -1,2 +1,2 @@ -3:20PM INF Injecting 5 Infisical secrets into your application process + Injecting 5 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports index c54b77867..dfe56064a 100644 --- a/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports +++ b/cli/test/.snapshots/test-TestServiceToken_RunCmdWithoutImports @@ -1,2 +1,2 @@ -3:20PM INF Injecting 3 Infisical secrets into your application process + Injecting 3 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports index 0040acd4e..4106549cd 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdRecursiveAndImports @@ -1,2 +1,2 @@ -3:20PM INF Injecting 6 Infisical secrets into your application process + Injecting 6 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports index 9bd775eaa..86475d3e5 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithImports @@ -1,2 +1,2 @@ -3:20PM INF Injecting 5 Infisical secrets into your application process + Injecting 5 Infisical secrets into your application process hello world diff --git a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports index c54b77867..dfe56064a 100644 --- a/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports +++ b/cli/test/.snapshots/test-TestUniversalAuth_RunCmdWithoutImports @@ -1,2 +1,2 @@ -3:20PM INF Injecting 3 Infisical secrets into your application process + Injecting 3 Infisical secrets into your application process hello world From eb5f71cb058aefb7b91f3d9bdf334de537d005b8 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:27:35 +0200 Subject: [PATCH 38/45] Chore: Disable build as the tests handle this automatically --- .github/workflows/run-cli-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/run-cli-tests.yml b/.github/workflows/run-cli-tests.yml index e3237a34e..846998528 100644 --- a/.github/workflows/run-cli-tests.yml +++ b/.github/workflows/run-cli-tests.yml @@ -31,4 +31,4 @@ jobs: CLI_TESTS_PROJECT_ID: ${{ secrets.CLI_TESTS_PROJECT_ID }} CLI_TESTS_ENV_SLUG: ${{ secrets.CLI_TESTS_ENV_SLUG }} - run: TEST_MODE=true go test -v -count=1 ./test + run: go test -v -count=1 ./test From 35071af4783c3d75e66520a738d254ddbf017b47 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:27:42 +0200 Subject: [PATCH 39/45] Fix: Run cmd tests --- cli/test/run_test.go | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/cli/test/run_test.go b/cli/test/run_test.go index b9518b835..76dde0c72 100644 --- a/cli/test/run_test.go +++ b/cli/test/run_test.go @@ -1,6 +1,7 @@ package tests import ( + "bytes" "fmt" "testing" @@ -12,12 +13,13 @@ func TestServiceToken_RunCmdRecursiveAndImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent", "--", "echo", "hello world") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } + output = string(bytes.Split([]byte(output), []byte("INF"))[1]) + fmt.Printf("output: %v\n", output) + // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) if err != nil { @@ -29,12 +31,13 @@ func TestServiceToken_RunCmdWithImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--", "echo", "hello world") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } + output = string(bytes.Split([]byte(output), []byte("INF"))[1]) + fmt.Printf("output: %v\n", output) + // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) if err != nil { @@ -48,12 +51,13 @@ func TestUniversalAuth_RunCmdRecursiveAndImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent", "--", "echo", "hello world") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } + output = string(bytes.Split([]byte(output), []byte("INF"))[1]) + fmt.Printf("output: %v\n", output) + // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) if err != nil { @@ -67,12 +71,14 @@ func TestUniversalAuth_RunCmdWithImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--", "echo", "hello world") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } + // remove the first few characters from the output because we don't care about the time, and it will change every time + output = string(bytes.Split([]byte(output), []byte("INF"))[1]) + fmt.Printf("output: %v\n", output) + // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) if err != nil { @@ -86,12 +92,13 @@ func TestUniversalAuth_RunCmdWithoutImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--include-imports=false", "--", "echo", "hello world") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } + output = string(bytes.Split([]byte(output), []byte("INF"))[1]) + fmt.Printf("output: %v\n", output) + // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) if err != nil { @@ -104,12 +111,14 @@ func TestServiceToken_RunCmdWithoutImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "run", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--include-imports=false", "--", "echo", "hello world") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } + // Remove everything before "INF" because it's not relevant to the test + output = string(bytes.Split([]byte(output), []byte("INF"))[1]) + fmt.Printf("output: %v\n", output) + // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) if err != nil { From 83107f56bb1a15cfde19f071e1dd12e6623d193b Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:59:16 +0200 Subject: [PATCH 40/45] Fix: Removed old test code --- cli/packages/cmd/export.go | 1 - cli/packages/cmd/login.go | 4 --- cli/packages/cmd/run.go | 7 ++---- cli/packages/cmd/secrets.go | 3 --- cli/packages/util/tests.go | 50 ------------------------------------- 5 files changed, 2 insertions(+), 63 deletions(-) delete mode 100644 cli/packages/util/tests.go diff --git a/cli/packages/cmd/export.go b/cli/packages/cmd/export.go index 86e9d8e97..4e927ed59 100644 --- a/cli/packages/cmd/export.go +++ b/cli/packages/cmd/export.go @@ -120,7 +120,6 @@ var exportCmd = &cobra.Command{ secrets = util.FilterSecretsByTag(secrets, tagSlugs) secrets = util.SortSecretsByKeys(secrets) - util.HandleSendTestSecrets(cmd, secrets) output, err = formatEnvs(secrets, format) if err != nil { diff --git a/cli/packages/cmd/login.go b/cli/packages/cmd/login.go index a6401e3d0..bbb2c3a05 100644 --- a/cli/packages/cmd/login.go +++ b/cli/packages/cmd/login.go @@ -191,10 +191,6 @@ var loginCmd = &cobra.Command{ util.HandleError(err) } - if util.IS_TEST_MODE { - util.HandleSendUniversalAuthToken(cmd, res.AccessToken) - return - } if plainOutput { fmt.Println(res.AccessToken) return diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index dc123235a..f137a146d 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -174,7 +174,6 @@ var runCmd = &cobra.Command{ } log.Debug().Msgf("injecting the following environment variables into shell: %v", env) - util.HandleSendTestEnvVars(cmd, env) Telemetry.CaptureEvent("cli-command:run", posthog.NewProperties(). @@ -311,10 +310,8 @@ func execCmd(cmd *exec.Cmd) error { return fmt.Errorf("failed to wait for command termination: %v", err) } - if !util.IS_TEST_MODE { - waitStatus := cmd.ProcessState.Sys().(syscall.WaitStatus) - os.Exit(waitStatus.ExitStatus()) - } + waitStatus := cmd.ProcessState.Sys().(syscall.WaitStatus) + os.Exit(waitStatus.ExitStatus()) return nil } diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index b333931a3..80b1d0292 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -119,7 +119,6 @@ var secretsCmd = &cobra.Command{ // convert secrets to the same order as the keys secrets = util.SortSecretsByKeys(secrets) - util.HandleSendTestSecrets(cmd, secrets) visualize.PrintAllSecretDetails(secrets) Telemetry.CaptureEvent("cli-command:secrets", posthog.NewProperties().Set("secretCount", len(secrets)).Set("version", util.CLI_VERSION)) }, @@ -509,8 +508,6 @@ func getSecretsByNames(cmd *cobra.Command, args []string) { } } - util.HandleSendTestSecrets(cmd, requestedSecrets) - if showOnlyValue && len(requestedSecrets) > 1 { util.PrintErrorMessageAndExit("--raw-value only works with one secret.") } diff --git a/cli/packages/util/tests.go b/cli/packages/util/tests.go deleted file mode 100644 index 989376e58..000000000 --- a/cli/packages/util/tests.go +++ /dev/null @@ -1,50 +0,0 @@ -package util - -import ( - "encoding/json" - "fmt" - "os" - - "github.com/Infisical/infisical-merge/packages/models" - "github.com/spf13/cobra" -) - -var IS_TEST_MODE = os.Getenv("TEST_MODE") == "true" - -func HandleSendTestSecrets(cmd *cobra.Command, secrets []models.SingleEnvironmentVariable) { - - if !IS_TEST_MODE { - return - } - - jsonOut, err := json.Marshal(secrets) - if err != nil { - HandleError(err, "Unable to marshal secrets") - } - - fmt.Fprint(cmd.OutOrStdout(), string(jsonOut)) -} - -func HandleSendTestEnvVars(cmd *cobra.Command, envs []string) { - - if !IS_TEST_MODE { - return - } - - stringEnvVars := "" - - for _, env := range envs { - stringEnvVars += env + "\n" - } - - fmt.Fprint(cmd.OutOrStdout(), string(stringEnvVars)) -} - -func HandleSendUniversalAuthToken(cmd *cobra.Command, token string) { - - if !IS_TEST_MODE { - return - } - - fmt.Fprint(cmd.OutOrStdout(), token) -} From 34543ef1279e3d2b8a70d96237d398a6543376d7 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 15:59:54 +0200 Subject: [PATCH 41/45] Fix: Removed old code --- cli/packages/cmd/root.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cli/packages/cmd/root.go b/cli/packages/cmd/root.go index 7fe172c07..06846260f 100644 --- a/cli/packages/cmd/root.go +++ b/cli/packages/cmd/root.go @@ -87,7 +87,3 @@ func initLog() { zerolog.SetGlobalLevel(zerolog.InfoLevel) } } - -func NewRootCmd() *cobra.Command { - return rootCmd -} From e008fb26a27ba38b7adeb84e930f62b37c606da1 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 16:02:16 +0200 Subject: [PATCH 42/45] Cleanup --- cli/packages/cmd/run.go | 1 - cli/packages/cmd/secrets.go | 2 +- cli/packages/util/helper.go | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index f137a146d..04fe2588b 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -312,6 +312,5 @@ func execCmd(cmd *exec.Cmd) error { waitStatus := cmd.ProcessState.Sys().(syscall.WaitStatus) os.Exit(waitStatus.ExitStatus()) - return nil } diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index 80b1d0292..f2f694075 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -116,7 +116,7 @@ var secretsCmd = &cobra.Command{ secrets = util.ExpandSecrets(secrets, authParams, "") } - // convert secrets to the same order as the keys + // Sort the secrets by key so we can create a consistent output secrets = util.SortSecretsByKeys(secrets) visualize.PrintAllSecretDetails(secrets) diff --git a/cli/packages/util/helper.go b/cli/packages/util/helper.go index 33e22e040..5ffe5c9f1 100644 --- a/cli/packages/util/helper.go +++ b/cli/packages/util/helper.go @@ -53,8 +53,8 @@ func GetBase64DecodedSymmetricEncryptionDetails(key string, cipher string, IV st }, nil } +// Helper function to sort the secrets by key so we can create a consistent output func SortSecretsByKeys(secrets []models.SingleEnvironmentVariable) []models.SingleEnvironmentVariable { - // sort secrets by key for i := 0; i < len(secrets); i++ { for j := i + 1; j < len(secrets); j++ { if secrets[i].Key > secrets[j].Key { From 973ed370181d86810755884d96bfe66929d9557d Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 19:50:15 +0200 Subject: [PATCH 43/45] Update export.go --- cli/packages/cmd/export.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/packages/cmd/export.go b/cli/packages/cmd/export.go index 4e927ed59..423ed5435 100644 --- a/cli/packages/cmd/export.go +++ b/cli/packages/cmd/export.go @@ -117,7 +117,6 @@ var exportCmd = &cobra.Command{ secrets = util.ExpandSecrets(secrets, authParams, "") } - secrets = util.FilterSecretsByTag(secrets, tagSlugs) secrets = util.SortSecretsByKeys(secrets) From 519d6f98a246fbd39dbf98c34b7e4cdfe64f3bba Mon Sep 17 00:00:00 2001 From: Daniel Hougaard <62331820+DanielHougaard@users.noreply.github.com> Date: Mon, 22 Apr 2024 19:50:24 +0200 Subject: [PATCH 44/45] Chore: Use standard lib --- cli/packages/util/helper.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/cli/packages/util/helper.go b/cli/packages/util/helper.go index 5ffe5c9f1..9a4d960db 100644 --- a/cli/packages/util/helper.go +++ b/cli/packages/util/helper.go @@ -8,6 +8,7 @@ import ( "os" "os/exec" "path" + "sort" "strings" "time" @@ -55,14 +56,9 @@ func GetBase64DecodedSymmetricEncryptionDetails(key string, cipher string, IV st // Helper function to sort the secrets by key so we can create a consistent output func SortSecretsByKeys(secrets []models.SingleEnvironmentVariable) []models.SingleEnvironmentVariable { - for i := 0; i < len(secrets); i++ { - for j := i + 1; j < len(secrets); j++ { - if secrets[i].Key > secrets[j].Key { - secrets[i], secrets[j] = secrets[j], secrets[i] - } - } - } - + sort.Slice(secrets, func(i, j int) bool { + return secrets[i].Key < secrets[j].Key + }) return secrets } From de21b444864a528b9727ca803dc30fd2fe64535e Mon Sep 17 00:00:00 2001 From: Maidul Islam Date: Mon, 22 Apr 2024 14:36:33 -0400 Subject: [PATCH 45/45] small nits --- cli/test/export_test.go | 9 --------- cli/test/run_test.go | 7 ------- cli/test/secrets_by_name_test.go | 13 ------------- cli/test/secrets_test.go | 11 ----------- 4 files changed, 40 deletions(-) diff --git a/cli/test/export_test.go b/cli/test/export_test.go index d3069701b..9a936871d 100644 --- a/cli/test/export_test.go +++ b/cli/test/export_test.go @@ -1,7 +1,6 @@ package tests import ( - "fmt" "testing" "github.com/bradleyjkemp/cupaloy/v2" @@ -13,8 +12,6 @@ func TestUniversalAuth_ExportSecretsWithImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "export", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -31,8 +28,6 @@ func TestServiceToken_ExportSecretsWithImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "export", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -50,8 +45,6 @@ func TestUniversalAuth_ExportSecretsWithoutImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "export", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--include-imports=false") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -68,8 +61,6 @@ func TestServiceToken_ExportSecretsWithoutImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "export", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--silent", "--include-imports=false") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } diff --git a/cli/test/run_test.go b/cli/test/run_test.go index 76dde0c72..808f4f14f 100644 --- a/cli/test/run_test.go +++ b/cli/test/run_test.go @@ -2,7 +2,6 @@ package tests import ( "bytes" - "fmt" "testing" "github.com/bradleyjkemp/cupaloy/v2" @@ -18,7 +17,6 @@ func TestServiceToken_RunCmdRecursiveAndImports(t *testing.T) { } output = string(bytes.Split([]byte(output), []byte("INF"))[1]) - fmt.Printf("output: %v\n", output) // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) @@ -36,7 +34,6 @@ func TestServiceToken_RunCmdWithImports(t *testing.T) { } output = string(bytes.Split([]byte(output), []byte("INF"))[1]) - fmt.Printf("output: %v\n", output) // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) @@ -56,7 +53,6 @@ func TestUniversalAuth_RunCmdRecursiveAndImports(t *testing.T) { } output = string(bytes.Split([]byte(output), []byte("INF"))[1]) - fmt.Printf("output: %v\n", output) // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) @@ -77,7 +73,6 @@ func TestUniversalAuth_RunCmdWithImports(t *testing.T) { // remove the first few characters from the output because we don't care about the time, and it will change every time output = string(bytes.Split([]byte(output), []byte("INF"))[1]) - fmt.Printf("output: %v\n", output) // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) @@ -97,7 +92,6 @@ func TestUniversalAuth_RunCmdWithoutImports(t *testing.T) { } output = string(bytes.Split([]byte(output), []byte("INF"))[1]) - fmt.Printf("output: %v\n", output) // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) @@ -117,7 +111,6 @@ func TestServiceToken_RunCmdWithoutImports(t *testing.T) { // Remove everything before "INF" because it's not relevant to the test output = string(bytes.Split([]byte(output), []byte("INF"))[1]) - fmt.Printf("output: %v\n", output) // Use cupaloy to snapshot test the output err = cupaloy.Snapshot(output) diff --git a/cli/test/secrets_by_name_test.go b/cli/test/secrets_by_name_test.go index bd86749b6..440324e1a 100644 --- a/cli/test/secrets_by_name_test.go +++ b/cli/test/secrets_by_name_test.go @@ -1,7 +1,6 @@ package tests import ( - "fmt" "testing" "github.com/bradleyjkemp/cupaloy/v2" @@ -12,8 +11,6 @@ func TestServiceToken_GetSecretsByNameRecursive(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "TEST-SECRET-2", "FOLDER-SECRET-1", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -30,8 +27,6 @@ func TestServiceToken_GetSecretsByNameWithNotFoundSecret(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "TEST-SECRET-2", "FOLDER-SECRET-1", "DOES-NOT-EXIST", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -48,8 +43,6 @@ func TestServiceToken_GetSecretsByNameWithImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "STAGING-SECRET-2", "FOLDER-SECRET-1", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -67,8 +60,6 @@ func TestUniversalAuth_GetSecretsByNameRecursive(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "TEST-SECRET-2", "FOLDER-SECRET-1", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -86,8 +77,6 @@ func TestUniversalAuth_GetSecretsByNameWithNotFoundSecret(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "TEST-SECRET-2", "FOLDER-SECRET-1", "DOES-NOT-EXIST", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -105,8 +94,6 @@ func TestUniversalAuth_GetSecretsByNameWithImports(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "get", "TEST-SECRET-1", "STAGING-SECRET-2", "FOLDER-SECRET-1", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } diff --git a/cli/test/secrets_test.go b/cli/test/secrets_test.go index aca5f806b..453666406 100644 --- a/cli/test/secrets_test.go +++ b/cli/test/secrets_test.go @@ -1,7 +1,6 @@ package tests import ( - "fmt" "testing" "github.com/bradleyjkemp/cupaloy/v2" @@ -12,8 +11,6 @@ func TestServiceToken_SecretsGetWithImportsAndRecursiveCmd(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -30,8 +27,6 @@ func TestServiceToken_SecretsGetWithoutImportsAndWithoutRecursiveCmd(t *testing. output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.ServiceToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--include-imports=false", "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -49,8 +44,6 @@ func TestUniversalAuth_SecretsGetWithImportsAndRecursiveCmd(t *testing.T) { output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--recursive", "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -68,8 +61,6 @@ func TestUniversalAuth_SecretsGetWithoutImportsAndWithoutRecursiveCmd(t *testing output, err := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", creds.EnvSlug, "--include-imports=false", "--silent") - fmt.Printf("output: %v\n", output) - if err != nil { t.Fatalf("error running CLI command: %v", err) } @@ -87,8 +78,6 @@ func TestUniversalAuth_SecretsGetWrongEnvironment(t *testing.T) { output, _ := ExecuteCliCommand(FORMATTED_CLI_NAME, "secrets", "--token", creds.UAAccessToken, "--projectId", creds.ProjectID, "--env", "invalid-env", "--recursive", "--silent") - fmt.Printf("output: %v\n", output) - // Use cupaloy to snapshot test the output err := cupaloy.Snapshot(output) if err != nil {