Skip to content

Commit 103930d

Browse files
committed
feat: configure elasticsearch to authenticate with an apiKey
1 parent 43e7ff9 commit 103930d

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

.github/workflows/CI-build-test.yml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,38 @@ jobs:
3737
uses: elastic/elastic-github-actions/elasticsearch@master
3838
with:
3939
stack-version: 8.15.3
40-
security-enabled: false
40+
security-enabled: true
41+
elasticsearch_password: elasticsearch-password
4142
plugins: analysis-kuromoji
4243

4344
- name: Elasticsearch is reachable
4445
run: |
45-
curl --verbose --show-error http://localhost:9200
46+
curl --verbose --show-error --fail -k -u elastic:elasticsearch-password http://localhost:9200
47+
48+
- name: Create Elasticsearch API Key
49+
id: create_key
50+
run: |
51+
API_KEY_CREATE_RESPONSE=$(
52+
curl -u "elastic:elasticsearch-password" \
53+
-X POST "http://localhost:9200/_security/api_key" \
54+
-H "Content-Type: application/json" \
55+
-d '{
56+
"name": "main-key",
57+
"expiration": "1825d",
58+
"role_descriptors": {
59+
"main-role": {
60+
"cluster": ["monitor"],
61+
"indices": [
62+
{
63+
"names": ["test_*"],
64+
"privileges": ["write", "read", "manage"]
65+
}
66+
]
67+
}
68+
}
69+
}')
70+
ENCODED_KEY=$(echo $API_KEY_CREATE_RESPONSE | jq -r '.encoded')
71+
echo "INAT_ES_API_KEY=$ENCODED_KEY" >> $GITHUB_ENV
4672
4773
- name: Copy config
4874
run: |

config.js.ci

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
const {
2+
INAT_ES_API_KEY
3+
} = process.env;
4+
15
module.exports = {
26
apiURL: "http://localhost:3000",
37
currentVersionURL: "http://localhost:4000/v1",
48
elasticsearch: {
5-
host: "http://localhost:9200"
9+
host: "http://localhost:9200",
10+
apiKey: INAT_ES_API_KEY
611
},
712
database: {
813
user: "postgres",

config_example.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
INAT_DB_USER,
66
INAT_DB_PASS,
77
INAT_ES_HOST,
8+
INAT_ES_API_KEY,
89
INAT_REDIS_HOST,
910
INAT_RAILS_URL
1011
} = process.env;
@@ -17,7 +18,8 @@ module.exports = {
1718
apiHostSSL: false,
1819
writeHostSSL: false,
1920
elasticsearch: {
20-
host: INAT_ES_HOST ? `http://${INAT_ES_HOST}:9200` : "http://localhost:9200"
21+
host: INAT_ES_HOST ? `http://${INAT_ES_HOST}:9200` : "http://localhost:9200",
22+
apiKey: INAT_ES_API_KEY || "apiKey"
2123
},
2224
// Note that the database name will be inferred from the NODE_ENV
2325
// environment variable, e.g. `inaturalist_${process.env.NODE_ENV}`, or it

lib/es_client.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const config = require( "../config" );
66
const util = require( "./util" );
77

88
const esClient = { connection: null };
9-
const { host, hosts } = config.elasticsearch;
9+
const { host, hosts, apiKey } = config.elasticsearch;
1010

1111
esClient.connect = ( ) => {
1212
if ( esClient.connection ) { return esClient.connection; }
@@ -21,6 +21,7 @@ esClient.connect = ( ) => {
2121
clientConfig.nodeSelector = "random";
2222
clientConfig.requestTimeout = 60000;
2323
clientConfig.maxRetries = 1;
24+
clientConfig.auth = { apiKey };
2425
esClient.connection = new Client( clientConfig );
2526

2627
esClient.connection.diagnostic.on( "response", ( err, result ) => {

0 commit comments

Comments
 (0)