-
Notifications
You must be signed in to change notification settings - Fork 93
Add Config to FlagConfig struct #332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
peppi-lotta
wants to merge
2
commits into
prometheus:master
Choose a base branch
from
Nordix:peppi-lotta/add-web.Config-to-FlagConfig
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+377
−33
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -64,7 +64,8 @@ type TLSConfig struct { | |||
type FlagConfig struct { | ||||
WebListenAddresses *[]string | ||||
WebSystemdSocket *bool | ||||
WebConfigFile *string | ||||
WebConfigFile *string // Optional: path to the TLS config file. Ether this or TLSConfig must be set. | ||||
WebConfig *Config // Optional: Configuration. If set, it overrides WebConfigFile. | ||||
} | ||||
|
||||
// SetDirectory joins any relative file paths with dir. | ||||
|
@@ -135,6 +136,11 @@ func getTLSConfig(configPath string) (*tls.Config, error) { | |||
if err != nil { | ||||
return nil, err | ||||
} | ||||
|
||||
if err := validateUsers(c); err != nil { | ||||
return nil, err | ||||
} | ||||
|
||||
return ConfigToTLSConfig(&c.TLSConfig) | ||||
} | ||||
|
||||
|
@@ -345,13 +351,28 @@ func parseVsockPort(address string) (uint32, error) { | |||
// WebConfigFile in the FlagConfig, TLS or basic auth could be enabled. | ||||
func Serve(l net.Listener, server *http.Server, flags *FlagConfig, logger *slog.Logger) error { | ||||
logger.Info("Listening on", "address", l.Addr().String()) | ||||
tlsConfigPath := *flags.WebConfigFile | ||||
if tlsConfigPath == "" { | ||||
logger.Info("TLS is disabled.", "http2", false, "address", l.Addr().String()) | ||||
return server.Serve(l) | ||||
var c *Config | ||||
var err error | ||||
|
||||
// WebConfig overrides WebConfigFile. If WebConfig field is not set, then WebConfigFile is used. | ||||
if flags.WebConfig == nil { | ||||
tlsConfigPath := *flags.WebConfigFile | ||||
if tlsConfigPath == "" { | ||||
logger.Info("TLS is disabled.", "http2", false, "address", l.Addr().String()) | ||||
return server.Serve(l) | ||||
} | ||||
|
||||
c, err = getConfig(tlsConfigPath) | ||||
if err != nil { | ||||
return err | ||||
} | ||||
} else { | ||||
// Use the provided config. | ||||
c = flags.WebConfig | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, exporter-toolkit/web/tls_config.go Line 117 in 0603156
|
||||
} | ||||
|
||||
if err := validateUsers(tlsConfigPath); err != nil { | ||||
err = ValidateWebConfig(c) | ||||
if err != nil { | ||||
return err | ||||
} | ||||
|
||||
|
@@ -361,16 +382,11 @@ func Serve(l net.Listener, server *http.Server, flags *FlagConfig, logger *slog. | |||
handler = server.Handler | ||||
} | ||||
|
||||
c, err := getConfig(tlsConfigPath) | ||||
if err != nil { | ||||
return err | ||||
} | ||||
|
||||
server.Handler = &webHandler{ | ||||
tlsConfigPath: tlsConfigPath, | ||||
logger: logger, | ||||
handler: handler, | ||||
cache: newCache(), | ||||
config: c, | ||||
logger: logger, | ||||
handler: handler, | ||||
cache: newCache(), | ||||
} | ||||
|
||||
config, err := ConfigToTLSConfig(&c.TLSConfig) | ||||
|
@@ -395,12 +411,29 @@ func Serve(l net.Listener, server *http.Server, flags *FlagConfig, logger *slog. | |||
// Set the GetConfigForClient method of the HTTPS server so that the config | ||||
// and certs are reloaded on new connections. | ||||
server.TLSConfig.GetConfigForClient = func(*tls.ClientHelloInfo) (*tls.Config, error) { | ||||
config, err := getTLSConfig(tlsConfigPath) | ||||
if err != nil { | ||||
return nil, err | ||||
var tlsConfig *tls.Config | ||||
var err error | ||||
// Config overrides WebConfigFile. If Config fiels is not set, then WebConfigFile is used. | ||||
if flags.WebConfig == nil { | ||||
tlsConfigPath := *flags.WebConfigFile | ||||
|
||||
tlsConfig, err = getTLSConfig(tlsConfigPath) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
} else { | ||||
err = ValidateWebConfig(flags.WebConfig) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
// Use the provided config. | ||||
tlsConfig, err = ConfigToTLSConfig(&flags.WebConfig.TLSConfig) | ||||
if err != nil { | ||||
return nil, err | ||||
} | ||||
} | ||||
config.NextProtos = server.TLSConfig.NextProtos | ||||
return config, nil | ||||
tlsConfig.NextProtos = server.TLSConfig.NextProtos | ||||
return tlsConfig, nil | ||||
} | ||||
return server.ServeTLS(l, "", "") | ||||
} | ||||
|
@@ -410,20 +443,37 @@ func Validate(tlsConfigPath string) error { | |||
if tlsConfigPath == "" { | ||||
return nil | ||||
} | ||||
if err := validateUsers(tlsConfigPath); err != nil { | ||||
return err | ||||
} | ||||
c, err := getConfig(tlsConfigPath) | ||||
if err != nil { | ||||
return err | ||||
} | ||||
if err := validateUsers(c); err != nil { | ||||
return err | ||||
} | ||||
_, err = ConfigToTLSConfig(&c.TLSConfig) | ||||
if err == errNoTLSConfig { | ||||
return nil | ||||
} | ||||
return err | ||||
} | ||||
|
||||
// ValidateWebConfig validates the web configuration, including the TLS config and HTTP headers. | ||||
func ValidateWebConfig(config *Config) error { | ||||
if config == nil { | ||||
return nil | ||||
} | ||||
if err := validateUsers(config); err != nil { | ||||
return err | ||||
} | ||||
if err := validateHeaderConfig(config.HTTPConfig.Header); err != nil { | ||||
return err | ||||
} | ||||
if _, err := ConfigToTLSConfig(&config.TLSConfig); err != nil && err != errNoTLSConfig { | ||||
return err | ||||
} | ||||
return nil | ||||
} | ||||
|
||||
type Cipher uint16 | ||||
|
||||
func (c *Cipher) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we check that u.config is well-defined? The code later loops over
c.HTTPConfig.Header
and accessesc.Users[user]
. getConfig() also sets some defaults here:exporter-toolkit/web/tls_config.go
Line 117 in 0603156