Java Code Examples for org.apache.neethi.Policy#addAssertion()

The following examples show how to use org.apache.neethi.Policy#addAssertion() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ExternalAttachmentProviderTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
void setUpAttachment(Object subject, boolean applies, ExternalAttachmentProvider eap) {
    attachments.clear();
    attachment = control.createMock(PolicyAttachment.class);
    attachments.add(attachment);
    policy = new Policy();
    assertion = new PrimitiveAssertion(TEST_ASSERTION_TYPE);
    policy.addAssertion(assertion);
    eap.setAttachments(attachments);
    if (subject instanceof ServiceInfo) {
        EasyMock.expect(attachment.appliesTo((ServiceInfo)subject)).andReturn(applies);
    } else if (subject instanceof EndpointInfo) {
        EasyMock.expect(attachment.appliesTo((EndpointInfo)subject)).andReturn(applies);
    } else if (subject instanceof BindingOperationInfo) {
        EasyMock.expect(attachment.appliesTo((BindingOperationInfo)subject)).andReturn(applies);
    } else if (subject instanceof BindingMessageInfo) {
        EasyMock.expect(attachment.appliesTo((BindingMessageInfo)subject)).andReturn(applies);
    } else if (subject instanceof BindingFaultInfo) {
        EasyMock.expect(attachment.appliesTo((BindingFaultInfo)subject)).andReturn(applies);
    } else {
        System.err.println("subject class: " + subject.getClass());
    }
    if (applies) {
        EasyMock.expect(attachment.getPolicy()).andReturn(policy);
    }
}
 
Example 2
Source File: EndpointPolicyImplTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void doTestUpdateWithEmptyPolicy(Policy emptyPolicy) {
    Policy p1 = new Policy();
    QName aqn1 = new QName("http://x.y.z", "a");
    p1.addAssertion(mockAssertion(aqn1, 5, true));

    EndpointPolicyImpl epi = new TestEndpointPolicy();
    control.replay();

    epi.setPolicy(p1.normalize(true));

    Policy ep = epi.updatePolicy(emptyPolicy, createMessage()).getPolicy();

    List<ExactlyOne> pops = CastUtils.cast(ep.getPolicyComponents(), ExactlyOne.class);
    assertEquals("New policy must have 1 top level policy operator", 1, pops.size());
    List<All> alts = CastUtils.cast(pops.get(0).getPolicyComponents(), All.class);
    assertEquals("1 alternatives should be available", 1, alts.size());

    List<PolicyAssertion> assertions1 = CastUtils
        .cast(alts.get(0).getAssertions(), PolicyAssertion.class);
    assertEquals("1 assertion should be available", 1, assertions1.size());

    QName n1 = assertions1.get(0).getName();
    assertEquals("Policy was not merged", n1, aqn1);
}
 
Example 3
Source File: SecurityWithServiceDescriptorTest.java    From product-ei with Apache License 2.0 5 votes vote down vote up
private static Policy loadPolicy(String xmlPath, String clientKey, String userName)
		throws Exception {

	StAXOMBuilder builder = new StAXOMBuilder(xmlPath);
	Policy policy = PolicyEngine.getPolicy(builder.getDocumentElement());

	RampartConfig rc = new RampartConfig();

	rc.setUser(userName);
	rc.setUserCertAlias("wso2carbon");
	rc.setEncryptionUser("wso2carbon");
	rc.setPwCbClass(SecurityWithServiceDescriptorTest.class.getName());

	CryptoConfig sigCryptoConfig = new CryptoConfig();
	sigCryptoConfig.setProvider("org.apache.ws.security.components.crypto.Merlin");

	Properties prop1 = new Properties();
	prop1.put("org.apache.ws.security.crypto.merlin.keystore.type", "JKS");
	prop1.put("org.apache.ws.security.crypto.merlin.file", clientKey);
	prop1.put("org.apache.ws.security.crypto.merlin.keystore.password", "wso2carbon");
	sigCryptoConfig.setProp(prop1);

	CryptoConfig encrCryptoConfig = new CryptoConfig();
	encrCryptoConfig.setProvider("org.apache.ws.security.components.crypto.Merlin");

	Properties prop2 = new Properties();
	prop2.put("org.apache.ws.security.crypto.merlin.keystore.type", "JKS");
	prop2.put("org.apache.ws.security.crypto.merlin.file", clientKey);
	prop2.put("org.apache.ws.security.crypto.merlin.keystore.password", "wso2carbon");
	encrCryptoConfig.setProp(prop2);

	rc.setSigCryptoConfig(sigCryptoConfig);
	rc.setEncrCryptoConfig(encrCryptoConfig);

	policy.addAssertion(rc);
	return policy;
}
 
Example 4
Source File: IdentityBaseUtil.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
public static Policy getDefaultRampartConfig() {

        //Extract the primary keystore information from server configuration
        ServerConfiguration serverConfig = ServerConfiguration.getInstance();
        String keyStore = serverConfig.getFirstProperty("Security.KeyStore.Location");
        String keyStoreType = serverConfig.getFirstProperty("Security.KeyStore.Type");
        String keyStorePassword = serverConfig.getFirstProperty("Security.KeyStore.Password");
        String privateKeyAlias = serverConfig.getFirstProperty("Security.KeyStore.KeyAlias");
        String privateKeyPassword = serverConfig.getFirstProperty("Security.KeyStore.KeyPassword");

        //Populate Rampart Configuration
        RampartConfig rampartConfig = new RampartConfig();
        rampartConfig.setUser(privateKeyAlias);
        //TODO use a registry based callback handler
        rampartConfig.setPwCbClass("org.wso2.carbon.identity.base.InMemoryPasswordCallbackHandler");

        //Set the private key alias and private key password in the password callback handler
        InMemoryPasswordCallbackHandler.addUser(privateKeyAlias, privateKeyPassword);

        CryptoConfig sigCrypto = new CryptoConfig();
        Properties props = new Properties();
        sigCrypto.setProvider("org.apache.ws.security.components.crypto.Merlin");
        props.setProperty("org.apache.ws.security.crypto.merlin.keystore.type", keyStoreType);
        props.setProperty("org.apache.ws.security.crypto.merlin.file", keyStore);
        props.setProperty("org.apache.ws.security.crypto.merlin.keystore.password", keyStorePassword);

        // This property is set in order to fix IDENTITY-1931.
        // This issue is however not found in IS-4.5.0.
        // The reason for the error is unknown. Suspecting JCE provider.
        // Error occurrs when WSS4J tries to read the certificates in the JDK's cacerts store.
        props.setProperty("org.apache.ws.security.crypto.merlin.load.cacerts", "false");
        sigCrypto.setProp(props);

        rampartConfig.setSigCryptoConfig(sigCrypto);
        Policy policy = new Policy();
        policy.addAssertion(rampartConfig);

        return policy;

    }
 
Example 5
Source File: PolicyEngineTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddAssertions() {
    engine = new PolicyEngineImpl();
    Collection<Assertion> assertions = new ArrayList<>();

    Assertion a = control.createMock(Assertion.class);
    EasyMock.expect(a.getType()).andReturn(Constants.TYPE_ASSERTION);
    EasyMock.expect(a.isOptional()).andReturn(true);

    control.replay();
    engine.addAssertions(a, false, assertions);
    assertTrue(assertions.isEmpty());
    control.verify();

    control.reset();
    EasyMock.expect(a.getType()).andReturn(Constants.TYPE_ASSERTION);
    control.replay();
    engine.addAssertions(a, true, assertions);
    assertEquals(1, assertions.size());
    assertSame(a, assertions.iterator().next());
    control.verify();

    assertions.clear();
    Policy p = new Policy();
    a = new PrimitiveAssertion(new QName("http://x.y.z", "a"));
    p.addAssertion(a);

    // id has no #
    engine.getRegistry().register("ab", p);

    // local reference is an id + #
    PolicyReference pr = new PolicyReference();
    pr.setURI("#ab");

    engine.addAssertions(pr, false, assertions);
    assertEquals(1, assertions.size());
    assertSame(a, assertions.iterator().next());
}
 
Example 6
Source File: EndpointPolicyImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdatePolicy() {

    EndpointPolicyImpl epi = new TestEndpointPolicy();

    Policy p1 = new Policy();
    QName aqn1 = new QName("http://x.y.z", "a");
    p1.addAssertion(mockAssertion(aqn1, 5, true));

    Policy p2 = new Policy();
    QName aqn2 = new QName("http://x.y.z", "b");
    p2.addAssertion(mockAssertion(aqn2, 5, true));
    control.replay();

    epi.setPolicy(p1.normalize(null, true));

    Policy ep = epi.updatePolicy(p2, createMessage()).getPolicy();

    List<ExactlyOne> pops = CastUtils.cast(ep.getPolicyComponents(), ExactlyOne.class);
    assertEquals("New policy must have 1 top level policy operator", 1, pops.size());
    List<All> alts = CastUtils.cast(pops.get(0).getPolicyComponents(), All.class);
    assertEquals("2 alternatives should be available", 2, alts.size());

    List<PolicyAssertion> assertions1 = CastUtils
        .cast(alts.get(0).getAssertions(), PolicyAssertion.class);
    assertEquals("1 assertion should be available", 1, assertions1.size());

    List<PolicyAssertion> assertions2 = CastUtils
        .cast(alts.get(1).getAssertions(), PolicyAssertion.class);
    assertEquals("1 assertion should be available", 1, assertions2.size());

    QName n1 = assertions1.get(0).getName();
    QName n2 = assertions2.get(0).getName();
    assertTrue("Policy was not merged",
               n1.equals(aqn1) && n2.equals(aqn2) || n1.equals(aqn2) && n2.equals(aqn1));
}
 
Example 7
Source File: AssertionInfoMapTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testAllAssertionsIn() {

    Policy nested = new Policy();
    Assertion nb = new PrimitiveAssertion(
        new QName("http://x.y.z", "b"));
    nested.addAssertion(nb);

    Policy p = new Policy();
    Assertion a1 = new PrimitiveAssertion(
                            new QName("http://x.y.z", "a"));
    Assertion a2 = new PrimitiveAssertion(
                             new QName("http://x.y.z", "a"));
    Assertion b = new PrimitiveAssertion(
                            new QName("http://x.y.z", "b"));
    Assertion c = new PolicyContainingPrimitiveAssertion(
                           new QName("http://x.y.z", "c"), false, false, nested);

    All alt1 = new All();
    alt1.addAssertion(a1);
    alt1.addAssertion(b);
    All alt2 = new All();
    alt1.addAssertion(a2);
    alt2.addAssertion(c);
    ExactlyOne ea = new ExactlyOne();
    ea.addPolicyComponent(alt1);
    ea.addPolicyComponent(alt2);
    p.addPolicyComponent(ea);

    AssertionInfoMap aim = new AssertionInfoMap(p);

    Collection<AssertionInfo> listA =
        aim.getAssertionInfo(new QName("http://x.y.z", "a"));
    assertEquals("2 A assertions should've been added", 2, listA.size());
    AssertionInfo[] ais = listA.toArray(new AssertionInfo[] {});
    assertTrue("Two different A instances should be added",
               ais[0].getAssertion() == a1 && ais[1].getAssertion() == a2
               || ais[0].getAssertion() == a2 && ais[1].getAssertion() == a1);

    Collection<AssertionInfo> listB =
        aim.getAssertionInfo(new QName("http://x.y.z", "b"));
    assertEquals("2 B assertions should've been added", 2, listB.size());
    ais = listB.toArray(new AssertionInfo[] {});
    assertTrue("Two different B instances should be added",
               ais[0].getAssertion() == nb && ais[1].getAssertion() == b
               || ais[0].getAssertion() == b && ais[1].getAssertion() == nb);

    Collection<AssertionInfo> listC =
        aim.getAssertionInfo(new QName("http://x.y.z", "c"));
    assertEquals("1 C assertion should've been added", 1, listC.size());
    ais = listC.toArray(new AssertionInfo[] {});
    assertSame("One C instances should be added",
               ais[0].getAssertion(), c);

}