server/image-hash: optionally allow for elasticsearch authentication

This commit is contained in:
Shyam Sunder 2019-09-15 12:45:17 -04:00
parent 734e28e014
commit 54eab0aa35
6 changed files with 27 additions and 14 deletions

View file

@ -24,6 +24,10 @@ services:
#POSTGRES_PORT: 5432 #POSTGRES_PORT: 5432
#ESEARCH_PORT: 9200 #ESEARCH_PORT: 9200
#ESEARCH_INDEX: szurubooru #ESEARCH_INDEX: szurubooru
#ESEARCH_PASSWORD: (empty by default, set if you are using an external
# source for elasticsearch and want to use HTTP basic
# authentication for security)
#ESEARCH_USER: szurubooru (only used if password is set)
#LOG_SQL: 0 (1 for verbose SQL logs) #LOG_SQL: 0 (1 for verbose SQL logs)
volumes: volumes:
- "${MOUNT_DATA}:/data" - "${MOUNT_DATA}:/data"

View file

@ -146,3 +146,5 @@ privileges:
# host: localhost # host: localhost
# port: 9200 # port: 9200
# index: szurubooru # index: szurubooru
# user: szurubooru
# pass:

View file

@ -5,6 +5,7 @@ SQLAlchemy>=1.0.12
coloredlogs==5.0 coloredlogs==5.0
elasticsearch>=5.0.0,<7.0.0 elasticsearch>=5.0.0,<7.0.0
elasticsearch-dsl>=5.0.0,<7.0.0 elasticsearch-dsl>=5.0.0,<7.0.0
certifi>=2017.11.5
numpy>=1.8.2 numpy>=1.8.2
pillow>=4.3.0 pillow>=4.3.0
pynacl==1.2.1 pynacl==1.2.1

View file

@ -40,7 +40,9 @@ def _docker_config() -> Dict:
'elasticsearch': { 'elasticsearch': {
'host': os.getenv('ESEARCH_HOST'), 'host': os.getenv('ESEARCH_HOST'),
'port': int(os.getenv('ESEARCH_PORT', 9200)), 'port': int(os.getenv('ESEARCH_PORT', 9200)),
'index': os.getenv('ESEARCH_INDEX', 'szurubooru') 'index': os.getenv('ESEARCH_INDEX', 'szurubooru'),
'user': os.getenv('ESEARCH_USER', os.getenv('ESEARCH_INDEX', 'szurubooru')),
'pass': os.getenv('ESEARCH_PASSWORD', False)
} }
} }

View file

@ -31,11 +31,18 @@ Window = Tuple[Tuple[float, float], Tuple[float, float]]
NpMatrix = Any NpMatrix = Any
def _get_session() -> elasticsearch.Elasticsearch: def get_session() -> elasticsearch.Elasticsearch:
extra_args = {}
if config.config['elasticsearch']['pass']:
extra_args['http_auth'] = (
config.config['elasticsearch']['user'],
config.config['elasticsearch']['pass'])
extra_args['scheme'] = 'https'
extra_args['port'] = 443
return elasticsearch.Elasticsearch([{ return elasticsearch.Elasticsearch([{
'host': config.config['elasticsearch']['host'], 'host': config.config['elasticsearch']['host'],
'port': config.config['elasticsearch']['port'], 'port': config.config['elasticsearch']['port'],
}]) }], **extra_args)
def _preprocess_image(content: bytes) -> NpMatrix: def _preprocess_image(content: bytes) -> NpMatrix:
@ -271,7 +278,7 @@ def add_image(path: str, image_content: bytes) -> None:
for i in range(MAX_WORDS): for i in range(MAX_WORDS):
record['simple_word_' + str(i)] = words[i].tolist() record['simple_word_' + str(i)] = words[i].tolist()
_get_session().index( get_session().index(
index=config.config['elasticsearch']['index'], index=config.config['elasticsearch']['index'],
doc_type=ES_DOC_TYPE, doc_type=ES_DOC_TYPE,
body=record, body=record,
@ -281,7 +288,7 @@ def add_image(path: str, image_content: bytes) -> None:
@_safety_blanket(lambda: None) @_safety_blanket(lambda: None)
def delete_image(path: str) -> None: def delete_image(path: str) -> None:
assert path assert path
_get_session().delete_by_query( get_session().delete_by_query(
index=config.config['elasticsearch']['index'], index=config.config['elasticsearch']['index'],
doc_type=ES_DOC_TYPE, doc_type=ES_DOC_TYPE,
body={'query': {'term': {'path': path}}}) body={'query': {'term': {'path': path}}})
@ -292,7 +299,7 @@ def search_by_image(image_content: bytes) -> List[Lookalike]:
signature = _generate_signature(image_content) signature = _generate_signature(image_content)
words = _get_words(signature, k=SAMPLE_WORDS, n=MAX_WORDS) words = _get_words(signature, k=SAMPLE_WORDS, n=MAX_WORDS)
res = _get_session().search( res = get_session().search(
index=config.config['elasticsearch']['index'], index=config.config['elasticsearch']['index'],
doc_type=ES_DOC_TYPE, doc_type=ES_DOC_TYPE,
body={ body={
@ -333,7 +340,7 @@ def search_by_image(image_content: bytes) -> List[Lookalike]:
@_safety_blanket(lambda: None) @_safety_blanket(lambda: None)
def purge() -> None: def purge() -> None:
_get_session().delete_by_query( get_session().delete_by_query(
index=config.config['elasticsearch']['index'], index=config.config['elasticsearch']['index'],
doc_type=ES_DOC_TYPE, doc_type=ES_DOC_TYPE,
body={'query': {'match_all': {}}}, body={'query': {'match_all': {}}},
@ -344,7 +351,7 @@ def purge() -> None:
def get_all_paths() -> Set[str]: def get_all_paths() -> Set[str]:
search = ( search = (
elasticsearch_dsl.Search( elasticsearch_dsl.Search(
using=_get_session(), using=get_session(),
index=config.config['elasticsearch']['index'], index=config.config['elasticsearch']['index'],
doc_type=ES_DOC_TYPE) doc_type=ES_DOC_TYPE)
.source(['path'])) .source(['path']))

View file

@ -6,17 +6,14 @@ Docker helper script. Blocks until the ElasticSearch service is ready.
import logging import logging
import time import time
import elasticsearch from szurubooru import errors
from szurubooru import config, errors from szurubooru.func.image_hash import get_session
def main(): def main():
print('Looking for ElasticSearch connection...') print('Looking for ElasticSearch connection...')
logging.basicConfig(level=logging.ERROR) logging.basicConfig(level=logging.ERROR)
es = elasticsearch.Elasticsearch([{ es = get_session()
'host': config.config['elasticsearch']['host'],
'port': config.config['elasticsearch']['port'],
}])
TIMEOUT = 30 TIMEOUT = 30
DELAY = 0.1 DELAY = 0.1