Skip to content

Commit

Permalink
security policy component added
Browse files Browse the repository at this point in the history
  • Loading branch information
IsurangaPerera committed Jul 12, 2017
1 parent 5cdae67 commit e80dba2
Show file tree
Hide file tree
Showing 10 changed files with 238 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
package org.wso2.carbon.sts.resources;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

import javax.ws.rs.core.Context;
import javax.xml.stream.XMLStreamReader;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.ExchangeImpl;
import org.apache.cxf.message.MessageImpl;
import org.apache.cxf.staxutils.StaxUtils;
import org.wso2.msf4j.Request;

public abstract class AbstractResource {

private SoapMessage setUpMessage(byte[] msg) throws Exception {
SoapMessage message = new SoapMessage(new MessageImpl());
Exchange ex = new ExchangeImpl();
ex.setInMessage(message);
message.setContent(XMLStreamReader.class, StaxUtils.createXMLStreamReader(new ByteArrayInputStream(msg)));
return message;
}

protected SoapMessage setUpMessage(byte[] msg) throws Exception {
SoapMessage message = new SoapMessage(new MessageImpl());
Exchange ex = new ExchangeImpl();
ex.setInMessage(message);
message.setContent(XMLStreamReader.class,
StaxUtils.createXMLStreamReader(new ByteArrayInputStream(msg)));
return message;
}

public abstract void processRequest(@Context Request request)
throws UnsupportedEncodingException;

}
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
package org.wso2.carbon.sts.resources;

import io.swagger.annotations.Api;
import io.swagger.annotations.Info;
import io.swagger.annotations.License;
import io.swagger.annotations.SwaggerDefinition;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import org.apache.cxf.binding.soap.SoapMessage;
import org.osgi.service.component.annotations.Component;
import org.wso2.msf4j.Microservice;
import org.wso2.msf4j.Request;
import org.wso2.msf4j.util.BufferUtil;

@Component(
name = "org.wso2.carbon.sts.resources.STSResource",
service = Microservice.class,
immediate = true
)
@Api(value = "scim/v2/ServiceProviderConfig")
@SwaggerDefinition(
info = @Info(
title = "/SecurityTokenService Endpoint Swagger Definition", version = "1.0",
description = "STS /SecurityTokenService endpoint",
license = @License(name = "Apache 2.0", url = "http://www.apache.org/licenses/LICENSE-2.0"))
)
@Path("/services")
public class STSResource extends AbstractResource {

@Path("/wso2carbon-sts")
@POST
@Consumes(MediaType.TEXT_XML)
public void processRequest(@Context Request request) throws UnsupportedEncodingException{
List<ByteBuffer> fullMessageBody = request.getFullMessageBody();
ByteBuffer buffer = BufferUtil.merge(fullMessageBody);

SoapMessage soap = null;
try {
soap = setUpMessage(buffer.array());
} catch (Exception e) {
e.printStackTrace();
}
}

}
26 changes: 26 additions & 0 deletions org.wso2.carbon.sts.policy.mgt/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
51 changes: 51 additions & 0 deletions org.wso2.carbon.sts.policy.mgt/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
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. -->
<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">

<parent>
<groupId>org.wso2.carbon.sts</groupId>
<artifactId>identity-inbound-auth-sts</artifactId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>org.wso2.carbon.sts.policy.mgt</artifactId>
<packaging>bundle</packaging>
<name>WSO2 Carbon - Sample Carbon Component</name>

<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-ws-policy -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-policy</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.wso2.msf4j</groupId>
<artifactId>msf4j-core</artifactId>
<version>2.1.0</version>
</dependency>

<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.1.11</version>
</dependency>
</dependencies>

<properties>
<private.package>org.wso2.carbon.sts.policy.mgt.internal</private.package>
<export.package>!org.wso2.carbon.sts.policy.mgt.internal,
org.wso2.carbon.sts.policy.mgt.*;version="5.1.1"</export.package>
<import.package>org.osgi.framework.*;version="[1.8.0, 2.0.0)",
org.wso2.carbon.kernel;version="[5.0.0, 6.0.0)"</import.package>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.wso2.carbon.sts.policy.mgt;

import java.io.FileNotFoundException;
import java.io.FileReader;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.apache.neethi.PolicyBuilder;
import org.apache.neethi.Policy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wso2.carbon.sts.policy.mgt.internal.DataHolder;
import org.wso2.carbon.sts.policy.util.PolicyConstants;

public class SecurityPolicyManager implements SecurityPolicyProvider {

private static final Logger logger = LoggerFactory
.getLogger(DataHolder.class);

private Policy effectivePolicy;

@Override
public void updateEffectivePolicy(int scenario) {

XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader streamReader = null;
try {
streamReader = factory.createXMLStreamReader(new FileReader(
PolicyConstants.POLICY_REPOSITORY_PATH + Integer.toString(scenario)
+ ".xml"));
} catch (FileNotFoundException | XMLStreamException e) {
logger.error("Policy File Not Found");
}
PolicyBuilder builder = new PolicyBuilder();
setEffectivePolicy(builder.getPolicy(streamReader));
}

public Policy getEffectivePolicy() {
return effectivePolicy;
}

public void setEffectivePolicy(Policy effectivePolicy) {
this.effectivePolicy = effectivePolicy;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.wso2.carbon.sts.policy.mgt;

public interface SecurityPolicyProvider {

public void updateEffectivePolicy(int scenario);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.wso2.carbon.sts.policy.mgt.internal;

public class DataHolder {
private int policyScenario;

private static DataHolder instance = new DataHolder();

private DataHolder() {

}

public static DataHolder getInstance() {
return instance;
}

public int getPolicyScenario() {
return policyScenario;
}

public void setPolicyScenario(int i) {
policyScenario = i;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.wso2.carbon.sts.policy.mgt.internal;

import java.util.logging.Logger;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.wso2.carbon.sts.policy.mgt.SecurityPolicyManager;
import org.wso2.carbon.sts.policy.mgt.SecurityPolicyProvider;

@Component(name = "org.wso2.carbon.sts.policy.mgt.internal.ServiceComponent", immediate = true)
public class ServiceComponent {

Logger logger = Logger.getLogger(ServiceComponent.class.getName());
private ServiceRegistration<?> serviceRegistration;

@Activate
protected void start(BundleContext bundleContext) throws Exception {
logger.info("Policy Management Component is activated");

//Replace this with appropriate policy retrieval method
DataHolder.getInstance().setPolicyScenario(1);

serviceRegistration = bundleContext.registerService(
SecurityPolicyProvider.class.getName(),
new SecurityPolicyManager(), null);
}

@Deactivate
protected void stop() throws Exception {
logger.info("Service Component is deactivated");

serviceRegistration.unregister();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.wso2.carbon.sts.policy.util;

public class PolicyConstants {

//Need to replace with policy repository path
public static String POLICY_REPOSITORY_PATH ;
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<module>org.wso2.carbon.sts.claim.mgt</module>
<module>org.wso2.carbon.sts.token.provider</module>
<module>org.wso2.carbon.sts2</module>
<module>org.wso2.carbon.sts.policy.mgt</module>
</modules>

<dependencies>
Expand Down

0 comments on commit e80dba2

Please sign in to comment.