Development scenarios

The scenarios below explain the use of the SPARTA tool support in various development scenarios. For each of the scenarios the relevant eclipse and standalone SPARTA dependencies are mentioned.

Shared scenarios

Caution

The usage of the maven repository is explained in Standalone Prerequisites section.

Caution

Using the SPARTA modeling classes in a standalone Java program requires some initialization steps. These are explained in the Using the standalone libraries section.

Creating DFD models

This scenario explains the programmatic creation and manipulation of DFD models.

Note

It requires either be.kuleuven.cs.distrinet.sparta.spartamodel or the standalone-dependencies.

All elements for a SPARTA model can be created using the SpartaModelFactory. The model is hierarchically structured. Elements that can contain other elements (e.g., the DFDModel, TrustBoundaryContainers) have a containedElements list to which the other elements have to be added.

DFD Model and Element Creation
1SpartaModelFactory fact = new SpartaModelFactoryImpl();
2DFDModel dfdmodel = fact.createDFDModel();
3dfdmodel.setName("DFD Model Name");
4List<ModelElement> dfdElements = dfdmodel.getContainedElements();
5
6Process proc = fact.createProcess();
7proc.setName("Process");
8dfdElements.add(proc);

Saving DFD Models

Note

It requires either be.kuleuven.cs.distrinet.sparta.spartamodel or the standalone-dependencies.

The SPARTA DFD models are saved as eclipse resources.

Caution

The eclipse resource concept to save files is different from the SpartaResource concept in SPARTA which represents any type of useful resource such as security and privacy solution catalogs and threat type catalogs thay you may want to load into the DFD model.

Saving DFD models
 1// make sure that the SpartaModelResourceFactory is registered to save sparta files
 2Resource.Factory.Registry reg = Resource.Factory.Registry.INSTANCE;
 3reg.getExtensionToFactoryMap().put("sparta", new SpartaModelResourceFactoryImpl());
 4
 5// Obtain a new resource set
 6ResourceSet resSet = new ResourceSetImpl();
 7
 8// create a resource
 9Resource resource = resSet.createResource(fileURI);
10
11// add the model
12resource.getContents().add(dfdmodel);
13// save the resource to disk (map with save options or null)
14resource.save(null);

Loading DFD Models

Note

It requires either be.kuleuven.cs.distrinet.sparta.spartamodel or the standalone-dependencies.

Loading DFD models
1ResourceSet resourceSet = new ResourceSetImpl();
2URI fileURI = URI.createFileURI(System.getProperty("user.dir") + "/" + "filename.sparta");
3Resource resource = resourceSet.getResource(fileURI, true);
4try {
5    resource.load(null);
6} catch (IOException e1) {
7    //
8}
9EcoreUtil.resolveAll(resource);

Pairing existing threat type catalogs

Ideally, the model already specifies the threat type catalog to use for the threat elicitation and analysis. However, it possible to inject a threat type catalog at runtime to enable the elicitation of the provided threats.

Loading threat type catalog: setting up the eclipse resources
1DFDModel dfd;
2
3URI fileURI = URI.createFileURI(System.getProperty("user.dir") + "/dfd.sparta");
4
5// Obtain a new resource set
6ResourceSet resSet = new ResourceSetImpl();
7
8// create a resource
9Resource resource = resSet.createResource(fileURI);
Loading threat type catalog: loading the existing threat specification catalog
1URI threatcat = URI.createFileURI(System.getProperty("user.dir") + "/" + "ThreatSpecification.sparta");
2Resource threatres = resSet.getResource(threatcat, true);
3threatres.load(null);
Loading threat type catalog: linking the catalog to the model and saving the model
1// for every threat specification catalog, add it to the DFDModel's resource list
2threatres.getContents().stream()
3    .filter(ThreatSpecificationCatalog.class::isInstance).map(ThreatSpecificationCatalog.class::cast)
4    .forEach(dfd.getResource()::add);
5
6resource.getContents().add(dfd);
7
8resource.save(null);

Note

Note the dfd.getResource() which gets the DFDModel’s resource list of SpartaResource elements. This is a different from the Eclipse Resources. The ThreatSpecificationCatalog is added to this list.

Eclipse-specific scenarios

Extending SPARTA

Extending the SPARTA meta-model to include new, custom concepts can be done as follows:

  1. Install the SPARTA plugins in Eclipse (Via Help > Install New Software…, using https://downloads.sparta.distrinet-research.be/updates/).

  2. Create a new Ecore Modeling Project (File > New > Other > Ecore Modeling Project).

  3. Add the SPARTA meta-model as a dependency for your project (using maven and/or in Eclipse via META-INF > MANIFEST.MF > Dependencies > Add… > be.kuleuven.cs.distrinet.sparta.spartamodel).

  4. In the .aird file, add the spartamodel.ecore and spartamodel.genmodel resources to the Models.

  5. Model your extension using the standard EMF workflow. For example, create a new class NoSQL_DataStore and add the existing DataStore element as a supertype.

Example extension to the sparta meta-model.
  1. Add the SPARTA meta-model as a reference in the new model by right-clicking the root of the the generator model (your_project > model > your_model.genmodel) and selecting Reload > Ecore model > Next > Next > Referenced generated models > Add… > be.kuleuven.distrinet.sparta.spartamodel .

  2. In the .genmodel tree editor, select the new extension’s model (not the top level element). In the properties, under Edit, set the Child Creation Extenders property to true. (Note: if the new model extension should also be extensible similar to the spartamodel, also set the Extensible Provider Factory property to true.)

The genmodel properties for the sparta meta-model extension.
  1. Generate the model by right-clicking the genmodel and selecting Generate All.

Now, when running the generated editor plugin as an Eclipse Application, a NoSQL_DataStore can be added to a DFD Model.

Example extension to the sparta meta-model.

Standalone scenarios

Using the standalone libraries

Note

This requires sparta-model.

Using the Eclipse EMF classes outside of eclipse requires some initialization before first use.

EMF Initialization
1import org.eclipse.emf.ecore.resource.Resource;
2import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl;
3import be.kuleuven.cs.distrinet.sparta.spartamodel.SpartaModelPackage;
4
5// Initialization
6SpartaModelPackage.eINSTANCE.eClass();
7
8// Register resource factory
9Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("sparta", new XMIResourceFactoryImpl());

Performing threat elicitation and risk analysis

Note

It requires either be.kuleuven.cs.distrinet.sparta.analysis or the sparta-core.

Caution

The engine does not have a fixed built-in catalog of threat types. The threat elicitation will only work when a catalog of threat types is provided to the engine as part of the analyzed model. See Pairing existing threat type catalogs for more information on how to pair an existing catalog of threat types to a model.

Using the previously loaded or saved resourceSet:

Standalone threat elicitation and risk analysis
// Create a new engine
Engine e = new Engine(resourceSet);
// Run the analysis
List<Threat> results = e.runAnalysis();
// use the results

Analyzing threat results

Note

This requires the sparta-analysis.

The sparta-analysis library contains a number of helper classes for analyzing the resulting list of threats.

List<Threat> results;
ThreatAggregationAnalysis taa = new ThreatAggregationAnalysis(results);
taa.getDFDElementTypeCount();
taa.getTotalRiskReductionPct();
taa.getAverageThreatCounterPerElement();
taa.getPotentialRiskPerThreatType();
taa.getPotentialRiskPerDFDModelElementType();
taa.getThreatCountPerThreatType();
taa.getThreatCountPerDFDModelElementType();
// etc.