use of io.helidon.security.SecurityLevel in project helidon by oracle.
the class RoleValidatorTest method testServiceRoles.
@Test
void testServiceRoles() {
RoleValidator validator = RoleValidator.create();
RoleValidator.Roles annot = mock(RoleValidator.Roles.class);
String[] roleArray = new String[] { "admin" };
when(annot.value()).thenReturn(roleArray);
when(annot.subjectType()).thenReturn(SubjectType.SERVICE);
SecurityLevel appSecurityLevel = mock(SecurityLevel.class);
SecurityLevel classSecurityLevel = mock(SecurityLevel.class);
List<SecurityLevel> securityLevels = new ArrayList<>();
securityLevels.add(appSecurityLevel);
securityLevels.add(classSecurityLevel);
EndpointConfig ep = mock(EndpointConfig.class);
when(ep.securityLevels()).thenReturn(securityLevels);
when(classSecurityLevel.filterAnnotations(RoleValidator.Roles.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(annot));
RoleValidator.RoleConfig rConfig = validator.fromAnnotations(ep);
Errors.Collector collector = Errors.collector();
ProviderRequest request = mock(ProviderRequest.class);
when(request.service()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("service")).addGrant(Role.create("admin")).build()));
when(request.subject()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("myAdmin")).addGrant(Role.create("user")).build()));
validator.validate(rConfig, collector, request);
collector.collect().checkValid();
}
use of io.helidon.security.SecurityLevel in project helidon by oracle.
the class RoleValidatorTest method testUserRoles.
@Test
void testUserRoles() {
RoleValidator validator = RoleValidator.create();
RoleValidator.Roles annot = mock(RoleValidator.Roles.class);
String[] roleArray = new String[] { "admin" };
when(annot.value()).thenReturn(roleArray);
when(annot.subjectType()).thenReturn(SubjectType.USER);
SecurityLevel appSecurityLevel = mock(SecurityLevel.class);
SecurityLevel classSecurityLevel = mock(SecurityLevel.class);
List<SecurityLevel> securityLevels = new ArrayList<>();
securityLevels.add(appSecurityLevel);
securityLevels.add(classSecurityLevel);
EndpointConfig ep = mock(EndpointConfig.class);
when(ep.securityLevels()).thenReturn(securityLevels);
when(classSecurityLevel.filterAnnotations(RoleValidator.Roles.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(annot));
RoleValidator.RoleConfig rConfig = validator.fromAnnotations(ep);
Errors.Collector collector = Errors.collector();
ProviderRequest request = mock(ProviderRequest.class);
when(request.subject()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("myAdmin")).addGrant(Role.create("admin")).build()));
when(request.service()).thenReturn(Optional.empty());
validator.validate(rConfig, collector, request);
collector.collect().checkValid();
}
use of io.helidon.security.SecurityLevel in project helidon by oracle.
the class RoleValidatorTest method testRolesAllowedPermit.
@Test
void testRolesAllowedPermit() {
RoleValidator validator = RoleValidator.create();
RolesAllowed annot = mock(RolesAllowed.class);
String[] roleArray = new String[] { "admin" };
when(annot.value()).thenReturn(roleArray);
SecurityLevel appSecurityLevel = mock(SecurityLevel.class);
SecurityLevel classSecurityLevel = mock(SecurityLevel.class);
List<SecurityLevel> securityLevels = new ArrayList<>();
securityLevels.add(appSecurityLevel);
securityLevels.add(classSecurityLevel);
EndpointConfig ep = mock(EndpointConfig.class);
when(ep.securityLevels()).thenReturn(securityLevels);
when(classSecurityLevel.filterAnnotations(RolesAllowed.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(annot));
RoleValidator.RoleConfig rConfig = validator.fromAnnotations(ep);
Errors.Collector collector = Errors.collector();
ProviderRequest request = mock(ProviderRequest.class);
when(request.subject()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("myAdmin")).addGrant(Role.create("admin")).build()));
when(request.service()).thenReturn(Optional.empty());
validator.validate(rConfig, collector, request);
collector.collect().checkValid();
}
use of io.helidon.security.SecurityLevel in project helidon by oracle.
the class PolicyValidator method fromAnnotations.
@Override
public PolicyConfig fromAnnotations(EndpointConfig endpointConfig) {
PolicyConfig.Builder resultBuilder = PolicyConfig.builder();
for (SecurityLevel securityLevel : endpointConfig.securityLevels()) {
for (EndpointConfig.AnnotationScope scope : EndpointConfig.AnnotationScope.values()) {
List<Annotation> annotations = new ArrayList<>();
for (Class<? extends Annotation> annotation : supportedAnnotations()) {
annotations.addAll(securityLevel.filterAnnotations(annotation, scope));
}
for (Annotation annotation : annotations) {
if (annotation instanceof PolicyStatement) {
PolicyStatement statement = (PolicyStatement) annotation;
resultBuilder.from(PolicyConfig.builder().from(statement).build());
}
}
}
}
return resultBuilder.build();
}
use of io.helidon.security.SecurityLevel in project helidon by oracle.
the class RoleValidatorTest method testUserRolesDeny.
@Test
void testUserRolesDeny() {
RoleValidator validator = RoleValidator.create();
RoleValidator.Roles annot = mock(RoleValidator.Roles.class);
String[] roleArray = new String[] { "admin" };
when(annot.subjectType()).thenReturn(SubjectType.USER);
when(annot.value()).thenReturn(roleArray);
SecurityLevel appSecurityLevel = mock(SecurityLevel.class);
SecurityLevel classSecurityLevel = mock(SecurityLevel.class);
List<SecurityLevel> securityLevels = new ArrayList<>();
securityLevels.add(appSecurityLevel);
securityLevels.add(classSecurityLevel);
EndpointConfig ep = mock(EndpointConfig.class);
when(ep.securityLevels()).thenReturn(securityLevels);
when(classSecurityLevel.filterAnnotations(RoleValidator.Roles.class, EndpointConfig.AnnotationScope.METHOD)).thenReturn(List.of(annot));
RoleValidator.RoleConfig rConfig = validator.fromAnnotations(ep);
Errors.Collector collector = Errors.collector();
ProviderRequest request = mock(ProviderRequest.class);
when(request.subject()).thenReturn(Optional.of(Subject.builder().principal(Principal.create("myAdmin")).addGrant(Role.create("user")).build()));
when(request.service()).thenReturn(Optional.empty());
validator.validate(rConfig, collector, request);
if (collector.collect().isValid()) {
fail("User is not in admin role, should have failed");
}
}
Aggregations