mirror of
https://github.com/coder/coder.git
synced 2026-10-16 14:48:56 +08:00
## Summary Threads the deployment's configured module registry (`CODER_TEMPLATE_BUILDER_REGISTRY_URL`) into template-builder **base** rendering, so a base that embeds a catalog module (e.g. the quickstart base's `git-clone`) resolves its `source` through a registry mirror the same way wizard-composed modules already do. Previously base-embedded module sources hardcoded `registry.coder.com`, so on a mirror/private-registry deployment a base-embedded module reached the public registry while wizard modules used the mirror. Implements [DOCS-610](https://linear.app/codercom/issue/DOCS-610). ## What changed * `BaseRenderContext.RegistryBase` (new field), mirroring `ModuleRenderContext.RegistryBase`. `Compose` threads the deployment registry through `renderBase` into the base render context, and the four hardcoded base module sources (`quickstart` git-clone, `azure-linux`/`gcp-linux`/`gcp-windows` region modules) now use `{{ .RegistryBase }}`. Because the value defaults back to `registry.coder.com`, rendered sources stay byte-identical; the only golden change is the removal of the now-stale quickstart "follow-up" note. * **Boot-time validation.** `codersdk.ValidateTemplateBuilderRegistryURL` runs in `DeploymentValues.Validate()`, gated on the builder being enabled, matching the chat-hook precedent. A malformed value fails at server start naming the option, instead of surfacing as a per-request error to a template author who cannot fix it. It uses `net/url` in authority form to reject a scheme, path, query, fragment, trailing slash, or credentials and accept a bare `host[:port]`; it does not canonicalize the host. * `Compose` **trusts the validated value**, defaulting an unset one and defensively rejecting a malformed one for direct callers that bypass boot. ## Tests * `codersdk`: validator accept/reject matrix (incl. credentials-without-echo) and a `DeploymentValues.Validate` boundary test with a disabled-skips-validation case. * `templatebuilder`: a base honors the mirror and falls back to the default when unset, plus a defensive-reject case; existing compose tests updated to bare-host registry values. <details><summary>Design decision log</summary> This PR is a deliberately minimal reimplementation of the [DOCS-610](https://linear.app/codercom/issue/DOCS-610/template-builder-base-embedded-modules-should-honor-the-deployment) scope, started fresh from `main` rather than carrying forward an earlier, larger draft (coder/coder#28480). Key decisions: * **Scope kept to the ticket.** The ticket's fix is three bullets: add a registry field to `BaseRenderContext`, thread the deployment `RegistryURL` through `Compose` -> `renderBase`, and use it in base module sources. Independent hardening explored in the earlier draft was dropped as out of scope: `terraform-svchost`-based IDN/punycode/port canonicalization, a rendered-HCL backstop with a 500 mapping, and HCL module-source extraction helpers. Telemetry misclassification of mirror modules is tracked separately ([DOCS-712](https://linear.app/codercom/issue/DOCS-712/telemetry-misclassifies-registry-mirror-modules-as-non-coder)) and is not addressed here. * **Default lives in the render layer.** `templatebuilder.DefaultRegistryBase` holds the fallback host; `codersdk` keeps its own option-default literal. An earlier version exported the constant from `codersdk`, which only emitted a dead `typesGenerated.ts` entry that nothing in the frontend consumed, so it was moved out. The `codersdk` validator is a function, so it generates no TS type. * **Validate at the config boundary, fail fast.** The repo convention is to hard-fail structurally invalid config in `DeploymentValues.Validate()` (access URL, OAuth lifetimes, chat hook URL) and to warn only for functional-but-risky settings. A malformed registry host is structurally invalid, so it fails at boot. This also puts the error in front of the admin who set it, rather than a template author hitting a per-request error they cannot fix. * `net/url`**, not a regex.** The codebase validates identifiers/names with regexes but host/URL-shaped config with `url.Parse` (chat hook URL, OAuth redirect URIs, AI provider base URL). This value is host-shaped, so it follows that convention. `url.Parse("//" + v)` with `u.Host == v` rejects a scheme/path/query/fragment/trailing-slash in one check. </details> > This PR was created with AI assistance (Coder Agents).
167 lines
5.9 KiB
Go
167 lines
5.9 KiB
Go
package templatebuilder
|
|
|
|
import (
|
|
"bytes"
|
|
"io/fs"
|
|
"regexp"
|
|
"text/template"
|
|
|
|
"golang.org/x/xerrors"
|
|
)
|
|
|
|
// ImageOption represents a container image choice for base template parameters.
|
|
type ImageOption struct {
|
|
Name string
|
|
Value string
|
|
}
|
|
|
|
// DefaultRegistryBase is the module registry host used in rendered module source
|
|
// paths when a ComposeRequest does not carry the deployment's configured
|
|
// registry (CODER_TEMPLATE_BUILDER_REGISTRY_URL). It mirrors the default of the
|
|
// codersdk template-builder registry option.
|
|
const DefaultRegistryBase = "registry.coder.com"
|
|
|
|
// BaseRenderContext is the data passed to base template .tf.tmpl files.
|
|
type BaseRenderContext struct {
|
|
ContainerImage string
|
|
ImageOptions []ImageOption
|
|
// RegistryBase is the module registry host used in rendered module source
|
|
// paths, mirroring ModuleRenderContext.RegistryBase so a base-embedded
|
|
// module honors CODER_TEMPLATE_BUILDER_REGISTRY_URL like a wizard module.
|
|
RegistryBase string
|
|
Variables map[string]string
|
|
}
|
|
|
|
// ModuleRenderContext is the data passed to module .tf.tmpl files.
|
|
type ModuleRenderContext struct {
|
|
// RegistryBase is the module registry URL from the deployment config
|
|
// (CODER_TEMPLATE_BUILDER_REGISTRY_URL).
|
|
RegistryBase string
|
|
// PinnedVersion is the module version from the catalog manifest.
|
|
PinnedVersion string
|
|
// AgentResourceName is the Terraform resource name of the coder_agent
|
|
// declared in the base template (e.g. "main" or "dev").
|
|
AgentResourceName string
|
|
// Variables maps variable names to their HCL expressions.
|
|
Variables map[string]string
|
|
}
|
|
|
|
// RenderBaseTemplate executes a pre-parsed .tf.tmpl template for the given
|
|
// base, applying the provided render context. Templates are parsed once at
|
|
// first access via sync.OnceValues, so parse errors surface early instead
|
|
// of at render time.
|
|
func RenderBaseTemplate(exampleID, templatePath string, renderCtx BaseRenderContext) ([]byte, error) {
|
|
if renderCtx.Variables == nil {
|
|
renderCtx.Variables = make(map[string]string)
|
|
}
|
|
|
|
bases, err := loadBases()
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("load base catalog: %w", err)
|
|
}
|
|
|
|
base, ok := bases[exampleID]
|
|
if !ok {
|
|
return nil, xerrors.Errorf("unknown base template %q", exampleID)
|
|
}
|
|
|
|
tmpl, ok := base.Templates[templatePath]
|
|
if !ok {
|
|
return nil, xerrors.Errorf("template %s not found in base %q", templatePath, exampleID)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
if err := tmpl.Execute(&buf, renderCtx); err != nil {
|
|
return nil, xerrors.Errorf("execute template %s: %w", templatePath, err)
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
// RenderModuleTemplate parses and executes a module .tf.tmpl file from
|
|
// the given filesystem, applying the provided render context.
|
|
func RenderModuleTemplate(fsys fs.FS, templatePath string, renderCtx ModuleRenderContext) ([]byte, error) {
|
|
if renderCtx.Variables == nil {
|
|
renderCtx.Variables = make(map[string]string)
|
|
}
|
|
return renderTemplate(fsys, templatePath, renderCtx)
|
|
}
|
|
|
|
// renderTemplate is the shared implementation for module template rendering.
|
|
// It sets missingkey=error so that references to undefined variable keys fail
|
|
// loudly instead of producing "<no value>".
|
|
func renderTemplate(fsys fs.FS, templatePath string, data any) ([]byte, error) {
|
|
raw, err := fs.ReadFile(fsys, templatePath)
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("read template %s: %w", templatePath, err)
|
|
}
|
|
|
|
tmpl, err := template.New(templatePath).Option("missingkey=error").Parse(string(raw))
|
|
if err != nil {
|
|
return nil, xerrors.Errorf("parse template %s: %w", templatePath, err)
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
if err := tmpl.Execute(&buf, data); err != nil {
|
|
return nil, xerrors.Errorf("execute template %s: %w", templatePath, err)
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
// agentResourcePattern matches `resource "coder_agent" "<name>"` in HCL.
|
|
var agentResourcePattern = regexp.MustCompile(`resource\s+"coder_agent"\s+"(\w+)"`)
|
|
|
|
// agentCountPattern detects whether a coder_agent block uses count or
|
|
// for_each, which means references to it require an index (e.g. [0]).
|
|
var agentCountPattern = regexp.MustCompile(
|
|
`resource\s+"coder_agent"\s+"\w+"\s*\{[^}]*\b(?:count|for_each)\s*=`,
|
|
)
|
|
|
|
// ExtractAgentResourceName finds the coder_agent resource declaration in
|
|
// rendered HCL and returns the reference form to use in module templates.
|
|
// When the agent uses count or for_each, the returned name includes an
|
|
// index suffix (e.g. "dev[0]") so that module templates can reference it
|
|
// as coder_agent.<name>.id. Returns an error unless exactly one
|
|
// coder_agent resource is found; the builder only supports single-agent
|
|
// templates. The input is expected to be rendered output from our own
|
|
// curated base templates, not arbitrary user HCL.
|
|
func ExtractAgentResourceName(hcl []byte) (string, error) {
|
|
matches := agentResourcePattern.FindAllSubmatch(hcl, -1)
|
|
switch len(matches) {
|
|
case 0:
|
|
return "", xerrors.New("no coder_agent resource found in rendered template")
|
|
case 1:
|
|
name := string(matches[0][1])
|
|
if agentCountPattern.Match(hcl) {
|
|
name += "[0]"
|
|
}
|
|
return name, nil
|
|
default:
|
|
names := make([]string, 0, len(matches))
|
|
for _, m := range matches {
|
|
names = append(names, string(m[1]))
|
|
}
|
|
return "", xerrors.Errorf("expected exactly one coder_agent resource, found %d: %v",
|
|
len(matches), names)
|
|
}
|
|
}
|
|
|
|
// moduleBlockPattern matches a `module "<name>"` block declaration anchored to
|
|
// the start of a line in HCL.
|
|
var moduleBlockPattern = regexp.MustCompile(`(?m)^[ \t]*module[ \t]+"([^"]+)"`)
|
|
|
|
// ExtractModuleNames returns the labels of every module block declared in
|
|
// rendered HCL, in declaration order. Matching is anchored to the start of a
|
|
// line so commented-out references or string literals are ignored. The input
|
|
// is expected to be rendered output from our own curated base templates, not
|
|
// arbitrary user HCL.
|
|
func ExtractModuleNames(hcl []byte) []string {
|
|
matches := moduleBlockPattern.FindAllSubmatch(hcl, -1)
|
|
names := make([]string, 0, len(matches))
|
|
for _, m := range matches {
|
|
names = append(names, string(m[1]))
|
|
}
|
|
return names
|
|
}
|