-
Notifications
You must be signed in to change notification settings - Fork 30
CF-3931 : Reject mis-shaped --statement-defaults instead of silently ignoring it #3397
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: main
Are you sure you want to change the base?
Changes from 1 commit
2391d66
599851a
165a1e9
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package flink | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
|
|
@@ -26,7 +27,7 @@ func (c *command) newEnvironmentCreateCommand() *cobra.Command { | |
|
|
||
| cmd.Flags().String("kubernetes-namespace", "", "Kubernetes namespace to deploy Flink applications to.") | ||
| cmd.Flags().String("defaults", "", "JSON string defining the environment's Flink application defaults, or path to a file to read defaults from (with .yml, .yaml or .json extension).") | ||
| cmd.Flags().String("statement-defaults", "", "JSON string defining the environment's Flink statement defaults, or path to a file to read defaults from (with .yml, .yaml or .json extension).") | ||
| cmd.Flags().String("statement-defaults", "", `JSON string defining the environment's Flink statement defaults, or path to a file to read defaults from (with .yml, .yaml or .json extension). Expected shape: {"detached":{"flinkConfiguration":{...}},"interactive":{"flinkConfiguration":{...}}}.`) | ||
| cmd.Flags().String("compute-pool-defaults", "", "JSON string defining the environment's Flink compute pool defaults, or path to a file to read defaults from (with .yml, .yaml or .json extension).") | ||
|
|
||
| addCmfFlagSet(cmd) | ||
|
|
@@ -124,18 +125,18 @@ func parseDefaultsAsGenericType[T any](input, label string) (T, error) { | |
| if err != nil { | ||
| return out, fmt.Errorf("failed to read %s defaults JSON file: %w", label, err) | ||
| } | ||
| err = json.Unmarshal(data, &out) | ||
| err = decodeStrictJson(data, &out) | ||
|
|
||
| case ".yaml", ".yml": | ||
| data, err = os.ReadFile(input) | ||
| if err != nil { | ||
| return out, fmt.Errorf("failed to read %s defaults YAML file: %w", label, err) | ||
| } | ||
| err = yaml.Unmarshal(data, &out) | ||
| err = decodeStrictYaml(data, &out) | ||
|
|
||
| default: | ||
| // inline JSON string | ||
| err = json.Unmarshal([]byte(input), &out) | ||
| err = decodeStrictJson([]byte(input), &out) | ||
| } | ||
|
|
||
| if err != nil { | ||
|
|
@@ -144,6 +145,23 @@ func parseDefaultsAsGenericType[T any](input, label string) (T, error) { | |
| return out, nil | ||
| } | ||
|
|
||
| // decodeStrictJson decodes JSON into out, rejecting keys that do not map to a | ||
| // known field. This surfaces mis-shaped input (for example the wrong nesting for | ||
| // --statement-defaults) instead of silently dropping it. Decoding into a map is | ||
| // unaffected, since a map has no unknown fields. | ||
| func decodeStrictJson(data []byte, out any) error { | ||
| decoder := json.NewDecoder(bytes.NewReader(data)) | ||
| decoder.DisallowUnknownFields() | ||
| return decoder.Decode(out) | ||
| } | ||
|
|
||
| // decodeStrictYaml is the YAML counterpart of decodeStrictJson. | ||
| func decodeStrictYaml(data []byte, out any) error { | ||
| decoder := yaml.NewDecoder(bytes.NewReader(data)) | ||
| decoder.KnownFields(true) | ||
| return decoder.Decode(out) | ||
| } | ||
|
Comment on lines
+172
to
+199
Contributor
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. The comment makes sense, we can absorb it and make it part of the validation function as I suggest above.
Contributor
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. Handled — the strict decoders now reject trailing content. |
||
|
|
||
| func jsonMarshalHelper(v interface{}, label string) (string, error) { | ||
| data, err := json.Marshal(v) | ||
| if err != nil { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Error: failed to parse statement defaults: json: unknown field "config-overrides" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Error: failed to parse statement defaults: json: unknown field "config-overrides" |
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.
I suggest to:
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.
Otherwise the same
decodeStrictJson()anddecodeStrictYaml()will have to be applied everywhere for on-prem.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.
Done — extracted
parseStatementDefaults.Also dropped the Expected shape string from both flag-help texts.