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

fix: Correctly escape consecutive commas in cleanSetParameters #11

Merged
merged 7 commits into from
Sep 26, 2024

Conversation

eogns47
Copy link

@eogns47 eogns47 commented Sep 21, 2024

related issue #9 (1 problem / 2 problems)

  • AS-IS: Consecutive commas are not properly escaped in helm template parameters.

  • TO-BE: Consecutive commas will be correctly escaped to ensure they are handled appropriately in the helm template parameters.

  • REASON: Properly escaping consecutive commas is essential to avoid issues with parameter parsing. This change improves the handling of parameters and aligns with the expected behavior when using helm, ensuring that parameters with multiple commas are treated correctly.

  • Changes: Added test code and fixed errors.

Checklist:

  • Either (a) I've created an enhancement proposal and discussed it with the community, (b) this is a bug fix, or (c) this does not need to be in the release notes.
  • The title of the PR states what changed and the related issues number (used for the release note).
  • The title of the PR conforms to the Toolchain Guide
  • I've included "Closes [ISSUE #]" or "Fixes [ISSUE #]" in the description to automatically close the associated issue.
  • I've updated both the CLI and UI to expose my feature, or I plan to submit a second PR with them.
  • Does this PR require documentation updates?
  • I've updated documentation as required by this PR.
  • I have signed off all my commits as required by DCO
  • I have written unit and/or e2e tests for my change. PRs without these are unlikely to be merged.
  • My build is green (troubleshooting builds).
  • My new feature complies with the feature status guidelines.
  • I have added a brief description of why this PR is necessary and/or what this PR solves.
  • Optional. My organization is added to USERS.md.
  • Optional. For bug fixes, I've indicated what older releases this fix should be cherry-picked into (this may or may not happen depending on risk/complexity).

util/helm/cmd.go Outdated
@@ -352,6 +352,7 @@ func cleanSetParameters(val string) string {
if strings.HasPrefix(val, `{`) && strings.HasSuffix(val, `}`) {
return val
}
re := regexp.MustCompile(`,`)
Copy link
Member

@daengdaengLee daengdaengLee Sep 21, 2024

Choose a reason for hiding this comment

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

여기가 아니라 위에 기존 re 를 수정해야 할 것 같아요. (346 번 라인)
이렇게 하면 모든 , 가 잡히는데 기존 re 에서 의도한 것처럼 /, 인 경우는 빼고 잡아내야 할 것 같습니다.

기존 코드의 경우

앞에 `/` 가 아닌 다른 문자가 있는 `,` 문자

를 잡아내고 있어요.

문제가 되는 부분은

  • 앞에 아무 문자도 없는 경우일 때
  • ,, 처럼 , 가 연속 2번 나올 때 뒤에 나오는 ,

이 두 경우가 기존 정규식으로 안 잡혀요.

기존 의도는 유지한 채로 저 두 경우까지 잡아내는 방법을 고민해보면 좋을 것 같습니다.
@mirageoasis 님도 같이 봐주세요~

Copy link
Author

Choose a reason for hiding this comment

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

@daengdaengLee 넵 다시 확인하겠습니다!

Choose a reason for hiding this comment

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

근데 왜 slash가 아닌 다른 문자가 있는 ','를 잡아야하나요?
파일 경로 때문에 그런건가요?

Copy link
Member

Choose a reason for hiding this comment

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

/, 는 이미 이스케이프 처리 된 상태라서 저것까지 잡으면 /,//, 가 되어버려서요.
이미 이스케이프 처리된 건 건너뛰려고 하는 것 같아요.

Choose a reason for hiding this comment

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

쉽지 않군요 ^^ ㅋㅋㅋㅋㅋ...

Choose a reason for hiding this comment

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

unescapedRe := regexp.MustCompile(`([^\\]),`)
return unescapedRe.ReplaceAllString(val, `$1\\,`)

이런식은 어떤가요?

@daengdaengLee
Copy link
Member

@eogns47, @mirageoasis 님 안녕하세요.

  1. 기존 util/helm/helm_test.go 파일에 TestHelmArgCleaner 테스트에서 cleanSetParameters 함수를 테스트하고 있어서요. 새로 추가하신 util/helm/cmd_test.go 파일의 TestCleanSetParameters 는 삭제하고 기존 TestHelmArgCleaner 에 필요한 테스트 케이스를 추가하는 게 좋을 것 같아요.
  2. TestHelmArgCleaner 에 제가 생각하는 이 이슈에서 처리해야 할 엣지 케이스를 추가했습니다. 해당 테스트 통과 가능하도록 cleanSetParameters 를 구현해보면 좋을 것 같습니다.

감사합니다.

@eogns47
Copy link
Author

eogns47 commented Sep 22, 2024

@daengdaengLee 넵 그렇게 해보겠습니다. 감사합니다!

@eogns47
Copy link
Author

eogns47 commented Sep 22, 2024

@daengdaengLee 혹시 코드가 조금 길어지긴하지만 아예 정규식을 사용하지 않는 방식은 어떨까요??
시간 복잡도는 동일해 괜찮지 않을까 싶어서 한 번 커밋해보겠습니다 🙇🏻‍♂️

re := regexp.MustCompile(`,`)
return re.ReplaceAllString(val, `$1\,`)

var result strings.Builder

Choose a reason for hiding this comment

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

이렇게 할꺼면 함수로 따로 분리하는 것도 좋지 않을까요?

Copy link
Author

@eogns47 eogns47 Sep 22, 2024

Choose a reason for hiding this comment

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

개인적으로 추가된 코드가 가독성을 크게 해치지 않고 재사용할 여지가 없는 것 같아서 굳이 분리하지 않았습니다..!
혹시 상기 이유 외에 필요 여부가 더 있다면 분리해보겠습니다!

Choose a reason for hiding this comment

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

제가 생각한 테스트도 추가해도 되나요?

Copy link
Member

Choose a reason for hiding this comment

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

테스트 케이스는 helm_test.go 의 TestHelmArgCleaner 에 추가해주시면 될 것 같습니다.

@daengdaengLee
Copy link
Member

daengdaengLee commented Sep 23, 2024

@eogns47 님 안녕하세요.

저는 코드 로직 괜찮다고 생각합니다.
다만 go 에서는 문자열을 바로 index 기반으로 순회해버리면 문제가 될 수 있을 것 같네요.
rune 슬라이스로 바꾼 후에 순회해야 할 것 같습니다. 아니면 for - range 로 순회해도 될 것 같아요. (내부에서 rune 슬라이스로 자동 변환해주는 걸로 알고있습니다.)
혹시 이 부분 수정해주실 수 있을까요?

감사합니다.

@eogns47
Copy link
Author

eogns47 commented Sep 23, 2024

@eogns47 님 안녕하세요.

저는 코드 로직 괜찮다고 생각합니다. 다만 go 에서는 문자열을 바로 index 기반으로 순회해버리면 문제가 될 수 있을 것 같네요. rune 슬라이스로 바꾼 후에 순회해야 할 것 같습니다. 아니면 for - range 로 순회해도 될 것 같아요. (내부에서 rune 슬라이스로 자동 변환해주는 걸로 알고있습니다.) 혹시 이 부분 수정해주실 수 있을까요?

감사합니다.

수정 후 커밋하였습니다!
다중 바이트 문자는 고려하지 못하고 코드를 짰었네요 🥹

util/helm/cmd.go Outdated
var result strings.Builder
for _, r := range val {
if r == ',' {
if result.Len() == 0 || result.String()[result.Len()-1] != '\\' {
Copy link
Member

@daengdaengLee daengdaengLee Sep 24, 2024

Choose a reason for hiding this comment

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

  • 위에 _ 로 처리한 게 index 니까 저걸 i 로 잡아서 i == 0 으로 처리해도 될 것 같아요.
  • 직전 r 을 별도 변수값으로 관리해서 (ex. prevR) 그거랑 비교하는게 좋을 것 같아요. 이렇게 하면 매번 문자열 빌드해서요.

Copy link
Member

Choose a reason for hiding this comment

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

아래는 예시입니다.

var result strings.Builder
var prevR rune
for i, r := range test {
	if r == ',' {
		if i == 0 || prevR != '\\' {
			result.WriteString(`\,`)
		} else {
			result.WriteRune(r)
		}
	} else {
		result.WriteRune(r)
	}

	prevR = r
}

return result.String()

Copy link
Author

Choose a reason for hiding this comment

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

바로 고쳐보겠습니다!

Copy link
Author

@eogns47 eogns47 Sep 24, 2024

Choose a reason for hiding this comment

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

@daengdaengLee 다시 커밋해봤습니다! 혹시 '매번 문자열을 빌드한다'가 result.String()함수를 호출하면서 객체를 생성하는 것을 말씀하시는게 맞을까요?

Copy link
Member

Choose a reason for hiding this comment

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

네 맞습니다~ :)

Copy link
Member

@daengdaengLee daengdaengLee left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@eogns47 eogns47 merged commit a7b85b1 into issue-19269#2 Sep 26, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants