-
Notifications
You must be signed in to change notification settings - Fork 138
/
CommonServerSecurityTube.java
271 lines (232 loc) · 10.4 KB
/
CommonServerSecurityTube.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
/*
* Copyright (c) 1997, 2018 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.webservices;
import com.sun.xml.ws.api.pipe.TubeCloner;
import com.sun.xml.ws.api.pipe.helper.AbstractTubeImpl;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Map;
import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedActionException;
import javax.security.auth.Subject;
import jakarta.security.auth.message.config.*;
import jakarta.security.auth.message.AuthException;
import jakarta.security.auth.message.AuthStatus;
import jakarta.xml.ws.WebServiceException;
import com.sun.enterprise.security.jmac.provider.PacketMapMessageInfo;
import com.sun.enterprise.security.jmac.provider.PacketMessageInfo;
import com.sun.enterprise.security.jmac.provider.config.PipeHelper;
import com.sun.enterprise.util.LocalStringManagerImpl;
import com.sun.xml.ws.api.message.Packet;
import com.sun.xml.ws.api.pipe.NextAction;
import com.sun.xml.ws.api.pipe.Tube;
import com.sun.xml.ws.api.pipe.helper.AbstractFilterTubeImpl;
/**
* This pipe is used to do Jakarta Authentication security
*/
public class CommonServerSecurityTube extends AbstractFilterTubeImpl {
protected static final Logger _logger = LogUtils.getLogger();
protected static final LocalStringManagerImpl localStrings =
new LocalStringManagerImpl(CommonServerSecurityTube.class);
private final boolean isHttpBinding;
private PipeHelper helper;
// Introduced during Pipe to Tube conversion
private ServerAuthContext sAC = null;
private PacketMessageInfo info = null;
private Subject serverSubject = null;
public CommonServerSecurityTube(Map props, final Tube next, boolean isHttpBinding) {
super(next);
props.put(PipeConstants.SECURITY_PIPE, this);
this.helper = new PipeHelper(PipeConstants.SOAP_LAYER, props, null);
this.isHttpBinding = isHttpBinding;
}
protected CommonServerSecurityTube(CommonServerSecurityTube that, TubeCloner cloner) {
super(that, cloner);
// we can share the helper for all pipes so that the remove
// registration (in server side) can be done properly
this.helper = that.helper;
this.isHttpBinding = that.isHttpBinding;
}
/**
* This method is called once in server side and at most one in client side.
*/
@Override
public void preDestroy() {
helper.disable();
/**
Fix for bug 3932/4052
*/
next.preDestroy();
}
@Override
public NextAction processRequest(Packet request) {
try {
if (isHttpBinding) {
return doInvoke(super.next, request);
}
AuthStatus status = AuthStatus.SUCCESS;
info = new PacketMapMessageInfo(request, new Packet());
// XXX at this time, we expect the server subject to be null
serverSubject = (Subject) request.invocationProperties.get(PipeConstants.SERVER_SUBJECT);
//could change the request packet
sAC = helper.getServerAuthContext(info, serverSubject);
Subject clientSubject = getClientSubject(request);
final Packet validatedRequest;
try {
if (sAC != null) {
// client subject must not be null
// and when return status is SUCCESS, module
// must have called handler.handle(CallerPrincipalCallback)
status = sAC.validateRequest(info, clientSubject, serverSubject);
}
} catch (Exception e) {
_logger.log(Level.SEVERE, LogUtils.ERROR_REQUEST_VALIDATION, e);
WebServiceException wse = new WebServiceException(
localStrings.getLocalString("enterprise.webservice.cantValidateRequest",
"Cannot validate request for {0}",
new Object[]{helper.getModelName()}), e);
//set status for audit
status = AuthStatus.SEND_FAILURE;
// if unable to determine if two-way will return empty response
Packet ret = helper.getFaultResponse(info.getRequestPacket(), info.getResponsePacket(), wse);
return doReturnWith(ret);
} finally {
validatedRequest = info.getRequestPacket();
helper.auditInvocation(validatedRequest, status);
}
Packet response = null;
if (status == AuthStatus.SUCCESS) {
boolean authorized = false;
try {
helper.authorize(validatedRequest);
authorized = true;
} catch (Exception e) {
// not authorized, construct fault and proceded
response = helper.getFaultResponse(validatedRequest, info.getResponsePacket(), e);
return doReturnWith(response);
}
if (authorized) {
// only do doAdPriv if SecurityManager is in effect
if (System.getSecurityManager() == null) {
try {
// proceed to invoke the endpoint
return doInvoke(super.next, validatedRequest);
} catch (Exception e) {
_logger.log(Level.SEVERE, LogUtils.NEXT_PIPE, e);
response = helper.getFaultResponse(validatedRequest, info.getResponsePacket(), e);
return doReturnWith(response);
}
} else {
try {
final Tube next = super.next;
NextAction action = (NextAction) Subject.doAsPrivileged(clientSubject, new PrivilegedExceptionAction() {
@Override
public Object run() throws Exception {
// proceed to invoke the endpoint
return doInvoke(next, validatedRequest);
}
}, null);
return action;
} catch (PrivilegedActionException pae) {
Throwable cause = pae.getCause();
_logger.log(Level.SEVERE, LogUtils.NEXT_PIPE, cause);
response = helper.getFaultResponse(validatedRequest, info.getResponsePacket(), cause);
return doReturnWith(response);
}
}
} else { //if not authorized
// not authorized, construct fault and proceded
response = helper.getFaultResponse(
validatedRequest,info.getResponsePacket(), new Exception("Client Not Authorized"));
return doReturnWith(response);
}
} else {
// validateRequest did not return success
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE, "ws.status_validate_request", status);
}
// even for one-way mep, may return response with non-empty message
response = info.getResponsePacket();
return doReturnWith(response);
}
} catch (Throwable t) {
if (!(t instanceof WebServiceException)) {
t = new WebServiceException(t);
}
return doThrow(t);
}
}
@Override
public NextAction processResponse(Packet response) {
try{
//could be oneway
if((response == null) || (response.getMessage() == null)){
return doReturnWith(response);
}
Packet resp = response;
// secure response, including if it is a fault
if (sAC != null && response.getMessage() != null) {
info.setResponsePacket(response);
resp = processResponse(info, sAC, serverSubject);
}
return doReturnWith(resp);
}catch(Throwable t){
if (!(t instanceof WebServiceException)) {
t = new WebServiceException(t);
}
return doThrow(t);
}
}
// called when secureResponse is to be called
private Packet processResponse(PacketMessageInfo info, ServerAuthContext sAC, Subject serverSubject)
throws Exception {
AuthStatus status;
try {
status = sAC.secureResponse(info, serverSubject);
} catch (Exception e) {
if (e instanceof AuthException) {
if (_logger.isLoggable(Level.INFO)) {
_logger.log(Level.INFO, LogUtils.ERROR_RESPONSE_SECURING, e);
}
} else {
_logger.log(Level.SEVERE, LogUtils.ERROR_RESPONSE_SECURING, e);
}
return helper.makeFaultResponse(info.getResponsePacket(),e);
}
if (_logger.isLoggable(Level.FINE)) {
_logger.log(Level.FINE,"ws.status_secure_response", status);
}
return info.getResponsePacket();
}
private static Subject getClientSubject(Packet p) {
Subject s = null;
if (p != null) {
s =(Subject)
p.invocationProperties.get(PipeConstants.CLIENT_SUBJECT);
}
if (s == null) {
s = PipeHelper.getClientSubject();
if (p != null) {
p.invocationProperties.put(PipeConstants.CLIENT_SUBJECT,s);
}
}
return s;
}
@Override
public AbstractTubeImpl copy(TubeCloner cloner) {
return new CommonServerSecurityTube(this, cloner);
}
}