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

Added initial ES configuration. #1

Open
wants to merge 8 commits into
base: main
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
59 changes: 58 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,59 @@
# vivo-es
Configuration for ElasticSearch index used by VIVO
Configuration for Elasticsearch/OpenSearch index used by VIVO

# How to setup?

## Build the Docker Image:

```
sudo docker build -t vivo-elasticsearch -f elasticsearch/Dockerfile .
```
if you want to use OpenSearch:
```
sudo docker build -t vivo-opensearch -f opensearch/Dockerfile .
```

## Run the Container:

```
docker run -p 9200:9200 -p 9300:9300 --name vivo_es vivo-elasticsearch
```
if you built OpenSearch image:
```
docker run -p 9200:9200 -p 9300:9300 --name vivo_os vivo-opensearch
```

This setup will start Elasticsearch/OpenSearch, expose it on localhost:9200, and create the vivo index with the specified mapping by loading the configuration from index-config.json.

## Alternatively - Run Elasticsearch locally

1. Download Elasticsearch (suited for your platform) from the [official website](https://www.elastic.co/downloads/elasticsearch).
2. Run `bin/elasticsearch` (or `bin\elasticsearch.bat` on Windows) to start Elasticsearch [with security enabled](https://www.elastic.co/guide/en/elasticsearch/reference/current/configuring-stack-security.html).
3. Create `vivo` index by running:
ivanmrsulja marked this conversation as resolved.
Show resolved Hide resolved

```
curl -k -u '<ES_USERNAME>:<ES_PASSWORD>' -X PUT "https://localhost:9200/vivo" -H 'Content-Type: application/json' -d index-config.json
```

## Alternatively - Run OpenSearch locally

1. Download OpenSearch (suited for your platform) from the [official website](https://opensearch.org/versions/opensearch-2-18-0.html).
2. Run `./opensearch` (or `./opensearch.bat` on Windows) to start OpenSearch [with security enabled](https://opensearch.org/docs/latest/security/).
3. Create `vivo` index by running:

```
curl -k -u '<OS_USERNAME>:<OS_PASSWORD>' -X PUT "https://localhost:9200/vivo" -H 'Content-Type: application/json' -d index-config.json
```

# Adding security
By default, docker configuration sets up Elasticsearch/OpenSearch without any security measures. A configuration like that is only viable if you are running your ES/OS instance inside of an internal local-area network without exposing it to the internet. If you need to expose your ES/OS instance to the internet you MUST activate necessary security mesures.

In order to setup security (SSL + basic authentication) you have to do the following steps:

- Comment-out or remove the following line from the Dockerfile `ENV xpack.security.enabled=false` for Elasticsearch or `ENV plugins.security.disabled=true` for OpenSearch. If you use OS, you have to provide an initial administrator password through `OPENSEARCH_INITIAL_ADMIN_PASSWORD` variable.

- Build the image and run the respected container as explained above

- If you are using ES, upon startup, the script will generate a new secure password for you to use when connecting to the instance. Find the console output that looks like this: `The new password for the elastic user is: <PASSWORD_VALUE>` and save the password value, you cannot access it later. Keep in mind that, at this moment, VIVO only supports basic authentication for ES/OS (API key authentication will be added in the future).

- Then, simply use your new password (username is always `elastic`/`admin`) to connect to your ES/OS instance (respectfully) from VIVO application.
19 changes: 19 additions & 0 deletions elasticsearch/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM docker.elastic.co/elasticsearch/elasticsearch:8.9.0

# Simple, single node deployment
ENV discovery.type=single-node
ENV ES_JAVA_OPTS="-Xms512m -Xmx512m"
ENV xpack.security.enabled=false

# VIVO index name
ENV INDEX_NAME="vivo"

# Default es username
ENV ES_USERNAME="elastic"

EXPOSE 9200 9300

COPY ./elasticsearch/setup-index.sh /usr/share/elasticsearch/setup-index.sh
COPY index-config.json /usr/share/elasticsearch/index-config.json

CMD ["sh", "-c", "/usr/local/bin/docker-entrypoint.sh & sleep 15 && sh /usr/share/elasticsearch/setup-index.sh && wait"]
48 changes: 48 additions & 0 deletions elasticsearch/setup-index.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash


if [ -f /usr/share/elasticsearch/setup_done.flag ]; then
echo "Setup has already been completed, skipping setup."
exit 0
fi


SSL_ENABLED=$(curl -sk -X GET "https://localhost:9200")

if [ -z "$SSL_ENABLED" ]; then
echo "SSL not enabled, falling back to HTTP..."
SSL_ENABLED="false"
else
SSL_ENABLED="true"
echo "SSL is enabled"
fi

if [ "$SSL_ENABLED" == "true" ]; then
ES_URL="https://localhost:9200"
else
ES_URL="http://localhost:9200"
fi


ES_PASSWORD=$( /usr/share/elasticsearch/bin/elasticsearch-reset-password -u $ES_USERNAME --url $ES_URL --batch 2>&1 | grep -oP '(?<=New value: ).*' )

echo "The new password for the [$ES_USERNAME] user is: $ES_PASSWORD"


AUTH=""
if [[ -n "$ES_USERNAME" && -n "$ES_PASSWORD" ]]; then
AUTH="-u $ES_USERNAME:$ES_PASSWORD"
fi


until curl -k -s $AUTH -X GET "$ES_URL"; do
echo "Waiting for Elasticsearch to start..."
sleep 5
done

echo "Elasticsearch is up..."


curl -k $AUTH -X PUT "$ES_URL/$INDEX_NAME" -H 'Content-Type: application/json' -d @/usr/share/elasticsearch/index-config.json

touch /usr/share/elasticsearch/setup_done.flag
181 changes: 181 additions & 0 deletions index-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
{
"settings":{
"index":{
"analysis":{
"tokenizer":{
"keyword_tokenizer":{
"type":"keyword"
},
"whitespace_tokenizer":{
"type":"whitespace"
}
},
"filter":{
"lowercase_filter":{
"type":"lowercase"
},
"edgengram_filter":{
"type":"edge_ngram",
"min_gram":2,
"max_gram":25
},
"word_delimiter_filter":{
"type":"word_delimiter",
"generate_word_parts":true,
"generate_number_parts":true,
"catenate_words":false,
"catenate_numbers":false,
"catenate_all":false,
"split_on_case_change":true
},
"porter_stem_filter":{
"type":"snowball",
"language":"English"
}
},
"analyzer":{
"default":{
"type":"english"
},
"edgengram_untokenized":{
"type":"custom",
"tokenizer":"keyword_tokenizer",
"filter":[
"lowercase_filter",
"edgengram_filter"
]
},
"edgengram_untokenized_query":{
"type":"custom",
"tokenizer":"keyword_tokenizer",
"filter":[
"lowercase_filter"
]
},
"edgengram_stemmed":{
"type":"custom",
"tokenizer":"whitespace_tokenizer",
"filter":[
"word_delimiter_filter",
"lowercase_filter",
"porter_stem_filter",
"edgengram_filter"
]
},
"edgengram_stemmed_query":{
"type":"custom",
"tokenizer":"whitespace_tokenizer",
"filter":[
"word_delimiter_filter",
"lowercase_filter",
"porter_stem_filter"
]
}
}
}
}
},
"mappings":{
"dynamic_templates":[
{
"field_sort_template":{
"match":"*_label_sort",
"mapping":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword"
}
},
"fielddata":true
}
}
},
{
"field_ss_template":{
"match":"*_ss",
"mapping":{
"type":"text",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
},
"fielddata":true
}
}
},
{
"date_range_template":{
"match":"*_drsim",
"mapping":{
"type":"date_range",
"format":"strict_date_optional_time||epoch_millis"
}
}
}
],
"properties":{
"ALLTEXT":{
"type":"text",
"analyzer":"english",
"fields":{
"keyword":{
"type":"keyword",
"ignore_above":256
}
}
},
"ALLTEXTUNSTEMMED":{
"type":"text",
"analyzer":"standard"
},
"DocId":{
"type":"keyword"
},
"classgroup":{
"type":"keyword"
},
"type":{
"type":"keyword"
},
"mostSpecificTypeURIs":{
"type":"keyword"
},
"indexedTime":{
"type":"long"
},
"nameRaw":{
"type":"keyword"
},
"URI":{
"type":"keyword"
},
"THUMBNAIL":{
"type":"integer"
},
"THUMBNAIL_URL":{
"type":"keyword"
},
"nameLowercaseSingleValued":{
"type":"text",
"analyzer":"standard",
"fielddata":true
},
"BETA":{
"type":"float"
},
"acNameUntokenized":{
"type":"text",
"analyzer":"edgengram_untokenized",
"search_analyzer":"edgengram_untokenized_query"
},
"acNameStemmed":{
"type":"text",
"analyzer":"edgengram_stemmed",
"search_analyzer":"edgengram_stemmed_query"
}
}
}
}
22 changes: 22 additions & 0 deletions opensearch/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM opensearchproject/opensearch:2.18.0

# Simple, single node deployment
ENV discovery.type=single-node
ENV OPENSEARCH_JAVA_OPTS="-Xms512m -Xmx512m"
ENV plugins.security.disabled=true

# VIVO index name
ENV INDEX_NAME="vivo"

# Default OpenSearch username
ENV OS_USERNAME="admin"

# Provide strong initial password
ENV OPENSEARCH_INITIAL_ADMIN_PASSWORD="<STRONG_PASSWORD>"

EXPOSE 9200 9300

COPY /opensearch/setup-index.sh /usr/share/opensearch/setup-index.sh
COPY index-config.json /usr/share/opensearch/index-config.json

CMD ["sh", "-c", "/usr/share/opensearch/opensearch-docker-entrypoint.sh & sleep 15 && sh /usr/share/opensearch/setup-index.sh && wait"]
39 changes: 39 additions & 0 deletions opensearch/setup-index.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash


if [ -f /usr/share/opensearch/setup_done.flag ]; then
echo "Setup has already been completed, skipping setup."
exit 0
fi


SSL_ENABLED=$(curl -sk -X GET "https://localhost:9200")

if [ -z "$SSL_ENABLED" ]; then
echo "SSL not enabled, falling back to HTTP..."
SSL_ENABLED="false"
else
SSL_ENABLED="true"
echo "SSL is enabled"
fi

if [ "$SSL_ENABLED" == "true" ]; then
OS_URL="https://localhost:9200"
else
OS_URL="http://localhost:9200"
fi


AUTH="-u $OS_USERNAME:$OPENSEARCH_INITIAL_ADMIN_PASSWORD"

until curl -k -s $AUTH -X GET "$OS_URL"; do
echo "Waiting for OpenSearch to start..."
sleep 5
done

echo "OpenSearch is up..."


curl -k $AUTH -X PUT "$OS_URL/$INDEX_NAME" -H 'Content-Type: application/json' -d @/usr/share/opensearch/index-config.json

touch /usr/share/opensearch/setup_done.flag