Skip to content

Commit 677e56a

Browse files
committed
feat: fix tests, move to controlplane/netassert image
1 parent fdda42a commit 677e56a

File tree

7 files changed

+142
-18
lines changed

7 files changed

+142
-18
lines changed

Dockerfile

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ WORKDIR /code
44
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
55
CMD ["npm", "test"]
66

7+
ENV GOSU_VERSION="1.10"
8+
79
RUN \
810
bash -euxo pipefail -c "curl -sL https://deb.nodesource.com/setup_9.x | bash -x" \
911
&& DEBIAN_FRONTEND=noninteractive \
@@ -18,11 +20,31 @@ RUN \
1820
ssh \
1921
wget \
2022
\
21-
&& rm -rf /var/lib/apt/lists/*
23+
&& rm -rf /var/lib/apt/lists/* \
24+
\
25+
&& ARCH="$(dpkg --print-architecture | awk -F- '{ print $NF }')" \
26+
\
27+
&& wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/${GOSU_VERSION}/gosu-${ARCH}" \
28+
&& chmod +x /usr/local/bin/gosu \
29+
&& gosu nobody true
30+
31+
RUN \
32+
adduser \
33+
--shell /bin/bash \
34+
--uid 30000 \
35+
--gecos "" \
36+
--disabled-password \
37+
netassert \
38+
&& \
39+
CACHE_DIR=/code/node_modules/.cache \
40+
&& mkdir -p "${CACHE_DIR}" \
41+
&& chown netassert -R "${CACHE_DIR}"
2242

2343
COPY package.json /code/
2444
RUN npm install
2545

46+
COPY node_modules/node-nmap/ /code/node_modules/node-nmap/
47+
2648
# TODO(ajm) netassert doesn't run in the container yet
2749
COPY test/ /code/test/
2850
COPY entrypoint.sh yj netassert /usr/local/bin/

Jenkinsfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ pipeline {
77
GIT_CREDENTIALS = "ssh-key-jenkins-bot"
88
}
99

10+
// stages is "all pipeline stages"
1011
stages {
12+
// the name of this stage, represented in the stage view e.g. https://jenkins.ctlplane.io/job/netassert/
1113
stage('Build') {
14+
// defines the "agent" aka "jenkins slave"
1215
agent {
1316
docker {
17+
// always run in this image, it's got latest kubectl and is based from a google-managed image
1418
image 'docker.io/controlplane/gcloud-sdk:latest'
1519
args '-v /var/run/docker.sock:/var/run/docker.sock ' +
1620
'--user=root ' +
@@ -19,6 +23,7 @@ pipeline {
1923
}
2024
}
2125

26+
// here is the actual build for this stage
2227
steps {
2328
ansiColor('xterm') {
2429
sh 'make build CONTAINER_TAG="${CONTAINER_TAG}"'

Makefile

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,20 @@ endif
1616

1717
CONTAINER_TAG ?= $(GIT_TAG)
1818
CONTAINER_NAME := $(REGISTRY)/$(NAME):$(CONTAINER_TAG)
19+
TEST_CONTAINER_TAG := "testing"
20+
CONTAINER_NAME_TESTING := $(REGISTRY)/$(NAME):$(TEST_CONTAINER_TAG)
1921

2022
TEST_FILE := "test/test-localhost-remote.yaml"
2123

2224
export NAME REGISTRY BUILD_DATE GIT_MESSAGE GIT_SHA GIT_TAG CONTAINER_TAG CONTAINER_NAME
2325

26+
.PHONY: all test
27+
.SILENT:
28+
29+
all: help
30+
2431
.PHONY: cluster
25-
cluster: ## builds a test cluster
32+
cluster: ## creates a test GKE cluster
2633
@echo "+ $@"
2734
gcloud container clusters create \
2835
--zone europe-west2-a \
@@ -32,7 +39,7 @@ cluster: ## builds a test cluster
3239
--num-nodes 1 \
3340
--preemptible \
3441
--enable-network-policy \
35-
netassert
42+
netassert-test
3643

3744
.PHONY: build
3845
build: ## builds a docker image
@@ -52,9 +59,15 @@ push: ## pushes a docker image
5259
.PHONY: run-in-docker
5360
run-in-docker: ## runs the last build docker image inside docker
5461
@echo "+ $@"
55-
docker run -i \
62+
set -x ; docker run -i \
5663
--net=host \
64+
--cap-add NET_ADMIN \
65+
--cap-add NET_RAW \
5766
${DOCKER_ARGS} \
67+
-v ~/.config/gcloud/:/root/.config/gcloud/ \
68+
-v ~/.ssh/:/tmp/.ssh/:ro \
69+
-v ~/.kube/:/root/.kube:ro \
70+
-v $(shell readlink -f ~/.ssh/config):/tmp/ssh-config:ro \
5871
-v /var/run/docker.sock:/var/run/docker.sock:ro \
5972
"${CONTAINER_NAME}" ${ARGS}
6073

@@ -71,16 +84,54 @@ rollcage-test: ## build, test, and push container, then run local tests
7184
make rollcage && ./netassert test/test-all.yaml
7285

7386
.PHONY: test
74-
test: ## build, test, and push container, then run local tests
87+
test: test-deploy ## build, test, and push container, then run local tests
7588
@echo "+ $@"
76-
make rollcage && ./netassert test/test-all.yaml
89+
make build push CONTAINER_TAG="$(TEST_CONTAINER_TAG)" \
90+
&& ./netassert \
91+
--image ${CONTAINER_NAME_TESTING} \
92+
test/test-all.yaml \
93+
&& make run-in-docker \
94+
CONTAINER_NAME=$(CONTAINER_NAME_TESTING) \
95+
ARGS='netassert --image ${CONTAINER_NAME_TESTING} test/test-all.yaml'
96+
97+
.PHONY: test-local
98+
test-local: test-deploy ## test from the local machine
99+
@echo "+ $@"
100+
./netassert \
101+
--image ${CONTAINER_NAME_TESTING} \
102+
test/test-all.yaml
103+
104+
.PHONY: test-deploy
105+
test-deploy: ## deploy test services
106+
@echo "+ $@"
107+
set -x; for DEPLOYMENT_TYPE in \
108+
frontend \
109+
microservice \
110+
database \
111+
; do \
112+
\
113+
DEPLOYMENT="test-$${DEPLOYMENT_TYPE}"; \
114+
kubectl run "$${DEPLOYMENT}" \
115+
--image=busybox \
116+
--labels=app=web,role="$${DEPLOYMENT_TYPE}" \
117+
--requests='cpu=10m,memory=32Mi' \
118+
--expose \
119+
--port 80 \
120+
-- sh -c "while true; do { printf 'HTTP/1.1 200 OK\r\n\n I am a $${DEPLOYMENT_TYPE}\n'; } | nc -l -p 80; done"; \
121+
\
122+
kubectl scale deployment "$${DEPLOYMENT}" --replicas=3; \
123+
done; \
124+
\
125+
kubectl apply -f resource/net-pol/web-deny-all.yaml -f resource/net-pol/test-services-allow.yaml;
126+
77127

78128
.PHONY: rollcage
79129
rollcage: ## build, test, and push the container
80130
@echo "+ $@"
81131
rollcage build run push \
82132
--interactive false \
83-
--tag sublimino/scratch:dev --pull=false "npm test" \
133+
--tag controlplane/netassert:none \
134+
--pull=false "npm test" \
84135
-- \
85136
--net=host \
86137
--env DEBUG="" \
@@ -91,7 +142,8 @@ rollcage-docker: ## experimental, does not currently work with gcloud
91142
@echo "+ $@"
92143
rollcage build run push \
93144
--interactive false \
94-
--tag sublimino/scratch:dev --pull=false "npm test" \
145+
--tag controlplane/netassert:none \
146+
--pull=false "npm test" \
95147
-- \
96148
--net=host \
97149
--env DEBUG=1 \
@@ -125,7 +177,6 @@ $${ACTION}: \#\# help\n\
125177
.PHONY: help
126178
help: ## parse jobs and descriptions from this Makefile
127179
@grep -E '^[ a-zA-Z0-9_-]+:([^=]|$$)' $(MAKEFILE_LIST) \
128-
| grep -Ev '^help\b[[:space:]]*:' \
180+
| grep -Ev '^(all|help)\b[[:space:]]*:' \
129181
| sort \
130182
| awk 'BEGIN {FS = ":.*?##"}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
131-

entrypoint.sh

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,31 @@
11
#!/bin/bash
22

33
set -euo pipefail
4+
DEBUG=1
45

56
if [[ "${DEBUG:-}" != "" ]]; then
67
set -x
78
fi
89

10+
if [[ -e /var/run/docker.sock ]]; then
11+
groupadd docker
12+
13+
# get gid of docker socket file
14+
DOCKER_SOCK_GID=$(ls -ng /var/run/docker.sock | cut -f3 -d' ')
15+
16+
# get group of docker inside container
17+
DOCKER_GID=$(getent group docker | cut -f3 -d: || true)
18+
19+
# if they don't match, adjust
20+
if [[ ! -z "${DOCKER_SOCK_GID}" && "${DOCKER_SOCK_GID}" != "${DOCKER_GID}" ]]; then
21+
groupmod -g "${DOCKER_SOCK_GID}" docker
22+
fi
23+
24+
if ! groups netassert | grep -q docker; then
25+
usermod -aG docker netassert
26+
fi
27+
fi
28+
929
if [[ "${TEST_YAML:-}" != "" ]]; then
1030
echo "${TEST_YAML}" | base64 -d >/code/test/test.yaml
1131
fi
@@ -18,12 +38,38 @@ fi
1838
if [[ "${DEBUG:-}" != "" ]]; then
1939
pwd
2040
id
21-
ls -lasp /root/ /root/.ssh/ || true
41+
ls -lasp \
42+
/home/netassert/ \
43+
/home/netassert/.ssh/ || true
2244
echo "/code/test/test.yaml:"
2345
cat /code/test/test.yaml
2446
fi
2547

48+
2649
[[ -d ${HOME}/.parallel ]] || mkdir -p ${HOME}/.parallel || true
2750
[[ -f ${HOME}/.parallel/will-cite ]] || touch ~/.parallel/will-cite
2851

52+
gosu netassert bash -c "$(cat << EOF
53+
[[ -d \${HOME}/.parallel ]] || mkdir -p \${HOME}/.parallel || true
54+
[[ -f \${HOME}/.parallel/will-cite ]] || touch ~/.parallel/will-cite
55+
EOF
56+
)"
57+
58+
if [[ -d /tmp/.ssh ]]; then
59+
cp -a /tmp/.ssh /home/netassert/
60+
fi
61+
if [[ -L /home/netassert/.ssh/config ]]; then
62+
rm -f /home/netassert/.ssh/config
63+
fi
64+
if [[ -d /tmp/ssh-config ]]; then
65+
cp -af /tmp/ssh-config /home/netassert/.ssh/config
66+
fi
67+
68+
chown netassert -R /home/netassert
69+
70+
# TODO(AJM) run without root
71+
#exec gosu netassert "${@}"
72+
pwd
73+
ls -lasp
74+
2975
exec "${@}"

netassert

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ declare -a ARGUMENTS
3838
EXPECTED_NUM_ARGUMENTS=0
3939
ARGUMENTS=()
4040
FILENAME="test/test.yaml"
41-
NETASSERT_IMAGE_NAME="sublimino/scratch:dev"
41+
NETASSERT_IMAGE_NAME="controlplane/netassert:none"
4242
CONTAINER_BASE_NAME="netassert"
4343
CONTAINER_NAME="${CONTAINER_BASE_NAME}-$(cut -d- -f1 </proc/sys/kernel/random/uuid)"
4444
CONFIG=''
@@ -71,7 +71,6 @@ main() {
7171
iterate_k8s
7272

7373
iterate_host
74-
7574
}
7675

7776
count_expected_tests() {
@@ -187,7 +186,7 @@ configure_parallel() {
187186
mkdir -p "${TMPDIR}" &>/dev/null || true
188187

189188
(parallel --record-env)
190-
export -f _iterate_k8s_worker _iterate_host_worker jqc success info debug error warning ssh_to_node log_message_prefix is_gke to_yaml
189+
export -f _iterate_k8s_worker _iterate_host_worker jqc success info debug error warning ssh_to_node log_message_prefix is_gke to_yaml wait_safe
191190

192191
export CONFIG TEMP_DIR THIS_SCRIPT DIR NETASSERT_IMAGE_NAME DEBUG IS_OFFLINE CONTAINER_BASE_NAME SSH_USER GCLOUD_SSH_OPTIONS
193192
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "bash -c 'APP=\"ava --tap --concurrency=2\"; if [[ $(whoami) != root ]]; then echo \"WARNING: UDP requires root (currently $(whoami)). Requesting sudo\"; sudo ${APP}; else ${APP}; fi'"
7+
"test": "bash -c 'APP=\"ava --tap --concurrency=2\"; if false; then echo \"WARNING: UDP requires root (currently $(whoami)). Requesting sudo\"; sudo ${APP}; else ${APP}; fi'"
88
},
99
"repository": {
1010
"type": "git",

test/test-localhost.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ host:
22
localhost:
33
localhost:
44
- TCP:22
5-
- -999
6-
- -UDP:1234
7-
- -UDP:555
5+
- 39111
6+
# - -999
7+
# - -UDP:1234
8+
# - -UDP:555
89

0 commit comments

Comments
 (0)