Skip to content

Commit 8f29d59

Browse files
emkornfieldkolea2
authored andcommitted
---
yaml --- r: 26767 b: refs/heads/v4support c: 7bef921 h: refs/heads/master i: 26765: 20e77ea 26763: 62c78ba 26759: c982151 26751: 2f3797b
1 parent aee428c commit 8f29d59

4 files changed

Lines changed: 274 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ refs/heads/igorbernstein2-patch-1: f62464ee14df1e44a3b173cdc3976563d1b3078b
176176
refs/heads/mrschmidt-collectiongroup: a6d948bf3731a7e1ce1fcd3db8ab733a3c9b17de
177177
refs/heads/release-google-cloud-java-v0.83.0: 4b55ec1b81b3886ede61ae868391a3cdf7eed90e
178178
refs/heads/release-google-cloud-java-v0.83.1-SNAPSHOT: 8d6db7ee534d12b1df38d8cf314871df76f87577
179-
refs/heads/v4support: 237193aa169f201de8b9b3a5be0198593d4838a0
179+
refs/heads/v4support: 7bef9213fa5a3611d199b03a0a4dde2513abf22e
180180
refs/tags/v0.82.0: 7b9807d5d0a400c757b8905fee768be4c85eba25
181181
refs/tags/v0.83.0: 370ec5a1131a86b36db8efce4f1a943607de8a60
182182
refs/tags/v0.84.0: 71e85198495a39f4524afa2669434b5075c17c3d

branches/v4support/google-cloud-examples/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@
6969
<groupId>com.google.cloud</groupId>
7070
<artifactId>google-cloud-spanner</artifactId>
7171
</dependency>
72+
<dependency>
73+
<groupId>com.google.cloud</groupId>
74+
<artifactId>google-cloud-securitycenter</artifactId>
75+
</dependency>
7276
<dependency>
7377
<groupId>com.google.cloud</groupId>
7478
<artifactId>google-cloud-speech</artifactId>
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.cloud.examples.securitycenter.snippets;
17+
18+
import com.google.cloud.securitycenter.v1beta1.ListAssetsRequest;
19+
import com.google.cloud.securitycenter.v1beta1.ListAssetsResponse.ListAssetsResult;
20+
import com.google.cloud.securitycenter.v1beta1.OrganizationName;
21+
import com.google.cloud.securitycenter.v1beta1.SecurityCenterClient;
22+
import com.google.cloud.securitycenter.v1beta1.SecurityCenterClient.ListAssetsPagedResponse;
23+
import com.google.common.base.MoreObjects;
24+
import com.google.common.base.Preconditions;
25+
import com.google.common.collect.ImmutableList;
26+
import java.io.IOException;
27+
import org.threeten.bp.Duration;
28+
import org.threeten.bp.Instant;
29+
30+
/** Snippets for how to work with Assets in Cloud Security Command Center. */
31+
public class AssetSnippets {
32+
private AssetSnippets() {}
33+
34+
/**
35+
* Lists all assets for an organization.
36+
*
37+
* @param organizationName The organization to list assets for.
38+
*/
39+
// [START list_all_assets]
40+
static ImmutableList<ListAssetsResult> listAssets(OrganizationName organizationName) {
41+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
42+
// Start setting up a request for to search for all assets in an organization.
43+
// OrganizationName organizationName = OrganizationName.of("123234324");
44+
ListAssetsRequest.Builder request =
45+
ListAssetsRequest.newBuilder().setParent(organizationName.toString());
46+
47+
// Call the API.
48+
ListAssetsPagedResponse response = client.listAssets(request.build());
49+
50+
// This creates one list for all assets. If your organization has a large number of assets
51+
// this can cause out of memory issues. You can process them batches by returning
52+
// the Iterable returned response.iterateAll() directly.
53+
ImmutableList<ListAssetsResult> results = ImmutableList.copyOf(response.iterateAll());
54+
System.out.println("All assets:");
55+
System.out.println(results);
56+
return results;
57+
} catch (IOException e) {
58+
throw new RuntimeException("Couldn't create client.", e);
59+
}
60+
}
61+
// [END list_all_assets]
62+
63+
/**
64+
* Lists all project assets for an organization.
65+
*
66+
* @param organizationName The organization to list assets for.
67+
*/
68+
// [START list_assets_with_filter]
69+
static ImmutableList<ListAssetsResult> listAssetsWithFilter(OrganizationName organizationName) {
70+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
71+
// Start setting up a request for to search for all assets in an organization.
72+
// OrganizationName organizationName = OrganizationName.of("123234324");
73+
ListAssetsRequest request =
74+
ListAssetsRequest.newBuilder()
75+
.setParent(organizationName.toString())
76+
.setFilter(
77+
"security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"")
78+
.build();
79+
80+
// Call the API.
81+
ListAssetsPagedResponse response = client.listAssets(request);
82+
83+
// This creates one list for all assets. If your organization has a large number of assets
84+
// this can cause out of memory issues. You can process them batches by returning
85+
// the Iterable returned response.iterateAll() directly.
86+
ImmutableList<ListAssetsResult> results = ImmutableList.copyOf(response.iterateAll());
87+
System.out.println("Projects:");
88+
System.out.println(results);
89+
return results;
90+
} catch (IOException e) {
91+
throw new RuntimeException("Couldn't create client.", e);
92+
}
93+
}
94+
// [END list_assets_with_filter]
95+
96+
/**
97+
* Lists all project assets for an organization at a given point in time.
98+
*
99+
* @param organizationName The organization to list assets for.
100+
* @param asOf The snapshot time to query for assets. If null defaults to one day ago.
101+
*/
102+
// [START list_assets_as_of_time]
103+
static ImmutableList<ListAssetsResult> listAssetsAsOfYesterday(
104+
OrganizationName organizationName, Instant asOf) {
105+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
106+
// Start setting up a request for to search for all assets in an organization.
107+
// OrganizationName organizationName = OrganizationName.of("123234324");
108+
109+
// Initialize the builder with the organization and filter
110+
ListAssetsRequest.Builder request =
111+
ListAssetsRequest.newBuilder()
112+
.setParent(organizationName.toString())
113+
.setFilter(
114+
"security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"");
115+
116+
// Set read time to either the instant passed in or one day ago.
117+
asOf = MoreObjects.firstNonNull(asOf, Instant.now().minus(Duration.ofDays(1)));
118+
request.getReadTimeBuilder().setSeconds(asOf.getEpochSecond()).setNanos(asOf.getNano());
119+
120+
// Call the API.
121+
ListAssetsPagedResponse response = client.listAssets(request.build());
122+
123+
// This creates one list for all assets. If your organization has a large number of assets
124+
// this can cause out of memory issues. You can process them batches by returning
125+
// the Iterable returned response.iterateAll() directly.
126+
ImmutableList<ListAssetsResult> results = ImmutableList.copyOf(response.iterateAll());
127+
System.out.println("Projects:");
128+
System.out.println(results);
129+
return results;
130+
} catch (IOException e) {
131+
throw new RuntimeException("Couldn't create client.", e);
132+
}
133+
}
134+
// [END list_assets_as_of_time]
135+
136+
/**
137+
* Returns Assets and metadata about assets activity (e.g. added, removed, no change) between
138+
* between <code>asOf.minus(timespan)</code> and <code>asOf</code>.
139+
*
140+
* @param timeSpan The time-range to compare assets over.
141+
* @param asOf The instant in time to query for. If null, current time is assumed.
142+
*/
143+
// [START list_asset_changes_status_changes]
144+
static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(
145+
OrganizationName organizationName, Duration timeSpan, Instant asOf) {
146+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
147+
148+
// Start setting up a request for to search for all assets in an organization.
149+
// OrganizationName organizationName = OrganizationName.of("123234324");
150+
ListAssetsRequest.Builder request =
151+
ListAssetsRequest.newBuilder()
152+
.setParent(organizationName.toString())
153+
.setFilter(
154+
"security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"");
155+
request
156+
.getCompareDurationBuilder()
157+
.setSeconds(timeSpan.getSeconds())
158+
.setNanos(timeSpan.getNano());
159+
160+
// Set read time to either the instant passed in or now.
161+
asOf = MoreObjects.firstNonNull(asOf, Instant.now());
162+
request.getReadTimeBuilder().setSeconds(asOf.getEpochSecond()).setNanos(asOf.getNano());
163+
164+
// Call the API.
165+
ListAssetsPagedResponse response = client.listAssets(request.build());
166+
167+
// This creates one list for all assets. If your organization has a large number of assets
168+
// this can cause out of memory issues. You can process them batches by returning
169+
// the Iterable returned response.iterateAll() directly.
170+
ImmutableList<ListAssetsResult> results = ImmutableList.copyOf(response.iterateAll());
171+
System.out.println("Projects:");
172+
System.out.println(results);
173+
return results;
174+
} catch (IOException e) {
175+
throw new RuntimeException("Couldn't create client.", e);
176+
}
177+
}
178+
// [END list_asset_changes_status_changes]
179+
180+
public static void main(String... args) {
181+
String org_id = System.getenv("ORGANIZATION_ID");
182+
if (args.length > 0) {
183+
org_id = args[0];
184+
}
185+
186+
Preconditions.checkNotNull(
187+
org_id,
188+
"Organization ID must either be set in the environment variable \"ORGANIZATION_ID\" or passed"
189+
+ " as the first parameter to the program.");
190+
191+
listAssetsWithFilter(OrganizationName.of(org_id));
192+
}
193+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2019 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.securitycenter.snippets;
18+
19+
import static junit.framework.TestCase.assertTrue;
20+
import static org.junit.Assert.assertEquals;
21+
22+
import com.google.cloud.securitycenter.v1beta1.ListAssetsResponse.ListAssetsResult;
23+
import com.google.cloud.securitycenter.v1beta1.ListAssetsResponse.ListAssetsResult.State;
24+
import com.google.cloud.securitycenter.v1beta1.OrganizationName;
25+
import com.google.common.collect.ImmutableList;
26+
import java.io.IOException;
27+
import org.junit.Test;
28+
import org.threeten.bp.Duration;
29+
import org.threeten.bp.Instant;
30+
import org.threeten.bp.LocalDateTime;
31+
import org.threeten.bp.ZoneOffset;
32+
33+
/** Smoke tests for {@link com.google.cloud.examples.securitycenter.snippets.AssetSnippets} */
34+
public class ITAssetSnippets {
35+
36+
private static final Instant NOTHING_INSTANCE =
37+
LocalDateTime.of(2019, 1, 1, 0, 0).toInstant(ZoneOffset.UTC);
38+
private static final Instant SOMETHING_INSTANCE =
39+
LocalDateTime.of(2019, 3, 14, 8, 0).toInstant(ZoneOffset.ofHours((-8)));
40+
41+
@Test
42+
public void mainRuns() throws IOException {
43+
AssetSnippets.main(getOrganizationId().getOrganization());
44+
}
45+
46+
@Test
47+
public void testBeforeDateNoAssetsReturned() {
48+
assertTrue(
49+
AssetSnippets.listAssetsAsOfYesterday(getOrganizationId(), NOTHING_INSTANCE).isEmpty());
50+
}
51+
52+
@Test
53+
public void testListAssetsNoFilterOrDate() {
54+
assertTrue(59 >= AssetSnippets.listAssets(getOrganizationId()).size());
55+
}
56+
57+
@Test
58+
public void testListAssetsWithFilterAndInstance() {
59+
assertTrue(
60+
3 >= AssetSnippets.listAssetsAsOfYesterday(getOrganizationId(), SOMETHING_INSTANCE).size());
61+
}
62+
63+
@Test
64+
public void testChangesReturnsValues() {
65+
ImmutableList<ListAssetsResult> result =
66+
AssetSnippets.listAssetAndStatusChanges(
67+
getOrganizationId(), Duration.ofDays(3), SOMETHING_INSTANCE);
68+
assertTrue("Result: " + result.toString(), result.toString().contains("ADDED"));
69+
assertTrue(3 >= result.size());
70+
assertEquals(result.get(0).getState(), State.ADDED);
71+
}
72+
73+
private static OrganizationName getOrganizationId() {
74+
return OrganizationName.of(System.getenv("GCLOUD_ORGANIZATION"));
75+
}
76+
}

0 commit comments

Comments
 (0)