Skip to content

Commit 8480b95

Browse files
committed
Add Session IDs to User-Agent header even when cache does not exist.
1 parent 458acc4 commit 8480b95

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "bugfix",
3+
"category": "User Agent",
4+
"description": "Ensure that session IDs are added to the User-Agent HTTP header even when the local AWS CLI cache does not exist."
5+
}

awscli/telemetry.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ class CLISessionDatabaseConnection:
7676
"""
7777
_ENABLE_WAL = 'PRAGMA journal_mode=WAL'
7878

79-
def __init__(self, connection=None):
79+
def __init__(self, connection=None, cache_dir=None):
80+
self._cache_dir = cache_dir or _CACHE_DIR
81+
self._ensure_cache_dir()
8082
self._connection = connection or sqlite3.connect(
81-
_CACHE_DIR / _DATABASE_FILENAME,
83+
self._cache_dir / _DATABASE_FILENAME,
8284
check_same_thread=False,
8385
isolation_level=None,
8486
)
85-
self._ensure_cache_dir()
8687
self._ensure_database_setup()
8788

8889
def execute(self, query, *parameters):
@@ -95,7 +96,7 @@ def execute(self, query, *parameters):
9596
return sqlite3.Cursor(self._connection)
9697

9798
def _ensure_cache_dir(self):
98-
_CACHE_DIR.mkdir(parents=True, exist_ok=True)
99+
self._cache_dir.mkdir(parents=True, exist_ok=True)
99100

100101
def _ensure_database_setup(self):
101102
self._create_session_table()

tests/functional/test_telemetry.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,20 @@ def test_ensure_database_setup(self, session_conn):
107107
)
108108
assert cursor.fetchall() == [('session',), ('host_id',)]
109109

110+
def test_creates_database_when_cache_dir_does_not_exist(self, tmp_path):
111+
# When the cache directory doesn't exist, the connection should still
112+
# be established successfully.
113+
nonexistent_dir = tmp_path / 'nonexistent' / 'nested' / 'cache'
114+
assert not nonexistent_dir.exists()
115+
conn = CLISessionDatabaseConnection(cache_dir=nonexistent_dir)
116+
assert nonexistent_dir.exists()
117+
assert (nonexistent_dir / 'session.db').exists()
118+
# Verify the database is functional.
119+
writer = CLISessionDatabaseWriter(conn)
120+
reader = CLISessionDatabaseReader(conn)
121+
writer.write(CLISessionData('key', 'sid', 1000000000))
122+
assert reader.read('key').session_id == 'sid'
123+
110124
def test_timeout_does_not_raise_exception(self, session_conn):
111125
test_query = """
112126
SELECT name

0 commit comments

Comments
 (0)