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
10 changes: 9 additions & 1 deletion samcli/lib/config/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import logging
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any
from typing import Any, Dict, Type

import tomlkit
from ruamel.yaml import YAML, YAMLError
Expand Down Expand Up @@ -332,3 +332,11 @@ def put_comment(document: Any, comment: str) -> Any:
"""
document.update({COMMENT_KEY: comment})
return document


FILE_MANAGER_MAPPER: Dict[str, Type[FileManager]] = { # keys ordered by priority
".toml": TomlFileManager,
".yaml": YamlFileManager,
".yml": YamlFileManager,
".json": JsonFileManager,
}
22 changes: 9 additions & 13 deletions samcli/lib/config/samconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import logging
import os
from pathlib import Path
from typing import Any, Dict, Iterable, Type
from typing import Any, Iterable

from samcli.lib.config.exceptions import FileParseException, SamConfigFileReadException, SamConfigVersionException
from samcli.lib.config.file_manager import FileManager, JsonFileManager, TomlFileManager, YamlFileManager
from samcli.lib.config.file_manager import FILE_MANAGER_MAPPER
from samcli.lib.config.version import SAM_CONFIG_VERSION, VERSION_KEY
from samcli.lib.telemetry.event import EventTracker

LOG = logging.getLogger(__name__)

Expand All @@ -25,13 +26,6 @@ class SamConfig:
Class to represent `samconfig` config options.
"""

FILE_MANAGER_MAPPER: Dict[str, Type[FileManager]] = { # keys ordered by priority
".toml": TomlFileManager,
".yaml": YamlFileManager,
".yml": YamlFileManager,
".json": JsonFileManager,
}

def __init__(self, config_dir, filename=None):
"""
Initialize the class
Expand All @@ -46,16 +40,18 @@ def __init__(self, config_dir, filename=None):
"""
self.document = {}
self.filepath = Path(config_dir, filename or self.get_default_file(config_dir=config_dir))
self.file_manager = self.FILE_MANAGER_MAPPER.get(self.filepath.suffix, None)
file_extension = self.filepath.suffix
self.file_manager = FILE_MANAGER_MAPPER.get(file_extension, None)
if not self.file_manager:
LOG.warning(
f"The config file extension '{self.filepath.suffix}' is not supported. "
f"Supported formats are: [{'|'.join(self.FILE_MANAGER_MAPPER.keys())}]"
f"The config file extension '{file_extension}' is not supported. "
f"Supported formats are: [{'|'.join(FILE_MANAGER_MAPPER.keys())}]"
Comment on lines +47 to +48

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information

This expression logs [sensitive data (password)](1) as clear text.
)
raise SamConfigFileReadException(
f"The config file {self.filepath} uses an unsupported extension, and cannot be read."
)
self._read()
EventTracker.track_event("SamConfigFileExtension", file_extension)

def get_stage_configuration_names(self):
if self.document:
Expand Down Expand Up @@ -266,7 +262,7 @@ def get_default_file(config_dir: str) -> str:
config_files_found = 0
config_file = DEFAULT_CONFIG_FILE_NAME

for extension in reversed(list(SamConfig.FILE_MANAGER_MAPPER.keys())):
for extension in reversed(list(FILE_MANAGER_MAPPER.keys())):
filename = DEFAULT_CONFIG_FILE + extension
if Path(config_dir, filename).exists():
config_files_found += 1
Expand Down
3 changes: 3 additions & 0 deletions samcli/lib/telemetry/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from samcli.cli.context import Context
from samcli.lib.build.workflows import ALL_CONFIGS
from samcli.lib.config.file_manager import FILE_MANAGER_MAPPER
from samcli.lib.telemetry.telemetry import Telemetry
from samcli.local.common.runtime_template import INIT_RUNTIMES

Expand All @@ -26,6 +27,7 @@ class EventName(Enum):
SYNC_FLOW_START = "SyncFlowStart"
SYNC_FLOW_END = "SyncFlowEnd"
BUILD_WORKFLOW_USED = "BuildWorkflowUsed"
CONFIG_FILE_EXTENSION = "SamConfigFileExtension"


class UsedFeature(Enum):
Expand Down Expand Up @@ -69,6 +71,7 @@ class EventType:
EventName.SYNC_FLOW_START: _SYNC_FLOWS,
EventName.SYNC_FLOW_END: _SYNC_FLOWS,
EventName.BUILD_WORKFLOW_USED: _WORKFLOWS,
EventName.CONFIG_FILE_EXTENSION: list(FILE_MANAGER_MAPPER.keys()),
}

@staticmethod
Expand Down
7 changes: 5 additions & 2 deletions tests/integration/telemetry/test_experimental_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,11 @@ def test_must_send_not_experimental_metrics_if_not_experimental(self):

self.assertEqual(process.returncode, 2, "Command should fail")
all_requests = server.get_all_requests()
self.assertEqual(1, len(all_requests), "Command run metric must be sent")
request = all_requests[0]
self.assertEqual(2, len(all_requests), "Command run and event metrics must be sent")
# NOTE: Since requests happen asynchronously, we cannot guarantee whether the
# commandRun metric will be first or second, so we sort for consistency.
all_requests.sort(key=lambda x: list(x["data"]["metrics"][0].keys())[0])
request = all_requests[0] # "commandRun" comes before "events"
self.assertIn("Content-Type", request["headers"])
self.assertEqual(request["headers"]["Content-Type"], "application/json")

Expand Down
4 changes: 3 additions & 1 deletion tests/integration/telemetry/test_installed_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def test_send_installed_metric_on_first_run(self):
self.assertIn(EXPECTED_TELEMETRY_PROMPT, stderrdata.decode())

all_requests = server.get_all_requests()
self.assertEqual(2, len(all_requests), "There should be exactly two metrics request")
self.assertEqual(
3, len(all_requests), "There should be exactly three metrics request"
) # 3 = 2 expected + events

# First one is usually the installed metric
requests = filter_installed_metric_requests(all_requests)
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/telemetry/test_telemetry_contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ def test_must_not_send_metrics_if_disabled_using_envvar(self):

self.assertEqual(process.returncode, 0, "Command should successfully complete")
all_requests = server.get_all_requests()
self.assertEqual(1, len(all_requests), "Command run metric should be sent")
self.assertEqual(
2, len(all_requests), "Command run and event metrics should be sent"
) # 2 = cmd_run + events

def test_must_send_metrics_if_enabled_via_envvar(self):
"""
Expand All @@ -52,7 +54,7 @@ def test_must_send_metrics_if_enabled_via_envvar(self):

self.assertEqual(process.returncode, 0, "Command should successfully complete")
all_requests = server.get_all_requests()
self.assertEqual(1, len(all_requests), "Command run metric must be sent")
self.assertEqual(2, len(all_requests), "Command run and event metrics must be sent") # cmd_run + events

def test_must_not_crash_when_offline(self):
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/cli/test_cli_config_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

from samcli.commands.exceptions import ConfigException
from samcli.cli.cli_config_file import ConfigProvider, configuration_option, configuration_callback, get_ctx_defaults
from samcli.lib.config.exceptions import FileParseException, SamConfigFileReadException, SamConfigVersionException
from samcli.lib.config.samconfig import DEFAULT_ENV, TomlFileManager
from samcli.lib.config.exceptions import SamConfigFileReadException, SamConfigVersionException
from samcli.lib.config.samconfig import DEFAULT_ENV

from tests.testing_utils import IS_WINDOWS

Expand Down
24 changes: 17 additions & 7 deletions tests/unit/lib/samconfig/test_samconfig.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import os
from pathlib import Path
from unittest.mock import patch
from parameterized import parameterized
import tempfile
from unittest import TestCase

from samcli.lib.config.exceptions import SamConfigFileReadException, SamConfigVersionException
from samcli.lib.config.file_manager import JsonFileManager, TomlFileManager, YamlFileManager
from samcli.lib.config.file_manager import FILE_MANAGER_MAPPER, JsonFileManager, TomlFileManager, YamlFileManager
from samcli.lib.config.samconfig import (
DEFAULT_CONFIG_FILE,
SamConfig,
Expand All @@ -14,6 +15,7 @@
DEFAULT_ENV,
)
from samcli.lib.config.version import VERSION_KEY, SAM_CONFIG_VERSION
from samcli.lib.telemetry.event import Event
from samcli.lib.utils import osutils


Expand Down Expand Up @@ -256,7 +258,7 @@ def test_config_uses_default_if_none_provided(self):

def test_config_priority(self):
config_files = []
extensions_in_priority = list(SamConfig.FILE_MANAGER_MAPPER.keys()) # priority by order in dict
extensions_in_priority = list(FILE_MANAGER_MAPPER.keys()) # priority by order in dict
for extension in extensions_in_priority:
filename = DEFAULT_CONFIG_FILE + extension
config = SamConfig(self.config_dir, filename=filename)
Expand Down Expand Up @@ -290,16 +292,24 @@ def test_file_manager_unsupported(self):

@parameterized.expand(
[
("samconfig.toml", TomlFileManager),
("samconfig.yaml", YamlFileManager),
("samconfig.yml", YamlFileManager),
("samconfig.json", JsonFileManager),
("samconfig.toml", TomlFileManager, ".toml"),
("samconfig.yaml", YamlFileManager, ".yaml"),
("samconfig.yml", YamlFileManager, ".yml"),
("samconfig.json", JsonFileManager, ".json"),
]
)
def test_file_manager(self, filename, expected_file_manager):
@patch("samcli.lib.telemetry.event.EventTracker.track_event")
def test_file_manager(self, filename, expected_file_manager, expected_extension, track_mock):
config_dir = tempfile.gettempdir()
config_path = Path(config_dir, filename)
tracked_events = []

def mock_tracker(name, value): # when track_event is called, just append the Event to our list
tracked_events.append(Event(name, value))

track_mock.side_effect = mock_tracker

samconfig = SamConfig(config_path, filename=filename)

self.assertIs(samconfig.file_manager, expected_file_manager)
self.assertIn(Event("SamConfigFileExtension", expected_extension), tracked_events)