diff --git a/http/management.go b/http/management.go new file mode 100644 index 0000000..027e5ee --- /dev/null +++ b/http/management.go @@ -0,0 +1,84 @@ +package http + +import ( + "net" + + "github.com/dioad/config/configcheck" + + dtls "github.com/dioad/net/tls" +) + +// ManagementConfig is the settings of a management listener a service takes +// under one nested block, usually "management". It serves health, /metrics and, +// with enable-debug, pprof, and has no authentication of its own. Check +// validates it and Operational maps it onto OperationalConfig. A service with +// settings of its own for the listener embeds ManagementConfig squashed in its +// own block type. OperationsConfig is the flat form of the same listener for +// services that have no such block. +type ManagementConfig struct { + // ListenAddress is the host:port of the listener. Empty means no listener, + // which a service that always runs one reports itself. + ListenAddress string `desc:"address of the management listener" mapstructure:"listen-address"` + + // AllowUnauthenticatedNonLoopback confirms that the listener may bind beyond + // loopback with nothing in front of it to authenticate callers. Check + // accepts a non-loopback address without it only when TLSServer requires + // client certificates. + AllowUnauthenticatedNonLoopback bool `desc:"allow the unauthenticated management listener to bind a non-loopback address" mapstructure:"allow-unauthenticated-non-loopback"` + + // EnablePrometheusMetrics serves /metrics. + EnablePrometheusMetrics bool `desc:"serve Prometheus metrics" mapstructure:"enable-prometheus-metrics"` + + // EnableDebug serves pprof. It exposes memory contents, so keep it to + // development. + EnableDebug bool `desc:"expose pprof and verbose diagnostics; development only" mapstructure:"enable-debug"` + + // EnableProxyProtocol accepts a PROXY protocol header, for a listener a + // proxy forwards to. + EnableProxyProtocol bool `desc:"accept a PROXY protocol header" mapstructure:"enable-proxy-protocol"` + + // TLSServer serves the listener over TLS. With client-auth-type + // RequireAndVerifyClientCert and a client-ca-file it also authenticates + // callers, which lets the listener bind beyond loopback unacknowledged. + TLSServer dtls.ServerConfig `desc:"TLS settings of the management listener" mapstructure:"tls-server"` +} + +// Check reports to c a listener that is malformed, one that is not loopback +// with nothing to authenticate its callers, an acknowledgement given with no +// listener to apply to, and a TLS block that does not check out. A listener is +// authenticated when TLSServer requires client certificates. c is positioned at +// the management block. +func (m ManagementConfig) Check(c *configcheck.Collector) { + m.TLSServer.Check(c.Field("tls-server")) + + address := c.Field("listen-address") + if m.ListenAddress == "" { + if m.AllowUnauthenticatedNonLoopback { + c.Field("allow-unauthenticated-non-loopback").Errorf("is set but has no effect without listen-address, remove it") + } + return + } + + configcheck.CheckListenAddress(address, m.ListenAddress) + if _, _, err := net.SplitHostPort(m.ListenAddress); err != nil { + return + } + if configcheck.IsLoopbackAddress(m.ListenAddress) || m.AllowUnauthenticatedNonLoopback || m.TLSServer.RequiresClientCertificate() { + return + } + address.Errorf("%q is reachable from other hosts and the management listener has no authentication; "+ + "bind to loopback, require client certificates (tls-server client-auth-type RequireAndVerifyClientCert with client-ca-file), "+ + "or set allow-unauthenticated-non-loopback if the network in front of it is trusted", m.ListenAddress) +} + +// Operational returns the OperationalConfig for the listener. Set TLSConfig on +// the result from TLSServer, and EnableStatus where the service has a status +// resource. +func (m ManagementConfig) Operational() OperationalConfig { + return OperationalConfig{ + ListenAddress: m.ListenAddress, + EnablePrometheusMetrics: m.EnablePrometheusMetrics, + EnableDebug: m.EnableDebug, + EnableProxyProtocol: m.EnableProxyProtocol, + } +} diff --git a/http/management_test.go b/http/management_test.go new file mode 100644 index 0000000..ef722a5 --- /dev/null +++ b/http/management_test.go @@ -0,0 +1,70 @@ +package http + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/dioad/config/configcheck" + + dtls "github.com/dioad/net/tls" +) + +func TestManagementConfig_Check(t *testing.T) { + t.Parallel() + + mutualTLS := dtls.ServerConfig{ClientAuthType: "RequireAndVerifyClientCert", ClientCAFile: "ca.pem"} + unreachable := func(address string) configcheck.Finding { + return configcheck.Finding{ + Path: "listen-address", + Message: `"` + address + `" is reachable from other hosts and the management listener has no authentication; ` + + "bind to loopback, require client certificates (tls-server client-auth-type RequireAndVerifyClientCert with client-ca-file), " + + "or set allow-unauthenticated-non-loopback if the network in front of it is trusted", + } + } + + tests := []struct { + name string + cfg ManagementConfig + want []configcheck.Finding + }{ + {name: "no listener", cfg: ManagementConfig{}}, + {name: "loopback", cfg: ManagementConfig{ListenAddress: "127.0.0.1:9000"}}, + {name: "acknowledged non-loopback", cfg: ManagementConfig{ListenAddress: ":9000", AllowUnauthenticatedNonLoopback: true}}, + {name: "non-loopback that requires client certificates", cfg: ManagementConfig{ListenAddress: ":9000", TLSServer: mutualTLS}}, + {name: "non-loopback without acknowledgement", cfg: ManagementConfig{ListenAddress: ":9000"}, want: []configcheck.Finding{unreachable(":9000")}}, + { + name: "client certificates requested but not verified against a CA", + cfg: ManagementConfig{ListenAddress: ":9000", TLSServer: dtls.ServerConfig{ClientAuthType: "RequireAndVerifyClientCert"}}, + want: []configcheck.Finding{unreachable(":9000")}, + }, + { + name: "acknowledgement without a listener", + cfg: ManagementConfig{AllowUnauthenticatedNonLoopback: true}, + want: []configcheck.Finding{{Path: "allow-unauthenticated-non-loopback", Message: "is set but has no effect without listen-address, remove it"}}, + }, + { + name: "malformed address is reported once", + cfg: ManagementConfig{ListenAddress: "9000"}, + want: []configcheck.Finding{{Path: "listen-address", Message: `"9000": address 9000: missing port in address`}}, + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + c := configcheck.NewCollector() + tc.cfg.Check(c) + + assert.Equal(t, tc.want, c.Findings()) + }) + } +} + +func TestManagementConfig_OperationalCarriesTheListenerSettings(t *testing.T) { + t.Parallel() + + got := ManagementConfig{ListenAddress: "127.0.0.1:9000", EnablePrometheusMetrics: true, EnableDebug: true, EnableProxyProtocol: true}.Operational() + + assert.Equal(t, OperationalConfig{ListenAddress: "127.0.0.1:9000", EnablePrometheusMetrics: true, EnableDebug: true, EnableProxyProtocol: true}, got) +} diff --git a/tls/check.go b/tls/check.go index e965108..79cea6d 100644 --- a/tls/check.go +++ b/tls/check.go @@ -15,7 +15,7 @@ var clientAuthTypes = []string{ "RequestClientCert", "RequireAnyClientCert", "VerifyClientCertIfGiven", - "RequireAndVerifyClientCert", + clientAuthRequireAndVerify, } // Check reports the problems in a tls-server block to findings, which should be @@ -95,3 +95,10 @@ func (c *ClientConfig) Check(findings *configcheck.Collector) { findings.Errorf("needs both cert and key for a client certificate") } } + +// RequiresClientCertificate reports whether the config makes every client +// present a certificate that verifies against client-ca-file, so that a +// listener using it authenticates its callers. +func (c *ServerConfig) RequiresClientCertificate() bool { + return c.ClientAuthType == clientAuthRequireAndVerify && c.ClientCAFile != "" +} diff --git a/tls/common.go b/tls/common.go index 023a246..f54e464 100644 --- a/tls/common.go +++ b/tls/common.go @@ -17,6 +17,10 @@ func convertTLSVersion(version string) uint16 { } } +// clientAuthRequireAndVerify is the client-auth-type that makes every client +// present a certificate that verifies against client-ca-file. +const clientAuthRequireAndVerify = "RequireAndVerifyClientCert" + func convertClientAuthType(authType string) tls.ClientAuthType { switch authType { case "RequestClientCert": @@ -25,7 +29,7 @@ func convertClientAuthType(authType string) tls.ClientAuthType { return tls.RequireAnyClientCert case "VerifyClientCertIfGiven": return tls.VerifyClientCertIfGiven - case "RequireAndVerifyClientCert": + case clientAuthRequireAndVerify: return tls.RequireAndVerifyClientCert default: return tls.NoClientCert