mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-19 15:14:40 +09:00
feat(packages): add private Terraform module registry (#38040)
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>
This commit is contained in:
22 files changed
+1092
-52
No files matched your search
@@ -2885,6 +2885,8 @@ LEVEL = Info
|
||||
;LIMIT_SIZE_VAGRANT = -1
|
||||
;; Maximum size of a Terraform state upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
|
||||
;LIMIT_SIZE_TERRAFORM_STATE = -1
|
||||
;; Maximum size of a Terraform module archive upload (`-1` means no limits, format `1000`, `1 MB`, `1 GiB`)
|
||||
;LIMIT_SIZE_TERRAFORM_MODULE = -1
|
||||
;; Enable RPM re-signing by default. (It will overwrite the old signature ,using v4 format, not compatible with CentOS 6 or older)
|
||||
;DEFAULT_RPM_SIGN_ENABLED = false
|
||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||
|
||||
@@ -33,6 +33,7 @@ import (
|
||||
"gitea.dev/modules/packages/rpm"
|
||||
"gitea.dev/modules/packages/rubygems"
|
||||
"gitea.dev/modules/packages/swift"
|
||||
"gitea.dev/modules/packages/terraform_module"
|
||||
"gitea.dev/modules/packages/vagrant"
|
||||
"gitea.dev/modules/util"
|
||||
|
||||
@@ -212,8 +213,10 @@ func GetPackageDescriptorWithCache(ctx context.Context, pv *PackageVersion, c *c
|
||||
metadata = &rubygems.Metadata{}
|
||||
case TypeSwift:
|
||||
metadata = &swift.Metadata{}
|
||||
case TypeTerraformModule:
|
||||
metadata = &terraform_module.Metadata{}
|
||||
case TypeTerraformState:
|
||||
// terraform packages have no metadata
|
||||
// terraform state packages have no metadata
|
||||
case TypeVagrant:
|
||||
metadata = &vagrant.Metadata{}
|
||||
default:
|
||||
|
||||
+29
-23
@@ -30,29 +30,30 @@ type Type string
|
||||
|
||||
// List of supported packages
|
||||
const (
|
||||
TypeAlpine Type = "alpine"
|
||||
TypeArch Type = "arch"
|
||||
TypeCargo Type = "cargo"
|
||||
TypeChef Type = "chef"
|
||||
TypeComposer Type = "composer"
|
||||
TypeConan Type = "conan"
|
||||
TypeConda Type = "conda"
|
||||
TypeContainer Type = "container"
|
||||
TypeCran Type = "cran"
|
||||
TypeDebian Type = "debian"
|
||||
TypeGeneric Type = "generic"
|
||||
TypeGo Type = "go"
|
||||
TypeHelm Type = "helm"
|
||||
TypeMaven Type = "maven"
|
||||
TypeNpm Type = "npm"
|
||||
TypeNuGet Type = "nuget"
|
||||
TypePub Type = "pub"
|
||||
TypePyPI Type = "pypi"
|
||||
TypeRpm Type = "rpm"
|
||||
TypeRubyGems Type = "rubygems"
|
||||
TypeSwift Type = "swift"
|
||||
TypeTerraformState Type = "terraform"
|
||||
TypeVagrant Type = "vagrant"
|
||||
TypeAlpine Type = "alpine"
|
||||
TypeArch Type = "arch"
|
||||
TypeCargo Type = "cargo"
|
||||
TypeChef Type = "chef"
|
||||
TypeComposer Type = "composer"
|
||||
TypeConan Type = "conan"
|
||||
TypeConda Type = "conda"
|
||||
TypeContainer Type = "container"
|
||||
TypeCran Type = "cran"
|
||||
TypeDebian Type = "debian"
|
||||
TypeGeneric Type = "generic"
|
||||
TypeGo Type = "go"
|
||||
TypeHelm Type = "helm"
|
||||
TypeMaven Type = "maven"
|
||||
TypeNpm Type = "npm"
|
||||
TypeNuGet Type = "nuget"
|
||||
TypePub Type = "pub"
|
||||
TypePyPI Type = "pypi"
|
||||
TypeRpm Type = "rpm"
|
||||
TypeRubyGems Type = "rubygems"
|
||||
TypeSwift Type = "swift"
|
||||
TypeTerraformModule Type = "terraform-module"
|
||||
TypeTerraformState Type = "terraform"
|
||||
TypeVagrant Type = "vagrant"
|
||||
)
|
||||
|
||||
var TypeList = []Type{
|
||||
@@ -77,6 +78,7 @@ var TypeList = []Type{
|
||||
TypeRpm,
|
||||
TypeRubyGems,
|
||||
TypeSwift,
|
||||
TypeTerraformModule,
|
||||
TypeTerraformState,
|
||||
TypeVagrant,
|
||||
}
|
||||
@@ -126,6 +128,8 @@ func (pt Type) Name() string {
|
||||
return "RubyGems"
|
||||
case TypeSwift:
|
||||
return "Swift"
|
||||
case TypeTerraformModule:
|
||||
return "Terraform Module"
|
||||
case TypeTerraformState:
|
||||
return "Terraform State"
|
||||
case TypeVagrant:
|
||||
@@ -179,6 +183,8 @@ func (pt Type) SVGName() string {
|
||||
return "gitea-rubygems"
|
||||
case TypeSwift:
|
||||
return "gitea-swift"
|
||||
case TypeTerraformModule:
|
||||
return "gitea-terraform"
|
||||
case TypeTerraformState:
|
||||
return "gitea-terraform"
|
||||
case TypeVagrant:
|
||||
|
||||
@@ -0,0 +1,235 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package terraform_module
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.dev/modules/container"
|
||||
)
|
||||
|
||||
var hclIndexedBlocks = container.SetOf("variable", "output", "terraform", "required_providers") // bodies of other blocks are skipped
|
||||
|
||||
type hclBlock struct {
|
||||
typ string
|
||||
labels []string
|
||||
attrs map[string]string // attribute name -> expression source
|
||||
blocks []*hclBlock
|
||||
}
|
||||
|
||||
// hclScanner reads HCL block structure without evaluating expressions, nesting is capped to bound recursion
|
||||
type hclScanner struct {
|
||||
src []byte
|
||||
pos int
|
||||
nesting int
|
||||
}
|
||||
|
||||
const hclMaxNesting = 32
|
||||
|
||||
func (s *hclScanner) peek(offset int) byte {
|
||||
if s.pos+offset < len(s.src) {
|
||||
return s.src[s.pos+offset]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (s *hclScanner) body(depth int) *hclBlock {
|
||||
body := &hclBlock{attrs: map[string]string{}}
|
||||
for {
|
||||
s.skipSpace(true)
|
||||
if s.pos >= len(s.src) {
|
||||
return body
|
||||
}
|
||||
if s.src[s.pos] == '}' {
|
||||
s.pos++
|
||||
return body
|
||||
}
|
||||
start := s.pos
|
||||
name := s.ident()
|
||||
s.skipSpace(false)
|
||||
if name != "" && s.peek(0) == '=' {
|
||||
s.pos++
|
||||
s.skipSpace(false)
|
||||
exprStart := s.pos
|
||||
body.attrs[name] = string(s.src[exprStart:s.skipExpr()])
|
||||
continue
|
||||
}
|
||||
var labels []string
|
||||
for name != "" && s.peek(0) != '{' {
|
||||
labelStart := s.pos
|
||||
if s.peek(0) == '"' {
|
||||
s.skipString()
|
||||
labels = append(labels, hclString(string(s.src[labelStart:s.pos])))
|
||||
} else if label := s.ident(); label != "" {
|
||||
labels = append(labels, label)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
s.skipSpace(false)
|
||||
}
|
||||
if name == "" || s.peek(0) != '{' || depth >= 2 || !hclIndexedBlocks.Contains(name) { // only terraform.required_providers needs two levels
|
||||
s.skipExpr()
|
||||
if s.pos == start {
|
||||
s.pos++
|
||||
}
|
||||
continue
|
||||
}
|
||||
s.pos++
|
||||
block := s.body(depth + 1)
|
||||
block.typ, block.labels = name, labels
|
||||
body.blocks = append(body.blocks, block)
|
||||
}
|
||||
}
|
||||
|
||||
// skipExpr advances to the newline, comma or unbalanced closing bracket that ends an expression and returns where its source ends
|
||||
func (s *hclScanner) skipExpr() (end int) {
|
||||
end = s.pos
|
||||
for depth := 0; s.pos < len(s.src); {
|
||||
switch c := s.src[s.pos]; {
|
||||
case c == '"':
|
||||
s.skipString()
|
||||
case c == '<' && s.skipHeredoc():
|
||||
case s.skipComment():
|
||||
continue
|
||||
case (c == '\n' || c == ',') && depth == 0:
|
||||
return end
|
||||
case c == '}' || c == ']' || c == ')':
|
||||
if depth == 0 {
|
||||
return end
|
||||
}
|
||||
depth--
|
||||
s.pos++
|
||||
case c == '{' || c == '[' || c == '(':
|
||||
depth++
|
||||
s.pos++
|
||||
case c == ' ' || c == '\t' || c == '\r' || c == '\n':
|
||||
s.pos++
|
||||
continue
|
||||
default:
|
||||
s.pos++
|
||||
}
|
||||
end = s.pos
|
||||
}
|
||||
return end
|
||||
}
|
||||
|
||||
func (s *hclScanner) skipString() {
|
||||
for s.pos++; s.pos < len(s.src); s.pos++ {
|
||||
switch c := s.src[s.pos]; {
|
||||
case c == '\\' && s.pos+1 < len(s.src) || (c == '$' || c == '%') && s.peek(1) == c:
|
||||
s.pos++
|
||||
case c == '"':
|
||||
s.pos++
|
||||
return
|
||||
case c == '\n':
|
||||
return
|
||||
case (c == '$' || c == '%') && s.peek(1) == '{':
|
||||
if s.nesting >= hclMaxNesting {
|
||||
return
|
||||
}
|
||||
s.nesting++
|
||||
s.pos += 2
|
||||
for s.skipExpr(); s.peek(0) == ',' || s.peek(0) == '\n'; s.skipExpr() { // commas and newlines don't end a template sequence
|
||||
s.pos++
|
||||
}
|
||||
s.nesting--
|
||||
if s.peek(0) != '}' {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *hclScanner) skipHeredoc() bool {
|
||||
start := s.pos
|
||||
if s.peek(1) != '<' {
|
||||
return false
|
||||
}
|
||||
s.pos += 2
|
||||
if s.peek(0) == '-' {
|
||||
s.pos++
|
||||
}
|
||||
delim := s.ident()
|
||||
if s.peek(0) == '\r' {
|
||||
s.pos++
|
||||
}
|
||||
if delim == "" || s.peek(0) != '\n' {
|
||||
s.pos = start
|
||||
return false
|
||||
}
|
||||
for s.pos < len(s.src) {
|
||||
lineStart := s.pos + 1
|
||||
s.pos = len(s.src)
|
||||
if lineEnd := bytes.IndexByte(s.src[lineStart:], '\n'); lineEnd >= 0 {
|
||||
s.pos = lineStart + lineEnd
|
||||
}
|
||||
if string(bytes.TrimSpace(s.src[lineStart:s.pos])) == delim {
|
||||
break
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *hclScanner) skipComment() bool {
|
||||
switch {
|
||||
case s.peek(0) == '#' || s.peek(0) == '/' && s.peek(1) == '/':
|
||||
for s.pos < len(s.src) && s.src[s.pos] != '\n' {
|
||||
s.pos++
|
||||
}
|
||||
case s.peek(0) == '/' && s.peek(1) == '*':
|
||||
if end := bytes.Index(s.src[s.pos+2:], []byte("*/")); end >= 0 {
|
||||
s.pos += end + 4
|
||||
} else {
|
||||
s.pos = len(s.src)
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *hclScanner) skipSpace(newlines bool) {
|
||||
for s.pos < len(s.src) {
|
||||
switch c := s.src[s.pos]; {
|
||||
case c == ' ' || c == '\t' || c == '\r' || newlines && (c == '\n' || c == ','):
|
||||
s.pos++
|
||||
case !s.skipComment():
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *hclScanner) ident() string {
|
||||
start := s.pos
|
||||
for ; s.pos < len(s.src); s.pos++ {
|
||||
c := s.src[s.pos]
|
||||
if !(c == '_' || 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || s.pos > start && (c == '-' || '0' <= c && c <= '9')) {
|
||||
break
|
||||
}
|
||||
}
|
||||
return string(s.src[start:s.pos])
|
||||
}
|
||||
|
||||
var (
|
||||
hclWithoutEscapedTemplates = strings.NewReplacer("$${", "", "%%{", "")
|
||||
hclUnescapeTemplates = strings.NewReplacer("$${", "${", "%%{", "%{")
|
||||
)
|
||||
|
||||
// hclString decodes a string literal or heredoc, returning "" for expressions and templates that need evaluation
|
||||
func hclString(expr string) string {
|
||||
if strings.HasPrefix(expr, "<<") {
|
||||
_, content, _ := strings.Cut(expr, "\n")
|
||||
return content[:strings.LastIndexByte(content, '\n')+1]
|
||||
}
|
||||
if unescaped := hclWithoutEscapedTemplates.Replace(expr); strings.Contains(unescaped, "${") || strings.Contains(unescaped, "%{") {
|
||||
return ""
|
||||
}
|
||||
decoded, err := strconv.Unquote(expr)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return hclUnescapeTemplates.Replace(decoded)
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package terraform_module
|
||||
|
||||
type Metadata struct {
|
||||
Readme string `json:"readme,omitempty"`
|
||||
Root *Module `json:"root,omitempty"`
|
||||
Submodules map[string]*Submodule `json:"submodules,omitempty"`
|
||||
}
|
||||
|
||||
// Submodule is a module packaged in the archive's modules/<name> directory
|
||||
type Submodule struct {
|
||||
Readme string `json:"readme,omitempty"`
|
||||
Module *Module `json:"module,omitempty"`
|
||||
}
|
||||
|
||||
type Module struct {
|
||||
Inputs []*Input `json:"inputs,omitempty"`
|
||||
Outputs []*Output `json:"outputs,omitempty"`
|
||||
Providers []*Provider `json:"providers,omitempty"`
|
||||
}
|
||||
|
||||
type Input struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Default string `json:"default,omitempty"`
|
||||
Required bool `json:"required"`
|
||||
}
|
||||
|
||||
type Output struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description,omitempty"`
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
Name string `json:"name"`
|
||||
Source string `json:"source,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package terraform_module
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"maps"
|
||||
"path"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
packages_module "gitea.dev/modules/packages"
|
||||
|
||||
"github.com/hashicorp/go-version"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidNamespace = errors.New("module namespace (owner name) is invalid")
|
||||
ErrInvalidName = errors.New("module name is invalid")
|
||||
ErrInvalidProvider = errors.New("module provider is invalid")
|
||||
ErrInvalidVersion = errors.New("module version is invalid")
|
||||
ErrNoRootModule = errors.New("no terraform module at the archive root: package the module directory's contents, not the directory itself (e.g. `tar -czf module.tar.gz -C path/to/module .`)")
|
||||
)
|
||||
|
||||
const maxReadmeSize = 1024 * 1024 // larger READMEs are not stored, they would be rendered on every page view
|
||||
|
||||
// same rules as Terraform's module source address parser (github.com/hashicorp/terraform-registry-address)
|
||||
var (
|
||||
nameRe = regexp.MustCompile(`\A[0-9A-Za-z](?:[0-9A-Za-z_-]{0,62}[0-9A-Za-z])?\z`)
|
||||
systemRe = regexp.MustCompile(`\A[0-9a-z]{1,64}\z`)
|
||||
)
|
||||
|
||||
func ValidateAddress(namespace, name, provider string) error {
|
||||
if !nameRe.MatchString(namespace) {
|
||||
return ErrInvalidNamespace
|
||||
}
|
||||
if !nameRe.MatchString(name) {
|
||||
return ErrInvalidName
|
||||
}
|
||||
if !systemRe.MatchString(provider) {
|
||||
return ErrInvalidProvider
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NormalizeVersion returns the canonical semver form, so `v1.0.0` and `1.0.0` are the same version
|
||||
func NormalizeVersion(raw string) (string, error) {
|
||||
parsed, err := version.NewSemver(raw)
|
||||
if err != nil {
|
||||
return "", ErrInvalidVersion
|
||||
}
|
||||
return parsed.String(), nil
|
||||
}
|
||||
|
||||
func ParseModuleArchive(r io.Reader) (*Metadata, error) {
|
||||
gz, err := gzip.NewReader(r)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid gzip stream: %w", err)
|
||||
}
|
||||
defer gz.Close()
|
||||
return parseTar(tar.NewReader(packages_module.NewLimitedReader(gz, packages_module.MaxMetadataSize)))
|
||||
}
|
||||
|
||||
func parseTar(tr *tar.Reader) (*Metadata, error) {
|
||||
retained := packages_module.NewLimitedReader(tr, packages_module.MaxMetadataSize) // sparse entries read more than they decompress
|
||||
metadata := &Metadata{}
|
||||
root := map[string][]byte{} // .tf.json content yields no HCL blocks
|
||||
submodules := map[string]map[string][]byte{}
|
||||
subReadmes := map[string]string{}
|
||||
for {
|
||||
hdr, err := tr.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid tar stream: %w", err)
|
||||
}
|
||||
dir, base := path.Split(path.Clean(hdr.Name))
|
||||
if hdr.Typeflag != tar.TypeReg || strings.HasPrefix(base, ".") { // Terraform ignores dotfiles
|
||||
continue
|
||||
}
|
||||
dir = strings.TrimSuffix(dir, "/")
|
||||
isSource := strings.HasSuffix(base, ".tf") || strings.HasSuffix(base, ".tf.json")
|
||||
isReadme := strings.EqualFold(base, "readme.md") || strings.EqualFold(base, "readme")
|
||||
name, isSubmodule := strings.CutPrefix(dir, "modules/")
|
||||
isSubmodule = isSubmodule && !strings.Contains(name, "/") // standard module structure, one level deep
|
||||
if !isSource && !isReadme || dir != "" && !isSubmodule || isReadme && hdr.Size > maxReadmeSize {
|
||||
continue
|
||||
}
|
||||
data, err := io.ReadAll(retained)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch {
|
||||
case isSubmodule && isSource:
|
||||
if submodules[name] == nil {
|
||||
submodules[name] = map[string][]byte{}
|
||||
}
|
||||
submodules[name][base] = data
|
||||
case isSubmodule:
|
||||
subReadmes[name] = string(data)
|
||||
case isReadme:
|
||||
metadata.Readme = string(data)
|
||||
default:
|
||||
root[base] = data
|
||||
}
|
||||
}
|
||||
if len(root) == 0 && len(submodules) == 0 {
|
||||
return nil, ErrNoRootModule
|
||||
}
|
||||
if len(root) > 0 {
|
||||
metadata.Root = parseModule(root)
|
||||
}
|
||||
if len(submodules) > 0 {
|
||||
metadata.Submodules = make(map[string]*Submodule, len(submodules))
|
||||
for name, files := range submodules {
|
||||
metadata.Submodules[name] = &Submodule{Readme: subReadmes[name], Module: parseModule(files)}
|
||||
}
|
||||
}
|
||||
return metadata, nil
|
||||
}
|
||||
|
||||
func parseModule(files map[string][]byte) *Module {
|
||||
module := &Module{}
|
||||
providers := map[string]*Provider{}
|
||||
for _, filename := range slices.Sorted(maps.Keys(files)) {
|
||||
for _, block := range (&hclScanner{src: bytes.TrimPrefix(files[filename], []byte("\xef\xbb\xbf"))}).body(0).blocks {
|
||||
attrs := block.attrs
|
||||
switch {
|
||||
case block.typ == "variable" && len(block.labels) == 1:
|
||||
_, hasDefault := attrs["default"]
|
||||
module.Inputs = append(module.Inputs, &Input{
|
||||
Name: block.labels[0],
|
||||
Type: attrs["type"],
|
||||
Description: hclString(attrs["description"]),
|
||||
Default: attrs["default"],
|
||||
Required: !hasDefault,
|
||||
})
|
||||
case block.typ == "output" && len(block.labels) == 1:
|
||||
module.Outputs = append(module.Outputs, &Output{Name: block.labels[0], Description: hclString(attrs["description"])})
|
||||
case block.typ == "terraform":
|
||||
for _, inner := range block.blocks {
|
||||
if inner.typ != "required_providers" {
|
||||
continue
|
||||
}
|
||||
for name, expr := range inner.attrs {
|
||||
if providers[name] != nil {
|
||||
continue
|
||||
}
|
||||
provider := &Provider{Name: name}
|
||||
if strings.HasPrefix(expr, "{") {
|
||||
fields := (&hclScanner{src: []byte(expr[1:])}).body(2).attrs
|
||||
provider.Source, provider.Version = hclString(fields["source"]), hclString(fields["version"])
|
||||
} else {
|
||||
provider.Version = hclString(expr) // legacy `name = "<version>"` form
|
||||
}
|
||||
providers[name] = provider
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
module.Providers = slices.SortedFunc(maps.Values(providers), func(a, b *Provider) int { return strings.Compare(a.Name, b.Name) })
|
||||
return module
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package terraform_module
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
packages_module "gitea.dev/modules/packages"
|
||||
"gitea.dev/modules/test"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func buildArchive(files map[string]string) *bytes.Buffer {
|
||||
return test.WriteTarCompression(gzip.NewWriter, files)
|
||||
}
|
||||
|
||||
func TestParseModuleArchive(t *testing.T) {
|
||||
metadata, err := ParseModuleArchive(buildArchive(map[string]string{
|
||||
"./main.tf": "\xef\xbb\xbf" + `terraform {
|
||||
required_providers {
|
||||
aws = {
|
||||
source = "hashicorp/aws"
|
||||
version = "~> 5.0"
|
||||
configuration_aliases = [aws.alternate]
|
||||
}
|
||||
random = "~> 3.0"
|
||||
}
|
||||
}
|
||||
# an unbalanced { in a comment
|
||||
locals {
|
||||
joined = "%{ for k, v in { a = "b" } }${k}%{ endfor }"
|
||||
}
|
||||
variable "region" {
|
||||
type = string
|
||||
description = "AWS region, not $${var.template} }"
|
||||
default = "eu-west-1" // trailing comment
|
||||
}
|
||||
variable "tags" {
|
||||
type = map(string)
|
||||
description = <<EOT
|
||||
Tags }
|
||||
EOT
|
||||
}
|
||||
output "vpc_id" {
|
||||
description = "VPC id"
|
||||
value = aws_vpc.this.id
|
||||
}
|
||||
`,
|
||||
"README.md": "# vpc\n",
|
||||
"._main.tf": `variable "ignored" {}`,
|
||||
"backup.TF": `variable "ignored" {}`,
|
||||
"examples/basic/main.tf": `variable "ignored" {}`,
|
||||
"modules/net/main.tf": "variable \"cidr\" {}\n",
|
||||
"modules/net/README.md": "# Net submodule\n",
|
||||
"modules/net/LICENSE": "must not become the readme",
|
||||
"modules/net/deep/main.tf": "",
|
||||
"modules/json/main.tf.json": "",
|
||||
}))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, &Metadata{
|
||||
Readme: "# vpc\n",
|
||||
Root: &Module{
|
||||
Inputs: []*Input{
|
||||
{Name: "region", Type: "string", Description: "AWS region, not ${var.template} }", Default: `"eu-west-1"`},
|
||||
{Name: "tags", Type: "map(string)", Description: "Tags }\n", Required: true},
|
||||
},
|
||||
Outputs: []*Output{{Name: "vpc_id", Description: "VPC id"}},
|
||||
Providers: []*Provider{
|
||||
{Name: "aws", Source: "hashicorp/aws", Version: "~> 5.0"},
|
||||
{Name: "random", Version: "~> 3.0"},
|
||||
},
|
||||
},
|
||||
Submodules: map[string]*Submodule{
|
||||
"json": {Module: &Module{}},
|
||||
"net": {Readme: "# Net submodule\n", Module: &Module{Inputs: []*Input{{Name: "cidr", Required: true}}}},
|
||||
},
|
||||
}, metadata)
|
||||
|
||||
metadata, err = ParseModuleArchive(buildArchive(map[string]string{"modules/net/main.tf": ""}))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, &Metadata{Submodules: map[string]*Submodule{"net": {Module: &Module{}}}}, metadata)
|
||||
|
||||
metadata, err = ParseModuleArchive(buildArchive(map[string]string{
|
||||
"main.tf": "",
|
||||
"README.md": strings.Repeat("#", maxReadmeSize+1),
|
||||
"modules/net/main.tf": "",
|
||||
"modules/net/README.md": strings.Repeat("#", maxReadmeSize+1),
|
||||
}))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, &Metadata{Root: &Module{}, Submodules: map[string]*Submodule{"net": {Module: &Module{}}}}, metadata)
|
||||
|
||||
_, err = ParseModuleArchive(buildArchive(map[string]string{"mod-1.0.0/main.tf": ""}))
|
||||
assert.ErrorIs(t, err, ErrNoRootModule)
|
||||
_, err = ParseModuleArchive(buildArchive(map[string]string{"main.tf": strings.Repeat("#", packages_module.MaxMetadataSize)}))
|
||||
assert.ErrorIs(t, err, packages_module.ErrContentTooLarge)
|
||||
|
||||
for _, src := range []string{`variable "x" { description = "`, `variable "x" { description = "\`, `x = <<EOT`, `/*`, `variable {`, `"${"${`, `}}}`} {
|
||||
assert.NotNil(t, parseModule(map[string][]byte{"main.tf": slices.Clip([]byte(src))}))
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateAddress(t *testing.T) {
|
||||
assert.NoError(t, ValidateAddress("org", "My_vpc-2", "azurerm"))
|
||||
assert.ErrorIs(t, ValidateAddress("john.doe", "vpc", "aws"), ErrInvalidNamespace)
|
||||
assert.ErrorIs(t, ValidateAddress("org", "vpc-", "aws"), ErrInvalidName)
|
||||
assert.ErrorIs(t, ValidateAddress("org", "vpc", "my-cloud"), ErrInvalidProvider)
|
||||
}
|
||||
+27
-25
@@ -16,31 +16,32 @@ var (
|
||||
Storage *Storage
|
||||
Enabled bool
|
||||
|
||||
LimitTotalOwnerCount int64
|
||||
LimitTotalOwnerSize int64
|
||||
LimitSizeAlpine int64
|
||||
LimitSizeArch int64
|
||||
LimitSizeCargo int64
|
||||
LimitSizeChef int64
|
||||
LimitSizeComposer int64
|
||||
LimitSizeConan int64
|
||||
LimitSizeConda int64
|
||||
LimitSizeContainer int64
|
||||
LimitSizeCran int64
|
||||
LimitSizeDebian int64
|
||||
LimitSizeGeneric int64
|
||||
LimitSizeGo int64
|
||||
LimitSizeHelm int64
|
||||
LimitSizeMaven int64
|
||||
LimitSizeNpm int64
|
||||
LimitSizeNuGet int64
|
||||
LimitSizePub int64
|
||||
LimitSizePyPI int64
|
||||
LimitSizeRpm int64
|
||||
LimitSizeRubyGems int64
|
||||
LimitSizeSwift int64
|
||||
LimitSizeTerraformState int64
|
||||
LimitSizeVagrant int64
|
||||
LimitTotalOwnerCount int64
|
||||
LimitTotalOwnerSize int64
|
||||
LimitSizeAlpine int64
|
||||
LimitSizeArch int64
|
||||
LimitSizeCargo int64
|
||||
LimitSizeChef int64
|
||||
LimitSizeComposer int64
|
||||
LimitSizeConan int64
|
||||
LimitSizeConda int64
|
||||
LimitSizeContainer int64
|
||||
LimitSizeCran int64
|
||||
LimitSizeDebian int64
|
||||
LimitSizeGeneric int64
|
||||
LimitSizeGo int64
|
||||
LimitSizeHelm int64
|
||||
LimitSizeMaven int64
|
||||
LimitSizeNpm int64
|
||||
LimitSizeNuGet int64
|
||||
LimitSizePub int64
|
||||
LimitSizePyPI int64
|
||||
LimitSizeRpm int64
|
||||
LimitSizeRubyGems int64
|
||||
LimitSizeSwift int64
|
||||
LimitSizeTerraformModule int64
|
||||
LimitSizeTerraformState int64
|
||||
LimitSizeVagrant int64
|
||||
|
||||
DefaultRPMSignEnabled bool
|
||||
}{
|
||||
@@ -87,6 +88,7 @@ func loadPackagesFrom(rootCfg ConfigProvider) (err error) {
|
||||
Packages.LimitSizeRpm = mustBytes(sec, "LIMIT_SIZE_RPM")
|
||||
Packages.LimitSizeRubyGems = mustBytes(sec, "LIMIT_SIZE_RUBYGEMS")
|
||||
Packages.LimitSizeSwift = mustBytes(sec, "LIMIT_SIZE_SWIFT")
|
||||
Packages.LimitSizeTerraformModule = mustBytes(sec, "LIMIT_SIZE_TERRAFORM_MODULE")
|
||||
Packages.LimitSizeTerraformState = mustBytes(sec, "LIMIT_SIZE_TERRAFORM_STATE")
|
||||
Packages.LimitSizeVagrant = mustBytes(sec, "LIMIT_SIZE_VAGRANT")
|
||||
Packages.DefaultRPMSignEnabled = sec.Key("DEFAULT_RPM_SIGN_ENABLED").MustBool(false)
|
||||
|
||||
@@ -3687,6 +3687,18 @@
|
||||
"packages.terraform.lock.error.already_locked": "Terraform state is already locked.",
|
||||
"packages.terraform.delete.locked": "Terraform state is locked and cannot be deleted.",
|
||||
"packages.terraform.delete.latest": "The latest version of a Terraform state cannot be deleted.",
|
||||
"packages.terraform_module.install": "Add the module to your Terraform configuration:",
|
||||
"packages.terraform_module.install_terragrunt": "Or add it to your Terragrunt configuration:",
|
||||
"packages.terraform_module.credentials": "Configure credentials for this registry in ~/.terraformrc:",
|
||||
"packages.terraform_module.inputs": "Inputs",
|
||||
"packages.terraform_module.outputs": "Outputs",
|
||||
"packages.terraform_module.providers": "Required Providers",
|
||||
"packages.terraform_module.submodules": "Submodules",
|
||||
"packages.terraform_module.name": "Name",
|
||||
"packages.terraform_module.type": "Type",
|
||||
"packages.terraform_module.default": "Default",
|
||||
"packages.terraform_module.description": "Description",
|
||||
"packages.terraform_module.required": "required",
|
||||
"packages.vagrant.install": "To add a Vagrant box, run the following command:",
|
||||
"packages.settings.link": "Link this package to a repository",
|
||||
"packages.settings.link.description": "If you link a package with a repository, the package will appear in the repository's package list.",
|
||||
|
||||
@@ -35,6 +35,7 @@ import (
|
||||
"gitea.dev/routers/api/packages/rubygems"
|
||||
"gitea.dev/routers/api/packages/swift"
|
||||
"gitea.dev/routers/api/packages/terraform"
|
||||
tfmodule "gitea.dev/routers/api/packages/terraform_module"
|
||||
"gitea.dev/routers/api/packages/vagrant"
|
||||
"gitea.dev/services/auth"
|
||||
"gitea.dev/services/context"
|
||||
@@ -101,6 +102,19 @@ func CommonRoutes() *web.Router {
|
||||
&chef.Auth{},
|
||||
}, verifyAuthOptions{})
|
||||
|
||||
// host-level base URL for service discovery, the namespace is the owner
|
||||
r.Group("/-/terraform/modules/{username}/{name}/{provider}", func() {
|
||||
r.Get("/{version}/archive", func(ctx *context.Context) {
|
||||
if !tfmodule.HasValidArchiveSignature(ctx) {
|
||||
reqPackageAccess(perm.AccessModeRead)(ctx)
|
||||
}
|
||||
}, tfmodule.DownloadArchive)
|
||||
r.Get("/versions", reqPackageAccess(perm.AccessModeRead), tfmodule.ListVersions)
|
||||
r.Get("/{version}/download", reqPackageAccess(perm.AccessModeRead), tfmodule.DownloadRedirect)
|
||||
r.Put("/{version}", reqPackageAccess(perm.AccessModeWrite), tfmodule.UploadModule)
|
||||
r.Delete("/{version}", reqPackageAccess(perm.AccessModeWrite), tfmodule.DeleteModule)
|
||||
}, context.UserAssignmentWeb(), context.PackageAssignment())
|
||||
|
||||
r.Group("/{username}", func() {
|
||||
r.Group("/alpine", func() {
|
||||
r.Get("/key", alpine.GetRepositoryKey)
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
// 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)
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func ListPackages(ctx *context.APIContext) {
|
||||
// in: query
|
||||
// description: package type filter
|
||||
// type: string
|
||||
// enum: [alpine, cargo, chef, composer, conan, conda, container, cran, debian, generic, go, helm, maven, npm, nuget, pub, pypi, rpm, rubygems, swift, terraform, vagrant]
|
||||
// enum: [alpine, arch, cargo, chef, composer, conan, conda, container, cran, debian, generic, go, helm, maven, npm, nuget, pub, pypi, rpm, rubygems, swift, terraform, terraform-module, vagrant]
|
||||
// - name: q
|
||||
// in: query
|
||||
// description: name filter
|
||||
|
||||
@@ -43,7 +43,7 @@ func ListPackages(ctx *context.APIContext) {
|
||||
// in: query
|
||||
// description: package type filter
|
||||
// type: string
|
||||
// enum: [alpine, cargo, chef, composer, conan, conda, container, cran, debian, generic, go, helm, maven, npm, nuget, pub, pypi, rpm, rubygems, swift, terraform, vagrant]
|
||||
// enum: [alpine, arch, cargo, chef, composer, conan, conda, container, cran, debian, generic, go, helm, maven, npm, nuget, pub, pypi, rpm, rubygems, swift, terraform, terraform-module, vagrant]
|
||||
// - name: q
|
||||
// in: query
|
||||
// description: name filter
|
||||
|
||||
@@ -26,6 +26,7 @@ import (
|
||||
container_module "gitea.dev/modules/packages/container"
|
||||
rpm_module "gitea.dev/modules/packages/rpm"
|
||||
terraform_module "gitea.dev/modules/packages/terraform"
|
||||
tfmod "gitea.dev/modules/packages/terraform_module"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/templates"
|
||||
"gitea.dev/modules/util"
|
||||
@@ -203,6 +204,16 @@ func ViewPackageVersion(ctx *context.Context) {
|
||||
}
|
||||
ctx.Data["PackageRegistryHost"] = registryHostURL.Host
|
||||
|
||||
if submodule := ctx.PathParam("submodule"); submodule != "" {
|
||||
metadata, ok := pd.Metadata.(*tfmod.Metadata) // the route is shared by all package types
|
||||
if !ok || metadata.Submodules[submodule] == nil {
|
||||
ctx.NotFound(nil)
|
||||
return
|
||||
}
|
||||
ctx.Data["PackageSubmodule"] = submodule
|
||||
ctx.Data["Title"] = pd.Package.Name + " //modules/" + submodule
|
||||
}
|
||||
|
||||
switch pd.Package.Type {
|
||||
case packages_model.TypeAlpine:
|
||||
branches := make(container.Set[string])
|
||||
|
||||
@@ -27,6 +27,7 @@ import (
|
||||
"gitea.dev/modules/web/middleware"
|
||||
"gitea.dev/modules/web/routing"
|
||||
"gitea.dev/modules/web/types"
|
||||
tfmodule "gitea.dev/routers/api/packages/terraform_module"
|
||||
"gitea.dev/routers/common"
|
||||
"gitea.dev/routers/web/admin"
|
||||
"gitea.dev/routers/web/auth"
|
||||
@@ -526,6 +527,7 @@ func registerWebRoutes(m *web.Router, webAuth *AuthMiddleware) {
|
||||
ctx.Redirect(setting.AppSubURL + "/user/settings/account")
|
||||
})
|
||||
m.Get("/passkey-endpoints", passkeyEndpoints)
|
||||
m.Get("/terraform.json", packagesEnabled, tfmodule.ServiceDiscovery)
|
||||
m.Methods("GET, HEAD", "/*", public.FileHandlerFunc())
|
||||
}, optionsCorsHandler())
|
||||
|
||||
@@ -1088,6 +1090,7 @@ func registerWebRoutes(m *web.Router, webAuth *AuthMiddleware) {
|
||||
m.Get("", user.ViewPackageVersion)
|
||||
m.Post("", reqPackageAccess(perm.AccessModeWrite), user.PackageVersionDelete)
|
||||
m.Get("/{version_sub}", user.ViewPackageVersion)
|
||||
m.Get("/modules/{submodule}", user.ViewPackageVersion)
|
||||
m.Group("/terraform", func() {
|
||||
m.Post("/lock", user.ActionPackageTerraformLock)
|
||||
m.Post("/unlock", user.ActionPackageTerraformUnlock)
|
||||
|
||||
@@ -9,7 +9,7 @@ type PackageCleanupRuleForm struct {
|
||||
middleware.FormDefaultValidator
|
||||
ID int64
|
||||
Enabled bool
|
||||
Type string `binding:"Required;In(alpine,arch,cargo,chef,composer,conan,conda,container,cran,debian,generic,go,helm,maven,npm,nuget,pub,pypi,rpm,rubygems,swift,terraform,vagrant)"`
|
||||
Type string `binding:"Required;In(alpine,arch,cargo,chef,composer,conan,conda,container,cran,debian,generic,go,helm,maven,npm,nuget,pub,pypi,rpm,rubygems,swift,terraform,terraform-module,vagrant)"`
|
||||
KeepCount int `binding:"In(0,1,5,10,25,50,100)"`
|
||||
KeepPattern string `binding:"RegexPattern"`
|
||||
RemoveDays int `binding:"In(0,7,14,30,60,90,180)"`
|
||||
|
||||
@@ -437,6 +437,8 @@ func CheckSizeQuotaExceeded(ctx context.Context, doer, owner *user_model.User, p
|
||||
typeSpecificSize = setting.Packages.LimitSizeRubyGems
|
||||
case packages_model.TypeSwift:
|
||||
typeSpecificSize = setting.Packages.LimitSizeSwift
|
||||
case packages_model.TypeTerraformModule:
|
||||
typeSpecificSize = setting.Packages.LimitSizeTerraformModule
|
||||
case packages_model.TypeTerraformState:
|
||||
typeSpecificSize = setting.Packages.LimitSizeTerraformState
|
||||
case packages_model.TypeVagrant:
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
{{if eq .PackageDescriptor.Package.Type "terraform-module"}}
|
||||
{{$metadata := .PackageDescriptor.Metadata}}
|
||||
{{$source := print .PackageRegistryHost "/" .PackageDescriptor.Owner.Name "/" .PackageDescriptor.Package.Name}}
|
||||
{{$submodule := .PackageSubmodule}}
|
||||
{{$moduleSource := $source}}
|
||||
{{$moduleLabel := index (StringUtils.Cut .PackageDescriptor.Package.Name "/") 0}}
|
||||
{{$readme := $metadata.Readme}}
|
||||
{{$module := $metadata.Root}}
|
||||
{{$treePath := ""}}
|
||||
{{if $submodule}}
|
||||
{{$sub := index $metadata.Submodules $submodule}}
|
||||
{{$moduleSource = print $source "//modules/" $submodule}}
|
||||
{{$moduleLabel = $submodule}}
|
||||
{{$readme = $sub.Readme}}
|
||||
{{$module = $sub.Module}}
|
||||
{{$treePath = print "modules/" $submodule}}
|
||||
{{end}}
|
||||
<h4 class="ui top attached header">{{ctx.Locale.Tr "packages.installation"}}</h4>
|
||||
<div class="ui attached segment">
|
||||
<div class="ui form">
|
||||
<div class="field">
|
||||
<label>{{svg "octicon-code"}} {{ctx.Locale.Tr "packages.terraform_module.install"}}</label>
|
||||
<div class="markup"><pre class="code-block"><code>module "{{$moduleLabel}}" {
|
||||
source = "{{$moduleSource}}"
|
||||
version = "{{.PackageDescriptor.Version.Version}}"
|
||||
}</code></pre></div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>{{svg "octicon-code"}} {{ctx.Locale.Tr "packages.terraform_module.install_terragrunt"}}</label>
|
||||
<div class="markup"><pre class="code-block"><code>terraform {
|
||||
source = "tfr://{{$moduleSource}}?version={{.PackageDescriptor.Version.Version}}"
|
||||
}</code></pre></div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>{{svg "octicon-key"}} {{ctx.Locale.Tr "packages.terraform_module.credentials"}}</label>
|
||||
<div class="markup"><pre class="code-block"><code>credentials "{{.PackageRegistryHost}}" {
|
||||
token = "<token>"
|
||||
}</code></pre></div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>{{ctx.Locale.Tr "packages.registry.documentation" "Terraform Module" "https://docs.gitea.com/usage/packages/terraform-module"}}</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{if $readme}}
|
||||
<h4 class="ui top attached header">{{ctx.Locale.Tr "packages.about"}}</h4>
|
||||
<div class="ui attached segment">{{ctx.RenderUtils.RenderPackageMarkdown $readme .PackageDescriptor.Repository $treePath}}</div>
|
||||
{{end}}
|
||||
|
||||
{{if and $module $module.Inputs}}
|
||||
<h4 class="ui top attached header">{{ctx.Locale.Tr "packages.terraform_module.inputs"}}</h4>
|
||||
<div class="ui attached segment">
|
||||
<table class="ui table">
|
||||
<thead><tr>
|
||||
<th>{{ctx.Locale.Tr "packages.terraform_module.name"}}</th>
|
||||
<th>{{ctx.Locale.Tr "packages.terraform_module.type"}}</th>
|
||||
<th>{{ctx.Locale.Tr "packages.terraform_module.default"}}</th>
|
||||
<th>{{ctx.Locale.Tr "packages.terraform_module.description"}}</th>
|
||||
</tr></thead>
|
||||
<tbody>
|
||||
{{range $input := $module.Inputs}}
|
||||
<tr>
|
||||
<td><code>{{$input.Name}}</code>{{if $input.Required}} <span class="ui tiny red label">{{ctx.Locale.Tr "packages.terraform_module.required"}}</span>{{end}}</td>
|
||||
<td><code>{{$input.Type}}</code></td>
|
||||
<td><code>{{$input.Default}}</code></td>
|
||||
<td>{{$input.Description}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{if and $module $module.Outputs}}
|
||||
<h4 class="ui top attached header">{{ctx.Locale.Tr "packages.terraform_module.outputs"}}</h4>
|
||||
<div class="ui attached segment">
|
||||
<table class="ui table">
|
||||
<thead><tr>
|
||||
<th>{{ctx.Locale.Tr "packages.terraform_module.name"}}</th>
|
||||
<th>{{ctx.Locale.Tr "packages.terraform_module.description"}}</th>
|
||||
</tr></thead>
|
||||
<tbody>
|
||||
{{range $output := $module.Outputs}}
|
||||
<tr>
|
||||
<td><code>{{$output.Name}}</code></td>
|
||||
<td>{{$output.Description}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{if and $module $module.Providers}}
|
||||
<h4 class="ui top attached header">{{ctx.Locale.Tr "packages.terraform_module.providers"}}</h4>
|
||||
<div class="ui attached segment">
|
||||
<ul>{{range $provider := $module.Providers}}<li><code>{{$provider.Name}}</code>{{if $provider.Source}} ({{$provider.Source}}){{end}}{{if $provider.Version}} <code>{{$provider.Version}}</code>{{end}}</li>{{end}}</ul>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
{{if and (not $submodule) $metadata.Submodules}}
|
||||
<h4 class="ui top attached header">{{ctx.Locale.Tr "packages.terraform_module.submodules"}}</h4>
|
||||
<div class="ui attached segment">
|
||||
<ul>{{range $name, $_sub := $metadata.Submodules}}<li><a href="{{$.PackageDescriptor.VersionWebLink}}/modules/{{PathEscape $name}}"><code>{{$source}}//modules/{{$name}}</code></a></li>{{end}}</ul>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
@@ -39,6 +39,7 @@
|
||||
{{template "package/content/rubygems" .}}
|
||||
{{template "package/content/swift" .}}
|
||||
{{template "package/content/terraform" .}}
|
||||
{{template "package/content/terraform_module" .}}
|
||||
{{template "package/content/vagrant" .}}
|
||||
</div>
|
||||
<div class="ui segment packages-content-right">
|
||||
|
||||
+4
@@ -12011,6 +12011,7 @@
|
||||
"schema": {
|
||||
"enum": [
|
||||
"alpine",
|
||||
"arch",
|
||||
"cargo",
|
||||
"chef",
|
||||
"composer",
|
||||
@@ -12031,6 +12032,7 @@
|
||||
"rubygems",
|
||||
"swift",
|
||||
"terraform",
|
||||
"terraform-module",
|
||||
"vagrant"
|
||||
],
|
||||
"type": "string"
|
||||
@@ -16295,6 +16297,7 @@
|
||||
"schema": {
|
||||
"enum": [
|
||||
"alpine",
|
||||
"arch",
|
||||
"cargo",
|
||||
"chef",
|
||||
"composer",
|
||||
@@ -16315,6 +16318,7 @@
|
||||
"rubygems",
|
||||
"swift",
|
||||
"terraform",
|
||||
"terraform-module",
|
||||
"vagrant"
|
||||
],
|
||||
"type": "string"
|
||||
|
||||
+4
@@ -630,6 +630,7 @@
|
||||
{
|
||||
"enum": [
|
||||
"alpine",
|
||||
"arch",
|
||||
"cargo",
|
||||
"chef",
|
||||
"composer",
|
||||
@@ -650,6 +651,7 @@
|
||||
"rubygems",
|
||||
"swift",
|
||||
"terraform",
|
||||
"terraform-module",
|
||||
"vagrant"
|
||||
],
|
||||
"type": "string",
|
||||
@@ -4728,6 +4730,7 @@
|
||||
{
|
||||
"enum": [
|
||||
"alpine",
|
||||
"arch",
|
||||
"cargo",
|
||||
"chef",
|
||||
"composer",
|
||||
@@ -4748,6 +4751,7 @@
|
||||
"rubygems",
|
||||
"swift",
|
||||
"terraform",
|
||||
"terraform-module",
|
||||
"vagrant"
|
||||
],
|
||||
"type": "string",
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package integration
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.dev/models/unittest"
|
||||
user_model "gitea.dev/models/user"
|
||||
"gitea.dev/modules/setting"
|
||||
"gitea.dev/modules/test"
|
||||
"gitea.dev/tests"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPackageTerraformModule(t *testing.T) {
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
admin := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||
privateOrg := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "privated_org"})
|
||||
root := "/api/packages/-/terraform/modules/" + privateOrg.Name
|
||||
base := root + "/vpc/aws"
|
||||
archive := test.WriteTarCompression(gzip.NewWriter, map[string]string{
|
||||
"main.tf": `variable "region" {}`,
|
||||
"README.md": "# VPC module",
|
||||
"modules/net/main.tf": "variable \"cidr\" {}\n",
|
||||
"modules/net/README.md": "# Net submodule\n",
|
||||
}).Bytes()
|
||||
|
||||
upload := func(t *testing.T, uploadURL string, body []byte, status int) {
|
||||
MakeRequest(t, NewRequestWithBody(t, "PUT", uploadURL, bytes.NewReader(body)).AddBasicAuth(admin.Name), status)
|
||||
}
|
||||
|
||||
t.Run("ServiceDiscovery", func(t *testing.T) {
|
||||
resp := MakeRequest(t, NewRequest(t, "GET", "/.well-known/terraform.json"), http.StatusOK)
|
||||
assert.JSONEq(t, `{"modules.v1":"/api/packages/-/terraform/modules/"}`, resp.Body.String())
|
||||
|
||||
defer test.MockVariableValue(&setting.Packages.Enabled, false)()
|
||||
MakeRequest(t, NewRequest(t, "GET", "/.well-known/terraform.json"), http.StatusForbidden)
|
||||
})
|
||||
|
||||
t.Run("Upload", func(t *testing.T) {
|
||||
upload(t, base+"/v1.0.0", archive, http.StatusCreated)
|
||||
upload(t, base+"/1.0.0", archive, http.StatusConflict)
|
||||
upload(t, base+"/not-semver", archive, http.StatusBadRequest)
|
||||
upload(t, root+"/vpc/my-cloud/1.0.0", archive, http.StatusBadRequest)
|
||||
upload(t, base+"/2.0.0", test.WriteTarCompression(gzip.NewWriter, map[string]string{"vpc-2.0.0/main.tf": `variable "region" {}`}).Bytes(), http.StatusBadRequest)
|
||||
MakeRequest(t, NewRequestWithBody(t, "PUT", base+"/2.0.0", bytes.NewReader(archive)), http.StatusUnauthorized)
|
||||
})
|
||||
|
||||
t.Run("Web", func(t *testing.T) {
|
||||
session := loginUser(t, admin.Name)
|
||||
session.MakeRequest(t, NewRequestWithValues(t, "POST", "/user/settings/packages/rules/add", map[string]string{
|
||||
"type": "terraform-module", "action": "save", "keep_count": "0", "remove_days": "0",
|
||||
}), http.StatusSeeOther)
|
||||
page := "/" + privateOrg.Name + "/-/packages/terraform-module/vpc%2Faws/1.0.0"
|
||||
appURL, _ := url.Parse(setting.AppURL)
|
||||
content := NewHTMLParser(t, session.MakeRequest(t, NewRequest(t, "GET", page), http.StatusOK).Body).Find(".packages-content-left").Text()
|
||||
assert.Contains(t, content, `source = "`+appURL.Host+"/"+privateOrg.Name+`/vpc/aws"`)
|
||||
assert.Contains(t, content, "VPC module")
|
||||
assert.Contains(t, content, "region")
|
||||
assert.Contains(t, content, "/vpc/aws//modules/net")
|
||||
|
||||
content = NewHTMLParser(t, session.MakeRequest(t, NewRequest(t, "GET", page+"/modules/net"), http.StatusOK).Body).Find(".packages-content-left").Text()
|
||||
assert.Contains(t, content, `source = "`+appURL.Host+"/"+privateOrg.Name+`/vpc/aws//modules/net"`)
|
||||
assert.Contains(t, content, "cidr")
|
||||
assert.Contains(t, content, "Net submodule")
|
||||
session.MakeRequest(t, NewRequest(t, "GET", page+"/modules/unknown"), http.StatusNotFound)
|
||||
|
||||
MakeRequest(t, NewRequestWithBody(t, "PUT", "/api/packages/"+privateOrg.Name+"/generic/vpc/1.0.0/file.bin", strings.NewReader("x")).AddBasicAuth(admin.Name), http.StatusCreated)
|
||||
session.MakeRequest(t, NewRequest(t, "GET", "/"+privateOrg.Name+"/-/packages/generic/vpc/1.0.0/modules/net"), http.StatusNotFound)
|
||||
})
|
||||
|
||||
t.Run("ListVersions", func(t *testing.T) {
|
||||
resp := MakeRequest(t, NewRequest(t, "GET", base+"/versions").AddBasicAuth(admin.Name), http.StatusOK)
|
||||
assert.JSONEq(t, `{"modules":[{"versions":[{"version":"1.0.0"}]}]}`, resp.Body.String())
|
||||
MakeRequest(t, NewRequest(t, "GET", base+"/versions"), http.StatusUnauthorized)
|
||||
MakeRequest(t, NewRequest(t, "GET", root+"/unknown/aws/versions").AddBasicAuth(admin.Name), http.StatusNotFound)
|
||||
})
|
||||
|
||||
t.Run("Download", func(t *testing.T) {
|
||||
downloadURL := base + "/1.0.0/download"
|
||||
resp := MakeRequest(t, NewRequest(t, "GET", downloadURL).AddBasicAuth(admin.Name), http.StatusNoContent)
|
||||
location, err := url.Parse(resp.Header().Get("X-Terraform-Get"))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "tar.gz", location.Query().Get("archive"))
|
||||
archiveURL := (&url.URL{Path: downloadURL}).ResolveReference(location).String()
|
||||
|
||||
resp = MakeRequest(t, NewRequest(t, "GET", archiveURL), http.StatusOK)
|
||||
assert.Equal(t, archive, resp.Body.Bytes())
|
||||
MakeRequest(t, NewRequest(t, "GET", strings.Replace(archiveURL, "code=", "code=x", 1)), http.StatusUnauthorized)
|
||||
MakeRequest(t, NewRequest(t, "GET", base+"/1.0.0/archive").AddBasicAuth(admin.Name), http.StatusOK)
|
||||
MakeRequest(t, NewRequest(t, "GET", base+"/9.9.9/download").AddBasicAuth(admin.Name), http.StatusNotFound)
|
||||
})
|
||||
|
||||
t.Run("Delete", func(t *testing.T) {
|
||||
MakeRequest(t, NewRequest(t, "DELETE", base+"/1.0.0"), http.StatusUnauthorized)
|
||||
MakeRequest(t, NewRequest(t, "DELETE", base+"/v1.0.0").AddBasicAuth(admin.Name), http.StatusNoContent)
|
||||
MakeRequest(t, NewRequest(t, "GET", base+"/versions").AddBasicAuth(admin.Name), http.StatusNotFound)
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user