[management] Create HttpConfig before applying TLS flag overrides (#8168)

With the embedded IdP, management.json can leave out the HttpConfig
section, and ApplyEmbeddedIdPConfig creates it later. Command-line
overrides run before that, so --letsencrypt-domain, or --cert-file with
--cert-key, dereferenced a nil HttpConfig and management crashed at
startup.

Create the section only when one of those flags writes to it, so a
config without TLS flags keeps HttpConfig unset.
This commit is contained in:
Nicolas Frati
2026-10-12 13:44:30 +02:00
committed by GitHub
parent 57722d3235
commit fe7841721a
2 changed files with 62 additions and 3 deletions
+49
View File
@@ -80,6 +80,55 @@ func TestApplyEmbeddedIdPConfigRequiresSingleAccountDomain(t *testing.T) {
require.ErrorContains(t, ApplyEmbeddedIdPConfig(context.Background(), cfg), "embedded IdP requires single account mode")
}
// A config without an HttpConfig section is valid with the embedded IdP, so the
// TLS flags must not assume the section exists.
func TestApplyCommandLineOverridesWithoutHttpConfig(t *testing.T) {
previousDomain, previousCertFile, previousCertKey := mgmtLetsencryptDomain, certFile, certKey
t.Cleanup(func() {
mgmtLetsencryptDomain, certFile, certKey = previousDomain, previousCertFile, previousCertKey
})
tests := []struct {
name string
domain string
certFile string
certKey string
want nbconfig.HttpServerConfig
}{
{
name: "letsencrypt domain",
domain: "mgmt.example.com",
want: nbconfig.HttpServerConfig{LetsEncryptDomain: "mgmt.example.com"},
},
{
name: "certificate files",
certFile: "/etc/netbird/tls/tls.crt",
certKey: "/etc/netbird/tls/tls.key",
want: nbconfig.HttpServerConfig{CertFile: "/etc/netbird/tls/tls.crt", CertKey: "/etc/netbird/tls/tls.key"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mgmtLetsencryptDomain, certFile, certKey = tt.domain, tt.certFile, tt.certKey
cfg := &nbconfig.Config{}
ApplyCommandLineOverrides(cfg)
require.NotNil(t, cfg.HttpConfig, "HttpConfig should be created for the TLS flags")
assert.Equal(t, tt.want, *cfg.HttpConfig, "HttpConfig should carry only the flag values")
})
}
t.Run("no TLS flags", func(t *testing.T) {
mgmtLetsencryptDomain, certFile, certKey = "", "", ""
cfg := &nbconfig.Config{}
ApplyCommandLineOverrides(cfg)
assert.Nil(t, cfg.HttpConfig, "HttpConfig should stay unset so the embedded IdP does not warn about it")
})
}
func createConfig(config string) (string, error) {
tmpfile, err := os.CreateTemp("", "config.json")
if err != nil {