summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src
diff options
context:
space:
mode:
Diffstat (limited to 'sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src')
-rw-r--r--sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/main/java/org/apache/tuscany/sca/policy/security/jsr250/JSR250PolicyProcessor.java221
-rw-r--r--sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor20
-rw-r--r--sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/test/java/org/apache/tuscany/sca/policy/security/jsr250/PolicyProcessorTestCase.java224
3 files changed, 465 insertions, 0 deletions
diff --git a/sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/main/java/org/apache/tuscany/sca/policy/security/jsr250/JSR250PolicyProcessor.java b/sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/main/java/org/apache/tuscany/sca/policy/security/jsr250/JSR250PolicyProcessor.java
new file mode 100644
index 0000000000..5f3f1a33ff
--- /dev/null
+++ b/sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/main/java/org/apache/tuscany/sca/policy/security/jsr250/JSR250PolicyProcessor.java
@@ -0,0 +1,221 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.policy.security.jsr250;
+
+import java.lang.reflect.Method;
+
+import javax.annotation.security.DenyAll;
+import javax.annotation.security.PermitAll;
+import javax.annotation.security.RolesAllowed;
+import javax.annotation.security.RunAs;
+import javax.xml.namespace.QName;
+import javax.xml.xpath.XPath;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+
+import org.apache.tuscany.sca.assembly.AssemblyFactory;
+import org.apache.tuscany.sca.assembly.xml.Constants;
+import org.apache.tuscany.sca.common.xml.stax.reader.NamespaceContextImpl;
+import org.apache.tuscany.sca.common.xml.xpath.XPathHelper;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.core.FactoryExtensionPoint;
+import org.apache.tuscany.sca.implementation.java.IntrospectionException;
+import org.apache.tuscany.sca.implementation.java.JavaImplementation;
+import org.apache.tuscany.sca.implementation.java.introspect.BaseJavaClassVisitor;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.interfacedef.java.JavaOperation;
+import org.apache.tuscany.sca.policy.PolicyExpression;
+import org.apache.tuscany.sca.policy.PolicyFactory;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.authorization.AuthorizationPolicy;
+import org.apache.tuscany.sca.policy.identity.SecurityIdentityPolicy;
+
+/**
+ * Processes an {@link javax.annotation.security.*} annotation
+ * Below is a list of annotations
+ *
+ * Type Method
+ * RunAs x
+ * RolesAllowed x x
+ * PermitAll x x
+ * DenyAll x
+ *
+ */
+public class JSR250PolicyProcessor extends BaseJavaClassVisitor {
+ private static final QName RUN_AS = new QName(Constants.SCA11_TUSCANY_NS,"runAs");
+ private static final QName ALLOW = new QName(Constants.SCA11_TUSCANY_NS,"allow");
+ private static final QName PERMIT_ALL = new QName(Constants.SCA11_TUSCANY_NS,"permitAll");
+ private static final QName DENY_ALL = new QName(Constants.SCA11_TUSCANY_NS,"denyAll");
+
+ private PolicyFactory policyFactory;
+ private XPathHelper xpathHelper;
+ private String appliesToString = "//sca:implementation.java";
+ private XPathExpression appliesToExpression = null;
+
+ public JSR250PolicyProcessor(ExtensionPointRegistry registry) throws IntrospectionException {
+ super(registry.getExtensionPoint(FactoryExtensionPoint.class).getFactory(AssemblyFactory.class));
+ this.policyFactory = registry.getExtensionPoint(FactoryExtensionPoint.class).getFactory(PolicyFactory.class);
+
+ this.xpathHelper = XPathHelper.getInstance(registry);
+ NamespaceContextImpl nsContext = new NamespaceContextImpl(null);
+ nsContext.register("sca", "http://docs.oasis-open.org/ns/opencsa/sca/200912");
+ XPath path = xpathHelper.newXPath();
+ try {
+ appliesToExpression = xpathHelper.compile(path, nsContext, appliesToString);
+ } catch (XPathExpressionException e) {
+ throw new IntrospectionException(e);
+ }
+ }
+
+/*
+ public JSR250PolicyProcessor(AssemblyFactory assemblyFactory, PolicyFactory policyFactory) {
+ super(assemblyFactory);
+ this.policyFactory = policyFactory;
+ }
+*/
+
+ @Override
+ public <T> void visitClass(Class<T> clazz, JavaImplementation type) throws IntrospectionException {
+
+ RunAs runAs = clazz.getAnnotation(javax.annotation.security.RunAs.class);
+ if (runAs != null) {
+
+ String roleName = runAs.value();
+ if(roleName == null) {
+ //FIXME handle monitor or error
+ }
+
+ SecurityIdentityPolicy policy = new SecurityIdentityPolicy();
+ policy.setRunAsRole(roleName);
+ PolicySet policySet = createPolicySet(RUN_AS, SecurityIdentityPolicy.NAME, policy);
+ type.getPolicySets().add(policySet);
+ }
+
+ RolesAllowed rolesAllowed = clazz.getAnnotation(javax.annotation.security.RolesAllowed.class);
+ if(rolesAllowed != null) {
+ if(rolesAllowed.value().length == 0) {
+ //FIXME handle monitor or error
+ }
+
+ AuthorizationPolicy policy = new AuthorizationPolicy();
+ policy.setAccessControl(AuthorizationPolicy.AcessControl.allow);
+
+ for(String role : rolesAllowed.value()) {
+ policy.getRoleNames().add(role);
+ }
+
+ PolicySet policySet = createPolicySet(ALLOW, AuthorizationPolicy.NAME, policy);
+ type.getPolicySets().add(policySet);
+ }
+
+ PermitAll permitAll = clazz.getAnnotation(javax.annotation.security.PermitAll.class);
+ if(permitAll != null) {
+ AuthorizationPolicy policy = new AuthorizationPolicy();
+ policy.setAccessControl(AuthorizationPolicy.AcessControl.permitAll);
+ PolicySet policySet = createPolicySet(PERMIT_ALL, AuthorizationPolicy.NAME, policy);
+ type.getPolicySets().add(policySet);
+ }
+
+ }
+
+ @Override
+ public void visitMethod(Method method, JavaImplementation type) throws IntrospectionException {
+ RolesAllowed rolesAllowed = method.getAnnotation(javax.annotation.security.RolesAllowed.class);
+ if(rolesAllowed != null) {
+ if(rolesAllowed.value().length == 0) {
+ //FIXME handle monitor or error
+ }
+
+ AuthorizationPolicy policy = new AuthorizationPolicy();
+ policy.setAccessControl(AuthorizationPolicy.AcessControl.allow);
+
+ for(String role : rolesAllowed.value()) {
+ policy.getRoleNames().add(role);
+ }
+
+ // find the operation in the interface model
+ Operation operation = getOperationModel(method, type);
+
+ if (operation != null){
+ PolicySet policySet = createPolicySet(ALLOW, AuthorizationPolicy.NAME, policy);
+ operation.getPolicySets().add(policySet);
+ }
+ }
+
+ PermitAll permitAll = method.getAnnotation(javax.annotation.security.PermitAll.class);
+ if(permitAll != null) {
+ AuthorizationPolicy policy = new AuthorizationPolicy();
+ policy.setAccessControl(AuthorizationPolicy.AcessControl.permitAll);
+
+ // find the operation in the interface model
+ Operation operation = getOperationModel(method, type);
+
+ if (operation != null){
+ PolicySet policySet = createPolicySet(PERMIT_ALL, AuthorizationPolicy.NAME, policy);
+ operation.getPolicySets().add(policySet);
+ }
+ }
+
+ DenyAll denyAll = method.getAnnotation(javax.annotation.security.DenyAll.class);
+ if(denyAll != null) {
+ AuthorizationPolicy policy = new AuthorizationPolicy();
+ policy.setAccessControl(AuthorizationPolicy.AcessControl.denyAll);
+
+ // find the operation in the interface model
+ Operation operation = getOperationModel(method, type);
+
+ if (operation != null){
+ PolicySet policySet = createPolicySet(DENY_ALL, AuthorizationPolicy.NAME, policy);
+ operation.getPolicySets().add(policySet);
+ }
+ }
+ }
+
+ private Operation getOperationModel(Method method, JavaImplementation type){
+
+ for(Operation op : type.getOperations()){
+ if (((JavaOperation)op).getJavaMethod().equals(method)){
+ return op;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Here we generate policy sets on the fly so they have to be configured as though they
+ * had been read and resolved from a defintions.xml file. I.e. they have to have appropriate
+ * appliesTo configuration and be marked as resolved.
+ */
+ private PolicySet createPolicySet(QName policySetName, QName policyExpressionName, Object policy){
+
+ PolicyExpression policyExpression = policyFactory.createPolicyExpression();
+ policyExpression.setName(policyExpressionName);
+ policyExpression.setPolicy(policy);
+
+ PolicySet policySet = policyFactory.createPolicySet();
+ policySet.setName(policySetName);
+ policySet.setAppliesTo(appliesToString);
+ policySet.setAppliesToXPathExpression(appliesToExpression);
+ policySet.getPolicies().add(policyExpression);
+ policySet.setUnresolved(false);
+
+ return policySet;
+ }
+}
diff --git a/sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor b/sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor
new file mode 100644
index 0000000000..37bff7cc10
--- /dev/null
+++ b/sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/main/resources/META-INF/services/org.apache.tuscany.sca.implementation.java.introspect.JavaClassVisitor
@@ -0,0 +1,20 @@
+# Licensed to the Apache Software Foundation
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0
+# "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.
+#
+# NOTE: The ranking attribute is important for the JavaClassVistors
+# Some visitors need to be called after the others
+org.apache.tuscany.sca.policy.security.jsr250.JSR250PolicyProcessor;ranking=600 \ No newline at end of file
diff --git a/sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/test/java/org/apache/tuscany/sca/policy/security/jsr250/PolicyProcessorTestCase.java b/sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/test/java/org/apache/tuscany/sca/policy/security/jsr250/PolicyProcessorTestCase.java
new file mode 100644
index 0000000000..de8e0ccacf
--- /dev/null
+++ b/sca-java-2.x/branches/2.0/modules/policy-security-jsr250/src/test/java/org/apache/tuscany/sca/policy/security/jsr250/PolicyProcessorTestCase.java
@@ -0,0 +1,224 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.apache.tuscany.sca.policy.security.jsr250;
+
+import java.lang.reflect.Method;
+
+import javax.annotation.security.DenyAll;
+import javax.annotation.security.PermitAll;
+import javax.annotation.security.RolesAllowed;
+import javax.annotation.security.RunAs;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.tuscany.sca.assembly.DefaultAssemblyFactory;
+import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry;
+import org.apache.tuscany.sca.core.ExtensionPointRegistry;
+import org.apache.tuscany.sca.implementation.java.DefaultJavaImplementationFactory;
+import org.apache.tuscany.sca.implementation.java.JavaImplementation;
+import org.apache.tuscany.sca.implementation.java.JavaImplementationFactory;
+import org.apache.tuscany.sca.implementation.java.introspect.impl.PolicyProcessor;
+import org.apache.tuscany.sca.implementation.java.introspect.impl.ServiceProcessor;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.interfacedef.java.DefaultJavaInterfaceFactory;
+import org.apache.tuscany.sca.interfacedef.java.JavaInterface;
+import org.apache.tuscany.sca.interfacedef.java.JavaOperation;
+import org.apache.tuscany.sca.interfacedef.java.impl.PolicyJavaInterfaceVisitor;
+import org.apache.tuscany.sca.policy.DefaultPolicyFactory;
+import org.oasisopen.sca.annotation.Service;
+
+/**
+ * @version $Rev: 662474 $ $Date: 2008-06-02 17:18:28 +0100 (Mon, 02 Jun 2008) $
+ */
+public class PolicyProcessorTestCase extends TestCase {
+ private ExtensionPointRegistry registry;
+ private ServiceProcessor serviceProcessor;
+ private PolicyProcessor policyProcessor;
+ private JSR250PolicyProcessor jsr250Processor;
+ private PolicyJavaInterfaceVisitor visitor;
+ private JavaImplementation type;
+
+ private interface Interface1 {
+ int method1();
+
+ int method2();
+
+ int method3();
+
+ int method4();
+ }
+
+ @RunAs("Role1")
+ @Service(Interface1.class)
+ private class Service1 implements Interface1 {
+ public int method1() {
+ return 0;
+ }
+
+ public int method2() {
+ return 0;
+ }
+
+ public int method3() {
+ return 0;
+ }
+
+ public int method4() {
+ return 0;
+ }
+ }
+
+ @RolesAllowed({"Role2", "Role3"})
+ @Service(Interface1.class)
+ private class Service2 implements Interface1 {
+ public int method1() {
+ return 0;
+ }
+
+ public int method2() {
+ return 0;
+ }
+
+ public int method3() {
+ return 0;
+ }
+
+ public int method4() {
+ return 0;
+ }
+ }
+
+ @PermitAll()
+ @Service(Interface1.class)
+ private class Service3 implements Interface1 {
+ public int method1() {
+ return 0;
+ }
+
+ public int method2() {
+ return 0;
+ }
+
+ public int method3() {
+ return 0;
+ }
+
+ public int method4() {
+ return 0;
+ }
+ }
+
+
+ @Service(Interface1.class)
+ private class Service4 implements Interface1 {
+ public int method1() {
+ return 0;
+ }
+
+ @RolesAllowed({"Role4", "Role5"})
+ public int method2() {
+ return 0;
+ }
+
+ @PermitAll
+ public int method3() {
+ return 0;
+ }
+
+ @DenyAll
+ public int method4() {
+ return 0;
+ }
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ registry = new DefaultExtensionPointRegistry();
+ registry.start();
+ serviceProcessor = new ServiceProcessor(new DefaultAssemblyFactory(), new DefaultJavaInterfaceFactory(registry));
+ policyProcessor = new PolicyProcessor(registry);
+ jsr250Processor = new JSR250PolicyProcessor(registry);
+ visitor = new PolicyJavaInterfaceVisitor(registry);
+ JavaImplementationFactory javaImplementationFactory = new DefaultJavaImplementationFactory();
+ type = javaImplementationFactory.createJavaImplementation();
+ }
+
+ public void testSingleInterfaceWithRunAsAtClassLevel() throws Exception {
+ runProcessors(Service1.class, null, type);
+ Assert.assertEquals(1, type.getPolicySets().size());
+ }
+
+ public void testSingleInterfaceWithRolesAllowedsAtClassLevel() throws Exception {
+ runProcessors(Service2.class, null, type);
+ Assert.assertEquals(1, type.getPolicySets().size());
+ }
+
+ public void testSingleInterfaceWithPermitAllAtClassLevel() throws Exception {
+ runProcessors(Service3.class, null, type);
+ Assert.assertEquals(1, type.getPolicySets().size());
+ }
+
+ public void testSingleInterfaceWithRolesAllowedAtMethodLevel() throws Exception {
+ Method aMethod = Service4.class.getDeclaredMethod("method2");
+ runProcessors(Service4.class, aMethod, type);
+ Operation op = getOperationModel(aMethod, type);
+ Assert.assertEquals(1, op.getPolicySets().size());
+ }
+
+ public void testSingleInterfaceWithPermitAllAtMethodLevel() throws Exception {
+ Method aMethod = Service4.class.getDeclaredMethod("method3");
+ runProcessors(Service4.class, aMethod, type);
+ Operation op = getOperationModel(aMethod, type);
+ Assert.assertEquals(1, op.getPolicySets().size());
+ }
+
+ public void testSingleInterfaceWithDenyAllAtMethodLevel() throws Exception {
+ Method aMethod = Service4.class.getDeclaredMethod("method4");
+ runProcessors(Service4.class, aMethod, type);
+ Operation op = getOperationModel(aMethod, type);
+ Assert.assertEquals(1, op.getPolicySets().size());
+ }
+
+ public void testSingleInterfaceWithNothingAtMethodLevel() throws Exception {
+ Method aMethod = Service4.class.getDeclaredMethod("method1");
+ runProcessors(Service4.class, aMethod, type);
+ Operation op = getOperationModel(aMethod, type);
+ Assert.assertEquals(0, op.getPolicySets().size());
+ }
+
+ private void runProcessors(Class clazz, Method method, JavaImplementation type)throws Exception {
+ serviceProcessor.visitClass(clazz, type);
+ policyProcessor.visitClass(clazz, type);
+ jsr250Processor.visitClass(clazz, type);
+ if (method != null){
+ jsr250Processor.visitMethod(method, type);
+ }
+ }
+
+ private Operation getOperationModel(Method method, JavaImplementation type){
+ for(Operation op : type.getOperations()){
+ if (((JavaOperation)op).getJavaMethod().equals(method)){
+ return op;
+ }
+ }
+ return null;
+ }
+}