Skip to content

Commit

Permalink
Merge pull request #190 from gocept/fix-28/duplicate-attribs
Browse files Browse the repository at this point in the history
Fail if an attribute is set both in environment and via secrets.
  • Loading branch information
ctheune authored Jun 25, 2021
2 parents 306182c + a9c40f4 commit e422f6c
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.installed.cfg
.pytest_cache
.vagrant
/.tox
/bin
/develop-eggs
/doc/build
Expand Down
8 changes: 6 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
2.3b2 (unreleased)
------------------

- NetLoc objects are now comparable. (#88)
- NetLoc objects are now comparable.
([#88](https://github.com/flyingcircusio/batou/issues/88))

- Render a better error message if gpg failed to decrypt a secrets file.
([#123](https://github.com/flyingcircusio/batou/issues/123))

- Support `ls` syntax in mode attributes. (#61)
- Support `ls` syntax in mode attributes.
([#61](https://github.com/flyingcircusio/batou/issues/61))

- Fail if an attribute is set both in environment and via secrets.
([#28](https://github.com/flyingcircusio/batou/issues/28))
- Raise exception when calling `batou secrets add` or `batou secrets remove`
with an unknown environment name.
([#143](https://github.com/flyingcircusio/batou/issues/143))
Expand Down
1 change: 1 addition & 0 deletions examples/errors/components/component1/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class Component1(Component):

do_what_is_needed = Attribute("literal", None)
my_attribute = None


class Component2(Component):
Expand Down
1 change: 1 addition & 0 deletions examples/errors/environments/errors.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ localhost = component1, component2, component3, component4, missingcomponent, cy

[component:component1]
do_what_is_needed = false
my_attribute = set in environment

[component:component2]
this_does_not_exist = True
Expand Down
Binary file modified examples/errors/secrets/errors.cfg
Binary file not shown.
1 change: 1 addition & 0 deletions examples/errors2/components/component1/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
class Component1(Component):

do_what_is_needed = Attribute("literal", None)
my_attribute = None


class Component2(Component):
Expand Down
22 changes: 22 additions & 0 deletions src/batou/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,28 @@ def report(self):
# TODO provide traceback in debug output


class DuplicateOverride(ConfigurationError):
"""An override for a component attribute was found both in the secrets and
in the environment configuration."""

sort_key = (0,)

def __init__(self, component, attribute):
self.component = component
self.attribute = attribute

def __str__(self):
return (f'A value {self.component}.{self.attribute} is defined both in'
' environment and secrets.')

def report(self):
output.error(
"Attribute override found both in environment and secrets")
output.tabular("Component", self.component, red=True)
output.tabular("Attribute", self.attribute, red=True)
# TODO provide traceback in debug output


class CycleErrorDetected(ConfigurationError):
"""We think we found a cycle in the component dependencies.
"""
Expand Down
13 changes: 9 additions & 4 deletions src/batou/secrets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
"""

from batou import SuperfluousSecretsSection
from .encryption import EncryptedConfigFile
from batou import DuplicateOverride
from batou import SuperfluousSecretsSection
import os.path
import glob

Expand Down Expand Up @@ -50,9 +51,13 @@ def add_secrets_to_environment(environment):
if component not in environment.components:
environment.exceptions.append(
SuperfluousSecretsSection(component))
o = environment.overrides.setdefault(component, {})
o.update(((k, o.value)
for k, o in config_file.config.items(section_)))
overrides = environment.overrides.setdefault(component, {})
for k, v in config_file.config.items(section_):
if k in overrides:
environment.exceptions.append(
DuplicateOverride(component, k))
else:
overrides[k] = v.value

# additional_secrets
prefix = "secrets/{}-".format(environment.name)
Expand Down
4 changes: 4 additions & 0 deletions src/batou/tests/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def test_main_with_errors(capsys):
ERROR: Override section for unknown component found
Component: nonexisting-component-section
ERROR: Attribute override found both in environment and secrets
Component: component1
Attribute: my_attribute
ERROR: Secrets section for unknown component found
Component: another-nonexisting-component-section
======================= DEPLOYMENT FAILED (during load) ========================
Expand Down
4 changes: 4 additions & 0 deletions src/batou/tests/test_endtoend.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def test_example_errors_early():
ERROR: Override section for unknown component found
Component: nonexisting-component-section
ERROR: Attribute override found both in environment and secrets
Component: component1
Attribute: my_attribute
ERROR: Secrets section for unknown component found
Component: another-nonexisting-component-section
======================= DEPLOYMENT FAILED (during load) ========================
Expand Down

0 comments on commit e422f6c

Please sign in to comment.