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.
pull/4676/head^2
James Elliott 2023-01-03 17:08:49 +11:00 committed by GitHub
parent 1c3219e93f
commit 53a6275a79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 93 additions and 11 deletions

View File

@ -122,6 +122,8 @@ func runServices(ctx *CmdCtx) {
}() }()
if mainServer, mainListener, err = server.CreateDefaultServer(*ctx.config, ctx.providers); err != nil { if mainServer, mainListener, err = server.CreateDefaultServer(*ctx.config, ctx.providers); err != nil {
ctx.log.WithError(err).Error("Create Server (main) returned error")
return err return err
} }
@ -146,6 +148,8 @@ func runServices(ctx *CmdCtx) {
}() }()
if metricsServer, metricsListener, err = server.CreateMetricsServer(ctx.config.Telemetry.Metrics); err != nil { if metricsServer, metricsListener, err = server.CreateMetricsServer(ctx.config.Telemetry.Metrics); err != nil {
ctx.log.WithError(err).Error("Create Server (metrics) returned error")
return err return err
} }
@ -163,7 +167,11 @@ func runServices(ctx *CmdCtx) {
if watcher, err := runServiceFileWatcher(ctx, ctx.config.AuthenticationBackend.File.Path, provider); err != nil { if watcher, err := runServiceFileWatcher(ctx, ctx.config.AuthenticationBackend.File.Path, provider); err != nil {
ctx.log.WithError(err).Errorf("Error opening file watcher") ctx.log.WithError(err).Errorf("Error opening file watcher")
} else { } 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)
} }
} }

View File

@ -20,7 +20,7 @@ import (
// CreateDefaultServer Create Authelia's internal webserver with the given configuration and providers. // 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) { func CreateDefaultServer(config schema.Configuration, providers middlewares.Providers) (server *fasthttp.Server, listener net.Listener, err error) {
if err = providers.Templates.LoadTemplatedAssets(assets); err != nil { 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{ server = &fasthttp.Server{

View File

@ -1,9 +1,14 @@
package templates package templates
import ( import (
"io/fs"
"os"
"path/filepath"
"testing" "testing"
"text/template"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestIsSecretEnvKey(t *testing.T) { 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)
})
}
}

View File

@ -1,7 +1,7 @@
VITE_LOGO_OVERRIDE={{.LogoOverride}} VITE_LOGO_OVERRIDE={{ .LogoOverride }}
VITE_PUBLIC_URL={{.Base}} VITE_PUBLIC_URL={{ .Base }}
VITE_DUO_SELF_ENROLLMENT={{.DuoSelfEnrollment}} VITE_DUO_SELF_ENROLLMENT={{ .DuoSelfEnrollment }}
VITE_REMEMBER_ME={{.RememberMe}} VITE_REMEMBER_ME={{ .RememberMe }}
VITE_RESET_PASSWORD={{.ResetPassword}} VITE_RESET_PASSWORD={{ .ResetPassword }}
VITE_RESET_PASSWORD_CUSTOM_URL={{.ResetPasswordCustomURL}} VITE_RESET_PASSWORD_CUSTOM_URL={{ .ResetPasswordCustomURL }}
VITE_THEME={{.Theme}} VITE_THEME={{ .Theme }}

View File

@ -1,8 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<base href="{{.BaseURL}}" /> <base href="{{ .BaseURL }}" />
<meta property="csp-nonce" content="{{.CSPNonce}}" /> <meta property="csp-nonce" content="{{ .CSPNonce }}" />
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" /> <meta name="theme-color" content="#000000" />