-
Notifications
You must be signed in to change notification settings - Fork 5
/
Makefile
194 lines (167 loc) · 6.26 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#Python3 and virtual environment
VENV := $(shell mktemp -d /tmp/caramel-test.XXXXX)
PYTHON3 := $(VENV)/bin/python3
# PIDs of background processes saved at VENV/[PROGRAM]-[test].pid
SERVER := $(VENV)/server
AUTOSIGN := $(VENV)/autosign
# Database and CA cert and key for caramel server to use
DB_FILE := $(VENV)/caramel.sqlite
DB_URL := sqlite:///$(DB_FILE)
CA_CERT := $(VENV)/example_ca/caramel.ca.cert
CA_KEY := $(VENV)/example_ca/caramel.ca.key
# client.crt will be generated if the server correctly gives our stored CSR back
CLIENT_CERT := $(VENV)/client.crt
# If caramel_tool exists in the venv caramel has been installed
CARAMEL_TOOL := $(VENV)/bin/caramel_tool
# Terminal formatting
BOLD := printf "\033[1m"
PASS := $(BOLD); printf "\033[32m"
FAIL := $(BOLD); printf "\033[31m"
LINE := $(BOLD); echo "---------------------------------------"
RESET_TERM := printf "\033[0m"
BLR := $(BOLD); $(LINE); $(RESET_TERM) #Bold Line, Reset formatting
# Make sure we dont use any exsisting env vars
unexport CARAMEL_INI CARAMEL_CA_CERT CARAMEL_CA_KEY CARAMEL_DBURL CARAMEL_HOST CARAMEL_PORT CARAMEL_LOG_LEVEL
#Check for python3 install and virtual environment
$(PYTHON3):
@if [ -z python3 ]; then \
$(FAIL);\
echo "Python 3 could not be found.";\
$(RESET_TERM);\
exit 2; \
fi
@$(BOLD); echo "Create a new venv for testing at $(VENV)";\
$(BLR);
python3 -m venv $(VENV)
@$(BLR)
#Install the project via setup.py
.PHONY: venv-install
venv-install: $(CARAMEL_TOOL)
$(CARAMEL_TOOL): $(PYTHON3) setup.py
@$(BOLD); echo "Install caramel and its dependencies in venv: $(VENV)";\
$(BLR);
$(VENV)/bin/python3 -m pip install -e .
cp development.ini $(VENV)/development.ini
mkdir $(VENV)/example_ca
@$(BLR)
# Create a sqlite-db configured for use with caramel
.PHONY: gen-db%
gen-db%: $(CARAMEL_TOOL)
@$(BOLD); echo "Create a new DB at $(DB_FILE)";\
$(BLR);
$(VENV)/bin/caramel_initialize_db $(CARAMEL_COMMAND_LINE)
@$(BLR)
# Generate a new CA cert and key pair
.PHONY: ca-cert%
ca-cert%: $(CARAMEL_TOOL)
@$(BOLD); echo "Generate new CA cert and key with tests/ca_test_input.txt";\
$(BLR)
$(VENV)/bin/caramel_ca $(CARAMEL_COMMAND_LINE) < tests/ca_test_input.txt
@$(BLR)
# Start caramel using pserve in the background, save PID to SERVER
$(SERVER)-env.pid: ca-cert-env gen-db-env
@ $(BOLD); echo "Start new caramel server in the background, sleep 2s to \
give it time to start";\
$(BLR)
chmod +x scripts/caramel_launcher.sh
setsid ./scripts/caramel_launcher.sh $(VENV)/bin/pserve >/dev/null 2>&1 < /dev/null & \
echo $$! > $(SERVER)-env.pid
sleep 2s
@$(BLR)
# Start caramel using pserve in the background, save PID to SERVER
$(SERVER)-in%.pid: ca-cert-in% gen-db-in%
@ $(BOLD); echo "Start new caramel server in the background, sleep 2s to \
give it time to start";\
$(BLR)
setsid $(VENV)/bin/pserve $(CARAMEL_COMMAND_LINE) >/dev/null 2>&1 < /dev/null & \
echo $$! > $(SERVER)-in$*.pid
sleep 2s
@$(BLR)
# Start caramel_autosign in the background, save PID to ENV_AUTOSIGN
$(AUTOSIGN)%.pid: $(CARAMEL_TOOL) ca-cert% gen-db%
@$(BOLD);echo "Start new caramel_autosign in the background";\
$(BLR)
setsid $(VENV)/bin/caramel_autosign $(CARAMEL_COMMAND_LINE) >/dev/null 2>&1 < /dev/null &\
echo $$! > $(AUTOSIGN)$*.pid
@$(BLR)
# Try to upload a CSR to a caramel server and then confirm our CSR was stored
.PHONY: client-run%
client-run%: $(SERVER)%.pid $(AUTOSIGN)%.pid
@ $(BOLD); echo "Use client-example.sh to upload our CSR, wait for it to \
get processed, then call it again to confirm the server stored our CSR";\
$(BLR)
chmod +x scripts/client-example.sh
./scripts/client-example.sh $(VENV)
sleep 2s
./scripts/client-example.sh $(VENV)
@$(BLR)
# Basic tests that caramel can be installed and run with test data,
.PHONY: systest%
systest: systest-env systest-ini systest-ini-env systest-ini-commandline
@ $(PASS); $(LINE);\
echo "Systest passed"; \
$(BLR)
# using environment variables for config
systest-env: export CARAMEL_COMMAND_LINE =
systest-env: export CARAMEL_DBURL = $(DB_URL)
systest-env: export CARAMEL_CA_CERT = $(CA_CERT)
systest-env: export CARAMEL_CA_KEY = $(CA_KEY)
systest-env: export CARAMEL_HOST = 127.0.0.1
systest-env: export CARAMEL_PORT = 6543
systest-env: export CARAMEL_LOG_LEVEL = ERROR
# using only .ini-file for config on the command line
systest-ini: export CARAMEL_COMMAND_LINE = $(VENV)/development.ini
# using only .ini-file for config for server, rest ini from env
systest-ini-env: export CARAMEL_INI = $(VENV)/development.ini
$(SERVER)-ini-env.pid: export CARAMEL_COMMAND_LINE = $(VENV)/development.ini
$(AUTOSIGN)-db-ini-env.pid: export CARAMEL_COMMAND_LINE =
ca-cert-ini-env: export CARAMEL_COMMAND_LINE =
gen-db-ini-env: export CARAMEL_COMMAND_LINE =
# using only .ini-file for config for server, rest commandline
$(SERVER)-ini-commandline.pid: export CARAMEL_COMMAND_LINE = $(VENV)/development.ini
$(AUTOSIGN)-ini-commandline.pid: export CARAMEL_COMMAND_LINE = --dburl=$(DB_URL) --ca-cert="$(CA_CERT)" --ca-key="$(CA_KEY)"
ca-cert-ini-commandline: export CARAMEL_COMMAND_LINE = --ca-cert="$(CA_CERT)" --ca-key="$(CA_KEY)"
gen-db-ini-commandline: export CARAMEL_COMMAND_LINE = --dburl $(DB_URL)
systest-%: client-run-%
@kill $(shell cat $(SERVER)-$*.pid);\
if [ $$? -eq 0 ]; then \
$(PASS); $(LINE);\
echo "Caramel server started and terminated successfully";\
else \
$(FAIL); $(LINE);\
echo "$@ failed: Caramel server exited before termination with\
exit code: $$?";\
exit 1;\
fi;
@$(BLR)
@kill $(shell cat $(AUTOSIGN)-$*.pid);\
if [ $$? -eq 0 ]; then \
$(PASS); $(LINE);\
echo "Autosign server started and terminated successfully";\
else \
$(FAIL); $(LINE);\
echo "$@ failed: Autosign server exited before terminated with\
exit code: $$?";\
exit 1;\
fi;
@$(BLR)
@if [ -f $(CLIENT_CERT) ]; then \
$(PASS); $(LINE);\
echo "$@ passed: Caramel successfully registered our CSR";\
else \
$(FAIL); $(LINE);\
echo "$@ failed: Something went wrong when communicating with \
the server";\
exit 1;\
fi;\
$(BLR)
@ echo "Move test data after successfull run"
mkdir $(VENV)/$@
mv -t $(VENV)/$@ $(CLIENT_CERT) $(DB_FILE) $(CA_CERT) $(CA_KEY)
# Removes the virtual environment created via this makefile,
# NOTE: this will remove all previous virtual environments
.PHONY: clean
clean:
@echo "Removing local test virtual environment"; $(BLR)
rm -rf $(VENV)
@$(BLR)