use of org.apache.cxf.configuration.security.AuthorizationPolicy in project teiid by teiid.
the class BaseWSConnection method setDispatchProperties.
private <T> void setDispatchProperties(Dispatch<T> dispatch, String binding) {
if (this.mcf.getConfig().getAsSecurityType() == WSConfiguration.SecurityType.HTTPBasic || this.mcf.getConfig().getAsSecurityType() == WSConfiguration.SecurityType.Digest) {
String userName = this.mcf.getConfig().getAuthUserName();
String password = this.mcf.getConfig().getAuthPassword();
// if security-domain is specified and caller identity is used; then use
// credentials from subject
Subject subject = getSubject();
if (subject != null) {
userName = getUserName(subject, userName);
password = getPassword(subject, userName, password);
}
AuthorizationPolicy policy = new AuthorizationPolicy();
policy.setUserName(userName);
policy.setPassword(password);
if (this.mcf.getConfig().getAsSecurityType() == WSConfiguration.SecurityType.Digest) {
policy.setAuthorizationType("Digest");
} else {
policy.setAuthorizationType("Basic");
}
dispatch.getRequestContext().put(AuthorizationPolicy.class.getName(), policy);
} else if (this.mcf.getConfig().getAsSecurityType() == WSConfiguration.SecurityType.Kerberos) {
boolean credentialFound = false;
Subject subject = getSubject();
if (subject != null) {
GSSCredential credential = getSecurityCredential(subject, GSSCredential.class);
if (credential != null) {
dispatch.getRequestContext().put(GSSCredential.class.getName(), credential);
credentialFound = true;
}
}
if (!credentialFound) {
// $NON-NLS-1$
throw new WebServiceException(WSConnectionFactory.UTIL.getString("no_gss_credential"));
}
} else if (this.mcf.getConfig().getAsSecurityType() == WSConfiguration.SecurityType.OAuth) {
boolean credentialFound = false;
Subject subject = getSubject();
if (subject != null) {
OAuthCredential credential = getSecurityCredential(subject, OAuthCredential.class);
if (credential != null) {
dispatch.getRequestContext().put(OAuthCredential.class.getName(), credential);
credentialFound = true;
}
}
if (!credentialFound) {
// $NON-NLS-1$
throw new WebServiceException(WSConnectionFactory.UTIL.getString("no_oauth_credential"));
}
}
if (this.mcf.getConfig().getRequestTimeout() != null) {
dispatch.getRequestContext().put(RECEIVE_TIMEOUT, this.mcf.getConfig().getRequestTimeout());
}
if (this.mcf.getConfig().getConnectTimeout() != null) {
dispatch.getRequestContext().put(CONNECTION_TIMEOUT, this.mcf.getConfig().getConnectTimeout());
}
if (HTTPBinding.HTTP_BINDING.equals(binding)) {
Map<String, List<String>> httpHeaders = (Map<String, List<String>>) dispatch.getRequestContext().get(MessageContext.HTTP_REQUEST_HEADERS);
if (httpHeaders == null) {
httpHeaders = new HashMap<String, List<String>>();
}
// $NON-NLS-1$ //$NON-NLS-2$
httpHeaders.put("Content-Type", Collections.singletonList("text/xml; charset=utf-8"));
// $NON-NLS-1$ //$NON-NLS-2$
httpHeaders.put("User-Agent", Collections.singletonList("Teiid Server"));
dispatch.getRequestContext().put(MessageContext.HTTP_REQUEST_HEADERS, httpHeaders);
}
}
use of org.apache.cxf.configuration.security.AuthorizationPolicy in project tesb-rt-se by Talend.
the class SecurityArgumentsTest method buildDigestAuthorizationPolicy.
@Test
public void buildDigestAuthorizationPolicy() throws Exception {
EsbSecurity esbSecurity = EsbSecurity.fromString("DIGEST");
String username = "username";
String password = "password";
String alias = "alias";
STSClientUtils stsClientUtils = new STSClientUtils(new HashMap<>());
SecurityArguments sa = new SecurityArguments(esbSecurity, null, username, password, alias, null, null, null, null, stsClientUtils);
AuthorizationPolicy p = sa.buildAuthorizationPolicy();
assertSame(p.getPassword(), password);
assertSame(p.getUserName(), username);
assertSame(p.getAuthorizationType(), "Digest");
}
use of org.apache.cxf.configuration.security.AuthorizationPolicy in project tesb-rt-se by Talend.
the class MessageToEventMapper method mapToEvent.
/**
* Map to event.
*
* @param message
* the message
* @return the event
*/
public Event mapToEvent(Message message) {
Event event = new Event();
MessageInfo messageInfo = new MessageInfo();
Originator originator = new Originator();
boolean isRestMessage = isRestMessage(message);
event.setMessageInfo(messageInfo);
event.setOriginator(originator);
String content = getPayload(message);
event.setContent(content);
handleContentLength(event);
event.setEventType(null);
Date date = new Date();
event.setTimestamp(date);
// if (isRestMessage) {
// String queryString = (String) message.get(Message.QUERY_STRING);
// if (queryString == null && message.getExchange().getInMessage() != null) {
// queryString = (String) message.getExchange().getInMessage().get(Message.QUERY_STRING);
// }
// if (queryString != null && queryString.contains("_wadl")) {
// return null;
// }
// }
messageInfo.setFlowId(FlowIdHelper.getFlowId(message));
if (!isRestMessage) {
messageInfo.setMessageId(getMessageId(message));
ServiceInfo serviceInfo = message.getExchange().getBinding().getBindingInfo().getService();
if (null != serviceInfo) {
String portTypeName = serviceInfo.getInterface().getName().toString();
messageInfo.setPortType(portTypeName);
messageInfo.setOperationName(getOperationName(message));
}
SoapBinding soapBinding = (SoapBinding) message.getExchange().getBinding();
if (soapBinding.getBindingInfo() instanceof SoapBindingInfo) {
SoapBindingInfo soapBindingInfo = (SoapBindingInfo) soapBinding.getBindingInfo();
messageInfo.setTransportType(soapBindingInfo.getTransportURI());
}
} else {
messageInfo.setTransportType("http://cxf.apache.org/transports/http");
messageInfo.setPortType(message.getExchange().getEndpoint().getEndpointInfo().getName().toString());
String opName = getRestOperationName(message);
messageInfo.setOperationName(opName);
}
if (messageInfo.getTransportType() == null) {
messageInfo.setTransportType("Unknown transport type");
}
// add custom properties from CXF properties
if (null != message.getExchange().getEndpoint().get(EventFeature.SAM_PROPERTIES)) {
Map<String, String> customProp = (Map<String, String>) message.getExchange().getEndpoint().get(EventFeature.SAM_PROPERTIES);
event.getCustomInfo().putAll(customProp);
}
String addr = message.getExchange().getEndpoint().getEndpointInfo().getAddress();
if (null != addr) {
event.getCustomInfo().put("address", addr);
}
String correlationId = CorrelationIdHelper.getCorrelationId(message);
if (null != correlationId) {
event.getCustomInfo().put("CorrelationID", correlationId);
}
try {
InetAddress inetAddress = InetAddress.getLocalHost();
originator.setIp(inetAddress.getHostAddress());
originator.setHostname(inetAddress.getHostName());
} catch (UnknownHostException e) {
originator.setHostname("Unknown hostname");
originator.setIp("Unknown ip address");
}
originator.setProcessId(Converter.getPID());
if (isRestMessage) {
// String queryString = (String) message.get(Message.QUERY_STRING);
// if (null == queryString && null != message.getExchange().getInMessage()) {
// queryString = (String) message.getExchange().getInMessage().get(Message.QUERY_STRING);
// }
// if (null != queryString) {
// event.getCustomInfo().put("Query String", queryString);
// }
String accept = (String) message.get(Message.ACCEPT_CONTENT_TYPE);
if (null != accept) {
event.getCustomInfo().put("Accept Type", accept);
}
// String httpMethod = (String) message.get(Message.HTTP_REQUEST_METHOD);
// if (null != httpMethod) {
// event.getCustomInfo().put("HTTP Method", httpMethod);
// }
String contentType = (String) message.get(Message.CONTENT_TYPE);
if (null != contentType) {
event.getCustomInfo().put("Content Type", contentType);
}
Integer responseCode = (Integer) message.get(Message.RESPONSE_CODE);
if (null != responseCode) {
event.getCustomInfo().put("Response Code", responseCode.toString());
}
}
SecurityContext sc = message.get(SecurityContext.class);
if (sc != null && sc.getUserPrincipal() != null) {
originator.setPrincipal(sc.getUserPrincipal().getName());
}
if (originator.getPrincipal() == null) {
AuthorizationPolicy authPolicy = message.get(AuthorizationPolicy.class);
if (authPolicy != null) {
originator.setPrincipal(authPolicy.getUserName());
}
}
EventTypeEnum eventType = getEventType(message);
event.setEventType(eventType);
CustomInfo customInfo = CustomInfo.getOrCreateCustomInfo(message);
// System.out.println("custom props: " + customInfo);
event.getCustomInfo().putAll(customInfo);
return event;
}
use of org.apache.cxf.configuration.security.AuthorizationPolicy in project tesb-rt-se by Talend.
the class SecurityArguments method buildAuthorizationPolicy.
public AuthorizationPolicy buildAuthorizationPolicy() {
AuthorizationPolicy authzPolicy = null;
if (EsbSecurity.BASIC == esbSecurity) {
authzPolicy = new AuthorizationPolicy();
authzPolicy.setUserName(username);
authzPolicy.setPassword(password);
authzPolicy.setAuthorizationType(HttpAuthHeader.AUTH_TYPE_BASIC);
} else if (EsbSecurity.DIGEST == esbSecurity) {
authzPolicy = new AuthorizationPolicy();
authzPolicy.setUserName(username);
authzPolicy.setPassword(password);
authzPolicy.setAuthorizationType(HttpAuthHeader.AUTH_TYPE_DIGEST);
}
return authzPolicy;
}
use of org.apache.cxf.configuration.security.AuthorizationPolicy in project tesb-rt-se by Talend.
the class MessageToEventMapperTest method testMapEventRest.
@Test
public void testMapEventRest() throws IOException, EndpointException {
QName portType = new QName("PORT_TYPE");
EndpointInfo info = EasyMock.createMock(EndpointInfo.class);
EasyMock.expect(info.getName()).andReturn(portType).anyTimes();
EasyMock.expect(info.getAddress()).andReturn(null).anyTimes();
EasyMock.replay(info);
Endpoint endpoint = EasyMock.createMock(Endpoint.class);
EasyMock.expect(endpoint.getEndpointInfo()).andReturn(info).anyTimes();
Map<String, String> samProperties = new HashMap<String, String>();
EasyMock.expect(endpoint.get(EventFeature.SAM_PROPERTIES)).andReturn(samProperties).anyTimes();
EasyMock.replay(endpoint);
Message outMessage = EasyMock.createMock(Message.class);
EasyMock.expect(outMessage.containsKey(Message.HTTP_REQUEST_METHOD)).andReturn(true).anyTimes();
EasyMock.expect(outMessage.get(Message.HTTP_REQUEST_METHOD)).andReturn("POST").anyTimes();
EasyMock.expect(outMessage.containsKey(Message.REQUEST_URI)).andReturn(true).anyTimes();
EasyMock.expect(outMessage.get(Message.REQUEST_URI)).andReturn("REQUEST_URI").anyTimes();
EasyMock.expect(outMessage.containsKey(Message.BASE_PATH)).andReturn(true).anyTimes();
EasyMock.expect(outMessage.get(Message.BASE_PATH)).andReturn("REQUEST_URI").anyTimes();
EasyMock.replay(outMessage);
Exchange e = EasyMock.createMock(Exchange.class);
EasyMock.expect(e.getOutMessage()).andReturn(outMessage).anyTimes();
EasyMock.expect(e.getOutFaultMessage()).andReturn(null).anyTimes();
EasyMock.expect(e.getInFaultMessage()).andReturn(null).anyTimes();
EasyMock.expect(e.getBinding()).andReturn(null).anyTimes();
EasyMock.expect(e.getEndpoint()).andReturn(endpoint).anyTimes();
EasyMock.expect(e.get("org.apache.cxf.resource.operation.name")).andReturn("operationName").anyTimes();
EasyMock.replay(e);
AuthorizationPolicy authPolicy = EasyMock.createMock(AuthorizationPolicy.class);
EasyMock.expect(authPolicy.getUserName()).andReturn("USERNAME").anyTimes();
EasyMock.replay(authPolicy);
CachedOutputStream cos = new CachedOutputStream();
cos.write(1);
cos.write(2);
cos.write(3);
Message message = EasyMock.createNiceMock(Message.class);
EasyMock.expect(message.entrySet()).andReturn(null).anyTimes();
EasyMock.expect(message.get(Message.REQUESTOR_ROLE)).andReturn(true).anyTimes();
EasyMock.expect(message.getExchange()).andReturn(e).anyTimes();
EasyMock.expect(message.get(Message.ENCODING)).andReturn("UTF-8").anyTimes();
EasyMock.expect(message.getContent(CachedOutputStream.class)).andReturn(cos).anyTimes();
EasyMock.expect(message.get("FlowId")).andReturn(FlowID).anyTimes();
EasyMock.expect(message.get(CorrelationIdHelper.CORRELATION_ID_KEY)).andReturn("CORRELATION_ID_KEY").anyTimes();
EasyMock.expect(message.get(Message.ACCEPT_CONTENT_TYPE)).andReturn("XML").anyTimes();
EasyMock.expect(message.get(Message.CONTENT_TYPE)).andReturn("XML").anyTimes();
EasyMock.expect(message.get(Message.RESPONSE_CODE)).andReturn(0).anyTimes();
EasyMock.expect(message.get(SecurityContext.class)).andReturn(null).anyTimes();
EasyMock.expect(message.get(AuthorizationPolicy.class)).andReturn(authPolicy).anyTimes();
EasyMock.expect(message.get(CustomInfo.class)).andReturn(EasyMock.createMock(CustomInfo.class)).anyTimes();
EasyMock.replay(message);
MessageToEventMapper mapper = new MessageToEventMapper();
mapper.setMaxContentLength(2);
Event event = mapper.mapToEvent(message);
Assert.assertEquals(EventTypeEnum.RESP_IN, event.getEventType());
Assert.assertEquals("PORT_TYPE", event.getMessageInfo().getPortType());
Assert.assertEquals("POST[/]", event.getMessageInfo().getOperationName());
Assert.assertEquals("http://cxf.apache.org/transports/http", event.getMessageInfo().getTransportType());
Assert.assertEquals(FlowID, event.getMessageInfo().getFlowId());
Assert.assertNull(event.getMessageInfo().getMessageId());
}
Aggregations