Skip to content

Commit

Permalink
Merge pull request #42 from threatspec/missing_threatmodel_dir
Browse files Browse the repository at this point in the history
Creates threatmodel directory if missing on init and run
  • Loading branch information
zeroXten committed Dec 30, 2020
2 parents 11c91f8 + 078949a commit 0c03d60
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 13 deletions.
47 changes: 47 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Contributing

## Pull requests

## Testing

### Local install

To create a version of threatspec from the current branch, simple run

```
threatspec$ python setup.py install
```

### Unit testing

Unit tests are done using pytest. You can find the test code in the `tests` directory in this repository.

```
threatspec$ python setup.py test
```

### End to end testing with tox

To perform a full set of tests using BATs for each supported version of python, run the tox command:

```
threatspec$ tox
GLOB sdist-make: threatspec/setup.py
py36 inst-nodeps: threatspec/.tox/.tmp/package/1/threatspec-0.5.1.dev7+g11c91f8.zip
py36 installed: atomicwrites==1.3.0,attrs==19.1.0,Click==7.0,comment-parser==1.1.2,graphviz==0.12,importlib-metadata==0.19,Jinja2==2.10.1,jsonschema==3.0.2,MarkupSafe==1.1.1,more-itertools==7.2.0,numpy==1.17.0,packaging==19.1,pandas==0.25.1,pkg-resources==0.0.0,pluggy==0.12.0,py==1.8.0,pyparsing==2.4.2,pyrsistent==0.15.4,pytest==5.1.1,python-dateutil==2.8.0,python-magic==0.4.15,pytz==2019.2,PyYAML==5.1.2,six==1.12.0,threatspec==0.5.1.dev7+g11c91f8,wcwidth==0.1.7,zipp==0.5.2
py36 run-test-pre: PYTHONHASHSEED='3710368835'
py36 run-test: commands[0] | pytest
=============================================================== test session starts ===============================================================
platform linux -- Python 3.6.8, pytest-5.1.1, py-1.8.0, pluggy-0.12.0
...
```

CLI test files can be found in the `cli_tests` directory.

A quick CLI test using the local version can be done using the following command:

```
export TERM=linux; bats cli_tests
```

## Code of Conduct
10 changes: 9 additions & 1 deletion cli_tests/test_init.bats
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ teardown() {
assert_file_contains "threatspec.yaml" 'name: "threatspec project"'

assert_dir_exists "threatmodel"
}
}

@test "create missing threatmodel directory" {
run threatspec init
rmdir threatmodel

run threatspec init
assert_dir_exists "threatmodel"
}
2 changes: 1 addition & 1 deletion cli_tests/test_run_basic_source.bats
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ teardown() {
fi
}

@test "threat model json files created" {
@test "js threat model json files created" {
run threatspec run
assert_success

Expand Down
55 changes: 55 additions & 0 deletions cli_tests/test_run_missing_threatmodel_directory.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bats

load helpers/assert
load helpers/teardown

setup_once() {
cat <<'EOF' > source.js
// @mitigates Path:To:Component against A Threat with A Control
function a_line_of_code(with, parameters) {
this.is_fake()
}
EOF

cat <<'EOF' > threatspec.yaml
project:
name: clitest
description: CLI test
paths:
- path: "source.js"
mime: "text/x-javascript"
EOF

if [ -d "threatmodel" ]; then
rmdir threatmodel
fi
}

teardown_once() {
if [ -f "source.js" ]; then
rm source.js
fi
teardown_common
}

setup() {
if [ "$BATS_TEST_NUMBER" -eq 1 ]; then
setup_once
fi
}

teardown() {
if [ "$BATS_TEST_NUMBER" -eq ${#BATS_TEST_NAMES[@]} ]; then
teardown_once
fi
}

@test "creates threatmodel directory if missing" {
refute_file_exists "threatmodel"
run threatspec run
assert_success

assert_dir_exists "threatmodel"
}


2 changes: 1 addition & 1 deletion cli_tests/test_run_yaml_source.bats
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ teardown() {
fi
}

@test "threat model json files created" {
@test "yaml threat model json files created" {
run threatspec run
assert_success

Expand Down
25 changes: 15 additions & 10 deletions threatspec/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def load_component_library(self, path, local=False):
pass

def load_libraries(self):
self.create_directories()
self.load_threat_library(data.cwd(), local=True)
self.load_control_library(data.cwd(), local=True)
self.load_component_library(data.cwd(), local=True)
Expand Down Expand Up @@ -203,27 +204,31 @@ def load_local_config(self):
def init(self):
logger.info("Initialising threatspec...")

self.create_default_config()
self.load_local_config()
self.create_directories()

logger.info("""
Threatspec has been initialised. You can now configure the project in this
repository by editing the following file:
threatspec.yaml
""")

def create_default_config(self):
logger.debug("Creating default configuration file")
try:
data.copy_pkg_file(os.path.join("data", "default_config.yaml"), "threatspec.yaml")
except FileExistsError:
logger.error("Configuration file already exists, it looks like threatspec has already been initiated here.")
sys.exit(1)

self.load_local_config()
logger.warn("Configuration file already exists, it looks like threatspec has already been initiated here.")

def create_directories(self):
logger.debug("Creating directories")
try:
data.create_directories(["threatmodel"])
except IOError as e:
logger.error("Failed to create directories: {}".format(str(e)))
raise
logger.info("""
Threatspec has been initialised. You can now configure the project in this
repository by editing the following file:
threatspec.yaml
""")

def run(self):
logger.info("Running threatspec...")
Expand Down

0 comments on commit 0c03d60

Please sign in to comment.