From 53a6275a7990d90495cfbf74beb602105f03ccca Mon Sep 17 00:00:00 2001 From: James Elliott Date: Tue, 3 Jan 2023 17:08:49 +1100 Subject: [PATCH] fix(server): errors not logged (#4682) This fixes a couple errors that were previously not logged as well as ensuring most templates are appropriately parsed via tests. --- internal/commands/root.go | 10 ++++- internal/server/server.go | 2 +- internal/templates/util_test.go | 74 +++++++++++++++++++++++++++++++++ web/.env.production | 14 +++---- web/index.html | 4 +- 5 files changed, 93 insertions(+), 11 deletions(-) diff --git a/internal/commands/root.go b/internal/commands/root.go index 29a1bdc0d..29fdad27a 100644 --- a/internal/commands/root.go +++ b/internal/commands/root.go @@ -122,6 +122,8 @@ func runServices(ctx *CmdCtx) { }() if mainServer, mainListener, err = server.CreateDefaultServer(*ctx.config, ctx.providers); err != nil { + ctx.log.WithError(err).Error("Create Server (main) returned error") + return err } @@ -146,6 +148,8 @@ func runServices(ctx *CmdCtx) { }() if metricsServer, metricsListener, err = server.CreateMetricsServer(ctx.config.Telemetry.Metrics); err != nil { + ctx.log.WithError(err).Error("Create Server (metrics) returned error") + return err } @@ -163,7 +167,11 @@ func runServices(ctx *CmdCtx) { if watcher, err := runServiceFileWatcher(ctx, ctx.config.AuthenticationBackend.File.Path, provider); err != nil { ctx.log.WithError(err).Errorf("Error opening file watcher") } else { - defer watcher.Close() + defer func(watcher *fsnotify.Watcher) { + if err := watcher.Close(); err != nil { + ctx.log.WithError(err).Errorf("Error closing file watcher") + } + }(watcher) } } diff --git a/internal/server/server.go b/internal/server/server.go index 5c1ca950f..319c708bc 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -20,7 +20,7 @@ import ( // CreateDefaultServer Create Authelia's internal webserver with the given configuration and providers. func CreateDefaultServer(config schema.Configuration, providers middlewares.Providers) (server *fasthttp.Server, listener net.Listener, err error) { if err = providers.Templates.LoadTemplatedAssets(assets); err != nil { - return nil, nil, fmt.Errorf("failed to load templated assets") + return nil, nil, fmt.Errorf("failed to load templated assets: %w", err) } server = &fasthttp.Server{ diff --git a/internal/templates/util_test.go b/internal/templates/util_test.go index 1260717fa..9397a91d8 100644 --- a/internal/templates/util_test.go +++ b/internal/templates/util_test.go @@ -1,9 +1,14 @@ package templates import ( + "io/fs" + "os" + "path/filepath" "testing" + "text/template" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func TestIsSecretEnvKey(t *testing.T) { @@ -28,3 +33,72 @@ func TestIsSecretEnvKey(t *testing.T) { }) } } + +func TestParseTemplateDirectories(t *testing.T) { + testCases := []struct { + name, path string + }{ + {"Templates", "./src"}, + {"OpenAPI", "../../api"}, + {"Generators", "../../cmd/authelia-gen/templates"}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + funcMap := FuncMap() + + if tc.name == "Generators" { + funcMap["joinX"] = FuncStringJoinX + } + + var ( + data []byte + ) + + require.NoError(t, filepath.Walk(tc.path, func(path string, info fs.FileInfo, err error) error { + if info.IsDir() { + return nil + } + + name := info.Name() + + if tc.name == "Templates" { + name = filepath.Base(filepath.Dir(path)) + "/" + name + } + + t.Run(name, func(t *testing.T) { + data, err = os.ReadFile(path) + + require.NoError(t, err) + + _, err = template.New(tc.name).Funcs(funcMap).Parse(string(data)) + + require.NoError(t, err) + }) + + return nil + })) + }) + } +} + +func TestParseMiscTemplates(t *testing.T) { + testCases := []struct { + name, path string + }{ + {"ReactIndex", "../../web/index.html"}, + {"ViteEnv", "../../web/.env.production"}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + data, err := os.ReadFile(tc.path) + + require.NoError(t, err) + + _, err = template.New(tc.name).Funcs(FuncMap()).Parse(string(data)) + + require.NoError(t, err) + }) + } +} diff --git a/web/.env.production b/web/.env.production index 2bdd24c5c..39ae4c29e 100644 --- a/web/.env.production +++ b/web/.env.production @@ -1,7 +1,7 @@ -VITE_LOGO_OVERRIDE={{.LogoOverride}} -VITE_PUBLIC_URL={{.Base}} -VITE_DUO_SELF_ENROLLMENT={{.DuoSelfEnrollment}} -VITE_REMEMBER_ME={{.RememberMe}} -VITE_RESET_PASSWORD={{.ResetPassword}} -VITE_RESET_PASSWORD_CUSTOM_URL={{.ResetPasswordCustomURL}} -VITE_THEME={{.Theme}} +VITE_LOGO_OVERRIDE={{ .LogoOverride }} +VITE_PUBLIC_URL={{ .Base }} +VITE_DUO_SELF_ENROLLMENT={{ .DuoSelfEnrollment }} +VITE_REMEMBER_ME={{ .RememberMe }} +VITE_RESET_PASSWORD={{ .ResetPassword }} +VITE_RESET_PASSWORD_CUSTOM_URL={{ .ResetPasswordCustomURL }} +VITE_THEME={{ .Theme }} diff --git a/web/index.html b/web/index.html index 70970870c..fc961d145 100644 --- a/web/index.html +++ b/web/index.html @@ -1,8 +1,8 @@ - - + +