From c0add863bed7081708e0418f2861979168bf9093 Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Tue, 3 Jun 2025 19:41:37 +0800 Subject: [PATCH 1/2] misc: workspace file now only needed when project id is omitted (CLI) --- cli/packages/cmd/dynamic_secrets.go | 5 --- cli/packages/cmd/secrets.go | 9 +++-- cli/packages/util/folders.go | 18 ++++------ cli/packages/util/secrets.go | 51 +++++++++++++++-------------- 4 files changed, 37 insertions(+), 46 deletions(-) diff --git a/cli/packages/cmd/dynamic_secrets.go b/cli/packages/cmd/dynamic_secrets.go index 665bfaa54..b92a675c6 100644 --- a/cli/packages/cmd/dynamic_secrets.go +++ b/cli/packages/cmd/dynamic_secrets.go @@ -72,7 +72,6 @@ func getDynamicSecretList(cmd *cobra.Command, args []string) { infisicalToken = token.Token } else { util.RequireLogin() - util.RequireLocalWorkspaceFile() loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) if err != nil { @@ -190,7 +189,6 @@ func createDynamicSecretLeaseByName(cmd *cobra.Command, args []string) { infisicalToken = token.Token } else { util.RequireLogin() - util.RequireLocalWorkspaceFile() loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) if err != nil { @@ -321,7 +319,6 @@ func renewDynamicSecretLeaseByName(cmd *cobra.Command, args []string) { infisicalToken = token.Token } else { util.RequireLogin() - util.RequireLocalWorkspaceFile() loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) if err != nil { @@ -429,7 +426,6 @@ func revokeDynamicSecretLeaseByName(cmd *cobra.Command, args []string) { infisicalToken = token.Token } else { util.RequireLogin() - util.RequireLocalWorkspaceFile() loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) if err != nil { @@ -536,7 +532,6 @@ func listDynamicSecretLeaseByName(cmd *cobra.Command, args []string) { infisicalToken = token.Token } else { util.RequireLogin() - util.RequireLocalWorkspaceFile() loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) if err != nil { diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index 4b3465d8f..080bc20cc 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -158,10 +158,6 @@ var secretsSetCmd = &cobra.Command{ util.HandleError(err, "Unable to parse flag") } - if token == nil { - util.RequireLocalWorkspaceFile() - } - environmentName, _ := cmd.Flags().GetString("env") if !cmd.Flags().Changed("env") { environmentFromWorkspace := util.GetEnvFromWorkspaceFile() @@ -175,6 +171,10 @@ var secretsSetCmd = &cobra.Command{ util.HandleError(err, "Unable to parse flag") } + if token == nil && projectId == "" { + util.RequireLocalWorkspaceFile() + } + secretsPath, err := cmd.Flags().GetString("path") if err != nil { util.HandleError(err, "Unable to parse flag") @@ -317,7 +317,6 @@ var secretsDeleteCmd = &cobra.Command{ httpClient.SetAuthToken(token.Token) } else { util.RequireLogin() - util.RequireLocalWorkspaceFile() loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) if err != nil { diff --git a/cli/packages/util/folders.go b/cli/packages/util/folders.go index 412f29ac3..33931c05d 100644 --- a/cli/packages/util/folders.go +++ b/cli/packages/util/folders.go @@ -15,7 +15,6 @@ func GetAllFolders(params models.GetAllFoldersParameters) ([]models.SingleFolder var folderErr error if params.InfisicalToken == "" && params.UniversalAuthAccessToken == "" { RequireLogin() - RequireLocalWorkspaceFile() log.Debug().Msg("GetAllFolders: Trying to fetch folders using logged in details") @@ -28,16 +27,15 @@ func GetAllFolders(params models.GetAllFoldersParameters) ([]models.SingleFolder loggedInUserDetails = EstablishUserLoginSession() } - workspaceFile, err := GetWorkSpaceFromFile() - if err != nil { - return nil, err + if params.WorkspaceId == "" { + workspaceFile, err := GetWorkSpaceFromFile() + if err != nil { + return nil, err + } + params.WorkspaceId = workspaceFile.WorkspaceId } - if params.WorkspaceId != "" { - workspaceFile.WorkspaceId = params.WorkspaceId - } - - folders, err := GetFoldersViaJTW(loggedInUserDetails.UserCredentials.JTWToken, workspaceFile.WorkspaceId, params.Environment, params.FoldersPath) + folders, err := GetFoldersViaJTW(loggedInUserDetails.UserCredentials.JTWToken, params.WorkspaceId, params.Environment, params.FoldersPath) folderErr = err foldersToReturn = folders } else if params.InfisicalToken != "" { @@ -186,7 +184,6 @@ func CreateFolder(params models.CreateFolderParameters) (models.SingleFolder, er // If no token is provided, we will try to get the token from the current logged in user if params.InfisicalToken == "" { RequireLogin() - RequireLocalWorkspaceFile() loggedInUserDetails, err := GetCurrentLoggedInUserDetails(true) if err != nil { @@ -235,7 +232,6 @@ func DeleteFolder(params models.DeleteFolderParameters) ([]models.SingleFolder, // If no token is provided, we will try to get the token from the current logged in user if params.InfisicalToken == "" { RequireLogin() - RequireLocalWorkspaceFile() loggedInUserDetails, err := GetCurrentLoggedInUserDetails(true) diff --git a/cli/packages/util/secrets.go b/cli/packages/util/secrets.go index 0ee3eaa72..1d5763321 100644 --- a/cli/packages/util/secrets.go +++ b/cli/packages/util/secrets.go @@ -251,10 +251,12 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo var errorToReturn error if params.InfisicalToken == "" && params.UniversalAuthAccessToken == "" { - if projectConfigFilePath == "" { - RequireLocalWorkspaceFile() - } else { - ValidateWorkspaceFile(projectConfigFilePath) + if params.WorkspaceId == "" { + if projectConfigFilePath == "" { + RequireLocalWorkspaceFile() + } else { + ValidateWorkspaceFile(projectConfigFilePath) + } } RequireLogin() @@ -276,29 +278,28 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo loggedInUserDetails = EstablishUserLoginSession() } - var infisicalDotJson models.WorkspaceConfigFile + if params.WorkspaceId == "" { + var infisicalDotJson models.WorkspaceConfigFile - if projectConfigFilePath == "" { - projectConfig, err := GetWorkSpaceFromFile() - if err != nil { - return nil, err + if projectConfigFilePath == "" { + projectConfig, err := GetWorkSpaceFromFile() + if err != nil { + return nil, err + } + + infisicalDotJson = projectConfig + } else { + projectConfig, err := GetWorkSpaceFromFilePath(projectConfigFilePath) + if err != nil { + return nil, err + } + + infisicalDotJson = projectConfig } - - infisicalDotJson = projectConfig - } else { - projectConfig, err := GetWorkSpaceFromFilePath(projectConfigFilePath) - if err != nil { - return nil, err - } - - infisicalDotJson = projectConfig + params.WorkspaceId = infisicalDotJson.WorkspaceId } - if params.WorkspaceId != "" { - infisicalDotJson.WorkspaceId = params.WorkspaceId - } - - res, err := GetPlainTextSecretsV3(loggedInUserDetails.UserCredentials.JTWToken, infisicalDotJson.WorkspaceId, + res, err := GetPlainTextSecretsV3(loggedInUserDetails.UserCredentials.JTWToken, params.WorkspaceId, params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, true) log.Debug().Msgf("GetAllEnvironmentVariables: Trying to fetch secrets JTW token [err=%s]", err) @@ -307,7 +308,7 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo if err != nil { return nil, err } - WriteBackupSecrets(infisicalDotJson.WorkspaceId, params.Environment, params.SecretsPath, backupEncryptionKey, res.Secrets) + WriteBackupSecrets(params.WorkspaceId, params.Environment, params.SecretsPath, backupEncryptionKey, res.Secrets) } secretsToReturn = res.Secrets @@ -316,7 +317,7 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo if !isConnected { backupEncryptionKey, _ := GetBackupEncryptionKey() if backupEncryptionKey != nil { - backedUpSecrets, err := ReadBackupSecrets(infisicalDotJson.WorkspaceId, params.Environment, params.SecretsPath, backupEncryptionKey) + backedUpSecrets, err := ReadBackupSecrets(params.WorkspaceId, params.Environment, params.SecretsPath, backupEncryptionKey) if len(backedUpSecrets) > 0 { PrintWarning("Unable to fetch the latest secret(s) due to connection error, serving secrets from last successful fetch. For more info, run with --debug") secretsToReturn = backedUpSecrets From f3fb65fcc307bc48eec4e4909e63e47fbf2de7d9 Mon Sep 17 00:00:00 2001 From: Sheen Capadngan Date: Tue, 3 Jun 2025 20:06:42 +0800 Subject: [PATCH 2/2] misc: update error message being displayed --- cli/packages/cmd/dynamic_secrets.go | 10 +++++----- cli/packages/cmd/folder.go | 4 ++-- cli/packages/cmd/secrets.go | 9 ++++++--- cli/packages/util/folders.go | 2 +- cli/packages/util/secrets.go | 7 +++++-- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/cli/packages/cmd/dynamic_secrets.go b/cli/packages/cmd/dynamic_secrets.go index b92a675c6..8761b84ef 100644 --- a/cli/packages/cmd/dynamic_secrets.go +++ b/cli/packages/cmd/dynamic_secrets.go @@ -63,7 +63,7 @@ func getDynamicSecretList(cmd *cobra.Command, args []string) { if projectId == "" { workspaceFile, err := util.GetWorkSpaceFromFile() if err != nil { - util.HandleError(err, "Unable to get local project details") + util.PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } projectId = workspaceFile.WorkspaceId } @@ -180,7 +180,7 @@ func createDynamicSecretLeaseByName(cmd *cobra.Command, args []string) { if projectId == "" { workspaceFile, err := util.GetWorkSpaceFromFile() if err != nil { - util.HandleError(err, "Unable to get local project details") + util.PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } projectId = workspaceFile.WorkspaceId } @@ -310,7 +310,7 @@ func renewDynamicSecretLeaseByName(cmd *cobra.Command, args []string) { if projectId == "" { workspaceFile, err := util.GetWorkSpaceFromFile() if err != nil { - util.HandleError(err, "Unable to get local project details") + util.PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } projectId = workspaceFile.WorkspaceId } @@ -417,7 +417,7 @@ func revokeDynamicSecretLeaseByName(cmd *cobra.Command, args []string) { if projectId == "" { workspaceFile, err := util.GetWorkSpaceFromFile() if err != nil { - util.HandleError(err, "Unable to get local project details") + util.PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } projectId = workspaceFile.WorkspaceId } @@ -523,7 +523,7 @@ func listDynamicSecretLeaseByName(cmd *cobra.Command, args []string) { if projectId == "" { workspaceFile, err := util.GetWorkSpaceFromFile() if err != nil { - util.HandleError(err, "Unable to get local project details") + util.PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } projectId = workspaceFile.WorkspaceId } diff --git a/cli/packages/cmd/folder.go b/cli/packages/cmd/folder.go index 538f1e2dc..b59652191 100644 --- a/cli/packages/cmd/folder.go +++ b/cli/packages/cmd/folder.go @@ -112,7 +112,7 @@ var createCmd = &cobra.Command{ if projectId == "" { workspaceFile, err := util.GetWorkSpaceFromFile() if err != nil { - util.HandleError(err, "Unable to get workspace file") + util.PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } projectId = workspaceFile.WorkspaceId @@ -180,7 +180,7 @@ var deleteCmd = &cobra.Command{ if projectId == "" { workspaceFile, err := util.GetWorkSpaceFromFile() if err != nil { - util.HandleError(err, "Unable to get workspace file") + util.PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } projectId = workspaceFile.WorkspaceId diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index 080bc20cc..930a27a56 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -172,7 +172,10 @@ var secretsSetCmd = &cobra.Command{ } if token == nil && projectId == "" { - util.RequireLocalWorkspaceFile() + _, err := util.GetWorkSpaceFromFile() + if err != nil { + util.PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") + } } secretsPath, err := cmd.Flags().GetString("path") @@ -225,7 +228,7 @@ var secretsSetCmd = &cobra.Command{ if projectId == "" { workspaceFile, err := util.GetWorkSpaceFromFile() if err != nil { - util.HandleError(err, "unable to get your local config details [err=%v]") + util.PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } projectId = workspaceFile.WorkspaceId @@ -308,7 +311,7 @@ var secretsDeleteCmd = &cobra.Command{ if projectId == "" { workspaceFile, err := util.GetWorkSpaceFromFile() if err != nil { - util.HandleError(err, "Unable to get local project details") + util.PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } projectId = workspaceFile.WorkspaceId } diff --git a/cli/packages/util/folders.go b/cli/packages/util/folders.go index 33931c05d..fb4f2a322 100644 --- a/cli/packages/util/folders.go +++ b/cli/packages/util/folders.go @@ -30,7 +30,7 @@ func GetAllFolders(params models.GetAllFoldersParameters) ([]models.SingleFolder if params.WorkspaceId == "" { workspaceFile, err := GetWorkSpaceFromFile() if err != nil { - return nil, err + PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } params.WorkspaceId = workspaceFile.WorkspaceId } diff --git a/cli/packages/util/secrets.go b/cli/packages/util/secrets.go index 1d5763321..814e7da23 100644 --- a/cli/packages/util/secrets.go +++ b/cli/packages/util/secrets.go @@ -253,7 +253,10 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo if params.InfisicalToken == "" && params.UniversalAuthAccessToken == "" { if params.WorkspaceId == "" { if projectConfigFilePath == "" { - RequireLocalWorkspaceFile() + _, err := GetWorkSpaceFromFile() + if err != nil { + PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") + } } else { ValidateWorkspaceFile(projectConfigFilePath) } @@ -284,7 +287,7 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo if projectConfigFilePath == "" { projectConfig, err := GetWorkSpaceFromFile() if err != nil { - return nil, err + PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag") } infisicalDotJson = projectConfig