Hardcode the repository releases come from
routerUpdate.repository (a module option beside the update keys, not a router.* setting) defaults to the project's repository; the Updates page names it but no longer has a field for it. Forks and tests set it in a module. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -3,21 +3,20 @@ package main
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Updates from a git repository (router.update.repository): router-update
|
||||
// check looks at the repository's release tags (vX.Y.Z) once a day and when the
|
||||
// page asks, and writes what it found to update/check.json; installing builds
|
||||
// the router from a tag, like an update from a bundle.
|
||||
// Updates from a git repository: router-update check looks at the
|
||||
// repository's release tags (vX.Y.Z) once a day and when the page asks, and
|
||||
// writes what it found to update/check.json; installing builds the router
|
||||
// from a tag, like an update from a bundle. The repository is part of the
|
||||
// installed version (routerUpdate.repository, written to
|
||||
// /etc/router-update-repository), not something the page sets.
|
||||
|
||||
var (
|
||||
// The same shape the option accepts: no credentials, no query.
|
||||
repositoryRe = regexp.MustCompile(`^https?://[A-Za-z0-9.-]+(:[0-9]+)?(/[A-Za-z0-9._~/-]*)?$`)
|
||||
releaseTagRe = regexp.MustCompile(`^v[0-9]+\.[0-9]+\.[0-9]+$`)
|
||||
)
|
||||
var releaseTagRe = regexp.MustCompile(`^v[0-9]+\.[0-9]+\.[0-9]+$`)
|
||||
|
||||
// GitCheck is update/check.json: the newest release tag of the repository.
|
||||
type GitCheck struct {
|
||||
@@ -32,14 +31,23 @@ type GitCheck struct {
|
||||
Newer bool `json:"newer"`
|
||||
}
|
||||
|
||||
func (s Settings) repository() string { return str(s, "router", "update", "repository") }
|
||||
// repository is where releases come from ("" if the router has none).
|
||||
func (s System) repository() string {
|
||||
path := s.RepositoryFile
|
||||
if path == "" {
|
||||
path = "/etc/router-update-repository"
|
||||
}
|
||||
data, _ := os.ReadFile(path)
|
||||
return strings.TrimSpace(string(data))
|
||||
}
|
||||
|
||||
const gitCheckUnit = "router-update-check.service"
|
||||
|
||||
// gitCheck is what the last check found, if it is still about this repository
|
||||
// and this installed version.
|
||||
func (a *app) gitCheck(repository string) *GitCheck {
|
||||
func (a *app) gitCheck() *GitCheck {
|
||||
var c GitCheck
|
||||
repository := a.sys.repository()
|
||||
if repository == "" || !readUpgradeFile(filepath.Join(a.sys.updateDir(), "check.json"), &c) {
|
||||
return nil
|
||||
}
|
||||
@@ -52,37 +60,15 @@ func (a *app) gitCheck(repository string) *GitCheck {
|
||||
// newRouterVersion is the version of a release that is out and not installed,
|
||||
// for the Overview ("" if there is none or nothing was checked).
|
||||
func (a *app) newRouterVersion() string {
|
||||
s, err := loadSettings(a.sys.settingsPath())
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
if c := a.gitCheck(s.repository()); c != nil && c.Newer {
|
||||
if c := a.gitCheck(); c != nil && c.Newer {
|
||||
return c.Newest.Version
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (a *app) updateSaveRepository(w http.ResponseWriter, r *http.Request, _ session) {
|
||||
v := strings.TrimRight(strings.TrimSpace(r.PostFormValue("repository")), "/")
|
||||
if !r.PostForm.Has("repository") {
|
||||
a.updateBack(w, r, "Not saved: no address was sent.")
|
||||
return
|
||||
}
|
||||
if v != "" && !repositoryRe.MatchString(v) {
|
||||
a.updateBack(w, r, "Not saved: an http:// or https:// address of a public repository, without credentials or a query.")
|
||||
return
|
||||
}
|
||||
if err := a.sys.setUpdateField("repository", v); err != nil {
|
||||
a.updateBack(w, r, "Not saved: "+err.Error())
|
||||
return
|
||||
}
|
||||
a.updateBack(w, r, "Saved.")
|
||||
}
|
||||
|
||||
func (a *app) updateGitCheck(w http.ResponseWriter, r *http.Request, _ session) {
|
||||
s, err := loadSettings(a.sys.settingsPath())
|
||||
if err != nil || s.repository() == "" {
|
||||
a.updateBack(w, r, "Set the repository first.")
|
||||
if a.sys.repository() == "" {
|
||||
a.updateBack(w, r, "This router has no repository to look at.")
|
||||
return
|
||||
}
|
||||
if err := startUnit(gitCheckUnit); err != nil {
|
||||
@@ -102,9 +88,8 @@ func (a *app) updateGitInstall(w http.ResponseWriter, r *http.Request, _ session
|
||||
a.updateBack(w, r, "Not a release tag: "+url.QueryEscape(tag))
|
||||
return
|
||||
}
|
||||
s, err := loadSettings(a.sys.settingsPath())
|
||||
if err != nil || s.repository() == "" {
|
||||
a.updateBack(w, r, "Set the repository first.")
|
||||
if a.sys.repository() == "" {
|
||||
a.updateBack(w, r, "This router has no repository to update from.")
|
||||
return
|
||||
}
|
||||
if err := a.sys.requestUpdate("git", tag, r.PostFormValue("force") == "on"); err != nil {
|
||||
|
||||
@@ -9,9 +9,13 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
const testRepository = "https://git.example.org/me/nixos-router"
|
||||
|
||||
func TestUpdatesFromGit(t *testing.T) {
|
||||
a, sys := newTestApp(t)
|
||||
sys.ReleaseFile = filepath.Join(t.TempDir(), "router-release")
|
||||
dir := t.TempDir()
|
||||
sys.ReleaseFile = filepath.Join(dir, "router-release")
|
||||
sys.RepositoryFile = filepath.Join(dir, "router-update-repository")
|
||||
os.WriteFile(sys.ReleaseFile, []byte(`{"version":"1.0.0","serial":1000}`), 0o600)
|
||||
a.sys = sys
|
||||
orig := startUnit
|
||||
@@ -26,26 +30,24 @@ func TestUpdatesFromGit(t *testing.T) {
|
||||
return do(h, "POST", path, form, cookie).Header().Get("Location")
|
||||
}
|
||||
|
||||
// The repository: an address of a public repository, nothing else.
|
||||
for _, bad := range []string{"git@git.example.org:me/r.git", "https://user:pw@git.example.org/r", "https://git.example.org/r?x=1", "file:///etc", "https://git.example.org/a b"} {
|
||||
if loc := post("/update/repository", url.Values{"repository": {bad}}); !strings.Contains(loc, "Not+saved") {
|
||||
t.Errorf("%q was accepted: %s", bad, loc)
|
||||
}
|
||||
}
|
||||
if loc := post("/update/gitcheck", url.Values{}); !strings.Contains(loc, "Set+the+repository") || len(started) != 0 {
|
||||
// A router without a repository has nothing to look at.
|
||||
if loc := post("/update/gitcheck", url.Values{}); !strings.Contains(loc, "no+repository") || len(started) != 0 {
|
||||
t.Fatalf("a check without a repository: %q %v", loc, started)
|
||||
}
|
||||
if loc := post("/update/gitinstall", url.Values{"tag": {"v1.1.0"}}); !strings.Contains(loc, "Set+the+repository") {
|
||||
if loc := post("/update/gitinstall", url.Values{"tag": {"v1.1.0"}}); !strings.Contains(loc, "no+repository") {
|
||||
t.Fatalf("an install without a repository: %q", loc)
|
||||
}
|
||||
post("/update/repository", url.Values{"repository": {"https://git.example.org/me/nixos-router/"}})
|
||||
s, _ := loadSettings(sys.settingsPath())
|
||||
if got := s.repository(); got != "https://git.example.org/me/nixos-router" {
|
||||
t.Fatalf("repository: %q", got)
|
||||
if rec := do(h, "POST", "/update/repository", url.Values{"csrf": {token}, "repository": {"https://evil.example.org/r"}}, cookie); rec.Code != 404 && rec.Code != 405 {
|
||||
t.Fatalf("the repository can be set from the page: %d", rec.Code)
|
||||
}
|
||||
// It takes effect when updating, so it is no unapplied change.
|
||||
if c, err := loadSettings(sys.confirmedPath()); err == nil && c.repository() != "" {
|
||||
t.Logf("confirmed settings carry it: %q", c.repository())
|
||||
if body := do(h, "GET", "/update", nil, cookie).Body.String(); strings.Contains(body, `name="repository"`) {
|
||||
t.Fatalf("the page has a field for the repository:\n%s", body)
|
||||
}
|
||||
|
||||
// The repository is the installed version's; the page names it.
|
||||
os.WriteFile(sys.RepositoryFile, []byte(testRepository+"\n"), 0o644)
|
||||
if body := do(h, "GET", "/update", nil, cookie).Body.String(); !strings.Contains(body, testRepository) || !strings.Contains(body, "Not looked yet") {
|
||||
t.Fatalf("the page does not say which repository:\n%s", body)
|
||||
}
|
||||
|
||||
// Looking starts the unit; what it found is shown while it is about this repository and version.
|
||||
@@ -53,9 +55,6 @@ func TestUpdatesFromGit(t *testing.T) {
|
||||
if len(started) != 1 || started[0] != "router-update-check.service" {
|
||||
t.Fatalf("started %v", started)
|
||||
}
|
||||
if body := do(h, "GET", "/update", nil, cookie).Body.String(); !strings.Contains(body, "Not looked yet") {
|
||||
t.Fatalf("a result before any look:\n%s", body)
|
||||
}
|
||||
write := func(repo, installed string, newer bool) {
|
||||
os.MkdirAll(sys.updateDir(), 0o755)
|
||||
data, _ := json.Marshal(map[string]any{"repository": repo, "time": "2026-11-26T08:00:00Z", "installed": installed,
|
||||
@@ -66,17 +65,17 @@ func TestUpdatesFromGit(t *testing.T) {
|
||||
if body := do(h, "GET", "/", nil, cookie).Body.String(); strings.Contains(body, "is out") {
|
||||
t.Fatalf("a notice from another repository:\n%s", body)
|
||||
}
|
||||
write("https://git.example.org/me/nixos-router", "0.9.0", true)
|
||||
write(testRepository, "0.9.0", true)
|
||||
if body := do(h, "GET", "/", nil, cookie).Body.String(); strings.Contains(body, "is out") {
|
||||
t.Fatalf("a notice from before the last update:\n%s", body)
|
||||
}
|
||||
write("https://git.example.org/me/nixos-router", "1.0.0", true)
|
||||
write(testRepository, "1.0.0", true)
|
||||
for _, path := range []string{"/", "/update"} {
|
||||
if body := do(h, "GET", path, nil, cookie).Body.String(); !strings.Contains(body, "1.1.0") || (path == "/" && !strings.Contains(body, "nixos-router 1.1.0 is out")) {
|
||||
t.Fatalf("%s lacks the release:\n%s", path, body)
|
||||
}
|
||||
}
|
||||
write("https://git.example.org/me/nixos-router", "1.0.0", false)
|
||||
write(testRepository, "1.0.0", false)
|
||||
if body := do(h, "GET", "/", nil, cookie).Body.String(); strings.Contains(body, "nixos-router 1.1.0 is out") {
|
||||
t.Fatalf("a notice when up to date")
|
||||
}
|
||||
|
||||
@@ -341,7 +341,6 @@ func (a *app) routes() http.Handler {
|
||||
mux.HandleFunc("POST /update/url", a.auth(a.updateSaveURL))
|
||||
mux.HandleFunc("POST /update/check", a.auth(a.updateCheck))
|
||||
mux.HandleFunc("POST /update/install", a.auth(a.updateInstall))
|
||||
mux.HandleFunc("POST /update/repository", a.auth(a.updateSaveRepository))
|
||||
mux.HandleFunc("POST /update/gitcheck", a.auth(a.updateGitCheck))
|
||||
mux.HandleFunc("POST /update/gitinstall", a.auth(a.updateGitInstall))
|
||||
mux.HandleFunc("POST /update/upload", a.updateUpload)
|
||||
|
||||
@@ -21,6 +21,8 @@ type System struct {
|
||||
StateDir string // /var/lib/router: secrets, UI state
|
||||
// ReleaseFile describes the installed nixos-router; /etc/router-release if empty.
|
||||
ReleaseFile string
|
||||
// RepositoryFile holds the git repository releases come from; /etc/router-update-repository if empty.
|
||||
RepositoryFile string
|
||||
// VersionFile holds the running NixOS version; /run/current-system/nixos-version if empty.
|
||||
VersionFile string
|
||||
// LeasesDNSDir is where leases-dns leaves its status; /run/router-leases-dns if empty.
|
||||
|
||||
@@ -40,15 +40,10 @@
|
||||
|
||||
<section>
|
||||
<h2>Releases from git</h2>
|
||||
<p>The router looks once a day at the release tags (<code>vX.Y.Z</code>) of a public repository and says here and on the
|
||||
Overview when a newer one exists. Installing builds it on the router, like a bundle: the settings are kept (and
|
||||
migrated), and the new version must be confirmed afterwards, or the router goes back. The repository is trusted
|
||||
as it is: nothing is signed on this path.</p>
|
||||
<form method="post" action="/update/repository" class="actions">
|
||||
<input type="hidden" name="csrf" value="{{.CSRF}}">
|
||||
<label>Repository <input name="repository" value="{{$u.Repository}}" size="44" placeholder="https://git.example.org/me/nixos-router"></label>
|
||||
<button class="secondary">Save</button>
|
||||
</form>
|
||||
<p>The router looks once a day at the release tags (<code>vX.Y.Z</code>) of <code>{{or $u.Repository "no repository"}}</code>
|
||||
and says here and on the Overview when a newer one exists. Installing builds it on the router, like a bundle: the
|
||||
settings are kept (and migrated), and the new version must be confirmed afterwards, or the router goes back. The
|
||||
repository is trusted as it is: nothing is signed on this path.</p>
|
||||
{{if $u.Repository}}
|
||||
<form method="post" action="/update/gitcheck" class="actions">
|
||||
<input type="hidden" name="csrf" value="{{.CSRF}}">
|
||||
|
||||
@@ -277,7 +277,7 @@ type updateData struct {
|
||||
OfferErr string
|
||||
Pending bool // an update waits for confirmation
|
||||
|
||||
Repository string // router.update.repository
|
||||
Repository string // routerUpdate.repository
|
||||
Git *GitCheck // what the last look at it found
|
||||
GitRun UpdateStatus // the look that is running or ran last
|
||||
}
|
||||
@@ -291,10 +291,8 @@ func (a *app) updateData() updateData {
|
||||
Pending: st.RollbackIn > 0,
|
||||
GitRun: unitRun(gitCheckUnit),
|
||||
}
|
||||
if s, err := loadSettings(a.sys.settingsPath()); err == nil {
|
||||
d.Repository = s.repository()
|
||||
d.Git = a.gitCheck(d.Repository)
|
||||
}
|
||||
d.Repository = a.sys.repository()
|
||||
d.Git = a.gitCheck()
|
||||
d.Progress, d.Step = updateProgress(d.Status.Log)
|
||||
return d
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user