Require access to Still via method

This commit adds a safeguard to accessing the still from a specific
component by requiring access via the component.GetStill method.
This commit is contained in:
Tom Wiesing 2024-04-08 22:39:32 +02:00
parent 81fa84c244
commit 8235ea9105
No known key found for this signature in database
63 changed files with 288 additions and 197 deletions

View file

@ -78,7 +78,7 @@ func (admin *Admin) Status(ctx context.Context, QuickInformation bool) (target s
func (admin *Admin) Fetch(flags component.FetcherFlags, target *status.Distillery) error {
target.Time = time.Now().UTC()
target.Config = admin.Config
target.Config = component.GetStill(admin).Config
return nil
}

View file

@ -56,9 +56,10 @@ func (admin *Admin) instanceSSH(ctx context.Context) http.Handler {
return ctx, nil, httpx.ErrNotFound
}
config := component.GetStill(admin).Config
ctx.Hostname = ctx.Instance.Domain()
ctx.PanelDomain = admin.Config.HTTP.PanelDomain()
ctx.Port = admin.Config.Listen.SSHPort
ctx.PanelDomain = config.HTTP.PanelDomain()
ctx.Port = config.Listen.SSHPort
keys, err := ctx.Instance.SSH().Keys(r.Context())
if err != nil {

View file

@ -99,11 +99,12 @@ func (control *Cron) Once(ctx context.Context) {
//
// The returned channel is closed once no more cron tasks are active.
func (control *Cron) Start(ctx context.Context, signal <-chan struct{}) <-chan struct{} {
zerolog.Ctx(ctx).Info().Dur("interval", control.Config.CronInterval).Msg("Scheduling Cron() tasks")
interval := component.GetStill(control).Config.CronInterval
zerolog.Ctx(ctx).Info().Dur("interval", interval).Msg("Scheduling Cron() tasks")
// run runs cron tasks with the configured timeout
run := func() {
ctx, done := context.WithTimeout(ctx, control.Config.CronInterval)
ctx, done := context.WithTimeout(ctx, interval)
defer done()
control.Once(ctx)
@ -123,7 +124,7 @@ func (control *Cron) Start(ctx context.Context, signal <-chan struct{}) <-chan s
defer timex.ReleaseTimer(t)
for {
timex.StopTimer(t)
t.Reset(control.Config.CronInterval)
t.Reset(interval)
select {
case <-t.C:

View file

@ -37,7 +37,8 @@ func (h *Handling) interceptor(parent httpx.ErrInterceptor) httpx.ErrInterceptor
pf = func(r *http.Request, err error) {}
}
parent.RenderError = h.Config.HTTP.Debug.Set && h.Config.HTTP.Debug.Value
config := component.GetStill(h).Config
parent.RenderError = config.HTTP.Debug.Set && config.HTTP.Debug.Value
parent.OnFallback = func(r *http.Request, err error) {
pf(r, err)

View file

@ -30,7 +30,7 @@ func (home *Home) Routes() component.Routes {
MatchAllDomains: true,
CSRF: false,
MenuTitle: home.Config.Home.Title,
MenuTitle: component.GetStill(home).Config.Home.Title,
MenuSticky: true,
MenuPriority: component.MenuHome,
}
@ -45,7 +45,7 @@ func (home *Home) HandleRoute(ctx context.Context, route string) (http.Handler,
dflt.Fallback = home.publicHandler(ctx)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
slug, ok := home.Config.HTTP.NormSlugFromHost(r.Host)
slug, ok := component.GetStill(home).Config.HTTP.NormSlugFromHost(r.Host)
switch {
case !ok:
http.NotFound(w, r)

View file

@ -48,14 +48,14 @@ type publicContext struct {
const logoHTML = template.HTML(`<img src="/logo.svg" alt="WissKI Distillery Logo" class="biglogo">`)
func (home *Home) publicHandler(ctx context.Context) http.Handler {
title := home.Config.Home.Title
config := component.GetStill(home).Config.Home
tpl := publicTemplate.Prepare(
home.dependencies.Templating,
// set title and menu item
templating.Title(title),
templating.Title(config.Title),
templating.Crumbs(
component.MenuItem{Title: title, Path: "/"},
component.MenuItem{Title: config.Title, Path: "/"},
),
)
@ -73,7 +73,7 @@ func (home *Home) publicHandler(ctx context.Context) http.Handler {
// prepare about
pc.aboutContext.Logo = logoHTML
pc.aboutContext.Instances = home.dependencies.ListInstances.Infos()
pc.aboutContext.SelfRedirect = home.Config.Home.SelfRedirect.String()
pc.aboutContext.SelfRedirect = config.SelfRedirect.String()
// render the about template
@ -88,7 +88,7 @@ func (home *Home) publicHandler(ctx context.Context) http.Handler {
pc.ListEnabled = home.dependencies.ListInstances.ShouldShowList(r)
// title of the list
pc.ListTitle = home.Config.Home.List.Title
pc.ListTitle = config.List.Title
return
})

View file

@ -6,6 +6,8 @@ import (
"net/http"
"os"
"strings"
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
)
func (home *Home) loadRedirect(ctx context.Context) (redirect Redirect, err error) {
@ -19,7 +21,7 @@ func (home *Home) loadRedirect(ctx context.Context) (redirect Redirect, err erro
redirect.Permanent = false
// load the overrides file
overrides, err := os.Open(home.Config.Paths.OverridesJSON)
overrides, err := os.Open(component.GetStill(home).Config.Paths.OverridesJSON)
if err != nil {
return redirect, err
}

View file

@ -41,7 +41,7 @@ type APISystem struct {
func (a *API) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
return &api.Handler[[]APISystem]{
Config: a.Config,
Config: component.GetStill(a).Config,
Auth: a.dependencies.Auth,
Methods: []string{"GET"},

View file

@ -35,8 +35,9 @@ func (li *ListInstances) Infos() []status.WissKI {
// ShouldShowList determines if a list should be shown for the given request
func (li *ListInstances) ShouldShowList(r *http.Request) bool {
allowPrivate := li.Config.Home.List.Private.Value
allowPublic := li.Config.Home.List.Public.Value
config := component.GetStill(li).Config.Home.List
allowPrivate := config.Private.Value
allowPublic := config.Public.Value
if allowPrivate == allowPublic {
return allowPrivate

View file

@ -30,7 +30,7 @@ func (api *API) Routes() component.Routes {
func (a *API) HandleRoute(ctx context.Context, path string) (http.Handler, error) {
return &api.Handler[[]Item]{
Config: a.Config,
Config: component.GetStill(a).Config,
Auth: a.dependencies.Auth,
Methods: []string{"GET"},

View file

@ -53,7 +53,7 @@ func (server *Server) Server(ctx context.Context, progress io.Writer) (public ht
}()
// determine if we are on a slug from a host
slug, ok := server.Config.HTTP.NormSlugFromHost(r.Host)
slug, ok := component.GetStill(server).Config.HTTP.NormSlugFromHost(r.Host)
rctx := component.WithRouteContext(r.Context(), component.RouteContext{
DefaultDomain: slug == "" && ok,
@ -126,13 +126,15 @@ func (server *Server) Server(ctx context.Context, progress io.Writer) (public ht
// CSRF returns a CSRF handler for the given function
func (server *Server) csrf() func(http.Handler) http.Handler {
config := component.GetStill(server).Config
var opts []csrf.Option
opts = append(opts, csrf.Secure(server.Config.HTTP.HTTPSEnabled()))
opts = append(opts, csrf.Secure(config.HTTP.HTTPSEnabled()))
opts = append(opts, csrf.SameSite(csrf.SameSiteStrictMode))
opts = append(opts, csrf.Path("/"))
opts = append(opts, csrf.CookieName(CSRFCookie))
opts = append(opts, csrf.FieldName(CSRFCookieField))
return csrf.Protect(server.Config.CSRFSecret(), opts...)
return csrf.Protect(config.CSRFSecret(), opts...)
}
// WithCSP adds a Content-Security-Policy header to every response

View file

@ -11,28 +11,30 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/dis/component"
)
func (control Server) Path() string {
return filepath.Join(control.Still.Config.Paths.Root, "core", "dis")
func (server *Server) Path() string {
return filepath.Join(component.GetStill(server).Config.Paths.Root, "core", "dis")
}
//go:embed all:server
var resources embed.FS
func (server *Server) Stack() component.StackWithResources {
config := component.GetStill(server).Config
return component.MakeStack(server, component.StackWithResources{
Resources: resources,
ContextPath: "server",
EnvContext: map[string]string{
"DOCKER_NETWORK_NAME": server.Config.Docker.Network(),
"HOST_RULE": server.Config.HTTP.PanelHostRule(),
"HTTPS_ENABLED": server.Config.HTTP.HTTPSEnabledEnv(),
"DOCKER_NETWORK_NAME": config.Docker.Network(),
"HOST_RULE": config.HTTP.PanelHostRule(),
"HTTPS_ENABLED": config.HTTP.HTTPSEnabledEnv(),
"CONFIG_PATH": server.Config.ConfigPath,
"DEPLOY_ROOT": server.Config.Paths.Root,
"CONFIG_PATH": config.ConfigPath,
"DEPLOY_ROOT": config.Paths.Root,
"SELF_OVERRIDES_FILE": server.Config.Paths.OverridesJSON,
"SELF_RESOLVER_BLOCK_FILE": server.Config.Paths.ResolverBlocks,
"SELF_OVERRIDES_FILE": config.Paths.OverridesJSON,
"SELF_RESOLVER_BLOCK_FILE": config.Paths.ResolverBlocks,
"CUSTOM_ASSETS_PATH": server.dependencies.Templating.CustomAssetsPath(),
},
@ -48,6 +50,6 @@ func (server *Server) Trigger(ctx context.Context) error {
func (server *Server) Context(parent component.InstallationContext) component.InstallationContext {
return component.InstallationContext{
bootstrap.Executable: server.Config.Paths.CurrentExecutable(), // TODO: Does this make sense?
bootstrap.Executable: component.GetStill(server).Config.Paths.CurrentExecutable(), // TODO: Does this make sense?
}
}

View file

@ -8,7 +8,7 @@ import (
// CustomAssetsPath is the path custom assets are stored at
func (tpl *Templating) CustomAssetsPath() string {
return filepath.Join(tpl.Config.Paths.Root, "core", "assets")
return filepath.Join(component.GetStill(tpl).Config.Paths.Root, "core", "assets")
}
func (tpl *Templating) CustomAssetPath(name string) string {