Skip to content

Commit aa78075

Browse files
committed
update rust-webservice template to use build container
1 parent c26b218 commit aa78075

File tree

4 files changed

+131
-90
lines changed

4 files changed

+131
-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: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 only shows a proof of concept of building a C 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: Initial System Upgrade
17+
ansible.builtin.apt:
18+
update_cache: true
19+
install_recommends: false
20+
upgrade: full
21+
22+
# Install the tools required to compile your code such as npm, nodejs, gcc...
23+
- name: Install dependencies to build the track
24+
ansible.builtin.apt:
25+
name:
26+
- npm
27+
- curl
28+
state: present
29+
30+
- name: Check if cargo is installed
31+
ansible.builtin.stat:
32+
path: /root/.cargo/bin/cargo
33+
register: cargo_exists
34+
35+
- name: Download Cargo Installer
36+
ansible.builtin.get_url:
37+
url: https://sh.rustup.rs
38+
dest: /tmp/sh.rustup.rs
39+
mode: '0755'
40+
force: true
41+
when: not cargo_exists.stat.exists
42+
tags:
43+
- rust
44+
45+
- name: Install Cargo
46+
when: not cargo_exists.stat.exists
47+
ansible.builtin.command: /tmp/sh.rustup.rs -y
48+
register: my_output
49+
changed_when: my_output.rc != 0
50+
tags:
51+
- rust
52+
53+
- name: Copy the challenge sources
54+
ansible.builtin.copy:
55+
src: challenge/
56+
dest: /tmp/{{ data.name }}
57+
owner: root
58+
group: root
59+
mode: '0644'
60+
61+
- name: NPM install
62+
community.general.npm:
63+
path: /tmp/{{ data.name }}/client/
64+
environment:
65+
NODE_OPTIONS: "--dns-result-order=ipv4first"
66+
67+
- name: Build
68+
ansible.builtin.command:
69+
cmd: /root/.cargo/bin/cargo build --release
70+
chdir: /tmp/{{ data.name }}/
71+
register: my_output
72+
changed_when: my_output.rc != 0
73+
74+
- name: Create dist directory
75+
ansible.builtin.file:
76+
path: /tmp/dist/{{ data.name }}
77+
state: directory
78+
mode: '0755'
79+
80+
- name: Copy server binary
81+
ansible.builtin.copy:
82+
remote_src: true
83+
src: /tmp/{{ data.name }}/target/release/{{ data.name }}
84+
dest: /tmp/dist/{{ data.name }}/{{ data.name }}
85+
owner: root
86+
group: root
87+
mode: '0744'
88+
89+
- name: Copy client
90+
ansible.builtin.copy:
91+
remote_src: true
92+
src: /tmp/{{ data.name }}/dist
93+
dest: /tmp/dist/{{ data.name }}/
94+
owner: root
95+
group: root
96+
mode: '0644'
97+
98+
# Create a TAR archive with the compiled program
99+
- name: Create archive of build
100+
community.general.archive:
101+
path: /tmp/dist/{{ data.name }}
102+
dest: /tmp/build.tar
103+
format: tar
104+
mode: '0644'
105+
106+
# Extract the archive from the build container and save it on the local host
107+
- name: Fetch archive
108+
ansible.builtin.fetch:
109+
src: /tmp/build.tar
110+
dest: /tmp/nsec/{{ data.name }}.tar
111+
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)