From fee8a6a5c438320f978f327584323909cec3f259 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 5 Jun 2022 21:07:28 +0200 Subject: [PATCH] cmd/dockerd: make newAPIServerConfig() more idiomatic Construct the TLSConfig if needed, before constructing and returning the whole config. Signed-off-by: Sebastiaan van Stijn --- cmd/dockerd/daemon.go | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/cmd/dockerd/daemon.go b/cmd/dockerd/daemon.go index 6d1fa3ec63..d11e193227 100644 --- a/cmd/dockerd/daemon.go +++ b/cmd/dockerd/daemon.go @@ -612,32 +612,34 @@ func (cli *DaemonCli) getContainerdDaemonOpts() ([]supervisor.DaemonOpt, error) } func newAPIServerConfig(config *config.Config) (*apiserver.Config, error) { - serverConfig := &apiserver.Config{ - SocketGroup: config.SocketGroup, - Version: dockerversion.Version, - CorsHeaders: config.CorsHeaders, - } - + var tlsConfig *tls.Config if config.TLS != nil && *config.TLS { - tlsOptions := tlsconfig.Options{ + var ( + clientAuth tls.ClientAuthType + err error + ) + if config.TLSVerify == nil || *config.TLSVerify { + // server requires and verifies client's certificate + clientAuth = tls.RequireAndVerifyClientCert + } + tlsConfig, err = tlsconfig.Server(tlsconfig.Options{ CAFile: config.CommonTLSOptions.CAFile, CertFile: config.CommonTLSOptions.CertFile, KeyFile: config.CommonTLSOptions.KeyFile, ExclusiveRootPools: true, - } - - if config.TLSVerify == nil || *config.TLSVerify { - // server requires and verifies client's certificate - tlsOptions.ClientAuth = tls.RequireAndVerifyClientCert - } - tlsConfig, err := tlsconfig.Server(tlsOptions) + ClientAuth: clientAuth, + }) if err != nil { return nil, errors.Wrap(err, "invalid TLS configuration") } - serverConfig.TLSConfig = tlsConfig } - return serverConfig, nil + return &apiserver.Config{ + SocketGroup: config.SocketGroup, + Version: dockerversion.Version, + CorsHeaders: config.CorsHeaders, + TLSConfig: tlsConfig, + }, nil } // checkTLSAuthOK checks basically for an explicitly disabled TLS/TLSVerify