use of com.google.cloud.securitycenter.v1.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());
}
}
use of com.google.cloud.securitycenter.v1.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());
}
}
use of com.google.cloud.securitycenter.v1.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);
}
}
use of com.google.cloud.securitycenter.v1.Finding in project google-cloud-java by GoogleCloudPlatform.
the class FindingSnippets method testIamPermissions.
// [END securitycenter_list_findings_at_time]
/**
* Demonstrate calling testIamPermissions to determin if the service account has the correct
* permissions.
*
* @param sourceName The source to create a finding for.
*/
// [START securitycenter_test_iam]
static TestIamPermissionsResponse testIamPermissions(SourceName sourceName) {
try (SecurityCenterClient client = SecurityCenterClient.create()) {
// SourceName sourceName = SourceName.of(/*organizationId=*/"123234324",
// /*sourceId=*/"423432321");
// Iam permission to test.
List<String> permissionsToTest = new ArrayList<>();
permissionsToTest.add("securitycenter.findings.update");
// Call the API.
TestIamPermissionsResponse response = client.testIamPermissions(sourceName.toString(), permissionsToTest);
System.out.println("IAM Permission:");
System.out.println(response);
return response;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
use of com.google.cloud.securitycenter.v1.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());
}
}
Aggregations