-
Notifications
You must be signed in to change notification settings - Fork 188
/
TestIntegrationSecurity.java
293 lines (234 loc) · 11.5 KB
/
TestIntegrationSecurity.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (c) 2016-2018 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.web;
import static com.evolveum.midpoint.web.AdminGuiTestConstants.USER_JACK_OID;
import static com.evolveum.midpoint.web.AdminGuiTestConstants.USER_JACK_USERNAME;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import com.evolveum.midpoint.test.TestResource;
import com.evolveum.midpoint.xml.ns._public.common.common_3.FocusType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.FilterInvocation;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;
import com.evolveum.midpoint.model.api.authentication.GuiProfiledPrincipalManager;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.PolicyViolationException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.security.MidPointGuiAuthorizationEvaluator;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
/**
* @author semancik
*/
@ContextConfiguration(locations = {"classpath:ctx-admin-gui-test-main.xml"})
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class TestIntegrationSecurity extends AbstractInitializedGuiIntegrationTest {
private static final File TEST_DIR = new File("src/test/resources/security");
private static final File ROLE_UI_ALLOW_ALL_FILE = new File(TEST_DIR, "role-ui-allow-all.xml");
private static final String ROLE_UI_ALLOW_ALL_OID = "d8f78cfe-d05d-11e7-8ee6-038ce21862f3";
private static final File ROLE_UI_DENY_ALL_FILE = new File(TEST_DIR, "role-ui-deny-all.xml");
private static final String ROLE_UI_DENY_ALL_OID = "c4a5923c-d02b-11e7-9ac5-13b0d906fa81";
private static final File ROLE_UI_DENY_ALLOW_FILE = new File(TEST_DIR, "role-ui-deny-allow.xml");
private static final String ROLE_UI_DENY_ALLOW_OID = "da47fcf6-d02b-11e7-9e78-f31ae9aa0674";
private static final TestResource ROLE_AUTHORIZATION_1 = new TestResource(TEST_DIR, "role-authorization-1.xml", "97984277-e809-4a86-ae9b-d5c40e09df0b");
private static final TestResource ROLE_AUTHORIZATION_2 = new TestResource(TEST_DIR, "role-authorization-2.xml", "96b02d58-5147-4f5a-852c-0f415230ce2c");
private static final Trace LOGGER = TraceManager.getTrace(TestIntegrationSecurity.class);
@Autowired private GuiProfiledPrincipalManager focusProfileService;
private MidPointGuiAuthorizationEvaluator midPointGuiAuthorizationEvaluator;
@Override
public void initSystem(Task initTask, OperationResult initResult) throws Exception {
super.initSystem(initTask, initResult);
midPointGuiAuthorizationEvaluator = new MidPointGuiAuthorizationEvaluator(securityEnforcer, securityContextManager, taskManager);
repoAddObjectFromFile(ROLE_UI_ALLOW_ALL_FILE, initResult);
repoAddObjectFromFile(ROLE_UI_DENY_ALL_FILE, initResult);
repoAddObjectFromFile(ROLE_UI_DENY_ALLOW_FILE, initResult);
repoAdd(ROLE_AUTHORIZATION_1, initResult);
repoAdd(ROLE_AUTHORIZATION_2, initResult);
}
// TODO: decide tests with anon user
@Test
public void test100DecideNoRole() throws Exception {
final String TEST_NAME = "test100DecideNoRole";
// GIVEN
cleanupAutzTest(USER_JACK_OID);
PrismObject<UserType> user = getUser(USER_JACK_OID);
display("user before", user);
login(USER_JACK_USERNAME);
Authentication authentication = createPasswordAuthentication(USER_JACK_USERNAME, UserType.class);
// WHEN
when();
assertAllow(authentication, "/login");
assertAllow(authentication, "/");
assertDeny(authentication, "/noautz");
assertDeny(authentication, "/admin/users");
assertDeny(authentication, "/self/dashboard");
assertDeny(authentication, "/admin/config/system");
assertDeny(authentication, "/admin/config/debugs");
// THEN
then();
}
@Test
public void test110DecideRoleUiAllowAll() throws Exception {
final String TEST_NAME = "test110DecideRoleUiAllowAll";
// GIVEN
cleanupAutzTest(USER_JACK_OID);
assignRole(USER_JACK_OID, ROLE_UI_ALLOW_ALL_OID);
PrismObject<UserType> user = getUser(USER_JACK_OID);
display("user before", user);
login(USER_JACK_USERNAME);
Authentication authentication = createPasswordAuthentication(USER_JACK_USERNAME, UserType.class);
// WHEN
when();
assertAllow(authentication, "/login");
assertAllow(authentication, "/");
assertDeny(authentication, "/noautz");
assertAllow(authentication, "/admin/users");
assertAllow(authentication, "/self/dashboard");
assertAllow(authentication, "/admin/config/system");
assertAllow(authentication, "/admin/config/debugs");
// THEN
then();
}
@Test
public void test120DecideRoleUiDenyAll() throws Exception {
final String TEST_NAME = "test120DecideRoleUiDenyAll";
// GIVEN
cleanupAutzTest(USER_JACK_OID);
assignRole(USER_JACK_OID, ROLE_UI_DENY_ALL_OID);
PrismObject<UserType> user = getUser(USER_JACK_OID);
display("user before", user);
login(USER_JACK_USERNAME);
Authentication authentication = createPasswordAuthentication(USER_JACK_USERNAME, UserType.class);
// WHEN
when();
assertAllow(authentication, "/login");
assertAllow(authentication, "/");
assertDeny(authentication, "/noautz");
assertDeny(authentication, "/admin/users");
assertDeny(authentication, "/self/dashboard");
assertDeny(authentication, "/admin/config/system");
assertDeny(authentication, "/admin/config/debugs");
// THEN
then();
}
/**
* MID-4129
*/
@Test
public void test200DecideRoleUiDenyAllow() throws Exception {
final String TEST_NAME = "test200DecideRoleUiDenyAllow";
// GIVEN
cleanupAutzTest(USER_JACK_OID);
assignRole(USER_JACK_OID, ROLE_UI_DENY_ALLOW_OID);
PrismObject<UserType> user = getUser(USER_JACK_OID);
display("user before", user);
login(USER_JACK_USERNAME);
Authentication authentication = createPasswordAuthentication(USER_JACK_USERNAME, UserType.class);
// WHEN
when();
assertAllow(authentication, "/login");
assertAllow(authentication, "/");
assertDeny(authentication, "/noautz");
assertAllow(authentication, "/self/dashboard");
assertAllow(authentication, "/admin/users");
assertDeny(authentication, "/admin/config/system");
assertDeny(authentication, "/admin/config/debugs");
// THEN
then();
}
/**
* MID-5002
*/
@Test
public void test300ConflictingAuthorizationIds() throws Exception {
final String TEST_NAME = "test300ConflictingAuthorizationIds";
// GIVEN
cleanupAutzTest(USER_JACK_OID);
assignRole(USER_JACK_OID, ROLE_AUTHORIZATION_1.oid);
assignRole(USER_JACK_OID, ROLE_AUTHORIZATION_2.oid);
PrismObject<UserType> user = getUser(USER_JACK_OID);
display("user before", user);
// WHEN
when("Jack logs in");
login(USER_JACK_USERNAME);
// THEN
then();
assertLoggedInUsername(USER_JACK_USERNAME);
}
private void assertAllow(Authentication authentication, String path) {
try {
LOGGER.debug("*** Attempt to DECIDE {} (expected allow)", path);
midPointGuiAuthorizationEvaluator.decide(authentication, createFilterInvocation(path), createAuthConfigAttributes());
display("DECIDE OK allowed access to " + path);
} catch (AccessDeniedException e) {
display("DECIDE WRONG failed to allowed access to " + path);
throw new AssertionError("Expected that access to "+path+" is allowed, but it was denied", e);
}
}
private void assertDeny(Authentication authentication, String path) {
try {
LOGGER.debug("*** Attempt to DECIDE {} (expected deny)", path);
midPointGuiAuthorizationEvaluator.decide(authentication, createFilterInvocation(path), createAuthConfigAttributes());
display("DECIDE WRONG failed to deny access to " + path);
fail("Expected that access to "+path+" is denied, but it was allowed");
} catch (AccessDeniedException e) {
// expected
display("DECIDE OK denied access to " + path);
}
}
private Authentication createPasswordAuthentication(String username, Class<? extends FocusType> focusType) throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException, SecurityViolationException, ExpressionEvaluationException {
MidPointPrincipal principal = focusProfileService.getPrincipal(username, focusType);
UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(principal, null, principal.getAuthorities());
return auth;
}
private FilterInvocation createFilterInvocation(String requestPath) {
return new FilterInvocation(requestPath, "http");
}
private Collection<ConfigAttribute> createAuthConfigAttributes() {
return createConfigAttributes("fullyAuthenticated");
}
private Collection<ConfigAttribute> createConfigAttributes(String... actions) {
Collection<ConfigAttribute> configAttributes = new ArrayList<>();
for (String action: actions) {
configAttributes.add(new ConfigAttribute() {
private static final long serialVersionUID = 1L;
@Override
public String getAttribute() {
return action;
}
@Override
public String toString() {
return action;
}
});
}
return null;
}
private void cleanupAutzTest(String userOid) throws ObjectNotFoundException, SchemaException, ExpressionEvaluationException, CommunicationException, ConfigurationException, ObjectAlreadyExistsException, PolicyViolationException, SecurityViolationException, IOException {
login(userAdministrator);
unassignAllRoles(userOid);
}
}