Search in sources :

Example 1 with Subject

use of alien4cloud.security.Subject in project alien4cloud by alien4cloud.

the class LocationSecurityService method getSubjectsFromContext.

/**
 * Get subjects from context (current user, current user's groups, current environment, application ...)
 *
 * @param environment the environment from which the request has been made
 * @return a map of subject type to subjects' ids
 */
public Map<Subject, Set<String>> getSubjectsFromContext(ApplicationEnvironment environment) {
    Map<Subject, Set<String>> subjectsMap = new HashMap<>();
    User user = AuthorizationUtil.getCurrentUser();
    if (user != null) {
        subjectsMap.put(Subject.USER, Sets.newHashSet(user.getUsername()));
        Set<String> userGroups = AuthorizationUtil.getUserGroups(user);
        subjectsMap.put(Subject.GROUP, userGroups);
    }
    if (environment != null) {
        subjectsMap.put(Subject.ENVIRONMENT, Sets.newHashSet(environment.getId()));
        subjectsMap.put(Subject.ENVIRONMENT_TYPE, Sets.newHashSet(environment.getApplicationId() + ":" + environment.getEnvironmentType().toString()));
        subjectsMap.put(Subject.APPLICATION, Sets.newHashSet(environment.getApplicationId()));
    }
    return subjectsMap;
}
Also used : Set(java.util.Set) User(alien4cloud.security.model.User) HashMap(java.util.HashMap) Subject(alien4cloud.security.Subject)

Example 2 with Subject

use of alien4cloud.security.Subject in project alien4cloud by alien4cloud.

the class AbstractLocationResourcesBatchSecurityController method processRevokeForSubjectType.

private void processRevokeForSubjectType(Subject subjectType, String[] resources, String[] subjects) {
    if (ArrayUtils.isEmpty(resources)) {
        return;
    }
    Arrays.stream(resources).forEach(resourceId -> {
        AbstractLocationResourceTemplate resourceTemplate = locationResourceService.getOrFail(resourceId);
        resourcePermissionService.revokePermission(resourceTemplate, (resource -> locationResourceService.saveResource((AbstractLocationResourceTemplate) resource)), subjectType, subjects);
    });
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) ApplicationEnvironmentService(alien4cloud.application.ApplicationEnvironmentService) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Subject(alien4cloud.security.Subject) LocationService(alien4cloud.orchestrators.locations.services.LocationService) ResourcePermissionService(alien4cloud.authorization.ResourcePermissionService) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayUtils(org.apache.commons.lang3.ArrayUtils) LocationSecurityService(alien4cloud.orchestrators.locations.services.LocationSecurityService) Location(alien4cloud.model.orchestrators.locations.Location) RequestBody(org.springframework.web.bind.annotation.RequestBody) ApiOperation(io.swagger.annotations.ApiOperation) Audit(alien4cloud.audit.annotation.Audit) RestResponseBuilder(alien4cloud.rest.model.RestResponseBuilder) RestResponse(alien4cloud.rest.model.RestResponse) ILocationResourceService(alien4cloud.orchestrators.locations.services.ILocationResourceService) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) MediaType(org.springframework.http.MediaType) Resource(javax.annotation.Resource) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SubjectsAuthorizationRequest(alien4cloud.rest.orchestrator.model.SubjectsAuthorizationRequest) List(java.util.List) AbstractLocationResourceTemplate(alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate) ApplicationEnvironmentAuthorizationUpdateRequest(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest) AbstractLocationResourceTemplate(alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate)

Example 3 with Subject

use of alien4cloud.security.Subject in project alien4cloud by alien4cloud.

the class AbstractLocationResourcesBatchSecurityController method processGrantForSubjectType.

private void processGrantForSubjectType(Subject subjectType, String orchestratorId, String locationId, String[] resources, String[] subjects) {
    if (ArrayUtils.isEmpty(resources)) {
        return;
    }
    Location location = locationService.getLocation(orchestratorId, locationId);
    locationSecurityService.grantAuthorizationOnLocationIfNecessary(location, subjectType, subjects);
    Arrays.stream(resources).forEach(resourceId -> {
        AbstractLocationResourceTemplate resourceTemplate = locationResourceService.getOrFail(resourceId);
        // prefer using locationResourceService.saveResource so that the location update date is update.
        // This will then trigger a deployment topology update
        resourcePermissionService.grantPermission(resourceTemplate, (resource -> locationResourceService.saveResource(location, (AbstractLocationResourceTemplate) resource)), subjectType, subjects);
    });
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Arrays(java.util.Arrays) ApplicationEnvironmentService(alien4cloud.application.ApplicationEnvironmentService) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Subject(alien4cloud.security.Subject) LocationService(alien4cloud.orchestrators.locations.services.LocationService) ResourcePermissionService(alien4cloud.authorization.ResourcePermissionService) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayUtils(org.apache.commons.lang3.ArrayUtils) LocationSecurityService(alien4cloud.orchestrators.locations.services.LocationSecurityService) Location(alien4cloud.model.orchestrators.locations.Location) RequestBody(org.springframework.web.bind.annotation.RequestBody) ApiOperation(io.swagger.annotations.ApiOperation) Audit(alien4cloud.audit.annotation.Audit) RestResponseBuilder(alien4cloud.rest.model.RestResponseBuilder) RestResponse(alien4cloud.rest.model.RestResponse) ILocationResourceService(alien4cloud.orchestrators.locations.services.ILocationResourceService) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) MediaType(org.springframework.http.MediaType) Resource(javax.annotation.Resource) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SubjectsAuthorizationRequest(alien4cloud.rest.orchestrator.model.SubjectsAuthorizationRequest) List(java.util.List) AbstractLocationResourceTemplate(alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate) ApplicationEnvironmentAuthorizationUpdateRequest(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest) AbstractLocationResourceTemplate(alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate) Location(alien4cloud.model.orchestrators.locations.Location)

Aggregations

Subject (alien4cloud.security.Subject)3 Set (java.util.Set)3 ApplicationEnvironmentService (alien4cloud.application.ApplicationEnvironmentService)2 Audit (alien4cloud.audit.annotation.Audit)2 ResourcePermissionService (alien4cloud.authorization.ResourcePermissionService)2 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)2 AbstractLocationResourceTemplate (alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate)2 Location (alien4cloud.model.orchestrators.locations.Location)2 ILocationResourceService (alien4cloud.orchestrators.locations.services.ILocationResourceService)2 LocationSecurityService (alien4cloud.orchestrators.locations.services.LocationSecurityService)2 LocationService (alien4cloud.orchestrators.locations.services.LocationService)2 RestResponse (alien4cloud.rest.model.RestResponse)2 RestResponseBuilder (alien4cloud.rest.model.RestResponseBuilder)2 ApplicationEnvironmentAuthorizationUpdateRequest (alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest)2 SubjectsAuthorizationRequest (alien4cloud.rest.orchestrator.model.SubjectsAuthorizationRequest)2 Sets (com.google.common.collect.Sets)2 ApiOperation (io.swagger.annotations.ApiOperation)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2