-
Notifications
You must be signed in to change notification settings - Fork 30
/
CommonUtils.java
264 lines (218 loc) · 10.5 KB
/
CommonUtils.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
/*
* Copyright (c) 2024 Contributors to Eclipse Foundation.
* Copyright (c) 2011, 2020 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 ee.jakarta.tck.authentication.test.basic.servlet;
import ee.jakarta.tck.authentication.test.basic.sam.ProviderConfigurationEntry;
import ee.jakarta.tck.authentication.test.basic.sam.ProviderConfigurationXMLFileProcessor;
import jakarta.security.auth.message.config.AuthConfigFactory;
import jakarta.security.auth.message.config.AuthConfigProvider;
import jakarta.security.auth.message.config.RegistrationListener;
import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
*
* @author Oracle
*/
public class CommonUtils implements Serializable {
private static final long serialVersionUID = 1L;
public CommonUtils() {
}
public static AuthConfigFactory register(String logFileLocation, String providerConfigurationFileLocation, String vendorAuthConfigFactoryClass) {
AuthConfigFactory acf = null;
try {
printVerticalIndent();
// Get an instance of Vendor's AuthConfigFactory
AuthConfigFactory vFactory = getAuthConfigFactory(vendorAuthConfigFactoryClass);
// Set vendor's AuthConfigFactory
AuthConfigFactory.setFactory(vFactory);
// Get system default AuthConfigFactory
acf = AuthConfigFactory.getFactory();
if (acf != null) {
printIt("Default AuthConfigFactory class name = " + acf.getClass().getName());
} else {
printError("Default AuthConfigFactory is null" + " can't register TestSuite Providers with null");
return null;
}
/**
* Read the ProviderConfiguration XML file This file contains the list of providers that needs to be loaded by the
* vendor's default AuthConfigFactory
*/
Collection<ProviderConfigurationEntry> providerConfigurationEntriesCollection = readProviderConfigurationXMLFile(
providerConfigurationFileLocation);
ProviderConfigurationEntry pce = null;
printVerticalIndent();
Iterator<ProviderConfigurationEntry> iterator = providerConfigurationEntriesCollection.iterator();
while (iterator.hasNext()) {
// obtain each ProviderConfigurationEntry and register it
// with vendor's default AuthConfigFactory
pce = iterator.next();
if (pce != null) {
printIt("pce.getProviderClassName() = " + pce.getProviderClassName());
printIt("pce.getMessageLayer() = " + pce.getMessageLayer());
printIt("pce.getApplicationContextId() = " + pce.getApplicationContextId());
printIt("pce.getRegistrationDescription() = " + pce.getRegistrationDescription());
if (pce.getProperties() != null) {
printIt("pce.getProperties.toString() = " + pce.getProperties().toString());
} else {
printIt("pce.getProperties() = null");
}
printIt("Registering Provider " + pce.getProviderClassName() + " ...");
Map<String, String> newProps = getCleanACPProps(pce.getProperties());
acf.registerConfigProvider(pce.getProviderClassName(), newProps, pce.getMessageLayer(), pce.getApplicationContextId(),
pce.getRegistrationDescription());
printIt("Registration Successful");
} else {
printIt("WARNING: pce was null and probably should not have been.");
}
}
printVerticalIndent();
} catch (SecurityException ex) {
// if here we may not have permission to invoke ACF.getFactory...
String msg = "SecurityException: make sure you have permission to call ACF.getFactory";
msg = msg + "or ACF.setFactory(). Check your server side security policies.";
printIt(msg);
ex.printStackTrace();
} catch (Exception e) {
printIt("Error Registering TestSuite AuthConfig Providers");
e.printStackTrace();
}
return acf;
}
private static Map<String, String> getCleanACPProps(Map<String, String> origProps) {
if (origProps == null) {
return null;
}
HashMap<String, String> props = new HashMap<>();
// loop thru passed in props and remove anything that is
// not of type String since only Strings are allowed in our
// calls to registerConfigProvider()
for (String key : origProps.keySet()) {
if (key != null) {
Object val = origProps.get(key);
if ((val != null) && ((val instanceof java.lang.String))) {
// we found entry that is not String so remove it
props.put(key, (String) val);
printIt("added key=" + key + " with value = " + val);
} else {
printIt("found non-string value for key=" + key);
}
}
}
return props;
}
/**
* This method instantiates and ruturns a AuthConfigFactory based on the specified className
*/
public static AuthConfigFactory getAuthConfigFactory(String className) {
AuthConfigFactory vFactory = null;
if (className != null) {
try {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class clazz = Class.forName(className, true, loader);
vFactory = (AuthConfigFactory) clazz.newInstance();
printIt("Instantiated Vendor's AuthConfigFactory");
} catch (Exception e) {
printIt("Error instantiating vendor's " + "AuthConfigFactory class :" + className);
e.printStackTrace();
}
}
return vFactory;
}
/**
* This method resets the default ACF.
*
*/
public static void resetDefaultACF() throws Exception {
try {
printIt("resetting ACF back to CTS default: " + JASPICData.TSSV_ACF);
AuthConfigFactory origAcf = getAuthConfigFactory(JASPICData.TSSV_ACF);
AuthConfigFactory.setFactory(origAcf);
} catch (Exception ex) {
printIt("Exception while trying to restore original factory class in ACFSwitchFactorys");
throw ex;
}
}
/*
* Read the provider configuration XML file and registers each provider with Vendor's default AuthConfigFactory
*/
public static Collection<ProviderConfigurationEntry> readProviderConfigurationXMLFile(String acpCfgFile) {
Collection<ProviderConfigurationEntry> providerConfigurationEntriesCollection = null;
printIt("Reading TestSuite Providers from :" + acpCfgFile);
try {
// Given the provider configuration xml file
// This reader parses the xml file and stores the configuration
// entries as a collection.
ProviderConfigurationXMLFileProcessor configFileProcessor = new ProviderConfigurationXMLFileProcessor(acpCfgFile);
// Retrieve the ProviderConfigurationEntries collection
providerConfigurationEntriesCollection = configFileProcessor.getProviderConfigurationEntriesCollection();
printIt("TestSuite Providers read successfully " + "from ProviderConfiguration.xml file");
return providerConfigurationEntriesCollection;
} catch (Exception e) {
printIt("Error loading Providers");
e.printStackTrace();
}
return null;
}
public static void printVerticalIndent() {
printIt("**********************************************************");
printIt("\n");
}
public static void dumpProviders(AuthConfigFactory acf, String[] regIDs) {
printIt("****************************************************************");
printIt("******* Dumping AuthConfigFactory registered providers *******");
for (int ii = 0; ii < regIDs.length; ii++) {
if (regIDs[ii] != null) {
printIt("registrationID[" + ii + "] = " + regIDs[ii]);
AuthConfigFactory.RegistrationContext rc = acf.getRegistrationContext(regIDs[ii]);
printIt("AppContext = " + rc.getAppContext());
printIt("Message layer = " + rc.getMessageLayer());
printIt("Description = " + rc.getDescription());
printIt("isPersistent = " + rc.isPersistent());
}
}
printIt("****************************************************************");
printIt(" ");
}
public static String getRegisteredProviderID(String msgLayer, String appContext, RegistrationListener rl) {
String regID = null;
AuthConfigFactory acf = AuthConfigFactory.getFactory();
AuthConfigProvider acp = acf.getConfigProvider(msgLayer, appContext, rl);
String[] allIDs = acf.getRegistrationIDs(acp);
for (int ii = 0; ii < allIDs.length; ii++) {
AuthConfigFactory.RegistrationContext rc = acf.getRegistrationContext(allIDs[ii]);
if (rc != null) {
String mlayer = rc.getMessageLayer();
String appctxt = rc.getAppContext();
if (msgLayer.equals(mlayer) && appContext.equals(appctxt)) {
regID = allIDs[ii];
break;
}
} else {
printIt("no registrationContext for regID = " + allIDs[ii]);
}
}
return regID;
}
public static void printIt(String str) {
System.out.println(str);
}
public static void printError(String str) {
System.err.println(str);
}
}