mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
Adds a Terraform module registry package type implementing the [module registry protocol](https://developer.hashicorp.com/terraform/internals/module-registry-protocol), so `terraform init` and Terragrunt `tfr://` can resolve modules from Gitea: ```hcl module "vpc" { source = "gitea.example.com/owner/vpc/aws" version = "~> 1.0" } ``` - Modules are uploaded with `PUT /api/packages/-/terraform/modules/{owner}/{name}/{provider}/{version}` as a `.tar.gz` with the module at its root, stored verbatim and immutable. - `X-Terraform-Get` is a short-lived signed URL because Terraform sends no credentials when downloading the archive. - The package page shows install snippets, README, the root module's inputs, outputs and providers, and `modules/*` submodules. - Metadata is read by a small in-tree HCL scanner instead of `hashicorp/hcl/v2`, which would add 5 modules and ~1.4 MB to the binary. Docs: https://gitea.com/gitea/docs/pulls/433 --------- Signed-off-by: Michael Lopez <mickael.lopez@gmail.com> Co-authored-by: TheFox0x7 <thefox0x7@gmail.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: silverwind <me@silverwind.io>
198 lines
5.7 KiB
Go
198 lines
5.7 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package terraform_module
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
|
|
packages_model "gitea.dev/models/packages"
|
|
"gitea.dev/modules/base"
|
|
packages_module "gitea.dev/modules/packages"
|
|
tfmod "gitea.dev/modules/packages/terraform_module"
|
|
"gitea.dev/modules/setting"
|
|
"gitea.dev/modules/util"
|
|
"gitea.dev/routers/api/packages/helper"
|
|
"gitea.dev/services/context"
|
|
packages_service "gitea.dev/services/packages"
|
|
)
|
|
|
|
const (
|
|
archiveFilename = "module.tar.gz"
|
|
archiveCodeMinutes = 10
|
|
)
|
|
|
|
func apiError(ctx *context.Context, status int, obj any) {
|
|
message := helper.ProcessErrorForUser(ctx, status, obj)
|
|
ctx.PlainText(status, message)
|
|
}
|
|
|
|
func packageName(ctx *context.Context) (string, error) {
|
|
name, provider := ctx.PathParam("name"), ctx.PathParam("provider")
|
|
if err := tfmod.ValidateAddress(ctx.Package.Owner.Name, name, provider); err != nil {
|
|
return "", err
|
|
}
|
|
return name + "/" + provider, nil
|
|
}
|
|
|
|
func packageInfo(ctx *context.Context) (*packages_service.PackageInfo, error) {
|
|
name, err := packageName(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
version, err := tfmod.NormalizeVersion(ctx.PathParam("version"))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &packages_service.PackageInfo{
|
|
Owner: ctx.Package.Owner,
|
|
PackageType: packages_model.TypeTerraformModule,
|
|
Name: name,
|
|
Version: version,
|
|
}, nil
|
|
}
|
|
|
|
func archiveCodeData(pi *packages_service.PackageInfo) string {
|
|
return fmt.Sprintf("terraform-module:%d:%s:%s", pi.Owner.ID, pi.Name, pi.Version)
|
|
}
|
|
|
|
func HasValidArchiveSignature(ctx *context.Context) bool {
|
|
pi, err := packageInfo(ctx)
|
|
return err == nil && base.VerifyTimeLimitCode(time.Now(), archiveCodeData(pi), archiveCodeMinutes, ctx.FormString("code"))
|
|
}
|
|
|
|
func ServiceDiscovery(ctx *context.Context) {
|
|
ctx.JSON(http.StatusOK, map[string]string{"modules.v1": setting.AppSubURL + "/api/packages/-/terraform/modules/"})
|
|
}
|
|
|
|
func ListVersions(ctx *context.Context) {
|
|
name, err := packageName(ctx)
|
|
if err != nil {
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
pvs, err := packages_model.GetVersionsByPackageName(ctx, ctx.Package.Owner.ID, packages_model.TypeTerraformModule, name)
|
|
if err != nil {
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
if len(pvs) == 0 {
|
|
apiError(ctx, http.StatusNotFound, packages_model.ErrPackageNotExist)
|
|
return
|
|
}
|
|
versions := make([]map[string]string, 0, len(pvs))
|
|
for _, pv := range pvs {
|
|
versions = append(versions, map[string]string{"version": pv.Version})
|
|
}
|
|
ctx.JSON(http.StatusOK, map[string]any{"modules": []any{map[string]any{"versions": versions}}})
|
|
}
|
|
|
|
func DownloadRedirect(ctx *context.Context) {
|
|
pi, err := packageInfo(ctx)
|
|
if err != nil {
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
if _, err := packages_model.GetVersionByNameAndVersion(ctx, pi.Owner.ID, pi.PackageType, pi.Name, pi.Version); err != nil {
|
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
|
return
|
|
}
|
|
// signed because Terraform sends no credentials when fetching it, "archive" tells go-getter the format of the extensionless URL
|
|
ctx.Resp.Header().Set("X-Terraform-Get", "./archive?archive=tar.gz&code="+base.CreateTimeLimitCode(archiveCodeData(pi), archiveCodeMinutes, time.Now(), nil))
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func DownloadArchive(ctx *context.Context) {
|
|
pi, err := packageInfo(ctx)
|
|
if err != nil {
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
s, u, pf, err := packages_service.OpenFileForDownloadByPackageNameAndVersion(ctx, pi, &packages_service.PackageFileInfo{Filename: archiveFilename}, ctx.Req.Method)
|
|
if err != nil {
|
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
|
return
|
|
}
|
|
helper.ServePackageFile(ctx, s, u, pf)
|
|
}
|
|
|
|
func UploadModule(ctx *context.Context) {
|
|
pi, err := packageInfo(ctx)
|
|
if err != nil {
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
|
|
upload, needToClose, err := ctx.UploadStream()
|
|
if err != nil {
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
if needToClose {
|
|
defer upload.Close()
|
|
}
|
|
|
|
buf, err := packages_module.CreateHashedBufferFromReader(upload)
|
|
if err != nil {
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
defer buf.Close()
|
|
|
|
metadata, err := tfmod.ParseModuleArchive(buf)
|
|
if err != nil {
|
|
apiError(ctx, util.Iif(errors.Is(err, packages_module.ErrContentTooLarge), http.StatusRequestEntityTooLarge, http.StatusBadRequest), err)
|
|
return
|
|
}
|
|
if _, err := buf.Seek(0, io.SeekStart); err != nil {
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
return
|
|
}
|
|
|
|
_, _, err = packages_service.CreatePackageAndAddFile(
|
|
ctx,
|
|
&packages_service.PackageCreationInfo{
|
|
PackageInfo: *pi,
|
|
SemverCompatible: true,
|
|
Creator: ctx.Doer,
|
|
Metadata: metadata,
|
|
},
|
|
&packages_service.PackageFileCreationInfo{
|
|
PackageFileInfo: packages_service.PackageFileInfo{Filename: archiveFilename},
|
|
Creator: ctx.Doer,
|
|
Data: buf,
|
|
IsLead: true,
|
|
},
|
|
)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, packages_model.ErrDuplicatePackageVersion):
|
|
apiError(ctx, http.StatusConflict, err)
|
|
case errors.Is(err, packages_service.ErrQuotaTotalCount), errors.Is(err, packages_service.ErrQuotaTypeSize), errors.Is(err, packages_service.ErrQuotaTotalSize):
|
|
apiError(ctx, http.StatusForbidden, err)
|
|
default:
|
|
apiError(ctx, http.StatusInternalServerError, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
ctx.Status(http.StatusCreated)
|
|
}
|
|
|
|
func DeleteModule(ctx *context.Context) {
|
|
pi, err := packageInfo(ctx)
|
|
if err != nil {
|
|
apiError(ctx, http.StatusBadRequest, err)
|
|
return
|
|
}
|
|
if err := packages_service.RemovePackageVersionByNameAndVersion(ctx, ctx.Doer, pi); err != nil {
|
|
apiError(ctx, helper.PackageErrorStatus(err), err)
|
|
return
|
|
}
|
|
ctx.Status(http.StatusNoContent)
|
|
}
|