Skip to content

Commit

Permalink
adjustments for pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafael Rodrigues committed May 2, 2024
1 parent b824256 commit 79c7341
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@

public class GetSource {

public static void main(String[] args) {
public static void main(String[] args) throws IOException {
// TODO: Replace the below variables.
// projectId: Google Cloud Project id.
// organizationId: Google Cloud Organization id.
String organizationId = "{google-cloud-organization-id}";

// Specify the source-id.
Expand All @@ -38,24 +38,23 @@ public static void main(String[] args) {
}

// Demonstrates how to retrieve a specific source.
public static Source getSource(String organizationId, String sourceId) {
public static Source getSource(String organizationId, String sourceId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (SecurityCenterClient client = SecurityCenterClient.create()) {

// Start setting up a request to get a source.
SourceName sourceName = SourceName.ofOrganizationSourceName(organizationId, sourceId);

GetSourceRequest.Builder request = GetSourceRequest.newBuilder()
.setName(sourceName.toString());
GetSourceRequest request = GetSourceRequest.newBuilder()
.setName(sourceName.toString())
.build();

// Call the API.
Source response = client.getSource(request.build());
Source response = client.getSource(request);

System.out.println("Source: " + response);
return response;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

// [START securitycenter_list_sources_v2]

import com.google.cloud.securitycenter.v2.OrganizationLocationName;
import com.google.cloud.securitycenter.v2.OrganizationName;
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
import com.google.cloud.securitycenter.v2.SecurityCenterClient.ListSourcesPagedResponse;
Expand All @@ -37,20 +38,21 @@ public static void main(String[] args) throws IOException {
}

// Demonstrates how to list all security sources in an organization.
public static List<Source> listSources(String organizationId) {
public static List<Source> listSources(String organizationId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (SecurityCenterClient client = SecurityCenterClient.create()) {

// Start setting up a request to get a source.
OrganizationName parent = OrganizationName.of(organizationId);

ListSourcesPagedResponse response = client.listSources(parent);

List<Source> sourcesList = new ArrayList<>();
response.iterateAll().forEach(sourcesList::add);
System.out.println("sourcesList: " + sourcesList.stream().toList());

return sourcesList;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package vtwo.source;

// [START securitycenter_update_finding_source_properties_v2]

import com.google.cloud.securitycenter.v2.Finding;
import com.google.cloud.securitycenter.v2.FindingName;
import com.google.cloud.securitycenter.v2.SecurityCenterClient;
Expand Down Expand Up @@ -47,19 +49,27 @@ public static void main(String[] args) throws IOException {

// Creates or updates a finding.
public static Finding updateFinding(String organizationId,
String location, String sourceId, String findingId) {
String location, String sourceId, String findingId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (SecurityCenterClient client = SecurityCenterClient.create()) {

// Instead of using the FindingName, a plain String can also be used. E.g.:
// String findingName = String.format("organizations/%s/sources/%s/locations/%s/findings/%s",
// organizationId,sourceId,location,findingId);
// organizationId, sourceId, location, findingId);
FindingName findingName = FindingName
.ofOrganizationSourceLocationFindingName(organizationId, sourceId, location, findingId);

// 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();

// Set the update mask to specify which properties should be updated.
// If empty, all mutable fields will be updated.
// For more info on constructing field mask path, see the proto or:
// https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
FieldMask updateMask =
FieldMask.newBuilder()
.addPaths("event_time")
Expand All @@ -77,16 +87,18 @@ public static Finding updateFinding(String organizationId,
.putSourceProperties("stringKey", stringValue)
.build();

UpdateFindingRequest.Builder request =
UpdateFindingRequest.newBuilder().setFinding(finding).setUpdateMask(updateMask);
UpdateFindingRequest request =
UpdateFindingRequest.newBuilder()
.setFinding(finding)
.setUpdateMask(updateMask)
.build();

// Call the API.
Finding response = client.updateFinding(request.build());
Finding response = client.updateFinding(request);

System.out.println("Updated finding source: " + response);
return response;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
}
// [END securitycenter_update_finding_source_properties_v2]
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.io.IOException;

public class UpdateSource {

public static void main(String[] args) throws IOException {
// TODO: Replace the below variables.
// organizationId: Google Cloud Organization id.
Expand All @@ -38,37 +39,36 @@ public static void main(String[] args) throws IOException {
}

// Demonstrates how to update a source.
public static Source updateSource(String organizationId, String sourceId) {
public static Source updateSource(String organizationId, String sourceId) throws IOException {
// Initialize client that will be used to send requests. This client only needs to be created
// once, and can be reused for multiple requests.
try (SecurityCenterClient client = SecurityCenterClient.create()) {

// Start setting up a request to get a source.
SourceName sourceName = SourceName.ofOrganizationSourceName(organizationId, sourceId);
Source source = Source.newBuilder()
.setDisplayName("Updated Display Name")
.setName(sourceName.toString())
.build();
.setDisplayName("Updated Display Name")
.setName(sourceName.toString())
.build();

// Set the update mask to specify which properties should be updated.
// If empty, all mutable fields will be updated.
// For more info on constructing field mask path, see the proto or:
// https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
FieldMask updateMask = FieldMask.newBuilder()
.addPaths("display_name")
.build();
.addPaths("display_name")
.build();

UpdateSourceRequest request = UpdateSourceRequest.newBuilder()
.setSource(source)
.setUpdateMask(updateMask)
.build();
.setSource(source)
.setUpdateMask(updateMask)
.build();

// Call the API.
Source response = client.updateSource(request);

System.out.println("Updated Source: " + response);
return response;
} catch (IOException e) {
throw new RuntimeException("Couldn't create client.", e);
}
}
}
Expand Down
36 changes: 27 additions & 9 deletions security-command-center/snippets/src/test/java/vtwo/SourceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.Assert.assertTrue;

import com.google.cloud.securitycenter.v2.Finding;
import com.google.cloud.securitycenter.v2.SecurityCenterClient.ListSourcesPagedResponse;
import com.google.cloud.securitycenter.v2.Source;
import com.google.cloud.testing.junit4.MultipleAttemptsRule;
import com.google.protobuf.Value;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -83,34 +87,48 @@ public static void setUp() throws IOException, InterruptedException {
System.setOut(out);
}

@Before
public void beforeEach() {
stdOut = new ByteArrayOutputStream();
System.setOut(new PrintStream(stdOut));
}

@After
public void afterEach() {
stdOut = null;
System.setOut(null);
}

@Test
public void testListAllSources() {
public void testListAllSources() throws IOException {
List<Source> response = ListSources.listSources(ORGANIZATION_ID);

assertThat(response.stream().map(Source::getName)).contains(SOURCE.getName());
}

@Test
public void testGetSource() {
public void testGetSource() throws IOException {
Source source = GetSource.getSource(ORGANIZATION_ID, SOURCE.getName().split("/")[3]);

assertThat(source.getName()).isEqualTo(SOURCE.getName());
}

@Test
public void testUpdateSource() {
public void testUpdateSource() throws IOException {
Source source = UpdateSource.updateSource(ORGANIZATION_ID, SOURCE.getName().split("/")[3]);

assertThat(source.getDisplayName()).contains("Updated Display Name");
}

@Test
public void testUpdateFindingSource() {
Finding findingUpdated = UpdateFindingSource.updateFinding(ORGANIZATION_ID, LOCATION,
SOURCE.getName().split("/")[3], FINDING.getName().split("/")[7]);

assertThat(findingUpdated).isNotNull();
assertThat(findingUpdated).isNotEqualTo(Finding.getDefaultInstance());
public void testUpdateFindingSource() throws IOException {
Value stringValue = Value.newBuilder().setStringValue("value").build();

assertTrue(UpdateFindingSource.updateFinding(ORGANIZATION_ID, LOCATION,
SOURCE.getName().split("/")[3], FINDING.getName().split("/")[7])
.getSourcePropertiesMap()
.get("stringKey")
.equals(stringValue));
}

}

0 comments on commit 79c7341

Please sign in to comment.