Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 8 additions & 6 deletions samcli/lib/telemetry/project_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 <hostname>/<owner>/<project_name>.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
Expand Down Expand Up @@ -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
"""
Expand All @@ -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.
"""
Expand Down Expand Up @@ -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()
18 changes: 13 additions & 5 deletions tests/unit/lib/telemetry/test_project_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand All @@ -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(
[
Expand All @@ -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(
[
Expand All @@ -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())