-
Notifications
You must be signed in to change notification settings - Fork 102
fix: MarshalYAML receivers on TLSVersion and Curve
#288
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,9 +27,13 @@ import ( | |
| "net/http" | ||
| "os" | ||
| "regexp" | ||
| "strings" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/common/config" | ||
| "go.yaml.in/yaml/v2" | ||
| ) | ||
|
|
||
| // Helpers for literal FlagConfig | ||
|
|
@@ -715,3 +719,126 @@ func TestUsers(t *testing.T) { | |
| t.Run(testInputs.Name, testInputs.Test) | ||
| } | ||
| } | ||
|
|
||
| func TestConfigGeneration(t *testing.T) { | ||
|
Member
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. Maybe we can have narrower tests as wel. Additionally, this test could test the round trip as well.
Author
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. Could you please elaborate on narrower tests? What exactly do you see? Cheers!
Member
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. testing marshal/unmarshal on curve directly as well |
||
| // Secrets to be rendered without any masking | ||
| config.MarshalSecretValue = true | ||
|
|
||
| testTables := []struct { | ||
| Name string | ||
| Config Config | ||
| Expected string | ||
| }{ | ||
| { | ||
| Name: "Only basic auth", | ||
| Config: Config{ | ||
| Users: map[string]config.Secret{ | ||
| "admin": config.Secret("$2y$10$X0h1gDsPszWURQaxFh.zoubFi6DXncSjhoQNJgRrnGs7EsimhC7zG"), | ||
| }, | ||
| }, | ||
| Expected: ` | ||
| basic_auth_users: | ||
| admin: $2y$10$X0h1gDsPszWURQaxFh.zoubFi6DXncSjhoQNJgRrnGs7EsimhC7zG`, | ||
| }, | ||
| { | ||
| Name: "Only TLS", | ||
| Config: Config{ | ||
| TLSConfig: TLSConfig{ | ||
| TLSCertPath: "cert.pem", | ||
| TLSKeyPath: "key.pem", | ||
| MinVersion: TLSVersion(tls.VersionTLS12), | ||
| CurvePreferences: []Curve{ | ||
| Curve(tls.CurveP256), | ||
| Curve(tls.CurveP521), | ||
| }, | ||
| CipherSuites: []Cipher{ | ||
| Cipher(tls.TLS_AES_128_GCM_SHA256), | ||
| }, | ||
| ClientAllowedSans: []string{ | ||
| "example.com", | ||
| "example.org", | ||
| }, | ||
| }, | ||
| }, | ||
| Expected: ` | ||
| tls_server_config: | ||
| cert_file: cert.pem | ||
| key_file: key.pem | ||
| cipher_suites: | ||
| - TLS_AES_128_GCM_SHA256 | ||
| curve_preferences: | ||
| - CurveP256 | ||
| - CurveP521 | ||
| min_version: TLS12 | ||
| client_allowed_sans: | ||
| - example.com | ||
| - example.org`, | ||
| }, | ||
| { | ||
| Name: "Only HTTP config", | ||
| Config: Config{ | ||
| HTTPConfig: HTTPConfig{ | ||
| HTTP2: true, | ||
| Header: map[string]string{ | ||
| "X-Custom-Header": "value", | ||
| }, | ||
| }, | ||
| }, | ||
| Expected: ` | ||
| http_server_config: | ||
| http2: true | ||
| headers: | ||
| X-Custom-Header: value`, | ||
| }, | ||
| { | ||
| Name: "Basic auth and TLS", | ||
| Config: Config{ | ||
| Users: map[string]config.Secret{ | ||
| "admin": config.Secret("$2y$10$X0h1gDsPszWURQaxFh.zoubFi6DXncSjhoQNJgRrnGs7EsimhC7zG"), | ||
| }, | ||
| TLSConfig: TLSConfig{ | ||
| TLSCertPath: "cert.pem", | ||
| TLSKeyPath: "key.pem", | ||
| MinVersion: TLSVersion(tls.VersionTLS12), | ||
| CurvePreferences: []Curve{ | ||
| Curve(tls.CurveP256), | ||
| Curve(tls.CurveP521), | ||
| }, | ||
| CipherSuites: []Cipher{ | ||
| Cipher(tls.TLS_AES_128_GCM_SHA256), | ||
| }, | ||
| ClientAllowedSans: []string{ | ||
| "example.com", | ||
| "example.org", | ||
| }, | ||
| }, | ||
| }, | ||
| Expected: ` | ||
| tls_server_config: | ||
| cert_file: cert.pem | ||
| key_file: key.pem | ||
| cipher_suites: | ||
| - TLS_AES_128_GCM_SHA256 | ||
| curve_preferences: | ||
| - CurveP256 | ||
| - CurveP521 | ||
| min_version: TLS12 | ||
| client_allowed_sans: | ||
| - example.com | ||
| - example.org | ||
| basic_auth_users: | ||
| admin: $2y$10$X0h1gDsPszWURQaxFh.zoubFi6DXncSjhoQNJgRrnGs7EsimhC7zG`, | ||
| }, | ||
| } | ||
|
|
||
| for _, test := range testTables { | ||
|
Member
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. Please use t.Fatalf and subtests
Author
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. Ok, will do that! Cheers! |
||
| yamlConfig, err := yaml.Marshal(&test.Config) | ||
| if err != nil { | ||
| t.Error(err) | ||
| } | ||
|
|
||
| if strings.TrimSpace(test.Expected) != strings.TrimSpace(string(yamlConfig)) { | ||
| t.Fatalf("Expected config: %s, got config: %s", test.Expected, string(yamlConfig)) | ||
| } | ||
| } | ||
| } | ||
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.
Please remove all the omitempty and let's have this in another PR where we can discuss which ones are valuable and which one are not wanted.
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.
Well, without
omitempty, the marshalling config structs can produce "invalid" config files which is not desired I assume. For instance, if we do not setmin_versionormax_versionin config struct, withotomitempty, the config file will containmin_version: "0"which is not valid.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.
Yes it depends on the fields, but some of them are really off. I do think we would want to discuss this separately