Merge pull request #3707 from Infisical/misc/workspace-file-now-only-needed-when-project-id-omitted

misc: workspace file now only needed when project id is omitted (CLI)
This commit is contained in:
Sheen
2025-06-04 00:24:40 +08:00
committed by GitHub
5 changed files with 52 additions and 55 deletions
+5 -10
View File
@@ -63,7 +63,7 @@ func getDynamicSecretList(cmd *cobra.Command, args []string) {
if projectId == "" { if projectId == "" {
workspaceFile, err := util.GetWorkSpaceFromFile() workspaceFile, err := util.GetWorkSpaceFromFile()
if err != nil { 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 projectId = workspaceFile.WorkspaceId
} }
@@ -72,7 +72,6 @@ func getDynamicSecretList(cmd *cobra.Command, args []string) {
infisicalToken = token.Token infisicalToken = token.Token
} else { } else {
util.RequireLogin() util.RequireLogin()
util.RequireLocalWorkspaceFile()
loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true)
if err != nil { if err != nil {
@@ -181,7 +180,7 @@ func createDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
if projectId == "" { if projectId == "" {
workspaceFile, err := util.GetWorkSpaceFromFile() workspaceFile, err := util.GetWorkSpaceFromFile()
if err != nil { 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 projectId = workspaceFile.WorkspaceId
} }
@@ -190,7 +189,6 @@ func createDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
infisicalToken = token.Token infisicalToken = token.Token
} else { } else {
util.RequireLogin() util.RequireLogin()
util.RequireLocalWorkspaceFile()
loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true)
if err != nil { if err != nil {
@@ -312,7 +310,7 @@ func renewDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
if projectId == "" { if projectId == "" {
workspaceFile, err := util.GetWorkSpaceFromFile() workspaceFile, err := util.GetWorkSpaceFromFile()
if err != nil { 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 projectId = workspaceFile.WorkspaceId
} }
@@ -321,7 +319,6 @@ func renewDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
infisicalToken = token.Token infisicalToken = token.Token
} else { } else {
util.RequireLogin() util.RequireLogin()
util.RequireLocalWorkspaceFile()
loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true)
if err != nil { if err != nil {
@@ -420,7 +417,7 @@ func revokeDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
if projectId == "" { if projectId == "" {
workspaceFile, err := util.GetWorkSpaceFromFile() workspaceFile, err := util.GetWorkSpaceFromFile()
if err != nil { 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 projectId = workspaceFile.WorkspaceId
} }
@@ -429,7 +426,6 @@ func revokeDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
infisicalToken = token.Token infisicalToken = token.Token
} else { } else {
util.RequireLogin() util.RequireLogin()
util.RequireLocalWorkspaceFile()
loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true)
if err != nil { if err != nil {
@@ -527,7 +523,7 @@ func listDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
if projectId == "" { if projectId == "" {
workspaceFile, err := util.GetWorkSpaceFromFile() workspaceFile, err := util.GetWorkSpaceFromFile()
if err != nil { 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 projectId = workspaceFile.WorkspaceId
} }
@@ -536,7 +532,6 @@ func listDynamicSecretLeaseByName(cmd *cobra.Command, args []string) {
infisicalToken = token.Token infisicalToken = token.Token
} else { } else {
util.RequireLogin() util.RequireLogin()
util.RequireLocalWorkspaceFile()
loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true)
if err != nil { if err != nil {
+2 -2
View File
@@ -112,7 +112,7 @@ var createCmd = &cobra.Command{
if projectId == "" { if projectId == "" {
workspaceFile, err := util.GetWorkSpaceFromFile() workspaceFile, err := util.GetWorkSpaceFromFile()
if err != nil { 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 projectId = workspaceFile.WorkspaceId
@@ -180,7 +180,7 @@ var deleteCmd = &cobra.Command{
if projectId == "" { if projectId == "" {
workspaceFile, err := util.GetWorkSpaceFromFile() workspaceFile, err := util.GetWorkSpaceFromFile()
if err != nil { 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 projectId = workspaceFile.WorkspaceId
+9 -7
View File
@@ -158,10 +158,6 @@ var secretsSetCmd = &cobra.Command{
util.HandleError(err, "Unable to parse flag") util.HandleError(err, "Unable to parse flag")
} }
if token == nil {
util.RequireLocalWorkspaceFile()
}
environmentName, _ := cmd.Flags().GetString("env") environmentName, _ := cmd.Flags().GetString("env")
if !cmd.Flags().Changed("env") { if !cmd.Flags().Changed("env") {
environmentFromWorkspace := util.GetEnvFromWorkspaceFile() environmentFromWorkspace := util.GetEnvFromWorkspaceFile()
@@ -175,6 +171,13 @@ var secretsSetCmd = &cobra.Command{
util.HandleError(err, "Unable to parse flag") util.HandleError(err, "Unable to parse flag")
} }
if token == nil && projectId == "" {
_, 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") secretsPath, err := cmd.Flags().GetString("path")
if err != nil { if err != nil {
util.HandleError(err, "Unable to parse flag") util.HandleError(err, "Unable to parse flag")
@@ -225,7 +228,7 @@ var secretsSetCmd = &cobra.Command{
if projectId == "" { if projectId == "" {
workspaceFile, err := util.GetWorkSpaceFromFile() workspaceFile, err := util.GetWorkSpaceFromFile()
if err != nil { 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 projectId = workspaceFile.WorkspaceId
@@ -308,7 +311,7 @@ var secretsDeleteCmd = &cobra.Command{
if projectId == "" { if projectId == "" {
workspaceFile, err := util.GetWorkSpaceFromFile() workspaceFile, err := util.GetWorkSpaceFromFile()
if err != nil { 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 projectId = workspaceFile.WorkspaceId
} }
@@ -317,7 +320,6 @@ var secretsDeleteCmd = &cobra.Command{
httpClient.SetAuthToken(token.Token) httpClient.SetAuthToken(token.Token)
} else { } else {
util.RequireLogin() util.RequireLogin()
util.RequireLocalWorkspaceFile()
loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true) loggedInUserDetails, err := util.GetCurrentLoggedInUserDetails(true)
if err != nil { if err != nil {
+7 -11
View File
@@ -15,7 +15,6 @@ func GetAllFolders(params models.GetAllFoldersParameters) ([]models.SingleFolder
var folderErr error var folderErr error
if params.InfisicalToken == "" && params.UniversalAuthAccessToken == "" { if params.InfisicalToken == "" && params.UniversalAuthAccessToken == "" {
RequireLogin() RequireLogin()
RequireLocalWorkspaceFile()
log.Debug().Msg("GetAllFolders: Trying to fetch folders using logged in details") 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() loggedInUserDetails = EstablishUserLoginSession()
} }
workspaceFile, err := GetWorkSpaceFromFile() if params.WorkspaceId == "" {
if err != nil { workspaceFile, err := GetWorkSpaceFromFile()
return nil, err if err != nil {
PrintErrorMessageAndExit("Please either run infisical init to connect to a project or pass in project id with --projectId flag")
}
params.WorkspaceId = workspaceFile.WorkspaceId
} }
if params.WorkspaceId != "" { folders, err := GetFoldersViaJTW(loggedInUserDetails.UserCredentials.JTWToken, params.WorkspaceId, params.Environment, params.FoldersPath)
workspaceFile.WorkspaceId = params.WorkspaceId
}
folders, err := GetFoldersViaJTW(loggedInUserDetails.UserCredentials.JTWToken, workspaceFile.WorkspaceId, params.Environment, params.FoldersPath)
folderErr = err folderErr = err
foldersToReturn = folders foldersToReturn = folders
} else if params.InfisicalToken != "" { } 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 no token is provided, we will try to get the token from the current logged in user
if params.InfisicalToken == "" { if params.InfisicalToken == "" {
RequireLogin() RequireLogin()
RequireLocalWorkspaceFile()
loggedInUserDetails, err := GetCurrentLoggedInUserDetails(true) loggedInUserDetails, err := GetCurrentLoggedInUserDetails(true)
if err != nil { 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 no token is provided, we will try to get the token from the current logged in user
if params.InfisicalToken == "" { if params.InfisicalToken == "" {
RequireLogin() RequireLogin()
RequireLocalWorkspaceFile()
loggedInUserDetails, err := GetCurrentLoggedInUserDetails(true) loggedInUserDetails, err := GetCurrentLoggedInUserDetails(true)
+29 -25
View File
@@ -251,10 +251,15 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo
var errorToReturn error var errorToReturn error
if params.InfisicalToken == "" && params.UniversalAuthAccessToken == "" { if params.InfisicalToken == "" && params.UniversalAuthAccessToken == "" {
if projectConfigFilePath == "" { if params.WorkspaceId == "" {
RequireLocalWorkspaceFile() if projectConfigFilePath == "" {
} else { _, err := GetWorkSpaceFromFile()
ValidateWorkspaceFile(projectConfigFilePath) 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)
}
} }
RequireLogin() RequireLogin()
@@ -276,29 +281,28 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo
loggedInUserDetails = EstablishUserLoginSession() loggedInUserDetails = EstablishUserLoginSession()
} }
var infisicalDotJson models.WorkspaceConfigFile if params.WorkspaceId == "" {
var infisicalDotJson models.WorkspaceConfigFile
if projectConfigFilePath == "" { if projectConfigFilePath == "" {
projectConfig, err := GetWorkSpaceFromFile() projectConfig, err := GetWorkSpaceFromFile()
if err != nil { 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
} else {
projectConfig, err := GetWorkSpaceFromFilePath(projectConfigFilePath)
if err != nil {
return nil, err
}
infisicalDotJson = projectConfig
} }
params.WorkspaceId = infisicalDotJson.WorkspaceId
infisicalDotJson = projectConfig
} else {
projectConfig, err := GetWorkSpaceFromFilePath(projectConfigFilePath)
if err != nil {
return nil, err
}
infisicalDotJson = projectConfig
} }
if params.WorkspaceId != "" { res, err := GetPlainTextSecretsV3(loggedInUserDetails.UserCredentials.JTWToken, params.WorkspaceId,
infisicalDotJson.WorkspaceId = params.WorkspaceId
}
res, err := GetPlainTextSecretsV3(loggedInUserDetails.UserCredentials.JTWToken, infisicalDotJson.WorkspaceId,
params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, true) params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, true)
log.Debug().Msgf("GetAllEnvironmentVariables: Trying to fetch secrets JTW token [err=%s]", err) log.Debug().Msgf("GetAllEnvironmentVariables: Trying to fetch secrets JTW token [err=%s]", err)
@@ -307,7 +311,7 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo
if err != nil { if err != nil {
return nil, err 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 secretsToReturn = res.Secrets
@@ -316,7 +320,7 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo
if !isConnected { if !isConnected {
backupEncryptionKey, _ := GetBackupEncryptionKey() backupEncryptionKey, _ := GetBackupEncryptionKey()
if backupEncryptionKey != nil { 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 { 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") 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 secretsToReturn = backedUpSecrets