Skip to content

Commit

Permalink
Add tests for netrc creds loading and host masking.
Browse files Browse the repository at this point in the history
  • Loading branch information
drebs committed Apr 4, 2017
1 parent 29975f1 commit f9d1c5b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/pytest_benchmark/storage/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def default(self, data):
return "UNSERIALIZABLE[%r]" % data


def _mask_hosts(hosts):
m = re.compile('^([^:]+)://[^@]+@')
sub_fun = partial(m.sub, '\\1://***:***@')
masked_hosts = list(map(sub_fun, hosts))
return masked_hosts


class ElasticsearchStorage(object):
def __init__(self, hosts, index, doctype, project_name, logger,
default_machine_id=None):
Expand Down Expand Up @@ -165,9 +172,7 @@ def save(self, output_json, save):
id=doc_id,
)
# hide user's credentials before logging
m = re.compile('^([^:]+)://[^@]+@')
sub_fun = partial(m.sub, '\\1://***:***@')
masked_hosts = list(map(sub_fun, self._es_hosts))
masked_hosts = _mask_hosts(self._es_hosts)
self.logger.info("Saved benchmark data to %s to index %s as doctype %s" % (
masked_hosts, self._es_index, self._es_doctype))

Expand Down
59 changes: 59 additions & 0 deletions tests/test_elasticsearch_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import logging
import os
from io import BytesIO
from io import StringIO

Expand All @@ -16,6 +17,8 @@
from pytest_benchmark.plugin import pytest_benchmark_generate_json
from pytest_benchmark.plugin import pytest_benchmark_group_stats
from pytest_benchmark.storage.elasticsearch import ElasticsearchStorage
from pytest_benchmark.storage.elasticsearch import _mask_hosts
from pytest_benchmark.utils import parse_elasticsearch_storage

try:
import unittest.mock as mock
Expand Down Expand Up @@ -173,3 +176,59 @@ def test_handle_saving(sess, logger_output, monkeypatch):
body=ES_DATA,
id='FoobarOS_commitId_tests/test_normal.py::test_xfast_parametrized[0]',
)


def test_parse_with_no_creds():
string = 'https://example.org,another.org'
hosts, _, _, _ = parse_elasticsearch_storage(string)
assert len(hosts) == 2
assert 'https://example.org' in hosts
assert 'https://another.org' in hosts


def test_parse_with_creds_in_first_host_of_url():
string = 'https://user:[email protected],another.org'
hosts, _, _, _ = parse_elasticsearch_storage(string)
assert len(hosts) == 2
assert 'https://user:[email protected]' in hosts
assert 'https://another.org' in hosts


def test_parse_with_creds_in_second_host_of_url():
string = 'https://example.org,user:[email protected]'
hosts, _, _, _ = parse_elasticsearch_storage(string)
assert len(hosts) == 2
assert 'https://example.org' in hosts
assert 'https://user:[email protected]' in hosts


def test_parse_with_creds_in_netrc(tmpdir):
netrc_file = os.path.join(tmpdir.strpath, 'netrc')
with open(netrc_file, 'w') as f:
f.write('machine example.org login user1 password pass1\n')
f.write('machine another.org login user2 password pass2\n')
string = 'https://example.org,another.org'
hosts, _, _, _ = parse_elasticsearch_storage(string, netrc_file=netrc_file)
assert len(hosts) == 2
assert 'https://user1:[email protected]' in hosts
assert 'https://user2:[email protected]' in hosts


def test_parse_url_creds_supersedes_netrc_creds(tmpdir):
netrc_file = os.path.join(tmpdir.strpath, 'netrc')
with open(netrc_file, 'w') as f:
f.write('machine example.org login user1 password pass1\n')
f.write('machine another.org login user2 password pass2\n')
string = 'https://user3:[email protected],another.org'
hosts, _, _, _ = parse_elasticsearch_storage(string, netrc_file=netrc_file)
assert len(hosts) == 2
assert 'https://user3:[email protected]' in hosts # superseded by creds in url
assert 'https://user2:[email protected]' in hosts # got creds from netrc file


def test__mask_hosts():
hosts = ['https://user1:[email protected]', 'https://user2:[email protected]']
masked_hosts = _mask_hosts(hosts)
assert len(masked_hosts) == len(hosts)
assert 'https://***:***@example.org' in masked_hosts
assert 'https://***:***@another.org' in masked_hosts

0 comments on commit f9d1c5b

Please sign in to comment.