Skip to content
Merged
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
31 changes: 15 additions & 16 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
FROM python:3.14.4-alpine3.23

FROM python:alpine3.20
WORKDIR /app
ENTRYPOINT ["/app/entrypoint.sh"]


WORKDIR /usr/src/app
ENTRYPOINT ["sh"]

ENV PLANTUML_VER 1.2021.7
ENV PLANTUML_PATH /usr/local/lib/plantuml.jar
ENV PANDOC_VER 2.14.0.1
ENV PLANTUML_VER=1.2026.2
ENV PLANTUML_PATH=/usr/local/lib/plantuml.jar
ENV PANDOC_VER=2.19.2

RUN apk add --no-cache graphviz openjdk11-jre fontconfig make curl ttf-liberation ttf-linux-libertine ttf-dejavu \
&& apk add --no-cache --virtual .build-deps gcc musl-dev \
&& rm -rf /var/cache/apk/* \
&& curl -LO https://master.dl.sourceforge.net/project/plantuml/$PLANTUML_VER/plantuml.$PLANTUML_VER.jar \
&& mv plantuml.$PLANTUML_VER.jar $PLANTUML_PATH \
&& curl -LO https://github.com/plantuml/plantuml/releases/download/v$PLANTUML_VER/plantuml-mit-$PLANTUML_VER.jar \
&& mv plantuml-mit-$PLANTUML_VER.jar $PLANTUML_PATH \
&& curl -LO https://github.com/jgm/pandoc/releases/download/$PANDOC_VER/pandoc-$PANDOC_VER-linux-amd64.tar.gz \
&& tar xvzf pandoc-$PANDOC_VER-linux-amd64.tar.gz --strip-components 1 -C /usr/local/

ENV _JAVA_OPTIONS -Duser.home=/tmp -Dawt.useSystemAAFontSettings=gasp
ENV _JAVA_OPTIONS=-Duser.home=/tmp -Dawt.useSystemAAFontSettings=gasp
RUN printf '@startuml\n@enduml' | java -Djava.awt.headless=true -jar $PLANTUML_PATH -tpng -pipe >/dev/null

COPY requirements.txt requirements-dev.txt ./
RUN pip install --no-cache-dir -r requirements-dev.txt \
&& apk del .build-deps

COPY pyproject.toml ./
COPY pytm ./pytm
COPY docs ./docs
COPY *.py Makefile ./
COPY *.py Makefile entrypoint.sh ./

RUN pip install poetry \
&& poetry config virtualenvs.create false \
&& poetry install
20 changes: 7 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
MKFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
CWD := $(patsubst %/,%,$(dir $(MKFILE_PATH)))
DOCKER_IMG := pytm

ifeq ($(USE_DOCKER),true)
SHELL=docker
.SHELLFLAGS=run -u $$(id -u) -v $(CWD):/usr/src/app --rm $(DOCKER_IMG):latest -c
endif
ifndef PLANTUML_PATH
export PLANTUML_PATH = ./plantuml.jar
endif
Expand All @@ -24,7 +18,7 @@ endif


docs/pytm/index.html: $(wildcard pytm/*.py)
PYTHONPATH=. pdoc --html --force --output-dir docs pytm
poetry run pdoc --html --force --output-dir docs pytm

docs/threats.md: $(wildcard pytm/threatlib/*.json)
printf "# Threat database\n" > $@
Expand All @@ -38,13 +32,13 @@ $(MODEL): safe_filename
$(MAKE) MODEL=$(MODEL) report

$(MODEL)/dfd.png: $(MODEL).py $(libs)
./$< --dfd | dot -Tpng -o $@
poetry run python $< --dfd | dot -Tpng -o $@

$(MODEL)/seq.png: $(MODEL).py $(libs)
./$< --seq | java -Djava.awt.headless=true -jar $$PLANTUML_PATH -tpng -pipe > $@
poetry run python $< --seq | java -Djava.awt.headless=true -jar $$PLANTUML_PATH -tpng -pipe > $@

$(MODEL)/report.html: $(MODEL).py $(libs) docs/basic_template.md docs/Stylesheet.css
./$< --report docs/basic_template.md | pandoc -f markdown -t html > $@
poetry run python $< --report docs/basic_template.md | pandoc -f markdown-tex_math_dollars -t html > $@

dfd: $(MODEL)/dfd.png

Expand All @@ -54,11 +48,11 @@ report: $(MODEL)/report.html seq dfd

.PHONY: test
test:
@python3 -m unittest
poetry run pytest

.PHONY: describe
describe:
./tm.py --describe "TM Element Boundary ExternalEntity Actor Lambda Server Process SetOfProcesses Datastore Dataflow"
poetry run python tm.py --describe "TM Element Boundary ExternalEntity Actor Lambda Server Process SetOfProcesses Datastore Dataflow"

.PHONY: image
image:
Expand All @@ -69,4 +63,4 @@ docs: docs/pytm/index.html docs/threats.md

.PHONY: fmt
fmt:
black $(wildcard pytm/*.py) $(wildcard tests/*.py) $(wildcard *.py)
poetry run black $(wildcard pytm/*.py) $(wildcard tests/*.py) $(wildcard *.py)
53 changes: 53 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/sh
set -e

TARGET="${1:-all}"
MODEL_FILE="${2:-tm.py}"
MODEL=$(basename "${MODEL_FILE}" .py)
WORK_DIR=/work
OUTPUT_DIR="${WORK_DIR}/${MODEL}"

if [ ! -f "${WORK_DIR}/${MODEL_FILE}" ]; then
echo "Error: ${MODEL_FILE} not found in mounted directory"
echo "Usage: docker run --rm -v \$(pwd):/work pytm [dfd|seq|report|all] [model.py]"
exit 1
fi

mkdir -p "${OUTPUT_DIR}"

run_dfd() {
echo "Generating DFD..."
python "${WORK_DIR}/${MODEL}.py" --dfd | dot -Tpng -o "${OUTPUT_DIR}/dfd.png"
}

run_seq() {
echo "Generating sequence diagram..."
python "${WORK_DIR}/${MODEL}.py" --seq \
| java -Djava.awt.headless=true -jar "${PLANTUML_PATH}" -tpng -pipe \
> "${OUTPUT_DIR}/seq.png"
}

run_report() {
echo "Generating report..."
python "${WORK_DIR}/${MODEL}.py" --report /app/docs/basic_template.md \
| pandoc -f markdown-tex_math_dollars -t html \
> "${OUTPUT_DIR}/report.html"
}

case "${TARGET}" in
dfd) run_dfd ;;
seq) run_seq ;;
report) run_report ;;
all)
run_dfd
run_seq
run_report
;;
*)
echo "Unknown target: ${TARGET}"
echo "Usage: docker run --rm -v \$(pwd):/work pytm [dfd|seq|report|all] [model.py]"
exit 1
;;
esac

echo "Output written to ${MODEL}/"
Loading