diff --git a/samcli/lib/telemetry/project_metadata.py b/samcli/lib/telemetry/project_metadata.py index ac5818e737..c825db9022 100644 --- a/samcli/lib/telemetry/project_metadata.py +++ b/samcli/lib/telemetry/project_metadata.py @@ -2,11 +2,11 @@ Creates and encrypts metadata regarding SAM CLI projects. """ +import hashlib from os import getcwd import re import subprocess from typing import List, Optional -from uuid import uuid5, NAMESPACE_URL from samcli.cli.global_config import GlobalConfig @@ -18,7 +18,7 @@ def get_git_remote_origin_url() -> Optional[str]: Returns ------- str | None - A UUID5 encrypted string of the git remote origin url, formatted such that the + A SHA256 hexdigest string of the git remote origin url, formatted such that the encrypted value follows the pattern //.git. If telemetry is opted out of by the user, or the `.git` folder is not found (the directory is not a git repository), returns None @@ -46,7 +46,7 @@ def get_project_name() -> Optional[str]: Returns ------- str | None - A UUID5 encrypted string of either the name of the project, or the name of the + A SHA256 hexdigest string of either the name of the project, or the name of the current working directory that the command is running in. If telemetry is opted out of by the user, returns None """ @@ -72,7 +72,7 @@ def get_initial_commit_hash() -> Optional[str]: Returns ------- str | None - A UUID5 encrypted string of the git project's initial commit hash. + A SHA256 hexdigest string of the git project's initial commit hash. If telemetry is opted out of by the user, or the `.git` folder is not found (the directory is not a git repository), returns None. """ @@ -105,5 +105,7 @@ def _parse_remote_origin_url(url: str) -> List[str]: def _encrypt_value(value: str) -> str: - """Encrypt a string, and then return the encrypted value as a string.""" - return str(uuid5(NAMESPACE_URL, value)) + """Encrypt a string, and then return the encrypted value as a byte string.""" + h = hashlib.sha256() + h.update(value.encode("utf-8")) + return h.hexdigest() diff --git a/tests/unit/lib/telemetry/test_project_metadata.py b/tests/unit/lib/telemetry/test_project_metadata.py index 8bd6f485c3..b165e912fa 100644 --- a/tests/unit/lib/telemetry/test_project_metadata.py +++ b/tests/unit/lib/telemetry/test_project_metadata.py @@ -2,8 +2,8 @@ Module for testing the project_metadata.py methods. """ +import hashlib from subprocess import CompletedProcess, CalledProcessError -from uuid import uuid5, NAMESPACE_URL from unittest.mock import patch, Mock from unittest import TestCase @@ -49,7 +49,9 @@ def test_retrieve_git_origin(self, origin, expected, sp_mock): sp_mock.return_value = CompletedProcess(["git", "config", "--get", "remote.origin.url"], 0, stdout=origin) git_origin = get_git_remote_origin_url() - self.assertEqual(git_origin, str(uuid5(NAMESPACE_URL, expected))) + expected_hash = hashlib.sha256() + expected_hash.update(expected.encode("utf-8")) + self.assertEqual(git_origin, expected_hash.hexdigest()) @patch("samcli.lib.telemetry.project_metadata.subprocess.run") def test_retrieve_git_origin_when_not_a_repo(self, sp_mock): @@ -73,7 +75,9 @@ def test_retrieve_project_name_from_git(self, origin, expected, sp_mock): sp_mock.return_value = CompletedProcess(["git", "config", "--get", "remote.origin.url"], 0, stdout=origin) project_name = get_project_name() - self.assertEqual(project_name, str(uuid5(NAMESPACE_URL, expected))) + expected_hash = hashlib.sha256() + expected_hash.update(expected.encode("utf-8")) + self.assertEqual(project_name, expected_hash.hexdigest()) @parameterized.expand( [ @@ -94,7 +98,9 @@ def test_retrieve_project_name_from_dir(self, cwd, sp_mock, cwd_mock): cwd_mock.return_value = cwd project_name = get_project_name() - self.assertEqual(project_name, str(uuid5(NAMESPACE_URL, cwd.replace("\\", "/")))) + expected_hash = hashlib.sha256() + expected_hash.update(cwd.replace("\\", "/").encode("utf-8")) + self.assertEqual(project_name, expected_hash.hexdigest()) @parameterized.expand( [ @@ -108,4 +114,6 @@ def test_retrieve_initial_commit(self, git_hash, sp_mock): sp_mock.return_value = CompletedProcess(["git", "rev-list", "--max-parents=0", "HEAD"], 0, stdout=git_hash) initial_commit = get_initial_commit_hash() - self.assertEqual(initial_commit, str(uuid5(NAMESPACE_URL, git_hash))) + expected_hash = hashlib.sha256() + expected_hash.update(git_hash.encode("utf-8")) + self.assertEqual(initial_commit, expected_hash.hexdigest())