-
Notifications
You must be signed in to change notification settings - Fork 138
/
Realm.java
376 lines (322 loc) · 14.3 KB
/
Realm.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
/*
* 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 com.sun.enterprise.security.SecurityLoggerInfo.realmCreated;
import static com.sun.enterprise.security.auth.realm.RealmsManagerHolder.getNonNullRealmsManager;
import static com.sun.enterprise.security.auth.realm.RealmsManagerHolder.getRealmsManager;
import static java.util.logging.Level.INFO;
import com.sun.enterprise.security.SecurityLoggerInfo;
import com.sun.enterprise.security.auth.realm.exceptions.BadRealmException;
import com.sun.enterprise.security.auth.realm.exceptions.NoSuchRealmException;
import java.text.MessageFormat;
import java.util.Enumeration;
import java.util.Properties;
import java.util.logging.Logger;
import org.glassfish.external.probe.provider.PluginPoint;
import org.glassfish.external.probe.provider.StatsProviderManager;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.internal.api.ClassLoaderHierarchy;
import org.glassfish.internal.api.Globals;
import org.jvnet.hk2.annotations.Contract;
/**
* javadoc
*
* @see java.security.Principal
*
* @author Harish Prabandham
* @author Harpreet Singh
* @author Jyri Virkki
* @author Shing Wai Chan
*
*/
@Contract
public abstract class Realm extends AbstractGlassFishRealmState implements GlassFishUserStore, GlassFishUserManagement {
protected static final Logger _logger = SecurityLoggerInfo.getLogger();
// Keep a mapping from "default" to default realm (if no such named
// realm is present) for the sake of all the hardcoded accesses to it.
// This needs to be removed as part of RI security service cleanup.
final static String RI_DEFAULT = "default";
private static RealmStatsProvider realmStatsProvier;
/**
* Instantiate a Realm with the given name and properties using the Class name given. This method is used by iAS and not
* RI.
*
* @param name Name of the new realm.
* @param className Java Class name of the realm to create.
* @param props Properties containing values of the Property element from server.xml
* @returns Reference to the new Realm. The Realm class keeps an internal list of all instantiated realms.
* @throws BadRealmException If the requested realm cannot be instantiated.
*
*/
public static synchronized Realm instantiate(String name, String className, Properties props) throws BadRealmException {
// Register the realm provider
registerRealmStatsProvier();
Realm realmClass = _getInstance(name);
if (realmClass == null) {
realmClass = doInstantiate(name, className, props);
getRealmsManager().putIntoLoadedRealms(name, realmClass);
}
return realmClass;
}
/**
* Instantiate a Realm with the given name and properties using the Class name given. This method is used by iAS and not
* RI.
*
* @param name Name of the new realm.
* @param className Java Class name of the realm to create.
* @param props Properties containing values of the Property element from server.xml
* @param configName the config to which this realm belongs
* @returns Reference to the new Realm. The Realm class keeps an internal list of all instantiated realms.
* @throws BadRealmException If the requested realm cannot be instantiated.
*
*/
public static synchronized Realm instantiate(String name, String className, Properties props, String configName) throws BadRealmException {
// Register the realm provider
registerRealmStatsProvier();
Realm realmClass = _getInstance(configName, name);
if (realmClass == null) {
realmClass = doInstantiate(name, className, props);
getRealmsManager().putIntoLoadedRealms(configName, name, realmClass);
}
return realmClass;
}
/**
* Convenience method which returns the Realm object representing the current default realm. Equivalent to
* getInstance(getDefaultRealm()).
*
* @return Realm representing default realm.
* @exception NoSuchRealmException if default realm does not exist
*/
public static synchronized Realm getDefaultInstance() throws NoSuchRealmException {
return getInstance(getDefaultRealm());
}
/**
* Returns the realm identified by the name which is passed as a parameter. This function knows about all the realms
* which exist; it is not possible to store (or create) one which is not accessible through this routine.
*
* @param name identifies the realm
* @return the requested realm
* @exception NoSuchRealmException if the realm is invalid
* @exception BadRealmException if realm data structures are bad
*/
public static synchronized Realm getInstance(String name) throws NoSuchRealmException {
Realm instance = _getInstance(name);
if (instance == null) {
throw new NoSuchRealmException(MessageFormat.format("Realm {0} does not exists.", name));
}
return instance;
}
/**
* Returns the realm identified by the name which is passed as a parameter. This function knows about all the realms
* which exist; it is not possible to store (or create) one which is not accessible through this routine.
*
* @param name identifies the realm
* @return the requested realm
* @exception NoSuchRealmException if the realm is invalid
* @exception BadRealmException if realm data structures are bad
*/
public static synchronized Realm getInstance(String configName, String name) throws NoSuchRealmException {
Realm instance = _getInstance(configName, name);
if (instance == null) {
throw new NoSuchRealmException(MessageFormat.format("Realm {0} does not exists.", name));
}
return instance;
}
/**
* Returns the name of the default realm.
*
* @return Default realm name.
*
*/
public static synchronized String getDefaultRealm() {
return getNonNullRealmsManager().getDefaultRealmName();
}
/**
* Sets the name of the default realm.
*
* @param realmName Name of realm to set as default.
*
*/
public static synchronized void setDefaultRealm(String realmName) {
getNonNullRealmsManager().setDefaultRealmName(realmName);
}
/**
* Checks if the given realm name is loaded/valid.
*
* @param String name of the realm to check.
* @return true if realm present, false otherwise.
*/
public static boolean isValidRealm(String name) {
return getNonNullRealmsManager().isValidRealm(name);
}
/**
* Checks if the given realm name is loaded/valid.
*
* @param String name of the realm to check.
* @return true if realm present, false otherwise.
*/
public static boolean isValidRealm(String configName, String name) {
return getNonNullRealmsManager().isValidRealm(configName, name);
}
/**
* Returns the names of accessible realms.
*
* @return set of realm names
*/
public static synchronized Enumeration<String> getRealmNames() {
return getNonNullRealmsManager().getRealmNames();
}
/**
* Remove realm with given name from cache.
*
* @param realmName
* @exception NoSuchRealmException
*/
public static synchronized void unloadInstance(String realmName) throws NoSuchRealmException {
// make sure instance exist
getInstance(realmName);
getNonNullRealmsManager().removeFromLoadedRealms(realmName);
_logger.log(INFO, SecurityLoggerInfo.realmDeleted, realmName);
}
/**
* Remove realm with given name from cache.
*
* @param realmName
* @exception NoSuchRealmException
*/
public static synchronized void unloadInstance(String configName, String realmName) throws NoSuchRealmException {
getNonNullRealmsManager().removeFromLoadedRealms(configName, realmName);
_logger.log(INFO, SecurityLoggerInfo.realmDeleted, realmName);
}
/**
* Replace a Realm instance. Can be used by a Realm subclass to replace a previously initialized instance of itself.
* Future getInstance requests will then obtain the new instance.
*
* <P>
* Minimal error checking is done. The realm being replaced must already exist (instantiate() was previously called),
* the new instance must be fully initialized properly and it must of course be of the same class as the previous
* instance.
*
* @param realm The new realm instance.
* @param name The (previously instantiated) name for this realm.
*
*/
protected static synchronized void updateInstance(Realm realm, String name) {
RealmsManager realmsManager = getNonNullRealmsManager();
Realm oldRealm = realmsManager.getFromLoadedRealms(name);
if (!oldRealm.getClass().equals(realm.getClass())) {
// would never happen unless bug in realm subclass
throw new Error("Incompatible class " + realm.getClass() + " in replacement realm " + name);
}
realm.setName(oldRealm.getName());
realmsManager.putIntoLoadedRealms(name, realm);
_logger.log(INFO, SecurityLoggerInfo.realmUpdated, new Object[] { realm.getName() });
}
/**
* Replace a Realm instance. Can be used by a Realm subclass to replace a previously initialized instance of itself.
* Future getInstance requests will then obtain the new instance.
*
* <P>
* Minimal error checking is done. The realm being replaced must already exist (instantiate() was previously called),
* the new instance must be fully initialized properly and it must of course be of the same class as the previous
* instance.
*
* @param realm The new realm instance.
* @param name The (previously instantiated) name for this realm.
*
*/
protected static synchronized void updateInstance(String configName, Realm realm, String name) {
RealmsManager realmsManager = getNonNullRealmsManager();
Realm oldRealm = realmsManager.getFromLoadedRealms(configName, name);
if (!oldRealm.getClass().equals(realm.getClass())) {
// would never happen unless bug in realm subclass
throw new Error("Incompatible class " + realm.getClass() + " in replacement realm " + name);
}
realm.setName(oldRealm.getName());
realmsManager.putIntoLoadedRealms(configName, name, realm);
_logger.log(INFO, SecurityLoggerInfo.realmUpdated, new Object[] { realm.getName() });
}
public static synchronized void getRealmStatsProvier() {
if (realmStatsProvier == null) {
realmStatsProvier = new RealmStatsProvider();
}
}
// ### Private static methods
/**
* Instantiates a Realm class of the given type and invokes its init()
*
*/
private static synchronized Realm doInstantiate(String name, String className, Properties props) throws BadRealmException {
ServiceLocator serviceLocator = Globals.getDefaultBaseServiceLocator();
RealmsManager realmsManager = null;
try {
realmsManager = getRealmsManager();
// Try a HK2 route first
Realm realm = serviceLocator.getService(Realm.class, name);
if (realm == null) {
try {
// TODO: workaround here. Once fixed in V3 we should be able to use
// Context ClassLoader instead.
realm = (Realm) serviceLocator.getService(ClassLoaderHierarchy.class)
.getCommonClassLoader()
.loadClass(className)
.getDeclaredConstructor()
.newInstance();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException ex) {
realm = (Realm) Class.forName(className)
.getDeclaredConstructor()
.newInstance();
}
}
realm.setName(name);
realm.init(props);
if (realmsManager == null) {
throw new BadRealmException("Unable to locate RealmsManager Service");
}
_logger.log(INFO, realmCreated, new Object[] { name, className });
return realm;
} catch (NoSuchRealmException | ReflectiveOperationException ex) {
throw new BadRealmException(ex);
}
}
private static void registerRealmStatsProvier() {
if (realmStatsProvier == null) {
getRealmStatsProvier();
StatsProviderManager.register("security", PluginPoint.SERVER, "security/realm", realmStatsProvier);
}
}
/**
* This is a private method for getting realm instance. If realm does not exist, then it will not return null rather
* than throw exception.
*
* @param name identifies the realm
* @return the requested realm
*/
private static synchronized Realm _getInstance(String name) {
return getNonNullRealmsManager()._getInstance(name);
}
/**
* This is a private method for getting realm instance. If realm does not exist, then it will not return null rather
* than throw exception.
*
* @param name identifies the realm
* @return the requested realm
*/
private static synchronized Realm _getInstance(String configName, String name) {
return getNonNullRealmsManager()._getInstance(configName, name);
}
}