Skip to content

allow finding credentials for subdomain of terraform registry #4445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

wazy
Copy link
Contributor

@wazy wazy commented Jun 18, 2025

Description

Fixes #4444.

This allows terraform registries to provide module paths from subdomains where terragrunt will be able to use the token of the original host from the credentials file to still authenticate correctly.

Spacelift is an example of this as they ask you to login into spacelift.io but their registry will have you download from app.spacelift.io.

TODOs

Read the Gruntwork contribution guidelines.

  • [ X] I authored this code entirely myself
  • [ X] I am submitting code based on open source software (e.g. MIT, MPL-2.0, Apache)]
  • I am adding or upgrading a dependency or adapted code and confirm it has a compatible open source license
  • Update the docs.
  • [X ] Run the relevant tests successfully, including pre-commit checks.
  • Include release notes. If this PR is backward incompatible, include a migration guide.

Release Notes (draft)

Added support for using a terraform registry host token on subdomains for module download.

Migration Guide

Summary by CodeRabbit

  • New Features
    • Added support for retrieving credentials using host suffix matching, enabling more flexible authentication for hosts that share common domain endings.
  • Improvements
    • Enhanced authentication logic to check for credentials by exact hostname, then by host suffix, and finally by environment variable, providing a more robust credential resolution process.

@wazy wazy requested review from denis256 and yhakbar as code owners June 18, 2025 22:11
Copy link

vercel bot commented Jun 18, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
terragrunt-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 18, 2025 10:23pm

Copy link
Contributor

coderabbitai bot commented Jun 18, 2025

📝 Walkthrough

"""

Walkthrough

A new method for suffix-based credential lookup was added to the credentials system, allowing tokens to be matched using host suffixes rather than exact hostnames. The HTTP request authentication logic was updated to use this new method, attempting host suffix matches before falling back to environment variable tokens.

Changes

File(s) Change Summary
tf/cliconfig/credentials.go Added ForHostSuffix method and helper for suffix-based credential lookup in environment and config.
tf/getter.go Enhanced applyHostToken to check for host suffix credentials before using fallback environment token.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant CredentialsSource
    participant Env
    participant Config

    Caller->>CredentialsSource: ForHost(host)
    alt Exact match found
        CredentialsSource-->>Caller: Return credentials
    else No exact match
        Caller->>CredentialsSource: ForHostSuffix(host)
        alt Suffix match in Env
            CredentialsSource->>Env: hostSuffixCredentialsFromEnv(host)
            Env-->>CredentialsSource: Return credentials
            CredentialsSource-->>Caller: Return credentials
        else Suffix match in Config
            CredentialsSource->>Config: Check for suffix match
            Config-->>CredentialsSource: Return credentials
            CredentialsSource-->>Caller: Return credentials
        else No suffix match
            CredentialsSource-->>Caller: nil
        end
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Support finding token for subdomain of a terraform registry (#4444)

Possibly related PRs

Suggested reviewers

  • denis256
  • yhakbar
    """

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eb4ea3d and a711ddd.

📒 Files selected for processing (1)
  • tf/cliconfig/credentials.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tf/cliconfig/credentials.go
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d291c0c and eb4ea3d.

📒 Files selected for processing (2)
  • tf/cliconfig/credentials.go (2 hunks)
  • tf/getter.go (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.go`: Review the Go code for quality and correctness. Make sure that the Go code follows best practices, is performant, and is easy to understand and maintain.

**/*.go: Review the Go code for quality and correctness. Make sure that the Go code follows best practices, is performant, and is easy to understand and maintain.

  • tf/getter.go
  • tf/cliconfig/credentials.go
🔇 Additional comments (1)
tf/getter.go (1)

337-343: Implementation looks correct, but inherits security concern.

The three-step credential resolution logic is well-implemented and the comments clearly explain the approach. However, this code will inherit the security concern from the ForHostSuffix method in tf/cliconfig/credentials.go.

Once the subdomain-specific matching is implemented in the credentials file, this function will automatically benefit from the improved security.

@yhakbar
Copy link
Collaborator

yhakbar commented Jun 19, 2025

@wazy

Could you add some testing for this? If it can't reliably be run without access to an external service, please add a test using build flags. We're happy to help if that seems daunting.

@wazy
Copy link
Contributor Author

wazy commented Jun 21, 2025

@wazy

Could you add some testing for this? If it can't reliably be run without access to an external service, please add a test using build flags. We're happy to help if that seems daunting.

I'll try to take a stab at that but not 100% sure the flow in this project so any pointers would be great 👍

@yhakbar I'm working my through this and believe this is how to run those specific tests and where I can add them but they are getting skipped? Probably missing something simple:

GOFLAGS='-tags=private_registry' go test -run 'TestPrivateRegistry*' -v
=== RUN   TestPrivateRegistryWithConfgFileToken
    integration_private_registry_test.go:54: Skipping test because it requires a valid Terraform registry token and url
--- SKIP: TestPrivateRegistryWithConfgFileToken (0.00s)
=== RUN   TestPrivateRegistrySubDomainWithConfgFileToken
    integration_private_registry_test.go:70: Skipping test because it requires a valid Terraform registry token and url
--- SKIP: TestPrivateRegistrySubDomainWithConfgFileToken (0.00s)
=== RUN   TestPrivateRegistryWithEnvToken
    integration_private_registry_test.go:86: Skipping test because it requires a valid Terraform registry token and url
--- SKIP: TestPrivateRegistryWithEnvToken (0.00s)
PASS

@wazy
Copy link
Contributor Author

wazy commented Jun 21, 2025

That was from the setup function in integration_private_registry_test. Should I add the tests there? I assume the PRIVATE_REGISTRY_TOKEN and PRIVATE_REGISTRY_URL should be real values I export when testing?

@wazy
Copy link
Contributor Author

wazy commented Jul 1, 2025

@yhakbar Could you give me some pointers here? I am a bit confused on how to test this properly with regards to setting up a mock/fake registry? I may be misunderstanding that a bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support finding token for subdomain of a terraform registry
2 participants