-
Notifications
You must be signed in to change notification settings - Fork 138
/
RealmsManager.java
313 lines (259 loc) · 10.7 KB
/
RealmsManager.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
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
* 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.auth.realm;
import static java.util.Collections.synchronizedMap;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.api.admin.ServerEnvironment;
import org.glassfish.hk2.api.ActiveDescriptor;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.utilities.BuilderHelper;
import org.glassfish.internal.api.Globals;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.config.types.Property;
import com.sun.enterprise.config.serverbeans.AuthRealm;
import com.sun.enterprise.config.serverbeans.Config;
import com.sun.enterprise.config.serverbeans.SecurityService;
import com.sun.enterprise.security.SecurityLoggerInfo;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;
/**
*
* @author kumar.jayanti
*/
@Service
@Singleton
public class RealmsManager {
private static final Logger _logger = SecurityLoggerInfo.getLogger();
private static final String DEFAULT_DIGEST_ALGORITHM = "default-digest-algorithm";
// Per domain list of loaded Realms
// Wanted to get rid of Hashtable but the API exporting Enumeration<String> is preventing
// it for now.
private final Map<String, Hashtable<String, Realm>> loadedRealms = synchronizedMap(new HashMap<>());
// Keep track of name of default realm for this domain. This is updated during startup
// using value from server.xml
private volatile String defaultRealmName = "default";
private final RealmsProbeProvider probeProvider = new RealmsProbeProvider();
private String defaultDigestAlgorithm;
@Inject
@Named(ServerEnvironment.DEFAULT_INSTANCE_NAME)
private Config config;
public RealmsManager() {
}
/**
* Checks if the given realm name is loaded/valid.
*
* @param name of the realm to check.
* @return true if realm present, false otherwise.
*/
public boolean isValidRealm(String name) {
if (name == null) {
return false;
}
return configContainsRealm(name, config.getName());
}
/**
* Checks if the given realm name is loaded/valid.
*
* @param name of the realm to check.
* @return true if realm present, false otherwise.
*/
public boolean isValidRealm(String configName, String name) {
if (name == null) {
return false;
}
return configContainsRealm(name, configName);
}
/**
* Returns the names of accessible realms.
*
* @return set of realm names
*/
public Enumeration<String> getRealmNames() {
return getRealmNames(config.getName());
}
Realm _getInstance(String configName, String name) {
Realm retval = null;
retval = configGetRealmInstance(configName, name);
// Some tools as well as numerous other locations assume that
// getInstance("default") always works; keep them from breaking
// until code can be properly cleaned up. 4628429
// Also note that for example the appcontainer will actually create
// a Subject always containing realm='default' so this notion
// needs to be fixed/handled.
if ((retval == null) && (Realm.RI_DEFAULT.equals(name))) {
retval = configGetRealmInstance(configName, getDefaultRealmName());
}
return retval;
}
Realm _getInstance(String name) {
return _getInstance(config.getName(), name);
}
public void removeFromLoadedRealms(String realmName) {
Realm r = removeFromLoadedRealms(config.getName(), realmName);
if (r != null) {
probeProvider.realmRemovedEvent(realmName);
}
}
void putIntoLoadedRealms(String realmName, Realm realm) {
putIntoLoadedRealms(config.getName(), realmName, realm);
probeProvider.realmAddedEvent(realmName);
}
public Realm getFromLoadedRealms(String realmName) {
return configGetRealmInstance(config.getName(), realmName);
}
public Realm getFromLoadedRealms(String configName, String realmName) {
return configGetRealmInstance(configName, realmName);
}
public synchronized String getDefaultRealmName() {
return defaultRealmName;
}
public synchronized void setDefaultRealmName(String defaultRealmName) {
this.defaultRealmName = defaultRealmName;
}
/**
* Returns names of predefined AuthRealms' classes supported by security service.
*
* @returns array of predefind AuthRealms' classes
*
*/
public List<String> getPredefinedAuthRealmClassNames() {
ServiceLocator serviceLocator = Globals.getDefaultBaseServiceLocator();
List<ActiveDescriptor<?>> collection = serviceLocator.getDescriptors(BuilderHelper.createContractFilter(Realm.class.getName()));
List<String> predefinedAuthRealmClassNames = new ArrayList<String>();
for (ActiveDescriptor<?> it : collection) {
predefinedAuthRealmClassNames.add(it.getImplementation());
}
return predefinedAuthRealmClassNames;
}
public void createRealms() {
createRealms(config.getSecurityService(), config);
}
public void createRealms(Config cfg) {
if (cfg == null) {
return;
}
createRealms(cfg.getSecurityService(), cfg);
}
private void setDefaultDigestAlgorithm() {
SecurityService service = config.getSecurityService();
if (service == null) {
return;
}
List<Property> props = service.getProperty();
if (props == null) {
return;
}
Iterator<Property> propsIterator = props.iterator();
while (propsIterator != null && propsIterator.hasNext()) {
Property property = propsIterator.next();
if (property != null && DEFAULT_DIGEST_ALGORITHM.equals(property.getName())) {
defaultDigestAlgorithm = property.getValue();
break;
}
}
}
public String getDefaultDigestAlgorithm() {
return defaultDigestAlgorithm;
}
/**
* Load all configured realms from server.xml and initialize each one. Initialization is done by calling
* Realm.initialize() with its name, class and properties. The name of the default realm is also saved in the Realm
* class for reference during server operation.
*
* <P>
* This method superceeds the RI RealmManager.createRealms() method.
*
*/
private void createRealms(SecurityService securityBean, Config cfg) {
// check if realms are already loaded by admin GUI ?
if (realmsAlreadyLoaded(cfg.getName())) {
return;
}
setDefaultDigestAlgorithm();
try {
if (_logger.isLoggable(Level.FINE)) {
_logger.fine("Initializing configured realms from SecurityService in Domain.xml....");
}
if (securityBean == null) {
securityBean = cfg.getSecurityService();
assert (securityBean != null);
}
// grab default realm name
String defaultRealm = securityBean.getDefaultRealm();
// get set of auth-realms and process each
List<AuthRealm> realms = securityBean.getAuthRealm();
assert (realms != null);
RealmConfig.createRealms(defaultRealm, realms, cfg.getName());
} catch (Exception e) {
_logger.log(Level.SEVERE, SecurityLoggerInfo.noRealmsError, e);
}
}
private boolean realmsAlreadyLoaded(String cfgName) {
Enumeration<String> en = getRealmNames(cfgName);
return (en != null && en.hasMoreElements()) ? true : false;
}
private boolean configContainsRealm(String name, String configName) {
Hashtable<String, Realm> containedRealms = loadedRealms.get(configName);
return (containedRealms != null) ? containedRealms.containsKey(name) : false;
}
private Enumeration<String> getRealmNames(String configName) {
Hashtable<String, Realm> containedRealms = loadedRealms.get(configName);
return (containedRealms != null) ? containedRealms.keys() : null;
}
private Realm configGetRealmInstance(String configName, String realm) {
Hashtable<String, Realm> containedRealms = loadedRealms.get(configName);
return (containedRealms != null) ? (Realm) containedRealms.get(realm) : null;
}
public Realm removeFromLoadedRealms(String configName, String realmName) {
Hashtable<String, Realm> containedRealms = loadedRealms.get(configName);
return (containedRealms != null) ? (Realm) containedRealms.remove(realmName) : null;
}
public void putIntoLoadedRealms(String configName, String realmName, Realm realm) {
Hashtable<String, Realm> containedRealms = loadedRealms.get(configName);
if (containedRealms == null) {
containedRealms = new Hashtable<String, Realm>();
if (configName == null) {
configName = config.getName();
}
loadedRealms.put(configName, containedRealms);
}
containedRealms.put(realmName, realm);
}
public void refreshRealm(String configName, String realmName) {
if (realmName != null && realmName.length() > 0) {
try {
Realm realm = Realm.getInstance(configName, realmName);
if (realm != null) {
realm.refresh(configName);
}
} catch (com.sun.enterprise.security.auth.realm.exceptions.NoSuchRealmException nre) {
// _logger.fine("Realm: "+realmName+" is not configured");
} catch (com.sun.enterprise.security.auth.realm.exceptions.BadRealmException bre) {
// _logger.fine("Realm: "+realmName+" is not configured");
}
}
}
}