/
MidPointApplication.java
631 lines (530 loc) · 24.4 KB
/
MidPointApplication.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/*
* Copyright (c) 2010-2018 Evolveum
*
* 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.evolveum.midpoint.web.security;
import com.evolveum.midpoint.common.LocalizationService;
import com.evolveum.midpoint.common.configuration.api.MidpointConfiguration;
import com.evolveum.midpoint.gui.api.page.PageBase;
import com.evolveum.midpoint.gui.api.util.MidPointApplicationConfiguration;
import com.evolveum.midpoint.gui.api.util.WebComponentUtil;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.model.api.*;
import com.evolveum.midpoint.model.common.SystemObjectCache;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.crypto.Protector;
import com.evolveum.midpoint.prism.match.MatchingRuleRegistry;
import com.evolveum.midpoint.repo.api.RepositoryService;
import com.evolveum.midpoint.repo.common.expression.ExpressionFactory;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.SchemaDebugUtil;
import com.evolveum.midpoint.security.api.AuthorizationConstants;
import com.evolveum.midpoint.security.api.MidPointPrincipal;
import com.evolveum.midpoint.security.api.SecurityContextManager;
import com.evolveum.midpoint.security.enforcer.api.SecurityEnforcer;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SystemException;
import com.evolveum.midpoint.util.logging.LoggingUtils;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.web.application.AsyncWebProcessManager;
import com.evolveum.midpoint.web.application.DescriptorLoader;
import com.evolveum.midpoint.web.page.admin.home.PageDashboard;
import com.evolveum.midpoint.web.page.error.*;
import com.evolveum.midpoint.web.page.login.PageLogin;
import com.evolveum.midpoint.web.page.self.PageSelfDashboard;
import com.evolveum.midpoint.web.resource.img.ImgResources;
import com.evolveum.midpoint.web.util.MidPointResourceStreamLocator;
import com.evolveum.midpoint.web.util.MidPointStringResourceLoader;
import com.evolveum.midpoint.web.util.SchrodingerComponentInitListener;
import com.evolveum.midpoint.wf.api.WorkflowManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SystemConfigurationType;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.io.IOUtils;
import org.apache.wicket.*;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.core.request.mapper.MountedMapper;
import org.apache.wicket.core.util.resource.locator.IResourceStreamLocator;
import org.apache.wicket.core.util.resource.locator.caching.CachingResourceStreamLocator;
import org.apache.wicket.markup.head.PriorityFirstComparator;
import org.apache.wicket.markup.html.SecurePackageResourceGuard;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.request.mapper.parameter.PageParametersEncoder;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.request.resource.SharedResourceReference;
import org.apache.wicket.resource.loader.IStringResourceLoader;
import org.apache.wicket.settings.ApplicationSettings;
import org.apache.wicket.settings.ResourceSettings;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.apache.wicket.util.lang.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.security.web.csrf.CsrfToken;
import java.io.*;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
/**
* @author lazyman
*/
public class MidPointApplication extends AuthenticatedWebApplication {
public static final String SYSTEM_PROPERTY_SCHRODINGER = "midpoint.schrodinger";
/**
* Max. photo size for user/jpegPhoto
*/
public static final Bytes FOCUS_PHOTO_MAX_FILE_SIZE = Bytes.kilobytes(192);
public static final String WEB_APP_CONFIGURATION = "midpoint.webApplication";
public static final List<LocaleDescriptor> AVAILABLE_LOCALES;
private static final String LOCALIZATION_DESCRIPTOR = "localization/locale.properties";
private static final String PROP_NAME = ".name";
private static final String PROP_FLAG = ".flag";
private static final String PROP_DEFAULT = ".default";
private static final Trace LOGGER = TraceManager.getTrace(MidPointApplication.class);
static {
SchemaDebugUtil.initialize();
}
static {
String midpointHome = System.getProperty(WebApplicationConfiguration.MIDPOINT_HOME);
File file = new File(midpointHome, LOCALIZATION_DESCRIPTOR);
Resource[] localeDescriptorResources = new Resource[]{
new FileSystemResource(file),
new ClassPathResource(LOCALIZATION_DESCRIPTOR)
};
List<LocaleDescriptor> locales = new ArrayList<>();
for (Resource resource : localeDescriptorResources) {
if (!resource.isReadable()) {
continue;
}
try {
LOGGER.debug("Found localization descriptor {}.", new Object[]{resource.getURL()});
locales = loadLocaleDescriptors(resource);
break;
} catch (Exception ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't load localization", ex);
}
}
Collections.sort(locales);
AVAILABLE_LOCALES = Collections.unmodifiableList(locales);
}
@Autowired
transient ModelService model;
@Autowired
transient ModelInteractionService modelInteractionService;
@Autowired
transient TaskService taskService;
@Autowired
transient PrismContext prismContext;
@Autowired
transient ExpressionFactory expressionFactory;
@Autowired
transient TaskManager taskManager;
@Autowired
transient ModelAuditService auditService;
@Autowired
transient private RepositoryService repositoryService; // temporary
@Autowired
transient private WorkflowService workflowService;
@Autowired
transient private WorkflowManager workflowManager;
@Autowired
transient MidpointConfiguration configuration;
@Autowired
transient Protector protector;
@Autowired
transient MatchingRuleRegistry matchingRuleRegistry;
@Autowired
transient SecurityEnforcer securityEnforcer;
@Autowired
transient SecurityContextManager securityContextManager;
@Autowired
transient SystemObjectCache systemObjectCache;
@Autowired
transient LocalizationService localizationService;
@Autowired
transient AsyncWebProcessManager asyncWebProcessManager;
@Autowired
transient ApplicationContext applicationContext;
private WebApplicationConfiguration webApplicationConfiguration;
@Override
public Class<? extends PageBase> getHomePage() {
if (WebComponentUtil.isAuthorized(AuthorizationConstants.AUTZ_UI_DASHBOARD_URL,
AuthorizationConstants.AUTZ_UI_HOME_ALL_URL)) {
return PageDashboard.class;
} else {
return PageSelfDashboard.class;
}
}
@Override
public void init() {
super.init();
getJavaScriptLibrarySettings().setJQueryReference(
new PackageResourceReference(MidPointApplication.class,
"../../../../../webjars/adminlte/2.3.11/plugins/jQuery/jquery-2.2.3.min.js"));
getComponentInstantiationListeners().add(new SpringComponentInjector(this));
ResourceSettings resourceSettings = getResourceSettings();
resourceSettings.setParentFolderPlaceholder("$-$");
resourceSettings.setHeaderItemComparator(new PriorityFirstComparator(true));
SecurePackageResourceGuard guard = (SecurePackageResourceGuard) resourceSettings.getPackageResourceGuard();
guard.addPattern("+*.woff2");
List<IStringResourceLoader> resourceLoaders = resourceSettings.getStringResourceLoaders();
resourceLoaders.add(0, new MidPointStringResourceLoader(localizationService));
IResourceStreamLocator locator = new CachingResourceStreamLocator(
new MidPointResourceStreamLocator(resourceSettings.getResourceFinders()));
resourceSettings.setResourceStreamLocator(locator);
resourceSettings.setThrowExceptionOnMissingResource(false);
getMarkupSettings().setStripWicketTags(true);
if (RuntimeConfigurationType.DEVELOPMENT.equals(getConfigurationType())) {
getDebugSettings().setAjaxDebugModeEnabled(true);
getDebugSettings().setDevelopmentUtilitiesEnabled(true);
initializeDevelopmentSerializers();
}
//pretty url for resources (e.g. images)
mountFiles(ImgResources.BASE_PATH, ImgResources.class);
//exception handling an error pages
ApplicationSettings appSettings = getApplicationSettings();
appSettings.setAccessDeniedPage(PageError401.class);
appSettings.setInternalErrorPage(PageError.class);
appSettings.setPageExpiredErrorPage(PageError.class);
mount(new MountedMapper("/error", PageError.class, new PageParametersEncoder()));
mount(new MountedMapper("/error/401", PageError401.class, new PageParametersEncoder()));
mount(new MountedMapper("/error/403", PageError403.class, new PageParametersEncoder()));
mount(new MountedMapper("/error/404", PageError404.class, new PageParametersEncoder()));
mount(new MountedMapper("/error/410", PageError410.class, new PageParametersEncoder()));
getRequestCycleListeners().add(new LoggingRequestCycleListener(this));
getAjaxRequestTargetListeners().add(new AjaxRequestTarget.AbstractListener() {
@Override
public void updateAjaxAttributes(AbstractDefaultAjaxBehavior behavior, AjaxRequestAttributes attributes) {
// check whether behavior will use POST method, if not then don't put CSRF token there
if (!isPostMethodTypeBehavior(behavior, attributes)) {
return;
}
CsrfToken csrfToken = SecurityUtils.getCsrfToken();
if (csrfToken == null) {
return;
}
String parameterName = csrfToken.getParameterName();
String value = csrfToken.getToken();
attributes.getExtraParameters().put(parameterName, value);
}
});
getSessionListeners().add((ISessionListener) asyncWebProcessManager);
//descriptor loader, used for customization
new DescriptorLoader().loadData(this);
if (applicationContext != null) {
Map<String, MidPointApplicationConfiguration> map =
applicationContext.getBeansOfType(MidPointApplicationConfiguration.class);
if (map != null) {
map.forEach((key, value) -> value.init(this));
}
}
// for schrodinger selenide library
initializeSchrodinger();
}
private void initializeSchrodinger() {
if (applicationContext == null) {
return;
}
Environment environment = applicationContext.getEnvironment();
if (environment == null) {
return;
}
String value = environment.getProperty(SYSTEM_PROPERTY_SCHRODINGER);
Boolean enabled = Boolean.parseBoolean(value);
if (enabled) {
LOGGER.info("Schrodinger plugin enabled");
getComponentInitializationListeners().add(new SchrodingerComponentInitListener());
}
}
private boolean isPostMethodTypeBehavior(AbstractDefaultAjaxBehavior behavior, AjaxRequestAttributes attributes) {
if (behavior instanceof AjaxFormComponentUpdatingBehavior) {
// these also uses POST, but they set it after this method is called
return true;
}
if (behavior instanceof AjaxFormSubmitBehavior) {
AjaxFormSubmitBehavior fb = (AjaxFormSubmitBehavior) behavior;
Form form = fb.getForm();
String formMethod = form.getMarkupAttributes().getString("method");
if (formMethod == null || "POST".equalsIgnoreCase(formMethod) || form.getRootForm().isMultiPart()) {
// this will also use POST
return true;
}
}
return AjaxRequestAttributes.Method.POST.equals(attributes.getMethod());
}
private static List<LocaleDescriptor> loadLocaleDescriptors(Resource resource) throws IOException {
List<LocaleDescriptor> locales = new ArrayList<>();
Properties properties = new Properties();
Reader reader = null;
try {
reader = new InputStreamReader(resource.getInputStream(), "utf-8");
properties.load(reader);
Map<String, Map<String, String>> localeMap = new HashMap<>();
Set<String> keys = (Set) properties.keySet();
for (String key : keys) {
String[] array = key.split("\\.");
if (array.length != 2) {
continue;
}
String locale = array[0];
Map<String, String> map = localeMap.get(locale);
if (map == null) {
map = new HashMap<>();
localeMap.put(locale, map);
}
map.put(key, properties.getProperty(key));
}
for (Map.Entry<String, Map<String, String>> entry : localeMap.entrySet()) {
Map<String, String> localeDefinition = entry.getValue();
if (!localeDefinition.containsKey(entry + PROP_NAME)
|| !localeDefinition.containsKey(entry + PROP_FLAG)) {
continue;
}
LocaleDescriptor descriptor = new LocaleDescriptor(
localeDefinition.get(entry + PROP_NAME),
localeDefinition.get(entry + PROP_FLAG),
localeDefinition.get(entry + PROP_DEFAULT),
WebComponentUtil.getLocaleFromString(entry.getKey())
);
locales.add(descriptor);
}
} finally {
IOUtils.closeQuietly(reader);
}
return locales;
}
private URL buildMidpointHomeLocalizationFolderUrl() {
String midpointHome = System.getProperty(WebApplicationConfiguration.MIDPOINT_HOME);
File file = new File(midpointHome, "localization");
try {
return file.toURI().toURL();
} catch (IOException ex) {
throw new SystemException("Couldn't transform localization folder file to url", ex);
}
}
private void initializeDevelopmentSerializers() {
// JavaSerializer javaSerializer = new JavaSerializer( getApplicationKey() ) {
// @Override
// protected ObjectOutputStream newObjectOutputStream(OutputStream out) throws IOException {
// LOGGER.info("XXXXXXX YX Y");
//// IObjectChecker checker1 = new MidPointObjectChecker();
////// IObjectChecker checker2 = new NotDetachedModelChecker();
//// IObjectChecker checker3 = new ObjectSerializationChecker();
//// return new CheckingObjectOutputStream(out, checker1, checker3);
// return new ObjectOutputStream(out);
// }
// };
// getFrameworkSettings().setSerializer( javaSerializer );
}
private void mountFiles(String path, Class<?> clazz) {
try {
List<Resource> list = new ArrayList<>();
String packagePath = clazz.getPackage().getName().replace('.', '/');
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] res = resolver.getResources("classpath:" + packagePath + "/*.png");
if (res != null) {
list.addAll(Arrays.asList(res));
}
res = resolver.getResources("classpath:" + packagePath + "/*.gif");
if (res != null) {
list.addAll(Arrays.asList(res));
}
for (Resource resource : list) {
URI uri = resource.getURI();
File file = new File(uri.toString());
mountResource(path + "/" + file.getName(), new SharedResourceReference(clazz, file.getName()));
}
} catch (Exception ex) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't mount files", ex);
}
}
public WebApplicationConfiguration getWebApplicationConfiguration() {
if (webApplicationConfiguration == null) {
Configuration config = configuration.getConfiguration(WEB_APP_CONFIGURATION);
webApplicationConfiguration = new WebApplicationConfiguration(config);
}
return webApplicationConfiguration;
}
public SecurityEnforcer getSecurityEnforcer() {
return securityEnforcer;
}
public ModelService getModel() {
return model;
}
public TaskManager getTaskManager() {
return taskManager;
}
public ModelAuditService getAuditService() {
return auditService;
}
public RepositoryService getRepositoryService() {
return repositoryService;
}
public TaskService getTaskService() {
return taskService;
}
public PrismContext getPrismContext() {
return prismContext;
}
public ExpressionFactory getExpressionFactory() {
return expressionFactory;
}
public Protector getProtector() {
return protector;
}
@Override
protected Class<? extends WebPage> getSignInPageClass() {
return PageLogin.class;
}
@Override
protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass() {
return MidPointAuthWebSession.class;
}
public WorkflowService getWorkflowService() {
return workflowService;
}
public WorkflowManager getWorkflowManager() {
return workflowManager;
}
public ModelInteractionService getModelInteractionService() {
return modelInteractionService;
}
public static boolean containsLocale(Locale locale) {
if (locale == null) {
return false;
}
for (LocaleDescriptor descriptor : AVAILABLE_LOCALES) {
if (locale.equals(descriptor.getLocale())) {
return true;
}
}
return false;
}
public static Locale getDefaultLocale() {
for (LocaleDescriptor descriptor : AVAILABLE_LOCALES) {
if (descriptor.isDefault()) {
return descriptor.getLocale();
}
}
return new Locale("en", "US");
}
public MatchingRuleRegistry getMatchingRuleRegistry() {
return matchingRuleRegistry;
}
public SystemConfigurationType getSystemConfiguration() throws SchemaException {
PrismObject<SystemConfigurationType> config = systemObjectCache.getSystemConfiguration(new OperationResult("dummy"));
return config != null ? config.asObjectable() : null;
}
public SystemConfigurationType getSystemConfigurationIfAvailable() {
try {
PrismObject<SystemConfigurationType> config = systemObjectCache.getSystemConfiguration(new OperationResult("dummy"));
return config != null ? config.asObjectable() : null;
} catch (SchemaException e) {
LoggingUtils.logUnexpectedException(LOGGER, "Couldn't retrieve system configuration", e);
return null;
}
}
public static MidPointApplication get() {
return (MidPointApplication) WebApplication.get();
}
public Task createSimpleTask(String operation) {
MidPointPrincipal user = SecurityUtils.getPrincipalUser();
if (user == null) {
throw new RestartResponseException(PageLogin.class);
}
return WebModelServiceUtils.createSimpleTask(operation, user.getUser().asPrismObject(), getTaskManager());
}
public AsyncWebProcessManager getAsyncWebProcessManager() {
return asyncWebProcessManager;
}
public SecurityContextManager getSecurityContextManager() {
return securityContextManager;
}
private static class Utf8BundleStringResourceLoader implements IStringResourceLoader {
private final String bundleName;
public Utf8BundleStringResourceLoader(String bundleName) {
this.bundleName = bundleName;
}
@Override
public String loadStringResource(Class<?> clazz, String key, Locale locale, String style, String variation) {
return loadStringResource((Component) null, key, locale, style, variation);
}
@Override
public String loadStringResource(Component component, String key, Locale locale, String style, String variation) {
if (locale == null) {
locale = Session.exists() ? Session.get().getLocale() : Locale.getDefault();
}
ResourceBundle.Control control = new UTF8Control();
try {
return ResourceBundle.getBundle(bundleName, locale, control).getString(key);
} catch (MissingResourceException ex) {
try {
return ResourceBundle.getBundle(bundleName, locale,
Thread.currentThread().getContextClassLoader(), control).getString(key);
} catch (MissingResourceException ex2) {
return null;
}
}
}
private static class UTF8Control extends ResourceBundle.Control {
@Override
public ResourceBundle newBundle(String baseName, Locale locale, String format, ClassLoader loader,
boolean reload) throws IllegalAccessException, InstantiationException,
IOException {
// The below is a copy of the default implementation.
String bundleName = toBundleName(baseName, locale);
String resourceName = toResourceName(bundleName, "properties");
ResourceBundle bundle = null;
InputStream stream = null;
if (reload) {
URL url = loader.getResource(resourceName);
if (url != null) {
URLConnection connection = url.openConnection();
if (connection != null) {
connection.setUseCaches(false);
stream = connection.getInputStream();
}
}
} else {
stream = loader.getResourceAsStream(resourceName);
}
if (stream != null) {
try {
// Only this line is changed to make it to read properties files as UTF-8.
bundle = new PropertyResourceBundle(new InputStreamReader(stream, "UTF-8"));
} finally {
IOUtils.closeQuietly(stream);
}
}
return bundle;
}
}
}
}