Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-UserAgent-47668.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "User Agent",
"description": "Ensure that session IDs are added to the User-Agent HTTP header even when the local AWS CLI cache does not exist."
}
9 changes: 5 additions & 4 deletions awscli/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ class CLISessionDatabaseConnection:
"""
_ENABLE_WAL = 'PRAGMA journal_mode=WAL'

def __init__(self, connection=None):
def __init__(self, connection=None, cache_dir=None):
self._cache_dir = cache_dir or _CACHE_DIR
self._ensure_cache_dir()
self._connection = connection or sqlite3.connect(
_CACHE_DIR / _DATABASE_FILENAME,
self._cache_dir / _DATABASE_FILENAME,
check_same_thread=False,
isolation_level=None,
)
self._ensure_cache_dir()
self._ensure_database_setup()

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

def _ensure_cache_dir(self):
_CACHE_DIR.mkdir(parents=True, exist_ok=True)
self._cache_dir.mkdir(parents=True, exist_ok=True)

def _ensure_database_setup(self):
self._create_session_table()
Expand Down
14 changes: 14 additions & 0 deletions tests/functional/test_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,20 @@ def test_ensure_database_setup(self, session_conn):
)
assert cursor.fetchall() == [('session',), ('host_id',)]

def test_creates_database_when_cache_dir_does_not_exist(self, tmp_path):
# When the cache directory doesn't exist, the connection should still
# be established successfully.
nonexistent_dir = tmp_path / 'nonexistent' / 'nested' / 'cache'
assert not nonexistent_dir.exists()
conn = CLISessionDatabaseConnection(cache_dir=nonexistent_dir)
assert nonexistent_dir.exists()
assert (nonexistent_dir / 'session.db').exists()
# Verify the database is functional.
writer = CLISessionDatabaseWriter(conn)
reader = CLISessionDatabaseReader(conn)
writer.write(CLISessionData('key', 'sid', 1000000000))
assert reader.read('key').session_id == 'sid'

def test_timeout_does_not_raise_exception(self, session_conn):
test_query = """
SELECT name
Expand Down
9 changes: 0 additions & 9 deletions tests/integration/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,15 +310,6 @@ def test_version(self):
)
self.assertTrue(re.fullmatch(user_agent_regex, version_output))

def test_version_does_not_create_cache_directory(self):
Comment thread
aemous marked this conversation as resolved.
# Regression test: --version should not create any files/directories.
with tempfile.TemporaryDirectory() as tmpdir:
env = os.environ.copy()
env['HOME'] = tmpdir
aws('--version', env_vars=env)
aws_dir = os.path.join(tmpdir, '.aws')
self.assertFalse(os.path.exists(aws_dir))

def test_version_with_exec_env(self):
base_env_vars = os.environ.copy()
base_env_vars['AWS_EXECUTION_ENV'] = 'an_execution_env'
Expand Down
Loading