OWASP O2 Platform Blog

Creating an API to be consumed by an O2 Script

After creating a Lamba method and putting it on a CSharp file the usual next step is to create an API that can wrap the functionality that we want.

In this case, I’m creating an API for the OWASP version of  TeamMentor  (which you can get from Git-Hub) so here are the two steps I took to create the API

Step 1: Create a CSharp file called API_TeamMentor.cs and located in C:\O2\_New_O2_Scripts\TeamMentor\API_TeamMentor.cs

// This file is part of the OWASP O2 Platform (<a href="http://www.owasp.org/index.php/OWASP_O2_Platform">http://www.owasp.org/index.php/OWASP_O2_Platform</a>) and is released under the Apache 2.0 License (<a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>)
using System;
using System.Text;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;using O2.Extra_O2_Scripts;
//O2File:C:\O2\_New_O2_Scripts\TeamMentor\_Extra_O2_Scripts.cs
namespace O2.XRules.Database.APIs
{
    public class API_TeamMentor
    {      
  public string WorkFolder {get;set;}
  public string DataFolder {get;set;}
  
  public API_TeamMentor()
  {
   WorkFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
   DataFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
  }
  
  public API_TeamMentor createAssembliesFromXmlFiles()
  {
   //create the dlls that we need to consume these xml file
   DataFolder.pathCombine("OWASP.xml").createAssemblyFromXml(WorkFolder, "tm_Main");
   DataFolder.pathCombine(@"OWASP\index.xml").createAssemblyFromXml(WorkFolder, "tm_Index");
   DataFolder.pathCombine(@"OWASP\Attack\2b2a09fd-a10d-479f-a3df-8e28870319b6.xml").createAssemblyFromXml(WorkFolder, "tm_Article");   
   return this;
  }
  
    }
}

Step 2: Consume API file from an O2 Script:


var teamMentor = new API_TeamMentor();
teamMentor.createAssembliesFromXmlFiles();

return teamMentor;

return "ok";
//using O2.XRules.Database.APIs
//O2File:C:\O2\_New_O2_Scripts\TeamMentor\API_TeamMentor.cs

Note: the execution of the above code is going to create 3 *.XSD, 3 *.CS and 3 *.DLL files in the C:\O2\_New_O2_Scripts\TeamMentor folder:

February 20, 2011 Posted by | O2 Internals | Leave a comment

Consuming extension method from external file

Once we have a LAMDA method that we want to consume in a generic way (i.e. from other scripts), the best place to put it is in a external *.cs file (which can be passed as a reference to any O2 Script)

For example, this is how to do this for the script developed here: https://o2platform.wordpress.com/2011/02/20/o2-script-create-xsd-and-assembly-from-xml-file/

step 1: Create a stand alone CSharp file (in this case called _Extra_O2_Scripts.cs and located in C:\O2\_New_O2_Scripts\TeamMentor\_Extra_O2_Scripts.cs)

// This file is part of the OWASP O2 Platform (<a href="http://www.owasp.org/index.php/OWASP_O2_Platform">http://www.owasp.org/index.php/OWASP_O2_Platform</a>) and is released under the Apache 2.0 License (<a href="http://www.apache.org/licenses/LICENSE-2.0">http://www.apache.org/licenses/LICENSE-2.0</a>)
using System;
using O2.Kernel;
using O2.Kernel.ExtensionMethods;
using O2.DotNetWrappers.ExtensionMethods;
using O2.External.SharpDevelop.ExtensionMethods;
using NUnit.Framework;
//O2Ref:nunit.framework.dll

namespace O2.Extra_O2_Scripts
{
    public static class XmlToAssembly_ExtensionMethods
    {   
     public static string createAssemblyFromXml(this string xmlFile, string targetFolder, string xsdAndDll_Name )
     {
   var xsd = targetFolder.pathCombine(xsdAndDll_Name + ".xsd");
   xmlFile.xmlCreateXSD().saveAs(xsd);
   Assert.That(xsd.fileExists(), "xsd was not created");
   "[createAssemblyFromXml] XSD Created: {0}".debug(xsd);
   var cs = xsd.xsdCreateCSharpFile()
      .fileInsertAt(0,"//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line());;
   
   Assert.That(cs.fileExists(), "cs was not created");  
   "[createAssemblyFromXml] CSharp file Created: {0}".debug(cs);
   
   var dll = targetFolder.pathCombine(xsdAndDll_Name + ".dll");
   var tempDll = cs.compile(dll);
   
   Assert.That(dll.fileExists(), "dll_MainFile was not created");
   "[createAssemblyFromXml] dll file Created: {0}".debug(dll);
   
   var assembly = dll.assembly();
   Assert.That(assembly.notNull(), "assembly was null");  
   return dll;        
  }
    }
}

step 2: Consume it from an O2 Script:

var workFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";

var mainFile = rootFolder.pathCombine("OWASP.xml");

return mainFile.createAssemblyFromXml(workFolder, "tm_Main");

return "ok";
 
//using O2.Extra_O2_Scripts
//O2File:C:\O2\_New_O2_Scripts\TeamMentor\_Extra_O2_Scripts.cs

February 20, 2011 Posted by | O2 Internals | 1 Comment

O2 Script: Create XSD and Assembly from XML file

This script sequence shows how I wrote a function that autocreates an XSD and DLL from an original XSD using O2’s ‘Quick Development GUI’.

The original file used was taken from the TeamMentor OWASP editon (which you can get from Git-Hub)

step 1: define folders and check if file exists

var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
return mainFile.fileExists();

step 2: create XSD file from XML file

panel.clear();
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd =  mainFile.xmlCreateXSD();
return xsd;

step 3: save XSD file in target folder

panel.clear();
var targetFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd_MainFile = targetFolder.pathCombine("TM_MainFile.xsd");
return mainFile.xmlCreateXSD().saveAs(xsd_MainFile);

step 4: create CSharp file from XSD, added NUnit references so that we can use Asserts to make sure all it happening as expected

panel.clear();
var targetFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd_MainFile = targetFolder.pathCombine("TM_MainFile.xsd");
mainFile.xmlCreateXSD().saveAs(xsd_MainFile);
Assert.That(xsd_MainFile.fileExists(), "xsd_MainFile was not created");

return xsd_MainFile.xsdCreateCSharpFile();
return "ok";
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

step 5: view created CSharp file in a Source Code Editor (using the ‘panel’ reference that is given to us from the ‘Quick Development GUI’)

panel.clear();
var targetFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd_MainFile = targetFolder.pathCombine("TM_MainFile.xsd");
mainFile.xmlCreateXSD().saveAs(xsd_MainFile);
Assert.That(xsd_MainFile.fileExists(), "xsd_MainFile was not created");

var cs_MainFile = xsd_MainFile.xsdCreateCSharpFile();
panel.add_SourceCodeEditor().open(cs_MainFile);
return "ok";
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

step 6: inserting dynamic compilation reference “//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll” inside the cs_MainFile (so that it can be compiled)

panel.clear();
var targetFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd_MainFile = targetFolder.pathCombine("TM_MainFile.xsd");
mainFile.xmlCreateXSD().saveAs(xsd_MainFile);
Assert.That(xsd_MainFile.fileExists(), "xsd_MainFile was not created");

var cs_MainFile = xsd_MainFile.xsdCreateCSharpFile()
       .fileInsertAt(0,"//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line());;
panel.add_SourceCodeEditor().open(cs_MainFile);
return "ok";
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

step 7: Compile CSharp file into an .NET Dll

panel.clear();
var targetFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";
var mainFile = rootFolder.pathCombine("OWASP.xml");
var xsd_MainFile = targetFolder.pathCombine("TM_MainFile.xsd");
mainFile.xmlCreateXSD().saveAs(xsd_MainFile);
Assert.That(xsd_MainFile.fileExists(), "xsd_MainFile was not created");

var cs_MainFile = xsd_MainFile.xsdCreateCSharpFile()
         .fileInsertAt(0,"//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line());;
panel.add_SourceCodeEditor().open(cs_MainFile);
return cs_MainFile.compile().Location;
return "ok";
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

step 8: create LAMDA function (createAssemblyFromXml) which is now generic and can be used for any XML file

panel.clear();Func<string, string, string,Assembly> createAssemblyFromXml =
 (xmlFile, targetFolder, xsdAndDll_Name )=>{

  var xsd = targetFolder.pathCombine(xsdAndDll_Name + ".xsd");
  xmlFile.xmlCreateXSD().saveAs(xsd);
  Assert.That(xsd.fileExists(), "xsd was not created");
  
  var cs = xsd.xsdCreateCSharpFile()
     .fileInsertAt(0,"//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line());;
  
  Assert.That(cs.fileExists(), "cs was not created");    
  var dll = targetFolder.pathCombine(xsdAndDll_Name + ".dll");
  cs.compile(dll);
  Assert.That(dll.fileExists(), "dll_MainFile was not created");
  var assembly = dll.assembly();
  Assert.That(assembly.notNull(), "assembly was null");
  return assembly;
 };

var workFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";

var mainFile = rootFolder.pathCombine("OWASP.xml");

return createAssemblyFromXml(mainFile, workFolder, "__TEMP__");

return "ok";
//using System.Reflection
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

step 9: Added a nunber of Asserts to createAssemblyFromXml Function

panel.clear();Func<string, string, string,string> createAssemblyFromXml =
 (xmlFile, targetFolder, xsdAndDll_Name )=>{

  var xsd = targetFolder.pathCombine(xsdAndDll_Name + ".xsd");
  xmlFile.xmlCreateXSD().saveAs(xsd);
  Assert.That(xsd.fileExists(), "xsd was not created");
  "[createAssemblyFromXml] XSD Created: {0}".debug(xsd);
  var cs = xsd.xsdCreateCSharpFile()
     .fileInsertAt(0,"//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line());;
  
  Assert.That(cs.fileExists(), "cs was not created");  
  "[createAssemblyFromXml] CSharp file Created: {0}".debug(cs);
  
  var dll = targetFolder.pathCombine(xsdAndDll_Name + ".dll");
  var tempDll = cs.compile(dll);
  
  Assert.That(dll.fileExists(), "dll_MainFile was not created");
  "[createAssemblyFromXml] dll file Created: {0}".debug(dll);
  
  var assembly = dll.assembly();
  Assert.That(assembly.notNull(), "assembly was null");  
  return dll;
 };

var workFolder = @"C:\O2\_New_O2_Scripts\TeamMentor";
var rootFolder = @"C:\SI\SecurityInnovation-OWASP-TeamMentor-Library-9f0a009";

var mainFile = rootFolder.pathCombine("OWASP.xml");

return createAssemblyFromXml(mainFile, workFolder, "__TEMP__");

return "ok";
//using System.Reflection
//using NUnit.Framework;
//O2Ref:nunit.framework.dll

February 20, 2011 Posted by | .NET | 2 Comments