Files
James ElliottandAmir Zarrinkafsh eced48ac4a feat(configuration): template delimiters (#11907)
Adds the `--config.filters.template.delimiter.left` and
`--config.filters.template.delimiter.right` options which customize the
delimiters used by the template file filter, allowing configuration
files to remain valid for their own file type so they can be linted.
This does not address validation against a schema.

In addition the `--config.experimental.filters` flag is renamed to
`--config.filters`, and the deprecated `expand-env` filter is removed in
favor of the `template` filter.

---------

Signed-off-by: James Elliott <james-d-elliott@users.noreply.github.com>
Co-authored-by: Amir Zarrinkafsh <3339418+nightah@users.noreply.github.com>
2026-09-27 11:15:40 +00:00

142 lines
3.4 KiB
Go

// SPDX-FileCopyrightText: 2026 Authelia
//
// SPDX-License-Identifier: Apache-2.0
package embed
import (
"context"
"fmt"
"net"
"github.com/sirupsen/logrus"
"github.com/authelia/authelia/v4/experimental/embed/provider"
"github.com/authelia/authelia/v4/internal/clock"
"github.com/authelia/authelia/v4/internal/configuration/schema"
"github.com/authelia/authelia/v4/internal/middlewares"
"github.com/authelia/authelia/v4/internal/random"
)
// New creates a new instance of the embedded Authelia context. This can later be used with embed.ServiceRunAll. The
// valuesFiles are passed to NewNamedConfigFileFilters and populate .Values in the 'template' filter.
func New(paths []string, filterNames []string, valuesFiles []string) (ctx Context, val *schema.StructValidator, err error) {
if len(paths) == 0 {
return nil, nil, fmt.Errorf("no paths provided")
}
filters, err := NewNamedConfigFileFilters(valuesFiles, "", "", filterNames...)
if err != nil {
return nil, nil, err
}
keys, config, val, err := NewConfiguration(paths, filters)
if err != nil {
return nil, val, err
}
ValidateConfigurationAndKeys(config, keys, val)
if val.HasErrors() {
return nil, val, fmt.Errorf("configuration validation errors")
}
providers, warns, errs := provider.New(config, nil)
for _, warn := range warns {
val.PushWarning(warn)
}
for _, err = range errs {
val.Push(err)
}
if val.HasErrors() {
return nil, val, fmt.Errorf("provider validation errors")
}
ctx = &ctxEmbed{
Configuration: (*Configuration)(config),
ConfigurationPaths: paths,
Logger: logrus.NewEntry(logrus.StandardLogger()),
Providers: Providers(providers),
}
return ctx, nil, err
}
// Context is an interface used in various areas of Authelia to simplify access to important elements like the
// configuration, providers, and logger.
type Context interface {
GetLogger() *logrus.Entry
GetProviders() middlewares.Providers
GetConfiguration() *schema.Configuration
GetClock() (clock clock.Provider)
GetRandom() (random random.Provider)
RemoteIP() net.IP
context.Context
}
// ServiceContext is an interface used in various areas of Authelia to simplify access to important elements like the
// configuration, providers, and logger.
type ServiceContext interface {
GetLogger() *logrus.Entry
GetProviders() middlewares.Providers
GetConfiguration() *schema.Configuration
GetConfigurationPaths() (paths []string)
context.Context
}
type ctxEmbed struct {
Configuration *Configuration
ConfigurationPaths []string
Providers Providers
Logger *logrus.Entry
context.Context
}
func (c *ctxEmbed) GetConfiguration() *schema.Configuration {
return c.Configuration.ToInternal()
}
func (c *ctxEmbed) GetConfigurationPaths() (paths []string) {
return c.ConfigurationPaths
}
func (c *ctxEmbed) GetProviders() middlewares.Providers {
return c.Providers.ToInternal()
}
func (c *ctxEmbed) GetLogger() *logrus.Entry {
return c.Logger
}
func (c *ctxEmbed) GetClock() (provider clock.Provider) {
if c.Providers.Clock == nil {
c.Providers.Clock = clock.New()
}
return c.Providers.Clock
}
func (c *ctxEmbed) GetRandom() (provider random.Provider) {
if c.Providers.Random == nil {
c.Providers.Random = random.New()
}
return c.Providers.Random
}
func (c *ctxEmbed) RemoteIP() net.IP {
return nil
}
var (
_ middlewares.Context = (*ctxEmbed)(nil)
_ Context = (*ctxEmbed)(nil)
)