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

Enhance RestLayerPrivilegesEvaluator Code Coverage #3218

Merged
merged 13 commits into from
Aug 23, 2023

Conversation

stephen-crawford
Copy link
Contributor

Description

RestLayerPrivilegesEvaluator is expected to have 100% code coverage. This change should boost the coverage as high as is reasonable. Per this comment: #2929 (comment), I am not adding tests for debug logs.

Issues Resolved

Resolves #2929

Check List

  • New functionality includes testing
  • New functionality has been documented
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

stephen-crawford and others added 4 commits August 21, 2023 10:02
Signed-off-by: Stephen Crawford <steecraw@amazon.com>
Signed-off-by: Stephen Crawford <steecraw@amazon.com>
Signed-off-by: Stephen Crawford <steecraw@amazon.com>
willyborankin
willyborankin previously approved these changes Aug 21, 2023
Copy link
Member

Choose a reason for hiding this comment

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

If code cov says that there is a no change, are you sure these tests are executed and their coverage data submitted? Do you see these tests executed from the artifacts of the check runs?

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does not look like it ran. I am not sure how to fix that though. I don't think it is something related to this change--I triggered a re-run.

Copy link
Collaborator

@willyborankin willyborankin Aug 21, 2023

Choose a reason for hiding this comment

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

@scrawfor99 I think you need to call it RestPathMatchesTests.java not RestPathMatchesTest.java.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe it is not only your case but others as well.

Copy link
Member

Choose a reason for hiding this comment

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

Nice catch @willyborankin . I've got a change that will expect 100% code coverage in the tests (e.g. all test code should be executed) that should help us catch this kind of issue

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes I did the same mistake :-D. Here is PR: #3224. Lets see.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huh, who knew. I will swap over the name.

@stephen-crawford
Copy link
Contributor Author

Screenshot 2023-08-22 at 10 13 43 AM

Confirmed tests ran.

@stephen-crawford
Copy link
Contributor Author

RestLayerPrivilegeEvaluator still lists isInitialized as having partial coverage. However, I think this is adequate. I added a test to check that it fails when not initialized (one of the two possibilities) and all other tests which test something else will inherently check isInitialized is true during their evaluation. This covers the true case.

@stephen-crawford
Copy link
Contributor Author

stephen-crawford commented Aug 22, 2023

This is the code coverage for the other SecurityRestFilter: https://app.codecov.io/gh/opensearch-project/security/pull/3218/blob/src/main/java/org/opensearch/security/filter/SecurityRestFilter.java

It still labels a significant amount as uncovered. I am not sure what is expected here however, as the way to exercise the files is to extract the internal method logic into separate functions so we can provide good and bad inputs and test the handling.

In RestLayerPrivilegesEvaluator there was a number of unused parameters
removed those to trim down the test requirements.

For the test cases switched to assertThat for easy of reading, enabled
debug logging to exercise debug logging code, adjusted usages of mocks
for cleaner test flow in a couple of spaces.

Verified locally with the following command and then checked the jacoco
report under build/reports/jacoco/test/html/...

./gradlew jacocoTestReport test --tests \
  org.opensearch.security.privileges.RestLayerPrivilegesEvaluatorTest

Signed-off-by: Peter Nied <petern@amazon.com>
…erage

Signed-off-by: Peter Nied <petern@amazon.com>
Copy link
Member

@peternied peternied left a comment

Choose a reason for hiding this comment

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

I've created a pull request for your fork that reduces the product code, adds more assertions, and gets more coverage, take a look and see if you'd like to include it with PR

@codecov
Copy link

codecov bot commented Aug 22, 2023

Codecov Report

Merging #3218 (b58e3bc) into main (75f529a) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #3218      +/-   ##
============================================
+ Coverage     62.43%   62.45%   +0.01%     
+ Complexity     3352     3351       -1     
============================================
  Files           254      254              
  Lines         19748    19743       -5     
  Branches       3334     3334              
============================================
  Hits          12330    12330              
+ Misses         5789     5785       -4     
+ Partials       1629     1628       -1     
Files Changed Coverage Δ
.../opensearch/security/OpenSearchSecurityPlugin.java 84.34% <100.00%> (ø)
...urity/privileges/RestLayerPrivilegesEvaluator.java 94.11% <100.00%> (+12.06%) ⬆️

Signed-off-by: Stephen Crawford <steecraw@amazon.com>
DarshitChanpura and others added 2 commits August 23, 2023 11:53
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
Cleans up RestLayerPrivilegesEvaluatorTest
Copy link
Member

@DarshitChanpura DarshitChanpura left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks @scrawfor99 !

@peternied peternied merged commit 46dfd84 into opensearch-project:main Aug 23, 2023
@DarshitChanpura DarshitChanpura added the backport 2.x backport to 2.x branch label Aug 24, 2023
@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.x failed:

The process '/usr/bin/git' failed with exit code 128

To backport manually, run these commands in your terminal:

# Navigate to the root of your repository
cd $(git rev-parse --show-toplevel)
# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add ../.worktrees/security/backport-2.x 2.x
# Navigate to the new working tree
pushd ../.worktrees/security/backport-2.x
# Create a new branch
git switch --create backport/backport-3218-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 46dfd841abeae193075d8cf95dd4023e9ea71ef9
# Push it to GitHub
git push --set-upstream origin backport/backport-3218-to-2.x
# Go back to the original working tree
popd
# Delete the working tree
git worktree remove ../.worktrees/security/backport-2.x

Then, create a pull request where the base branch is 2.x and the compare/head branch is backport/backport-3218-to-2.x.

DarshitChanpura pushed a commit to DarshitChanpura/security that referenced this pull request Aug 24, 2023
…t#3218)

Enhance RestLayerPrivilegesEvaluator Code Coverage

Signed-off-by: Stephen Crawford <steecraw@amazon.com>
Signed-off-by: Peter Nied <petern@amazon.com>
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
Co-authored-by: Peter Nied <petern@amazon.com>
Co-authored-by: Darshit Chanpura <dchanp@amazon.com>
(cherry picked from commit 46dfd84)
@DarshitChanpura
Copy link
Member

Backported in #2956 as that is the parent change for this PR.

@peternied peternied removed backport 2.x backport to 2.x branch backport-failed labels Aug 31, 2023
@stephen-crawford stephen-crawford deleted the boostedCoverage branch September 14, 2023 14:50
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.

RestLayerPrivilegesEvaluator must have 100% branch code coverage
4 participants