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 to allow whitelist globbing for strings after wildcard #796

Merged
merged 4 commits into from
Oct 3, 2019

Conversation

dedamico
Copy link
Contributor

@dedamico dedamico commented Oct 1, 2019

Fixes #692

Copy link
Member

@lkysow lkysow left a comment

Choose a reason for hiding this comment

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

Nice work! One test case I think we should cover.

"owner/prefix-repo",
"github.com",
true,
},
Copy link
Member

Choose a reason for hiding this comment

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

Can you add tests for the wildcard at the beginning of the string too: *-owner/repo? That will the cover all the cases I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I added the new test case and ran the test. If you are okay let me know and I can squash & merge.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

Copy link
Member

Choose a reason for hiding this comment

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

So I meant at the beginning of the whitelist:

		{
			"should match if wildcard is at beginning",
			"*-owner/repo",
			"prefix-owner/repo",
			"github.com",
			true,
		},

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So i'm a bit confused since all of the other tests start with "github.com" as the second value in the struct (= whitelist) and I thought this was required in the whitelist as per the below statement on this doc:

https://www.runatlantis.io/docs/server-configuration.html#repo-whitelist

Format is {hostname}/{owner}/{repo}, ex. github.com/runatlantis/atlantis

Copy link
Member

Choose a reason for hiding this comment

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

It's not required. You can do --repo-whitelist='*' for example to match everything.

@codecov
Copy link

codecov bot commented Oct 2, 2019

Codecov Report

Merging #796 into master will decrease coverage by 0.01%.
The diff coverage is 60%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #796      +/-   ##
==========================================
- Coverage   72.78%   72.77%   -0.02%     
==========================================
  Files          63       63              
  Lines        4807     4812       +5     
==========================================
+ Hits         3499     3502       +3     
- Misses       1050     1051       +1     
- Partials      258      259       +1
Impacted Files Coverage Δ
server/events/repo_whitelist_checker.go 92.85% <60%> (-7.15%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c4c439a...2d584f7. Read the comment docs.

"owner/prefix-repo",
"github.com",
true,
},
Copy link
Member

Choose a reason for hiding this comment

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

So I meant at the beginning of the whitelist:

		{
			"should match if wildcard is at beginning",
			"*-owner/repo",
			"prefix-owner/repo",
			"github.com",
			true,
		},

// substr(rule): -abc
if wildcardIdx != len(rule)-1 {
// If the rule substring after wildcard does not exist in the candidate, then it is not a match.
if !strings.Contains(candidate, rule[wildcardIdx+1:]) {
Copy link
Member

Choose a reason for hiding this comment

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

This works, but I think it might be clearer to re-use the strings.Index (or LastIndex) call.

idx := strings.LastIndex(candidate, rule[wildcardIdx+1:])
if idx == -1 {
  return false
}
return candidate[idx:] == rule[wildcardIdx+1:]

For me this seems clearer because rather than relying on strings.Contains implying that strings.Index will return a positive value, we're making that requirement clear in the code.

return false
}

return candidate[strings.Index(candidate, rule[wildcardIdx+1:]):] == rule[wildcardIdx+1:]
Copy link
Member

Choose a reason for hiding this comment

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

I think we need to use strings.LastIndex here? Otherwise this test fails:

		{
			"should match with duplicate",
			"*runatlantis",
			"runatlantis/runatlantis",
			"github.com",
			true,
		},

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is this a test you just added ad-hoc? also not understanding this test rationale (as per my other comment to include github.com at the beginning). So the reply to that one may also answer this one. thanks!

Copy link
Member

Choose a reason for hiding this comment

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

Yes it's just one I added.

@dedamico
Copy link
Contributor Author

dedamico commented Oct 2, 2019

@lkysow thanks for responding to my questions and for taking a closer look at my code, much appreciated. i think I covered everything and I went ahead and added the other tests you referred to. If it all looks okay I can do the squash & merge.

Copy link
Member

@lkysow lkysow left a comment

Choose a reason for hiding this comment

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

🎉 nice!

@dedamico dedamico merged commit ae57df9 into master Oct 3, 2019
@dedamico dedamico deleted the Issue692 branch October 3, 2019 10:48
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.

repo whitelist globbing ignores everything after
2 participants