|
| 1 | +# OCI Protocol Support for Terraform Modules and Providers |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +This implementation adds support for the OCI (Open Container Initiative) protocol for Terraform modules and providers. Terraform supports pulling modules and providers from OCI-compatible registries using the `oci://` protocol prefix. |
| 6 | + |
| 7 | +## Changes Made |
| 8 | + |
| 9 | +### 1. Module Extraction (`lib/modules/manager/terraform/extractors/others/modules.ts`) |
| 10 | + |
| 11 | +**Added:** |
| 12 | + |
| 13 | +- `ociRefMatchRegex`: New regex pattern to detect OCI protocol sources |
| 14 | + - Pattern: `/^oci:\/\/(?<registry>[^/:]+)\/(?<repository>[^:]+?)(?::(?<tag>.+))?$/` |
| 15 | + - Captures the registry hostname, repository path, and optional tag/version |
| 16 | + - Supports both formats: |
| 17 | + - `oci://registry/repo` (version in separate field) |
| 18 | + - `oci://registry/repo:tag` (version in URL) |
| 19 | + |
| 20 | +**Modified:** |
| 21 | + |
| 22 | +- Imported `DockerDatasource` for handling OCI registry requests |
| 23 | +- Updated `analyseTerraformModule()` method to detect and handle OCI sources |
| 24 | + - OCI sources are now configured to use the Docker datasource |
| 25 | + - Registry URLs are extracted and set appropriately |
| 26 | + - Package names follow the format: `registry/repository` |
| 27 | + - **Version handling**: If a tag is present in the source URL, it takes precedence over the separate `version` field |
| 28 | + |
| 29 | +**Example Detection:** |
| 30 | + |
| 31 | +```hcl |
| 32 | +# Version in separate field |
| 33 | +module "vpc" { |
| 34 | + source = "oci://registry.example.com/terraform-modules/vpc" |
| 35 | + version = "1.2.3" |
| 36 | +} |
| 37 | +
|
| 38 | +# Version embedded in source URL |
| 39 | +module "storage" { |
| 40 | + source = "oci://docker.io/terraform-modules/storage:3.1.0" |
| 41 | +} |
| 42 | +
|
| 43 | +# Digest in source URL |
| 44 | +module "database" { |
| 45 | + source = "oci://registry.example.com/modules/db:sha256:abc123" |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +### 2. Provider Extraction (`lib/modules/manager/terraform/base.ts`) |
| 50 | + |
| 51 | +**Added:** |
| 52 | + |
| 53 | +- `ociRefMatchRegex` property to `TerraformProviderExtractor` class |
| 54 | + |
| 55 | +**Modified:** |
| 56 | + |
| 57 | +- Imported `DockerDatasource` for handling OCI provider sources |
| 58 | +- Updated `analyzeTerraformProvider()` method to check for OCI protocol before standard parsing |
| 59 | + - OCI providers delegate to Docker datasource |
| 60 | + - Registry URLs extracted from the OCI source URL |
| 61 | + - Maintains compatibility with existing provider source formats |
| 62 | + |
| 63 | +**Example Detection:** |
| 64 | + |
| 65 | +```hcl |
| 66 | +terraform { |
| 67 | + required_providers { |
| 68 | + custom = { |
| 69 | + source = "oci://registry.example.com/providers/custom" |
| 70 | + version = "1.0.0" |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +### 3. Test Coverage |
| 77 | + |
| 78 | +**Test Fixture (`lib/modules/manager/terraform/__fixtures__/oci.tf`):** |
| 79 | + |
| 80 | +- Created comprehensive test file demonstrating OCI usage |
| 81 | +- Includes both module and provider examples |
| 82 | +- Uses various OCI registries (ghcr.io, docker.io, custom registries) |
| 83 | + |
| 84 | +**Unit Tests (`lib/modules/manager/terraform/extractors/others/modules.spec.ts`):** |
| 85 | + |
| 86 | +- Added `ociRefMatchRegex` test suite |
| 87 | +- Tests various OCI URL formats: |
| 88 | + - Simple registry: `oci://registry.example.com/namespace/module` |
| 89 | + - GitHub Container Registry: `oci://ghcr.io/org/repo/module` |
| 90 | + - Docker Hub: `oci://docker.io/user/module` |
| 91 | + |
| 92 | +**Integration Tests (`lib/modules/manager/terraform/extract.spec.ts`):** |
| 93 | + |
| 94 | +- Added comprehensive test case for OCI module and provider extraction |
| 95 | +- Validates correct datasource assignment (docker vs terraform-module/provider) |
| 96 | +- Verifies registry URL extraction |
| 97 | +- Tests mixed configuration (OCI + traditional sources) |
| 98 | + |
| 99 | +## How It Works |
| 100 | + |
| 101 | +### OCI Module Sources |
| 102 | + |
| 103 | +1. **Detection**: The module extractor checks if the source starts with `oci://` |
| 104 | +2. **Parsing**: Extracts registry hostname, repository path, and optional tag/version |
| 105 | +3. **Datasource Assignment**: Uses Docker datasource instead of Terraform Module datasource |
| 106 | +4. **Registry Configuration**: Sets `registryUrls` to the HTTPS version of the OCI registry |
| 107 | +5. **Version Handling**: |
| 108 | + - If tag is in the source URL (`oci://registry/repo:1.2.3`), it becomes `currentValue` |
| 109 | + - If tag is NOT in the URL, the separate `version` field is used |
| 110 | + - Supports both semver tags and digest references |
| 111 | + |
| 112 | +### OCI Provider Sources |
| 113 | + |
| 114 | +1. **Detection**: Provider extractor checks for `oci://` prefix before standard source parsing |
| 115 | +2. **Parsing**: Same pattern as modules - extracts registry, repository, and optional tag |
| 116 | +3. **Datasource Assignment**: Uses Docker datasource for version lookups |
| 117 | +4. **Version Handling**: Same as modules - tag in URL takes precedence over `version` field |
| 118 | + |
| 119 | +## Datasource Delegation |
| 120 | + |
| 121 | +When OCI protocol is detected: |
| 122 | + |
| 123 | +- **Datasource**: `docker` (instead of `terraform-module` or `terraform-provider`) |
| 124 | +- **Registry URLs**: Extracted from the OCI URL and converted to HTTPS |
| 125 | +- **Package Name**: Full path including registry hostname |
| 126 | +- **Version Resolution**: Handled by Docker datasource using OCI registry APIs |
| 127 | + |
| 128 | +## Benefits |
| 129 | + |
| 130 | +1. **Registry Flexibility**: Users can host Terraform modules/providers in any OCI-compatible registry |
| 131 | +2. **Unified Tooling**: Leverage existing container registry infrastructure |
| 132 | +3. **Access Control**: Use existing registry authentication and authorization |
| 133 | +4. **Reuse Docker Datasource**: No need to implement OCI-specific logic; Docker datasource handles it |
| 134 | + |
| 135 | +## Supported Registries |
| 136 | + |
| 137 | +Any OCI-compatible container registry, including: |
| 138 | + |
| 139 | +- GitHub Container Registry (ghcr.io) |
| 140 | +- Docker Hub (docker.io) |
| 141 | +- Google Container Registry (gcr.io) |
| 142 | +- Amazon Elastic Container Registry (ECR) |
| 143 | +- Azure Container Registry (azurecr.io) |
| 144 | +- Private/self-hosted registries |
| 145 | +- Harbor, Quay, Artifactory, etc. |
| 146 | + |
| 147 | +## Example Configurations |
| 148 | + |
| 149 | +### Module from GitHub Container Registry |
| 150 | + |
| 151 | +```hcl |
| 152 | +module "networking" { |
| 153 | + source = "oci://ghcr.io/myorg/terraform-modules/networking" |
| 154 | + version = "2.0.0" |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +### Provider from Private Registry |
| 159 | + |
| 160 | +```hcl |
| 161 | +terraform { |
| 162 | + required_providers { |
| 163 | + internal = { |
| 164 | + source = "oci://registry.company.com/providers/internal" |
| 165 | + version = "1.5.0" |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +### Mixed Configuration |
| 172 | + |
| 173 | +```hcl |
| 174 | +# OCI module |
| 175 | +module "vpc" { |
| 176 | + source = "oci://registry.example.com/modules/vpc" |
| 177 | + version = "1.0.0" |
| 178 | +} |
| 179 | +
|
| 180 | +# Traditional module |
| 181 | +module "consul" { |
| 182 | + source = "hashicorp/consul/aws" |
| 183 | + version = "0.1.0" |
| 184 | +} |
| 185 | +
|
| 186 | +terraform { |
| 187 | + required_providers { |
| 188 | + # OCI provider |
| 189 | + custom = { |
| 190 | + source = "oci://ghcr.io/company/providers/custom" |
| 191 | + version = "2.0.0" |
| 192 | + } |
| 193 | +
|
| 194 | + # Traditional provider |
| 195 | + aws = { |
| 196 | + source = "hashicorp/aws" |
| 197 | + version = "5.0.0" |
| 198 | + } |
| 199 | + } |
| 200 | +} |
| 201 | +``` |
| 202 | + |
| 203 | +## Implementation Notes |
| 204 | + |
| 205 | +### Regex Pattern Design |
| 206 | + |
| 207 | +The OCI regex pattern is intentionally simple: |
| 208 | + |
| 209 | +- Matches `oci://` prefix exactly |
| 210 | +- Captures registry as the first path segment (hostname) |
| 211 | +- Captures everything after as repository path |
| 212 | +- Doesn't validate specific URL components (delegated to Docker datasource) |
| 213 | + |
| 214 | +### Datasource Choice |
| 215 | + |
| 216 | +Using Docker datasource for OCI sources: |
| 217 | + |
| 218 | +- OCI is the standard for container registries |
| 219 | +- Docker datasource already implements OCI registry protocol |
| 220 | +- Reduces code duplication |
| 221 | +- Terraform OCI modules/providers use the same registry APIs as container images |
| 222 | + |
| 223 | +### Compatibility |
| 224 | + |
| 225 | +- Fully backward compatible with existing Terraform configurations |
| 226 | +- OCI support is additive - no changes to existing functionality |
| 227 | +- Falls back to standard parsing if OCI pattern doesn't match |
| 228 | + |
| 229 | +## Testing |
| 230 | + |
| 231 | +Run the tests with: |
| 232 | + |
| 233 | +```bash |
| 234 | +npm test -- terraform |
| 235 | +``` |
| 236 | + |
| 237 | +Specific test files: |
| 238 | + |
| 239 | +```bash |
| 240 | +npm test -- lib/modules/manager/terraform/extractors/others/modules.spec.ts |
| 241 | +npm test -- lib/modules/manager/terraform/extract.spec.ts |
| 242 | +``` |
| 243 | + |
| 244 | +## References |
| 245 | + |
| 246 | +- [Terraform Module Sources](https://developer.hashicorp.com/terraform/language/modules/sources) |
| 247 | +- [OCI Registry Specification](https://github.com/opencontainers/distribution-spec) |
| 248 | +- [Terraform Registry Protocol](https://developer.hashicorp.com/terraform/internals/module-registry-protocol) |
0 commit comments