Skip to content

Commit 453553f

Browse files
authored
Change the way we get environment variables (#280)
* Change the way we get environment variables * Change environ to getenv * Read from envvar, then config file * Switch to get_path * Lint: Remove unused import * Add --cloud-id/--elasticsearch-url * Fix comment copy-pasta
1 parent 9d22970 commit 453553f

File tree

2 files changed

+36
-34
lines changed

2 files changed

+36
-34
lines changed

detection_rules/eswrap.py

+23-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from kibana import Kibana, RuleResource
1313

1414
from .main import root
15-
from .misc import set_param_values
15+
from .misc import getdefault
1616
from .utils import normalize_timing_and_sort, unix_time_to_formatted, get_path
1717
from .rule_loader import get_rule, rta_mappings, load_rule_files, load_rules
1818

@@ -179,10 +179,10 @@ def run(self, agent_hostname, indexes, verbose=True, **match):
179179

180180
@es_group.command('collect-events')
181181
@click.argument('agent-hostname')
182-
@click.option('--elasticsearch-url', '-u', callback=set_param_values, expose_value=True)
183-
@click.option('--cloud-id', callback=set_param_values, expose_value=True)
184-
@click.option('--user', '-u', callback=set_param_values, expose_value=True, hide_input=False)
185-
@click.option('--password', '-p', callback=set_param_values, expose_value=True, hide_input=True)
182+
@click.option('--elasticsearch-url', '-u', default=getdefault("elasticsearch_url"))
183+
@click.option('--cloud-id', default=getdefault("cloud_id"))
184+
@click.option('--user', '-u', default=getdefault("user"))
185+
@click.option('--password', '-p', default=getdefault("password"))
186186
@click.option('--index', '-i', multiple=True, help='Index(es) to search against (default: all indexes)')
187187
@click.option('--agent-type', '-a', help='Restrict results to a source type (agent.type) ex: auditbeat')
188188
@click.option('--rta-name', '-r', help='Name of RTA in order to save events directly to unit tests data directory')
@@ -193,6 +193,13 @@ def collect_events(agent_hostname, elasticsearch_url, cloud_id, user, password,
193193
"""Collect events from Elasticsearch."""
194194
match = {'agent.type': agent_type} if agent_type else {}
195195

196+
if not cloud_id or elasticsearch_url:
197+
raise click.ClickException("Missing required --cloud-id or --elasticsearch-url")
198+
199+
# don't prompt for these until there's a cloud id or elasticsearch URL
200+
user = user or click.prompt("user")
201+
password = password or click.prompt("password", hide_input=True)
202+
196203
try:
197204
client = get_es_client(elasticsearch_url=elasticsearch_url, use_ssl=True, cloud_id=cloud_id, user=user,
198205
password=password)
@@ -229,16 +236,23 @@ def normalize_file(events_file):
229236

230237
@root.command("kibana-upload")
231238
@click.argument("toml-files", nargs=-1, required=True)
232-
@click.option('--kibana-url', '-u', callback=set_param_values, expose_value=True)
233-
@click.option('--cloud-id', callback=set_param_values, expose_value=True)
234-
@click.option('--user', '-u', callback=set_param_values, expose_value=True, hide_input=False)
235-
@click.option('--password', '-p', callback=set_param_values, expose_value=True, hide_input=True)
239+
@click.option('--kibana-url', '-u', default=getdefault("kibana_url"))
240+
@click.option('--cloud-id', default=getdefault("cloud_id"))
241+
@click.option('--user', '-u', default=getdefault("user"))
242+
@click.option('--password', '-p', default=getdefault("password"))
236243
def kibana_upload(toml_files, kibana_url, cloud_id, user, password):
237244
"""Upload a list of rule .toml files to Kibana."""
238245
from uuid import uuid4
239246
from .packaging import manage_versions
240247
from .schemas import downgrade
241248

249+
if not cloud_id or kibana_url:
250+
raise click.ClickException("Missing required --cloud-id or --kibana-url")
251+
252+
# don't prompt for these until there's a cloud id or kibana URL
253+
user = user or click.prompt("user")
254+
password = password or click.prompt("password", hide_input=True)
255+
242256
with Kibana(cloud_id=cloud_id, url=kibana_url) as kibana:
243257
kibana.login(user, password)
244258

detection_rules/misc.py

+13-25
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import click
1313
import requests
1414

15-
from .utils import ROOT_DIR
15+
from .utils import cached, get_path
1616

1717
_CONFIG = {}
1818

@@ -200,35 +200,23 @@ def download_worker(rule_info):
200200
return kibana_rules
201201

202202

203+
@cached
203204
def parse_config():
204205
"""Parse a default config file."""
205-
global _CONFIG
206+
config_file = get_path('.detection-rules-cfg.json')
207+
config = {}
206208

207-
if not _CONFIG:
208-
config_file = os.path.join(ROOT_DIR, '.detection-rules-cfg.json')
209+
if os.path.exists(config_file):
210+
with open(config_file) as f:
211+
config = json.load(f)
209212

210-
if os.path.exists(config_file):
211-
with open(config_file) as f:
212-
_CONFIG = json.load(f)
213+
click.secho('Loaded config file: {}'.format(config_file), fg='yellow')
213214

214-
click.secho('Loaded config file: {}'.format(config_file), fg='yellow')
215+
return config
215216

216-
return _CONFIG
217217

218-
219-
def set_param_values(ctx, param, value):
220-
"""Get value for defined key."""
221-
key = param.name
218+
def getdefault(name):
219+
"""Callback function for `default` to get an environment variable."""
220+
envvar = f"DR_{name.upper()}"
222221
config = parse_config()
223-
env_key = 'DR_' + key.upper()
224-
prompt = True if param.hide_input is not False else False
225-
226-
if value:
227-
return value
228-
elif os.environ.get(env_key):
229-
return os.environ[env_key]
230-
elif config.get(key) is not None:
231-
return config[key]
232-
elif prompt:
233-
return click.prompt(key, default=param.default if not param.default else None, hide_input=param.hide_input,
234-
show_default=True if param.default else False)
222+
return lambda: os.environ.get(envvar, config.get(name))

0 commit comments

Comments
 (0)