middleware

package
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 16, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package middleware holds this application's own Echo middleware, request logging with a propagated request id, the active-organization context, and the guard that keeps config-managed stacks read-only in the UI. Generic concerns (auth, CSRF, flash) come from hamr/pkg/middleware instead.

Index

Constants

View Source
const (
	CtxOrgs = "orgs"     // []repo.Org, the user's organizations
	CtxOrg  = "org"      // repo.Org, the active one
	CtxRole = "org_role" // string, the user's role in the active org
	// CtxWriteHere is set by the access checks below: the user's write right in
	// the org that owns the resource this request addresses, which is not
	// necessarily the active one.
	CtxWriteHere = "org_write_here"
)

Context keys set by OrgContext for every site request.

View Source
const HeaderRequestID = "X-Request-ID"

HeaderRequestID is the HTTP header used to propagate request IDs.

View Source
const OrgCookie = "stackr_org"

OrgCookie stores the active organization's id.

View Source
const SetupCookie = "stackr_setup"

SetupCookie marks a GitHub connector round trip that started in the onboarding wizard, so the callback can return there instead of to settings. It lives here because both ends of that trip are in different handler packages.

Variables

This section is empty.

Functions

func ActiveOrg

func ActiveOrg(c echo.Context) (repo.Org, bool)

ActiveOrg returns the active organization, ok=false outside OrgContext.

func CanWrite

func CanWrite(c echo.Context) bool

CanWrite reports content-write rights on the active org.

A server admin always may. CanWriteOrg has always said so; this said the opposite, and the gap showed on the server-admin screens, which are not org-addressed at all: on a fresh install nobody has an org yet, so the active role is "" and every POST under /servers, add a node, drain one, save a volume, came back "read-only access" to the one account that owns the machine.

func CanWriteHere

func CanWriteHere(c echo.Context) bool

CanWriteHere answers "may this user write to the thing this request is about". It reads the right the access check already resolved for the resource's own org; outside one it falls back to the active org, which is the best a page with no resource can do.

Templates used to ask CanWrite directly and so offered upload and delete buttons to a viewer whose cookie pointed at an org they can write.

func CanWriteOrg

func CanWriteOrg(c echo.Context, store repo.Store, orgID string) bool

CanWriteOrg reports content-write rights in one specific org.

CanWrite answers for the *active* org (the one the cookie selects) which is the right question for the page the user is looking at and the wrong one for a resource addressed by id. Someone who owns org A and only views org B carries role "owner" while acting on B's tiles.

func CurrentUser

func CurrentUser(c echo.Context) *repo.User

CurrentUser pulls the authenticated user loaded by BrowserAuth (nil when logged out).

func InOrg

func InOrg(c echo.Context, orgID string) bool

InOrg reports whether the user belongs to (or, for admins, may act on) the org, the tenancy check for id-addressed resources.

func IsAdmin

func IsAdmin(c echo.Context) bool

IsAdmin reports whether the request's user is a server admin.

func IsOwner

func IsOwner(c echo.Context) bool

IsOwner reports owner rights on the active org (admins included).

func Logging

func Logging() echo.MiddlewareFunc

Logging generates or propagates a request ID, attaches a structured logger to the request context, and logs request completion.

func ManagedErr

func ManagedErr(s *repo.Stack) *echo.HTTPError

ManagedErr is the 409 returned when a structural write is blocked because the config file owns the stack, a config plan would revert it (the silent-drift bug). Secrets, certs and provisioning are not structural and don't use this.

func OrgContext

func OrgContext(store repo.Store) echo.MiddlewareFunc

OrgContext resolves the user's organizations (admins see all), the active one (cookie, else first), and the user's role in it.

func OrgRole

func OrgRole(c echo.Context) string

OrgRole returns the user's role in the active org ("" outside OrgContext).

func Orgs

func Orgs(c echo.Context) []repo.Org

Orgs returns the user's organizations loaded by OrgContext.

func ReadOnlyGuard

func ReadOnlyGuard() echo.MiddlewareFunc

ReadOnlyGuard blocks mutating requests from viewers. Session actions that belong to the user (logout, org switch) stay allowed.

func RequireOrgAccess

func RequireOrgAccess(c echo.Context, orgID string) error

RequireOrgAccess is InOrg as an error: 404 (not 403) so resource ids don't leak across tenants. It is also where an unfinished org stops answering: every org- and stack-addressed route in the panel comes through here, which the settings route group it used to be gated by never could.

Setup state is read off the list OrgContext already loaded (both queries select the whole row), so the gate costs no query. An org missing from that list means the load failed, which only an admin survives, and the handler behind this is about to fail on the same database.

func RequireOrgSetup

func RequireOrgSetup(c echo.Context, o repo.Org) error

RequireOrgSetup refuses a request addressed at an org whose wizard is still open. Called from the access checks every org- and stack-addressed route already goes through, so gating is one condition rather than a route group the id-keyed half of the route table could never have joined.

func RequireOrgWrite

func RequireOrgWrite(c echo.Context, store repo.Store, orgID string) error

RequireOrgWrite is membership plus write rights in that same org, for org-addressed mutations.

func RequireSetupDone

func RequireSetupDone(store repo.Store) echo.MiddlewareFunc

RequireSetupDone closes an org's settings until its owner has finished the onboarding wizard. Without it the wizard is a suggestion: every step links to a settings tab, so one click ejects you from the flow and the steps behind you are never marked done.

The owner goes to the wizard's summary rather than to a guessed "first unfinished" step, every step is skippable, so guessing would drop people back onto steps they skipped on purpose. A member gets SetupPending, which the web renders as the holding page, so settings and the canvas give them the same answer; someone who is in neither still gets not-found.

This group is no longer the only gate: RequireOrgSetup below runs from the access checks, so the canvas, the plans pages and every stack under the org are closed too (plan 29). It stays because the settings routes are a group and a group is the cheapest place to stop them.

func RequireStackAccess

func RequireStackAccess(c echo.Context, store repo.Store, stackID string) error

RequireStackAccess verifies a stack (by id) belongs to one of the user's orgs, the tenancy check for tile-, env-, and deployment-addressed routes.

On a mutating request it also checks write rights in *that stack's* org. ReadOnlyGuard cannot: it only knows the active org, so a viewer in the org that owns the tile passes it whenever their cookie points somewhere they can write. That gap reaches every id-addressed write, volume files, table rows, deploys, so the check belongs here, where all of them already come through. Admins used to short-circuit before the stack was even loaded. They no longer can: the setup gate is keyed on the stack's org, so the org has to be known first. RequireOrgWrite waves an admin through everything else it checks, including the write right this used to record by hand.

func RequireUnmanaged

func RequireUnmanaged(c echo.Context, store repo.Store, stackID string) error

RequireUnmanaged blocks a structural write when the config file owns the stack. It fails closed: a lookup error blocks the write rather than letting it through, since "can't tell" must not become "allowed" on a locked stack.

func SecureCookie

func SecureCookie(c echo.Context) bool

SecureCookie follows the request scheme for non-session browser cookies. TLS terminates at Traefik in production, so X-Forwarded-Proto is the usual signal; direct TLS remains useful in tests and alternate deployments.

func Theme

func Theme(ctx context.Context) string

Theme returns the theme stored by ThemeContext ("" when unset).

func ThemeContext

func ThemeContext() echo.MiddlewareFunc

ThemeContext copies the signed-in user's theme onto the request context.

Components normally read the theme off the echo context, but the error page cannot: hamr renders it through hamrmw.ErrorPages, whose ErrorPage signature is (code, message) with no echo.Context in it. So every error page fell back to prefers-color-scheme and served the OS palette to an account that had pinned the other one. respond.HTML renders with c.Request().Context(), which is the one thing the component does get.

Must be registered after the auth loader, or there is no user to read.

Types

type SetupPending

type SetupPending struct {
	OrgID string
	Slug  string
	Name  string
}

SetupPending is what an access check returns when the org that owns the resource has not been through the onboarding wizard. It is an error and not a redirect written on the spot for two reasons: the checks it comes out of return errors, and the web and the API answer it differently, the web sends the owner back to the wizard, the API refuses with a 409, because a 303 to an HTML page is a parse error to the CLI.

func (SetupPending) Error

func (e SetupPending) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL