Search in sources :

Example 21 with Location

use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.

the class AbstractLocationResourcesSecurityController method grantAccessToUsers.

/**
 *****************************************************************************************************************************
 *
 * SECURITY ON USERS
 *
 ******************************************************************************************************************************
 */
/**
 * Grant access to the location resoure to the user (deploy on the location)
 *
 * @param locationId The location's id.
 * @param resourceId The location resource's id.
 * @param userNames The authorized users.
 * @return A {@link Void} {@link RestResponse}.
 */
@ApiOperation(value = "Grant access to the location's resource to the users, send back the new authorised users list", notes = "Only user with ADMIN role can grant access to another users.")
@RequestMapping(value = "/users", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
@Audit
public synchronized RestResponse<List<UserDTO>> grantAccessToUsers(@PathVariable String orchestratorId, @PathVariable String locationId, @PathVariable String resourceId, @RequestBody String[] userNames) {
    Location location = locationService.getLocation(orchestratorId, locationId);
    locationSecurityService.grantAuthorizationOnLocationIfNecessary(location, Subject.USER, userNames);
    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)), Subject.USER, userNames);
    List<UserDTO> users = UserDTO.convert(resourcePermissionService.getAuthorizedUsers(resourceTemplate));
    return RestResponseBuilder.<List<UserDTO>>builder().data(users).build();
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Lists(org.elasticsearch.common.collect.Lists) Arrays(java.util.Arrays) ApplicationEnvironmentService(alien4cloud.application.ApplicationEnvironmentService) Subject(alien4cloud.security.Subject) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) LocationService(alien4cloud.orchestrators.locations.services.LocationService) ResourcePermissionService(alien4cloud.authorization.ResourcePermissionService) ApplicationEnvironmentAuthorizationDTO(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationDTO) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayUtils(org.apache.commons.lang3.ArrayUtils) LocationSecurityService(alien4cloud.orchestrators.locations.services.LocationSecurityService) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) 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) Application(alien4cloud.model.application.Application) ILocationResourceService(alien4cloud.orchestrators.locations.services.ILocationResourceService) MapUtils(org.apache.commons.collections4.MapUtils) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) MediaType(org.springframework.http.MediaType) Resource(javax.annotation.Resource) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) IGenericSearchDAO(alien4cloud.dao.IGenericSearchDAO) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) GroupDTO(alien4cloud.rest.orchestrator.model.GroupDTO) UserDTO(alien4cloud.rest.orchestrator.model.UserDTO) AbstractLocationResourceTemplate(alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate) ApplicationEnvironmentAuthorizationUpdateRequest(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest) UserDTO(alien4cloud.rest.orchestrator.model.UserDTO) AbstractLocationResourceTemplate(alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate) List(java.util.List) Location(alien4cloud.model.orchestrators.locations.Location) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 22 with Location

use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.

the class AbstractLocationResourcesSecurityController method grantAccessToGroups.

/**
 *****************************************************************************************************************************
 *
 * SECURITY ON GROUPS
 *
 ******************************************************************************************************************************
 */
/**
 * Grant access to the location resource to the groups
 *
 * @param locationId The location's id.
 * @param groupIds The authorized groups.
 * @return A {@link Void} {@link RestResponse}.
 */
@ApiOperation(value = "Grant access to the location to the groups", notes = "Only user with ADMIN role can grant access to a group.")
@RequestMapping(value = "/groups", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
@Audit
public synchronized RestResponse<List<GroupDTO>> grantAccessToGroups(@PathVariable String orchestratorId, @PathVariable String locationId, @PathVariable String resourceId, @RequestBody String[] groupIds) {
    Location location = locationService.getLocation(orchestratorId, locationId);
    locationSecurityService.grantAuthorizationOnLocationIfNecessary(location, Subject.GROUP, groupIds);
    AbstractLocationResourceTemplate resourceTemplate = locationResourceService.getOrFail(resourceId);
    resourcePermissionService.grantPermission(resourceTemplate, (resource -> locationResourceService.saveResource(location, (AbstractLocationResourceTemplate) resource)), Subject.GROUP, groupIds);
    List<GroupDTO> groups = GroupDTO.convert(resourcePermissionService.getAuthorizedGroups(resourceTemplate));
    return RestResponseBuilder.<List<GroupDTO>>builder().data(groups).build();
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) Lists(org.elasticsearch.common.collect.Lists) Arrays(java.util.Arrays) ApplicationEnvironmentService(alien4cloud.application.ApplicationEnvironmentService) Subject(alien4cloud.security.Subject) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) LocationService(alien4cloud.orchestrators.locations.services.LocationService) ResourcePermissionService(alien4cloud.authorization.ResourcePermissionService) ApplicationEnvironmentAuthorizationDTO(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationDTO) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ArrayUtils(org.apache.commons.lang3.ArrayUtils) LocationSecurityService(alien4cloud.orchestrators.locations.services.LocationSecurityService) AlienUtils.safe(alien4cloud.utils.AlienUtils.safe) 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) Application(alien4cloud.model.application.Application) ILocationResourceService(alien4cloud.orchestrators.locations.services.ILocationResourceService) MapUtils(org.apache.commons.collections4.MapUtils) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) MediaType(org.springframework.http.MediaType) Resource(javax.annotation.Resource) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) IGenericSearchDAO(alien4cloud.dao.IGenericSearchDAO) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) GroupDTO(alien4cloud.rest.orchestrator.model.GroupDTO) UserDTO(alien4cloud.rest.orchestrator.model.UserDTO) AbstractLocationResourceTemplate(alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate) ApplicationEnvironmentAuthorizationUpdateRequest(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest) GroupDTO(alien4cloud.rest.orchestrator.model.GroupDTO) AbstractLocationResourceTemplate(alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate) List(java.util.List) Location(alien4cloud.model.orchestrators.locations.Location) Audit(alien4cloud.audit.annotation.Audit) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with Location

use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.

the class LocationMetaPropertiesController method upsertMetaProperty.

/**
 * Update or create a property for an orchestrator
 *
 * @param orchestratorId id of the orchestrator the location belongs to.
 * @param locationId id of the location to update
 * @param propertyRequest property request
 * @return information on the constraint
 */
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
@Audit
public RestResponse<ConstraintInformation> upsertMetaProperty(@ApiParam(value = "Id of the orchestrator for which the location is defined.") @PathVariable String orchestratorId, @ApiParam(value = "Id of the location to get", required = true) @PathVariable String locationId, @ApiParam(value = "Id of the location to get", required = true) @RequestBody PropertyRequest propertyRequest) throws ConstraintViolationException, ConstraintValueDoNotMatchPropertyTypeException {
    AuthorizationUtil.hasOneRoleIn(Role.ADMIN);
    Location location = locationService.getOrFail(locationId);
    try {
        metaPropertiesService.upsertMetaProperty(location, propertyRequest.getDefinitionId(), propertyRequest.getValue());
    } catch (ConstraintViolationException e) {
        log.error("Constraint violation error for property <" + propertyRequest.getDefinitionId() + "> with value <" + propertyRequest.getValue() + ">", e);
        return RestResponseBuilder.<ConstraintInformation>builder().data(e.getConstraintInformation()).error(RestErrorBuilder.builder(RestErrorCode.PROPERTY_CONSTRAINT_VIOLATION_ERROR).message(e.getMessage()).build()).build();
    } catch (ConstraintValueDoNotMatchPropertyTypeException e) {
        log.error("Constraint value violation error for property <" + e.getConstraintInformation().getName() + "> with value <" + e.getConstraintInformation().getValue() + "> and type <" + e.getConstraintInformation().getType() + ">", e);
        return RestResponseBuilder.<ConstraintInformation>builder().data(e.getConstraintInformation()).error(RestErrorBuilder.builder(RestErrorCode.PROPERTY_TYPE_VIOLATION_ERROR).message(e.getMessage()).build()).build();
    }
    return RestResponseBuilder.<ConstraintInformation>builder().data(null).error(null).build();
}
Also used : ConstraintValueDoNotMatchPropertyTypeException(org.alien4cloud.tosca.exceptions.ConstraintValueDoNotMatchPropertyTypeException) ConstraintViolationException(org.alien4cloud.tosca.exceptions.ConstraintViolationException) ConstraintInformation(alien4cloud.tosca.properties.constraints.ConstraintUtil.ConstraintInformation) Location(alien4cloud.model.orchestrators.locations.Location) Audit(alien4cloud.audit.annotation.Audit) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 24 with Location

use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.

the class DeploymentController method getRelatedLocationsSummaries.

private Map<String, Location> getRelatedLocationsSummaries(Deployment[] deployments) {
    Set<String> locationIds = Sets.newHashSet();
    for (Deployment deployment : deployments) {
        if (ArrayUtils.isNotEmpty(deployment.getLocationIds())) {
            locationIds.addAll(Sets.newHashSet(deployment.getLocationIds()));
        }
    }
    Map<String, Location> locations = null;
    if (!locationIds.isEmpty()) {
        locations = locationService.findByIds(FetchContext.SUMMARY, locationIds.toArray(new String[locationIds.size()]));
    }
    return locations != null ? locations : Maps.newHashMap();
}
Also used : Deployment(alien4cloud.model.deployment.Deployment) Location(alien4cloud.model.orchestrators.locations.Location)

Example 25 with Location

use of alien4cloud.model.orchestrators.locations.Location in project alien4cloud by alien4cloud.

the class DeploymentController method buildDeploymentsDTOS.

private List<DeploymentDTO> buildDeploymentsDTOS(boolean includeSourceSummary, Deployment... deployments) {
    List<DeploymentDTO> dtos = Lists.newArrayList();
    if (ArrayUtils.isEmpty(deployments)) {
        return dtos;
    }
    Map<String, ? extends IDeploymentSource> sources = Maps.newHashMap();
    // get the app summaries if true
    if (includeSourceSummary) {
        DeploymentSourceType sourceType = deployments[0].getSourceType();
        String[] sourceIds = getSourceIdsFromDeployments(deployments);
        if (sourceIds[0] != null) {
            // can have no application deployed
            switch(sourceType) {
                case APPLICATION:
                    Map<String, ? extends IDeploymentSource> appSources = applicationService.findByIdsIfAuthorized(FetchContext.SUMMARY, sourceIds);
                    if (appSources != null) {
                        sources = appSources;
                    }
            }
        }
    }
    Map<String, Location> locationsSummariesMap = getRelatedLocationsSummaries(deployments);
    for (Object object : deployments) {
        Deployment deployment = (Deployment) object;
        IDeploymentSource source = sources.get(deployment.getSourceId());
        if (source == null) {
            source = new DeploymentSourceDTO(deployment.getSourceId(), deployment.getSourceName());
        }
        List<Location> locationsSummaries = getLocations(deployment.getLocationIds(), locationsSummariesMap);
        DeploymentDTO dto = new DeploymentDTO(deployment, source, locationsSummaries);
        dtos.add(dto);
    }
    return dtos;
}
Also used : Deployment(alien4cloud.model.deployment.Deployment) DeploymentSourceType(alien4cloud.model.deployment.DeploymentSourceType) IDeploymentSource(alien4cloud.model.deployment.IDeploymentSource) Location(alien4cloud.model.orchestrators.locations.Location)

Aggregations

Location (alien4cloud.model.orchestrators.locations.Location)80 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)31 ApiOperation (io.swagger.annotations.ApiOperation)30 List (java.util.List)28 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)28 Audit (alien4cloud.audit.annotation.Audit)21 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)15 LocationService (alien4cloud.orchestrators.locations.services.LocationService)13 Set (java.util.Set)13 Collectors (java.util.stream.Collectors)13 Application (alien4cloud.model.application.Application)12 AbstractLocationResourceTemplate (alien4cloud.model.orchestrators.locations.AbstractLocationResourceTemplate)12 RestResponse (alien4cloud.rest.model.RestResponse)12 RestResponseBuilder (alien4cloud.rest.model.RestResponseBuilder)12 GroupDTO (alien4cloud.rest.orchestrator.model.GroupDTO)12 UserDTO (alien4cloud.rest.orchestrator.model.UserDTO)12 Resource (javax.annotation.Resource)12 ApplicationEnvironmentService (alien4cloud.application.ApplicationEnvironmentService)11 ResourcePermissionService (alien4cloud.authorization.ResourcePermissionService)11 ApplicationEnvironmentAuthorizationUpdateRequest (alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest)11