Skip to content

Commit 2fd9d2f

Browse files
author
Sebastian Wagner
committed
ENH: intelmqsetup: extend to intelmq-api
fixes #1783
1 parent 319efd5 commit 2fd9d2f

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ CHANGELOG
3333
- Add missing newlines at end of various test input files (PR#1785 by Sebastian Wagner, fixes #1777).
3434

3535
### Tools
36+
- `intelmqsetup`:
37+
- Also cover required directory layout and file permissions for `intelmq-api` (PR#1787 by Sebastian Wagner, fixes #1783).
3638

3739
### Contrib
3840

intelmq/bin/intelmqsetup.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
* set intelmq as owner for those
1010
* providing example configuration files if not already existing
1111
12+
If intelmq-api is installed, the similar steps are performed:
13+
* creates needed directories
14+
* sets the webserver as group for them
15+
* sets group write permissions
16+
1217
Reasoning:
1318
Pip does not (and cannot) create `/opt/intelmq`/user-given ROOT_DIR, as described in
1419
https://github.com/certtools/intelmq/issues/819
@@ -17,6 +22,7 @@
1722
import glob
1823
import os
1924
import shutil
25+
import stat
2026
import sys
2127
import pkg_resources
2228

@@ -78,6 +84,18 @@ def change_owner(file: str, owner=None, group=None):
7884
shutil.chown(file, group=group)
7985

8086

87+
def find_webserver_user():
88+
candidates = ('www-data', 'wwwrun', 'httpd', 'apache')
89+
for candidate in candidates:
90+
try:
91+
getpwnam(candidate)
92+
except KeyError:
93+
pass
94+
else:
95+
print(f'Detected webserver username {candidate!r}.')
96+
return candidate
97+
98+
8199
def intelmqsetup_core(ownership=True, state_file=STATE_FILE_PATH):
82100
directories_modes = ((FILE_OUTPUT_PATH, 0o755, 'drwxr-xr-x'),
83101
(VAR_RUN_PATH, 0o755, 'drwxr-xr-x'),
@@ -115,6 +133,24 @@ def intelmqsetup_core(ownership=True, state_file=STATE_FILE_PATH):
115133
controller.upgrade_conf(state_file=state_file, no_backup=True)
116134

117135

136+
def intelmqsetup_api(ownership: bool = True, webserver_user: Optional[str] = None):
137+
if ownership:
138+
change_owner(CONFIG_DIR, group='intelmq')
139+
140+
# Manager configuration directory
141+
create_directory(MANAGER_CONFIG_DIR, 0o775, 'drwxrwxr-x')
142+
if ownership:
143+
change_owner(MANAGER_CONFIG_DIR, group='intelmq')
144+
145+
intelmq_group = getgrnam('intelmq')
146+
webserver_user = webserver_user or find_webserver_user()
147+
if webserver_user not in intelmq_group.gr_mem:
148+
sys.exit(red("Webserver user {webserver_user} is not a member of the 'intelmq' group. "
149+
f"Please add it with: 'usermod -aG intelmq {webserver_user}'."))
150+
151+
print('Setup of intelmq-api successful.')
152+
153+
118154
def main():
119155
parser = argparse.ArgumentParser("Set's up directories and example "
120156
"configurations for IntelMQ.")
@@ -123,11 +159,17 @@ def main():
123159
parser.add_argument('--state-file',
124160
help='The state file location to use.',
125161
default=STATE_FILE_PATH)
162+
parser.add_argument('--webserver-user',
163+
help='The webserver to use instead of auto-detection.')
126164
args = parser.parse_args()
127165

128166
basic_checks(skip_ownership=args.skip_ownership)
129167
intelmqsetup_core(ownership=not args.skip_ownership,
130168
state_file=args.state_file)
169+
if intelmq_api:
170+
print('Running setup for intelmq-api.')
171+
intelmqsetup_api(ownership=not args.skip_ownership,
172+
webserver_user=args.webserver_user)
131173

132174

133175
if __name__ == '__main__':

0 commit comments

Comments
 (0)