Skip to content

Commit

Permalink
Implemented sample for create and update stored infotypes
Browse files Browse the repository at this point in the history
  • Loading branch information
jainamtrivedi-crest authored and amanda-tarafa committed Aug 23, 2023
1 parent 6fc80da commit 7228f52
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 0 deletions.
55 changes: 55 additions & 0 deletions dlp/api/Snippets.Tests/CreateStoredInfoTypesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2023 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License

using Google.Cloud.Dlp.V2;
using Google.Cloud.Storage.V1;
using Xunit;
using System;

namespace GoogleCloudSamples
{
public class CreateStoredInfoTypesTests : IClassFixture<DlpTestFixture>
{
private DlpTestFixture _fixture;
public CreateStoredInfoTypesTests(DlpTestFixture fixture) => _fixture = fixture;

[Fact]
public void TestCreate()
{
Random random = new Random();
// Create a bucket.
var storage = StorageClient.Create();
var bucket = storage.CreateBucket(_fixture.ProjectId, $"dlp_dotnet_test_bucket_samples_{random.Next()}");

var dlp = DlpServiceClient.Create();
try
{
var storedInfoTypeId = $"github-usernames-create-{random.Next()}";
var result = CreateStoredInfoTypes.Create(_fixture.ProjectId, $"gs://{bucket.Name}", storedInfoTypeId);
Assert.Contains(storedInfoTypeId, result.Name);

// Delete the created stored infoTypes.
dlp.DeleteStoredInfoType(new DeleteStoredInfoTypeRequest
{
Name = result.Name
});
}
finally
{
// Delete the created bucket.
storage.DeleteBucket(bucket.Name);
}
}
}
}
67 changes: 67 additions & 0 deletions dlp/api/Snippets.Tests/InspectDataWithStoredInfotypesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License

using Google.Cloud.Dlp.V2;
using Google.Cloud.Storage.V1;
using System;
using System.Runtime.Intrinsics.Arm;
using System.Threading;
using Xunit;

namespace GoogleCloudSamples
{
public class InspectDataWithStoredInfotypesTests : IClassFixture<DlpTestFixture>
{
private DlpTestFixture _fixture;
public InspectDataWithStoredInfotypesTests(DlpTestFixture fixture) => _fixture = fixture;

[Fact]
public void TestInspect()
{
// Create the bucket for creating stored infoType.
Random random = new Random();
var storage = StorageClient.Create();
var bucket = storage.CreateBucket(_fixture.ProjectId, $"dlp_dotnet_test_bucket_samples_{random.Next()}");

try
{
// Create Stored InfoType first.
var storedInfoTypeId = $"github-usernames-inspect-{random.Next()}";
var response = CreateStoredInfoTypes.Create(_fixture.ProjectId, $"gs://{bucket.Name}", storedInfoTypeId);
Thread.Sleep(TimeSpan.FromSeconds(30)); // Sleep until created stored infotype gets ready.

var githubUser = "jamesC311";
var text = $"Commit was made by {githubUser}.";
var result = InspectDataWithStoredInfotypes.Inspect(_fixture.ProjectId, response.Name, text);
Assert.Single(result.Result.Findings);
Assert.Contains(result.Result.Findings, f => f.InfoType.Name == "GITHUB_LOGINS");
Assert.Contains(result.Result.Findings, f => f.Quote == githubUser);

var dlp = DlpServiceClient.Create();

// Delete the created stored infoTypes
dlp.DeleteStoredInfoType(new DeleteStoredInfoTypeRequest
{
Name = response.Name
});

}
finally
{
// Delete the created bucket.
storage.DeleteBucket(bucket.Name);
}
}
}
}
72 changes: 72 additions & 0 deletions dlp/api/Snippets.Tests/UpdateStoredInfoTypesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2023 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License

using Google.Cloud.Dlp.V2;
using Google.Cloud.Storage.V1;
using System;
using System.IO;
using Xunit;

namespace GoogleCloudSamples
{
public class UpdateStoredInfoTypesTests : IClassFixture<DlpTestFixture>
{
private DlpTestFixture _fixture;
public UpdateStoredInfoTypesTests(DlpTestFixture fixture) => _fixture = fixture;

[Fact]
public void TestUpdate()
{
Random random = new Random();
// Create the bucket for creating stored infoType.
var storage = StorageClient.Create();
var bucket = storage.CreateBucket(_fixture.ProjectId, $"dlp_update_dotnet_test_bucket_samples_{random.Next()}");

// Create output bucket.
var bucketOutput = storage.CreateBucket(_fixture.ProjectId, $"dlp_update_dotnet_test_stored_infotype_bucket_{random.Next()}");

// Upload the file to cloud storage.
var filePath = Path.Combine(_fixture.ResourcePath, "test.txt");
using var fileStream = File.OpenRead(filePath);
storage.UploadObject(bucket.Name, "test.txt", null, fileStream);

try
{
// Create Stored InfoType first.
var storedInfoTypeId = $"github-usernames-update-{random.Next()}";
var response = CreateStoredInfoTypes.Create(_fixture.ProjectId, $"gs://{bucket.Name}", storedInfoTypeId);

var result = UpdateStoredInfoTypes.Update($"gs://{bucket.Name}/test.txt", response.Name, $"gs://{bucketOutput.Name}");

// Check whether stored infoType contains cloud storage file set field or not
// as we are changing source from Bigquery to GCS.
Assert.Contains("cloudStorageFileSet", result.ToString());

var dlp = DlpServiceClient.Create();

// Delete the created stored infoType.
dlp.DeleteStoredInfoType(new DeleteStoredInfoTypeRequest
{
Name = result.Name
});
}
finally
{
// Delete the created buckets.
storage.DeleteBucket(bucketOutput.Name);
storage.DeleteBucket(bucket.Name, new DeleteBucketOptions { DeleteObjects = true });
}
}
}
}
77 changes: 77 additions & 0 deletions dlp/api/Snippets/CreateStoredInfoTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2023 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// [START dlp_create_stored_infotype]

using System;
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Dlp.V2;

public class CreateStoredInfoTypes
{
public static StoredInfoType Create(
string projectId,
string outputPath,
string storedInfoTypeId)
{
// Instantiate the dlp client.
var dlp = DlpServiceClient.Create();

// Construct the stored infotype config by specifying the public table and
// cloud storage output path.
var storedInfoTypeConfig = new StoredInfoTypeConfig
{
DisplayName = "Github Usernames",
Description = "Dictionary of Github usernames used in commits.",
LargeCustomDictionary = new LargeCustomDictionaryConfig
{
BigQueryField = new BigQueryField
{
Table = new BigQueryTable
{
DatasetId = "samples",
ProjectId = "bigquery-public-data",
TableId = "github_nested"
},
Field = new FieldId
{
Name = "actor"
}
},
OutputPath = new CloudStoragePath
{
Path = outputPath
}
},
};

// Construct the request.
var request = new CreateStoredInfoTypeRequest
{
ParentAsLocationName = new LocationName(projectId, "global"),
Config = storedInfoTypeConfig,
StoredInfoTypeId = storedInfoTypeId
};

// Call the API.
StoredInfoType response = dlp.CreateStoredInfoType(request);

// Inspect the response.
Console.WriteLine($"Created the stored infotype at path: {response.Name}");

return response;
}
}

// [END dlp_create_stored_infotype]
75 changes: 75 additions & 0 deletions dlp/api/Snippets/InspectDataWithStoredInfotypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2023 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

// [START dlp_inspect_with_stored_infotype]

using System;
using Google.Api.Gax.ResourceNames;
using Google.Cloud.Dlp.V2;

public class InspectDataWithStoredInfotypes
{
public static InspectContentResponse Inspect(
string projectId,
string storedInfotypePath,
string text,
InfoType infoType = null)
{
// Instantiate the dlp client.
var dlp = DlpServiceClient.Create();

// Construct the infotype if null.
var infotype = infoType ?? new InfoType { Name = "GITHUB_LOGINS" };

// Construct the inspect config using stored infotype.
var inspectConfig = new InspectConfig
{
CustomInfoTypes =
{
new CustomInfoType
{
InfoType = infotype,
StoredType = new StoredType { Name = storedInfotypePath }
}
},
IncludeQuote = true
};

// Construct the request using inspect config.
var request = new InspectContentRequest
{
ParentAsLocationName = new LocationName(projectId, "global"),
InspectConfig = inspectConfig,
Item = new ContentItem { Value = text }
};

// Call the API.
InspectContentResponse response = dlp.InspectContent(request);

// Inspect the results.
var findings = response.Result.Findings;
Console.WriteLine($"Findings: {findings.Count}");
foreach (var f in findings)
{
Console.WriteLine("\tQuote: " + f.Quote);
Console.WriteLine("\tInfo type: " + f.InfoType.Name);
Console.WriteLine("\tLikelihood: " + f.Likelihood);
}

return response;
}
}

// [END dlp_inspect_with_stored_infotype]

Loading

0 comments on commit 7228f52

Please sign in to comment.