Skip to content

Commit c7696d3

Browse files
committed
chore: check ec2ic perms if not in qemu mode
1 parent 41a4f14 commit c7696d3

File tree

3 files changed

+36
-12
lines changed

3 files changed

+36
-12
lines changed

ansible/files/permission_check.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import subprocess
22
import json
33
import sys
4+
import argparse
5+
46

57
# Expected groups for each user
68
expected_results = {
@@ -88,6 +90,9 @@
8890
"messagebus": [
8991
{"groupname":"messagebus","username":"messagebus"}
9092
],
93+
"ec2-instance-connect": [
94+
{"groupname": "nogroup", "username": "ec2-instance-connect"}
95+
],
9196
"sshd": [
9297
{"groupname":"nogroup","username":"sshd"}
9398
],
@@ -142,20 +147,23 @@
142147
]
143148
}
144149

150+
145151
# This program depends on osquery being installed on the system
146152
# Function to run osquery
147153
def run_osquery(query):
148154
process = subprocess.Popen(['osqueryi', '--json', query], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
149155
output, error = process.communicate()
150156
return output.decode('utf-8')
151157

158+
152159
def parse_json(json_str):
153160
try:
154161
return json.loads(json_str)
155162
except json.JSONDecodeError as e:
156163
print("Error decoding JSON:", e)
157164
sys.exit(1)
158165

166+
159167
def compare_results(username, query_result):
160168
expected_result = expected_results.get(username)
161169
if expected_result is None:
@@ -170,6 +178,7 @@ def compare_results(username, query_result):
170178
print("Got:", query_result)
171179
sys.exit(1)
172180

181+
173182
def check_nixbld_users():
174183
query = """
175184
SELECT u.username, g.groupname
@@ -188,15 +197,30 @@ def check_nixbld_users():
188197

189198
print("All nixbld users are in the 'nixbld' group.")
190199

191-
# Define usernames for which you want to compare results
192-
usernames = ["postgres", "ubuntu", "root", "daemon", "bin", "sys", "sync", "games","man","lp","mail","news","uucp","proxy","www-data","backup","list","irc","gnats","nobody","systemd-network","systemd-resolve","systemd-timesync","messagebus","sshd","wal-g","pgbouncer","gotrue","envoy","kong","nginx","vector","adminapi","postgrest","tcpdump","systemd-coredump"]
193200

194-
# Iterate over usernames, run the query, and compare results
195-
for username in usernames:
196-
query = f"SELECT u.username, g.groupname FROM users u JOIN user_groups ug ON u.uid = ug.uid JOIN groups g ON ug.gid = g.gid WHERE u.username = '{username}' ORDER BY g.groupname;"
197-
query_result = run_osquery(query)
198-
parsed_result = parse_json(query_result)
199-
compare_results(username, parsed_result)
201+
def main():
202+
parser = argparse.ArgumentParser(
203+
prog='Supabase Postgres Artifact Permissions Checker',
204+
description='Checks the Postgres Artifact for the appropriate users and group memberships')
205+
parser.add_argument('-q', '--qemu', action='store_true', help='Whether we are checking a QEMU artifact')
206+
args = parser.parse_args()
207+
qemu_artifact = args.qemu or False
208+
209+
# Define usernames for which you want to compare results
210+
usernames = ["postgres", "ubuntu", "root", "daemon", "bin", "sys", "sync", "games","man","lp","mail","news","uucp","proxy","www-data","backup","list","irc","gnats","nobody","systemd-network","systemd-resolve","systemd-timesync","messagebus","sshd","wal-g","pgbouncer","gotrue","envoy","kong","nginx","vector","adminapi","postgrest","tcpdump","systemd-coredump"]
211+
if not qemu_artifact:
212+
usernames.append("ec2-instance-connect")
213+
214+
# Iterate over usernames, run the query, and compare results
215+
for username in usernames:
216+
query = f"SELECT u.username, g.groupname FROM users u JOIN user_groups ug ON u.uid = ug.uid JOIN groups g ON ug.gid = g.gid WHERE u.username = '{username}' ORDER BY g.groupname;"
217+
query_result = run_osquery(query)
218+
parsed_result = parse_json(query_result)
219+
compare_results(username, parsed_result)
220+
221+
# Check if all nixbld users are in the nixbld group
222+
check_nixbld_users()
223+
200224

201-
# Check if all nixbld users are in the nixbld group
202-
check_nixbld_users()
225+
if __name__ == "__main__":
226+
main()

ansible/playbook.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@
201201
- name: Run osquery permission checks
202202
become: yes
203203
shell: |
204-
sudo -u ubuntu bash -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && /usr/bin/python3 /tmp/ansible-playbook/ansible/files/permission_check.py"
204+
sudo -u ubuntu bash -c ". /nix/var/nix/profiles/default/etc/profile.d/nix-daemon.sh && /usr/bin/python3 /tmp/ansible-playbook/ansible/files/permission_check.py {{ '--qemu' if qemu_mode is defined else '' }}"
205205
when: stage2_nix
206206

207207
- name: Remove osquery

ebssurrogate/scripts/qemu-bootstrap-nix.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ EOF
9696
# Run Ansible playbook
9797
export ANSIBLE_LOG_PATH=/tmp/ansible.log && export ANSIBLE_REMOTE_TEMP=/tmp
9898
ansible-playbook ./ansible/playbook.yml \
99-
--extra-vars '{"nixpkg_mode": false, "stage2_nix": true, "debpkg_mode": false}' \
99+
--extra-vars '{"nixpkg_mode": false, "stage2_nix": true, "debpkg_mode": false, "qemu_mode": true}' \
100100
--extra-vars "git_commit_sha=${GIT_SHA}"
101101
}
102102

0 commit comments

Comments
 (0)