Terrakube Documentation

repository·main·Indexed 21 days ago

https://github.com/terrakube-io/terrakube

An open-source platform for Infrastructure as Code (IaC) automation and collaboration, providing a centralized interface for managing Terraform and OpenTofu workflows, private registries, and multi-VCS integrations. Documentation covers Azure DevOps VCS provider configuration (including service hooks and outbound commit polling), local deployment via Docker Compose with HTTPS using mkcert, and the setup of dynamic credentials for AWS and Azure using Web Identity Federation and OIDC.

Tokens
13.1K
Snippets
37
Records
55
Agent score
76%

What's inside Terrakube

  1. Overview of Terrakube features

    main

    Terrakube is an open-source IaC (Infrastructure as Code) Automation and Collaboration software. Key capabilities include:

    • Private Registry: Host and publish Terraform Modules and Providers privately.
    • Organizations and Workspaces: Structured management of infrastructure using Organizations and Workspaces. Workspaces support Tags for grouping, organizing, and filtering.
    • Version Control Integration: Connects with GitHub (Cloud and Enterprise), GitLab (EE and CE), Bitbucket, and Azure DevOps to manage Terraform/OpenTofu code.
    • Terraform Workflow: Execute terraform apply, terraform plan, and terraform destroy from anywhere. Includes management of variables and Terraform state files.
    • Custom Workflows: Extend IaC workflows using tools like OPA or Infracost via Terrakube extensions or the Terrakube API (e.g., for compliance, cost estimation, or security scans).
    • Access Control: Integrates with Dex to support identity providers like Azure Active Directory, Amazon Cognito, GitHub, and SAML. Supports granular permissions for Workspaces, Modules, and VCS using existing groups.
    • Remote Backend: Supports both remote backend and cloud blocks, allowing workflows to be run directly from the Terraform/OpenTofu CLI.
  2. Use AWS Session Tags (ABAC) for Workspace Scoping

    main

    Terrakube supports Attribute-Based Access Control (ABAC) by passing session tags during the AWS AssumeRoleWithWebIdentity call. This allows a single IAM role to serve multiple workspaces by scoping permissions based on the workspace name.

    Enabling Session Tags

    Session tags are opt-in. To enable them, you must set the following environment variable in your Terrakube workspace:

    • ENABLE_AWS_SESSION_TAGS=true

    Warning: Enabling this requires the sts:TagSession permission in your IAM role's trust policy. If this permission is missing, the identity assumption will fail.

    Available Transitive Principal Tags

    When enabled, Terrakube sends the following tags:

    • terrakube:org: The organization name.
    • terrakube:workspace: The workspace name.
    • terrakube:project: The project name (only if the workspace belongs to a project).

    IAM Trust Policy Requirements

    To use session tags, your IAM Role Trust Policy must:

    1. Allow the sts:TagSession action.
    2. Use a ForAllValues:StringEquals condition on aws:TagKeys to restrict which tags Terrakube can pass. This prevents unauthorized tag injection.

    Example Trust Policy snippet:

    "Action": [
      "sts:AssumeRoleWithWebIdentity",
      "sts:TagSession"
    ],
    "Condition": {
      "StringEquals": {
        "terrakube-api.mydomain.com:aud": "aws.workload.identity"
      },
      "StringLike": {
        "terrakube-api.mydomain.com:sub": "organization:my-org:workspace:*"
      },
      "ForAllValues:StringEquals": {
        "aws:TagKeys": [
          "terrakube:org",
          "terrakube:workspace",
          "terrakube:project"
        ]
      }
    }
  3. Use outbound commit polling for private networks

    main

    If Terrakube is running on a private network that cannot be reached by the Azure DevOps cloud, inbound service hooks will fail. In this scenario, you should opt-in to outbound commit polling.

    Polling reverses the communication direction: Terrakube periodically makes outbound calls to fetch the tip commit of each workspace branch. This requires no inbound network exposure.

    Behavior

    • Latency: Detection latency is close to the configured interval.
    • State: The last-seen commit per workspace/branch is stored in Redis using the key pattern azdo-poll:{workspaceId}:{branch}.
    • Initial Run: The first observation only records a baseline and does not trigger a run.
    • Filtering: Event matching (branch and file-path filters) is applied exactly as it is for inbound pushes.
  4. Use inbound service hooks for Azure DevOps triggers

    main

    By default, Terrakube uses inbound service hooks to trigger runs when code changes. When a workspace is saved, Terrakube automatically creates an Azure DevOps service hook subscription that posts to the Terrakube webhook endpoint.

    Requirements

    • Network: The Terrakube webhook endpoint must be reachable from the public internet (since hooks are delivered from Azure DevOps cloud).
    • Permissions: The service principal or token used must have Edit Subscriptions permission to create the hooks.

    Supported Events

    EventeventTyperesourceVersion
    Code pushedgit.push2.0
    Pull request createdgit.pullrequest.created2.0
    Pull request updatedgit.pullrequest.updated2.0
    Pull request commentms.vss-code.git-pullrequest-comment-event1.0

    Each delivery is authenticated using a per-webhook x-terrakube-token header. Terrakube reports commit status back to Azure DevOps as the run progresses.

  5. Initialize OpenBao for Terrakube Testing

    main

    Use these commands to set up a development environment with OpenBao, including enabling auth methods (userpass, approle, jwt), setting up KV secrets, and applying the Terrakube policy and JWT role.

    # Start OpenBao in dev mode
    bao server -dev -dev-root-token-id="dev-only-token" 
    
    # Setup configuration
    export VAULT_ADDR='http://localhost:8200'
    bao login dev-only-token
    bao auth enable userpass
    bao write auth/userpass/users/terrakube password=p@ssw0rd
    bao auth enable approle
    bao write auth/approle/role/terrakube-role secret_id_ttl=10m token_ttl=20m token_max_ttl=30m
    bao secrets enable -path shared -version 2 kv
    bao kv put -mount shared kv/creds username=terrakube password=p@ssw0rd
    bao auth enable jwt
    bao write auth/jwt/config oidc_discovery_url="https://terrakube-api.platform.local" bound_issuer="https://terrakube-api.platform.local" 
    bao policy write tfc-policy terrakube-policy.hcl
    bao write auth/jwt/role/tfc-role @vault-jwt-auth-role.json
  6. Build and run Terrakube UI locally

    main

    After generating your .env file using ./setupEnv.sh, you can build and start the application using yarn. The UI uses a static configuration file env-config.js (generated via env.sh) to load these values at runtime.

    Follow these steps to start the development environment:

    corepack enable
    yarn install
    yarn start
  7. Generate local HTTPS certificates with mkcert

    main

    To run Terrakube locally with HTTPS, use mkcert to manage local certificates and trust.

    1. Install the local CA into your system trust store:
      mkcert -install
    2. Generate certificates for the Terrakube domains:
      mkcert -key-file key.pem -cert-file cert.pem platform.local *.platform.local
    mkcert -install
    mkcert -key-file key.pem -cert-file cert.pem platform.local *.platform.local
  8. Install and test Terrakube

    main

    Terrakube can be deployed or tested using several methods depending on your environment:

    • Helm: Install Terrakube using Helm charts.
    • Docker Compose: Deploy using Docker Compose.
    • Minikube: Test the deployment using Minikube.
    • Gitpod: Test Terrakube in a cloud-based development environment via Gitpod.
    • GitHub Codespaces: Quickly test Terrakube without local installation using GitHub Codespaces.
  9. Configure Terrakube UI environment variables

    main

    Before compiling or running the Terrakube UI, you must define several environment variables on your local machine. These variables are used to configure the connection to the Terrakube API, Dex authentication, and registry settings.

    To simplify this, run the ./setupEnv.sh script to generate a .env file in your local directory. The script uses the following mapping for the generated .env file:

    Local Env VarGenerated .env KeyDescription
    REACT_CONFIG_TERRAKUBE_URLREACT_APP_TERRAKUBE_API_URLTerrakube API URL (e.g., https://someURL/terrakube-api/api/v1/)
    REACT_CONFIG_CLIENT_IDREACT_APP_CLIENT_IDDex Issuer Application ID
    REACT_CONFIG_AUTHORITYREACT_APP_AUTHORITYDex Issuer URL (from .well-known/openid-configuration)
    REACT_CONFIG_REDIRECTREACT_APP_REDIRECT_URIRedirect URL (e.g., http://localhost:3000)
    REACT_CONFIG_BASE_PATHREACT_APP_BASE_PATHUI base path (/ for root or /ui for subpath)
    REACT_CONFIG_REGISTRY_URIREACT_APP_REGISTRY_URITerrakube Registry URL
    REACT_CONFIG_SCOPEREACT_APP_SCOPEDex Scopes (e.g., email openid profile offline_access groups)
    REACT_CONFIG_TERRAKUBE_SEND_COOKIESREACT_APP_TERRAKUBE_SEND_COOKIESWhether to send browser cookies with API requests (true/false)
    ./setupEnv.sh
  10. Configure Docker network and local DNS for Terrakube

    main

    Before starting Terrakube, you must create a specific Docker bridge network and update your host's DNS entries to point the local domains to the Traefik gateway (10.25.25.253).

    1. Create the Docker network:

      docker network create terrakube-network -d bridge --subnet 10.25.25.0/24 --gateway 10.25.25.254
    2. Update /etc/hosts: Add the following entries to map the domains to the gateway IP:

      10.25.25.253 terrakube.platform.local
      10.25.25.253 terrakube-api.platform.local
      10.25.25.253 terrakube-registry.platform.local
      10.25.25.253 terrakube-dex.platform.local
    docker network create terrakube-network -d bridge --subnet 10.25.25.0/24 --gateway 10.25.25.254