Files
gitea/tests/integration/api_packages_terraform_module_test.go
T
5d9bfa1e14 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>
2026-11-07 10:45:05 +00:00

110 lines
5.3 KiB
Go

// 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)
})
}