/
DQPWorkContext.java
354 lines (296 loc) · 9.63 KB
/
DQPWorkContext.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
/*
* JBoss, Home of Professional Open Source.
* See the COPYRIGHT.txt file distributed with this work for information
* regarding copyright ownership. Some portions may be licensed
* to Red Hat, Inc. under one or more contributor license agreements.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
package org.teiid.dqp.internal.process;
import java.io.Serializable;
import java.security.Principal;
import java.security.acl.Group;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import javax.security.auth.Subject;
import org.teiid.adminapi.DataPolicy;
import org.teiid.adminapi.impl.SessionMetadata;
import org.teiid.adminapi.impl.VDBMetaData;
import org.teiid.client.security.SessionToken;
import org.teiid.core.util.PropertiesUtils;
import org.teiid.dqp.message.RequestID;
import org.teiid.logging.LogManager;
import org.teiid.metadata.MetadataFactory;
import org.teiid.query.metadata.SystemMetadata;
import org.teiid.query.metadata.TransformationMetadata;
import org.teiid.security.SecurityHelper;
public class DQPWorkContext implements Serializable {
private static final String TEIID_VDB = "teiid-vdb"; //$NON-NLS-1$
private static final String TEIID_SESSION = "teiid-session"; //$NON-NLS-1$
private static final long serialVersionUID = -6389893410233192977L;
private static final boolean longDatesTimes = PropertiesUtils.getBooleanProperty(System.getProperties(), "org.teiid.longDatesTimes", false); //$NON-NLS-1$
public enum Version {
SEVEN_1("7.1", (byte)0), //$NON-NLS-1$
SEVEN_3("7.3", (byte)0), //$NON-NLS-1$
SEVEN_4("7.4", (byte)0), //$NON-NLS-1$
EIGHT_0("8.0", (byte)(longDatesTimes?0:1)), //$NON-NLS-1$
EIGHT_2("8.2", (byte)2); //$NON-NLS-1$
private String string;
private byte clientSerializationVersion;
private Version(String string, byte clientSerializationVersion) {
this.string = string;
this.clientSerializationVersion = clientSerializationVersion;
}
public byte getClientSerializationVersion() {
return clientSerializationVersion;
}
@Override
public String toString() {
return string;
}
private static TreeMap<String, Version> versionMap = new TreeMap<String, Version>();
static {
for (Version v : Version.values()) {
versionMap.put(v.toString(), v);
}
}
public static Version getVersion(String version) {
Map.Entry<String, Version> v = versionMap.floorEntry(version);
if (v == null) {
return SEVEN_1;
}
return v.getValue();
}
}
private static ThreadLocal<DQPWorkContext> CONTEXTS = new ThreadLocal<DQPWorkContext>() {
protected DQPWorkContext initialValue() {
return new DQPWorkContext();
}
};
public static DQPWorkContext getWorkContext() {
return CONTEXTS.get();
}
public static void setWorkContext(DQPWorkContext context) {
LogManager.removeMdc(TEIID_SESSION);
LogManager.removeMdc(TEIID_VDB);
if (context == null) {
CONTEXTS.remove();
} else {
if (context.session != null) {
LogManager.putMdc(TEIID_SESSION, context.session.getSessionId());
if (context.session.getVdb() != null) {
LogManager.putMdc(TEIID_VDB, context.session.getVdb().getFullName());
}
}
CONTEXTS.set(context);
}
}
private SessionMetadata session = new SessionMetadata();
private String clientAddress;
private String clientHostname;
private SecurityHelper securityHelper;
private HashMap<String, DataPolicy> policies;
private boolean useCallingThread;
private Version clientVersion = Version.SEVEN_4;
private boolean admin;
private MetadataFactory metadataFactory;
public DQPWorkContext() {
}
public boolean useCallingThread() {
return useCallingThread;
}
public void setUseCallingThread(boolean useCallingThread) {
this.useCallingThread = useCallingThread;
}
public SessionMetadata getSession() {
return session;
}
public void setSession(SessionMetadata session) {
this.session = session;
this.policies = null;
}
public void setSecurityHelper(SecurityHelper securityHelper) {
this.securityHelper = securityHelper;
}
public SecurityHelper getSecurityHelper() {
return securityHelper;
}
/**
* @return
*/
public String getUserName() {
return session.getUserName();
}
public Subject getSubject() {
return session.getSubject();
}
/**
* @return
*/
public String getVdbName() {
return session.getVDBName();
}
/**
* @return
*/
public int getVdbVersion() {
return session.getVDBVersion();
}
public String getSessionId() {
return this.session.getSessionId();
}
public String getAppName() {
return session.getApplicationName();
}
public RequestID getRequestID(long exeuctionId) {
return new RequestID(this.getSessionId(), exeuctionId);
}
public SessionToken getSessionToken() {
return session.getSessionToken();
}
public void setClientAddress(String clientAddress) {
this.clientAddress = clientAddress;
}
/**
* Get the client address from the socket transport - not as reported from the client
* @return
*/
public String getClientAddress() {
return clientAddress;
}
public void setClientHostname(String clientHostname) {
this.clientHostname = clientHostname;
}
/**
* Get the client hostname from the socket transport - not as reported from the client
* @return
*/
public String getClientHostname() {
return clientHostname;
}
public String getSecurityDomain() {
return this.session.getSecurityDomain();
}
public Object getSecurityContext() {
return session.getSecurityContext();
}
public void setSecurityContext(Object securityContext) {
session.setSecurityContext(securityContext);
}
public VDBMetaData getVDB() {
return session.getVdb();
}
public <V> V runInContext(Callable<V> callable) throws Throwable {
FutureTask<V> task = new FutureTask<V>(callable);
runInContext(task);
try {
return task.get();
} catch (ExecutionException e) {
throw e.getCause();
}
}
public void runInContext(final Runnable runnable) {
DQPWorkContext previous = DQPWorkContext.getWorkContext();
DQPWorkContext.setWorkContext(this);
Object previousSecurityContext = null;
if (securityHelper != null) {
previousSecurityContext = securityHelper.associateSecurityContext(this.getSecurityContext());
}
try {
runnable.run();
} finally {
if (securityHelper != null) {
securityHelper.associateSecurityContext(previousSecurityContext);
}
DQPWorkContext.setWorkContext(previous);
}
}
public HashMap<String, DataPolicy> getAllowedDataPolicies() {
if (this.policies == null) {
this.policies = new HashMap<String, DataPolicy>();
Set<String> userRoles = getUserRoles();
// get data roles from the VDB
VDBMetaData vdb = getVDB();
TransformationMetadata metadata = vdb.getAttachment(TransformationMetadata.class);
Collection<? extends DataPolicy> allPolicies = null;
if (metadata == null) {
allPolicies = vdb.getDataPolicies();
} else {
allPolicies = metadata.getPolicies().values();
}
for (DataPolicy policy : allPolicies) {
if (matchesPrincipal(userRoles, policy)) {
this.policies.put(policy.getName(), policy);
}
}
}
return this.policies;
}
public void setPolicies(HashMap<String, DataPolicy> policies) {
this.policies = policies;
}
private boolean matchesPrincipal(Set<String> userRoles, DataPolicy policy) {
if (policy.isAnyAuthenticated()) {
return true;
}
return !Collections.disjoint(policy.getMappedRoleNames(), userRoles);
}
private Set<String> getUserRoles() {
if (getSubject() == null) {
return Collections.emptySet();
}
Set<String> roles = new HashSet<String>();
Set<Principal> principals = getSubject().getPrincipals();
for(Principal p: principals) {
// this JBoss specific, but no code level dependencies
if ((p instanceof Group) && p.getName().equals("Roles")){ //$NON-NLS-1$
Group g = (Group)p;
Enumeration<? extends Principal> rolesPrinciples = g.members();
while(rolesPrinciples.hasMoreElements()) {
roles.add(rolesPrinciples.nextElement().getName());
}
}
}
return roles;
}
public Version getClientVersion() {
return clientVersion;
}
public void setClientVersion(Version clientVersion) {
this.clientVersion = clientVersion;
}
public void setAdmin(boolean admin) {
this.admin = admin;
}
public boolean isAdmin() {
return admin;
}
public MetadataFactory getTempMetadataFactory() {
if (this.metadataFactory == null) {
this.metadataFactory = new MetadataFactory("temp", 1, "temp", SystemMetadata.getInstance().getRuntimeTypeMap(), null, null); //$NON-NLS-1$ //$NON-NLS-2$
}
return this.metadataFactory;
}
}