Author: sohil.shah(a)jboss.com
Date: 2009-08-05 12:45:20 -0400 (Wed, 05 Aug 2009)
New Revision: 13680
Added:
modules/authorization/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/provisioning/RelationalDBPolicyStore.java
modules/authorization/trunk/policy-server/src/main/resources/policy.hbm.xml
modules/authorization/trunk/policy-server/src/test/java/org/jboss/security/authz/policy/server/provisioning/
modules/authorization/trunk/policy-server/src/test/java/org/jboss/security/authz/policy/server/provisioning/TestRelationalDBPolicyStore.java
modules/authorization/trunk/policy-server/src/test/resources/hibernate.cfg.xml
Modified:
modules/authorization/trunk/.classpath
modules/authorization/trunk/common-api/src/main/java/org/jboss/security/authz/model/Policy.java
modules/authorization/trunk/policy-server/pom.xml
modules/authorization/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/XACMLPolicy.java
modules/authorization/trunk/policy-server/src/main/resources/META-INF/authz-config.xml
modules/authorization/trunk/pom.xml
Log:
Started Hibernate based RelationalDBPolicyStore implementation
Modified: modules/authorization/trunk/.classpath
===================================================================
--- modules/authorization/trunk/.classpath 2009-08-05 16:07:15 UTC (rev 13679)
+++ modules/authorization/trunk/.classpath 2009-08-05 16:45:20 UTC (rev 13680)
@@ -45,5 +45,6 @@
<classpathentry kind="var"
path="M2_REPO/javax/servlet/servlet-api/2.4/servlet-api-2.4.jar"/>
<classpathentry kind="var"
path="M2_REPO/commons-httpclient/commons-httpclient/3.1/commons-httpclient-3.1.jar"/>
<classpathentry kind="var"
path="M2_REPO/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar"/>
+ <classpathentry kind="var"
path="M2_REPO/org/hibernate/hibernate/3.1.2/hibernate-3.1.2.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Modified:
modules/authorization/trunk/common-api/src/main/java/org/jboss/security/authz/model/Policy.java
===================================================================
---
modules/authorization/trunk/common-api/src/main/java/org/jboss/security/authz/model/Policy.java 2009-08-05
16:07:15 UTC (rev 13679)
+++
modules/authorization/trunk/common-api/src/main/java/org/jboss/security/authz/model/Policy.java 2009-08-05
16:45:20 UTC (rev 13680)
@@ -26,67 +26,69 @@
* Represents an Authorization Policy
*
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
- *
+ *
*/
public abstract class Policy extends BaseObject
{
- protected String policyUri;
- protected PolicyMetaData metaData;
-
- /**
- *
- *
- */
- public Policy(String policyUri, PolicyMetaData metaData)
- {
- if(policyUri == null)
- {
- throw new IllegalArgumentException("PolicyUri cannot be Null");
- }
-
- if(metaData == null)
- {
- throw new IllegalArgumentException("PolicyMetaData cannot be Null");
- }
-
- this.policyUri = policyUri;
- this.metaData = metaData;
- }
-
- /**
- * A unique identifier for the Policy
- *
- * @return unique identifier for the Policy
- */
- public String getPolicyUri()
- {
- return this.policyUri;
- }
-
- /**
- *
- * @param policyUri
- */
- public void setPolicyUri(String policyUri)
- {
- this.policyUri = policyUri;
- }
+ protected String policyUri;
+ protected PolicyMetaData metaData;
+
+ public Policy()
+ {
+
+ }
+
+ public Policy(String policyUri, PolicyMetaData metaData)
+ {
+ if (policyUri == null)
+ {
+ throw new IllegalArgumentException("PolicyUri cannot be Null");
+ }
-
- public PolicyMetaData getMetaData()
- {
- return metaData;
- }
+ if (metaData == null)
+ {
+ throw new IllegalArgumentException("PolicyMetaData cannot be Null");
+ }
- public void setMetaData(PolicyMetaData metaData)
- {
- this.metaData = metaData;
- }
-
//------------------------------------------------------------------------------------------------------------------------------------------------
- /**
- * Generates the standard XACML markup that represents the Policy instance in
question
- *
- * @return XACML markup to represent this Policy
- */
- public abstract String generateSystemPolicy() throws PolicyException;
+ this.policyUri = policyUri;
+ this.metaData = metaData;
+ }
+
+ /**
+ * A unique identifier for the Policy
+ *
+ * @return unique identifier for the Policy
+ */
+ public String getPolicyUri()
+ {
+ return this.policyUri;
+ }
+
+ /**
+ *
+ * @param policyUri
+ */
+ public void setPolicyUri(String policyUri)
+ {
+ this.policyUri = policyUri;
+ }
+
+ public PolicyMetaData getMetaData()
+ {
+ return metaData;
+ }
+
+ public void setMetaData(PolicyMetaData metaData)
+ {
+ this.metaData = metaData;
+ }
+
+ //
------------------------------------------------------------------------------------------------------------------------------------------------
+ /**
+ * Generates the standard XACML markup that represents the Policy instance in
+ * question
+ *
+ * @return XACML markup to represent this Policy
+ */
+ public abstract String generateSystemPolicy() throws PolicyException;
}
Modified: modules/authorization/trunk/policy-server/pom.xml
===================================================================
--- modules/authorization/trunk/policy-server/pom.xml 2009-08-05 16:07:15 UTC (rev 13679)
+++ modules/authorization/trunk/policy-server/pom.xml 2009-08-05 16:45:20 UTC (rev 13680)
@@ -39,6 +39,19 @@
<dependency>
<groupId>org.drools</groupId>
<artifactId>drools-compiler</artifactId>
- </dependency>
+ </dependency>
+
+ <!-- Hibernate -->
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate</artifactId>
+ </dependency>
+
+ <!-- test execution dependencies -->
+ <dependency>
+ <groupId>org.jboss.microcontainer</groupId>
+ <artifactId>jboss-kernel</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
Modified:
modules/authorization/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/XACMLPolicy.java
===================================================================
---
modules/authorization/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/XACMLPolicy.java 2009-08-05
16:07:15 UTC (rev 13679)
+++
modules/authorization/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/plugin/XACMLPolicy.java 2009-08-05
16:45:20 UTC (rev 13680)
@@ -61,224 +61,257 @@
import org.jboss.security.xacml.core.model.policy.SubjectAttributeDesignatorType;
import org.jboss.security.xacml.factories.PolicyAttributeFactory;
-
/**
- * Used for mapping the Policy domain model to the system level Policy representation.
This particular instance maps the Policy domain model to
+ * Used for mapping the Policy domain model to the system level Policy
+ * representation. This particular instance maps the Policy domain model to
* XACML representation
*
*
* @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
- *
+ *
*/
public class XACMLPolicy extends Policy
-{
- public XACMLPolicy(String policyUri, PolicyMetaData metaData) throws PolicyException
- {
- super(policyUri, metaData);
- }
-
-
- @Override
- public String generateSystemPolicy() throws PolicyException
- {
- ByteArrayOutputStream bos = null;
- try
- {
- String xacmlXml = null;
-
- //SetUp the Policy Header
- ObjectFactory objectFactory = new ObjectFactory();
- PolicyType policyType = new PolicyType();
- policyType.setPolicyId(this.policyUri);
- policyType.setVersion("2.0");
- policyType.setRuleCombiningAlgId(new
NoPermitMeansDeniedAlg().getIdentifier().toString());
-
- TargetType targetType = new TargetType();
- policyType.setTarget(targetType);
-
- //Process Resource Matches as Targets for the Policy
- List<AttributeExpression> resourceMatches =
this.metaData.getTarget().getResourceMatches();
- if(resourceMatches != null && !resourceMatches.isEmpty())
- {
- ResourcesType resourcesType = new ResourcesType();
- targetType.setResources(resourcesType);
- ResourceType resourceType = new ResourceType();
-
- for(AttributeExpression resourceMatch: resourceMatches)
- {
- ResourceMatchType rmt = new ResourceMatchType();
-
- rmt.setMatchId(resourceMatch.getFunctionId());
-
-
rmt.setResourceAttributeDesignator(AttributeDesignatorUtil.getAttributeDesignator(resourceMatch));
-
- rmt.setAttributeValue(PolicyAttributeFactory
- .createStringAttributeType(resourceMatch.getAttribute().getValue()));
-
- resourceType.getResourceMatch().add(rmt);
- }
-
- resourcesType.getResource().add(resourceType);
- }
-
- //Process Action Matches as Targets for the Policy
- List<AttributeExpression> targetActionMatches =
this.metaData.getTarget().getActionMatches();
- if(targetActionMatches != null && !targetActionMatches.isEmpty())
- {
- ActionsType actionsType = this.generateRuleActions(targetActionMatches);
- targetType.setActions(actionsType);
- }
-
-
- //Process the Policy Rules
- Set<Rule> rules = this.metaData.getRules();
- if(rules != null && !rules.isEmpty())
- {
- for(Rule rule: rules)
- {
- RuleType ruleType = new RuleType();
- ruleType.setRuleId(rule.getRuleId());
- if(rule.getEffect() == Effect.PERMIT)
- {
- ruleType.setEffect(EffectType.PERMIT);
- }
- else
- {
- ruleType.setEffect(EffectType.DENY);
- }
-
- //Process the Rule Target
- if(rule.getTarget() != null)
- {
- List<AttributeExpression> actionMatches =
rule.getTarget().getActionMatches();
- List<AttributeExpression> subjectMatches =
rule.getTarget().getSubjectMatches();
- TargetType ruleTarget = new TargetType();
-
- if(actionMatches != null && !actionMatches.isEmpty())
- {
- ruleTarget.setActions(this.generateRuleActions(actionMatches));
- }
-
- if(subjectMatches != null && !subjectMatches.isEmpty())
- {
- ruleTarget.setSubjects(this.generateRuleSubjects(subjectMatches));
- }
-
- ruleType.setTarget(ruleTarget);
- }
-
- //Process the Rule Expression/Condition
- if(rule.getExpression() != null)
- {
- ConditionType condition = this.generateCondition(objectFactory,
rule.getExpression());
- ruleType.setCondition(condition);
- }
-
-
policyType.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().add(ruleType);
- }
- }
-
- bos = new ByteArrayOutputStream();
- PolicyUtil.marshall(bos, policyType);
- xacmlXml = new String(bos.toByteArray());
-
- return xacmlXml;
- }
- catch(Exception e)
- {
- throw new PolicyException(e);
- }
- finally
- {
- if(bos != null)
- {
- try{bos.close();}catch(IOException ioe){}
- }
- }
- }
-
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- private ActionsType generateRuleActions(List<AttributeExpression>
actionMatches)
- {
- ActionsType actions = new ActionsType();
-
- for(AttributeExpression action: actionMatches)
- {
- ActionType actionType = new ActionType();
- ActionMatchType amct = new ActionMatchType();
- amct.setMatchId(action.getFunctionId());
-
amct.setAttributeValue(PolicyAttributeFactory.createStringAttributeType(action.getAttribute().getValue()));
-
amct.setActionAttributeDesignator(AttributeDesignatorUtil.getAttributeDesignator(action));
- actionType.getActionMatch().add(amct);
- actions.getAction().add(actionType);
- }
-
- return actions;
- }
-
- private SubjectsType generateRuleSubjects(List<AttributeExpression>
subjectMatches)
- {
- SubjectsType subjects = new SubjectsType();
-
- for(AttributeExpression subject: subjectMatches)
- {
- SubjectType subjectType = new SubjectType();
- SubjectMatchType match = new SubjectMatchType();
- match.setMatchId(subject.getFunctionId());
-
match.setAttributeValue(PolicyAttributeFactory.createStringAttributeType(subject.getAttribute().getValue()));
-
match.setSubjectAttributeDesignator((SubjectAttributeDesignatorType)AttributeDesignatorUtil.getAttributeDesignator(subject));
- subjectType.getSubjectMatch().add(match);
- subjects.getSubject().add(subjectType);
- }
-
- return subjects;
- }
-
- /**
- *
- * @param expression
- * @return
- */
- private ConditionType generateCondition(ObjectFactory objectFactory, Expression
expression)
- {
- ConditionType condition = new ConditionType();
-
- if(expression instanceof AttributeExpression)
- {
- AttributeExpression attributeExpression = (AttributeExpression)expression;
-
- //Function to be applied
- ApplyType apply = new ApplyType();
- apply.setFunctionId(attributeExpression.getFunctionId());
-
- //Value to check against
- AttributeValueType attrValue =
PolicyAttributeFactory.createStringAttributeType(attributeExpression.getAttribute().getValue());
- JAXBElement<AttributeValueType> jaxbAttrValue =
objectFactory.createAttributeValue(attrValue);
- apply.getExpression().add(jaxbAttrValue);
-
- //Place within the Context where this Value should exist during an Authorization
Request
-
apply.getExpression().add(AttributeDesignatorUtil.getAttributeDesignatorXml(attributeExpression));
-
-
- condition.setExpression(objectFactory.createApply(apply));
- }
- else if(expression instanceof DroolsRuleExpression)
- {
- DroolsRuleExpression ruleExpression = (DroolsRuleExpression)expression;
-
- //Function to be applied
- ApplyType apply = new ApplyType();
- apply.setFunctionId(ruleExpression.getFunctionId());
-
-
- VariableReferenceType ruleReference = new VariableReferenceType();
- ruleReference.setVariableId(ruleExpression.getRuleReference());
- JAXBElement<VariableReferenceType> jaxbRuleReference =
objectFactory.createVariableReference(ruleReference);
- apply.getExpression().add(jaxbRuleReference);
-
-
- condition.setExpression(objectFactory.createApply(apply));
- }
-
- return condition;
- }
+{
+ public XACMLPolicy()
+ {
+
+ }
+
+ public XACMLPolicy(String policyUri, PolicyMetaData metaData)
+ throws PolicyException
+ {
+ super(policyUri, metaData);
+ }
+
+ @Override
+ public String generateSystemPolicy() throws PolicyException
+ {
+ ByteArrayOutputStream bos = null;
+ try
+ {
+ String xacmlXml = null;
+
+ // SetUp the Policy Header
+ ObjectFactory objectFactory = new ObjectFactory();
+ PolicyType policyType = new PolicyType();
+ policyType.setPolicyId(this.policyUri);
+ policyType.setVersion("2.0");
+ policyType.setRuleCombiningAlgId(new NoPermitMeansDeniedAlg()
+ .getIdentifier().toString());
+
+ TargetType targetType = new TargetType();
+ policyType.setTarget(targetType);
+
+ // Process Resource Matches as Targets for the Policy
+ List<AttributeExpression> resourceMatches = this.metaData.getTarget()
+ .getResourceMatches();
+ if (resourceMatches != null && !resourceMatches.isEmpty())
+ {
+ ResourcesType resourcesType = new ResourcesType();
+ targetType.setResources(resourcesType);
+ ResourceType resourceType = new ResourceType();
+
+ for (AttributeExpression resourceMatch : resourceMatches)
+ {
+ ResourceMatchType rmt = new ResourceMatchType();
+
+ rmt.setMatchId(resourceMatch.getFunctionId());
+
+ rmt.setResourceAttributeDesignator(AttributeDesignatorUtil
+ .getAttributeDesignator(resourceMatch));
+
+ rmt.setAttributeValue(PolicyAttributeFactory
+ .createStringAttributeType(resourceMatch.getAttribute()
+ .getValue()));
+
+ resourceType.getResourceMatch().add(rmt);
+ }
+
+ resourcesType.getResource().add(resourceType);
+ }
+
+ // Process Action Matches as Targets for the Policy
+ List<AttributeExpression> targetActionMatches = this.metaData.getTarget()
+ .getActionMatches();
+ if (targetActionMatches != null && !targetActionMatches.isEmpty())
+ {
+ ActionsType actionsType = this.generateRuleActions(targetActionMatches);
+ targetType.setActions(actionsType);
+ }
+
+ // Process the Policy Rules
+ Set<Rule> rules = this.metaData.getRules();
+ if (rules != null && !rules.isEmpty())
+ {
+ for (Rule rule : rules)
+ {
+ RuleType ruleType = new RuleType();
+ ruleType.setRuleId(rule.getRuleId());
+ if (rule.getEffect() == Effect.PERMIT)
+ {
+ ruleType.setEffect(EffectType.PERMIT);
+ }
+ else
+ {
+ ruleType.setEffect(EffectType.DENY);
+ }
+
+ // Process the Rule Target
+ if (rule.getTarget() != null)
+ {
+ List<AttributeExpression> actionMatches = rule.getTarget()
+ .getActionMatches();
+ List<AttributeExpression> subjectMatches = rule.getTarget()
+ .getSubjectMatches();
+ TargetType ruleTarget = new TargetType();
+
+ if (actionMatches != null && !actionMatches.isEmpty())
+ {
+ ruleTarget.setActions(this.generateRuleActions(actionMatches));
+ }
+
+ if (subjectMatches != null && !subjectMatches.isEmpty())
+ {
+ ruleTarget.setSubjects(this.generateRuleSubjects(subjectMatches));
+ }
+
+ ruleType.setTarget(ruleTarget);
+ }
+
+ // Process the Rule Expression/Condition
+ if (rule.getExpression() != null)
+ {
+ ConditionType condition = this.generateCondition(objectFactory,
+ rule.getExpression());
+ ruleType.setCondition(condition);
+ }
+
+ policyType
+ .getCombinerParametersOrRuleCombinerParametersOrVariableDefinition()
+ .add(ruleType);
+ }
+ }
+
+ bos = new ByteArrayOutputStream();
+ PolicyUtil.marshall(bos, policyType);
+ xacmlXml = new String(bos.toByteArray());
+
+ return xacmlXml;
+ }
+ catch (Exception e)
+ {
+ throw new PolicyException(e);
+ }
+ finally
+ {
+ if (bos != null)
+ {
+ try
+ {
+ bos.close();
+ }
+ catch (IOException ioe)
+ {
+ }
+ }
+ }
+ }
+
+ //
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
+ private ActionsType generateRuleActions(
+ List<AttributeExpression> actionMatches)
+ {
+ ActionsType actions = new ActionsType();
+
+ for (AttributeExpression action : actionMatches)
+ {
+ ActionType actionType = new ActionType();
+ ActionMatchType amct = new ActionMatchType();
+ amct.setMatchId(action.getFunctionId());
+ amct.setAttributeValue(PolicyAttributeFactory
+ .createStringAttributeType(action.getAttribute().getValue()));
+ amct.setActionAttributeDesignator(AttributeDesignatorUtil
+ .getAttributeDesignator(action));
+ actionType.getActionMatch().add(amct);
+ actions.getAction().add(actionType);
+ }
+
+ return actions;
+ }
+
+ private SubjectsType generateRuleSubjects(
+ List<AttributeExpression> subjectMatches)
+ {
+ SubjectsType subjects = new SubjectsType();
+
+ for (AttributeExpression subject : subjectMatches)
+ {
+ SubjectType subjectType = new SubjectType();
+ SubjectMatchType match = new SubjectMatchType();
+ match.setMatchId(subject.getFunctionId());
+ match.setAttributeValue(PolicyAttributeFactory
+ .createStringAttributeType(subject.getAttribute().getValue()));
+ match
+ .setSubjectAttributeDesignator((SubjectAttributeDesignatorType)
AttributeDesignatorUtil
+ .getAttributeDesignator(subject));
+ subjectType.getSubjectMatch().add(match);
+ subjects.getSubject().add(subjectType);
+ }
+
+ return subjects;
+ }
+
+ /**
+ *
+ * @param expression
+ * @return
+ */
+ private ConditionType generateCondition(ObjectFactory objectFactory,
+ Expression expression)
+ {
+ ConditionType condition = new ConditionType();
+
+ if (expression instanceof AttributeExpression)
+ {
+ AttributeExpression attributeExpression = (AttributeExpression) expression;
+
+ // Function to be applied
+ ApplyType apply = new ApplyType();
+ apply.setFunctionId(attributeExpression.getFunctionId());
+
+ // Value to check against
+ AttributeValueType attrValue = PolicyAttributeFactory
+ .createStringAttributeType(attributeExpression.getAttribute()
+ .getValue());
+ JAXBElement<AttributeValueType> jaxbAttrValue = objectFactory
+ .createAttributeValue(attrValue);
+ apply.getExpression().add(jaxbAttrValue);
+
+ // Place within the Context where this Value should exist during an
+ // Authorization Request
+ apply.getExpression().add(
+ AttributeDesignatorUtil
+ .getAttributeDesignatorXml(attributeExpression));
+
+ condition.setExpression(objectFactory.createApply(apply));
+ }
+ else if (expression instanceof DroolsRuleExpression)
+ {
+ DroolsRuleExpression ruleExpression = (DroolsRuleExpression) expression;
+
+ // Function to be applied
+ ApplyType apply = new ApplyType();
+ apply.setFunctionId(ruleExpression.getFunctionId());
+
+ VariableReferenceType ruleReference = new VariableReferenceType();
+ ruleReference.setVariableId(ruleExpression.getRuleReference());
+ JAXBElement<VariableReferenceType> jaxbRuleReference = objectFactory
+ .createVariableReference(ruleReference);
+ apply.getExpression().add(jaxbRuleReference);
+
+ condition.setExpression(objectFactory.createApply(apply));
+ }
+
+ return condition;
+ }
}
Added:
modules/authorization/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/provisioning/RelationalDBPolicyStore.java
===================================================================
---
modules/authorization/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/provisioning/RelationalDBPolicyStore.java
(rev 0)
+++
modules/authorization/trunk/policy-server/src/main/java/org/jboss/security/authz/policy/server/provisioning/RelationalDBPolicyStore.java 2009-08-05
16:45:20 UTC (rev 13680)
@@ -0,0 +1,125 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.security.authz.policy.server.provisioning;
+
+import java.util.Map;
+import java.util.HashMap;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.SessionFactory;
+import org.hibernate.cfg.Configuration;
+
+
+import org.jboss.security.authz.model.Policy;
+import org.jboss.security.authz.model.PolicyException;
+import org.jboss.security.authz.policy.server.spi.PolicyStore;
+
+/**
+ * This Policy Store stores the system Policies into a Relational Database
+ *
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ *
+ */
+public class RelationalDBPolicyStore implements PolicyStore
+{
+ private static Logger log = Logger.getLogger(RelationalDBPolicyStore.class);
+
+ private Map<String, Policy> policies;
+
+ SessionFactory sessionFactory;
+
+ public RelationalDBPolicyStore()
+ {
+ this.policies = new HashMap<String, Policy>();
+ }
+
+ public void start()
+ {
+ try
+ {
+ Configuration configuration = new Configuration();
+ this.sessionFactory = configuration.configure().buildSessionFactory();
+ log
+ .info("-----------------------------------------------------------------------");
+ log
+ .info("In-Memory Policy Store successfully
started............................");
+ log
+ .info("-----------------------------------------------------------------------");
+ }
+ catch(Throwable t)
+ {
+ log.error(this, t);
+ throw new RuntimeException(t);
+ }
+ }
+
+ public void stop()
+ {
+ this.sessionFactory.close();
+ }
+ //
-----------------------------------------------------------------------------------------------------------------------------------------------------------
+ /**
+ * Read a stored Policy identified by the unique policyUri
+ *
+ * @param policyUri
+ * @return a stored Policy
+ */
+ public Policy readPolicy(String policyUri) throws PolicyException
+ {
+ return this.policies.get(policyUri);
+ }
+
+ /**
+ * Returns all the stored Policies for the system
+ *
+ * @return all the stored Policies
+ */
+ public Policy[] readAllPolicies() throws PolicyException
+ {
+ return this.policies.values().toArray(new Policy[0]);
+ }
+
+ /**
+ * Saves a Policy into storage. If this policy already exists in storage, then
+ * it updates it
+ *
+ * @param policy
+ * Policy to be saved into storage
+ */
+ public void savePolicy(Policy policy) throws PolicyException
+ {
+ this.policies.put(policy.getPolicyUri(), policy);
+ }
+
+ /**
+ * Deletes the specified Policy from storage
+ *
+ * @param policyUri
+ * unique identifier for the Policy
+ */
+ public void deletePolicy(String policyUri) throws PolicyException
+ {
+ this.policies.remove(policyUri);
+ }
+}
Modified:
modules/authorization/trunk/policy-server/src/main/resources/META-INF/authz-config.xml
===================================================================
---
modules/authorization/trunk/policy-server/src/main/resources/META-INF/authz-config.xml 2009-08-05
16:07:15 UTC (rev 13679)
+++
modules/authorization/trunk/policy-server/src/main/resources/META-INF/authz-config.xml 2009-08-05
16:45:20 UTC (rev 13680)
@@ -22,7 +22,7 @@
<bean name="/policy-server/PolicyDeployer"
class="org.jboss.security.authz.policy.server.provisioning.PolicyDeployer">
</bean>
- <bean name="/policy-server/PolicyStore"
class="org.jboss.security.authz.policy.server.provisioning.MemoryPolicyStore">
+ <bean name="/policy-server/PolicyStore"
class="org.jboss.security.authz.policy.server.provisioning.RelationalDBPolicyStore">
</bean>
<bean name="/policy-server/DroolsRuleManager"
class="org.jboss.security.authz.policy.server.plugin.DroolsRuleManager">
Added: modules/authorization/trunk/policy-server/src/main/resources/policy.hbm.xml
===================================================================
--- modules/authorization/trunk/policy-server/src/main/resources/policy.hbm.xml
(rev 0)
+++ modules/authorization/trunk/policy-server/src/main/resources/policy.hbm.xml 2009-08-05
16:45:20 UTC (rev 13680)
@@ -0,0 +1,38 @@
+<?xml version="1.0"?>
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ JBoss, a division of Red Hat ~
+ ~ Copyright 2006, Red Hat Middleware, LLC, and individual ~
+ ~ contributors as indicated by the @authors tag. See the ~
+ ~ copyright.txt in the distribution for a full listing of ~
+ ~ individual contributors. ~
+ ~ ~
+ ~ This is free software; you can redistribute it and/or modify it ~
+ ~ under the terms of the GNU Lesser General Public License as ~
+ ~ published by the Free Software Foundation; either version 2.1 of ~
+ ~ the License, or (at your option) any later version. ~
+ ~ ~
+ ~ This software is distributed in the hope that it will be useful, ~
+ ~ but WITHOUT ANY WARRANTY; without even the implied warranty of ~
+ ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ~
+ ~ Lesser General Public License for more details. ~
+ ~ ~
+ ~ You should have received a copy of the GNU Lesser General Public ~
+ ~ License along with this software; if not, write to the Free ~
+ ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA ~
+ ~ 02110-1301 USA, or see the FSF site:
http://www.fsf.org. ~
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+
+<!DOCTYPE hibernate-mapping PUBLIC
+ "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
+ "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
+
+<hibernate-mapping>
+ <class name="org.jboss.security.authz.policy.server.plugin.XACMLPolicy"
table="jboss_authz_xacml_policy">
+ <id name="id" column="id">
+ <generator class="native"/>
+ </id>
+ <property name="policyUri"
+ not-null="true"
+ />
+ </class>
+</hibernate-mapping>
\ No newline at end of file
Added:
modules/authorization/trunk/policy-server/src/test/java/org/jboss/security/authz/policy/server/provisioning/TestRelationalDBPolicyStore.java
===================================================================
---
modules/authorization/trunk/policy-server/src/test/java/org/jboss/security/authz/policy/server/provisioning/TestRelationalDBPolicyStore.java
(rev 0)
+++
modules/authorization/trunk/policy-server/src/test/java/org/jboss/security/authz/policy/server/provisioning/TestRelationalDBPolicyStore.java 2009-08-05
16:45:20 UTC (rev 13680)
@@ -0,0 +1,137 @@
+/*
+* JBoss, a division of Red Hat
+* Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+*/
+package org.jboss.security.authz.policy.server.provisioning;
+
+import java.io.Serializable;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.Logger;
+
+import org.hibernate.SessionFactory;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+
+import org.jboss.security.authz.bootstrap.ServiceContainer;
+
+import org.jboss.security.authz.model.Policy;
+import org.jboss.security.authz.model.PolicyMetaData;
+import org.jboss.security.authz.policy.server.plugin.XACMLPolicy;
+
+/**
+ * @author <a href="mailto:sshah@redhat.com">Sohil Shah</a>
+ */
+public class TestRelationalDBPolicyStore extends TestCase
+{
+ private static Logger log = Logger.getLogger(TestRelationalDBPolicyStore.class);
+
+ private RelationalDBPolicyStore policyStore;
+
+ protected void setUp() throws Exception
+ {
+ ServiceContainer.bootstrap();
+ this.policyStore =
(RelationalDBPolicyStore)ServiceContainer.lookup("/policy-server/PolicyStore");
+ }
+
+ public void testSavePolicy() throws Exception
+ {
+ String policyUri = "blahPolicy";
+
+ //Create and Save a new policy
+ Policy policy = new XACMLPolicy(policyUri, new PolicyMetaData());
+ Serializable id = this.save(policy);
+ assertNotNull("Id Should Not be Null!!", id);
+ log.info("New Policy Id="+id);
+
+ //Read the persisted instance
+ policy = this.readPolicy(policyUri);
+ assertNotNull("Policy Must Not be Null!!", policy);
+ assertEquals("PolicyUri Must Match!!", policyUri, policy.getPolicyUri());
+ log.info("Stored Policy Id="+policy.getId());
+ log.info("Stored Policy Uri="+policy.getPolicyUri());
+ }
+ //-----Hibernate Data Access
code-----------------------------------------------------------------------------------------------------------------------------------
+ private Serializable save(Policy policy) throws Exception
+ {
+ SessionFactory sessionFactory = this.policyStore.sessionFactory;
+ Session session = null;
+ Transaction tx = null;
+ try
+ {
+ //Join or Create a New Session/Transaction
+ session = sessionFactory.getCurrentSession();
+ tx = session.beginTransaction();
+
+ Serializable id = session.save(policy);
+
+ tx.commit();
+
+ return id;
+ }
+ catch(Throwable t)
+ {
+ log.error(this, t);
+ tx.rollback();
+ throw new RuntimeException(t);
+ }
+ finally
+ {
+ if(session.isOpen())
+ {
+ session.close();
+ }
+ }
+ }
+
+ private Policy readPolicy(String policyUri) throws Exception
+ {
+ SessionFactory sessionFactory = this.policyStore.sessionFactory;
+ Session session = null;
+ Transaction tx = null;
+ try
+ {
+ //Join or Create a New Session/Transaction
+ session = sessionFactory.getCurrentSession();
+ tx = session.beginTransaction();
+
+ Policy policy = (Policy)session.createQuery("from XACMLPolicy where
policyUri=?").
+ setString(0, policyUri).uniqueResult();
+
+ tx.commit();
+
+ return policy;
+ }
+ catch(Throwable t)
+ {
+ log.error(this, t);
+ tx.rollback();
+ throw new RuntimeException(t);
+ }
+ finally
+ {
+ if(session.isOpen())
+ {
+ session.close();
+ }
+ }
+ }
+}
Added: modules/authorization/trunk/policy-server/src/test/resources/hibernate.cfg.xml
===================================================================
--- modules/authorization/trunk/policy-server/src/test/resources/hibernate.cfg.xml
(rev 0)
+++
modules/authorization/trunk/policy-server/src/test/resources/hibernate.cfg.xml 2009-08-05
16:45:20 UTC (rev 13680)
@@ -0,0 +1,59 @@
+<?xml version='1.0' encoding='utf-8'?>
+<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ JBoss, a division of Red Hat ~
+ ~ Copyright 2006, Red Hat Middleware, LLC, and individual ~
+ ~ contributors as indicated by the @authors tag. See the ~
+ ~ copyright.txt in the distribution for a full listing of ~
+ ~ individual contributors. ~
+ ~ ~
+ ~ This is free software; you can redistribute it and/or modify it ~
+ ~ under the terms of the GNU Lesser General Public License as ~
+ ~ published by the Free Software Foundation; either version 2.1 of ~
+ ~ the License, or (at your option) any later version. ~
+ ~ ~
+ ~ This software is distributed in the hope that it will be useful, ~
+ ~ but WITHOUT ANY WARRANTY; without even the implied warranty of ~
+ ~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ~
+ ~ Lesser General Public License for more details. ~
+ ~ ~
+ ~ You should have received a copy of the GNU Lesser General Public ~
+ ~ License along with this software; if not, write to the Free ~
+ ~ Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA ~
+ ~ 02110-1301 USA, or see the FSF site:
http://www.fsf.org. ~
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
+
+<!DOCTYPE hibernate-configuration PUBLIC
+ "-//Hibernate/Hibernate Configuration DTD//EN"
+ "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
+
+<hibernate-configuration>
+ <session-factory>
+ <!-- Database connection settings -->
+ <property
name="connection.driver_class">org.hsqldb.jdbcDriver</property>
+ <property
name="connection.url">jdbc:hsqldb:file:target/testdb</property>
+ <property name="connection.username">sa</property>
+ <property name="connection.password"></property>
+
+ <!-- JDBC connection pool (use the built-in) -->
+ <property name="connection.pool_size">1</property>
+
+ <!-- SQL dialect -->
+ <property
name="dialect">org.hibernate.dialect.HSQLDialect</property>
+
+ <!-- Enable Hibernate's automatic session context management -->
+ <property
name="current_session_context_class">thread</property>
+
+ <!-- Disable the second-level cache -->
+ <property
name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
+
+ <!-- Echo all executed SQL to stdout -->
+ <property name="show_sql">true</property>
+
+ <!--
+ Drop and re-create the database schema on startup
+ -->
+ <property name="hbm2ddl.auto">create</property>
+
+ <mapping resource="policy.hbm.xml"/>
+ </session-factory>
+</hibernate-configuration>
\ No newline at end of file
Modified: modules/authorization/trunk/pom.xml
===================================================================
--- modules/authorization/trunk/pom.xml 2009-08-05 16:07:15 UTC (rev 13679)
+++ modules/authorization/trunk/pom.xml 2009-08-05 16:45:20 UTC (rev 13680)
@@ -35,6 +35,10 @@
<!-- xstream dependency used by the agent side caching service -->
<version.com.thoughtworks.xstream>1.3.1</version.com.thoughtworks.xstream>
+ <!-- Hibernate related dependencies used by the RelationalDBPolicyStore -->
+ <version.org.hibernate>3.1.2</version.org.hibernate>
+ <version.hsqldb.hsqldb>1.8.0.7</version.hsqldb.hsqldb>
+
<version.org.jboss.microcontainer>2.0.2.GA</version.org.jboss.microcontainer>
<version.org.jboss.jboss-reflect>2.0.2.GA</version.org.jboss.jboss-reflect>
<version.org.jboss.jboss-common-core>2.2.9.GA</version.org.jboss.jboss-common-core>
@@ -194,7 +198,19 @@
<artifactId>commons-httpclient</artifactId>
<version>${version.commons-httpclient}</version>
</dependency>
- </dependencies>
+
+ <!-- Hibernate Dependency -->
+ <dependency>
+ <groupId>org.hibernate</groupId>
+ <artifactId>hibernate</artifactId>
+ <version>${version.org.hibernate}</version>
+ </dependency>
+ <dependency>
+ <groupId>hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <version>${version.hsqldb.hsqldb}</version>
+ </dependency>
+ </dependencies>
</dependencyManagement>
<!-- project wide dependencies -->
@@ -224,6 +240,11 @@
<artifactId>commons-httpclient</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>hsqldb</groupId>
+ <artifactId>hsqldb</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>