Skip to content

Commit fe51204

Browse files
feat: add terraform_provider_constraints rule for strict provider validation
Adds a new rule that enforces specific provider configurations across Terraform modules. This rule extends terraform_required_providers with strict constraint validation capabilities. Features: - Validate provider sources match expected values (e.g., enforce official namespaces) - Validate version constraints using exact structural matching (via go-version) - Restrict modules to only use providers from an allowed list - Autofix capability to update non-compliant provider configurations Configuration: ```hcl rule "terraform_provider_constraints" { enabled = true # Optional: Restrict to only these providers allowed_providers_only = true providers = { aws = { source = "hashicorp/aws" # Validates source if specified version = "~> 5.0" # Validates version constraint if specified } } } ``` Key behaviors: - Source and version validations are independent (only validate what's configured) - Uses go-version's structural equality for constraints (e.g., "~> 4.0" != ">= 4.0, < 5.0") - When allowed_providers_only=true, modules can only use providers in the configured list - Ignores the builtin "terraform" provider Use cases: - Monorepo provider coordination during major upgrades - Enforcing organizational standards for provider sources - Ensuring consistent version constraint patterns across modules - Automated compliance via tflint --fix This addresses provider version management challenges in large-scale Terraform environments where coordinated upgrades and consistent standards are essential.
1 parent f5dbd18 commit fe51204

File tree

3 files changed

+675
-2
lines changed

3 files changed

+675
-2
lines changed

docs/rules/terraform_required_providers.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# terraform_required_providers
22

3-
Require that all providers specify a `source` and `version` constraint through `required_providers`.
3+
Require that all providers specify a `source` and `version` constraint through `required_providers`. This rule can also enforce specific provider constraints and restrict which providers can be used.
44

55
> This rule is enabled by "recommended" preset.
66
@@ -13,9 +13,39 @@ rule "terraform_required_providers" {
1313
# defaults
1414
source = true
1515
version = true
16+
17+
# Optional: Set to true to only allow listed providers
18+
provider_whitelist = false
19+
20+
# Optional: Define constraints for specific providers
21+
providers = {
22+
aws = {
23+
source = "hashicorp/aws" # Require official AWS provider
24+
version = "~> 5.0" # Require version 5.x
25+
}
26+
azurerm = {
27+
source = "hashicorp/azurerm" # Require official Azure provider
28+
version = "~> 3.0" # Require version 3.x
29+
}
30+
}
1631
}
1732
```
1833

34+
## Configuration Options
35+
36+
### Basic Options
37+
38+
- **`source`** (boolean, default: true): Require all providers to specify a source attribute
39+
- **`version`** (boolean, default: true): Require all providers to specify a version constraint
40+
41+
### Advanced Options
42+
43+
- **`provider_whitelist`** (boolean, default: false): When set to `true`, modules can only use providers that are explicitly defined in your `providers` configuration. This creates an "allowlist" of approved providers.
44+
45+
- **`providers`** (map, default: {}): Defines specific constraints for each provider. For each provider, you can specify:
46+
- **`source`** (optional): The required source address (e.g., `"hashicorp/aws"`)
47+
- **`version`** (optional): The required version constraint pattern (e.g., `"~> 5.0"`)
48+
1949
## Examples
2050

2151
```hcl
@@ -139,3 +169,98 @@ terraform {
139169
Provider version constraints can be specified using a [version argument within a provider block](https://developer.hashicorp.com/terraform/language/providers/configuration#provider-versions) for backwards compatibility. This approach is now discouraged, particularly for child modules.
140170

141171
Optionally, you can disable enforcement of either `source` or `version` by setting the corresponding attribute in the rule configuration to `false`.
172+
173+
### Provider Constraints Examples
174+
175+
#### Enforcing Version Constraints
176+
177+
**Rule Configuration:**
178+
```hcl
179+
rule "terraform_required_providers" {
180+
enabled = true
181+
providers = {
182+
aws = {
183+
version = "~> 5.0" # All modules must use this exact pattern
184+
}
185+
}
186+
}
187+
```
188+
189+
**Problem Code:**
190+
```hcl
191+
terraform {
192+
required_providers {
193+
aws = {
194+
source = "hashicorp/aws"
195+
version = ">= 5.0, < 6.0" # Different pattern, but similar effect
196+
}
197+
}
198+
}
199+
```
200+
201+
**Error Message:**
202+
```
203+
Error: Provider "aws" version constraint does not match expected (expected: "~> 5.0", found: ">= 5.0, < 6.0")
204+
```
205+
206+
The rule requires the exact pattern `"~> 5.0"`, not just any constraint that accepts version 5.x.
207+
208+
#### Enforcing Provider Sources
209+
210+
**Rule Configuration:**
211+
```hcl
212+
rule "terraform_required_providers" {
213+
enabled = true
214+
providers = {
215+
aws = {
216+
source = "hashicorp/aws" # Must use official HashiCorp source
217+
}
218+
}
219+
}
220+
```
221+
222+
**Problem Code:**
223+
```hcl
224+
terraform {
225+
required_providers {
226+
aws = {
227+
source = "custom/aws" # Using a potentially unsafe custom source
228+
version = "~> 5.0"
229+
}
230+
}
231+
}
232+
```
233+
234+
**Error Message:**
235+
```
236+
Error: Provider "aws" has incorrect source (expected: "hashicorp/aws", found: "custom/aws")
237+
```
238+
239+
#### Restricting to Approved Providers
240+
241+
**Rule Configuration:**
242+
```hcl
243+
rule "terraform_required_providers" {
244+
enabled = true
245+
provider_whitelist = true # Only allow listed providers
246+
247+
providers = {
248+
aws = {
249+
source = "hashicorp/aws"
250+
version = "~> 5.0"
251+
}
252+
}
253+
}
254+
```
255+
256+
**Terraform Code:**
257+
```hcl
258+
resource "random_string" "example" { # Using unapproved provider
259+
length = 16
260+
}
261+
```
262+
263+
**Result:**
264+
```
265+
Error: Provider "random" is not in the allowed provider list
266+
```

0 commit comments

Comments
 (0)