-
Notifications
You must be signed in to change notification settings - Fork 137
/
SecurityLifecycle.java
175 lines (140 loc) · 5.88 KB
/
SecurityLifecycle.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
/*
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package com.sun.enterprise.security;
import static com.sun.enterprise.security.common.Util.writeConfigFileToTempDir;
import static java.util.logging.Level.INFO;
import static org.glassfish.api.event.EventTypes.SERVER_SHUTDOWN;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.api.event.EventListener;
import org.glassfish.api.event.Events;
import org.glassfish.hk2.api.PostConstruct;
import org.glassfish.hk2.api.PreDestroy;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.internal.api.ServerContext;
import org.jvnet.hk2.annotations.Optional;
import org.jvnet.hk2.annotations.Service;
import com.sun.enterprise.security.audit.AuditManager;
import com.sun.enterprise.security.auth.realm.RealmsManager;
import com.sun.enterprise.security.common.Util;
import com.sun.enterprise.security.ssl.SSLUtils;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
/**
* This class extends default implementation of ServerLifecycle interface. It provides security initialization and setup
* for the server.
*
* @author Shing Wai Chan
*/
@Service
@Singleton
public class SecurityLifecycle implements PostConstruct, PreDestroy {
private static final Logger _logger = SecurityLoggerInfo.getLogger();
private static final String SYS_PROP_LOGIN_CONF = "java.security.auth.login.config";
private static final String SYS_PROP_JAVA_SEC_POLICY = "java.security.policy";
@Inject
private ServerContext serverContext;
@Inject
private PolicyLoader policyLoader;
@Inject
private SecurityServicesUtil securityServicesUtil;
@Inject
private Util util;
@Inject
private SSLUtils sslUtils;
@Inject
private SecurityConfigListener configListener;
@Inject
private ServiceLocator serviceLocator;
@Inject
private RealmsManager realmsManager;
@Inject
@Optional
private ContainerSecurityLifecycle eeSecLifecycle;
private EventListener listener;
public SecurityLifecycle() {
try {
if (Util.isEmbeddedServer()) {
// If the user-defined login.conf/server.policy are set as system properties, then they are given priority
if (System.getProperty(SYS_PROP_LOGIN_CONF) == null) {
System.setProperty(SYS_PROP_LOGIN_CONF, writeConfigFileToTempDir("login.conf").getAbsolutePath());
}
if (System.getProperty(SYS_PROP_JAVA_SEC_POLICY) == null) {
System.setProperty(SYS_PROP_JAVA_SEC_POLICY, writeConfigFileToTempDir("server.policy").getAbsolutePath());
}
}
// security manager is set here so that it can be accessed from
// other lifecycles, like PEWebContainer
java.lang.SecurityManager secMgr = System.getSecurityManager();
if (_logger.isLoggable(INFO)) {
if (secMgr != null) {
_logger.info(SecurityLoggerInfo.secMgrEnabled);
} else {
_logger.info(SecurityLoggerInfo.secMgrDisabled);
}
}
} catch (Exception ex) {
_logger.log(Level.SEVERE, "java_security.init_securitylifecycle_fail", ex);
throw new RuntimeException(ex.toString(), ex);
}
}
// override default
public void onInitialization() {
try {
_logger.log(INFO, SecurityLoggerInfo.secServiceStartupEnter);
// TODO:V3 LoginContextDriver has a static variable dependency on BaseAuditManager
// And since LoginContextDriver has too many static methods that use BaseAuditManager
// we have to make this workaround here.
// Init Jakarta Authorization
policyLoader.loadPolicy();
realmsManager.createRealms();
// Start the audit mechanism
AuditManager auditManager = securityServicesUtil.getAuditManager();
auditManager.loadAuditModules();
// Audit the server started event
auditManager.serverStarted();
// initRoleMapperFactory is in J2EEServer.java and not moved to here
// this is because a DummyRoleMapperFactory is register due
// to invocation of ConnectorRuntime.createActiveResourceAdapter
// initRoleMapperFactory is called after it
// initRoleMapperFactory();
_logger.log(INFO, SecurityLoggerInfo.secServiceStartupExit);
} catch (Exception ex) {
throw new SecurityLifecycleException(ex);
}
}
@Override
public void postConstruct() {
onInitialization();
listener = new AuditServerShutdownListener();
Events events = serviceLocator.getService(Events.class);
events.register(listener);
}
@Override
public void preDestroy() {
// DO Nothing ?
// TODO:V3 need to see if something needs cleanup
}
// To audit the server shutdown event
public class AuditServerShutdownListener implements EventListener {
@Override
public void event(Event<?> event) {
if (SERVER_SHUTDOWN.equals(event.type())) {
securityServicesUtil.getAuditManager().serverShutdown();
}
}
}
}