Search in sources :

Example 6 with RhIdPrincipal

use of com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal in project notifications-backend by RedHatInsights.

the class EndpointResource method getOrCreateEmailSubscriptionEndpoint.

@POST
@Path("/system/email_subscription")
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@RolesAllowed(ConsoleIdentityProvider.RBAC_READ_INTEGRATIONS_ENDPOINTS)
@Transactional
public Endpoint getOrCreateEmailSubscriptionEndpoint(@Context SecurityContext sec, @NotNull @Valid RequestEmailSubscriptionProperties requestProps) {
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    if (requestProps.getGroupId() != null && requestProps.isOnlyAdmins()) {
        throw new BadRequestException(String.format("Cannot use RBAC groups and only admins in the same endpoint"));
    }
    if (requestProps.getGroupId() != null) {
        boolean isValid = rbacGroupValidator.validate(requestProps.getGroupId(), principal.getIdentity().rawIdentity);
        if (!isValid) {
            throw new BadRequestException(String.format("Invalid RBAC group identified with id %s", requestProps.getGroupId()));
        }
    }
    // Prevent from creating not public facing properties
    EmailSubscriptionProperties properties = new EmailSubscriptionProperties();
    properties.setOnlyAdmins(requestProps.isOnlyAdmins());
    properties.setGroupId(requestProps.getGroupId());
    return endpointRepository.getOrCreateEmailSubscriptionEndpoint(principal.getAccount(), properties);
}
Also used : RequestEmailSubscriptionProperties(com.redhat.cloud.notifications.routers.models.RequestEmailSubscriptionProperties) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Transactional(javax.transaction.Transactional)

Example 7 with RhIdPrincipal

use of com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal in project notifications-backend by RedHatInsights.

the class EndpointResource method getEndpoint.

@GET
@Path("/{id}")
@Produces(APPLICATION_JSON)
@RolesAllowed(ConsoleIdentityProvider.RBAC_READ_INTEGRATIONS_ENDPOINTS)
public Endpoint getEndpoint(@Context SecurityContext sec, @PathParam("id") UUID id) {
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    Endpoint endpoint = endpointRepository.getEndpoint(principal.getAccount(), id);
    if (endpoint == null) {
        throw new NotFoundException();
    } else {
        return endpoint;
    }
}
Also used : Endpoint(com.redhat.cloud.notifications.models.Endpoint) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 8 with RhIdPrincipal

use of com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal in project notifications-backend by RedHatInsights.

the class EndpointResource method getDetailedEndpointHistory.

@GET
@Path("/{id}/history/{history_id}/details")
@Produces(APPLICATION_JSON)
@RolesAllowed(ConsoleIdentityProvider.RBAC_READ_INTEGRATIONS_ENDPOINTS)
@APIResponse(responseCode = "200", content = @Content(schema = @Schema(type = SchemaType.STRING)))
public Response getDetailedEndpointHistory(@Context SecurityContext sec, @PathParam("id") UUID endpointId, @PathParam("history_id") UUID historyId) {
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    JsonObject json = notificationRepository.getNotificationDetails(principal.getAccount(), endpointId, historyId);
    if (json == null) {
        // Maybe 404 should only be returned if history_id matches nothing? Otherwise 204
        throw new NotFoundException();
    } else {
        if (json.isEmpty()) {
            return Response.noContent().build();
        }
        return Response.ok(json).build();
    }
}
Also used : RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) JsonObject(io.vertx.core.json.JsonObject) NotFoundException(javax.ws.rs.NotFoundException) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) APIResponse(org.eclipse.microprofile.openapi.annotations.responses.APIResponse) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 9 with RhIdPrincipal

use of com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal in project notifications-backend by RedHatInsights.

the class EndpointResource method getEndpoints.

@GET
@Produces(APPLICATION_JSON)
@RolesAllowed(ConsoleIdentityProvider.RBAC_READ_INTEGRATIONS_ENDPOINTS)
@Parameters({ @Parameter(name = "limit", in = ParameterIn.QUERY, description = "Number of items per page. If the value is 0, it will return all elements", schema = @Schema(type = SchemaType.INTEGER)), @Parameter(name = "pageNumber", in = ParameterIn.QUERY, description = "Page number. Starts at first page (0), if not specified starts at first page.", schema = @Schema(type = SchemaType.INTEGER)) })
public EndpointPage getEndpoints(@Context SecurityContext sec, @BeanParam Query query, @QueryParam("type") List<String> targetType, @QueryParam("active") Boolean activeOnly, @QueryParam("name") String name) {
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    List<Endpoint> endpoints;
    Long count;
    if (targetType != null && targetType.size() > 0) {
        Set<CompositeEndpointType> compositeType = targetType.stream().map(s -> {
            try {
                return CompositeEndpointType.fromString(s);
            } catch (IllegalArgumentException e) {
                throw new BadRequestException("Unknown endpoint type: [" + s + "]", e);
            }
        }).collect(Collectors.toSet());
        endpoints = endpointRepository.getEndpointsPerCompositeType(principal.getAccount(), name, compositeType, activeOnly, query);
        count = endpointRepository.getEndpointsCountPerCompositeType(principal.getAccount(), name, compositeType, activeOnly);
    } else {
        endpoints = endpointRepository.getEndpoints(principal.getAccount(), name, query);
        count = endpointRepository.getEndpointsCount(principal.getAccount(), name);
    }
    return new EndpointPage(endpoints, new HashMap<>(), new Meta(count));
}
Also used : RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) Endpoint(com.redhat.cloud.notifications.models.Endpoint) Path(javax.ws.rs.Path) SecurityContext(javax.ws.rs.core.SecurityContext) NotificationHistory(com.redhat.cloud.notifications.models.NotificationHistory) Valid(javax.validation.Valid) Parameters(org.eclipse.microprofile.openapi.annotations.parameters.Parameters) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) SchemaType(org.eclipse.microprofile.openapi.annotations.enums.SchemaType) EndpointPage(com.redhat.cloud.notifications.routers.models.EndpointPage) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) BadRequestException(javax.ws.rs.BadRequestException) APPLICATION_JSON(javax.ws.rs.core.MediaType.APPLICATION_JSON) APIResponse(org.eclipse.microprofile.openapi.annotations.responses.APIResponse) DELETE(javax.ws.rs.DELETE) Application(com.redhat.cloud.notifications.models.Application) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) EndpointProperties(com.redhat.cloud.notifications.models.EndpointProperties) Context(javax.ws.rs.core.Context) Transactional(javax.transaction.Transactional) RbacGroupValidator(com.redhat.cloud.notifications.auth.rbac.RbacGroupValidator) Set(java.util.Set) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) UUID(java.util.UUID) NotNull(javax.validation.constraints.NotNull) Collectors(java.util.stream.Collectors) BeanParam(javax.ws.rs.BeanParam) NotFoundException(javax.ws.rs.NotFoundException) RequestEmailSubscriptionProperties(com.redhat.cloud.notifications.routers.models.RequestEmailSubscriptionProperties) List(java.util.List) Response(javax.ws.rs.core.Response) EndpointType(com.redhat.cloud.notifications.models.EndpointType) ParameterIn(org.eclipse.microprofile.openapi.annotations.enums.ParameterIn) Parameter(org.eclipse.microprofile.openapi.annotations.parameters.Parameter) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) CompositeEndpointType(com.redhat.cloud.notifications.models.CompositeEndpointType) PathParam(javax.ws.rs.PathParam) EmailSubscriptionProperties(com.redhat.cloud.notifications.models.EmailSubscriptionProperties) GET(javax.ws.rs.GET) Logger(org.jboss.logging.Logger) Constants(com.redhat.cloud.notifications.Constants) EndpointRepository(com.redhat.cloud.notifications.db.repositories.EndpointRepository) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) BridgeAuth(com.redhat.cloud.notifications.openbridge.BridgeAuth) EmailSubscriptionRepository(com.redhat.cloud.notifications.db.repositories.EmailSubscriptionRepository) ConsoleIdentityProvider(com.redhat.cloud.notifications.auth.ConsoleIdentityProvider) Content(org.eclipse.microprofile.openapi.annotations.media.Content) EmailSubscriptionType(com.redhat.cloud.notifications.models.EmailSubscriptionType) ApplicationRepository(com.redhat.cloud.notifications.db.repositories.ApplicationRepository) Meta(com.redhat.cloud.notifications.routers.models.Meta) POST(javax.ws.rs.POST) RestClient(org.eclipse.microprofile.rest.client.inject.RestClient) Schema(org.eclipse.microprofile.openapi.annotations.media.Schema) Query(com.redhat.cloud.notifications.db.Query) BridgeApiService(com.redhat.cloud.notifications.openbridge.BridgeApiService) NotificationRepository(com.redhat.cloud.notifications.db.repositories.NotificationRepository) Bridge(com.redhat.cloud.notifications.openbridge.Bridge) MAX_NOTIFICATION_HISTORY_RESULTS(com.redhat.cloud.notifications.db.repositories.NotificationRepository.MAX_NOTIFICATION_HISTORY_RESULTS) PUT(javax.ws.rs.PUT) ConfigProperty(org.eclipse.microprofile.config.inject.ConfigProperty) CompositeEndpointType(com.redhat.cloud.notifications.models.CompositeEndpointType) EndpointPage(com.redhat.cloud.notifications.routers.models.EndpointPage) Meta(com.redhat.cloud.notifications.routers.models.Meta) Endpoint(com.redhat.cloud.notifications.models.Endpoint) RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) BadRequestException(javax.ws.rs.BadRequestException) RolesAllowed(javax.annotation.security.RolesAllowed) Parameters(org.eclipse.microprofile.openapi.annotations.parameters.Parameters) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 10 with RhIdPrincipal

use of com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal in project notifications-backend by RedHatInsights.

the class EndpointResource method createEndpoint.

@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@RolesAllowed(ConsoleIdentityProvider.RBAC_WRITE_INTEGRATIONS_ENDPOINTS)
@Transactional
public Endpoint createEndpoint(@Context SecurityContext sec, @NotNull @Valid Endpoint endpoint) {
    checkSystemEndpoint(endpoint.getType());
    RhIdPrincipal principal = (RhIdPrincipal) sec.getUserPrincipal();
    endpoint.setAccountId(principal.getAccount());
    if (endpoint.getProperties() == null) {
        throw new BadRequestException("Properties is required");
    }
    if (obEnabled) {
        // TODO NOTIF-429 - see similar in EndpointResources#createEndpoint
        String endpointSubType;
        if (endpoint.getSubType() != null) {
            endpointSubType = endpoint.getSubType();
        } else {
            if (endpoint.getType() == EndpointType.CAMEL) {
                endpointSubType = endpoint.getProperties(CamelProperties.class).getSubType();
            } else {
                // No Camel endpoint, so we can skip
                endpointSubType = "not-defined";
            }
        }
        if (endpointSubType != null && endpointSubType.equals("slack")) {
            CamelProperties properties = endpoint.getProperties(CamelProperties.class);
            String processorName = "p-" + endpoint.getAccountId() + "-" + UUID.randomUUID();
            properties.getExtras().put(OB_PROCESSOR_NAME, processorName);
            String processorId = null;
            try {
                processorId = setupOpenBridgeProcessor(endpoint, properties, processorName);
            } catch (Exception e) {
                LOGGER.warn("Processor setup failed: " + e.getMessage());
                throw new InternalServerErrorException("Can't set up the endpoint");
            }
            // TODO find a better place for these, that should not be
            // visible to users / OB actions
            // See also CamelTypeProcessor#callOpenBridge
            properties.getExtras().put(OB_PROCESSOR_ID, processorId);
        }
    }
    return endpointRepository.createEndpoint(endpoint);
}
Also used : RhIdPrincipal(com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal) CamelProperties(com.redhat.cloud.notifications.models.CamelProperties) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) NotFoundException(javax.ws.rs.NotFoundException) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Transactional(javax.transaction.Transactional)

Aggregations

RhIdPrincipal (com.redhat.cloud.notifications.auth.principal.rhid.RhIdPrincipal)15 Path (javax.ws.rs.Path)14 Produces (javax.ws.rs.Produces)13 RolesAllowed (javax.annotation.security.RolesAllowed)12 Transactional (javax.transaction.Transactional)10 GET (javax.ws.rs.GET)7 NotFoundException (javax.ws.rs.NotFoundException)7 APIResponse (org.eclipse.microprofile.openapi.annotations.responses.APIResponse)6 CompositeEndpointType (com.redhat.cloud.notifications.models.CompositeEndpointType)5 EndpointType (com.redhat.cloud.notifications.models.EndpointType)5 BadRequestException (javax.ws.rs.BadRequestException)5 Consumes (javax.ws.rs.Consumes)5 Application (com.redhat.cloud.notifications.models.Application)4 DELETE (javax.ws.rs.DELETE)4 POST (javax.ws.rs.POST)4 PUT (javax.ws.rs.PUT)4 CamelProperties (com.redhat.cloud.notifications.models.CamelProperties)3 Endpoint (com.redhat.cloud.notifications.models.Endpoint)3 InternalServerErrorException (javax.ws.rs.InternalServerErrorException)3 Response (javax.ws.rs.core.Response)3