Skip to content

Commit 8594728

Browse files
committed
[PAXCDI-191] Provide sample and test for DeltaSpike Security
1 parent 9bee1c8 commit 8594728

File tree

11 files changed

+418
-0
lines changed

11 files changed

+418
-0
lines changed

itest/src/it/itest-standalone/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@
137137
<scope>provided</scope>
138138
</dependency>
139139

140+
<dependency>
141+
<groupId>org.ops4j.pax.cdi.samples</groupId>
142+
<artifactId>pax-cdi-sample8-security</artifactId>
143+
<version>${project.version}</version>
144+
<scope>provided</scope>
145+
</dependency>
146+
140147
<dependency>
141148
<groupId>org.ops4j.pax.cdi</groupId>
142149
<artifactId>pax-cdi-extension</artifactId>
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2015 Harald Wellmann.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.ops4j.pax.cdi.test;
19+
20+
import static org.ops4j.pax.cdi.test.support.TestConfiguration.cdiProviderBundles;
21+
import static org.ops4j.pax.cdi.test.support.TestConfiguration.paxCdiProviderAdapter;
22+
import static org.ops4j.pax.cdi.test.support.TestConfiguration.regressionDefaults;
23+
import static org.ops4j.pax.cdi.test.support.TestConfiguration.workspaceBundle;
24+
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
25+
import static org.ops4j.pax.exam.CoreOptions.options;
26+
27+
import javax.inject.Inject;
28+
29+
import org.apache.deltaspike.security.api.authorization.AccessDeniedException;
30+
import org.junit.Ignore;
31+
import org.junit.Rule;
32+
import org.junit.Test;
33+
import org.junit.rules.ExpectedException;
34+
import org.junit.runner.RunWith;
35+
import org.ops4j.pax.cdi.sample8.service.SecuredClient;
36+
import org.ops4j.pax.exam.Configuration;
37+
import org.ops4j.pax.exam.Option;
38+
import org.ops4j.pax.exam.junit.PaxExam;
39+
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
40+
import org.ops4j.pax.exam.spi.reactors.PerClass;
41+
42+
@RunWith(PaxExam.class)
43+
@ExamReactorStrategy(PerClass.class)
44+
@Ignore
45+
public class SecurityTest {
46+
47+
@Rule
48+
public ExpectedException thrown = ExpectedException.none();
49+
50+
@Inject
51+
private SecuredClient securedService;
52+
53+
@Configuration
54+
public Option[] config() {
55+
return options(
56+
regressionDefaults(),
57+
paxCdiProviderAdapter(),
58+
cdiProviderBundles(),
59+
60+
mavenBundle("org.osgi", "org.osgi.enterprise").versionAsInProject(),
61+
62+
// DeltaSpike bundles
63+
mavenBundle("org.apache.deltaspike.core", "deltaspike-core-api").versionAsInProject(),
64+
mavenBundle("org.apache.deltaspike.core", "deltaspike-core-impl").versionAsInProject(),
65+
mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-api").versionAsInProject(),
66+
mavenBundle("org.apache.deltaspike.modules", "deltaspike-security-module-impl").versionAsInProject(),
67+
68+
// Sample bundles
69+
workspaceBundle("org.ops4j.pax.cdi.samples", "pax-cdi-sample8"));
70+
}
71+
72+
@Test
73+
public void shouldNotInvokeBlockService() {
74+
thrown.expect(AccessDeniedException.class);
75+
securedService.getBlockedResult();
76+
}
77+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<parent>
4+
<groupId>org.ops4j.pax.cdi</groupId>
5+
<artifactId>pax-cdi-samples</artifactId>
6+
<version>1.0.0-SNAPSHOT</version>
7+
</parent>
8+
<groupId>org.ops4j.pax.cdi.samples</groupId>
9+
<artifactId>pax-cdi-sample8</artifactId>
10+
<packaging>bundle</packaging>
11+
12+
<name>OPS4J Pax CDI Sample8</name>
13+
14+
<dependencies>
15+
16+
<dependency>
17+
<groupId>org.apache.deltaspike.modules</groupId>
18+
<artifactId>deltaspike-security-module-api</artifactId>
19+
<version>${deltaspike.version}</version>
20+
</dependency>
21+
22+
<dependency>
23+
<groupId>org.apache.deltaspike.modules</groupId>
24+
<artifactId>deltaspike-security-module-impl</artifactId>
25+
<version>${deltaspike.version}</version>
26+
</dependency>
27+
28+
<dependency>
29+
<groupId>org.ops4j.pax.cdi</groupId>
30+
<artifactId>pax-cdi-api</artifactId>
31+
<version>${project.version}</version>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.slf4j</groupId>
36+
<artifactId>slf4j-api</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.apache.geronimo.specs</groupId>
41+
<artifactId>geronimo-atinject_1.0_spec</artifactId>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>javax.enterprise</groupId>
46+
<artifactId>cdi-api</artifactId>
47+
<scope>provided</scope>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.osgi</groupId>
52+
<artifactId>org.osgi.core</artifactId>
53+
</dependency>
54+
55+
</dependencies>
56+
<build>
57+
<plugins>
58+
<plugin>
59+
<groupId>org.apache.felix</groupId>
60+
<artifactId>maven-bundle-plugin</artifactId>
61+
<configuration>
62+
<instructions>
63+
<Bundle-SymbolicName>org.ops4j.pax.cdi.sample8.service</Bundle-SymbolicName>
64+
<Import-Package>
65+
org.apache.deltaspike.security.api.authorization,
66+
org.apache.deltaspike.security.spi.authorization,
67+
org.apache.deltaspike.security.impl.extension,
68+
org.apache.deltaspike.security.impl.util,
69+
*
70+
</Import-Package>
71+
<Require-Capability>
72+
org.ops4j.pax.cdi.extension; filter:="(&amp;(extension=pax-cdi-extension)(version&gt;=${version;==;${pax.cdi.osgi.version.clean}})(!(version&gt;=${version;=+;${pax.cdi.osgi.version.clean}})))",
73+
org.ops4j.pax.cdi.extension; filter:="(&amp;(extension=deltaspike-security-module-impl))",
74+
org.ops4j.pax.cdi.extension; filter:="(&amp;(extension=deltaspike-core-impl))",
75+
org.ops4j.pax.cdi.extension; filter:="(&amp;(extension=deltaspike-core-api))",
76+
osgi.extender; filter:="(osgi.extender=pax.cdi)"
77+
</Require-Capability>
78+
</instructions>
79+
</configuration>
80+
<executions>
81+
<execution>
82+
<id>versions</id>
83+
<phase>validate</phase>
84+
<goals>
85+
<goal>cleanVersions</goal>
86+
</goals>
87+
<configuration>
88+
<versions>
89+
<pax.cdi.osgi.version.clean>${project.version}</pax.cdi.osgi.version.clean>
90+
</versions>
91+
</configuration>
92+
</execution>
93+
<execution>
94+
<id>generate-manifest</id>
95+
<phase>process-classes</phase>
96+
<goals>
97+
<goal>manifest</goal>
98+
</goals>
99+
</execution>
100+
</executions>
101+
</plugin>
102+
</plugins>
103+
</build>
104+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2015 Harald Wellmann.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.ops4j.pax.cdi.sample8.service;
19+
20+
public interface SecuredClient {
21+
22+
String getBlockedResult();
23+
24+
String getResult();
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2015 Harald Wellmann.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.ops4j.pax.cdi.sample8.service;
19+
20+
public interface SecuredService {
21+
22+
String getBlockedResult();
23+
24+
String getResult();
25+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2015 Harald Wellmann.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.ops4j.pax.cdi.sample8.service.impl;
19+
20+
import javax.inject.Inject;
21+
22+
import org.ops4j.pax.cdi.api.OsgiService;
23+
import org.ops4j.pax.cdi.api.OsgiServiceProvider;
24+
import org.ops4j.pax.cdi.sample8.service.SecuredClient;
25+
import org.ops4j.pax.cdi.sample8.service.SecuredService;
26+
27+
@OsgiServiceProvider
28+
public class SecuredClientImpl implements SecuredClient {
29+
30+
@Inject
31+
@OsgiService
32+
private SecuredService service;
33+
34+
@Override
35+
public String getBlockedResult() {
36+
return service.getBlockedResult();
37+
}
38+
39+
@Override
40+
public String getResult() {
41+
return service.getResult();
42+
}
43+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2015 Harald Wellmann.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.ops4j.pax.cdi.sample8.service.impl;
19+
20+
import org.apache.deltaspike.security.api.authorization.Secured;
21+
import org.ops4j.pax.cdi.api.OsgiServiceProvider;
22+
import org.ops4j.pax.cdi.sample8.service.SecuredService;
23+
24+
@OsgiServiceProvider
25+
@Secured(TestAccessDecisionVoter.class)
26+
public class SecuredServiceImpl implements SecuredService {
27+
28+
@Override
29+
public String getBlockedResult() {
30+
return "blocked result";
31+
}
32+
33+
@Override
34+
public String getResult() {
35+
return "result";
36+
}
37+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2015 Harald Wellmann.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
* implied.
14+
*
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.ops4j.pax.cdi.sample8.service.impl;
19+
20+
import java.lang.reflect.Method;
21+
import java.util.Collections;
22+
import java.util.HashSet;
23+
import java.util.Set;
24+
25+
import javax.enterprise.context.ApplicationScoped;
26+
import javax.interceptor.InvocationContext;
27+
28+
import org.apache.deltaspike.security.api.authorization.AccessDecisionVoter;
29+
import org.apache.deltaspike.security.api.authorization.AccessDecisionVoterContext;
30+
import org.apache.deltaspike.security.api.authorization.SecurityViolation;
31+
32+
@ApplicationScoped
33+
public class TestAccessDecisionVoter implements AccessDecisionVoter {
34+
35+
private static final long serialVersionUID = 1L;
36+
37+
@Override
38+
public Set<SecurityViolation> checkPermission(
39+
AccessDecisionVoterContext accessDecisionVoterContext) {
40+
Method method = accessDecisionVoterContext.<InvocationContext> getSource().getMethod();
41+
42+
if (!method.getName().contains("Blocked")) {
43+
return Collections.emptySet();
44+
}
45+
46+
Set<SecurityViolation> violations = new HashSet<>();
47+
violations.add(new SecurityViolation() {
48+
49+
private static final long serialVersionUID = 1L;
50+
51+
@Override
52+
public String getReason() {
53+
return "blocked";
54+
}
55+
});
56+
return violations;
57+
}
58+
}

0 commit comments

Comments
 (0)