Skip to content

Initial implementation #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 51 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
2720725
init
stigok Apr 21, 2022
0ee830c
list modules
stigok Apr 21, 2022
2748e38
working
stigok Apr 21, 2022
8b5a409
intermediate
stigok Apr 22, 2022
5dfdc69
working test
stigok Apr 22, 2022
50b43c9
extract router setup to own func
stigok Apr 22, 2022
cbe7102
refactor
stigok Apr 23, 2022
8c7f9ed
working nicely
stigok Apr 23, 2022
8db7de4
make tests independent of router implementation
stigok Apr 23, 2022
03dc15c
replace gorilla with chi
stigok Apr 23, 2022
e339890
refactor
stigok Apr 23, 2022
b383fe8
allow disabling auth
stigok Apr 23, 2022
30ecd2e
improve test for service discovery
stigok Apr 23, 2022
8e76634
use type safe json response for servicediscovery
stigok Apr 23, 2022
8108100
replace regex patterns with chi url params
stigok Apr 23, 2022
c6e9660
move index to own func
stigok Apr 23, 2022
532e505
refactor
stigok Apr 23, 2022
8e0ce31
rename SetupRoutes
stigok Apr 25, 2022
a42b4ce
add envconfig
stigok Apr 25, 2022
19b9ee2
add LoadAuthTokens
stigok Apr 25, 2022
217ac1c
refactor github
stigok Apr 25, 2022
12765e0
envconfig cleanup and logging improvements
stigok Apr 25, 2022
cba59af
clean and improve github integration
stigok Apr 25, 2022
d6fb6ca
struct tags and envconfig
stigok Apr 26, 2022
9844db8
improve error msg when authfile not specified
stigok Apr 26, 2022
699d212
major cleanup and refactor
stigok Apr 26, 2022
c6533dd
add url to welcome message
stigok Apr 26, 2022
2a99d1c
go mod tidy
stigok Apr 26, 2022
6eddd86
go get -u
stigok Apr 26, 2022
86f860a
add test for memory store
stigok Apr 26, 2022
de58fbb
add skipped github test
stigok Apr 26, 2022
57f486b
support toggling TLS setting
stigok Apr 26, 2022
ef44d51
move terraform example to examples folder
stigok Apr 26, 2022
584b96b
restructure
stigok Apr 26, 2022
491aa8a
move module version and store interface to core package
stigok Apr 26, 2022
229bfb0
add readme to tf example
stigok Apr 26, 2022
65a7615
update doc
stigok Apr 26, 2022
52dfe37
remove GitRef from moduleversion struct
stigok Apr 26, 2022
0650799
add docstrings
stigok Apr 26, 2022
ca2a4dc
add dockerfile
stigok Apr 26, 2022
963c7f7
improve docs and env var naming
stigok Apr 26, 2022
ba304b7
chore: add reuse tool for adding file license headers
stigok Apr 27, 2022
6ee81dd
docs: add references
stigok Apr 27, 2022
3a22966
feat: revamped github store
stigok Apr 27, 2022
9e623f0
fix: fix invalid gitignore and add missing files
stigok Apr 27, 2022
1b4a184
chore: add envfile for dev environment
stigok Apr 27, 2022
14417e5
docs: fix typo
stigok Apr 28, 2022
9db57ce
fix: run container as nonroot user
stigok Apr 28, 2022
dc857ce
feat: start container with dumb-init
stigok Apr 28, 2022
1f7fffd
chore(make): add dynamic year to license target
stigok Apr 28, 2022
7ec7541
chore(reuse): skip unrecognised files
stigok Apr 28, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-FileCopyrightText: 2022 NRK
#
# SPDX-License-Identifier: GPL-3.0-only

Dockerfile
terraform-registry
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
terraform-registry
/tmp
.terraform*
10 changes: 10 additions & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: terraform-registry
Upstream-Contact: Stig Otnes Kolstad <[email protected]>
Source: https://github.com/nrkno/terraform-registry

# Sample paragraph, commented out:
#
# Files: src/*
# Copyright: $YEAR $NAME <$CONTACT>
# License: ...
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# SPDX-FileCopyrightText: 2022 NRK
#
# SPDX-License-Identifier: GPL-3.0-only

FROM golang:1.17-bullseye as build

WORKDIR /go/src/app
ADD . /go/src/app

RUN go get -d -v ./...
RUN go build -o /go/bin/app ./cmd/terraform-registry/

FROM gcr.io/distroless/base-debian11
COPY --from=build /go/bin/app /
#USER 1000
CMD ["/app"]
674 changes: 0 additions & 674 deletions LICENSE

This file was deleted.

232 changes: 232 additions & 0 deletions LICENSES/GPL-3.0-only.txt

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: 2022 NRK
#
# SPDX-License-Identifier: GPL-3.0-only

CMD_SOURCE=./cmd/terraform-registry
BINARY_NAME=./terraform-registry

build:
go build -o ${BINARY_NAME} ${CMD_SOURCE}

test:
go test ./...

run:
go run ${CMD_SOURCE}

reuse:
find . -type f \
| grep -vP '^(./.git|./.reuse|./LICENSES/|./terraform-registry)' \
| grep -vP '(\.tf)$$' \
| xargs reuse addheader --license GPL-3.0-only --copyright NRK --year 2022
find . -type f -name '*.tf' \
| xargs reuse addheader --license GPL-3.0-only --copyright NRK --year 2022 --style python

clean:
go clean
rm ${BINARY_NAME}
198 changes: 198 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<!--
SPDX-FileCopyrightText: 2022 NRK

SPDX-License-Identifier: GPL-3.0-only
-->

# Terraform Registry

An implementation of a private Terraform registry.

**NOTE:** the APIs of this app is not currently considered stable and may
change without notice before hitting v1.0.

**NOTE:** please question and report issues you might have with this implementation.
There is surely a lot of room for improvement.

## Features

Supported Terraform protocols:
- [ ] login.v1
- [x] modules.v1
- [ ] providers.v1

Supported backends:
- `MemoryStore`: in-memory store that can be populated manually
- Must be populated manually
- `GitHubStore`: queries the GitHub API for modules, version tags and SSH download URLs
- A query for module `namespace/name/provider` will return repository `namespace/name`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Om jeg ikke husker feil så snakket vi om å implementere en "generic" path som default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#5

- `provider` part of module URLs is not implemented. Can be set to anything at all.
- Works for a single specified org/user
- No verification for the repo actually being a Terraform repo

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hvilke typ av repos vil GithubStore regne som TF moduler? (topicFilter)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#6

## Running

Environment variables:
- `LISTEN_ADDR` HTTP(S) listen address (default: `:8080`)
- `AUTH_DISABLED` disable authentication (default: `false`)
- `AUTH_TOKEN_FILE` filename with newline-separated strings of valid tokens
- `GITHUB_TOKEN` auth token for the GitHub API
- `GITHUB_ORG_NAME` name of org or user to search for repositories in
- `TLS_ENABLED` enable TLS (default: `false`)
- `TLS_CERT_FILE` path to a TLS certificate
- `TLS_KEY_FILE` path to a TLS key file

Build and run

```
$ make build
$ ./terraform-registry
```

## Development

Terraform does not allow disabling TLS certificate verification when accessing
a registry. Unless you have a valid certificate (signed by a valid CA) for your
hostname you will have to patch and build Terraform from source to disable the
TLS certificate validation.

### Build Terraform

Clone the official repository for the version you are using locally

```
$ terraform version
Terraform v1.1.9
on linux_amd6
$ git clone https://github.com/hashicorp/terraform --ref=v1.1.9 --depth=1
```

Apply the patch


```diff
diff --git a/internal/httpclient/client.go b/internal/httpclient/client.go
index bb06beb..5f9e424 100644
--- a/internal/httpclient/client.go
+++ b/internal/httpclient/client.go
@@ -1,6 +1,7 @@
package httpclient

import (
+ "crypto/tls"
"net/http"

cleanhttp "github.com/hashicorp/go-cleanhttp"
@@ -10,9 +11,14 @@ import (
// package that will also send a Terraform User-Agent string.
func New() *http.Client {
cli := cleanhttp.DefaultPooledClient()
+ transport := cli.Transport.(*http.Transport)
+ transport.TLSClientConfig = &tls.Config{
+ InsecureSkipVerify: true,
+ }
+
cli.Transport = &userAgentRoundTripper{
userAgent: UserAgentString(),
- inner: cli.Transport,
+ inner: transport,
}
return cli
}
```

Then build Terraform

```
$ go build .
```

### Create a self-signed TLS certificate

Create an OpenSSL configuration file to simplify creation of a self-signed cert.

```
$ tee > openssl.conf
[CA_default]
copy_extensions = copy

[req]
default_bits = 4096
prompt = no
default_md = sha256
distinguished_name = req_distinguished_name
x509_extensions = v3_ca

[req_distinguished_name]
C = NO
ST = Oslo
L = Oslo
O = Internet Widgits Pty Ltd
OU = Example
emailAddress = [email protected]
CN = example.com

[v3_ca]
basicConstraints = CA:FALSE
keyUsage = digitalSignature, keyEncipherment
subjectAltName = @alternate_names

[alternate_names]
DNS.1 = localhost
DNS.2 = localhost.localdomain
```

Create a self-signed certificate and private key

```
$ openssl req -x509 -newkey rsa:4096 -sha256 -utf8 -days 365 -nodes -config openssl.cnf -keyout cert.key -out cert.crt
```

(source: https://stackoverflow.com/a/46100856/90674)

### Run registry locally

```
$ export LISTEN_ADDR=:8080 AUTH_DISABLED=true
$ export GIT_HUB_ORG_NAME=myorg GIT_HUB_TOKEN=mytoken
$ export TLS_ENABLED=true TLS_CERT_FILE=cert.crt TLS_KEY_FILE=cert.key
$ make run
```

Now use `localhost.localdomain` as the registry URL for your module sources
in Terraform

```terraform
module "foo" {
source = "localhost.localdomain:8080/myorg/my-terraform-module-repo/generic//my-module"
version = "~> 2.0"
}
```

### Testing

```
$ make test
```

### Adding license information

This adds or updates licensing information of all relevant files in the respository
using [reuse](https://git.fsfe.org/reuse/tool#install). It is available in some package
managers and in The Python Package Index as `reuse` (`pip install reuse`).

```
$ make reuse
```

## References
- <https://www.terraform.io/language/modules/sources>
- <https://www.terraform.io/internals/login-protocol>
- <https://www.terraform.io/internals/module-registry-protocol>
- <https://www.terraform.io/internals/provider-registry-protocol>

## License

This project and all its files are licensed under GNU GPL v3 unless stated
otherwise with a different license header. See [./LICENSES](./LICENSES) for
the full license text of all used licenses.
7 changes: 7 additions & 0 deletions _examples/terraform-project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!--
SPDX-FileCopyrightText: 2022 NRK

SPDX-License-Identifier: GPL-3.0-only
-->

An example terraform file that gets a module from a local private registry.
12 changes: 12 additions & 0 deletions _examples/terraform-project/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# SPDX-FileCopyrightText: 2022 NRK
#
# SPDX-License-Identifier: GPL-3.0-only

terraform {
required_version = ">= 1.0"
}

module "foo" {
source = "localhost.localdomain:8080/terraform-aws-modules/terraform-aws-vpc/generic"
version = "~> 3.13"
}
24 changes: 24 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2022 NRK
//
// SPDX-License-Identifier: GPL-3.0-only

module github.com/nrkno/terraform-registry

go 1.17

require (
github.com/go-chi/chi/v5 v5.0.7
github.com/google/go-github/v43 v43.0.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/matryer/is v1.4.0
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
)

require (
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-querystring v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
)
Loading