Skip to content

Adds TFC_CLOUD_ORGANIZATION and TF_WORKSPACE Terraform env defaults for search tools - #564

Closed
sowju-hashicorp wants to merge 16 commits into
sowju/finding_resourcesfrom
sowju/adds_env_defaults
Closed

sowju-hashicorp wants to merge 16 commits into
sowju/finding_resourcesfrom
sowju/adds_env_defaults

Conversation

@sowju-hashicorp

Copy link
Copy Markdown

Description

Uses TF_CLOUD_ORGANIZATION and TF_WORKSPACE as the default query scope for the HCP Terraform Search workflow.

This prevents provider_list_schema_list and execute_query from unnecessarily stopping to request organization and workspace values when they are already configured in the environment.

Changes

  • Read organization and workspace from TF_CLOUD_ORGANIZATION and TF_WORKSPACE.
  • Give environment values precedence over tool arguments.
  • Expose configured environment values as defaults in the MCP tool schemas.
  • Tell the user which environment-derived organization and workspace are being used.
  • Fall back to tool arguments when the environment variables are unset or empty.
  • Prompt the user for both values when the environment does not provide the complete scope.
  • Continue to prevent unscoped provider schema requests and query execution.
  • Share scope resolution between provider_list_schema_list and execute_query.
  • Add tests for environment precedence, argument fallback, schema defaults, and missing scope handling.

Testing

  • go test ./pkg/tools/search
  • All Search tool tests pass.

Rollback

Revert this PR. No migrations, persistent data changes, or additional cleanup are required.

Security impact

No security controls are added or removed. Organization and workspace scope remain mandatory before provider schemas are fetched or queries are executed. Existing authenticated session and workspace-access validation remain unchanged.

PCI review checklist

  • I have documented a clear reason for, and description of, the change I am making.

  • If applicable, I've documented a plan to revert these changes if they require more than reverting the pull request.

  • If applicable, I've documented the impact of any changes to security controls.

## Description

This PR hardens provider schema discovery and aligns it with the other HCP Terraform Search tools.

Provider catalog and schema requests now use the session-scoped `go-tfe` client instead of manually extracting tokens and constructing authenticated HTTP requests. Each request validates access to the supplied workspace before calling the provider endpoint.

## Changes

- Validate the organization and workspace before fetching provider data.
- Stop before the provider endpoint when workspace lookup fails.
- Use the shared session TFE client for provider catalog and schema requests.
- Safely encode provider path segments and organization query parameters.
- Mark `provider_list_schema_list` as an open-world tool.
- Consolidate duplicated search tool error handling.
- Remove the now-unused context token helper and manual HTTP request code.
- Add handler-level tests for successful and failed workspace validation.

## Testing

- `go test ./pkg/tools/search ./pkg/client`
- `git diff --check sowju/finding_resources...HEAD`

## Rollback

Revert this PR. No migrations, persistent data changes, or configuration changes are required.

## Security impact

No security controls are added or removed. Provider requests continue to use the authenticated session client, and workspace access is now verified before accessing the provider catalog. The change also removes duplicate bearer-token handling.

## PCI review checklist

<!-- heimdall_github_prtemplate:grc-pci_dss-2024-01-05 -->

- [x] I have documented a clear reason for, and description of, the change I am making.

- [x] If applicable, I've documented a plan to revert these changes if they require more than reverting the pull request.

- [x] If applicable, I've documented the impact of any changes to security controls.
Copilot AI balanced review requested due to automatic review settings October 7, 2026 19:17
@sowju-hashicorp
sowju-hashicorp requested a review from a team as a code owner October 7, 2026 19:17

Copilot AI left a comment

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.

🟡 Changes recommended

Partial environment configuration can combine mismatched organization and workspace scopes and exposes an inconsistent schema contract.

3 open findings
What changed in this PR

Adds environment-based organization and workspace defaults to HCP Terraform search tools.

Changes:

  • Shares environment-first scope resolution between search tools.
  • Exposes environment values through MCP schemas.
  • Adds scope-resolution and schema tests.
File Description
pkg/​tools/​search/​provider_list_schema_list.go Adds environment-backed scope resolution and defaults.
pkg/​tools/​search/​provider_list_schema_list_test.go Tests scope precedence and missing scope handling.
pkg/​tools/​search/​execute_query.go Applies shared scope resolution to query execution.
pkg/​tools/​search/​execute_query_test.go Updates tool-definition expectations.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread pkg/tools/search/execute_query.go Outdated
Comment on lines +57 to +58
if organizationName != "" {
organizationOptions = append(organizationOptions, mcp.DefaultString(organizationName))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is intended if one variable is not set. Please prompt the user

Comment on lines +48 to +49
if organizationName != "" {
organizationOptions = append(organizationOptions, mcp.DefaultString(organizationName))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is intended if one variable is not set. Please prompt the user

Comment on lines +123 to +133
func resolveSearchScope(request mcp.CallToolRequest) (string, string) {
orgName := strings.TrimSpace(os.Getenv("TF_CLOUD_ORGANIZATION"))
if orgName == "" {
orgName = strings.TrimSpace(request.GetString("organization_name", ""))
}
workspaceName := strings.TrimSpace(os.Getenv("TF_WORKSPACE"))
if workspaceName == "" {
workspaceName = strings.TrimSpace(request.GetString("workspace_name", ""))
}
return orgName, workspaceName
}
@sowju-hashicorp
sowju-hashicorp force-pushed the sowju/adds_env_defaults branch from e024dd1 to 2634e1d Compare October 9, 2026 00:04
@mjyocca
mjyocca force-pushed the sowju/finding_resources branch from f287512 to 5e9c89e Compare October 12, 2026 17:24
@sowju-hashicorp
sowju-hashicorp deleted the sowju/adds_env_defaults branch October 14, 2026 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants