Skip to content

Commit

Permalink
fix: PROJECT_ID should be exposed as an env value just like INFISICAL…
Browse files Browse the repository at this point in the history
…_TOKEN Infisical#2912
  • Loading branch information
aaryan182 committed Jan 11, 2025
1 parent 99017ea commit 55e3319
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cli/packages/cmd/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 6 additions & 0 deletions cli/packages/models/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions cli/packages/util/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
45 changes: 45 additions & 0 deletions cli/packages/util/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down

0 comments on commit 55e3319

Please sign in to comment.