-
Notifications
You must be signed in to change notification settings - Fork 17
/
RolesExamples.java
100 lines (83 loc) · 3.52 KB
/
RolesExamples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright 2019 Trend Micro.
*
* 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.
*/
package com.trendmicro.deepsecurity.docs;
import com.trendmicro.deepsecurity.ApiException;
import com.trendmicro.deepsecurity.api.AdministratorRolesApi;
import com.trendmicro.deepsecurity.model.AdministratorRoles;
import com.trendmicro.deepsecurity.model.ComputerRights;
import com.trendmicro.deepsecurity.model.PlatformRights;
import com.trendmicro.deepsecurity.model.Rights;
import com.trendmicro.deepsecurity.model.Role;
import com.trendmicro.deepsecurity.model.SearchCriteria;
import com.trendmicro.deepsecurity.model.SearchFilter;
/**
* Creates and interacts with Deep Security Manager roles.
*/
public class RolesExamples {
/**
* Searches for a role by name.
*
* @param roleName The role name to search.
* @param apiVersion The version of the API to use.
* @throws ApiException if a problem occurs when searching for the role.
* @returns The ID of the found role, or null if no role is found
*/
public static Integer searchRolesByName(String roleName, String apiVersion) throws ApiException {
// Search criteria
SearchCriteria nameCriteria = new SearchCriteria();
nameCriteria.setFieldName("name");
nameCriteria.setStringValue(roleName);
nameCriteria.setStringTest(SearchCriteria.StringTestEnum.EQUAL);
// Search filter
SearchFilter roleFilter = new SearchFilter();
roleFilter.addSearchCriteriaItem(nameCriteria);
// Perform the search and obtain the ID of the returned role
AdministratorRolesApi adminRolesApi = new AdministratorRolesApi();
AdministratorRoles roles = adminRolesApi.searchAdministratorRoles(roleFilter, apiVersion);
Integer roleId = null;
if (!roles.getRoles().isEmpty()) {
roleId = roles.getRoles().get(0).getID();
}
return roleId;
}
/**
* Creates a role with rights that are appropriate for reading computer properties and assigning policies to computers.
*
* @param apiVersion The version of the API to use.
* @throws ApiException if a problem occurs when creating the role on Deep Security Manager.
* @return The ID of the new role.
*/
public static Integer createRoleForAssigningPoliciesToComputers(String apiVersion) throws ApiException {
// Create the Role object
Role runReportsRole = new Role();
runReportsRole.setName("Computer Status and Properties");
// No need for access to policies
runReportsRole.setAllPolicies(Boolean.FALSE);
// Add rights to edit computer properties
ComputerRights computerRights = new ComputerRights();
computerRights.setCanEditComputerProperties(Boolean.FALSE);
PlatformRights platformRights = new PlatformRights();
platformRights.setComputerRights(computerRights);
Rights rights = new Rights();
rights.setPlatformRights(platformRights);
// Add rights to the role
runReportsRole.setRights(rights);
// Create the role on Deep Security Manager
AdministratorRolesApi adminRolesApi = new AdministratorRolesApi();
runReportsRole = adminRolesApi.createAdministratorRole(runReportsRole, apiVersion);
return runReportsRole.getID();
}
}