Skip to content

Commit

Permalink
Minor fixes for agent component (#2839)
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-shelkovnikov authored Sep 19, 2024
1 parent e6f8650 commit ce5a4c1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ docker-push:

## Agent Docker Zone
# Only use it for local testing, that's it
AGENT_ES_HOSTS?=http://127.0.0.1:9200
AGENT_ES_HOSTS?=[http://127.0.0.1:9200]
AGENT_ES_USERNAME?=elastic
AGENT_ES_PASSWORD?=changeme
AGENT_DOCKERFILE_NAME?=Dockerfile.agent
Expand Down
12 changes: 11 additions & 1 deletion connectors/agent/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
# or more contributor license agreements. Licensed under the Elastic License 2.0;
# you may not use this file except in compliance with the Elastic License 2.0.
#
import base64

from connectors.config import add_defaults


Expand All @@ -24,6 +26,9 @@ def __init__(self):
"""
self._default_config = {
"_force_allow_native": True,
"service": {
"_use_native_connector_api_keys": False,
},
"native_service_types": [
"azure_blob_storage",
"box",
Expand Down Expand Up @@ -74,7 +79,12 @@ def try_update(self, source):
}

if source.fields.get("api_key"):
es_creds["api_key"] = source["api_key"]
api_key = source["api_key"]
# if beats_logstash_format we need to base64 the key
if ":" in api_key:
api_key = base64.b64encode(api_key.encode()).decode()

es_creds["api_key"] = api_key
elif source.fields.get("username") and source.fields.get("password"):
es_creds["username"] = source["username"]
es_creds["password"] = source["password"]
Expand Down
18 changes: 18 additions & 0 deletions tests/agent/test_agent_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ async def test_try_update_with_api_key_auth_data():
assert config_wrapper.get()["elasticsearch"]["api_key"] == api_key


@pytest.mark.asyncio
async def test_try_update_with_non_encoded_api_key_auth_data():
hosts = ["https://localhost:9200"]
api_key = "something:else"
encoded = "c29tZXRoaW5nOmVsc2U="

config_wrapper = ConnectorsAgentConfigurationWrapper()
source_mock = MagicMock()
fields_container = {"hosts": hosts, "api_key": api_key}

source_mock.fields = fields_container
source_mock.__getitem__.side_effect = fields_container.__getitem__

assert config_wrapper.try_update(source_mock) is True
assert config_wrapper.get()["elasticsearch"]["host"] == hosts[0]
assert config_wrapper.get()["elasticsearch"]["api_key"] == encoded


@pytest.mark.asyncio
async def test_try_update_with_basic_auth_auth_data():
hosts = ["https://localhost:9200"]
Expand Down

0 comments on commit ce5a4c1

Please sign in to comment.