Skip to content

Commit

Permalink
Add region to !cf_output tag (#56)
Browse files Browse the repository at this point in the history
Co-authored-by: plinio.menarin <[email protected]>
  • Loading branch information
plinioh and plinioh authored Sep 5, 2023
1 parent 90680a6 commit 058b374
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Changed

- Update yaml tag `tags.output_stack.OutputStackTag` to work with custom aws region. [issue-54](https://github.com/lucasvieirasilva/aws-ssm-secrets-cli/issues/54)

## [2.2.0] - 2023-08-21

### Nonfunctional
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ For naming convention, you should give the environment name for the file name (e
```yaml
kms:
arn: KMS_KEY_ARN (String) #Required
encryption_sdk: 'aws_encryption_sdk'
encryption_sdk: "aws_encryption_sdk"
parameters:
- name: myparametername
value: "MySecretValueHere"
Expand All @@ -115,7 +115,7 @@ or AWS Secrets manager with object
```yaml
kms:
arn: KMS_KEY_ARN (String) #Required
encryption_sdk: 'aws_encryption_sdk'
encryption_sdk: "aws_encryption_sdk"
parameters:
- name: myparametername
value: "MySecretValueHere"
Expand Down Expand Up @@ -186,7 +186,7 @@ aws-secrets decrypt -e dev.yaml --output dev.yaml --profile myprofile --region e
```yaml
kms:
arn: KMS_KEY_ARN
encryption_sdk: 'aws_encryption_sdk'
encryption_sdk: "aws_encryption_sdk"
parameters:
- name: myparametername
value: "MySecretValueHere"
Expand Down Expand Up @@ -389,11 +389,11 @@ This resolver is designed to load a file content to the SSM Parameter or Secrets
Example:

```yaml
...
---
secrets:
- name: mysecret
value: !file myfile.txt
...
```

##### !cf_output
Expand All @@ -411,6 +411,15 @@ parameters:
value: !cf_output "mystack.MyOutputKey"
```

```yaml
kms:
arn: !cf_output "mystack.MyOutputKey.us-east-1"
parameters:
- name: myparameter-name
type: String
value: !cf_output "mystack.MyOutputKey.us-east-1"
```

##### !cmd

This resolver can be used in `parameters[*].value` and `secrets[*].value` properties.
Expand Down
3 changes: 2 additions & 1 deletion aws_secrets/miscellaneous/cloudformation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from aws_secrets.helpers.catch_exceptions import CLIError
from botocore.session import Session

from aws_secrets.helpers.catch_exceptions import CLIError


def get_output_value(
session: Session,
Expand Down
19 changes: 15 additions & 4 deletions aws_secrets/tags/output_stack.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import yaml
from yaml.dumper import SafeDumper
from yaml.nodes import ScalarNode

from aws_secrets.helpers.catch_exceptions import CLIError
from aws_secrets.miscellaneous import cloudformation
from aws_secrets.miscellaneous.session import session
Expand All @@ -13,6 +14,7 @@ class OutputStackTag(yaml.YAMLObject):
Examples:
>>> !cf_output <stack>.<output-name>
>>> !cf_output <stack>.<output-name>.<aws-region>
value: <stack-output-value>
Args:
Expand All @@ -34,13 +36,22 @@ def __repr__(self) -> str:
`str`: stack output value
"""
stack_args = self.stack.split('.')
if len(stack_args) != 2:
if len(stack_args) < 2 or len(stack_args) > 3:
raise CLIError(
f'value {self.stack} is invalid, the correct way to ' +
'fill this information is <stack-name>.<output-name>')

(
f'value {self.stack} is invalid, the correct way to '
'fill this information is <stack-name>.<output-name> '
'or <stack-name>.<output-name>.<aws_region>'
)
)
region = None
stack_name = stack_args[0]
output_name = stack_args[1]
try:
region = stack_args[2]
session.aws_region = region
except IndexError:
pass

return cloudformation.get_output_value(session(), stack_name, output_name)

Expand Down
31 changes: 29 additions & 2 deletions tests/tags/test_output_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest
import yaml

from aws_secrets.helpers.catch_exceptions import CLIError
from aws_secrets.tags.output_stack import OutputStackTag

Expand Down Expand Up @@ -29,6 +30,29 @@ def test_cf_output_yaml_tag(
mock_get_output_value.assert_called_once_with(ANY, 'stack', 'output')


@patch('aws_secrets.miscellaneous.cloudformation.get_output_value')
def test_cf_output_yaml_tag_with_region(
mock_get_output_value
):
"""
Should resolve the cloudformation stack output value
"""
output = 'myvalue'

mock_get_output_value.return_value = output

yaml.SafeLoader.add_constructor('!cf_output', OutputStackTag.from_yaml)
yaml.SafeDumper.add_multi_representer(
OutputStackTag, OutputStackTag.to_yaml)

data = yaml.safe_load("""
key: !cf_output stack.output.region
""")

assert str(data['key']) == output
mock_get_output_value.assert_called_once_with(ANY, 'stack', 'output')


@patch('aws_secrets.miscellaneous.cloudformation.get_output_value')
def test_cf_output_yaml_tag_invalid_format(
mock_get_output_value
Expand All @@ -51,8 +75,11 @@ def test_cf_output_yaml_tag_invalid_format(
with pytest.raises(CLIError) as error:
print(str(data['key']))

assert 'value stack is invalid, the correct way to ' + \
'fill this information is <stack-name>.<output-name>' == str(error.value)
assert (
'value stack is invalid, the correct way to '
'fill this information is <stack-name>.<output-name> '
'or <stack-name>.<output-name>.<aws_region>'
) == str(error.value)
mock_get_output_value.assert_not_called()


Expand Down

0 comments on commit 058b374

Please sign in to comment.