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

proxy: Update trimTrailingEOL function to handle carriage return (\r) #3799

Merged
merged 2 commits into from
Aug 24, 2023

Conversation

praveenkumar
Copy link
Member

It will fix the proxy parse issue in case user update the cert on windows and add blank line at the end of certificate. Unit test is also added for same.

fixes: #3785

Copy link
Contributor

@cfergeau cfergeau left a comment

Choose a reason for hiding this comment

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

Oh good catch!
I don't think this necessarily is caused by users changing the cert file, it could also be tools which would add /r/n to the end of certificate files on Windows.
I'd give more details about what happens exactly. When the cert file ends with \r\n, trimTrailingEOL would only remove \n and the file will end with \r. Then this causes issues in

`
// Replace the carriage return ("\n" or "\r\n") with literal `\n` string
re := regexp.MustCompile(`\r?\n`)
p := fmt.Sprintf(proxyCABundleTemplate, re.ReplaceAllString(proxy.ProxyCACert, `\n`), trustedCAName)
which tries to replace \r\n and \n with \\n but will not do anything with a single \r.
This in turn causes bug #3785

@praveenkumar
Copy link
Member Author

When the cert file ends with \r\n, trimTrailingEOL would only remove \n and the file will end with \r. Then this causes issues in

@cfergeau you mean to say

`
// Replace the carriage return ("\n" or "\r\n") with literal `\n` string
re := regexp.MustCompile(`\r?\n`)
p := fmt.Sprintf(proxyCABundleTemplate, re.ReplaceAllString(proxy.ProxyCACert, `\n`), trustedCAName)
should handle \r case also and replace it \n because that is again going to fail. what we want there should be no new line after -----END CERTIFICATE----- otherwise user-ca-bundle.json is not applied correctly on the cluster so I think this should be handled by trimTrailingEOL itself?

@cfergeau
Copy link
Contributor

should handle \r case also and replace it \n because that is again going to fail.

I was only trying to describe the situation before your changes, imo it would be useful to have in the commit log.
I agree with the way you fixed it, fix trimEOL to fully remove EOL (ie both \r\n and \n). With trimEOL fixed, the regexp will also behave as expected. Without the fix, the string does not match the regexp expectations, and this causes the reported bug.

It will fix the proxy parse issue in case user update the cert on
windows and add blank line at the end of certificate. Unit test is also
added for same.

When the cert file ends with \r\n, trimTrailingEOL would only remove \n
and the file will end with \r. Then this causes issues in regexp which
only replace `\r\n` => `\n` but doesn't perform any action for `\r` and
eventualy turn up this bug.

fixes: crc-org#3785
@cfergeau
Copy link
Contributor

Not sure what happened, but the code was initially correct in #1443, and when moving it to a helper function, \r was lost :-/
After re-reading #1443 (comment) I'm still unsure if removing the EOL at the end of the file is required, or if it's done because it's nicer?

@cfergeau
Copy link
Contributor

There's a second occurrence of the same code:

pkg/crc/cluster/cluster.go:     sshPublicKey := strings.TrimRight(string(sshPublicKeyByte), "\r\n")

and the test code has

test/extended/util/checks.go:   actual = strings.TrimRight(actual, "\n")

Let's make use of the recently added pkg/strings package and add a shared helper. It's trivial, but we got it wrong once, better to implement it once and for all.

Looks like we are using `strings.TrimRight(<string>, "\n\r")` at
multiple locations so better to move this helper to pkg/strings for
better maintainance.
@praveenkumar
Copy link
Member Author

Let's make use of the recently added pkg/strings package and add a shared helper. It's trivial, but we got it wrong once, better to implement it once and for all.

Added another commit to do that.

@openshift-ci
Copy link

openshift-ci bot commented Aug 24, 2023

@praveenkumar: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
ci/prow/e2e-crc e0619ed link true /test e2e-crc
ci/prow/integration-crc 799787a link true /test integration-crc

Full PR test history. Your PR dashboard.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@cfergeau
Copy link
Contributor

/lgtm
/approve

@openshift-ci
Copy link

openshift-ci bot commented Aug 24, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cfergeau

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@praveenkumar praveenkumar merged commit c21cdc3 into crc-org:main Aug 24, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

[BUG] user-ca-bundle.json: json: offset 1425: invalid character '\r' in string literal
2 participants