Skip to content

Commit 2762447

Browse files
authored
Merge pull request #44 from zer0x64/rust-webserver-build
update rust-webservice template to use build container
2 parents c26b218 + c6796be commit 2762447

File tree

4 files changed

+142
-90
lines changed

4 files changed

+142
-90
lines changed

challenges/mock-track-python-service/ansible/challenge/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ def index():
2929
</form>
3030
</body>
3131
</html>
32-
"""
32+
"""

ctf/new.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ def new(
5252
typer.Option(
5353
"--with-build",
5454
help="If a build container is required.",
55-
prompt="Is a build container required?",
5655
),
5756
] = False,
5857
) -> None:
@@ -67,6 +66,9 @@ def new(
6766
)
6867
exit(code=1)
6968

69+
if template == Template.RUST_WEBSERVICE:
70+
with_build_container = True
71+
7072
if os.path.exists(
7173
path=(
7274
new_challenge_directory := os.path.join(
@@ -253,10 +255,19 @@ def new(
253255
LOG.debug(msg=f"Wrote {p}.")
254256

255257
if with_build_container:
256-
track_template = env.get_template(name=os.path.join("common", "build.yaml.j2"))
258+
try:
259+
track_template = env.get_template(
260+
name=os.path.join(template, "build.yaml.j2")
261+
)
262+
except jinja2.TemplateNotFound:
263+
track_template = env.get_template(
264+
name=os.path.join("common", "build.yaml.j2")
265+
)
266+
257267
render = track_template.render(
258268
data={"name": name, "with_build": with_build_container}
259269
)
270+
260271
with open(
261272
file=(p := os.path.join(ansible_directory, "build.yaml")),
262273
mode="w",
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# This Ansible script is used to compile your challenge, create an archive and extract that archive on to the local host.
2+
# This script builds a frontend using NPM and a Rust program. Change it as per your needs.
3+
- name: "Build container"
4+
hosts: "build-container"
5+
vars_files:
6+
- ../track.yaml
7+
tasks:
8+
- name: "Load flags"
9+
loop: "{{ '{{ flags }}' }}"
10+
vars:
11+
key: "{{ '{{ (item.tags).discourse }}' }}"
12+
value: "{{ '{{ item.flag }}' }}"
13+
ansible.builtin.set_fact:
14+
track_flags: "{{ '{{ track_flags | default({}) | combine({key: value}) }}' }}"
15+
16+
- name: Check if IPv4 address is set
17+
ansible.builtin.debug:
18+
msg: IPv4 address is set
19+
when: ansible_all_ipv4_addresses | length > 0
20+
21+
- name: Initial System Upgrade
22+
ansible.builtin.apt:
23+
update_cache: true
24+
install_recommends: false
25+
upgrade: full
26+
27+
# Install the tools required to compile your code such as npm, nodejs, gcc...
28+
- name: Install dependencies to build the track
29+
ansible.builtin.apt:
30+
name:
31+
- npm
32+
- curl
33+
state: present
34+
35+
- name: Check if cargo is installed
36+
ansible.builtin.stat:
37+
path: /root/.cargo/bin/cargo
38+
register: cargo_exists
39+
40+
- name: Download Cargo Installer
41+
ansible.builtin.get_url:
42+
url: https://sh.rustup.rs
43+
dest: /tmp/sh.rustup.rs
44+
mode: '0755'
45+
force: true
46+
when: not cargo_exists.stat.exists
47+
tags:
48+
- rust
49+
50+
- name: Install Cargo
51+
when: not cargo_exists.stat.exists
52+
ansible.builtin.command: /tmp/sh.rustup.rs -y
53+
register: my_output
54+
changed_when: my_output.rc != 0
55+
tags:
56+
- rust
57+
58+
- name: Copy the challenge sources
59+
ansible.builtin.copy:
60+
src: challenge/
61+
dest: /tmp/{{ data.name }}
62+
owner: root
63+
group: root
64+
mode: '0644'
65+
66+
- name: NPM install (if IPv4)
67+
community.general.npm:
68+
path: /tmp/{{ data.name }}/client/
69+
environment:
70+
NODE_OPTIONS: "--dns-result-order=ipv4first"
71+
when: ansible_all_ipv4_addresses | length > 0
72+
73+
- name: NPM install (if IPv6)
74+
community.general.npm:
75+
path: /tmp/{{ data.name }}/client/
76+
when: ansible_all_ipv4_addresses | length == 0
77+
78+
- name: Build
79+
ansible.builtin.command:
80+
cmd: /root/.cargo/bin/cargo build --release
81+
chdir: /tmp/{{ data.name }}/
82+
register: my_output
83+
changed_when: my_output.rc != 0
84+
85+
- name: Create dist directory
86+
ansible.builtin.file:
87+
path: /tmp/dist/{{ data.name }}
88+
state: directory
89+
mode: '0755'
90+
91+
- name: Copy server binary
92+
ansible.builtin.copy:
93+
remote_src: true
94+
src: /tmp/{{ data.name }}/target/release/{{ data.name }}
95+
dest: /tmp/dist/{{ data.name }}/{{ data.name }}
96+
owner: root
97+
group: root
98+
mode: '0744'
99+
100+
- name: Copy client
101+
ansible.builtin.copy:
102+
remote_src: true
103+
src: /tmp/{{ data.name }}/dist
104+
dest: /tmp/dist/{{ data.name }}/
105+
owner: root
106+
group: root
107+
mode: '0644'
108+
109+
# Create a TAR archive with the compiled program
110+
- name: Create archive of build
111+
community.general.archive:
112+
path: /tmp/dist/{{ data.name }}
113+
dest: /tmp/build.tar
114+
format: tar
115+
mode: '0644'
116+
117+
# Extract the archive from the build container and save it on the local host
118+
- name: Fetch archive
119+
ansible.builtin.fetch:
120+
src: /tmp/build.tar
121+
dest: /tmp/nsec/{{ data.name }}.tar
122+
flat: true
Lines changed: 6 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This is the main ansible script to deploy the challenge.
22

33
# Example on how to run stuff on all hosts of the track
4-
- name: "Install rust and npm"
4+
- name: "Setup web server"
55
hosts: all{% if data.with_build %},!build{% endif %}
66
vars_files:
77
- ../track.yaml
@@ -23,77 +23,13 @@
2323
install_recommends: false
2424
upgrade: full
2525

26-
- name: Install build dependencies
27-
ansible.builtin.apt:
28-
name:
29-
- npm
30-
- curl
31-
state: present
32-
33-
- name: Check if cargo is installed
34-
ansible.builtin.stat:
35-
path: /root/.cargo/bin/cargo
36-
register: cargo_exists
37-
38-
- name: Download Cargo Installer
39-
ansible.builtin.get_url:
40-
url: https://sh.rustup.rs
41-
dest: /tmp/sh.rustup.rs
42-
mode: '0755'
43-
force: true
44-
when: not cargo_exists.stat.exists
45-
tags:
46-
- rust
47-
48-
- name: Install Cargo
49-
when: not cargo_exists.stat.exists
50-
ansible.builtin.command: /tmp/sh.rustup.rs -y
51-
register: my_output
52-
changed_when: my_output.rc != 0
53-
tags:
54-
- rust
55-
56-
- name: Copy the challenge sources
57-
ansible.builtin.copy:
58-
src: challenge/
59-
dest: /tmp/{{ data.name }}
60-
owner: root
61-
group: root
62-
mode: '0644'
63-
64-
- name: NPM install
65-
community.general.npm:
66-
path: /tmp/slot-machine/client/
67-
environment:
68-
NODE_OPTIONS: "--dns-result-order=ipv4first"
69-
70-
- name: Build
71-
ansible.builtin.command:
72-
cmd: /root/.cargo/bin/cargo build --release
73-
chdir: /tmp/{{ data.name }}/
74-
register: my_output
75-
changed_when: my_output.rc != 0
76-
77-
- name: Copy server binary
78-
ansible.builtin.copy:
79-
src: /tmp/{{ data.name }}/target/release/{{ data.name }}
80-
dest: /opt/{{ data.name }}/{{ data.name }}
81-
owner: root
82-
group: root
83-
mode: '0744'
84-
85-
- name: Copy client
86-
ansible.builtin.copy:
87-
src: /tmp/{{ data.name }}/dist
88-
dest: /opt/{{ data.name }}/dist
26+
- name: Unarchive the content of the build
27+
ansible.builtin.unarchive:
28+
src: /tmp/nsec/{{ data.name }}.tar
29+
dest: /opt/
8930
owner: root
9031
group: root
91-
mode: '0644'
92-
93-
- name: Remove Build
94-
ansible.builtin.file:
95-
path: /tmp/{{ data.name }}/
96-
state: absent
32+
mode: '0755'
9733

9834
- name: Create systemd service
9935
ansible.builtin.copy:
@@ -116,26 +52,9 @@
11652
[Install]
11753
WantedBy=default.target
11854

119-
- name: Remove curl and npm
120-
ansible.builtin.apt:
121-
name:
122-
- npm
123-
- curl
124-
state: absent
125-
12655
- name: Start service
12756
ansible.builtin.service:
12857
name: {{ data.name }}.service
12958
state: restarted
13059
enabled: true
13160
daemon_reload: true
132-
{% if data.with_build %}
133-
# When using a build container, the unarchive module can be used to install the content on the remote.
134-
- name: Unarchive the content of the build
135-
ansible.builtin.unarchive:
136-
src: /tmp/build.tar
137-
dest: /tmp/
138-
owner: root
139-
group: root
140-
mode: '0755'
141-
{% endif %}

0 commit comments

Comments
 (0)