From 55e33198370e104b84e850f3aa7bc20a5db897a8 Mon Sep 17 00:00:00 2001 From: aaryan Date: Sat, 11 Jan 2025 10:20:52 +0530 Subject: [PATCH] fix: PROJECT_ID should be exposed as an env value just like INFISICAL_TOKEN #2912 --- cli/packages/cmd/secrets.go | 10 ++++++++ cli/packages/models/cli.go | 6 +++++ cli/packages/util/constants.go | 1 + cli/packages/util/helper.go | 45 ++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index e93d588856..36cec286d3 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -38,11 +38,21 @@ var secretsCmd = &cobra.Command{ util.HandleError(err, "Unable to parse flag") } + // Using GetProjectID utility function + projectDetails, err := util.GetProjectID(cmd) + if err != nil { + util.HandleError(err, "Unable to determine project ID") + } + projectId, err := cmd.Flags().GetString("projectId") if err != nil { util.HandleError(err, "Unable to parse flag") } + if projectDetails != nil { + projectId = projectDetails.ID + } + secretsPath, err := cmd.Flags().GetString("path") if err != nil { util.HandleError(err, "Unable to parse flag") diff --git a/cli/packages/models/cli.go b/cli/packages/models/cli.go index 8b9fef6f6c..effc26060a 100644 --- a/cli/packages/models/cli.go +++ b/cli/packages/models/cli.go @@ -69,6 +69,12 @@ type TokenDetails struct { Source string } +// ProjectDetails contains the project ID and its source +type ProjectDetails struct { + ID string + Source string +} + type SingleFolder struct { ID string `json:"_id"` Name string `json:"name"` diff --git a/cli/packages/util/constants.go b/cli/packages/util/constants.go index 8b4c586e68..d826f2418e 100644 --- a/cli/packages/util/constants.go +++ b/cli/packages/util/constants.go @@ -7,6 +7,7 @@ const ( INFISICAL_DEFAULT_EU_URL = "https://eu.infisical.com" INFISICAL_WORKSPACE_CONFIG_FILE_NAME = ".infisical.json" INFISICAL_TOKEN_NAME = "INFISICAL_TOKEN" + INFISICAL_PROJECT_ID = "INFISICAL_PROJECT_ID" INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN_NAME = "INFISICAL_UNIVERSAL_AUTH_ACCESS_TOKEN" INFISICAL_VAULT_FILE_PASSPHRASE_ENV_NAME = "INFISICAL_VAULT_FILE_PASSPHRASE" // This works because we've forked the keyring package and added support for this env variable. This explains why you won't find any occurrences of it in the CLI codebase. diff --git a/cli/packages/util/helper.go b/cli/packages/util/helper.go index 11a1e3e0a1..c85cbe654c 100644 --- a/cli/packages/util/helper.go +++ b/cli/packages/util/helper.go @@ -119,6 +119,51 @@ func GetInfisicalToken(cmd *cobra.Command) (token *models.TokenDetails, err erro } +// GetProjectID retrieves the project ID with the following precedence: +// 1. --projectId flag +// 2. INFISICAL_PROJECT_ID environment variable +// 3. Local workspace config file +func GetProjectID(cmd *cobra.Command) (*models.ProjectDetails, error) { + + projectID, err := cmd.Flags().GetString("projectId") + + if err != nil { + return nil, fmt.Errorf("unable to parse projectId flag: %w", err) + } + + if projectID != "" { + return &models.ProjectDetails{ + ID: projectID, + Source: "--projectId flag", + }, nil + } + + // Check environment variable + projectID = os.Getenv(INFISICAL_PROJECT_ID) + if projectID != "" { + return &models.ProjectDetails{ + ID: projectID, + Source: fmt.Sprintf("%s environment variable", INFISICAL_PROJECT_ID), + }, nil + } + + // Try workspace file + configFile, err := GetWorkSpaceFromFile() + if err == nil && configFile.WorkspaceId != "" { + return &models.ProjectDetails{ + ID: configFile.WorkspaceId, + Source: "workspace configuration file", + }, nil + } + + // When using service tokens or machine identities, project ID is required + if cmd.Flags().Changed("token") { + return nil, fmt.Errorf("when using service tokens or machine identities, you must provide a project ID either via --projectId flag or %s environment variable", INFISICAL_PROJECT_ID) + } + + return nil, nil // Return nil when no project ID is found and it's not required +} + func UniversalAuthLogin(clientId string, clientSecret string) (api.UniversalAuthLoginResponse, error) { httpClient := resty.New() httpClient.SetRetryCount(10000).