Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Signed-off-by: Brendan Dalpe <bdalpe@gmail.com>
  • Loading branch information
bdalpe committed Jun 9, 2024
0 parents commit a34b0d5
Show file tree
Hide file tree
Showing 18 changed files with 2,625 additions and 0 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
41 changes: 41 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
on:
push:
tags:
- "*.*.*"
branches:
- main
permissions:
contents: read
packages: write
jobs:
build:
name: Build Docker Images
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: npm install
run: npm ci
- name: npm build
run: npm run build
- name: Generate Docker Image Metadata
uses: docker/metadata-action@v5
id: meta
if: ${{ !env.ACT }}
with:
images: ${{ steps.login-ecr.outputs.registry }}/tf-provider-mirror
tags: |
type=ref,event=tag
type=raw,value=latest
flavor: |
latest=false
- name: Build Docker Container Image
uses: docker/build-push-action@v5
with:
push: ${{ !env.ACT }}
tags: ${{ steps.meta.outputs.tags }}
platforms: linux/amd64,linux/arm64
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
cache/
dist/
.DS_Store
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.6
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM node:bookworm-slim as builder

# Download tini to correctly handle signals for Node.js
ARG TINI_VERSION=v0.19.0
ARG TARGETARCH
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini-static-${TARGETARCH} /tini

# Copy and build app
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

# Final image
FROM gcr.io/distroless/nodejs20-debian12
COPY --from=builder --chmod=544 /tini /bin/tini
WORKDIR /app
COPY --from=builder /app/dist/index.js /app/index.js
ENTRYPOINT ["/bin/tini", "--", "/nodejs/bin/node"]
CMD ["/app/index.js"]
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 Brendan Dalpe

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# TF Registry Mirror

This project is an implementation of the [Terraform/OpenTofu Provider Network Mirror Protocol](https://developer.hashicorp.com/terraform/internals/provider-network-mirror-protocol) which provides online caching of Providers and Modules. Instead of running `terraform providers mirror`, you can connect to the proxy server which will serve the correct responses back to the `terraform`/`tofu` client.

The [src](src) folder contains code that converts JSON data from the online registry into the expected Mirror server JSON responses. Provider file caching is handled by a separate NGINX instance.

## Configuring

For configuring Provider installation using the proxy mirror, follow the [instructions to configure](https://developer.hashicorp.com/terraform/cli/config/config-file#network_mirror) your code:

```terraform
provider_installation {
network_mirror {
url = "https://mirror.example.com/v1/providers/"
}
}
```

The mirror also handles requests for module information, but does not cache the upstream data files due to how the protocol works.

```terraform
module "example" {
source = "mirror.example.com/path/to/module"
...
}
```
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
nginx:
image: openresty/openresty:1.25.3.1-2-alpine-apk
ports:
- "8888:8888"
volumes:
- ./cache:/var/cache/nginx
- ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf:ro
nodejs:
build: .
ports:
- "8081:8081"
35 changes: 35 additions & 0 deletions nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:10m max_size=1g inactive=60d use_temp_path=off;

resolver 127.0.0.11 valid=60s ipv6=off;
resolver_timeout 5s;

server {
listen 8888;
listen [::]:8888;

proxy_cache_valid 200 206 301 60d;
proxy_cache cache;
proxy_cache_key $http_host$uri;

# Some extra settings to maximize cache hits and efficiency
proxy_cache_lock on;
proxy_cache_lock_age 10s;
proxy_cache_lock_timeout 3s;
proxy_cache_revalidate on;

proxy_ssl_verify on;
proxy_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt;
proxy_ssl_verify_depth 2;

location = /healthz {
return 204 "";
access_log off;
}

location / {
proxy_pass https://$http_host$uri$is_args$args;
proxy_set_header Host $host;
proxy_ssl_server_name on;
add_header X-Cache-Status $upstream_cache_status always;
}
}
Loading

0 comments on commit a34b0d5

Please sign in to comment.