Skip to content
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

Validate rewrite annotation #2734

Merged
merged 4 commits into from
Jun 10, 2022
Merged

Validate rewrite annotation #2734

merged 4 commits into from
Jun 10, 2022

Conversation

haywoodsh
Copy link
Contributor

@haywoodsh haywoodsh commented Jun 3, 2022

Proposed changes

Validate rewrite annotation to ensure (1) "serviceName" and "rewrite" present, (2) serviceName value in "spec", and (3) rewrite paths start with "/" and not include any whitespace character, "{", "}" or "$".

Checklist

Before creating a PR, run through this checklist and mark each as complete.

  • I have read the CONTRIBUTING doc
  • I have added tests that prove my fix is effective or that my feature works
  • I have checked that all unit tests pass after adding my changes
  • I have updated necessary documentation
  • I have rebased my branch onto main
  • I will ensure my PR is targeting the main branch and pulling from my branch from my own fork

@github-actions github-actions bot added documentation Pull requests/issues for documentation enhancement Pull requests for new features/feature enhancements labels Jun 3, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jun 3, 2022

Codecov Report

Merging #2734 (92a5053) into main (33c67d6) will increase coverage by 0.00%.
The diff coverage is 76.00%.

@@           Coverage Diff           @@
##             main    #2734   +/-   ##
=======================================
  Coverage   53.54%   53.55%           
=======================================
  Files          52       52           
  Lines       14764    14782   +18     
=======================================
+ Hits         7905     7916   +11     
- Misses       6597     6601    +4     
- Partials      262      265    +3     
Impacted Files Coverage Δ
internal/configs/parsing_helpers.go 68.50% <40.00%> (-0.74%) ⬇️
internal/k8s/validation.go 99.08% <100.00%> (+0.03%) ⬆️
internal/k8s/configuration.go 95.47% <0.00%> (-0.39%) ⬇️

📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more

@haywoodsh haywoodsh force-pushed the feature/validate-rewrites branch 4 times, most recently from 71ac774 to 74bce18 Compare June 7, 2022 15:41
@haywoodsh haywoodsh marked this pull request as ready for review June 7, 2022 15:58
@@ -333,28 +333,38 @@ func parseRewrites(service string) (serviceName string, rewrite string, err erro
parts := strings.SplitN(strings.TrimSpace(service), " ", 2)

if len(parts) != 2 {
return "", "", fmt.Errorf("Invalid rewrite format: %s", service)
return "", "", fmt.Errorf("'%s' is not valid rewrite format, e.g. 'serviceName=tea-svc rewrite=/'", service)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would change to wording of this to be 'is not a valid'

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}

if !VerifyPath(rwPathParts[1]) {
return "", "", fmt.Errorf("path must start with '/' and must not include any whitespace character, '{', '}' or '$': '%s'", rwPathParts[1])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'P' should be upper case for the word 'path' here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

leaving as it is after discussion

@@ -1951,7 +2141,7 @@ func TestValidateNginxIngressAnnotations(t *testing.T) {
appProtectDosEnabled: false,
internalRoutesEnabled: true,
expectedErrors: []string{
`annotations.nginx.org/rewrites: Invalid value: "not_a_rewrite": must be a semicolon-separated list of rewrites`,
`annotations.nginx.org/rewrites: Invalid value: "not_a_rewrite": 'not_a_rewrite' is not valid rewrite format, e.g. 'serviceName=tea-svc rewrite=/'`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You will need to update this expected error message if you make the update from my first comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Contributor

@jjngx jjngx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!
As refactoring touches files that use fmt.Errorf() it may be worth to consider going a step further and implement sentinel errors. This would allow to leverage error handling and wrapping introduced in Go 1.13:

errors.Is(err, errType)
errors.As(err, &errType)

instead of comparing strings in unit tests.
Just food for thought...

@shaun-nx shaun-nx merged commit 70fa273 into main Jun 10, 2022
@shaun-nx shaun-nx deleted the feature/validate-rewrites branch June 10, 2022 13:57
@ciarams87 ciarams87 added this to the v2.3.0 milestone Jun 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Pull requests/issues for documentation enhancement Pull requests for new features/feature enhancements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants