Search in sources :

Example 11 with NonRetryableApplicationException

use of com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException in project bq-pii-classifier by GoogleCloudPlatform.

the class TaggerController method parseEvent.

private Operation parseEvent(PubSubEvent event) throws NonRetryableApplicationException {
    String defaultTrackingId = "0000000000000-z";
    // check if the message is sent from a Standard DLP inspection job
    if (event.getMessage().getAttributes() != null) {
        String dlpJobName = event.getMessage().getAttributes().getOrDefault("DlpJobName", "");
        if (!dlpJobName.isBlank()) {
            logger.logInfoWithTracker(defaultTrackingId, String.format("Parsed DlpJobName '%s'", dlpJobName));
            String trackingId = TrackingHelper.extractTrackingIdFromJobName(dlpJobName);
            String runId = TrackingHelper.parseRunIdAsPrefix(trackingId);
            Operation taggerRequest = new Operation(dlpJobName, runId, trackingId);
            logger.logInfoWithTracker(taggerRequest.getTrackingId(), String.format("Parsed Request from Standard DLP: %s", taggerRequest.toString()));
            return taggerRequest;
        }
    }
    try {
        String requestJsonString = event.getMessage().dataToUtf8String();
        // remove any escape characters (e.g. from Terraform
        requestJsonString = requestJsonString.replace("\\", "");
        logger.logInfoWithTracker(defaultTrackingId, String.format("Received payload: %s", requestJsonString));
        Operation taggerRequest = gson.fromJson(requestJsonString, Operation.class);
        logger.logInfoWithTracker(taggerRequest.getTrackingId(), String.format("Parsed Request from Dispatcher: %s", taggerRequest.toString()));
        return taggerRequest;
    } catch (Exception ex) {
        try {
            byte[] data = event.getMessage().getData();
            DataProfilePubSubMessage dataProfilePubSubMessage = DataProfilePubSubMessage.parseFrom(data);
            logger.logInfoWithTracker(defaultTrackingId, String.format("Parsed DataProfilePubSubMessage= '%s'", dataProfilePubSubMessage));
            TableSpec targetTable = TableSpec.fromFullResource(dataProfilePubSubMessage.getProfile().getFullResource());
            String runId = TrackingHelper.generateOneTimeTaggingSuffix();
            String trackingId = TrackingHelper.generateTrackingId(runId, targetTable.toSqlString());
            Operation taggerRequest = new Operation(targetTable.toSqlString(), runId, trackingId);
            logger.logInfoWithTracker(taggerRequest.getTrackingId(), String.format("Parsed Request from Auto DLP notifications : %s", taggerRequest.toString()));
            return taggerRequest;
        } catch (InvalidProtocolBufferException invalidProtocolBufferException) {
            throw new NonRetryableApplicationException(String.format("Couldn't parse PubSub event as Proto: %s : %s", invalidProtocolBufferException.getClass().getSimpleName(), invalidProtocolBufferException.getMessage()));
        }
    }
}
Also used : TableSpec(com.google.cloud.pso.bq_pii_classifier.entities.TableSpec) DataProfilePubSubMessage(com.google.cloud.pso.bq_pii_classifier.entities.dlp.DataProfilePubSubMessage) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) Operation(com.google.cloud.pso.bq_pii_classifier.entities.Operation) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException)

Example 12 with NonRetryableApplicationException

use of com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException in project bq-pii-classifier by GoogleCloudPlatform.

the class TaggerController method receiveMessage.

@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity receiveMessage(@RequestBody PubSubEvent requestBody) {
    String defaultTrackingId = "0000000000000-z";
    Operation taggerRequest = null;
    try {
        if (requestBody == null || requestBody.getMessage() == null) {
            String msg = "Bad Request: invalid message format";
            logger.logSevereWithTracker(defaultTrackingId, msg);
            throw new NonRetryableApplicationException("Request body or message is Null.");
        }
        // The pubsub message could come from different sources with different formats
        // 1. From the Dispatcher as JSON encoded "Operation" object (in standard and auto-dlp modes)
        // 2. From Standard DLP inspection job notifications (PubSub Actions) as a string message with the DLP job name
        // 3. From Auto DLP notifications (PubSub Actions) as Protobuf encoded "DataProfilePubSubMessage" (in AutoDLP mode only)
        // Here we try and parse it directly as an "Operation" object or indirectly via the "DataProfilePubSubMessage" proto
        taggerRequest = parseEvent(requestBody);
        BigQueryService bigQueryService = new BigQueryServiceImpl();
        // determine the Type of DLP FindingsReader based on env
        FindingsReader findingsReader = FindingsReaderFactory.getNewReader(FindingsReaderFactory.findReader(environment.getIsAutoDlpMode(), environment.getPromoteMixedTypes()), bigQueryService, environment.getProjectId(), environment.getDlpDataset(), environment.getIsAutoDlpMode() ? environment.getDlpTableAuto() : environment.getDlpTableStandard(), environment.getConfigViewDatasetDomainMap(), environment.getConfigViewProjectDomainMap(), environment.getConfigViewInfoTypePolicyTagsMap());
        Tagger tagger = new Tagger(environment.toConfig(), bigQueryService, findingsReader, new GCSPersistentSetImpl(environment.getGcsFlagsBucket()), "tagger-flags");
        tagger.execute(taggerRequest, requestBody.getMessage().getMessageId());
        return new ResponseEntity("Process completed successfully.", HttpStatus.OK);
    } catch (Exception e) {
        String trackingId = taggerRequest == null ? defaultTrackingId : taggerRequest.getTrackingId();
        return ControllerExceptionHelper.handleException(e, logger, trackingId);
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) BigQueryServiceImpl(com.google.cloud.pso.bq_pii_classifier.services.bq.BigQueryServiceImpl) BigQueryService(com.google.cloud.pso.bq_pii_classifier.services.bq.BigQueryService) Tagger(com.google.cloud.pso.bq_pii_classifier.functions.tagger.Tagger) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) FindingsReader(com.google.cloud.pso.bq_pii_classifier.services.findings.FindingsReader) Operation(com.google.cloud.pso.bq_pii_classifier.entities.Operation) GCSPersistentSetImpl(com.google.cloud.pso.bq_pii_classifier.services.set.GCSPersistentSetImpl) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 13 with NonRetryableApplicationException

use of com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException in project bq-pii-classifier by GoogleCloudPlatform.

the class InspectionDispatcherController method receiveMessage.

@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity receiveMessage(@RequestBody PubSubEvent requestBody) {
    String runId = TrackingHelper.generateInspectionRunId();
    String state = "";
    try {
        if (requestBody == null || requestBody.getMessage() == null) {
            String msg = "Bad Request: invalid message format";
            logger.logSevereWithTracker(runId, msg);
            throw new NonRetryableApplicationException("Request body or message is Null.");
        }
        String requestJsonString = requestBody.getMessage().dataToUtf8String();
        // remove any escape characters (e.g. from Terraform
        requestJsonString = requestJsonString.replace("\\", "");
        logger.logInfoWithTracker(runId, String.format("Received payload: %s", requestJsonString));
        BigQueryScope bqScope = gson.fromJson(requestJsonString, BigQueryScope.class);
        logger.logInfoWithTracker(runId, String.format("Parsed JSON input %s ", bqScope.toString()));
        Dispatcher dispatcher = new Dispatcher(environment.toConfig(), new BigQueryServiceImpl(), new PubSubServiceImpl(), new BigQueryScannerImpl(), new GCSPersistentSetImpl(environment.getGcsFlagsBucket()), "inspection-dispatcher-flags", runId);
        PubSubPublishResults results = dispatcher.execute(bqScope, requestBody.getMessage().getMessageId());
        state = String.format("Publishing results: %s SUCCESS MESSAGES and %s FAILED MESSAGES", results.getSuccessMessages().size(), results.getFailedMessages().size());
        logger.logInfoWithTracker(runId, state);
    } catch (Exception e) {
        logger.logNonRetryableExceptions(runId, e);
        state = String.format("ERROR '%s'", e.getMessage());
    }
    return new ResponseEntity(String.format("Process completed with state = %s", state), HttpStatus.OK);
}
Also used : BigQueryScannerImpl(com.google.cloud.pso.bq_pii_classifier.services.scan.BigQueryScannerImpl) ResponseEntity(org.springframework.http.ResponseEntity) BigQueryServiceImpl(com.google.cloud.pso.bq_pii_classifier.services.bq.BigQueryServiceImpl) PubSubPublishResults(com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubPublishResults) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) Dispatcher(com.google.cloud.pso.bq_pii_classifier.functions.dispatcher.Dispatcher) GCSPersistentSetImpl(com.google.cloud.pso.bq_pii_classifier.services.set.GCSPersistentSetImpl) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) BigQueryScope(com.google.cloud.pso.bq_pii_classifier.functions.dispatcher.BigQueryScope) PubSubServiceImpl(com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubServiceImpl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

NonRetryableApplicationException (com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException)13 FieldValueList (com.google.cloud.bigquery.FieldValueList)6 Job (com.google.cloud.bigquery.Job)6 TableResult (com.google.cloud.bigquery.TableResult)6 BigQueryServiceImpl (com.google.cloud.pso.bq_pii_classifier.services.bq.BigQueryServiceImpl)5 ArrayList (java.util.ArrayList)5 Operation (com.google.cloud.pso.bq_pii_classifier.entities.Operation)4 GCSPersistentSetImpl (com.google.cloud.pso.bq_pii_classifier.services.set.GCSPersistentSetImpl)4 ResponseEntity (org.springframework.http.ResponseEntity)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 PubSubPublishResults (com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubPublishResults)3 PubSubServiceImpl (com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubServiceImpl)3 TablePolicyTags (com.google.cloud.pso.bq_pii_classifier.entities.TablePolicyTags)2 TableSpec (com.google.cloud.pso.bq_pii_classifier.entities.TableSpec)2 BigQueryScope (com.google.cloud.pso.bq_pii_classifier.functions.dispatcher.BigQueryScope)2 Dispatcher (com.google.cloud.pso.bq_pii_classifier.functions.dispatcher.Dispatcher)2 BigQueryService (com.google.cloud.pso.bq_pii_classifier.services.bq.BigQueryService)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 HashMap (java.util.HashMap)2 DispatcherType (com.google.cloud.pso.bq_pii_classifier.entities.DispatcherType)1