-
Notifications
You must be signed in to change notification settings - Fork 1
BI-2255 - Experimental Collaborator Micronaut Changes #388
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
f9ee83c
[BI-2255] - refactored ProgramSecuredRoleGroup enum
mlm483 2c5dce8
[BI-2255] - refactored ProgramSecuredRoleGroup enum
mlm483 f1279b7
[BI-2255] WIP
davedrp e9351a9
[BI-2255] WIP
davedrp e037a0c
Merge pull request #377 from Breeding-Insight/feature/BI-1692
HMS17 69e68fe
[autocommit] bumping build number
rob-ouser-bi 6cabaed
[BI-2255] WIP
davedrp de9cad3
Merge branch 'develop' into feature/BI-2257
mlm483 16e3768
Merge pull request #383 from Breeding-Insight/feature/BI-2257
mlm483 1a2c6de
[autocommit] bumping build number
rob-ouser-bi 1654b5e
[BI-2255] WIP
davedrp 972848c
Merge branch 'develop' into feature/BI-2255
davedrp e65f791
[BI-2055] adjusted order of SecuredAnnotationRules
davedrp b7bc15b
[BI-2055] Removed parameters from ExperimentSecuredRule
davedrp ed72bc8
[BI-2055] renamed annotation from ExperimentSecured -> ExperimentColl…
davedrp bf6f0de
[BI-2055] created the extractExperimentId to make more readable
davedrp 760f054
[BI-2255] cleaned up code
davedrp 45fa5d4
[BI-2255] move initialization of variable to inside if-statement
davedrp b82badd
Merge branch 'develop' into feature/BI-2255
davedrp 51adb85
[BI-2255]Addressed PR-comments from Matthew
davedrp a9d71ea
Merge branch 'develop' into feature/BI-2255
davedrp 321f8ad
[BI-2255] fixed bug
davedrp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/org/breedinginsight/api/auth/ExperimentCollaboratorSecured.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * See the NOTICE file distributed with this work for additional information | ||
| * regarding copyright ownership. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.breedinginsight.api.auth; | ||
|
|
||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.METHOD) | ||
| public @interface ExperimentCollaboratorSecured { | ||
| //The only role is EXPERIMENTAL_COLLABORATOR | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
.../java/org/breedinginsight/api/auth/rules/ExperimentCollaboratorSecuredAnnotationRule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| /* | ||
| * See the NOTICE file distributed with this work for additional information | ||
| * regarding copyright ownership. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.breedinginsight.api.auth.rules; | ||
|
|
||
| import io.micronaut.http.HttpRequest; | ||
| import io.micronaut.http.HttpStatus; | ||
| import io.micronaut.http.exceptions.HttpStatusException; | ||
| import io.micronaut.http.server.exceptions.HttpServerException; | ||
| import io.micronaut.security.rules.SecuredAnnotationRule; | ||
| import io.micronaut.security.rules.SecurityRuleResult; | ||
| import io.micronaut.security.token.RolesFinder; | ||
| import io.micronaut.web.router.MethodBasedRouteMatch; | ||
| import io.micronaut.web.router.RouteMatch; | ||
| import org.brapi.client.v2.model.exceptions.ApiException; | ||
| import org.brapi.v2.model.core.BrAPITrial; | ||
| import org.breedinginsight.api.auth.*; | ||
| import org.breedinginsight.brapi.v2.dao.BrAPITrialDAO; | ||
| import org.breedinginsight.daos.ExperimentalCollaboratorDAO; | ||
| import org.breedinginsight.daos.ProgramDAO; | ||
| import org.breedinginsight.model.ProgramUser; | ||
| import org.breedinginsight.model.Role; | ||
| import org.breedinginsight.services.exceptions.DoesNotExistException; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import javax.inject.Inject; | ||
| import javax.inject.Singleton; | ||
| import java.util.*; | ||
|
|
||
| @Singleton | ||
| public class ExperimentCollaboratorSecuredAnnotationRule extends SecuredAnnotationRule { | ||
|
|
||
| // Executes before the ProgramSecuredAnnotationRule, and if the annotation exists, will return before the ProgramSecuredAnnotationRule can execute | ||
| public static final Integer ORDER = ProgramSecuredAnnotationRule.ORDER -2; | ||
|
|
||
| public ExperimentCollaboratorSecuredAnnotationRule(RolesFinder rolesFinder) { | ||
| super(rolesFinder); | ||
| } | ||
|
|
||
| @Inject | ||
| private SecurityService securityService; | ||
| @Inject | ||
| private ProgramDAO programDAO; | ||
| @Inject | ||
| private BrAPITrialDAO brAPITrialDAO; | ||
| @Inject | ||
| private ExperimentalCollaboratorDAO experimentalCollaboratorDAO; | ||
|
|
||
| @Override | ||
| public SecurityRuleResult check(HttpRequest<?> request, @Nullable RouteMatch<?> routeMatch, @Nullable Map<String, Object> claims) { | ||
| // Does not approve request so that checks after it can check. Only rejects on fail. | ||
|
|
||
| if (routeMatch instanceof MethodBasedRouteMatch) { | ||
| MethodBasedRouteMatch methodRoute = ((MethodBasedRouteMatch) routeMatch); | ||
|
|
||
| if (methodRoute.hasAnnotation(ExperimentCollaboratorSecured.class)) { | ||
| String programId = (String) routeMatch.getVariableValues() | ||
| .get("programId"); | ||
| String experimentId = extractExperimentId(routeMatch); | ||
| if (programId == null) { | ||
| throw new HttpServerException("Endpoint does not have program id to check roles against"); | ||
| } | ||
| if (experimentId == null) { | ||
| throw new HttpServerException("Endpoint does not have experiment id to check roles against"); | ||
| } | ||
|
|
||
| if (!programDAO.existsById(UUID.fromString(programId))) { | ||
| throw new HttpStatusException(HttpStatus.NOT_FOUND, "Program does not exist"); | ||
| } | ||
| Optional<BrAPITrial> trial; | ||
| try { | ||
| trial = brAPITrialDAO.getTrialById(UUID.fromString(programId), UUID.fromString(experimentId)); | ||
| } catch (ApiException e) { | ||
| throw new RuntimeException(e); | ||
| } catch (DoesNotExistException e) { | ||
| throw new HttpStatusException(HttpStatus.NOT_FOUND, "Experiment does not exist"); | ||
| } | ||
| if( trial.isEmpty()) { | ||
| throw new HttpStatusException(HttpStatus.NOT_FOUND, "Experiment does not exist"); | ||
| } | ||
|
|
||
| if (claims != null){ | ||
| AuthenticatedUser user = securityService.getUser(); | ||
|
|
||
| return checkAuthorization(user, experimentId, programId); | ||
| } | ||
|
|
||
| // Rejects if no claims | ||
| return SecurityRuleResult.REJECTED; | ||
| } | ||
| } | ||
|
|
||
| return SecurityRuleResult.UNKNOWN; | ||
| } | ||
|
|
||
| private static String extractExperimentId(@NotNull RouteMatch<?> routeMatch) { | ||
| //The endpoints can use either the "experimentId" or "trialId" parameter to pass the experiment ID | ||
| String experimentId = (String) routeMatch.getVariableValues() | ||
| .get("experimentId"); | ||
| if( experimentId==null) { | ||
| experimentId = (String) routeMatch.getVariableValues() | ||
| .get("trialId"); | ||
| } | ||
| return experimentId; | ||
| } | ||
|
|
||
| private SecurityRuleResult checkAuthorization(AuthenticatedUser authenticatedUser, String experimentId, String programId) { | ||
| ProgramUser programUserRole; | ||
| try { | ||
| programUserRole = authenticatedUser.extractProgramUser(UUID.fromString(programId)); | ||
| } catch (DoesNotExistException e) { | ||
| return SecurityRuleResult.UNKNOWN; | ||
| } | ||
| if(this.isExperimentCoordinator(programUserRole)){ | ||
| List<UUID> collaborativeExperimentIds = experimentalCollaboratorDAO.getExperimentIds(programUserRole.getId(), true); | ||
| if(collaborativeExperimentIds.contains( UUID.fromString(experimentId)) ){ | ||
| return SecurityRuleResult.ALLOWED; | ||
| } | ||
| } | ||
| else { | ||
| //Allow the next Secured Annotation to be run | ||
| return SecurityRuleResult.UNKNOWN; | ||
| } | ||
| return SecurityRuleResult.REJECTED; | ||
| } | ||
|
|
||
| private boolean isExperimentCoordinator(ProgramUser programUser){ | ||
| List<Role> roles = programUser.getRoles(); | ||
| if( roles.size()!=1 ){ return false; } | ||
| String primaryRole = roles.get(0).getDomain(); | ||
| return (primaryRole != null && | ||
| primaryRole.equals( ProgramSecuredRole.EXPERIMENTAL_COLLABORATOR.toString() ) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public int getOrder() { | ||
| return ORDER; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.