Skip to content
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

Validate Terraform input variables using OPA policies #977

Merged
merged 24 commits into from
Jan 27, 2025

Conversation

aknysh
Copy link
Member

@aknysh aknysh commented Jan 27, 2025

what

why

Use Open Policy Agent (OPA) policies to validate Terraform input variables.

Introduction

When executing atmos terraform <sub-command> commands, you can provide Terraform input variables on the command line using the -var flag. These variables will override the variables configured in Atmos stack manifests.

For example:

atmos terraform apply <component> -s <stack> -- -var name=api

atmos terraform apply <component> -s <stack> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}'

NOTE: Terraform processes variables in the following order of precedence (from highest to lowest):

  • Explicit -var flags: these variables have the highest priority and will override any other variable values, including those specified in --var-file.

  • Variables in --var-file: values in a variable file override default values set in the Terraform configuration.
    Atmos generates varfiles from stack configurations and provides it to Terraform using the --var-file flag.

  • Environment variables: variables set as environment variables using the TF_VAR_ prefix.

  • Default values in the Terraform configuration files: these have the lowest priority.


When log level Trace is used, Atmos prints the Terraform variables specified on the command line in the "CLI variables" output.

For example:

ATMOS_LOGS_LEVEL=Trace /
atmos terraform apply my-component -s plat-ue2-dev -- -var name=api -var 'tags={"Team":"api", "Group":"web"}'

Variables for the component 'my-component' in the stack 'plat-ue2-dev':
environment: ue2
namespace: cp
region: us-east-2
stage: dev
tenant: plat

Writing the variables to file:
components/terraform/my-component/plat-ue2-dev-my-component.terraform.tfvars.json

CLI variables (will override the variables defined in the stack manifests):
name: api
tags:
    Team: api
    Group: web

Atmos exposes the Terraform variables passed on the command line in the tf_cli_vars section, which can be used in OPA policies for validation.

Terraform Variables Validation using OPA Policies

In atmos.yaml, configure the schemas.opa section:

# Validation schemas
schemas:
  # https://www.openpolicyagent.org
  opa:
    # Can also be set using `ATMOS_SCHEMAS_OPA_BASE_PATH` ENV var, or `--schemas-opa-dir` command-line arguments
    # Supports both absolute and relative paths
    base_path: "stacks/schemas/opa"

In the component manifest, add the settings.validation section to point to the OPA policy file:

components:
  terraform:
    my-component:
      settings:
        # All validation steps must succeed to allow the component to be provisioned
        validation:
          check-template-functions-test-component-with-opa-policy:
            schema_type: opa
            # 'schema_path' can be an absolute path or a path relative to 'schemas.opa.base_path' defined in `atmos.yaml`
            schema_path: "my-component/validate-my-component.rego"
            description: Check 'my-component' component using OPA policy
            # Validation timeout in seconds
            timeout: 5

Require a Terraform variable to be specified on the command line

If you need to enforce that a Terraform variable must be specified on the command line (and not in Atmos stack manifests),
add the following OPA policy in the file stacks/schemas/opa/my-component/validate-my-component.rego

# 'package atmos' is required in all `atmos` OPA policies
package atmos

# Atmos looks for the 'errors' (array of strings) output from all OPA policies.
# If the 'errors' output contains one or more error messages, Atmos considers the policy failed.

errors["for the 'my-component' component, the variable 'name' must be provided on the command line using the '-var' flag"] {
    not input.tf_cli_vars.name
}

When executing the following command (and not passing the name variable on the command line), Atmos will validate the component using the OPA policy, which will fail and prevent the component from being provisioned:

atmos terraform apply my-component -s plat-ue2-dev

Validating the component 'my-component' using OPA file 'my-component/validate-my-component.rego'

for the 'my-component' component, the variable 'name' must be provided on the command line using the '-var' flag

On the other hand, when passing the name variable on the command line using the -var name=api flag, the command will succeed:

atmos terraform apply my-component -s plat-ue2-dev -- -var name=api

Restrict a Terraform variable from being provided on the command line

If you need to prevent a Terraform variable from being passed (and overridden) on the command line, add the following OPA policy in the file stacks/schemas/opa/my-component/validate-my-component.rego

package atmos

errors["for the 'my-component' component, the variable 'name' cannot be overridden on the command line using the '-var' flag"] {
    input.tf_cli_vars.name
}

When executing the following command, Atmos will validate the component using the OPA policy, which will fail and prevent the component from being provisioned:

atmos terraform apply my-component -s plat-ue2-dev -- -var name=api

Validating the component 'my-component' using OPA file 'my-component/validate-my-component.rego'

for the 'my-component' component, the variable 'name' cannot be overridden on the command line using the '-var' flag

This command will pass the validation and succeed:

atmos terraform apply my-component -s plat-ue2-dev

references

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Added support for command-line variable validation using Open Policy Agent (OPA).
    • Enhanced Terraform variable processing with CLI flag precedence.
    • Introduced new output variable "tags" in Terraform configuration.
    • Added a new validation step for OPA schema validation in YAML configuration.
  • Documentation

    • Added documentation for Terraform input variables validation.
    • Updated Atlantis integration documentation.
  • Dependency Updates

    • Upgraded multiple Go dependencies to their latest versions.
    • Updated Atmos CLI version to 1.156.0 in examples and documentation.
  • Chores

    • Improved code readability with formatting changes.
    • Updated comment descriptions in various files.
    • Adjusted sidebar navigation order in documentation.

@aknysh aknysh requested a review from osterman January 27, 2025 05:44
@aknysh aknysh self-assigned this Jan 27, 2025
@aknysh aknysh requested review from a team as code owners January 27, 2025 05:44
@mergify mergify bot added the triage Needs triage label Jan 27, 2025
Copy link

mergify bot commented Jan 27, 2025

Important

Cloud Posse Engineering Team Review Required

This pull request modifies files that require Cloud Posse's review. Please be patient, and a core maintainer will review your changes.

To expedite this process, reach out to us on Slack in the #pr-reviews channel.

@mergify mergify bot added the needs-cloudposse Needs Cloud Posse assistance label Jan 27, 2025
@aknysh aknysh removed the triage Needs triage label Jan 27, 2025
@aknysh aknysh added the minor New features that do not break anything label Jan 27, 2025
Copy link
Contributor

coderabbitai bot commented Jan 27, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a series of incremental changes across the Atmos project, focusing on enhancing configuration management, documentation, and dependency updates. The modifications span multiple files, including utility functions, configuration handling, documentation, and dependency version upgrades. Key changes include clarifying function comments, reinstating necessary imports, updating the Atmos tool version, and adding validation capabilities for Terraform variables.

Changes

File Change Summary
cmd/cmd_utils.go Updated comment for getConfigAndStacksInfo function
cmd/terraform.go Reinstated cobra package import
examples/quick-start-advanced/Dockerfile Updated Atmos version to 1.156.0
go.mod Upgraded 28 dependency versions to their latest compatible releases
internal/exec/helmfile.go Reformatted ValidateComponent function call
internal/exec/terraform.go Added CLI variable handling and logging enhancements
internal/exec/utils.go Introduced getCliVars function for processing CLI variables
pkg/config/const.go Added TerraformCliVarsSectionName and CliArgsSectionName constants
tests/fixtures/scenarios/complete/... Added new OPA validation configurations and Terraform outputs
website/docs/... Updated documentation for Terraform variable validation and Atlantis integration

Possibly related PRs

Suggested Reviewers

  • osterman
  • Gowiem
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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. (Beta)
  • @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 or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title 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: 0

🧹 Nitpick comments (2)
website/docs/core-concepts/validate/terraform-variables.mdx (2)

44-55: Consider adding a visual diagram for variable precedence.

The variable precedence section is well-explained but could benefit from a visual representation to make it more intuitive.


171-173: Consider adding a troubleshooting section.

Since this introduces a new validation feature, it would be helpful to include common issues and their solutions.

🧰 Tools
🪛 LanguageTool

[typographical] ~171-~171: Consider using a typographic opening quote here.
Context: ...nt.rego"> ```rego package atmos errors["for the 'my-component' component, the va...

(EN_QUOTES)


[typographical] ~171-~171: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] { input.cli_vars.name } ``` </File...

(EN_QUOTES)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4cd79f1 and bb070c4.

⛔ Files ignored due to path filters (1)
  • go.sum is excluded by !**/*.sum
📒 Files selected for processing (14)
  • cmd/cmd_utils.go (1 hunks)
  • cmd/terraform.go (1 hunks)
  • examples/quick-start-advanced/Dockerfile (1 hunks)
  • go.mod (9 hunks)
  • internal/exec/helmfile.go (1 hunks)
  • internal/exec/terraform.go (2 hunks)
  • internal/exec/utils.go (2 hunks)
  • pkg/config/const.go (1 hunks)
  • tests/fixtures/scenarios/complete/components/terraform/test/template-functions-test/outputs.tf (1 hunks)
  • tests/fixtures/scenarios/complete/stacks/catalog/terraform/template-functions-test/defaults.yaml (1 hunks)
  • tests/fixtures/scenarios/complete/stacks/schemas/opa/test/template-functions-test/validate-template-functions-test-component.rego (1 hunks)
  • website/docs/core-concepts/validate/editorconfig.mdx (1 hunks)
  • website/docs/core-concepts/validate/terraform-variables.mdx (1 hunks)
  • website/docs/integrations/atlantis.mdx (1 hunks)
✅ Files skipped from review due to trivial changes (5)
  • cmd/terraform.go
  • internal/exec/helmfile.go
  • cmd/cmd_utils.go
  • website/docs/integrations/atlantis.mdx
  • website/docs/core-concepts/validate/editorconfig.mdx
👮 Files not reviewed due to content moderation or server errors (4)
  • tests/fixtures/scenarios/complete/stacks/schemas/opa/test/template-functions-test/validate-template-functions-test-component.rego
  • pkg/config/const.go
  • internal/exec/terraform.go
  • internal/exec/utils.go
🧰 Additional context used
📓 Learnings (1)
examples/quick-start-advanced/Dockerfile (2)
Learnt from: aknysh
PR: cloudposse/atmos#775
File: examples/quick-start-advanced/Dockerfile:9-9
Timestamp: 2024-11-12T05:52:05.088Z
Learning: It is acceptable to set `ARG ATMOS_VERSION` to a future version like `1.105.0` in `examples/quick-start-advanced/Dockerfile` if that will be the next release.
Learnt from: osterman
PR: cloudposse/atmos#801
File: examples/quick-start-advanced/Dockerfile:9-9
Timestamp: 2024-11-23T00:13:22.004Z
Learning: When updating the `ATMOS_VERSION` in Dockerfiles, the team prefers to pin to the next future version when the PR merges, even if the version is not yet released.
🪛 Regal (0.29.2)
tests/fixtures/scenarios/complete/stacks/schemas/opa/test/template-functions-test/validate-template-functions-test-component.rego

[error] 2-2: Directory structure should mirror package

(idiomatic)


[error] 2-2: Use import rego.v1

(imports)

🪛 LanguageTool
website/docs/core-concepts/validate/terraform-variables.mdx

[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...tack> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` ...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...ck> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...-- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::ti...

(EN_QUOTES)


[typographical] ~32-~32: Consider using typographic quotation marks here.
Context: ...-var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Double-...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...e=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Double-da...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...i -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Double-dash `...

(EN_QUOTES)


[typographical] ~135-~135: Consider using a typographic opening quote here.
Context: ...os considers the policy failed. errors["for the 'my-component' component, the va...

(EN_QUOTES)


[typographical] ~135-~135: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] { not input.cli_vars.name } ``` </...

(EN_QUOTES)


[typographical] ~171-~171: Consider using a typographic opening quote here.
Context: ...nt.rego"> ```rego package atmos errors["for the 'my-component' component, the va...

(EN_QUOTES)


[typographical] ~171-~171: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] { input.cli_vars.name } ``` </File...

(EN_QUOTES)

⏰ Context from checks skipped due to timeout of 90000ms (25)
  • GitHub Check: [mock-macos] tests/fixtures/scenarios/complete
  • GitHub Check: [mock-macos] examples/demo-vendoring
  • GitHub Check: [mock-macos] examples/demo-context
  • GitHub Check: [mock-macos] examples/demo-component-versions
  • GitHub Check: [mock-macos] examples/demo-atlantis
  • GitHub Check: [mock-windows] tests/fixtures/scenarios/complete
  • GitHub Check: [mock-windows] examples/demo-vendoring
  • GitHub Check: [mock-windows] examples/demo-context
  • GitHub Check: [mock-windows] examples/demo-component-versions
  • GitHub Check: [validate] demo-localstack
  • GitHub Check: [mock-windows] examples/demo-atlantis
  • GitHub Check: [mock-linux] tests/fixtures/scenarios/complete
  • GitHub Check: [mock-linux] examples/demo-vendoring
  • GitHub Check: [validate] demo-context
  • GitHub Check: [mock-linux] examples/demo-context
  • GitHub Check: [lint] demo-context
  • GitHub Check: [mock-linux] examples/demo-component-versions
  • GitHub Check: [mock-linux] examples/demo-atlantis
  • GitHub Check: Acceptance Tests (macos-latest, macos)
  • GitHub Check: [k3s] demo-helmfile
  • GitHub Check: Acceptance Tests (windows-latest, windows)
  • GitHub Check: Docker Lint
  • GitHub Check: Acceptance Tests (ubuntu-latest, linux)
  • GitHub Check: [localstack] demo-localstack
  • GitHub Check: Summary
🔇 Additional comments (4)
tests/fixtures/scenarios/complete/components/terraform/test/template-functions-test/outputs.tf (1)

24-27: LGTM! Clean addition of tags output.

The new output follows Terraform conventions and provides access to the module's tags.

examples/quick-start-advanced/Dockerfile (1)

9-9: LGTM! Version bump follows team conventions.

The update to ATMOS_VERSION=1.156.0 aligns with the team's practice of pinning to future versions.

tests/fixtures/scenarios/complete/stacks/catalog/terraform/template-functions-test/defaults.yaml (1)

13-31: LGTM! Well-structured OPA validation configuration.

The validation setup includes all necessary components:

  • Schema type and path
  • Additional module paths
  • Appropriate timeout setting

Let's verify the referenced OPA policy file exists:

✅ Verification successful

OPA policy file verification successful! ⚔️

The referenced OPA policy file exists at the correct location, ensuring the validation configuration will work as intended.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify OPA policy file exists
fd "validate-template-functions-test-component.rego" --type f

Length of output: 191

go.mod (1)

Line range hint 10-107: LGTM! Dependency updates look appropriate.

The version bumps appear to be routine maintenance updates, with notable updates to:

  • OPA (v1.0.0 → v1.0.1)
  • AWS SDK and related packages

Let's verify compatibility with the updated dependencies:

✅ Verification successful

Dependencies update verified - all clear! 🛡️

The dependency updates are safe to proceed:

  • OPA usage follows stable SDK patterns and the v1.0.0 → v1.0.1 is a patch update
  • AWS SDK usage is well-contained and follows standard v2 patterns
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any known compatibility issues with updated dependencies
curl -s "https://pkg.go.dev/github.com/open-policy-agent/[email protected]?tab=versions" | grep -A 5 "Breaking Changes"

Length of output: 114


Script:

#!/bin/bash
# Check for OPA imports and usage patterns
echo "=== OPA Usage ==="
rg --type go "github.com/open-policy-agent/opa" -l

echo -e "\n=== AWS SDK Usage ==="
rg --type go "github.com/aws/aws-sdk-go-v2" -l

# Check for specific AWS SDK service usage
echo -e "\n=== AWS SSM Usage ==="
rg --type go "github.com/aws/aws-sdk-go-v2/service/ssm" -l

Length of output: 529


Script:

#!/bin/bash
# Check OPA usage patterns
echo "=== OPA Usage Patterns ==="
rg --type go -A 5 -B 5 "github.com/open-policy-agent/opa" internal/exec/validate_utils.go

echo -e "\n=== AWS SSM Client Usage ==="
rg --type go -A 5 "NewFromConfig" pkg/store/aws_ssm_param_store.go

Length of output: 1174

coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 27, 2025
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: 0

🧹 Nitpick comments (3)
website/docs/core-concepts/validate/terraform-variables.mdx (3)

20-82: Consider adding security considerations for command-line variables.

While the documentation clearly explains variable precedence and usage, it would be beneficial to add security considerations about:

  • Protecting sensitive values in command-line arguments
  • Using var-files for sensitive data instead of command-line variables
  • Logging implications when using trace level with sensitive data
🧰 Tools
🪛 LanguageTool

[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...tack> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` ...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...ck> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...-- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::ti...

(EN_QUOTES)


[typographical] ~32-~32: Consider using typographic quotation marks here.
Context: ...-var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Double-...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...e=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Double-da...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...i -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Double-dash `...

(EN_QUOTES)


117-119: Add context for the timeout value.

The timeout setting would benefit from additional context:

  • What happens when timeout is reached?
  • What's a reasonable default value?
  • Are there recommendations for different scenarios?

122-196: Consider adding more complex policy examples.

The current examples are excellent for basic understanding. Consider adding:

  • Validation of variable formats (e.g., enforcing naming conventions)
  • Complex conditional rules (e.g., if var X is set, var Y must also be set)
  • Examples of combining multiple validation rules
🧰 Tools
🪛 LanguageTool

[typographical] ~135-~135: Consider using a typographic opening quote here.
Context: ...os considers the policy failed. errors["for the 'my-component' component, the va...

(EN_QUOTES)


[typographical] ~135-~135: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] { not input.cli_vars.name } ``` </...

(EN_QUOTES)


[typographical] ~171-~171: Consider using a typographic opening quote here.
Context: ...nt.rego"> ```rego package atmos errors["for the 'my-component' component, the va...

(EN_QUOTES)


[typographical] ~171-~171: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] { input.cli_vars.name } ``` </File...

(EN_QUOTES)

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between bb070c4 and 15807b6.

📒 Files selected for processing (1)
  • website/docs/core-concepts/validate/terraform-variables.mdx (1 hunks)
🧰 Additional context used
🪛 LanguageTool
website/docs/core-concepts/validate/terraform-variables.mdx

[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...tack> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` ...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...ck> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...-- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::ti...

(EN_QUOTES)


[typographical] ~32-~32: Consider using typographic quotation marks here.
Context: ...-var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Double-...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...e=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Double-da...

(EN_QUOTES)


[typographical] ~32-~32: Consider using a typographic close quote here.
Context: ...i -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Double-dash `...

(EN_QUOTES)


[typographical] ~135-~135: Consider using a typographic opening quote here.
Context: ...os considers the policy failed. errors["for the 'my-component' component, the va...

(EN_QUOTES)


[typographical] ~135-~135: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] { not input.cli_vars.name } ``` </...

(EN_QUOTES)


[typographical] ~171-~171: Consider using a typographic opening quote here.
Context: ...nt.rego"> ```rego package atmos errors["for the 'my-component' component, the va...

(EN_QUOTES)


[typographical] ~171-~171: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] { input.cli_vars.name } ``` </File...

(EN_QUOTES)

⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: Build (windows-latest, windows)
  • GitHub Check: Build (ubuntu-latest, linux)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: website-deploy-preview
  • GitHub Check: Analyze (go)
  • GitHub Check: Summary
🔇 Additional comments (1)
website/docs/core-concepts/validate/terraform-variables.mdx (1)

1-13: Well-structured metadata and imports!

The file metadata and component imports are properly organized and follow documentation best practices.

coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 27, 2025
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: 0

🧹 Nitpick comments (6)
internal/exec/utils.go (2)

669-678: LGTM! Consider adding debug logging for CLI args.

The implementation correctly adds CLI arguments and variables to the component section, enabling validation through OPA policies. The error handling is robust.

Consider adding debug logging for CLI args similar to how CLI vars are logged:

 configAndStacksInfo.ComponentSection[cfg.CliArgsSectionName] = configAndStacksInfo.AdditionalArgsAndFlags
+if atmosConfig.Logs.Level == u.LogLevelTrace || atmosConfig.Logs.Level == u.LogLevelDebug {
+    u.LogDebug(atmosConfig, "\nCLI arguments:")
+    err = u.PrintAsYAMLToFileDescriptor(atmosConfig, configAndStacksInfo.AdditionalArgsAndFlags)
+    if err != nil {
+        return configAndStacksInfo, err
+    }
+}

1214-1242: Add input validation and improve documentation.

While the implementation is solid, consider these improvements:

  1. Add validation for empty or malformed var names
  2. Add validation for empty values
  3. Add function documentation with examples

Here's a suggested improvement:

+// getCliVars parses command-line variables provided with the -var flag and returns them as a map.
+// It supports both simple string values and JSON-formatted values.
+//
+// Example usage:
+//   -var name=test           -> {"name": "test"}
+//   -var 'tags={"a":"b"}'    -> {"tags": {"a": "b"}}
 func getCliVars(args []string) (map[string]any, error) {
 	var variables = make(map[string]any)
 	for i := 0; i < len(args); i++ {
 		if args[i] == "-var" && i+1 < len(args) {
 			kv := args[i+1]
 			parts := strings.SplitN(kv, "=", 2)
 			if len(parts) == 2 {
 				varName := parts[0]
+				if varName == "" {
+					return nil, fmt.Errorf("empty variable name in -var flag")
+				}
 				part2 := parts[1]
+				if part2 == "" {
+					return nil, fmt.Errorf("empty value for variable %q", varName)
+				}
 				var varValue any
 				if u.IsJSON(part2) {
tests/fixtures/scenarios/complete/stacks/schemas/opa/test/template-functions-test/validate-template-functions-test-component.rego (2)

7-9: Consider adding type checking for the name variable.

While the rule correctly checks for the presence of the name variable, it could benefit from type checking.

Add type checking to ensure the name variable is a string:

 errors["for the 'template-functions-test' component, the variable 'name' must be provided on the command line using the '-var' flag"] {
     not input.tf_cli_vars.name
 }
+
+errors["for the 'template-functions-test' component, the variable 'name' must be a string"] {
+    input.tf_cli_vars.name
+    not is_string(input.tf_cli_vars.name)
+}

11-21: Consider organizing reference links by category.

The reference links are valuable but could be better organized for clarity.

Consider organizing the links with comments:

-# https://www.openpolicyagent.org/docs/latest/policy-language
-# https://www.openpolicyagent.org/
-# https://blog.openpolicyagent.org/rego-design-principle-1-syntax-should-reflect-real-world-policies-e1a801ab8bfb
-# https://github.com/open-policy-agent/library
-# https://github.com/open-policy-agent/example-api-authz-go
-# https://github.com/open-policy-agent/opa/issues/2104
-# https://www.fugue.co/blog/5-tips-for-using-the-rego-language-for-open-policy-agent-opa
-# https://medium.com/@agarwalshubhi17/rego-cheat-sheet-5e25faa6eee8
-# https://code.tutsplus.com/tutorials/regular-expressions-with-go-part-1--cms-30403
-# https://www.styra.com/blog/how-to-write-your-first-rules-in-rego-the-policy-language-for-opa
-# https://www.openpolicyagent.org/docs/v0.12.2/how-does-opa-work
+# Official Documentation
+# https://www.openpolicyagent.org/
+# https://www.openpolicyagent.org/docs/latest/policy-language
+# https://www.openpolicyagent.org/docs/v0.12.2/how-does-opa-work
+
+# Examples and Libraries
+# https://github.com/open-policy-agent/library
+# https://github.com/open-policy-agent/example-api-authz-go
+
+# Best Practices and Tips
+# https://blog.openpolicyagent.org/rego-design-principle-1-syntax-should-reflect-real-world-policies-e1a801ab8bfb
+# https://www.fugue.co/blog/5-tips-for-using-the-rego-language-for-open-policy-agent-opa
+# https://www.styra.com/blog/how-to-write-your-first-rules-in-rego-the-policy-language-for-opa
+
+# Tutorials and Cheat Sheets
+# https://medium.com/@agarwalshubhi17/rego-cheat-sheet-5e25faa6eee8
+# https://code.tutsplus.com/tutorials/regular-expressions-with-go-part-1--cms-30403
+
+# Known Issues
+# https://github.com/open-policy-agent/opa/issues/2104
website/docs/core-concepts/validate/terraform-variables.mdx (2)

24-54: Consider adding more complex examples.

While the examples are clear, consider adding more complex scenarios.

Add examples for:

  1. Multiple variables with JSON values
  2. Variables with special characters
  3. Common error scenarios to avoid

Example addition:

 atmos terraform apply <component> -s <stack> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}'
+
+# Example with multiple JSON values and special characters
+atmos terraform apply <component> -s <stack> -- \
+  -var 'config={"api_key":"abc-123"}' \
+  -var 'tags={"Environment":"prod"}' \
+  -var 'name=my-special-app'
+
+# Common pitfalls to avoid
+# DON'T: Missing quotes around JSON
+# atmos terraform apply <component> -s <stack> -- -var tags={"key":"value"}
+
+# DO: Properly quote JSON
+# atmos terraform apply <component> -s <stack> -- -var 'tags={"key":"value"}'
🧰 Tools
🪛 LanguageTool

[typographical] ~30-~30: Consider using a typographic close quote here.
Context: ...tack> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` ...

(EN_QUOTES)


[typographical] ~30-~30: Consider using a typographic close quote here.
Context: ...ck> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :...

(EN_QUOTES)


[typographical] ~30-~30: Consider using a typographic close quote here.
Context: ...-- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::ti...

(EN_QUOTES)


[typographical] ~30-~30: Consider using typographic quotation marks here.
Context: ...-var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Use dou...

(EN_QUOTES)


[typographical] ~30-~30: Consider using a typographic close quote here.
Context: ...e=api -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Use doubl...

(EN_QUOTES)


[typographical] ~30-~30: Consider using a typographic close quote here.
Context: ...i -var 'tags={"Team":"api", "Group":"web"}' ``` :::tip Use double-da...

(EN_QUOTES)


80-194: Add troubleshooting section for common issues.

The configuration and examples are clear, but adding troubleshooting guidance would be helpful.

Consider adding a troubleshooting section:

### Troubleshooting

Common issues and solutions:

1. Policy not being applied
   ```console
   # Check if the policy file exists
   ls stacks/schemas/opa/my-component/validate-my-component.rego
   
   # Verify policy syntax
   opa check stacks/schemas/opa/my-component/validate-my-component.rego
  1. Unexpected validation failures

    • Ensure variable names match exactly (they are case-sensitive)
    • Check for whitespace in variable values
    • Verify JSON formatting in complex values
  2. Debug using trace logs

    ATMOS_LOGS_LEVEL=Trace atmos terraform plan ...

<details>
<summary>🧰 Tools</summary>

<details>
<summary>🪛 LanguageTool</summary>

[typographical] ~133-~133: Consider using a typographic opening quote here.
Context: ...os considers the policy failed.  errors["for the 'my-component' component, the va...

(EN_QUOTES)

---

[typographical] ~133-~133: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] {     not input.tf_cli_vars.name } ```...

(EN_QUOTES)

---

[typographical] ~169-~169: Consider using a typographic opening quote here.
Context: ...nt.rego"> ```rego package atmos  errors["for the 'my-component' component, the va...

(EN_QUOTES)

---

[typographical] ~169-~169: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] {     input.tf_cli_vars.name } ``` </F...

(EN_QUOTES)

</details>

</details>

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

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

<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between 15807b6c1dfe7dbc44d9e4e45eab3564361724f8 and 7c3372af41946d00353d0d1b0c04250701e7eb66.

</details>

<details>
<summary>📒 Files selected for processing (5)</summary>

* `internal/exec/terraform.go` (2 hunks)
* `internal/exec/utils.go` (2 hunks)
* `pkg/config/const.go` (1 hunks)
* `tests/fixtures/scenarios/complete/stacks/schemas/opa/test/template-functions-test/validate-template-functions-test-component.rego` (1 hunks)
* `website/docs/core-concepts/validate/terraform-variables.mdx` (1 hunks)

</details>

<details>
<summary>🚧 Files skipped from review as they are similar to previous changes (1)</summary>

* pkg/config/const.go

</details>

<details>
<summary>🧰 Additional context used</summary>

<details>
<summary>🪛 Regal (0.29.2)</summary>

<details>
<summary>tests/fixtures/scenarios/complete/stacks/schemas/opa/test/template-functions-test/validate-template-functions-test-component.rego</summary>

[error] 2-2: Directory structure should mirror package

(idiomatic)

---

[error] 2-2: Use `import rego.v1`

(imports)

</details>

</details>
<details>
<summary>🪛 LanguageTool</summary>

<details>
<summary>website/docs/core-concepts/validate/terraform-variables.mdx</summary>

[typographical] ~30-~30: Consider using a typographic close quote here.
Context: ...tack> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` </Terminal> ...

(EN_QUOTES)

---

[typographical] ~30-~30: Consider using a typographic close quote here.
Context: ...ck> -- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` </Terminal>  :...

(EN_QUOTES)

---

[typographical] ~30-~30: Consider using a typographic close quote here.
Context: ...-- -var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` </Terminal>  :::ti...

(EN_QUOTES)

---

[typographical] ~30-~30: Consider using typographic quotation marks here.
Context: ...-var name=api -var 'tags={"Team":"api", "Group":"web"}' ``` </Terminal>  :::tip Use dou...

(EN_QUOTES)

---

[typographical] ~30-~30: Consider using a typographic close quote here.
Context: ...e=api -var 'tags={"Team":"api", "Group":"web"}' ``` </Terminal>  :::tip Use doubl...

(EN_QUOTES)

---

[typographical] ~30-~30: Consider using a typographic close quote here.
Context: ...i -var 'tags={"Team":"api", "Group":"web"}' ``` </Terminal>  :::tip Use double-da...

(EN_QUOTES)

---

[typographical] ~133-~133: Consider using a typographic opening quote here.
Context: ...os considers the policy failed.  errors["for the 'my-component' component, the va...

(EN_QUOTES)

---

[typographical] ~133-~133: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] {     not input.tf_cli_vars.name } ```...

(EN_QUOTES)

---

[typographical] ~169-~169: Consider using a typographic opening quote here.
Context: ...nt.rego"> ```rego package atmos  errors["for the 'my-component' component, the va...

(EN_QUOTES)

---

[typographical] ~169-~169: Consider using a typographic close quote here.
Context: ...n the command line using the '-var' flag"] {     input.tf_cli_vars.name } ``` </F...

(EN_QUOTES)

</details>

</details>

</details>

<details>
<summary>⏰ Context from checks skipped due to timeout of 90000ms (2)</summary>

* GitHub Check: Acceptance Tests (windows-latest, windows)
* GitHub Check: Summary

</details>

<details>
<summary>🔇 Additional comments (5)</summary><blockquote>

<details>
<summary>internal/exec/terraform.go (2)</summary>

`221-239`: **LGTM! Clear documentation of variable precedence.**

The documentation block and debug logging implementation effectively communicate the variable precedence order, with proper references to Terraform's documentation.

---

`248-256`: **LGTM! Improved code formatting.**

The reformatting of the ValidateComponent function call improves readability without changing functionality.

</details>
<details>
<summary>tests/fixtures/scenarios/complete/stacks/schemas/opa/test/template-functions-test/validate-template-functions-test-component.rego (1)</summary>

`1-6`: **LGTM! Clear documentation of OPA policy requirements.**

The comments effectively explain the package requirement and how Atmos processes policy failures.

<details>
<summary>🧰 Tools</summary>

<details>
<summary>🪛 Regal (0.29.2)</summary>

[error] 2-2: Directory structure should mirror package

(idiomatic)

---

[error] 2-2: Use `import rego.v1`

(imports)

</details>

</details>

</details>
<details>
<summary>website/docs/core-concepts/validate/terraform-variables.mdx (2)</summary>

`1-23`: **LGTM! Clear and well-structured documentation header.**

The metadata, imports, and introduction effectively set up the documentation.

---

`55-79`: **LGTM! Comprehensive logging example.**

The trace logging example effectively demonstrates the output format and variable structure.

</details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

@aknysh aknysh merged commit 255a05a into main Jan 27, 2025
45 checks passed
@aknysh aknysh deleted the add-cli-vars-detection branch January 27, 2025 18:01
@mergify mergify bot removed the needs-cloudposse Needs Cloud Posse assistance label Jan 27, 2025
Copy link

These changes were released in v1.156.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor New features that do not break anything
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants