Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/util/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,26 @@ func FilterSecretsByTag(plainTextSecrets []models.SingleEnvironmentVariable, tag
return filteredSecrets
}

func resolveWorkspaceID(workspaceID, projectConfigFilePath string) (string, error) {
if workspaceID != "" {
return workspaceID, nil
}

if projectConfigFilePath == "" {
workspaceConfig, err := GetWorkSpaceFromFile()
if err != nil {
return "", err
}
return workspaceConfig.WorkspaceId, nil
}

workspaceConfig, err := GetWorkSpaceFromFilePath(projectConfigFilePath)
if err != nil {
return "", err
}
return workspaceConfig.WorkspaceId, nil
}

func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectConfigFilePath string) ([]models.SingleEnvironmentVariable, error) {
var secretsToReturn []models.SingleEnvironmentVariable
// var serviceTokenDetails api.GetServiceTokenDetailsResponse
Expand Down Expand Up @@ -366,6 +386,13 @@ func GetAllEnvironmentVariables(params models.GetAllSecretsParameters, projectCo
secretsToReturn, errorToReturn = GetPlainTextSecretsViaServiceToken(params.InfisicalToken, params.Environment, params.SecretsPath, params.IncludeImport, params.Recursive, params.TagSlugs, params.ExpandSecretReferences, params.IncludePersonalOverrides)
} else if params.UniversalAuthAccessToken != "" {

if params.WorkspaceId == "" {
workspaceID, err := resolveWorkspaceID(params.WorkspaceId, projectConfigFilePath)
if err == nil {
params.WorkspaceId = workspaceID
}
Comment on lines +391 to +393

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Workspace errors lose diagnostics

When .infisical.json is malformed, unreadable, or absent from the configured directory, this branch discards the configuration error and exits with Project ID is required when using machine identity, concealing the problem the user must correct.

Knowledge Base Used: Secrets Management

}

if params.WorkspaceId == "" {
PrintErrorMessageAndExit("Project ID is required when using machine identity")
}
Expand Down
35 changes: 35 additions & 0 deletions packages/util/secrets_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package util

import (
"os"
"path/filepath"
"testing"
)

func TestResolveWorkspaceID(t *testing.T) {
t.Run("uses explicit project ID before config", func(t *testing.T) {
workspaceID, err := resolveWorkspaceID("project-from-flag", t.TempDir())
if err != nil {
t.Fatalf("resolve workspace ID: %v", err)
}
if workspaceID != "project-from-flag" {
t.Errorf("workspace ID = %q, want %q", workspaceID, "project-from-flag")
}
})

t.Run("uses workspace config when project ID is omitted", func(t *testing.T) {
configDir := t.TempDir()
configPath := filepath.Join(configDir, INFISICAL_WORKSPACE_CONFIG_FILE_NAME)
if err := os.WriteFile(configPath, []byte(`{"workspaceId":"project-from-config"}`), 0600); err != nil {
t.Fatalf("write workspace config: %v", err)
}

workspaceID, err := resolveWorkspaceID("", configDir)
if err != nil {
t.Fatalf("resolve workspace ID: %v", err)
}
if workspaceID != "project-from-config" {
t.Errorf("workspace ID = %q, want %q", workspaceID, "project-from-config")
}
})
}