Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for http auth #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions service/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DataConnectionException(Exception):
"""


def elasticsearch_connection(hosts, sniff=False, sniffer_timeout=60):
def elasticsearch_connection(hosts, sniff=False, http_auth=False, sniffer_timeout=60):
"""Crea una conexión a Elasticsearch.

Args:
Expand All @@ -44,13 +44,19 @@ def elasticsearch_connection(hosts, sniff=False, sniffer_timeout=60):
"""
try:
options = {
'hosts': hosts
'hosts': hosts,

}

if sniff:
options['sniff_on_start'] = True
options['sniff_on_connection_fail'] = True
options['sniffer_timeout'] = sniffer_timeout

if http_auth:
options['http_auth'] = http_auth

return elasticsearch.Elasticsearch(**options)

return elasticsearch.Elasticsearch(**options)
except elasticsearch.ElasticsearchException as e:
Expand Down
11 changes: 9 additions & 2 deletions service/normalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
de los recursos que expone la API.
"""

import logging
import logging, os
from flask import current_app
from service import data, params, formatter, address, location, utils, street
from service import names as N
Expand All @@ -26,10 +26,17 @@ def get_elasticsearch():

"""
if not hasattr(current_app, 'elasticsearch'):
HTTP_AUTH_USER = os.environ.get("HTTP_AUTH_USER", "")
HTTP_AUTH_PASS = os.environ.get("HTTP_AUTH_PASS", "")
if HTTP_AUTH_USER and HTTP_AUTH_PASS:
auth = (HTTP_AUTH_USER, HTTP_AUTH_PASS)
else:
auth = False
current_app.elasticsearch = data.elasticsearch_connection(
hosts=current_app.config['ES_HOSTS'],
sniff=current_app.config['ES_SNIFF'],
sniffer_timeout=current_app.config['ES_SNIFFER_TIMEOUT']
sniffer_timeout=current_app.config['ES_SNIFFER_TIMEOUT'],
http_auth=auth
)

return current_app.elasticsearch
Expand Down