Skip to content

Commit

Permalink
Merge pull request #1311 from TheHive-Project/utils-improvements
Browse files Browse the repository at this point in the history
utils improvements
  • Loading branch information
nusantara-self authored Jan 16, 2025
2 parents 1b31d4c + fbf3b7a commit 9dd348d
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 2 deletions.
74 changes: 74 additions & 0 deletions utils/docker/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env bash

###
# This program assumes your analyzers and responders folder looks like:
#
# Custom-Analyzers
# ├── analyzers/
# │ └── My_custom_analyzer/
# └── responders/
# └── My_custom_responder/
# ├── customresponderflavor.json
# ├── Dockerfile
# ├── program.py*
# ├── README.md
# └── requirements.txt
#
# Usage:
# Update DOCKER_REPOSITORY variable
# cd ./Custom-Analyzers
# bash /path/to/build.sh
###

# Set your docker repository name
DOCKER_REPOSITORY=ilovestrangebee

build_image() {
JSON=$1
cat << EOF > /tmp/default_dockerfile
FROM python:3
WORKDIR /worker
ARG workername
ARG command
COPY . \$workername
RUN test ! -e \$workername/requirements.txt || pip install --no-cache-dir -r \$workername/requirements.txt
ENTRYPOINT \$command
EOF

DEFAULT_DOCKERFILE=/tmp/default_dockerfile
TAG=`cat ${JSON} | jq -r '( "'"$DOCKER_REPOSITORY"'" + "/" + (.name | ascii_downcase) + ":" + (.version))'`
WORKER_NAME=`cat ${JSON} | jq -r '(.version)'`
COMMAND=`cat ${JSON} | jq -r '(.command)'`
DIRNAME=`dirname ${JSON}`
WORKER_NAME=`basename ${DIRNAME}`
if test -f ${DIRNAME}/Dockerfile
then
docker build -t ${TAG} `dirname ${JSON}`
else
docker build --build-arg workername=${WORKER_NAME} --build-arg command=${COMMAND} -f ${DEFAULT_DOCKERFILE} -t ${TAG} `dirname ${JSON}`
fi
}

build_catalog() {
DIR=$1
echo '[' > ${DIR}/${DIR}.json


first=1
for JSON in ${DIR}/*/*.json
do
build_image ${JSON}
if test -z "${first}"
then
echo ',' >> ${DIR}/${DIR}.json
else
first=
fi
jq 'del(.command) + { dockerImage: ("'"$DOCKER_REPOSITORY"'" + "/" + (.name | ascii_downcase) + ":" + (.version)) }' ${JSON} >> ${DIR}/${DIR}.json
done

echo ']' >> ${DIR}/${DIR}.json
}

build_catalog analyzers
build_catalog responders
40 changes: 39 additions & 1 deletion utils/flavors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ optional arguments:
JSON Schema of a flavor
```


## Examples


Expand Down Expand Up @@ -47,3 +46,42 @@ python3 utils/flavors/check_json_schema.py -f analyzers/SEKOIAIntelligenceCenter
If you want to start writing an Analyzer or a Responder, or a new flavor, you can start with the appropriate template:
- `analyzer_flavor_template.json` for an new flavor of an analyser
- `responder_flavor_template.json` for a new flavor of a responder

## Running with virtualenv

### Create a Virtual Environment

To ensure a clean and isolated environment for running the script:

```bash
python3 -m venv env
```

### Activate the virtual environment

- On Linux/MacOS:
```
source env/bin/activate
```
- On Windows:
```
.\env\Scripts\activate
```

### Install Dependencies

Install the required package
```bash
pip install -r requirements.txt
```

### Run the script
Execute the script explicitly using the Python interpreter from the virtual environment:
```bash
env/bin/python3 utils/flavors/check_json_schema.py -r
```

When done running, deactivate the virtual environment when you're done by running:
```bash
deactivate
```
5 changes: 4 additions & 1 deletion utils/flavors/flavor_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
"type": "object",
"properties": {
"service": {
"type": "string"
"type": [
"number",
"string"
]
}
}
},
Expand Down
1 change: 1 addition & 0 deletions utils/flavors/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jsonschema
26 changes: 26 additions & 0 deletions utils/test_doc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Cortex-Neurons local documentation preview

This script renders the Cortex-Neurons documentation locally.

## Usage

1. Run the script from root directory Cortex-Analyzers
```bash
bash utils/test_doc/testdoc-venv.sh
```

2. View the documentation at http://0.0.0.0:8889

## Script Overview
The script performs the following actions:

- Creates a test environment for documentation in a temporary folder.
- Copies necessary files and directories into the temporary folder.
- Sets up a Python virtual environment and installs dependencies from requirements.txt.
- Clones the doc-builder repository from GitHub if not already cloned.
- Runs the documentation generation script.
- Serves the generated documentation using MkDocs.
- Cleans up temporary files upon completion.

## Notes
Ensure the script is ran from the root directory of the Cortex-Analyzers repository.
70 changes: 70 additions & 0 deletions utils/test_doc/testdoc-venv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

# This program is for test purposes only, to ensure the documentation is generated as expected.
# This program should be run from the Cortex-Analyzers/ path only.

ROOT_PATH="${PWD}"
TEST_PATH="./test_doc"
VENV_PATH="${ROOT_PATH}/venv"
REQUIREMENTS_FILE="${ROOT_PATH}/utils/test_doc/requirements.txt"

# Cleanup function
cleanup() {
echo "Cleaning up temporary files..."
deactivate 2>/dev/null
cd "${ROOT_PATH}" || exit
rm -rf "${TEST_PATH}"
}
trap 'cleanup' EXIT

# Create the test documentation path
mkdir -p "${TEST_PATH}"

# Copy necessary directories and files
for I in analyzers responders assets images AUTHORS docs *.md; do
cp -rv "$I" "${TEST_PATH}"
done

cd "${TEST_PATH}" || exit

# Create a Python virtual environment if not already created
if [ ! -d "$VENV_PATH" ]; then
echo "Creating virtual environment..."
python3 -m venv "$VENV_PATH"
fi

# Activate the virtual environment
source "${VENV_PATH}/bin/activate"

# Ensure pip is updated
pip install --upgrade pip

# Check and install dependencies from requirements.txt
if [ -f "$REQUIREMENTS_FILE" ]; then
echo "Installing dependencies from $REQUIREMENTS_FILE..."
pip install -r "$REQUIREMENTS_FILE"
else
echo "Error: $REQUIREMENTS_FILE not found!"
deactivate
exit 1
fi

# Clone the repository if not already cloned
if [ ! -d "doc-builder" ]; then
gh repo clone TheHive-Project/doc-builder
fi

# Execute the generate script
python doc-builder/build/Cortex-Neurons/generate.py

# Copy specific files to the docs folder
cp -v CHANGELOG.md docs/.
cp -v code_of_conduct.md docs/.
cp -v README.md docs/
cp -v SECURITY.md docs/
cp -v AUTHORS docs/AUTHORS.md

# Serve documentation with mkdocs
mkdocs serve -a 0.0.0.0:8889

echo "Script execution completed!"

0 comments on commit 9dd348d

Please sign in to comment.