Commit 62170419 authored by frederic motte's avatar frederic motte
Browse files

add the sf generator functionnalities and the configuration of the SF

parent ce0ffb14
......@@ -52,6 +52,26 @@
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.common</artifactId>
<version>2.11.0-v20150805-0538</version>
</dependency>
<dependency>
<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.ecore</artifactId>
<version>2.11.1-v20150805-0538</version>
</dependency>
<dependency>
<groupId>org.eclipse.emf</groupId>
<artifactId>org.eclipse.emf.ecore.xmi</artifactId>
<version>2.11.1-v20150805-0538</version>
</dependency>
<dependency>
<groupId>eu.chorevolution.modelingnotations</groupId>
<artifactId>eu.chorevolution.modelingnotations.security</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<repositories>
......@@ -102,29 +122,12 @@
<skip>true</skip>
</configuration>
</plugin>
<!-- <plugin>
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>
<version>0.11</version>
<configuration>
<excludes>
<exclude>**/rat.txt</exclude>
<exclude>**/build-copy-javadoc-files.xml</exclude>
<exclude>**/*.log</exclude>
<exclude>.git/**</exclude>
<exclude>**/.*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>rat-check</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin> -->
<!-- <plugin> <groupId>org.apache.rat</groupId> <artifactId>apache-rat-plugin</artifactId>
<version>0.11</version> <configuration> <excludes> <exclude>**/rat.txt</exclude>
<exclude>**/build-copy-javadoc-files.xml</exclude> <exclude>**/*.log</exclude>
<exclude>.git/**</exclude> <exclude>**/.*</exclude> </excludes> </configuration>
<executions> <execution> <id>rat-check</id> <phase>verify</phase> <goals>
<goal>check</goal> </goals> </execution> </executions> </plugin> -->
<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-checkstyle-plugin</artifactId>
<version>2.17</version> <configuration> <configLocation>${basedir}/src/main/resources/checkstyle.xml</configLocation>
......@@ -132,52 +135,17 @@
<id>checkstyle-check</id> <phase>verify</phase> <goals> <goal>check</goal>
</goals> </execution> </executions> </plugin> -->
<!-- Put NOTICE and LICENSE files in all artifacts and javadocs -->
<!-- <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-artifact-legal-files</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/META-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}</directory>
<includes>
<include>LICENSE</include>
<include>NOTICE</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-javadoc-legal-files</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/apidocs/META-INF</outputDirectory>
<resources>
<resource>
<directory>${basedir}</directory>
<includes>
<include>LICENSE</include>
<include>NOTICE</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
-->
<!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId>
<version>2.7</version> <executions> <execution> <id>copy-artifact-legal-files</id>
<phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals>
<configuration> <outputDirectory>${project.build.directory}/classes/META-INF</outputDirectory>
<resources> <resource> <directory>${basedir}</directory> <includes> <include>LICENSE</include>
<include>NOTICE</include> </includes> </resource> </resources> </configuration>
</execution> <execution> <id>copy-javadoc-legal-files</id> <phase>process-resources</phase>
<goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/apidocs/META-INF</outputDirectory>
<resources> <resource> <directory>${basedir}</directory> <includes> <include>LICENSE</include>
<include>NOTICE</include> </includes> </resource> </resources> </configuration>
</execution> </executions> </plugin> -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
......
......@@ -4,6 +4,16 @@ import eu.chorevolution.transformations.sfgenerator.model.SF;
public interface SFGenerator {
SF generateSecurityFilter(String sfName, String role, String STSUrl) throws SFGeneratorException;
/**
* Generation of the security filter
* @param sfName Name of the securityFilter
* @param role Role of the securityFilter
* @param STSUrl URL of the Federation Server
* @param securityModel The security model of the service
* @param serviceType The kind of the service (consumer, provider, prosumer)
* @return A SF element which contains the WAR element
* @throws SFGeneratorException
*/
SF generateSecurityFilter(String sfName, String role, String STSUrl, byte[] securityModel, ServiceType serviceType) throws SFGeneratorException;
}
......@@ -2,40 +2,123 @@ package eu.chorevolution.transformations.sfgenerator.impl;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.apache.commons.io.FileUtils;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import eu.chorevolution.transformations.sfgenerator.SFGeneratorException;
import eu.chorevolution.transformations.sfgenerator.ServiceType;
import eu.chorevolution.transformations.sfgenerator.impl.utility.Utilities;
import eu.chorevolution.transformations.sfgenerator.model.SF;
import eu.chorevolution.transformations.sfgenerator.util.SecurityModelUtil;
import eu.chorevolution.modelingnotations.configuration.ObjectFactory;
import eu.chorevolution.modelingnotations.configuration.SecurityPolicy;
import eu.chorevolution.modelingnotations.security.Authentication;
import eu.chorevolution.modelingnotations.security.CredentialType;
import eu.chorevolution.modelingnotations.security.SecurityModel;
import eu.chorevolution.modelingnotations.security.SecurityPolicySet;
import eu.chorevolution.transformations.sfgenerator.SFGenerator;
public class SFGeneratorImpl implements SFGenerator {
private File generateConfigurationFile(String sfName, String destDir, byte[] securityModel, ServiceType serviceType) throws SFGeneratorException {
File securityFile = Utilities.createSecurityModel(destDir, sfName, securityModel);
URI securityURI = URI.createURI(securityFile.toURI().toString());
SecurityModel sModel = SecurityModelUtil.loadSecurityModel(securityURI);
if (sModel.getSecuritypolicyset()==null)
throw new SFGeneratorException("No security policyu set defined into the security model");
SecurityPolicySet policyset = sModel.getSecuritypolicyset();
ObjectFactory SecurityPolicyFactory = new ObjectFactory();
SecurityPolicy securityPolicy = SecurityPolicyFactory.createSecurityPolicy();
securityPolicy.setDomain(sModel.getSecuritypolicyset().getDomainName());
securityPolicy.setConsumer(SecurityPolicyFactory.createSecurityPolicyConsumer());
securityPolicy.getConsumer().setCheckAuthN(true);
securityPolicy.getConsumer().setCheckAuthZ(true);
securityPolicy.getConsumer().setType("User");
securityPolicy.setProvider(SecurityPolicyFactory.createSecurityPolicyProvider());
securityPolicy.getProvider().setType(serviceType.name());
securityPolicy.getProvider().setServiceName(sModel.getSecuritypolicyset().getServiceName());
securityPolicy.getProvider().setRessourceURL(sModel.getSecuritypolicyset().getRessourceURL());
if (sModel.getSecuritypolicyset().getAuthentication()!=null){
EList<Authentication> AuthNList = sModel.getSecuritypolicyset().getAuthentication();
for (Iterator iterator = AuthNList.iterator(); iterator.hasNext();) {
Authentication authentication = (Authentication) iterator.next();
if (authentication.getCredentialType().equals(CredentialType.USERNAME_PASSWORD)){
eu.chorevolution.modelingnotations.configuration.SecurityPolicy.Provider.Credential e = SecurityPolicyFactory.createSecurityPolicyProviderCredential();
securityPolicy.getProvider().setCredential(e);
securityPolicy.getProvider().getCredential().setCredentialType(authentication.getCredentialType().getName());
securityPolicy.getProvider().getCredential().setGenericAccount(authentication.getGenericAccount());
securityPolicy.getProvider().getCredential().setGenericCredential(authentication.getGenericCredential());
}
}
}
File configxml = new File(destDir+System.getProperty("file.separator")+"WEB-INF"+System.getProperty("file.separator")+"config.xml");
JAXBContext jaxbContext;
try {
jaxbContext = JAXBContext.newInstance(SecurityPolicy.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(securityPolicy, configxml);
} catch (JAXBException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
throw new SFGeneratorException(e1);
}
return configxml;
}
@Override
public SF generateSecurityFilter(String sfName, String role, String STSUrl) throws SFGeneratorException {
public SF generateSecurityFilter(String sfName, String role, String STSUrl, byte[] securityModel, ServiceType serviceType) throws SFGeneratorException {
SF sf = new SF(sfName,role);
String destDir = FileUtils.getTempDirectoryPath();
String initialDestDir = destDir;
destDir = Utilities.getDestinationFolderPath(destDir);
System.out.println("destDir" + destDir);
Utilities.createWebXml(destDir, sf.getName(), STSUrl);
Utilities.copyLibsFolder(destDir);
Utilities.copyClassesFolder(destDir);
Utilities.createWar(sf.getName(), destDir);
System.out.println("destDir" + destDir);
Utilities.deleteProjectFolder(destDir);
sf.setWar(Utilities.getBytesFromWar(initialDestDir,sf.getName()));
try {
String destDir = FileUtils.getTempDirectoryPath();
String initialDestDir = destDir;
destDir = Utilities.getDestinationFolderPath(destDir);
Utilities.deleteProjectFolder(destDir);
File warResultFile = Utilities.copyWarTemplate(destDir);
File configurationFile = generateConfigurationFile(sfName, destDir, securityModel, serviceType);
Utilities.addConfigFileintoWar(destDir, configurationFile);
File webXml = Utilities.createWebXml(destDir, sf.getName(), STSUrl);
Utilities.addWebXmlFileintoWar(destDir, webXml);
sf.setWar(Utilities.getBytesFromWar(warResultFile));
Utilities.deleteProjectFolder(destDir);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sf;
}
public static void main(String[] args) throws IOException {
Path securityPath = Paths.get("My.security");
byte[] securityModel = Files.readAllBytes(securityPath);
SFGenerator cdGenerator = new SFGeneratorImpl();
SF cd = cdGenerator.generateSecurityFilter("test","Trvc", "http://localhost:8080/SecurityTokenService/services/securitytokenservice");
FileUtils.writeByteArrayToFile(new File( "." + File.separatorChar + "src" + File.separatorChar + "main"
+ File.separatorChar + "resources" + File.separatorChar+"SecurityfilterServletProxy.war"), cd.getWar());
SF cd = cdGenerator.generateSecurityFilter("tes2","SF", "http://192.168.150.131:8080/SecurityTokenService/services/securitytokenservice", securityModel, ServiceType.PROSUMER);
FileUtils.writeByteArrayToFile(new File( "." + File.separatorChar + "SecurityfilterServletProxy.war"), cd.getWar());
}
}
package eu.chorevolution.transformations.sfgenerator.impl.utility;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import eu.chorevolution.transformations.sfgenerator.SFGeneratorException;
public class Utilities {
private final static int BUFFERSIZE = 32768;
public final static String FILESEPARATOR = System.getProperty("file.separator");
private static boolean aborted = false;
private static int writtenBytes;
public final static String WARTEMPLATENAME = "SecurityfilterServletProxy.war";
private static Logger logger = LoggerFactory.getLogger(Utilities.class);
private final static String xmlLicenseHeader = new StringBuilder()
......@@ -67,37 +60,24 @@ public class Utilities {
.append("#").append(System.getProperty("line.separator"))
.toString();
public static void createWarProjectFoldersStructure(String projectDir,String cdName, String wsdlLocation) {
File webinf = new File(projectDir+System.getProperty("file.separator")+cdName+System.getProperty("file.separator")+"WEB-INF");
File classes = new File(projectDir+System.getProperty("file.separator")+cdName+System.getProperty("file.separator")+"WEB-INF"+System.getProperty("file.separator")+"classes");
File lib = new File(projectDir+System.getProperty("file.separator")+cdName+System.getProperty("file.separator")+"WEB-INF"+System.getProperty("file.separator")+"lib");
File wsdl = new File(projectDir+System.getProperty("file.separator")+cdName+System.getProperty("file.separator")+"WEB-INF"+System.getProperty("file.separator")+"wsdl");
try {
FileUtils.forceMkdir(webinf);
classes.mkdir();
lib.mkdir();
wsdl.mkdir();
FileUtils.copyFileToDirectory(new File(wsdlLocation), wsdl);
} catch (IOException e) {
logger.info(e.getMessage());
}
}
/**
* generate the desination folder
* @param destDir
* @return
*/
public static String getDestinationFolderPath(String destDir){
return (destDir+System.getProperty("file.separator")+System.currentTimeMillis()).replaceAll("\\s", "_");
}
public static void createWarFoldersStructure(String destPath){
try {
FileUtils.copyDirectoryToDirectory(new File(Utilities.class.getClassLoader().getResource("war-structure"+System.getProperty("file.separator")+"WEB-INF").getPath()), new File(destPath));
FileUtils.copyFileToDirectory(new File(Utilities.class.getClassLoader().getResource("war-structure"+System.getProperty("file.separator")+"index.jsp").getPath()), new File(destPath));
} catch (IOException e) {
logger.info(e.getMessage()); }
}
public static void createWebXml(String projectDir,String cdName, String sTSUrl){
/**
* Create the Web.xml file of the web archive
* @param projectDir The project directory
* @param sfName the name of the security filter
* @param sTSUrl The URL of the Federation server
* @return the web.xml file
*/
public static File createWebXml(String projectDir,String sfName, String sTSUrl){
File webxml = new File(projectDir+System.getProperty("file.separator")+"WEB-INF"+System.getProperty("file.separator")+"web.xml");
String content = new StringBuilder(xmlLicenseHeader)
......@@ -105,7 +85,7 @@ public class Utilities {
.append("xmlns:web=\"http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\"").append(System.getProperty("line.separator"))
.append("xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee\"").append(System.getProperty("line.separator"))
.append("version=\"3.0\"> ").append(System.getProperty("line.separator"))
.append("\t <display-name>"+cdName+"</display-name>").append(System.getProperty("line.separator"))
.append("\t <display-name>"+sfName+"</display-name>").append(System.getProperty("line.separator"))
.append("\t <filter>").append(System.getProperty("line.separator"))
.append("\t\t <filter-name>SetInvocationAddressfilter</filter-name>").append(System.getProperty("line.separator"))
......@@ -125,16 +105,16 @@ public class Utilities {
.append("\t <filter-mapping>").append(System.getProperty("line.separator"))
.append("\t\t <filter-name>SetInvocationAddressfilter</filter-name>").append(System.getProperty("line.separator"))
.append("\t\t <url-pattern>/"+cdName+"</url-pattern>").append(System.getProperty("line.separator"))
.append("\t\t <url-pattern>/"+sfName+"</url-pattern>").append(System.getProperty("line.separator"))
.append("\t </filter-mapping>").append(System.getProperty("line.separator"))
.append("\t <filter-mapping>").append(System.getProperty("line.separator"))
.append("\t\t <filter-name>Securityfilter</filter-name>").append(System.getProperty("line.separator"))
.append("\t\t <url-pattern>/"+cdName+"</url-pattern>").append(System.getProperty("line.separator"))
.append("\t\t <url-pattern>/"+sfName+"</url-pattern>").append(System.getProperty("line.separator"))
.append("\t </filter-mapping>").append(System.getProperty("line.separator"))
.append("\t<servlet>").append(System.getProperty("line.separator"))
.append("\t\t <servlet-name>"+cdName+"</servlet-name>").append(System.getProperty("line.separator"))
.append("\t\t <display-name>"+cdName+"</display-name>").append(System.getProperty("line.separator"))
.append("\t\t <servlet-name>"+sfName+"</servlet-name>").append(System.getProperty("line.separator"))
.append("\t\t <display-name>"+sfName+"</display-name>").append(System.getProperty("line.separator"))
.append("\t\t <servlet-class>eu.chorevolution.SecurityServlet</servlet-class>").append(System.getProperty("line.separator"))
// .append("\t\t <init-param>").append(System.getProperty("line.separator"))
// .append("\t\t\t <param-name>targetUri</param-name>").append(System.getProperty("line.separator"))
......@@ -157,8 +137,8 @@ public class Utilities {
.append("\t </servlet>").append(System.getProperty("line.separator"))
.append("\t <servlet-mapping>").append(System.getProperty("line.separator"))
.append("\t\t <servlet-name>"+cdName+"</servlet-name>").append(System.getProperty("line.separator"))
.append("\t\t <url-pattern>/"+cdName+"</url-pattern>").append(System.getProperty("line.separator"))
.append("\t\t <servlet-name>"+sfName+"</servlet-name>").append(System.getProperty("line.separator"))
.append("\t\t <url-pattern>/"+sfName+"</url-pattern>").append(System.getProperty("line.separator"))
.append("\t </servlet-mapping>").append(System.getProperty("line.separator"))
.append("\t <servlet-mapping>").append(System.getProperty("line.separator"))
......@@ -172,146 +152,14 @@ public class Utilities {
} catch (IOException e) {
logger.info(e.getMessage());
}
return webxml;
}
public static void copyLibsFolder(String projectDir){
//File libs = new File(Utilities.class.getClassLoader().getResource("war-structure"+System.getProperty("file.separator")+"WEB-INF"+System.getProperty("file.separator")+"lib").getPath());
File libs = new File( "." + File.separatorChar + "src" + File.separatorChar + "main" + File.separatorChar + "resources" + File.separatorChar+"war-structure"+File.separatorChar+"WEB-INF"+File.separatorChar+"lib");
try {
//FileUtils.copyDirectory(libs, new File(projectDir+System.getProperty("file.separator")+"WEB-INF"+System.getProperty("file.separator")+"lib"));
FileUtils.copyDirectory(libs, new File(projectDir+File.separatorChar+"WEB-INF"+File.separatorChar+"lib"));
} catch (IOException e) {
logger.info(e.getMessage());
}
}
public static void copyClassesFolder(String projectDir){
//File libs = new File(Utilities.class.getClassLoader().getResource("war-structure"+System.getProperty("file.separator")+"WEB-INF"+System.getProperty("file.separator")+"lib").getPath());
File libs = new File( "." + File.separatorChar + "src" + File.separatorChar + "main" + File.separatorChar + "resources" + File.separatorChar+"war-structure"+File.separatorChar+"WEB-INF"+File.separatorChar+"classes");
try {
//FileUtils.copyDirectory(libs, new File(projectDir+System.getProperty("file.separator")+"WEB-INF"+System.getProperty("file.separator")+"lib"));
FileUtils.copyDirectory(libs, new File(projectDir+File.separatorChar+"WEB-INF"+File.separatorChar+"classes"));
} catch (IOException e) {
logger.info(e.getMessage());
}
}
public static void createWar(String cd_name, String projectDir){
//jarDir(projectDir, cd_name+".war");
System.out.println("projectDir " + projectDir);
FileOutputStream fout;
aborted = false;
writtenBytes = 0;
try {
List<File> fileList = new ArrayList<File>();
List<File> folderList = new ArrayList<File>();
getAllFilesAndFolders(new File(projectDir), fileList, folderList);
fout = new FileOutputStream(new File(projectDir).getParent()+System.getProperty("file.separator")+cd_name+".war");
JarOutputStream jarOut = new JarOutputStream(fout);
for (File folder : folderList){
System.out.println("create entry" + folder.getPath().replace(projectDir + System.getProperty("file.separator") , "")+ "/");
jarOut.putNextEntry(new JarEntry(folder.getPath().replace(projectDir + System.getProperty("file.separator") , "")+ "/"));
}
for (File file : fileList) {
System.out.println(file.getPath());
System.out.println(file.getPath().replace(projectDir+ System.getProperty("file.separator"), ""));
jarOut.putNextEntry(new JarEntry(file.getPath().replace(projectDir + System.getProperty("file.separator") , "")));
FileInputStream in = new FileInputStream(file);
byte[] buffer = new byte[1024];
while(true){
int nRead = in.read(buffer, 0, buffer.length);
if (nRead <= 0)
break;
jarOut.write(buffer, 0, nRead);
}
jarOut.closeEntry();
in.close();
}
jarOut.close();
//fout.close();
} catch (IOException e) {
logger.info(e.getMessage());
}
}
public static void getAllFilesAndFolders(File dir, List<File> fileList, List<File> folderList) {
System.out.println("dir : " + dir.getAbsolutePath());
File[] files = dir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
folderList.add(file);
getAllFilesAndFolders(file, fileList, folderList);
}
else{
fileList.add(file);
}
}
}
static public void jarDir(String dirName, String zipName)
{
try
{
//create a ZipOutputStream to zip the data to
JarOutputStream zos = new JarOutputStream(new FileOutputStream(zipName));
jarDir(dirName, zos);
zos.flush();
zos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void jarDir(String dir2zip, JarOutputStream zos)
{
try
{
System.out.println("dir2zip" + dir2zip);
String dir = new String(dir2zip.getBytes());
File zipDirectory = new File(dir2zip);
String[] dirList = zipDirectory.list();
byte[] readBuffer = new byte[4096];
int bytesIn = 0;
for (int i = 0; i < dirList.length; i++)
{
File f = new File(zipDirectory, dirList[i]);
System.out.println(f.getAbsolutePath());
if (f.isDirectory())
{
String filePath = f.getPath();
jarDir(filePath, zos);
continue;
}