Search in sources :

Example 71 with Location

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

the class LocationSecurityController method getAuthorizedEnvironmentsAndEnvTypePerApplication.

/**
 * List all environments per application authorised to access the location.
 *
 * @return list of all environments per application.
 */
@ApiOperation(value = "List all applications,environments and environment types authorized to access the location", notes = "Only user with ADMIN role can list authorized applications,environments and environment types for the location.")
@RequestMapping(value = "/environmentsPerApplication", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
public RestResponse<List<ApplicationEnvironmentAuthorizationDTO>> getAuthorizedEnvironmentsAndEnvTypePerApplication(@PathVariable String orchestratorId, @PathVariable String locationId) {
    Location location = locationService.getLocation(orchestratorId, locationId);
    List<Application> applicationsRelatedToEnvironment = Lists.newArrayList();
    List<Application> applicationsRelatedToEnvironmentType = Lists.newArrayList();
    List<ApplicationEnvironment> environments = Lists.newArrayList();
    List<Application> applications = Lists.newArrayList();
    List<String> environmentTypes = Lists.newArrayList();
    if (location.getEnvironmentPermissions() != null && location.getEnvironmentPermissions().size() > 0) {
        environments = alienDAO.findByIds(ApplicationEnvironment.class, location.getEnvironmentPermissions().keySet().toArray(new String[location.getEnvironmentPermissions().size()]));
        Set<String> environmentApplicationIds = environments.stream().map(ae -> new String(ae.getApplicationId())).collect(Collectors.toSet());
        applicationsRelatedToEnvironment = alienDAO.findByIds(Application.class, environmentApplicationIds.toArray(new String[environmentApplicationIds.size()]));
    }
    if (location.getEnvironmentTypePermissions() != null && location.getEnvironmentTypePermissions().size() > 0) {
        environmentTypes.addAll(location.getEnvironmentTypePermissions().keySet());
        Set<String> environmentTypeApplicationIds = environmentTypes.stream().map(envType -> new String(envType.split(":")[0])).collect(Collectors.toSet());
        applicationsRelatedToEnvironmentType = alienDAO.findByIds(Application.class, environmentTypeApplicationIds.toArray(new String[environmentTypeApplicationIds.size()]));
    }
    if (location.getApplicationPermissions() != null && location.getApplicationPermissions().size() > 0) {
        applications = alienDAO.findByIds(Application.class, location.getApplicationPermissions().keySet().toArray(new String[location.getApplicationPermissions().size()]));
    }
    List<ApplicationEnvironmentAuthorizationDTO> result = ApplicationEnvironmentAuthorizationDTO.buildDTOs(applicationsRelatedToEnvironment, applicationsRelatedToEnvironmentType, environments, applications, environmentTypes);
    return RestResponseBuilder.<List<ApplicationEnvironmentAuthorizationDTO>>builder().data(result).build();
}
Also used : IntStream(java.util.stream.IntStream) PathVariable(org.springframework.web.bind.annotation.PathVariable) Lists(org.elasticsearch.common.collect.Lists) RequestParam(org.springframework.web.bind.annotation.RequestParam) 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) ApiParam(io.swagger.annotations.ApiParam) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) IdsFilterBuilder(org.elasticsearch.index.query.IdsFilterBuilder) User(alien4cloud.security.model.User) 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) Api(io.swagger.annotations.Api) MapUtils(org.apache.commons.collections4.MapUtils) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) FilterBuilders(org.elasticsearch.index.query.FilterBuilders) MediaType(org.springframework.http.MediaType) Resource(javax.annotation.Resource) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) IGenericSearchDAO(alien4cloud.dao.IGenericSearchDAO) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) IAlienGroupDao(alien4cloud.security.groups.IAlienGroupDao) IAlienUserDao(alien4cloud.security.users.IAlienUserDao) List(java.util.List) GroupDTO(alien4cloud.rest.orchestrator.model.GroupDTO) Group(alien4cloud.security.model.Group) UserDTO(alien4cloud.rest.orchestrator.model.UserDTO) ApplicationEnvironmentAuthorizationUpdateRequest(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest) ApplicationEnvironmentAuthorizationDTO(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationDTO) List(java.util.List) Application(alien4cloud.model.application.Application) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) Location(alien4cloud.model.orchestrators.locations.Location) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 72 with Location

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

the class LocationSecurityController method getAuthorizedEnvironmentsAndEnvTypesPerApplicationPaginated.

/**
 * search applications,environments and environment types authorised to access the location.
 *
 * @return {@link RestResponse} that contains a {@link GetMultipleDataResult} of {@link GroupDTO}..
 */
@ApiOperation(value = "List all applications,environments and environment types authorized to access the location", notes = "Only user with ADMIN role can list authorized applications,environments and environment types to the location.")
@RequestMapping(value = "/applications/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
public RestResponse<GetMultipleDataResult<ApplicationEnvironmentAuthorizationDTO>> getAuthorizedEnvironmentsAndEnvTypesPerApplicationPaginated(@PathVariable String orchestratorId, @PathVariable String locationId, @ApiParam(value = "Text Query to search.") @RequestParam(required = false) String query, @ApiParam(value = "Query from the given index.") @RequestParam(required = false, defaultValue = "0") int from, @ApiParam(value = "Maximum number of results to retrieve.") @RequestParam(required = false, defaultValue = "20") int size) {
    Location location = locationService.getLocation(orchestratorId, locationId);
    List<Application> applicationsRelatedToEnvironment = Lists.newArrayList();
    List<Application> applicationsRelatedToEnvironmentType = Lists.newArrayList();
    List<ApplicationEnvironment> environments = Lists.newArrayList();
    List<String> environmentTypes = Lists.newArrayList();
    List<Application> applications = Lists.newArrayList();
    // we get all authorized applications and environment to not favor the one of them
    if (MapUtils.isNotEmpty(location.getEnvironmentPermissions())) {
        environments = alienDAO.findByIds(ApplicationEnvironment.class, location.getEnvironmentPermissions().keySet().toArray(new String[location.getEnvironmentPermissions().size()]));
        Set<String> environmentApplicationIds = environments.stream().map(ae -> new String(ae.getApplicationId())).collect(Collectors.toSet());
        applicationsRelatedToEnvironment = alienDAO.findByIds(Application.class, environmentApplicationIds.toArray(new String[environmentApplicationIds.size()]));
    }
    if (MapUtils.isNotEmpty(location.getEnvironmentTypePermissions())) {
        environmentTypes.addAll(location.getEnvironmentTypePermissions().keySet());
        Set<String> environmentTypeApplicationIds = Sets.newHashSet();
        for (String envType : safe(location.getEnvironmentTypePermissions()).keySet()) {
            environmentTypeApplicationIds.add(envType.split(":")[0]);
        }
        applicationsRelatedToEnvironmentType = alienDAO.findByIds(Application.class, environmentTypeApplicationIds.toArray(new String[environmentTypeApplicationIds.size()]));
    }
    if (MapUtils.isNotEmpty(location.getApplicationPermissions())) {
        applications = alienDAO.findByIds(Application.class, location.getApplicationPermissions().keySet().toArray(new String[location.getApplicationPermissions().size()]));
    }
    List<ApplicationEnvironmentAuthorizationDTO> allDTOs = ApplicationEnvironmentAuthorizationDTO.buildDTOs(applicationsRelatedToEnvironment, applicationsRelatedToEnvironmentType, environments, applications, environmentTypes);
    int to = (from + size < allDTOs.size()) ? from + size : allDTOs.size();
    allDTOs = IntStream.range(from, to).mapToObj(allDTOs::get).collect(Collectors.toList());
    List<String> ids = allDTOs.stream().map(appEnvDTO -> appEnvDTO.getApplication().getId()).collect(Collectors.toList());
    IdsFilterBuilder idFilters = FilterBuilders.idsFilter().ids(ids.toArray(new String[ids.size()]));
    GetMultipleDataResult<Application> tempResult = alienDAO.search(Application.class, query, null, idFilters, null, from, to, "id", false);
    return RestResponseBuilder.<GetMultipleDataResult<ApplicationEnvironmentAuthorizationDTO>>builder().data(ApplicationEnvironmentAuthorizationDTO.convert(tempResult, allDTOs)).build();
}
Also used : IntStream(java.util.stream.IntStream) PathVariable(org.springframework.web.bind.annotation.PathVariable) Lists(org.elasticsearch.common.collect.Lists) RequestParam(org.springframework.web.bind.annotation.RequestParam) 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) ApiParam(io.swagger.annotations.ApiParam) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) IdsFilterBuilder(org.elasticsearch.index.query.IdsFilterBuilder) User(alien4cloud.security.model.User) 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) Api(io.swagger.annotations.Api) MapUtils(org.apache.commons.collections4.MapUtils) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) FilterBuilders(org.elasticsearch.index.query.FilterBuilders) MediaType(org.springframework.http.MediaType) Resource(javax.annotation.Resource) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) Set(java.util.Set) IGenericSearchDAO(alien4cloud.dao.IGenericSearchDAO) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) IAlienGroupDao(alien4cloud.security.groups.IAlienGroupDao) IAlienUserDao(alien4cloud.security.users.IAlienUserDao) List(java.util.List) GroupDTO(alien4cloud.rest.orchestrator.model.GroupDTO) Group(alien4cloud.security.model.Group) UserDTO(alien4cloud.rest.orchestrator.model.UserDTO) ApplicationEnvironmentAuthorizationUpdateRequest(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationUpdateRequest) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) IdsFilterBuilder(org.elasticsearch.index.query.IdsFilterBuilder) ApplicationEnvironmentAuthorizationDTO(alien4cloud.rest.orchestrator.model.ApplicationEnvironmentAuthorizationDTO) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) Application(alien4cloud.model.application.Application) Location(alien4cloud.model.orchestrators.locations.Location) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 73 with Location

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

the class LocationController method getSecretConfigurations.

private SecretProviderConfigurationsDTO getSecretConfigurations(Location location) {
    Set<String> availablePlugins = secretProviderService.getAvailablePlugins();
    Map<String, Map<String, Object>> genericFormDescriptionByPluginName = availablePlugins.stream().collect(Collectors.toMap(Function.identity(), pluginName -> pojoFormDescriptorGenerator.generateDescriptor(secretProviderService.getPluginConfigurationDescriptor(pluginName))));
    SecretProviderConfigurationsDTO dto = new SecretProviderConfigurationsDTO();
    dto.setCurrentConfiguration(location.getSecretProviderConfiguration());
    dto.setGenericFormByPluginName(genericFormDescriptionByPluginName);
    return dto;
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) LocationService(alien4cloud.orchestrators.locations.services.LocationService) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ApiParam(io.swagger.annotations.ApiParam) Function(java.util.function.Function) ReflectionUtil(alien4cloud.utils.ReflectionUtil) Location(alien4cloud.model.orchestrators.locations.Location) Inject(javax.inject.Inject) Valid(javax.validation.Valid) RequestBody(org.springframework.web.bind.annotation.RequestBody) ApiOperation(io.swagger.annotations.ApiOperation) Lists(com.google.common.collect.Lists) Audit(alien4cloud.audit.annotation.Audit) RestResponseBuilder(alien4cloud.rest.model.RestResponseBuilder) RestResponse(alien4cloud.rest.model.RestResponse) Map(java.util.Map) SecretProviderService(org.alien4cloud.secret.services.SecretProviderService) ILocationResourceService(alien4cloud.orchestrators.locations.services.ILocationResourceService) Api(io.swagger.annotations.Api) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) SecretProviderConfiguration(alien4cloud.model.secret.SecretProviderConfiguration) UpdateLocationRequest(alien4cloud.rest.orchestrator.model.UpdateLocationRequest) MediaType(org.springframework.http.MediaType) Resource(javax.annotation.Resource) SecretProviderConfigurationsDTO(alien4cloud.rest.secret.model.SecretProviderConfigurationsDTO) Set(java.util.Set) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) CreateLocationRequest(alien4cloud.rest.orchestrator.model.CreateLocationRequest) Collectors(java.util.stream.Collectors) RestController(org.springframework.web.bind.annotation.RestController) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) NotEmpty(org.hibernate.validator.constraints.NotEmpty) PojoFormDescriptorGenerator(alien4cloud.ui.form.PojoFormDescriptorGenerator) Authorization(io.swagger.annotations.Authorization) LocationDTO(alien4cloud.rest.orchestrator.model.LocationDTO) SecretProviderConfigurationsDTO(alien4cloud.rest.secret.model.SecretProviderConfigurationsDTO) Map(java.util.Map)

Example 74 with Location

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

the class LocationModifiersController method add.

@ApiOperation(value = "Add a modifier to a location.", authorizations = { @Authorization("ADMIN") })
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("hasAuthority('ADMIN')")
@Audit
public RestResponse<Void> add(@ApiParam(value = "Id of the location", required = true) @PathVariable String locationId, @ApiParam(value = "The location modifier to add", required = true) @Valid @RequestBody LocationModifierReference locationModifierReference) {
    Location location = locationService.getOrFail(locationId);
    locationModifierService.add(location, locationModifierReference);
    return RestResponseBuilder.<Void>builder().build();
}
Also used : 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 75 with Location

use of alien4cloud.model.orchestrators.locations.Location 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

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