-
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
Open
Paras Negi (paras-negi-flink)
wants to merge
2
commits into
main
Choose a base branch
from
cf-3931-strict-statement-defaults
base: main
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.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| package flink | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
@@ -26,7 +28,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 +126,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 +146,39 @@ func parseDefaultsAsGenericType[T any](input, label string) (T, error) { | |
| return out, nil | ||
| } | ||
|
|
||
| // decodeStrictJson decodes a single JSON value into out, rejecting unknown | ||
| // fields and trailing data so mis-shaped input surfaces instead of being | ||
| // silently dropped. Decoding into a map is unaffected (a map has no unknown | ||
| // fields). | ||
| func decodeStrictJson(data []byte, out any) error { | ||
| decoder := json.NewDecoder(bytes.NewReader(data)) | ||
| decoder.DisallowUnknownFields() | ||
| if err := decoder.Decode(out); err != nil { | ||
| return err | ||
| } | ||
| if decoder.More() { | ||
| return fmt.Errorf("unexpected trailing data after JSON value") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // decodeStrictYaml is the YAML counterpart of decodeStrictJson; it also rejects | ||
| // unknown fields and any additional documents. | ||
| func decodeStrictYaml(data []byte, out any) error { | ||
| decoder := yaml.NewDecoder(bytes.NewReader(data)) | ||
| decoder.KnownFields(true) | ||
| if err := decoder.Decode(out); err != nil { | ||
| return err | ||
| } | ||
| if err := decoder.Decode(&struct{}{}); err != io.EOF { | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return fmt.Errorf("unexpected additional YAML document") | ||
| } | ||
| return nil | ||
| } | ||
|
Comment on lines
+153
to
+180
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. |
||
|
|
||
| func jsonMarshalHelper(v interface{}, label string) (string, error) { | ||
| data, err := json.Marshal(v) | ||
| if err != nil { | ||
|
|
||
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
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
1 change: 1 addition & 0 deletions
1
test/fixtures/output/flink/environment/create-statement-defaults-invalid.golden
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Error: failed to parse statement defaults: json: unknown field "config-overrides" |
1 change: 1 addition & 0 deletions
1
test/fixtures/output/flink/environment/create-statement-defaults-trailing.golden
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Error: failed to parse statement defaults: unexpected trailing data after JSON value |
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
1 change: 1 addition & 0 deletions
1
test/fixtures/output/flink/environment/update-statement-defaults-invalid.golden
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Error: failed to parse statement defaults: json: unknown field "config-overrides" |
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
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.
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.