Skip to content

Commit 5d2fa11

Browse files
authored
use json to manage local workload identity and user id (#37)
1 parent a15ecb8 commit 5d2fa11

File tree

4 files changed

+60
-51
lines changed

4 files changed

+60
-51
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ cython_debug/
167167
# macOS
168168
.DS_Store
169169
.agentcore.yaml
170+
.agentcore.json
170171
.AppleDouble
171172
.LSOverride
172173

src/bedrock_agentcore/identity/auth.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,17 @@ async def _get_workload_access_token(client: IdentityClient) -> str:
157157

158158

159159
async def _set_up_local_auth(client: IdentityClient) -> str:
160+
import json
160161
import uuid
161162
from pathlib import Path
162163

163-
import yaml
164-
165-
config_path = Path(".agentcore.yaml")
164+
config_path = Path(".agentcore.json")
166165
workload_identity_name = None
167166
config = {}
168167
if config_path.exists():
169168
try:
170169
with open(config_path, "r", encoding="utf-8") as file:
171-
config = yaml.safe_load(file) or {}
170+
config = json.load(file) or {}
172171
except Exception:
173172
print("Could not find existing workload identity and user id")
174173

@@ -189,7 +188,7 @@ async def _set_up_local_auth(client: IdentityClient) -> str:
189188
try:
190189
config = {"workload_identity_name": workload_identity_name, "user_id": user_id}
191190
with open(config_path, "w", encoding="utf-8") as file:
192-
yaml.dump(config, file, default_flow_style=False, indent=2)
191+
json.dump(config, file, indent=2)
193192
except Exception:
194193
print("Warning: could not write the created workload identity to file")
195194

tests/bedrock_agentcore/identity/test_auth.py

Lines changed: 51 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Tests for Bedrock AgentCore authentication decorators and functions."""
22

3+
import json
34
import os
4-
from unittest.mock import AsyncMock, Mock, mock_open, patch
5+
from unittest.mock import AsyncMock, Mock, patch
56

67
import pytest
78

@@ -332,60 +333,67 @@ class TestSetUpLocalAuth:
332333
"""Test _set_up_local_auth function."""
333334

334335
@pytest.mark.asyncio
335-
async def test_existing_config(self):
336+
async def test_existing_config(self, tmp_path):
336337
"""Test when config file exists with both workload_identity_name and user_id."""
337338
config_content = {"workload_identity_name": "existing-workload-123", "user_id": "existing-user-456"}
338339
mock_client = Mock()
339340
mock_client.get_workload_access_token = Mock(return_value={"workloadAccessToken": "test-access-token-456"})
340341

341-
with patch("pathlib.Path") as mock_path_class:
342-
mock_path = Mock()
343-
mock_path.exists.return_value = True
344-
mock_path.absolute.return_value = "/test/.agentcore.yaml"
345-
mock_path_class.return_value = mock_path
346-
347-
with patch("builtins.open", mock_open()):
348-
with patch("yaml.safe_load", return_value=config_content):
349-
result = await _set_up_local_auth(mock_client)
350-
351-
# Should use existing workload identity and user_id
352-
assert result == "test-access-token-456"
353-
mock_client.create_workload_identity.assert_not_called()
354-
mock_client.get_workload_access_token.assert_called_once_with(
355-
"existing-workload-123", user_id="existing-user-456"
356-
)
342+
# Create the config file in the temp directory
343+
config_file = tmp_path / ".agentcore.json"
344+
config_file.write_text(json.dumps(config_content))
345+
346+
# Change to the temp directory for the test
347+
import os
348+
349+
original_dir = os.getcwd()
350+
try:
351+
os.chdir(tmp_path)
352+
result = await _set_up_local_auth(mock_client)
353+
354+
# Should use existing workload identity and user_id
355+
assert result == "test-access-token-456"
356+
mock_client.create_workload_identity.assert_not_called()
357+
mock_client.get_workload_access_token.assert_called_once_with(
358+
"existing-workload-123", user_id="existing-user-456"
359+
)
360+
finally:
361+
os.chdir(original_dir)
357362

358363
@pytest.mark.asyncio
359-
async def test_no_config(self):
364+
async def test_no_config(self, tmp_path):
360365
"""Test when config file doesn't exist."""
361366
mock_client = Mock()
362367
mock_client.create_workload_identity = Mock(return_value={"name": "test-workload-123"})
363368
mock_client.get_workload_access_token = Mock(return_value={"workloadAccessToken": "test-access-token-456"})
364369

365-
with patch("pathlib.Path") as mock_path_class:
366-
mock_path = Mock()
367-
mock_path.exists.return_value = False
368-
mock_path_class.return_value = mock_path
369-
370-
with patch("builtins.open", mock_open()):
371-
with patch("yaml.dump") as mock_yaml_dump:
372-
with patch("uuid.uuid4") as mock_uuid:
373-
mock_uuid.return_value.hex = "abcd1234efgh5678"
374-
375-
result = await _set_up_local_auth(mock_client)
376-
377-
# Should create new workload identity and user_id
378-
assert result == "test-access-token-456"
379-
mock_client.create_workload_identity.assert_called_once()
380-
mock_client.get_workload_access_token.assert_called_once_with(
381-
"test-workload-123", user_id="abcd1234"
382-
)
383-
384-
# Should create and save new config
385-
mock_yaml_dump.assert_called_once()
386-
saved_config = mock_yaml_dump.call_args[0][0]
387-
assert saved_config["workload_identity_name"] == "test-workload-123"
388-
assert saved_config["user_id"] == "abcd1234"
370+
# Change to the temp directory for the test
371+
import os
372+
373+
original_dir = os.getcwd()
374+
try:
375+
os.chdir(tmp_path)
376+
377+
with patch("uuid.uuid4") as mock_uuid:
378+
mock_uuid.return_value.hex = "abcd1234efgh5678"
379+
380+
result = await _set_up_local_auth(mock_client)
381+
382+
# Should create new workload identity and user_id
383+
assert result == "test-access-token-456"
384+
mock_client.create_workload_identity.assert_called_once()
385+
mock_client.get_workload_access_token.assert_called_once_with("test-workload-123", user_id="abcd1234")
386+
387+
# Verify that the config file was created
388+
config_file = tmp_path / ".agentcore.json"
389+
assert config_file.exists()
390+
391+
# Verify the config file content
392+
saved_config = json.loads(config_file.read_text())
393+
assert saved_config["workload_identity_name"] == "test-workload-123"
394+
assert saved_config["user_id"] == "abcd1234"
395+
finally:
396+
os.chdir(original_dir)
389397

390398

391399
class TestGetRegion:

tests_integ/identity/test_auth_flows.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ async def need_api_key(*, api_key: str):
3030
print(f"received api key for async func: {api_key}")
3131

3232

33-
asyncio.run(need_api_key(api_key=""))
34-
asyncio.run(need_token_2LO_async(access_token=""))
35-
asyncio.run(need_token_3LO_async(access_token=""))
33+
if __name__ == "__main__":
34+
asyncio.run(need_api_key(api_key=""))
35+
asyncio.run(need_token_2LO_async(access_token=""))
36+
asyncio.run(need_token_3LO_async(access_token=""))

0 commit comments

Comments
 (0)