diff --git a/samcli/commands/validate/validate.py b/samcli/commands/validate/validate.py index 059c94db56..40080a208f 100644 --- a/samcli/commands/validate/validate.py +++ b/samcli/commands/validate/validate.py @@ -4,11 +4,12 @@ import os import boto3 +from botocore.exceptions import NoCredentialsError import click from samtranslator.translator.managed_policy_translator import ManagedPolicyLoader - -from samcli.cli.main import pass_context, common_options as cli_framework_options +from samcli.commands.exceptions import UserException +from samcli.cli.main import pass_context, common_options as cli_framework_options, aws_creds_options from samcli.commands.local.cli_common.options import template_common_option as template_option from samcli.commands.local.cli_common.user_exceptions import InvalidSamTemplateException, SamTemplateNotFoundException from samcli.yamlhelper import yaml_parse @@ -19,6 +20,7 @@ @click.command("validate", short_help="Validate an AWS SAM template.") @template_option +@aws_creds_options @cli_framework_options @pass_context def cli(ctx, template): @@ -43,6 +45,8 @@ def do_cli(ctx, template): except InvalidSamDocumentException as e: click.secho("Template provided at '{}' was invalid SAM Template.".format(template), bg='red') raise InvalidSamTemplateException(str(e)) + except NoCredentialsError as e: + raise UserException("AWS Credentials are required. Please configure your credentials.") click.secho("{} is a valid SAM Template".format(template), fg='green') diff --git a/tests/unit/commands/validate/test_cli.py b/tests/unit/commands/validate/test_cli.py index d440eab06c..1fe75d5fe0 100644 --- a/tests/unit/commands/validate/test_cli.py +++ b/tests/unit/commands/validate/test_cli.py @@ -1,6 +1,9 @@ from unittest import TestCase from mock import Mock, patch +from botocore.exceptions import NoCredentialsError + +from samcli.commands.exceptions import UserException from samcli.commands.local.cli_common.user_exceptions import SamTemplateNotFoundException, InvalidSamTemplateException from samcli.commands.validate.lib.exceptions import InvalidSamDocumentException from samcli.commands.validate.validate import do_cli, _read_sam_file @@ -47,6 +50,21 @@ def test_template_fails_validation(self, read_sam_file_patch, click_patch, templ do_cli(ctx=None, template=template_path) + @patch('samcli.commands.validate.validate.SamTemplateValidator') + @patch('samcli.commands.validate.validate.click') + @patch('samcli.commands.validate.validate._read_sam_file') + def test_no_credentials_provided(self, read_sam_file_patch, click_patch, template_valiadator): + template_path = 'path_to_template' + read_sam_file_patch.return_value = {"a": "b"} + + is_valid_mock = Mock() + is_valid_mock.is_valid.side_effect = NoCredentialsError + template_valiadator.return_value = is_valid_mock + + with self.assertRaises(UserException): + do_cli(ctx=None, + template=template_path) + @patch('samcli.commands.validate.validate.SamTemplateValidator') @patch('samcli.commands.validate.validate.click') @patch('samcli.commands.validate.validate._read_sam_file')