Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ docs/node_modules/
docs/package-lock.json
docs/.hugo_build.lock
**.hugo_build.lock

certs
caldera_api*.json
61 changes: 61 additions & 0 deletions build_caldera_api.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash
# Before running:
# - go install github.com/go-swagger/go-swagger/cmd/swagger@latest

set -e
PROJ_PATH="$PWD"


CALDERA_VERSION="e4a4b9a320e707e58e69e3213ca07fdc561350ce" # Commit hash
OUTPUT_BASE_FOLDER="./internal/capability/caldera/api"

OUTPUT="$PROJ_PATH/caldera_api_raw.json"

# DOCKER_CMD="podman"
DOCKER_CMD="docker"

CALDERA_PORT="8888"
CALDERA_IMG_NAME="caldera:latest"
CALDERA_REPO="https://github.com/mitre/caldera"
TMP_WORKDIR="/tmp/caldera_api_build/"

if [ -d "$TMP_WORKDIR" ]; then
rm -rf $TMP_WORKDIR
fi

mkdir $TMP_WORKDIR
cd $TMP_WORKDIR

git clone $CALDERA_REPO caldera
cd caldera/
git checkout $CALDERA_VERSION
git submodule init
git submodule update --recursive

$DOCKER_CMD build . -t $CALDERA_IMG_NAME
CALDERA_CNTR_ID=$($DOCKER_CMD run -d -p $CALDERA_PORT:8888 $CALDERA_IMG_NAME)

# https://stackoverflow.com/questions/11904772/how-to-create-a-loop-in-bash-that-is-waiting-for-a-webserver-to-respond#21189440
# Interesting note: caldera doesn't like `HEAD`, and using that HTTP verb will cause curl to fail
until curl --output /dev/null --silent --fail http://127.0.0.1:$CALDERA_PORT/api/docs/swagger.json; do
printf '.'
sleep 5
done

printf 'CALDERA seems to be up!'

curl http://127.0.0.1:$CALDERA_PORT/api/docs/swagger.json --output $OUTPUT

$DOCKER_CMD stop -t0 $CALDERA_CNTR_ID
$DOCKER_CMD container rm -f $CALDERA_CNTR_ID
$DOCKER_CMD image rm -f $CALDERA_IMG_NAME

cd $PROJ_PATH

rm -rf $TMP_WORKDIR

python3 caldera_swagger_cleanup.py

go run github.com/go-swagger/go-swagger/cmd/swagger@latest generate client -f caldera_api.json -t $OUTPUT_BASE_FOLDER

printf 'Done!'
43 changes: 43 additions & 0 deletions caldera_swagger_cleanup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import json

with open('caldera_api_raw.json', 'r') as fd:
data = json.load(fd)


for p in data['paths'].values():
for m in p.values():
# Ensure everything has at least one possible response
if not m['responses']:
m['responses'] = {'200': {'description': "OK"}}

if m['parameters']:
for par in m['parameters']:
if 'schema' in par and '$ref' not in par['schema']:
for k, v in par['schema'].items():
par[k] = v
del par['schema']

if 'operation_id' in par:
del par['operation_id']

if 'access' in par:
del par['access']

if 'output' in par:
del par['output']

if 'type' in par and par['type'] == 'file' and 'consumes' not in m:
m['consumes'] = ["multipart/form-data"]

if 'required' not in par:
par['required'] = True

for d in data['definitions'].values():
for p in d['properties'].values():
if 'format' in p and not p['format']:
del p['format']
if 'pattern' in p and not p['pattern']:
del p['pattern']

with open('caldera_api.json', 'w') as fd:
json.dump(data, fd)
Loading