|
| 1 | +--- |
| 2 | +title: Security Best Practices |
| 3 | +weight: 3 |
| 4 | +variants: -flyte -serverless -byoc +selfmanaged |
| 5 | +--- |
| 6 | + |
| 7 | +# Security Best Practices |
| 8 | + |
| 9 | +**Never hardcode API keys directly in your Terraform configuration files.** API keys are sensitive credentials that should be stored securely and never committed to version control. |
| 10 | + |
| 11 | +## Recommended Approaches |
| 12 | + |
| 13 | +### 1. Use Cloud Secret Managers |
| 14 | + |
| 15 | +Store your Union API key in a cloud-based secret manager and retrieve it dynamically: |
| 16 | + |
| 17 | +#### AWS Secrets Manager |
| 18 | + |
| 19 | +```hcl |
| 20 | +data "aws_secretsmanager_secret" "unionai_api_key" { |
| 21 | + name = "unionai/terraform-api-key" |
| 22 | +} |
| 23 | +
|
| 24 | +data "aws_secretsmanager_secret_version" "unionai_api_key" { |
| 25 | + secret_id = data.aws_secretsmanager_secret.unionai_api_key.id |
| 26 | +} |
| 27 | +
|
| 28 | +provider "unionai" { |
| 29 | + api_key = data.aws_secretsmanager_secret_version.unionai_api_key.secret_string |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +#### Google Cloud Secret Manager |
| 34 | + |
| 35 | +```hcl |
| 36 | +data "google_secret_manager_secret_version" "unionai_api_key" { |
| 37 | + secret = "unionai-terraform-api-key" |
| 38 | + project = "your-project-id" |
| 39 | +} |
| 40 | +
|
| 41 | +provider "unionai" { |
| 42 | + api_key = data.google_secret_manager_secret_version.unionai_api_key.secret_data |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +#### Azure Key Vault |
| 47 | + |
| 48 | +```hcl |
| 49 | +data "azurerm_key_vault" "main" { |
| 50 | + name = "your-keyvault-name" |
| 51 | + resource_group_name = "your-resource-group" |
| 52 | +} |
| 53 | +
|
| 54 | +data "azurerm_key_vault_secret" "unionai_api_key" { |
| 55 | + name = "unionai-api-key" |
| 56 | + key_vault_id = data.azurerm_key_vault.main.id |
| 57 | +} |
| 58 | +
|
| 59 | +provider "unionai" { |
| 60 | + api_key = data.azurerm_key_vault_secret.unionai_api_key.value |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +### 2. Use HashiCorp Vault |
| 65 | + |
| 66 | +For multi-cloud or on-premises deployments, HashiCorp Vault provides centralized secret management: |
| 67 | + |
| 68 | +```hcl |
| 69 | +data "vault_generic_secret" "unionai_api_key" { |
| 70 | + path = "secret/terraform/unionai" |
| 71 | +} |
| 72 | +
|
| 73 | +provider "unionai" { |
| 74 | + api_key = data.vault_generic_secret.unionai_api_key.data["api_key"] |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### 3. Use Environment Variables |
| 79 | + |
| 80 | +For local development or CI/CD pipelines, use environment variables: |
| 81 | + |
| 82 | +```bash |
| 83 | +export UNIONAI_API_KEY="your-api-key-here" |
| 84 | +``` |
| 85 | + |
| 86 | +The provider will automatically read from the `UNIONAI_API_KEY` environment variable: |
| 87 | + |
| 88 | +```hcl |
| 89 | +provider "unionai" { |
| 90 | + # api_key is read from UNIONAI_API_KEY environment variable |
| 91 | +} |
| 92 | +``` |
| 93 | + |
| 94 | +### 4. Use Terraform Variables with `.tfvars` Files |
| 95 | + |
| 96 | +If using variable files, ensure they are excluded from version control: |
| 97 | + |
| 98 | +```hcl |
| 99 | +# variables.tf |
| 100 | +variable "unionai_api_key" { |
| 101 | + description = "Union API key" |
| 102 | + type = string |
| 103 | + sensitive = true |
| 104 | +} |
| 105 | +
|
| 106 | +# main.tf |
| 107 | +provider "unionai" { |
| 108 | + api_key = var.unionai_api_key |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +Create a `terraform.tfvars` file (add to `.gitignore`): |
| 113 | + |
| 114 | +```hcl |
| 115 | +unionai_api_key = "your-api-key-here" |
| 116 | +``` |
| 117 | + |
| 118 | +## Additional Security Measures |
| 119 | + |
| 120 | +### Encrypt Terraform State |
| 121 | + |
| 122 | +Always use encrypted remote state backends to protect sensitive data: |
| 123 | + |
| 124 | +```hcl |
| 125 | +terraform { |
| 126 | + backend "s3" { |
| 127 | + bucket = "my-terraform-state" |
| 128 | + key = "union/terraform.tfstate" |
| 129 | + region = "us-west-2" |
| 130 | + encrypt = true |
| 131 | + dynamodb_table = "terraform-state-lock" |
| 132 | + } |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +### Use State Locking |
| 137 | + |
| 138 | +Enable state locking to prevent concurrent modifications: |
| 139 | + |
| 140 | +- **AWS S3**: Use DynamoDB for state locking |
| 141 | +- **Google Cloud Storage**: Automatic state locking |
| 142 | +- **Azure Blob Storage**: Automatic state locking |
| 143 | + |
| 144 | +### Rotate API Keys Regularly |
| 145 | + |
| 146 | +Implement a rotation schedule for your API keys: |
| 147 | + |
| 148 | +1. Generate a new API key using the Flyte CLI |
| 149 | +2. Update the key in your secret manager |
| 150 | +3. Verify Terraform can authenticate with the new key |
| 151 | +4. Delete the old API key |
| 152 | + |
| 153 | +### Restrict Provider Permissions |
| 154 | + |
| 155 | +Use the `allowed_orgs` parameter to limit which organizations the provider can access: |
| 156 | + |
| 157 | +```hcl |
| 158 | +provider "unionai" { |
| 159 | + api_key = var.unionai_api_key |
| 160 | + allowed_orgs = ["production-org"] |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +This prevents accidental operations on the wrong organization. |
| 165 | + |
| 166 | +### Use Separate API Keys per Environment |
| 167 | + |
| 168 | +Create different API keys for each environment (development, staging, production): |
| 169 | + |
| 170 | +```hcl |
| 171 | +# Development |
| 172 | +provider "unionai" { |
| 173 | + alias = "dev" |
| 174 | + api_key = var.dev_api_key |
| 175 | +} |
| 176 | +
|
| 177 | +# Production |
| 178 | +provider "unionai" { |
| 179 | + alias = "prod" |
| 180 | + api_key = var.prod_api_key |
| 181 | +} |
| 182 | +``` |
| 183 | + |
| 184 | +## Security Checklist |
| 185 | + |
| 186 | +- ✅ Store API keys in a secret manager or secure vault |
| 187 | +- ✅ Use environment variables for local development |
| 188 | +- ✅ Mark variables containing secrets as `sensitive = true` |
| 189 | +- ✅ Add `*.tfvars`, `*.tfstate`, and `*.tfstate.backup` to `.gitignore` |
| 190 | +- ✅ Use remote state backends with encryption enabled |
| 191 | +- ✅ Enable state locking to prevent concurrent modifications |
| 192 | +- ✅ Rotate API keys regularly |
| 193 | +- ✅ Use separate API keys per environment |
| 194 | +- ✅ Restrict provider access with `allowed_orgs` |
| 195 | +- ✅ Review Terraform plans before applying changes |
| 196 | +- ❌ Never commit API keys to version control |
| 197 | +- ❌ Never hardcode API keys in `.tf` files |
| 198 | +- ❌ Never share API keys in plain text (chat, email, etc.) |
| 199 | +- ❌ Never use production API keys in development environments |
| 200 | + |
| 201 | +## CI/CD Pipeline Security |
| 202 | + |
| 203 | +When using Terraform in CI/CD pipelines: |
| 204 | + |
| 205 | +### GitHub Actions |
| 206 | + |
| 207 | +```yaml |
| 208 | +name: Terraform |
| 209 | + |
| 210 | +on: |
| 211 | + push: |
| 212 | + branches: [main] |
| 213 | + |
| 214 | +jobs: |
| 215 | + terraform: |
| 216 | + runs-on: ubuntu-latest |
| 217 | + steps: |
| 218 | + - uses: actions/checkout@v3 |
| 219 | + |
| 220 | + - name: Setup Terraform |
| 221 | + uses: hashicorp/setup-terraform@v2 |
| 222 | + |
| 223 | + - name: Terraform Init |
| 224 | + env: |
| 225 | + UNIONAI_API_KEY: ${{ secrets.UNIONAI_API_KEY }} |
| 226 | + run: terraform init |
| 227 | + |
| 228 | + - name: Terraform Apply |
| 229 | + env: |
| 230 | + UNIONAI_API_KEY: ${{ secrets.UNIONAI_API_KEY }} |
| 231 | + run: terraform apply -auto-approve |
| 232 | +``` |
| 233 | +
|
| 234 | +### GitLab CI |
| 235 | +
|
| 236 | +```yaml |
| 237 | +terraform: |
| 238 | + image: hashicorp/terraform:latest |
| 239 | + variables: |
| 240 | + UNIONAI_API_KEY: $UNIONAI_API_KEY |
| 241 | + script: |
| 242 | + - terraform init |
| 243 | + - terraform apply -auto-approve |
| 244 | + only: |
| 245 | + - main |
| 246 | +``` |
| 247 | +
|
| 248 | +### Best Practices for CI/CD |
| 249 | +
|
| 250 | +- Store API keys as encrypted secrets in your CI/CD platform |
| 251 | +- Use separate API keys for CI/CD (not personal keys) |
| 252 | +- Implement approval gates for production deployments |
| 253 | +- Enable audit logging for all Terraform operations |
| 254 | +- Restrict who can view/modify CI/CD secrets |
| 255 | +
|
| 256 | +## Additional Resources |
| 257 | +
|
| 258 | +- [Terraform Security Best Practices](https://developer.hashicorp.com/terraform/tutorials/configuration-language/sensitive-variables) |
| 259 | +- [HashiCorp Vault Documentation](https://developer.hashicorp.com/vault/docs) |
| 260 | +- [Flyte CLI Documentation](../../api-reference/flyte-cli) |
0 commit comments