/
EmbeddedPdpBasedAuthzInterceptorTest.java
157 lines (134 loc) · 6.45 KB
/
EmbeddedPdpBasedAuthzInterceptorTest.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
/*
* Copyright 2012-2024 THALES.
*
* This file is part of AuthzForce CE.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.ow2.authzforce.core.pdp.testutil.test.pep.cxf;
import jakarta.xml.ws.BindingProvider;
import jakarta.xml.ws.Service;
import org.apache.coheigea.cxf.sts.xacml.common.STSServer;
import org.apache.coheigea.cxf.sts.xacml.common.TokenTestUtils;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBusFactory;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
import org.example.contract.doubleit.DoubleItPortType;
import org.junit.BeforeClass;
import javax.xml.namespace.QName;
import java.net.URL;
import static org.junit.Assert.*;
/**
* This test is an adaptation of <a href="https://github.com/coheigea/testcases/tree/master/apache/cxf/cxf-sts-xacml">cxf-sts-xacml test</a> for AuthzForce PDP. The client authenticates to the STS
* using a username/password, and gets a signed holder-of-key SAML Assertion in return. This is presented to the service, who verifies proof-of-possession + the signature of the STS on the assertion.
* The CXF endpoint extracts roles from the Assertion + populates the security context. Note that the CXF endpoint requires a "role" Claim via the security policy.
* <p>
* The CXF Endpoint has configured the {@link EmbeddedPdpBasedAuthzInterceptor}, which creates a XACML 3.0 request for dispatch to the (co-located) PDP, and then enforces the PDP's decision.
*
* <p>
* Detailed description:
* </p>
* <p>
* In this test, the client obtains a SAML Token from the STS with the roles of the client embedded in the token. The service provider extracts the roles, and creates a XACML request. This is
* evaluated by a AuthzForce-based PDP which is co-located with the service. After evaluating the request, the PDP response is then enforced at the service side.
* </p>
* <p>
* The CXF associates the following users with roles:
* <ul>
* <li>"alice/boss+employee"</li>
* <li>"bob/employee"</li>
* </ul>
* The client authenticates to the STS using a username/password, and gets a signed holder-of-key SAML Assertion in return. This is presented to the service, who verifies proof-of-possession + the
* signature of the STS on the assertion. The CXF endpoint extracts roles from the Assertion + populates the security context. Note that the CXF endpoint requires a "role" Claim via the security
* policy.
* </p>
* <p>
* The CXF Endpoint set up the XACML 3.0 interceptor to create a XACML request for dispatch to the PDP, and then enforces the PDP's decision.
* </p>
* <p>
* The XACML 3.0 PDP evaluates the requests against some policies which are loaded into the PDP. It uses policies based on the RBAC profile of XACML. The user must have role "boss" to "execute" on the
* service "{http://www.example.org/contract/DoubleIt}DoubleItService#DoubleIt".
* </p>
*/
public class EmbeddedPdpBasedAuthzInterceptorTest extends AbstractBusClientServerTestBase
{
private static final String NAMESPACE = "http://www.example.org/contract/DoubleIt";
private static final QName SERVICE_QNAME = new QName(NAMESPACE, "DoubleItService");
private static final String PORT = allocatePort(Server.class);
private static final String STS_PORT = allocatePort(STSServer.class);
@BeforeClass
public static void startServers()
{
assertTrue("Server failed to launch",
// run the server for the target service (DoubleIt) in the same process
// set this to false to fork
launchServer(Server.class, true));
assertTrue("Server failed to launch",
// run the server in the same process
// set this to false to fork
launchServer(STSServer.class, true));
}
@org.junit.Test
public void testAuthorizedRequest() throws Exception
{
final SpringBusFactory bf = new SpringBusFactory();
final URL busFile = EmbeddedPdpBasedAuthzInterceptorTest.class.getResource("cxf-client.xml");
assert busFile != null;
final Bus bus = bf.createBus(busFile.toString());
SpringBusFactory.setDefaultBus(bus);
SpringBusFactory.setThreadDefaultBus(bus);
final URL wsdl = EmbeddedPdpBasedAuthzInterceptorTest.class.getResource("DoubleItSecure.wsdl");
final Service service = Service.create(wsdl, SERVICE_QNAME);
final QName portQName = new QName(NAMESPACE, "DoubleItTransportPort");
final DoubleItPortType transportPort = service.getPort(portQName, DoubleItPortType.class);
updateAddressPort(transportPort, PORT);
final Client client = ClientProxy.getClient(transportPort);
client.getRequestContext().put("ws-security.username", "alice");
TokenTestUtils.updateSTSPort((BindingProvider) transportPort, STS_PORT);
doubleIt(transportPort, 15);
}
@org.junit.Test
public void testUnauthorizedRequest() throws Exception
{
final SpringBusFactory bf = new SpringBusFactory();
final URL busFile = EmbeddedPdpBasedAuthzInterceptorTest.class.getResource("cxf-client.xml");
assert busFile != null;
final Bus bus = bf.createBus(busFile.toString());
SpringBusFactory.setDefaultBus(bus);
SpringBusFactory.setThreadDefaultBus(bus);
final URL wsdl = EmbeddedPdpBasedAuthzInterceptorTest.class.getResource("DoubleItSecure.wsdl");
final Service service = Service.create(wsdl, SERVICE_QNAME);
final QName portQName = new QName(NAMESPACE, "DoubleItTransportPort");
final DoubleItPortType transportPort = service.getPort(portQName, DoubleItPortType.class);
updateAddressPort(transportPort, PORT);
final Client client = ClientProxy.getClient(transportPort);
client.getRequestContext().put("ws-security.username", "bob");
TokenTestUtils.updateSTSPort((BindingProvider) transportPort, STS_PORT);
try
{
doubleIt(transportPort, 25);
fail("Failure expected on bob");
}
catch (final Exception ex)
{
// expected
}
}
private static void doubleIt(final DoubleItPortType port, final int numToDouble)
{
final int resp = port.doubleIt(numToDouble);
assertEquals(numToDouble * 2, resp);
}
}