clean: remove trash, update .gitignore
This commit is contained in:
@@ -1,23 +0,0 @@
|
||||
# Build Stage
|
||||
FROM golang:1.24-alpine AS builder
|
||||
|
||||
WORKDIR /app
|
||||
# Only copy what's needed for the registry server
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY cmd/registry ./cmd/registry
|
||||
# Build statically
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -o /registry-server ./cmd/registry
|
||||
|
||||
# Run Stage
|
||||
FROM alpine:3.19
|
||||
WORKDIR /app
|
||||
COPY --from=builder /registry-server /app/registry-server
|
||||
# Registry server uses HTTPS for S3, so CA certs are good to have
|
||||
RUN apk add --no-cache ca-certificates
|
||||
|
||||
USER 1000:1000
|
||||
EXPOSE 8080
|
||||
|
||||
CMD ["/app/registry-server"]
|
||||
@@ -1,281 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
s3 "github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
var (
|
||||
s3Client *s3.Client
|
||||
bucketName = "terraform-registry" // Default
|
||||
hostname = os.Getenv("REGISTRY_HOSTNAME")
|
||||
)
|
||||
|
||||
// Terraform Registry Protocol Structs
|
||||
type Discovery struct {
|
||||
ProvidersV1 string `json:"providers.v1"`
|
||||
}
|
||||
|
||||
type VersionList struct {
|
||||
ID string `json:"id"`
|
||||
Versions []Version `json:"versions"`
|
||||
Warnings []string `json:"warnings"`
|
||||
}
|
||||
|
||||
type Version struct {
|
||||
Version string `json:"version"`
|
||||
Protocols []string `json:"protocols"`
|
||||
Platforms []Platform `json:"platforms"`
|
||||
}
|
||||
|
||||
type Platform struct {
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
}
|
||||
|
||||
type DownloadResponse struct {
|
||||
Protocols []string `json:"protocols"`
|
||||
OS string `json:"os"`
|
||||
Arch string `json:"arch"`
|
||||
Filename string `json:"filename"`
|
||||
DownloadURL string `json:"download_url"`
|
||||
ShasumsURL string `json:"shasums_url"`
|
||||
ShasumsSignatureURL string `json:"shasums_signature_url"`
|
||||
Shasum string `json:"shasum"`
|
||||
SigningKeys SigningKeys `json:"signing_keys"`
|
||||
}
|
||||
|
||||
type SigningKeys struct {
|
||||
GPGPublicKeys []GPGPublicKey `json:"gpg_public_keys"`
|
||||
}
|
||||
|
||||
type GPGPublicKey struct {
|
||||
KeyID string `json:"key_id"`
|
||||
ASCIIArmor string `json:"ascii_armor"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
// 1. Init S3 Connection
|
||||
if hostname == "" {
|
||||
hostname = "localhost:8080"
|
||||
}
|
||||
|
||||
endpoint := os.Getenv("S3_ENDPOINT")
|
||||
accessKeyID := os.Getenv("S3_ACCESS_KEY")
|
||||
secretAccessKey := os.Getenv("S3_SECRET_KEY")
|
||||
|
||||
if os.Getenv("S3_BUCKET") != "" {
|
||||
bucketName = os.Getenv("S3_BUCKET")
|
||||
}
|
||||
|
||||
var err error
|
||||
s3Client, err = s3.New(endpoint, &s3.Options{
|
||||
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
||||
Secure: true, // Force secure for cloud S3
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
// 2. HTTP Handlers
|
||||
http.HandleFunc("/.well-known/terraform.json", discoveryHandler)
|
||||
http.HandleFunc("/v1/providers/", router)
|
||||
http.HandleFunc("/v1/proxy", proxyHandler)
|
||||
http.HandleFunc("/docs/", docsHandler)
|
||||
http.Handle("/", http.HandlerFunc(rootHandler))
|
||||
|
||||
log.Printf("Starting Registry Service on :8080 (Bucket: %s, Endpoint: %s)\n", bucketName, endpoint)
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
|
||||
func rootHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/" {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprint(w, `
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head><title>Terra Registry</title></head>
|
||||
<body>
|
||||
<h1>Terra Registry & Documentation Server</h1>
|
||||
<p>Status: <span style="color: green">ONLINE</span></p>
|
||||
<hr>
|
||||
<p>Powered by Nubes Cloud S3 Storage</p>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
}
|
||||
|
||||
func proxyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
bucket := r.URL.Query().Get("bucket")
|
||||
key := r.URL.Query().Get("key")
|
||||
|
||||
if bucket == "" || key == "" {
|
||||
http.Error(w, "Missing bucket or key params", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
obj, err := s3Client.GetObject(context.Background(), bucket, key, s3.GetObjectOptions{})
|
||||
if err != nil {
|
||||
log.Printf("Error getting object %s/%s: %v", bucket, key, err)
|
||||
http.Error(w, "File not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
defer obj.Close()
|
||||
|
||||
stat, err := obj.Stat()
|
||||
if err != nil {
|
||||
log.Printf("Error stating object %s/%s: %v", bucket, key, err)
|
||||
http.Error(w, "File not found or not accessible", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Length", fmt.Sprintf("%d", stat.Size))
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Header().Set("Last-Modified", stat.LastModified.Format(http.TimeFormat))
|
||||
|
||||
if _, err := io.Copy(w, obj); err != nil {
|
||||
log.Printf("Error streaming object: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func discoveryHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(Discovery{ProvidersV1: "/v1/providers/"})
|
||||
}
|
||||
|
||||
func router(w http.ResponseWriter, r *http.Request) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/v1/providers/")
|
||||
parts := strings.Split(path, "/")
|
||||
|
||||
if len(parts) == 3 && parts[2] == "versions" {
|
||||
listVersions(w, r, parts[0], parts[1])
|
||||
return
|
||||
}
|
||||
|
||||
if len(parts) == 6 && parts[3] == "download" {
|
||||
downloadVersion(w, r, parts[0], parts[1], parts[2], parts[4], parts[5])
|
||||
return
|
||||
}
|
||||
|
||||
http.Error(w, "Not Found", http.StatusNotFound)
|
||||
}
|
||||
|
||||
func listVersions(w http.ResponseWriter, r *http.Request, namespace, pType string) {
|
||||
prefix := fmt.Sprintf("%s/%s/%s/", hostname, namespace, pType)
|
||||
ctx := context.Background()
|
||||
versions := []Version{}
|
||||
seenVersions := map[string]*Version{}
|
||||
|
||||
objectCh := s3Client.ListObjects(ctx, bucketName, s3.ListObjectsOptions{
|
||||
Prefix: prefix,
|
||||
Recursive: true,
|
||||
})
|
||||
|
||||
for object := range objectCh {
|
||||
if object.Err != nil {
|
||||
continue
|
||||
}
|
||||
parts := strings.Split(object.Key, "/")
|
||||
if len(parts) < 5 {
|
||||
continue
|
||||
}
|
||||
verStr := parts[3]
|
||||
fileName := parts[4]
|
||||
|
||||
if _, ok := seenVersions[verStr]; !ok {
|
||||
seenVersions[verStr] = &Version{
|
||||
Version: verStr,
|
||||
Protocols: []string{"5.0"},
|
||||
Platforms: []Platform{},
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(fileName, "_linux_amd64.zip") {
|
||||
seenVersions[verStr].Platforms = append(seenVersions[verStr].Platforms, Platform{OS: "linux", Arch: "amd64"})
|
||||
}
|
||||
if strings.Contains(fileName, "_windows_amd64.zip") {
|
||||
seenVersions[verStr].Platforms = append(seenVersions[verStr].Platforms, Platform{OS: "windows", Arch: "amd64"})
|
||||
}
|
||||
if strings.Contains(fileName, "_darwin_amd64.zip") {
|
||||
seenVersions[verStr].Platforms = append(seenVersions[verStr].Platforms, Platform{OS: "darwin", Arch: "amd64"})
|
||||
}
|
||||
if strings.Contains(fileName, "_darwin_arm64.zip") {
|
||||
seenVersions[verStr].Platforms = append(seenVersions[verStr].Platforms, Platform{OS: "darwin", Arch: "arm64"})
|
||||
}
|
||||
}
|
||||
|
||||
for _, v := range seenVersions {
|
||||
versions = append(versions, *v)
|
||||
}
|
||||
|
||||
resp := VersionList{
|
||||
ID: fmt.Sprintf("%s/%s", namespace, pType),
|
||||
Versions: versions,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
|
||||
func downloadVersion(w http.ResponseWriter, r *http.Request, namespace, pType, version, osType, arch string) {
|
||||
basePath := fmt.Sprintf("%s/%s/%s/%s", hostname, namespace, pType, version)
|
||||
filename := fmt.Sprintf("terraform-provider-%s_%s_%s_%s.zip", pType, version, osType, arch)
|
||||
fullKey := fmt.Sprintf("%s/%s", basePath, filename)
|
||||
shasumsKey := fmt.Sprintf("%s/terraform-provider-%s_%s_SHA256SUMS", basePath, pType, version)
|
||||
sigKey := fmt.Sprintf("%s/terraform-provider-%s_%s_SHA256SUMS.sig", basePath, pType, version)
|
||||
|
||||
var shasumValue string
|
||||
shasumsObj, err := s3Client.GetObject(context.Background(), bucketName, shasumsKey, s3.GetObjectOptions{})
|
||||
if err == nil {
|
||||
defer shasumsObj.Close()
|
||||
scanner := bufio.NewScanner(shasumsObj)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.Contains(line, filename) {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) >= 1 {
|
||||
shasumValue = fields[0]
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
baseURL := "https://" + hostname
|
||||
downloadLink := fmt.Sprintf("%s/v1/proxy?bucket=%s&key=%s", baseURL, bucketName, url.QueryEscape(fullKey))
|
||||
shasumsLink := fmt.Sprintf("%s/v1/proxy?bucket=%s&key=%s", baseURL, bucketName, url.QueryEscape(shasumsKey))
|
||||
sigLink := fmt.Sprintf("%s/v1/proxy?bucket=%s&key=%s", baseURL, bucketName, url.QueryEscape(sigKey))
|
||||
|
||||
gpgKey := gpgPublicKey
|
||||
|
||||
resp := DownloadResponse{
|
||||
Protocols: []string{"5.0"},
|
||||
OS: osType,
|
||||
Arch: arch,
|
||||
Filename: filename,
|
||||
DownloadURL: downloadLink,
|
||||
ShasumsURL: shasumsLink,
|
||||
ShasumsSignatureURL: sigLink,
|
||||
Shasum: shasumValue,
|
||||
SigningKeys: SigningKeys{
|
||||
GPGPublicKeys: []GPGPublicKey{gpgKey},
|
||||
},
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
}
|
||||
Reference in New Issue
Block a user