Skip to content

Commit f82460a

Browse files
authored
feat(securitycenter): Add Resource SCC Management API Org SHA Custom Module code samples (Update, Get Eff, List Eff, List Desc, Simulate) (#9683)
* sample codes for security health analytics modules * fixed lint error * fixing testcase * fixing testcase * fixing testcase * addressed comments * addressed comments * addressed comments
1 parent 484fce8 commit f82460a

11 files changed

+488
-47
lines changed

security-command-center/snippets/src/main/java/management/api/CreateSecurityHealthAnalyticsCustomModule.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,29 @@ public class CreateSecurityHealthAnalyticsCustomModule {
3131

3232
public static void main(String[] args) throws IOException {
3333
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.securityHealthAnalyticsCustomModules/create
34-
// replace "project_id" with a real project ID
35-
String parent = String.format("projects/%s/locations/%s", "project_id", "global");
34+
// TODO: Developer should replace project_id with a real project ID before running this code
35+
String projectId = "project_id";
3636

3737
String customModuleDisplayName = "custom_module_display_name";
3838

39-
createSecurityHealthAnalyticsCustomModule(parent, customModuleDisplayName);
39+
createSecurityHealthAnalyticsCustomModule(projectId, customModuleDisplayName);
4040
}
4141

4242
public static SecurityHealthAnalyticsCustomModule createSecurityHealthAnalyticsCustomModule(
43-
String parent, String customModuleDisplayName) throws IOException {
43+
String projectId, String customModuleDisplayName) throws IOException {
4444

4545
// Initialize client that will be used to send requests. This client only needs
4646
// to be created
4747
// once, and can be reused for multiple requests.
4848
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
4949

5050
String name =
51-
String.format("%s/securityHealthAnalyticsCustomModules/%s", parent, "custom_module");
51+
String.format(
52+
"projects/%s/locations/global/securityHealthAnalyticsCustomModules/%s",
53+
projectId, "custom_module");
5254

53-
// define the CEL expression here, change it according to the your requirements
55+
// define the CEL expression here and this will scans for keys that have not been rotated in
56+
// the last 30 days, change it according to the your requirements
5457
Expr expr =
5558
Expr.newBuilder()
5659
.setExpression(
@@ -87,7 +90,7 @@ public static SecurityHealthAnalyticsCustomModule createSecurityHealthAnalyticsC
8790

8891
CreateSecurityHealthAnalyticsCustomModuleRequest request =
8992
CreateSecurityHealthAnalyticsCustomModuleRequest.newBuilder()
90-
.setParent(parent)
93+
.setParent(String.format("projects/%s/locations/global", projectId))
9194
.setSecurityHealthAnalyticsCustomModule(securityHealthAnalyticsCustomModule)
9295
.build();
9396

security-command-center/snippets/src/main/java/management/api/DeleteSecurityHealthAnalyticsCustomModule.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,26 @@ public class DeleteSecurityHealthAnalyticsCustomModule {
2525

2626
public static void main(String[] args) throws IOException {
2727
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.securityHealthAnalyticsCustomModules/delete
28-
// replace "project_id" with a real project ID
29-
String parent = String.format("projects/%s/locations/%s", "project_id", "global");
28+
// TODO: Developer should replace project_id with a real project ID before running this code
29+
String projectId = "project_id";
3030

3131
String customModuleId = "custom_module_id";
3232

33-
deleteSecurityHealthAnalyticsCustomModule(parent, customModuleId);
33+
deleteSecurityHealthAnalyticsCustomModule(projectId, customModuleId);
3434
}
3535

3636
public static boolean deleteSecurityHealthAnalyticsCustomModule(
37-
String parent, String customModuleId) throws IOException {
37+
String projectId, String customModuleId) throws IOException {
3838

3939
// Initialize client that will be used to send requests. This client only needs
4040
// to be created
4141
// once, and can be reused for multiple requests.
4242
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
43+
4344
String name =
44-
String.format("%s/securityHealthAnalyticsCustomModules/%s", parent, customModuleId);
45+
String.format(
46+
"projects/%s/locations/global/securityHealthAnalyticsCustomModules/%s",
47+
projectId, customModuleId);
4548

4649
DeleteSecurityHealthAnalyticsCustomModuleRequest request =
4750
DeleteSecurityHealthAnalyticsCustomModuleRequest.newBuilder().setName(name).build();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024 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 management.api;
18+
19+
// [START securitycenter_get_effective_security_health_analytics_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.EffectiveSecurityHealthAnalyticsCustomModule;
21+
import com.google.cloud.securitycentermanagement.v1.GetEffectiveSecurityHealthAnalyticsCustomModuleRequest;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
23+
import java.io.IOException;
24+
25+
public class GetEffectiveSecurityHealthAnalyticsCustomModule {
26+
27+
public static void main(String[] args) throws IOException {
28+
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.effectiveSecurityHealthAnalyticsCustomModules/get
29+
// TODO: Developer should replace project_id with a real project ID before running this code
30+
String projectId = "project_id";
31+
32+
String customModuleId = "custom_module_id";
33+
34+
getEffectiveSecurityHealthAnalyticsCustomModule(projectId, customModuleId);
35+
}
36+
37+
public static EffectiveSecurityHealthAnalyticsCustomModule
38+
getEffectiveSecurityHealthAnalyticsCustomModule(String projectId, String customModuleId)
39+
throws IOException {
40+
41+
// Initialize client that will be used to send requests. This client only needs
42+
// to be created
43+
// once, and can be reused for multiple requests.
44+
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
45+
46+
String name =
47+
String.format(
48+
"projects/%s/locations/global/effectiveSecurityHealthAnalyticsCustomModules/%s",
49+
projectId, customModuleId);
50+
51+
GetEffectiveSecurityHealthAnalyticsCustomModuleRequest request =
52+
GetEffectiveSecurityHealthAnalyticsCustomModuleRequest.newBuilder().setName(name).build();
53+
54+
EffectiveSecurityHealthAnalyticsCustomModule response =
55+
client.getEffectiveSecurityHealthAnalyticsCustomModule(request);
56+
57+
return response;
58+
}
59+
}
60+
}
61+
// [END securitycenter_get_effective_security_health_analytics_custom_module]

security-command-center/snippets/src/main/java/management/api/GetSecurityHealthAnalyticsCustomModule.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,26 @@ public class GetSecurityHealthAnalyticsCustomModule {
2626

2727
public static void main(String[] args) throws IOException {
2828
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.securityHealthAnalyticsCustomModules/get
29-
// replace "project_id" with a real project ID
30-
String parent = String.format("projects/%s/locations/%s", "project_id", "global");
29+
// TODO: Developer should replace project_id with a real project ID before running this code
30+
String projectId = "project_id";
3131

3232
String customModuleId = "custom_module_id";
3333

34-
getSecurityHealthAnalyticsCustomModule(parent, customModuleId);
34+
getSecurityHealthAnalyticsCustomModule(projectId, customModuleId);
3535
}
3636

3737
public static SecurityHealthAnalyticsCustomModule getSecurityHealthAnalyticsCustomModule(
38-
String parent, String customModuleId) throws IOException {
38+
String projectId, String customModuleId) throws IOException {
3939

4040
// Initialize client that will be used to send requests. This client only needs
4141
// to be created
4242
// once, and can be reused for multiple requests.
4343
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
4444

4545
String name =
46-
String.format("%s/securityHealthAnalyticsCustomModules/%s", parent, customModuleId);
46+
String.format(
47+
"projects/%s/locations/global/securityHealthAnalyticsCustomModules/%s",
48+
projectId, customModuleId);
4749

4850
GetSecurityHealthAnalyticsCustomModuleRequest request =
4951
GetSecurityHealthAnalyticsCustomModuleRequest.newBuilder().setName(name).build();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2024 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 management.api;
18+
19+
// [START securitycenter_list_descendant_security_health_analytics_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.ListDescendantSecurityHealthAnalyticsCustomModulesRequest;
21+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListDescendantSecurityHealthAnalyticsCustomModulesPagedResponse;
23+
import java.io.IOException;
24+
25+
public class ListDescendantSecurityHealthAnalyticsCustomModules {
26+
27+
public static void main(String[] args) throws IOException {
28+
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.securityHealthAnalyticsCustomModules/listDescendant
29+
// TODO: Developer should replace project_id with a real project ID before running this code
30+
String projectId = "project_id";
31+
32+
listDescendantSecurityHealthAnalyticsCustomModules(projectId);
33+
}
34+
35+
public static ListDescendantSecurityHealthAnalyticsCustomModulesPagedResponse
36+
listDescendantSecurityHealthAnalyticsCustomModules(String projectId) throws IOException {
37+
38+
// Initialize client that will be used to send requests. This client only needs
39+
// to be created
40+
// once, and can be reused for multiple requests.
41+
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
42+
43+
ListDescendantSecurityHealthAnalyticsCustomModulesRequest request =
44+
ListDescendantSecurityHealthAnalyticsCustomModulesRequest.newBuilder()
45+
.setParent(String.format("projects/%s/locations/global", projectId))
46+
.build();
47+
48+
ListDescendantSecurityHealthAnalyticsCustomModulesPagedResponse response =
49+
client.listDescendantSecurityHealthAnalyticsCustomModules(request);
50+
51+
return response;
52+
}
53+
}
54+
}
55+
// [END securitycenter_list_descendant_security_health_analytics_custom_module]
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2024 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 management.api;
18+
19+
// [START securitycenter_list_effective_security_health_analytics_custom_module]
20+
import com.google.cloud.securitycentermanagement.v1.ListEffectiveSecurityHealthAnalyticsCustomModulesRequest;
21+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
22+
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListEffectiveSecurityHealthAnalyticsCustomModulesPagedResponse;
23+
import java.io.IOException;
24+
25+
public class ListEffectiveSecurityHealthAnalyticsCustomModules {
26+
27+
public static void main(String[] args) throws IOException {
28+
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.effectiveSecurityHealthAnalyticsCustomModules/list
29+
// TODO: Developer should replace project_id with a real project ID before running this code
30+
String projectId = "project_id";
31+
32+
listEffectiveSecurityHealthAnalyticsCustomModules(projectId);
33+
}
34+
35+
public static ListEffectiveSecurityHealthAnalyticsCustomModulesPagedResponse
36+
listEffectiveSecurityHealthAnalyticsCustomModules(String projectId) throws IOException {
37+
38+
// Initialize client that will be used to send requests. This client only needs
39+
// to be created
40+
// once, and can be reused for multiple requests.
41+
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
42+
43+
ListEffectiveSecurityHealthAnalyticsCustomModulesRequest request =
44+
ListEffectiveSecurityHealthAnalyticsCustomModulesRequest.newBuilder()
45+
.setParent(String.format("projects/%s/locations/global", projectId))
46+
.build();
47+
48+
ListEffectiveSecurityHealthAnalyticsCustomModulesPagedResponse response =
49+
client.listEffectiveSecurityHealthAnalyticsCustomModules(request);
50+
51+
return response;
52+
}
53+
}
54+
}
55+
// [END securitycenter_list_effective_security_health_analytics_custom_module]

security-command-center/snippets/src/main/java/management/api/ListSecurityHealthAnalyticsCustomModules.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,23 @@ public class ListSecurityHealthAnalyticsCustomModules {
2626

2727
public static void main(String[] args) throws IOException {
2828
// https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.securityHealthAnalyticsCustomModules/list
29-
// replace "project_id" with a real project ID
30-
String parent = String.format("projects/%s/locations/%s", "project_id", "global");
29+
// TODO: Developer should replace project_id with a real project ID before running this code
30+
String projectId = "project_id";
3131

32-
listSecurityHealthAnalyticsCustomModules(parent);
32+
listSecurityHealthAnalyticsCustomModules(projectId);
3333
}
3434

3535
public static ListSecurityHealthAnalyticsCustomModulesPagedResponse
36-
listSecurityHealthAnalyticsCustomModules(String parent) throws IOException {
36+
listSecurityHealthAnalyticsCustomModules(String projectId) throws IOException {
3737
// Initialize client that will be used to send requests. This client only needs
3838
// to be created
3939
// once, and can be reused for multiple requests.
4040
try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {
4141

4242
ListSecurityHealthAnalyticsCustomModulesRequest request =
43-
ListSecurityHealthAnalyticsCustomModulesRequest.newBuilder().setParent(parent).build();
43+
ListSecurityHealthAnalyticsCustomModulesRequest.newBuilder()
44+
.setParent(String.format("projects/%s/locations/global", projectId))
45+
.build();
4446

4547
ListSecurityHealthAnalyticsCustomModulesPagedResponse response =
4648
client.listSecurityHealthAnalyticsCustomModules(request);

0 commit comments

Comments
 (0)