Search in sources :

Example 1 with NewReporter

use of io.gravitee.am.service.model.NewReporter in project gravitee-access-management by gravitee-io.

the class AuditReporterManagerImpl method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    logger.info("Register event listener for reporter events for the management API");
    eventManager.subscribeForEvents(this, ReporterEvent.class);
    // init noOpReporter
    noOpReporter = new NoOpReporter();
    // init internal reporter (organization reporter)
    NewReporter organizationReporter = reporterService.createInternal();
    logger.info("Initializing internal " + organizationReporter.getType() + " audit reporter");
    internalReporter = reporterPluginManager.create(organizationReporter.getType(), organizationReporter.getConfiguration(), null);
    logger.info("Internal audit " + organizationReporter.getType() + " reporter initialized");
    logger.info("Initializing audit reporters");
    reporterService.findAll().blockingForEach(reporter -> {
        logger.info("Initializing audit reporter : {} for domain {}", reporter.getName(), reporter.getDomain());
        try {
            AuditReporterLauncher launcher = new AuditReporterLauncher(reporter);
            domainService.findById(reporter.getDomain()).flatMapSingle(domain -> {
                if (ReferenceType.ENVIRONMENT.equals(domain.getReferenceType())) {
                    return environmentService.findById(domain.getReferenceId()).map(env -> new GraviteeContext(env.getOrganizationId(), env.getId(), domain.getId()));
                } else {
                    // currently domain is only linked to domainEnv
                    return Single.error(new EnvironmentNotFoundException("Domain " + reporter.getDomain() + " should be lined to an Environment"));
                }
            }).subscribeOn(Schedulers.io()).subscribe(launcher);
        } catch (Exception ex) {
            logger.error("An error has occurred while loading audit reporter: {} [{}]", reporter.getName(), reporter.getType(), ex);
            removeReporter(reporter.getId());
        }
    });
    // deploy internal reporter verticle
    deployReporterVerticle(asList(new EventBusReporterWrapper(vertx, internalReporter)));
}
Also used : GraviteeContext(io.gravitee.am.common.utils.GraviteeContext) EnvironmentNotFoundException(io.gravitee.am.service.exception.EnvironmentNotFoundException) NoOpReporter(io.gravitee.am.reporter.api.provider.NoOpReporter) EventBusReporterWrapper(io.gravitee.am.service.reporter.vertx.EventBusReporterWrapper) NewReporter(io.gravitee.am.service.model.NewReporter) ReporterNotFoundForDomainException(io.gravitee.am.service.exception.ReporterNotFoundForDomainException) EnvironmentNotFoundException(io.gravitee.am.service.exception.EnvironmentNotFoundException)

Example 2 with NewReporter

use of io.gravitee.am.service.model.NewReporter in project gravitee-access-management by gravitee-io.

the class ReporterServiceImpl method createMongoReporter.

private NewReporter createMongoReporter(String domain) {
    NewReporter newReporter = new NewReporter();
    newReporter.setId(RandomString.generate());
    newReporter.setEnabled(true);
    newReporter.setName("MongoDB Reporter");
    newReporter.setType("mongodb");
    newReporter.setConfiguration(createReporterConfig(domain));
    return newReporter;
}
Also used : NewReporter(io.gravitee.am.service.model.NewReporter)

Example 3 with NewReporter

use of io.gravitee.am.service.model.NewReporter in project gravitee-access-management by gravitee-io.

the class ReportersResource method create.

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create a reporter for a security domain", notes = "User must have the DOMAIN_REPORTER[CREATE] permission on the specified domain " + "or DOMAIN_REPORTER[CREATE] permission on the specified environment " + "or DOMAIN_REPORTER[CREATE] permission on the specified organization.")
@ApiResponses({ @ApiResponse(code = 201, message = "Reporter created for a security domain", response = Reporter.class), @ApiResponse(code = 500, message = "Internal server error") })
public void create(@PathParam("organizationId") String organizationId, @PathParam("environmentId") String environmentId, @PathParam("domain") String domain, NewReporter newReporter, @Suspended final AsyncResponse response) {
    User authenticatedUser = getAuthenticatedUser();
    checkAnyPermission(organizationId, environmentId, domain, Permission.DOMAIN_REPORTER, Acl.CREATE).andThen(domainService.findById(domain).switchIfEmpty(Maybe.error(new DomainNotFoundException(domain))).flatMapSingle(irrelevant -> reporterService.create(domain, newReporter, authenticatedUser, false)).map(reporter -> response.resume(Response.created(URI.create("/organizations/" + organizationId + "/environments/" + environmentId + "/domains/" + domain + "/reporters/" + reporter.getId())).entity(reporter).build()))).subscribe(response::resume, response::resume);
}
Also used : ReporterServiceProxy(io.gravitee.am.management.service.ReporterServiceProxy) Permission(io.gravitee.am.model.permissions.Permission) Maybe(io.reactivex.Maybe) DomainService(io.gravitee.am.service.DomainService) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractResource(io.gravitee.am.management.handlers.management.api.resources.AbstractResource) ApiResponses(io.swagger.annotations.ApiResponses) Single(io.reactivex.Single) NewReporter(io.gravitee.am.service.model.NewReporter) Acl(io.gravitee.am.model.Acl) ApiOperation(io.swagger.annotations.ApiOperation) User(io.gravitee.am.identityprovider.api.User) Api(io.swagger.annotations.Api) URI(java.net.URI) Context(javax.ws.rs.core.Context) AsyncResponse(javax.ws.rs.container.AsyncResponse) Reporter(io.gravitee.am.model.Reporter) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException) Suspended(javax.ws.rs.container.Suspended) Collectors(java.util.stream.Collectors) MediaType(io.gravitee.common.http.MediaType) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ApiResponse(io.swagger.annotations.ApiResponse) ResourceContext(javax.ws.rs.container.ResourceContext) User(io.gravitee.am.identityprovider.api.User) DomainNotFoundException(io.gravitee.am.service.exception.DomainNotFoundException) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with NewReporter

use of io.gravitee.am.service.model.NewReporter in project gravitee-access-management by gravitee-io.

the class ReporterServiceImpl method create.

@Override
public Single<Reporter> create(String domain, NewReporter newReporter, User principal, boolean system) {
    LOGGER.debug("Create a new reporter {} for domain {}", newReporter, domain);
    Reporter reporter = new Reporter();
    reporter.setId(newReporter.getId() == null ? RandomString.generate() : newReporter.getId());
    reporter.setEnabled(newReporter.isEnabled());
    reporter.setDomain(domain);
    reporter.setName(newReporter.getName());
    reporter.setSystem(system);
    reporter.setType(newReporter.getType());
    // currently only audit logs
    reporter.setDataType("AUDIT");
    reporter.setConfiguration(newReporter.getConfiguration());
    reporter.setCreatedAt(new Date());
    reporter.setUpdatedAt(reporter.getCreatedAt());
    return checkReporterConfiguration(reporter).flatMap(ignore -> reporterRepository.create(reporter)).flatMap(reporter1 -> {
        // create event for sync process
        Event event = new Event(Type.REPORTER, new Payload(reporter1.getId(), ReferenceType.DOMAIN, reporter1.getDomain(), Action.CREATE));
        return eventService.create(event).flatMap(__ -> Single.just(reporter1));
    }).onErrorResumeNext(ex -> {
        LOGGER.error("An error occurs while trying to create a reporter", ex);
        String message = "An error occurs while trying to create a reporter. ";
        if (ex instanceof ReporterConfigurationException) {
            message += ex.getMessage();
        }
        return Single.error(new TechnicalManagementException(message, ex));
    });
}
Also used : Json(io.vertx.core.json.Json) Primary(org.springframework.context.annotation.Primary) MessageDigest(java.security.MessageDigest) Date(java.util.Date) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) ReporterRepository(io.gravitee.am.repository.management.api.ReporterRepository) AuditService(io.gravitee.am.service.AuditService) Single(io.reactivex.Single) Event(io.gravitee.am.model.common.event.Event) ReporterService(io.gravitee.am.service.ReporterService) NewReporter(io.gravitee.am.service.model.NewReporter) ArrayList(java.util.ArrayList) Type(io.gravitee.am.common.event.Type) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) BackendConfigurationUtils.getMongoDatabaseName(io.gravitee.am.service.utils.BackendConfigurationUtils.getMongoDatabaseName) Flowable(io.reactivex.Flowable) User(io.gravitee.am.identityprovider.api.User) AuditBuilder(io.gravitee.am.service.reporter.builder.AuditBuilder) ReferenceType(io.gravitee.am.model.ReferenceType) EventService(io.gravitee.am.service.EventService) JsonObject(io.vertx.core.json.JsonObject) Action(io.gravitee.am.common.event.Action) Logger(org.slf4j.Logger) ReporterAuditBuilder(io.gravitee.am.service.reporter.builder.management.ReporterAuditBuilder) BaseEncoding(com.google.common.io.BaseEncoding) EventType(io.gravitee.am.common.audit.EventType) Reporter(io.gravitee.am.model.Reporter) AbstractManagementException(io.gravitee.am.service.exception.AbstractManagementException) Collectors(java.util.stream.Collectors) RandomString(io.gravitee.am.common.utils.RandomString) Component(org.springframework.stereotype.Component) List(java.util.List) ReporterNotFoundException(io.gravitee.am.service.exception.ReporterNotFoundException) UpdateReporter(io.gravitee.am.service.model.UpdateReporter) Payload(io.gravitee.am.model.common.event.Payload) Environment(org.springframework.core.env.Environment) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Optional(java.util.Optional) Lazy(org.springframework.context.annotation.Lazy) ReporterConfigurationException(io.gravitee.am.service.exception.ReporterConfigurationException) StringUtils(org.springframework.util.StringUtils) NewReporter(io.gravitee.am.service.model.NewReporter) Reporter(io.gravitee.am.model.Reporter) UpdateReporter(io.gravitee.am.service.model.UpdateReporter) Event(io.gravitee.am.model.common.event.Event) Payload(io.gravitee.am.model.common.event.Payload) RandomString(io.gravitee.am.common.utils.RandomString) ReporterConfigurationException(io.gravitee.am.service.exception.ReporterConfigurationException) Date(java.util.Date) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException)

Example 5 with NewReporter

use of io.gravitee.am.service.model.NewReporter in project gravitee-access-management by gravitee-io.

the class ReporterServiceImpl method createDefault.

@Override
public Single<Reporter> createDefault(String domain) {
    LOGGER.debug("Create default reporter for domain {}", domain);
    NewReporter newReporter = createInternal(domain);
    if (newReporter == null) {
        return Single.error(new ReporterNotFoundException("Reporter type " + this.environment.getProperty("management.type") + " not found"));
    }
    return create(domain, newReporter);
}
Also used : NewReporter(io.gravitee.am.service.model.NewReporter) ReporterNotFoundException(io.gravitee.am.service.exception.ReporterNotFoundException)

Aggregations

NewReporter (io.gravitee.am.service.model.NewReporter)6 User (io.gravitee.am.identityprovider.api.User)2 Reporter (io.gravitee.am.model.Reporter)2 ReporterNotFoundException (io.gravitee.am.service.exception.ReporterNotFoundException)2 Maybe (io.reactivex.Maybe)2 Single (io.reactivex.Single)2 Collectors (java.util.stream.Collectors)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 BaseEncoding (com.google.common.io.BaseEncoding)1 EventType (io.gravitee.am.common.audit.EventType)1 Action (io.gravitee.am.common.event.Action)1 Type (io.gravitee.am.common.event.Type)1 GraviteeContext (io.gravitee.am.common.utils.GraviteeContext)1 RandomString (io.gravitee.am.common.utils.RandomString)1 AbstractResource (io.gravitee.am.management.handlers.management.api.resources.AbstractResource)1 ReporterServiceProxy (io.gravitee.am.management.service.ReporterServiceProxy)1 Acl (io.gravitee.am.model.Acl)1 ReferenceType (io.gravitee.am.model.ReferenceType)1 Event (io.gravitee.am.model.common.event.Event)1 Payload (io.gravitee.am.model.common.event.Payload)1