From ce60e96008d7c70ef53116276279e07467c57b28 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Wed, 12 Mar 2025 12:39:20 -0700 Subject: [PATCH 01/22] chore(nix): add golang dependency --- flake.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/flake.nix b/flake.nix index 094cfedc6..db99998f1 100644 --- a/flake.nix +++ b/flake.nix @@ -14,6 +14,7 @@ git lazygit + go python312Full nodejs_20 nodePackages.prettier From c4e10df754e8e19b754570d5251e15a5559065ae Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Wed, 12 Mar 2025 12:48:50 -0700 Subject: [PATCH 02/22] fix(nix): set the goroot for tools like jetbrains JetBrains needs to know the GOROOT environment variables. For the sake of other tooling, we will just set these in the flake rather than only in the `.envrc` file. It also keeps all environment configuration localized to our project flake. --- flake.nix | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/flake.nix b/flake.nix index db99998f1..198da71fe 100644 --- a/flake.nix +++ b/flake.nix @@ -20,6 +20,11 @@ nodePackages.prettier infisical ]; + + env = { + GOROOT = "${pkgs.go}/share/go"; + GOPATH = "$(pwd)/.go"; + }; }; }; } From 82e950428503b8d889765e2fa45401a0b7b79026 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:08:55 -0700 Subject: [PATCH 03/22] chore: ignore .idea and .go folders --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index f2a23324b..d793d5736 100644 --- a/.gitignore +++ b/.gitignore @@ -63,10 +63,12 @@ yarn-error.log* # Editor specific .vscode/* -.idea/* +**/.idea/* frontend-build +# cli +.go/ *.tgz cli/infisical-merge cli/test/infisical-merge From 8924eaf251fbb6d08da253cf5fe0e8112f852511 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:09:38 -0700 Subject: [PATCH 04/22] chore: ignore direnv folder --- .gitignore | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index d793d5736..f21c85c41 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.direnv/ + # backend node_modules .env @@ -26,8 +28,6 @@ node_modules /.pnp .pnp.js -.env - # testing coverage reports From 7600a86dfca88f0ab06d0cd92dcdc860bcfddb34 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:09:57 -0700 Subject: [PATCH 05/22] fix(nix): set gopath for usage by IDEs --- flake.nix | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/flake.nix b/flake.nix index 198da71fe..cc4cf8d65 100644 --- a/flake.nix +++ b/flake.nix @@ -23,8 +23,12 @@ env = { GOROOT = "${pkgs.go}/share/go"; - GOPATH = "$(pwd)/.go"; }; + + shellHook = '' + export GOPATH="$(pwd)/.go" + mkdir -p "$GOPATH" + ''; }; }; } From c04b97c689d86069b008687d22322ae52a8b9a61 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:11:12 -0700 Subject: [PATCH 06/22] refactor: clean smelly code --- cli/packages/cmd/run.go | 1 - cli/packages/util/secrets.go | 35 +++++++++++++++++------------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 7f11a3f95..9351173bc 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -172,7 +172,6 @@ var runCmd = &cobra.Command{ } } } - }, } diff --git a/cli/packages/util/secrets.go b/cli/packages/util/secrets.go index 5a2a0ec24..54a4e5c14 100644 --- a/cli/packages/util/secrets.go +++ b/cli/packages/util/secrets.go @@ -101,7 +101,7 @@ func GetPlainTextSecretsV3(accessToken string, workspaceId string, environmentNa return models.PlaintextSecretResult{}, err } - plainTextSecrets := []models.SingleEnvironmentVariable{} + var plainTextSecrets []models.SingleEnvironmentVariable for _, secret := range rawSecrets.Secrets { plainTextSecrets = append(plainTextSecrets, models.SingleEnvironmentVariable{Key: secret.SecretKey, Value: secret.SecretValue, Type: secret.Type, WorkspaceId: secret.Workspace, SecretPath: secret.SecretPath}) @@ -292,7 +292,7 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo if err != nil { return nil, err } - WriteBackupSecrets(infisicalDotJson.WorkspaceId, params.Environment, params.SecretsPath, backupEncryptionKey, res.Secrets) + _ = WriteBackupSecrets(infisicalDotJson.WorkspaceId, params.Environment, params.SecretsPath, backupEncryptionKey, res.Secrets) } secretsToReturn = res.Secrets @@ -309,23 +309,22 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo } } } - - } else { - if params.InfisicalToken != "" { - log.Debug().Msg("Trying to fetch secrets using service token") - secretsToReturn, errorToReturn = GetPlainTextSecretsViaServiceToken(params.InfisicalToken, params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, params.ExpandSecretReferences) - } else if params.UniversalAuthAccessToken != "" { - - if params.WorkspaceId == "" { - PrintErrorMessageAndExit("Project ID is required when using machine identity") - } - - log.Debug().Msg("Trying to fetch secrets using universal auth") - res, err := GetPlainTextSecretsV3(params.UniversalAuthAccessToken, params.WorkspaceId, params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, params.ExpandSecretReferences) - - errorToReturn = err - secretsToReturn = res.Secrets + } else if params.InfisicalToken != "" { + log.Debug().Msg("Trying to fetch secrets using service token") + secretsToReturn, errorToReturn = GetPlainTextSecretsViaServiceToken(params.InfisicalToken, params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, params.ExpandSecretReferences) + } else if params.UniversalAuthAccessToken != "" { + if params.WorkspaceId == "" { + PrintErrorMessageAndExit("Project ID is required when using machine identity") } + + log.Debug().Msg("Trying to fetch secrets using universal auth") + res, err := GetPlainTextSecretsV3(params.UniversalAuthAccessToken, params.WorkspaceId, params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, params.ExpandSecretReferences) + if err != nil { + return nil, err + } + + errorToReturn = err + secretsToReturn = res.Secrets } return secretsToReturn, errorToReturn From 2ecc7424d98c2d234f9e589158dace91fe9524cf Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:12:24 -0700 Subject: [PATCH 07/22] feat(models): add model for environments --- cli/packages/api/model.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/cli/packages/api/model.go b/cli/packages/api/model.go index 72dbbc97b..1eb936f24 100644 --- a/cli/packages/api/model.go +++ b/cli/packages/api/model.go @@ -2,6 +2,12 @@ package api import "time" +type Environment struct { + Name string `json:"name"` + Slug string `json:"slug"` + ID string `json:"id"` +} + // Stores info for login one type LoginOneRequest struct { Email string `json:"email"` @@ -14,7 +20,6 @@ type LoginOneResponse struct { } // Stores info for login two - type LoginTwoRequest struct { Email string `json:"email"` ClientProof string `json:"clientProof"` @@ -168,9 +173,10 @@ type Secret struct { } type Project struct { - ID string `json:"id"` - Name string `json:"name"` - Slug string `json:"slug"` + ID string `json:"id"` + Name string `json:"name"` + Slug string `json:"slug"` + Environments []Environment `json:"environments"` } type RawSecret struct { From d0c9f1ca5321c0f3fc58cac49ff41ed31f1d805c Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:14:12 -0700 Subject: [PATCH 08/22] feat(projects): add new module in util package for getting project details --- cli/packages/util/projects.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 cli/packages/util/projects.go diff --git a/cli/packages/util/projects.go b/cli/packages/util/projects.go new file mode 100644 index 000000000..c88e88fe7 --- /dev/null +++ b/cli/packages/util/projects.go @@ -0,0 +1,19 @@ +package util + +import ( + "github.com/Infisical/infisical-merge/packages/api" + "github.com/go-resty/resty/v2" +) + +func GetProjectDetails(accessToken string, workspaceId string) (api.Project, error) { + httpClient := resty.New() + httpClient.SetAuthToken(accessToken). + SetHeader("Accept", "application/json") + + res, err := api.CallGetProjectById(httpClient, workspaceId) + if err != nil { + return api.Project{}, err + } + + return res, nil +} From cf9d8035bd1ff28f60335339cb639fcf22082cf8 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:14:44 -0700 Subject: [PATCH 09/22] feat(run): add function to confirm project has the requested environment --- cli/packages/cmd/run.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 9351173bc..d2a631e1b 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -438,8 +438,21 @@ func executeCommandWithWatchMode(commandFlag string, args []string, watchModeInt } } -func fetchAndFormatSecretsForShell(request models.GetAllSecretsParameters, projectConfigDir string, secretOverriding bool, token *models.TokenDetails) (models.InjectableEnvironmentResult, error) { +func confirmProjectHasEnvironment(environmentName, workspaceId, accessToken string) (bool, error) { + res, err := util.GetProjectDetails(accessToken, workspaceId) + if err != nil { + return false, err + } + for _, env := range res.Environments { + if env.Name == environmentName { + return true, nil + } + } + return false, nil +} + +func fetchAndFormatSecretsForShell(request models.GetAllSecretsParameters, projectConfigDir string, secretOverriding bool, token *models.TokenDetails) (models.InjectableEnvironmentResult, error) { if token != nil && token.Type == util.SERVICE_TOKEN_IDENTIFIER { request.InfisicalToken = token.Token } else if token != nil && token.Type == util.UNIVERSAL_AUTH_TOKEN_IDENTIFIER { From 62aa80a1041cfb5c46206503ac6f09b6ba166bb9 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:14:57 -0700 Subject: [PATCH 10/22] feat(run): ensure that the project has the requested environment --- cli/packages/cmd/run.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index d2a631e1b..bb23edf32 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -136,6 +136,18 @@ var runCmd = &cobra.Command{ util.HandleError(err, "Unable to parse flag") } + log.Debug().Msgf("Confirming selected environment is valid: %s", environmentName) + + hasEnvironment, err := confirmProjectHasEnvironment(projectId, environmentName, token.Token) + if err != nil { + util.HandleError(err, "Could not confirm project has environment") + } + if !hasEnvironment { + util.HandleError(fmt.Errorf("project '%s' does not have environment '%s'", projectId, environmentName)) + } + + log.Debug().Msgf("Project '%s' has environment '%s'", projectId, environmentName) + request := models.GetAllSecretsParameters{ Environment: environmentName, WorkspaceId: projectId, From fdfc020efc4d9d5e1fd38657840b7e2f856e579a Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:30:03 -0700 Subject: [PATCH 11/22] refactor: clean up more smelly code --- cli/packages/cmd/run.go | 1 - cli/packages/util/secrets.go | 1 - 2 files changed, 2 deletions(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index bb23edf32..31e03d367 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -319,7 +319,6 @@ func waitForExitCommand(cmd *exec.Cmd) (int, error) { } func executeCommandWithWatchMode(commandFlag string, args []string, watchModeInterval int, request models.GetAllSecretsParameters, projectConfigDir string, secretOverriding bool, token *models.TokenDetails) { - var cmd *exec.Cmd var err error var lastSecretsFetch time.Time diff --git a/cli/packages/util/secrets.go b/cli/packages/util/secrets.go index 54a4e5c14..2c53a478a 100644 --- a/cli/packages/util/secrets.go +++ b/cli/packages/util/secrets.go @@ -232,7 +232,6 @@ func FilterSecretsByTag(plainTextSecrets []models.SingleEnvironmentVariable, tag func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectConfigFilePath string) ([]models.SingleEnvironmentVariable, error) { var secretsToReturn []models.SingleEnvironmentVariable - // var serviceTokenDetails api.GetServiceTokenDetailsResponse var errorToReturn error if params.InfisicalToken == "" && params.UniversalAuthAccessToken == "" { From aecfa268aed0ab8c4dd36444be94b0c530c3df40 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:30:19 -0700 Subject: [PATCH 12/22] fix(run): handle case where we require a login --- cli/packages/cmd/run.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 31e03d367..e75f5eee4 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -138,7 +138,7 @@ var runCmd = &cobra.Command{ log.Debug().Msgf("Confirming selected environment is valid: %s", environmentName) - hasEnvironment, err := confirmProjectHasEnvironment(projectId, environmentName, token.Token) + hasEnvironment, err := confirmProjectHasEnvironment(projectId, environmentName, token) if err != nil { util.HandleError(err, "Could not confirm project has environment") } @@ -449,13 +449,32 @@ func executeCommandWithWatchMode(commandFlag string, args []string, watchModeInt } } -func confirmProjectHasEnvironment(environmentName, workspaceId, accessToken string) (bool, error) { - res, err := util.GetProjectDetails(accessToken, workspaceId) +func confirmProjectHasEnvironment(environmentName, workspaceId string, token *models.TokenDetails) (bool, error) { + var accessToken string + + if token != nil && (token.Type == util.SERVICE_TOKEN_IDENTIFIER || token.Type == util.UNIVERSAL_AUTH_TOKEN_IDENTIFIER) { + accessToken = token.Token + } else { + util.RequireLogin() + util.RequireLocalWorkspaceFile() + + loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) + if err != nil { + util.HandleError(err, "Unable to authenticate") + } + + if loggedInUserDetails.LoginExpired { + util.PrintErrorMessageAndExit("Your login session has expired, please run [infisical login] and try again") + } + accessToken = loggedInUserDetails.UserCredentials.JTWToken + } + + project, err := util.GetProjectDetails(accessToken, workspaceId) if err != nil { return false, err } - for _, env := range res.Environments { + for _, env := range project.Environments { if env.Name == environmentName { return true, nil } From 0b55ac141cf58722bc5d43559a4f121ff375c79a Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:41:02 -0700 Subject: [PATCH 13/22] refactor(projects): rename workspace to project --- cli/packages/util/projects.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/packages/util/projects.go b/cli/packages/util/projects.go index c88e88fe7..634078d5f 100644 --- a/cli/packages/util/projects.go +++ b/cli/packages/util/projects.go @@ -5,12 +5,12 @@ import ( "github.com/go-resty/resty/v2" ) -func GetProjectDetails(accessToken string, workspaceId string) (api.Project, error) { +func GetProjectDetails(accessToken string, projectId string) (api.Project, error) { httpClient := resty.New() httpClient.SetAuthToken(accessToken). SetHeader("Accept", "application/json") - res, err := api.CallGetProjectById(httpClient, workspaceId) + res, err := api.CallGetProjectById(httpClient, projectId) if err != nil { return api.Project{}, err } From 427201a634278c516f80eb2ed8ef63934a5f8ff5 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:41:23 -0700 Subject: [PATCH 14/22] refactor(run): set up variable before call --- cli/packages/cmd/run.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index e75f5eee4..8d2e35fd5 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -138,7 +138,8 @@ var runCmd = &cobra.Command{ log.Debug().Msgf("Confirming selected environment is valid: %s", environmentName) - hasEnvironment, err := confirmProjectHasEnvironment(projectId, environmentName, token) + var hasEnvironment bool + hasEnvironment, err = confirmProjectHasEnvironment(environmentName, projectId, token) if err != nil { util.HandleError(err, "Could not confirm project has environment") } From 2e90addbc5de4c33f60e23eb1b19174e08529374 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:41:36 -0700 Subject: [PATCH 15/22] refactor(run): do not report project id in error message --- cli/packages/cmd/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 8d2e35fd5..261a945f7 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -144,7 +144,7 @@ var runCmd = &cobra.Command{ util.HandleError(err, "Could not confirm project has environment") } if !hasEnvironment { - util.HandleError(fmt.Errorf("project '%s' does not have environment '%s'", projectId, environmentName)) + util.HandleError(fmt.Errorf("project does not have environment '%s'", environmentName)) } log.Debug().Msgf("Project '%s' has environment '%s'", projectId, environmentName) From 8146dcef16de5ff1a31487433ef133d75f009667 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:42:00 -0700 Subject: [PATCH 16/22] refactor(run): call it project instead of workspace --- cli/packages/cmd/run.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 261a945f7..da93be51e 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -450,7 +450,7 @@ func executeCommandWithWatchMode(commandFlag string, args []string, watchModeInt } } -func confirmProjectHasEnvironment(environmentName, workspaceId string, token *models.TokenDetails) (bool, error) { +func confirmProjectHasEnvironment(environmentName, projectId string, token *models.TokenDetails) (bool, error) { var accessToken string if token != nil && (token.Type == util.SERVICE_TOKEN_IDENTIFIER || token.Type == util.UNIVERSAL_AUTH_TOKEN_IDENTIFIER) { @@ -470,7 +470,7 @@ func confirmProjectHasEnvironment(environmentName, workspaceId string, token *mo accessToken = loggedInUserDetails.UserCredentials.JTWToken } - project, err := util.GetProjectDetails(accessToken, workspaceId) + project, err := util.GetProjectDetails(accessToken, projectId) if err != nil { return false, err } From 4c82408b51b7383eab788813b39d805849bcf9ef Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 11:42:19 -0700 Subject: [PATCH 17/22] fix(run): grap workspace id from workspace file if not defined on the cli --- cli/packages/cmd/run.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index da93be51e..6c2e1efbf 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -470,6 +470,15 @@ func confirmProjectHasEnvironment(environmentName, projectId string, token *mode accessToken = loggedInUserDetails.UserCredentials.JTWToken } + if projectId == "" { + workspaceFile, err := util.GetWorkSpaceFromFile() + if err != nil { + util.HandleError(err, "Unable to get local project details") + } + + projectId = workspaceFile.WorkspaceId + } + project, err := util.GetProjectDetails(accessToken, projectId) if err != nil { return false, err From ad207786e25e2f307c9f42031b72918838f477ff Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 12:18:54 -0700 Subject: [PATCH 18/22] refactor: clean up empty line --- cli/test/secrets_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/test/secrets_test.go b/cli/test/secrets_test.go index f7f0f13ff..642d33277 100644 --- a/cli/test/secrets_test.go +++ b/cli/test/secrets_test.go @@ -76,7 +76,6 @@ func TestUniversalAuth_SecretsGetWrongEnvironment(t *testing.T) { if err != nil { t.Fatalf("snapshot failed: %v", err) } - } func TestUserAuth_SecretsGetAll(t *testing.T) { From a500f00a490fe0b09b097c3c6e338d9b0ddd714e Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 13:21:12 -0700 Subject: [PATCH 19/22] fix(run): compare environment slug to environment slug --- cli/packages/cmd/run.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 6c2e1efbf..dd52fe93c 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -59,11 +59,11 @@ var runCmd = &cobra.Command{ return nil }, Run: func(cmd *cobra.Command, args []string) { - environmentName, _ := cmd.Flags().GetString("env") + environmentSlug, _ := cmd.Flags().GetString("env") if !cmd.Flags().Changed("env") { environmentFromWorkspace := util.GetEnvFromWorkspaceFile() if environmentFromWorkspace != "" { - environmentName = environmentFromWorkspace + environmentSlug = environmentFromWorkspace } } @@ -136,21 +136,21 @@ var runCmd = &cobra.Command{ util.HandleError(err, "Unable to parse flag") } - log.Debug().Msgf("Confirming selected environment is valid: %s", environmentName) + log.Debug().Msgf("Confirming selected environment is valid: %s", environmentSlug) var hasEnvironment bool - hasEnvironment, err = confirmProjectHasEnvironment(environmentName, projectId, token) + hasEnvironment, err = confirmProjectHasEnvironment(environmentSlug, projectId, token) if err != nil { util.HandleError(err, "Could not confirm project has environment") } if !hasEnvironment { - util.HandleError(fmt.Errorf("project does not have environment '%s'", environmentName)) + util.HandleError(fmt.Errorf("project does not have environment '%s'", environmentSlug)) } - log.Debug().Msgf("Project '%s' has environment '%s'", projectId, environmentName) + log.Debug().Msgf("Project '%s' has environment '%s'", projectId, environmentSlug) request := models.GetAllSecretsParameters{ - Environment: environmentName, + Environment: environmentSlug, WorkspaceId: projectId, TagSlugs: tagSlugs, SecretsPath: secretsPath, @@ -450,7 +450,7 @@ func executeCommandWithWatchMode(commandFlag string, args []string, watchModeInt } } -func confirmProjectHasEnvironment(environmentName, projectId string, token *models.TokenDetails) (bool, error) { +func confirmProjectHasEnvironment(environmentSlug, projectId string, token *models.TokenDetails) (bool, error) { var accessToken string if token != nil && (token.Type == util.SERVICE_TOKEN_IDENTIFIER || token.Type == util.UNIVERSAL_AUTH_TOKEN_IDENTIFIER) { @@ -485,7 +485,7 @@ func confirmProjectHasEnvironment(environmentName, projectId string, token *mode } for _, env := range project.Environments { - if env.Name == environmentName { + if env.Slug == environmentSlug { return true, nil } } From 81d5f639aed17291eb15bcedac49a1a009c6b867 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 16:33:05 -0700 Subject: [PATCH 20/22] revert: "refactor: clean smelly code" This reverts commit c04b97c689d86069b008687d22322ae52a8b9a61. --- cli/packages/cmd/run.go | 1 + cli/packages/util/secrets.go | 33 +++++++++++++++++---------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index dd52fe93c..21839e8ad 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -185,6 +185,7 @@ var runCmd = &cobra.Command{ } } } + }, } diff --git a/cli/packages/util/secrets.go b/cli/packages/util/secrets.go index 2c53a478a..0e26e03b0 100644 --- a/cli/packages/util/secrets.go +++ b/cli/packages/util/secrets.go @@ -101,7 +101,7 @@ func GetPlainTextSecretsV3(accessToken string, workspaceId string, environmentNa return models.PlaintextSecretResult{}, err } - var plainTextSecrets []models.SingleEnvironmentVariable + plainTextSecrets := []models.SingleEnvironmentVariable{} for _, secret := range rawSecrets.Secrets { plainTextSecrets = append(plainTextSecrets, models.SingleEnvironmentVariable{Key: secret.SecretKey, Value: secret.SecretValue, Type: secret.Type, WorkspaceId: secret.Workspace, SecretPath: secret.SecretPath}) @@ -291,7 +291,7 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo if err != nil { return nil, err } - _ = WriteBackupSecrets(infisicalDotJson.WorkspaceId, params.Environment, params.SecretsPath, backupEncryptionKey, res.Secrets) + WriteBackupSecrets(infisicalDotJson.WorkspaceId, params.Environment, params.SecretsPath, backupEncryptionKey, res.Secrets) } secretsToReturn = res.Secrets @@ -308,22 +308,23 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo } } } - } else if params.InfisicalToken != "" { - log.Debug().Msg("Trying to fetch secrets using service token") - secretsToReturn, errorToReturn = GetPlainTextSecretsViaServiceToken(params.InfisicalToken, params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, params.ExpandSecretReferences) - } else if params.UniversalAuthAccessToken != "" { - if params.WorkspaceId == "" { - PrintErrorMessageAndExit("Project ID is required when using machine identity") - } - log.Debug().Msg("Trying to fetch secrets using universal auth") - res, err := GetPlainTextSecretsV3(params.UniversalAuthAccessToken, params.WorkspaceId, params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, params.ExpandSecretReferences) - if err != nil { - return nil, err - } + } else { + if params.InfisicalToken != "" { + log.Debug().Msg("Trying to fetch secrets using service token") + secretsToReturn, errorToReturn = GetPlainTextSecretsViaServiceToken(params.InfisicalToken, params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, params.ExpandSecretReferences) + } else if params.UniversalAuthAccessToken != "" { - errorToReturn = err - secretsToReturn = res.Secrets + if params.WorkspaceId == "" { + PrintErrorMessageAndExit("Project ID is required when using machine identity") + } + + log.Debug().Msg("Trying to fetch secrets using universal auth") + res, err := GetPlainTextSecretsV3(params.UniversalAuthAccessToken, params.WorkspaceId, params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, params.ExpandSecretReferences) + + errorToReturn = err + secretsToReturn = res.Secrets + } } return secretsToReturn, errorToReturn From 08732cab62e7143911a781859afbac9203f8bf28 Mon Sep 17 00:00:00 2001 From: Mahyar Mirrashed Date: Thu, 13 Mar 2025 16:36:41 -0700 Subject: [PATCH 21/22] refactor(projects): move rest api call directly into run command module --- cli/packages/cmd/run.go | 8 +++++++- cli/packages/util/projects.go | 19 ------------------- 2 files changed, 7 insertions(+), 20 deletions(-) delete mode 100644 cli/packages/util/projects.go diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 21839e8ad..6a8abc039 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -6,6 +6,8 @@ package cmd import ( "errors" "fmt" + "github.com/Infisical/infisical-merge/packages/api" + "github.com/go-resty/resty/v2" "os" "os/exec" "os/signal" @@ -480,7 +482,11 @@ func confirmProjectHasEnvironment(environmentSlug, projectId string, token *mode projectId = workspaceFile.WorkspaceId } - project, err := util.GetProjectDetails(accessToken, projectId) + httpClient := resty.New() + httpClient.SetAuthToken(accessToken). + SetHeader("Accept", "application/json") + + project, err := api.CallGetProjectById(httpClient, projectId) if err != nil { return false, err } diff --git a/cli/packages/util/projects.go b/cli/packages/util/projects.go deleted file mode 100644 index 634078d5f..000000000 --- a/cli/packages/util/projects.go +++ /dev/null @@ -1,19 +0,0 @@ -package util - -import ( - "github.com/Infisical/infisical-merge/packages/api" - "github.com/go-resty/resty/v2" -) - -func GetProjectDetails(accessToken string, projectId string) (api.Project, error) { - httpClient := resty.New() - httpClient.SetAuthToken(accessToken). - SetHeader("Accept", "application/json") - - res, err := api.CallGetProjectById(httpClient, projectId) - if err != nil { - return api.Project{}, err - } - - return res, nil -} From b4faef797c2e4b16cfdd0ea6a2b01ca0d9cbf6ef Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Fri, 14 Mar 2025 03:47:25 +0400 Subject: [PATCH 22/22] fix: address comment --- cli/packages/cmd/run.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/packages/cmd/run.go b/cli/packages/cmd/run.go index 6a8abc039..388a4861d 100644 --- a/cli/packages/cmd/run.go +++ b/cli/packages/cmd/run.go @@ -6,8 +6,6 @@ package cmd import ( "errors" "fmt" - "github.com/Infisical/infisical-merge/packages/api" - "github.com/go-resty/resty/v2" "os" "os/exec" "os/signal" @@ -17,6 +15,9 @@ import ( "syscall" "time" + "github.com/Infisical/infisical-merge/packages/api" + "github.com/go-resty/resty/v2" + "github.com/Infisical/infisical-merge/packages/models" "github.com/Infisical/infisical-merge/packages/util" "github.com/fatih/color" @@ -140,8 +141,7 @@ var runCmd = &cobra.Command{ log.Debug().Msgf("Confirming selected environment is valid: %s", environmentSlug) - var hasEnvironment bool - hasEnvironment, err = confirmProjectHasEnvironment(environmentSlug, projectId, token) + hasEnvironment, err := confirmProjectHasEnvironment(environmentSlug, projectId, token) if err != nil { util.HandleError(err, "Could not confirm project has environment") }