Search in sources :

Example 1 with Finding

use of com.google.privacy.dlp.v2.Finding in project java-docs-samples by GoogleCloudPlatform.

the class Inspect method inspectString.

/**
 * [START dlp_inspect_string] Inspect a text for given InfoTypes
 *
 * @param string String to instpect
 * @param minLikelihood The minimum likelihood required before returning a match
 * @param maxFindings The maximum number of findings to report (0 = server maximum)
 * @param infoTypes The infoTypes of information to match
 * @param includeQuote Whether to include the matching string
 * @param projectId Google Cloud project ID
 */
private static void inspectString(String string, Likelihood minLikelihood, int maxFindings, List<InfoType> infoTypes, boolean includeQuote, String projectId) {
    // instantiate a client
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        FindingLimits findingLimits = FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).setIncludeQuote(includeQuote).build();
        ByteContentItem byteContentItem = ByteContentItem.newBuilder().setType(ByteContentItem.BytesType.TEXT_UTF8).setData(ByteString.copyFromUtf8(string)).build();
        ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
        InspectContentRequest request = InspectContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectConfig(inspectConfig).setItem(contentItem).build();
        InspectContentResponse response = dlpServiceClient.inspectContent(request);
        if (response.getResult().getFindingsCount() > 0) {
            System.out.println("Findings: ");
            for (Finding finding : response.getResult().getFindingsList()) {
                if (includeQuote) {
                    System.out.print("\tQuote: " + finding.getQuote());
                }
                System.out.print("\tInfo type: " + finding.getInfoType().getName());
                System.out.println("\tLikelihood: " + finding.getLikelihood());
            }
        } else {
            System.out.println("No findings.");
        }
    } catch (Exception e) {
        System.out.println("Error in inspectString: " + e.getMessage());
    }
}
Also used : FindingLimits(com.google.privacy.dlp.v2.InspectConfig.FindingLimits) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) InspectContentResponse(com.google.privacy.dlp.v2.InspectContentResponse) Finding(com.google.privacy.dlp.v2.Finding) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) InspectContentRequest(com.google.privacy.dlp.v2.InspectContentRequest) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ContentItem(com.google.privacy.dlp.v2.ContentItem) ParseException(org.apache.commons.cli.ParseException)

Example 2 with Finding

use of com.google.privacy.dlp.v2.Finding in project java-docs-samples by GoogleCloudPlatform.

the class QuickStart method main.

/**
 * Quick start to DLP API : inspects a given string for an InfoType.
 */
public static void main(String[] args) throws Exception {
    // string to inspect
    String text = "His name was Robert Frost";
    // The minimum likelihood required before returning a match:
    // LIKELIHOOD_UNSPECIFIED, VERY_UNLIKELY, UNLIKELY, POSSIBLE, LIKELY, VERY_LIKELY, UNRECOGNIZED
    Likelihood minLikelihood = Likelihood.POSSIBLE;
    // The maximum number of findings to report (0 = server maximum)
    int maxFindings = 0;
    // The infoTypes of information to match
    List<InfoType> infoTypes = Arrays.asList(InfoType.newBuilder().setName("PERSON_NAME").build(), InfoType.newBuilder().setName("US_STATE").build());
    // Whether to include the matching string
    boolean includeQuote = true;
    // instantiate a client
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        InspectConfig.FindingLimits findingLimits = InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerItem(maxFindings).build();
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).setIncludeQuote(includeQuote).build();
        ByteContentItem byteContentItem = ByteContentItem.newBuilder().setType(ByteContentItem.BytesType.TEXT_UTF8).setData(ByteString.copyFromUtf8(text)).build();
        ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
        String projectId = ServiceOptions.getDefaultProjectId();
        InspectContentRequest request = InspectContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectConfig(inspectConfig).setItem(contentItem).build();
        // Inspect the text for info types
        InspectContentResponse response = dlpServiceClient.inspectContent(request);
        InspectResult result = response.getResult();
        if (result.getFindingsCount() > 0) {
            System.out.println("Findings: ");
            for (Finding finding : result.getFindingsList()) {
                if (includeQuote) {
                    System.out.print("\tQuote: " + finding.getQuote());
                }
                System.out.print("\tInfo type: " + finding.getInfoType().getName());
                System.out.println("\tLikelihood: " + finding.getLikelihood());
            }
        } else {
            System.out.println("No findings.");
        }
    } catch (Exception e) {
        System.out.println("Error in inspectString: " + e.getMessage());
    }
}
Also used : Likelihood(com.google.privacy.dlp.v2.Likelihood) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ByteString(com.google.protobuf.ByteString) InspectContentRequest(com.google.privacy.dlp.v2.InspectContentRequest) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) InspectContentResponse(com.google.privacy.dlp.v2.InspectContentResponse) Finding(com.google.privacy.dlp.v2.Finding) InspectResult(com.google.privacy.dlp.v2.InspectResult) InfoType(com.google.privacy.dlp.v2.InfoType) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ContentItem(com.google.privacy.dlp.v2.ContentItem)

Example 3 with Finding

use of com.google.privacy.dlp.v2.Finding in project google-cloud-java by GoogleCloudPlatform.

the class FindingSnippets method setFindingState.

// [END securitycenter_update_finding_source_properties]
/**
 * Updates a finding's state to INACTIVE.
 *
 * @param findingName The finding to update.
 */
// [START securitycenter_update_finding_state]
static Finding setFindingState(FindingName findingName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // FindingName findingName = FindingName.of(/*organization=*/"123234324",
        // /*source=*/"423432321", /*findingId=*/"samplefindingid2");
        // Use the current time as the finding "event time".
        Instant eventTime = Instant.now();
        Finding response = client.setFindingState(findingName, State.INACTIVE, Timestamp.newBuilder().setSeconds(eventTime.getEpochSecond()).setNanos(eventTime.getNano()).build());
        System.out.println("Updated Finding: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : Instant(org.threeten.bp.Instant) Finding(com.google.cloud.securitycenter.v1.Finding) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 4 with Finding

use of com.google.privacy.dlp.v2.Finding in project java-docs-samples by GoogleCloudPlatform.

the class Inspect method inspectFile.

// [END dlp_inspect_string]
// [START dlp_inspect_file]
/**
 * Inspect a local file
 *
 * @param filePath The path to a local file to inspect. Can be a text, JPG, or PNG file.
 * @param minLikelihood The minimum likelihood required before returning a match
 * @param maxFindings The maximum number of findings to report (0 = server maximum)
 * @param infoTypes The infoTypes of information to match
 * @param includeQuote Whether to include the matching string
 * @param projectId Google Cloud project ID
 */
private static void inspectFile(String filePath, Likelihood minLikelihood, int maxFindings, List<InfoType> infoTypes, boolean includeQuote, String projectId) {
    // Instantiates a client
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        // detect file mime type, default to application/octet-stream
        String mimeType = URLConnection.guessContentTypeFromName(filePath);
        if (mimeType == null) {
            mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
        }
        ByteContentItem.BytesType bytesType;
        switch(mimeType) {
            case "image/jpeg":
                bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
                break;
            case "image/bmp":
                bytesType = ByteContentItem.BytesType.IMAGE_BMP;
                break;
            case "image/png":
                bytesType = ByteContentItem.BytesType.IMAGE_PNG;
                break;
            case "image/svg":
                bytesType = ByteContentItem.BytesType.IMAGE_SVG;
                break;
            default:
                bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
                break;
        }
        byte[] data = Files.readAllBytes(Paths.get(filePath));
        ByteContentItem byteContentItem = ByteContentItem.newBuilder().setType(bytesType).setData(ByteString.copyFrom(data)).build();
        ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
        FindingLimits findingLimits = FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).setIncludeQuote(includeQuote).build();
        InspectContentRequest request = InspectContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectConfig(inspectConfig).setItem(contentItem).build();
        InspectContentResponse response = dlpServiceClient.inspectContent(request);
        InspectResult result = response.getResult();
        if (result.getFindingsCount() > 0) {
            System.out.println("Findings: ");
            for (Finding finding : result.getFindingsList()) {
                if (includeQuote) {
                    System.out.print("\tQuote: " + finding.getQuote());
                }
                System.out.print("\tInfo type: " + finding.getInfoType().getName());
                System.out.println("\tLikelihood: " + finding.getLikelihood());
            }
        } else {
            System.out.println("No findings.");
        }
    } catch (Exception e) {
        System.out.println("Error in inspectFile: " + e.getMessage());
    }
}
Also used : FindingLimits(com.google.privacy.dlp.v2.InspectConfig.FindingLimits) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ByteString(com.google.protobuf.ByteString) InspectContentRequest(com.google.privacy.dlp.v2.InspectContentRequest) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) ParseException(org.apache.commons.cli.ParseException) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) InspectContentResponse(com.google.privacy.dlp.v2.InspectContentResponse) Finding(com.google.privacy.dlp.v2.Finding) InspectResult(com.google.privacy.dlp.v2.InspectResult) ByteContentItem(com.google.privacy.dlp.v2.ByteContentItem) ContentItem(com.google.privacy.dlp.v2.ContentItem)

Example 5 with Finding

use of com.google.privacy.dlp.v2.Finding in project google-cloud-java by GoogleCloudPlatform.

the class FindingSnippets method updateFinding.

// [END securitycenter_create_finding_with_source_properties]
/**
 * Update a finding's source properties.
 *
 * @param findingName The finding to update.
 */
// [START securitycenter_update_finding_source_properties]
static Finding updateFinding(FindingName findingName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // FindingName findingName = FindingName.of(/*organization=*/"123234324",
        // /*source=*/"423432321", /*findingId=*/"samplefindingid2");
        // Use the current time as the finding "event time".
        Instant eventTime = Instant.now();
        // Define source properties values as protobuf "Value" objects.
        Value stringValue = Value.newBuilder().setStringValue("value").build();
        FieldMask updateMask = FieldMask.newBuilder().addPaths("event_time").addPaths("source_properties.stringKey").build();
        Finding finding = Finding.newBuilder().setName(findingName.toString()).setEventTime(Timestamp.newBuilder().setSeconds(eventTime.getEpochSecond()).setNanos(eventTime.getNano())).putSourceProperties("stringKey", stringValue).build();
        UpdateFindingRequest.Builder request = UpdateFindingRequest.newBuilder().setFinding(finding).setUpdateMask(updateMask);
        // Call the API.
        Finding response = client.updateFinding(request.build());
        System.out.println("Updated Finding: " + response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : UpdateFindingRequest(com.google.cloud.securitycenter.v1.UpdateFindingRequest) Instant(org.threeten.bp.Instant) Finding(com.google.cloud.securitycenter.v1.Finding) Value(com.google.protobuf.Value) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) FieldMask(com.google.protobuf.FieldMask)

Aggregations

Finding (com.google.cloud.securitycenter.v1.Finding)4 SecurityCenterClient (com.google.cloud.securitycenter.v1.SecurityCenterClient)4 IOException (java.io.IOException)4 Instant (org.threeten.bp.Instant)4 DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)3 ByteContentItem (com.google.privacy.dlp.v2.ByteContentItem)3 ContentItem (com.google.privacy.dlp.v2.ContentItem)3 Finding (com.google.privacy.dlp.v2.Finding)3 InspectConfig (com.google.privacy.dlp.v2.InspectConfig)3 InspectContentRequest (com.google.privacy.dlp.v2.InspectContentRequest)3 InspectContentResponse (com.google.privacy.dlp.v2.InspectContentResponse)3 FindingLimits (com.google.privacy.dlp.v2.InspectConfig.FindingLimits)2 InspectResult (com.google.privacy.dlp.v2.InspectResult)2 ByteString (com.google.protobuf.ByteString)2 Value (com.google.protobuf.Value)2 ParseException (org.apache.commons.cli.ParseException)2 UpdateFindingRequest (com.google.cloud.securitycenter.v1.UpdateFindingRequest)1 InfoType (com.google.privacy.dlp.v2.InfoType)1 Likelihood (com.google.privacy.dlp.v2.Likelihood)1 FieldMask (com.google.protobuf.FieldMask)1