Skip to content

Commit

Permalink
Merge pull request #452 from dgraeber/feature/resolved-param-check
Browse files Browse the repository at this point in the history
adding resolved env and global params to checksum
  • Loading branch information
dgraeber authored Nov 2, 2023
2 parents 0f8cbd8 + b808670 commit f29f039
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 38 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 @@ This project adheres to [Semantic Versioning](http://semver.org/) and [Keep a Ch
## Unreleased

### New
- BREAKING CHANGE*** - the checksum calculation used to trigger module redeploy has changed
- Existing deployed modules may incur a redeployment when going from a previous version
- resolve global/regional parameters when calculating checksum for individual module redeploy
- resolve env parameters when calculating checksum for individual module redeploy

### Changes

Expand Down
41 changes: 4 additions & 37 deletions seedfarmer/commands/_deployment_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
import seedfarmer.checksum as checksum
import seedfarmer.errors
import seedfarmer.mgmt.deploy_utils as du
import seedfarmer.mgmt.module_info as mi
from seedfarmer import commands, config
from seedfarmer.commands._parameter_commands import load_parameter_values
from seedfarmer.commands._parameter_commands import load_parameter_values, resolve_params_for_checksum
from seedfarmer.mgmt.module_info import (
get_deployspec_path,
get_module_metadata,
Expand Down Expand Up @@ -592,41 +591,9 @@ def deploy_deployment(
data_files=module.data_files,
excluded_files=md5_excluded_module_files,
)

for param in module.parameters:
if param.value_from and param.value_from.parameter_store:
if ":" in param.value_from.parameter_store:
raise seedfarmer.errors.InvalidConfigurationError(
f"CodeBuild does not support Versioned SSM Parameters -- see {group.name}-{module.name}"
)
param.version = mi.get_ssm_parameter_version(
ssm_parameter_name=param.value_from.parameter_store,
session=SessionManager()
.get_or_create()
.get_deployment_session(
account_id=cast(str, module.get_target_account_id()),
region_name=cast(str, module.target_region),
),
)

elif param.value_from and param.value_from.secrets_manager:
param_name = param.value_from.secrets_manager
version_ref = None
if ":" in param_name:
parsed = param_name.split(":")
param_name = parsed[0]
version_ref = parsed[2] if len(parsed) == 3 else None

param.version = mi.get_secrets_version(
secret_name=param_name,
version_ref=version_ref,
session=SessionManager()
.get_or_create()
.get_deployment_session(
account_id=cast(str, module.get_target_account_id()),
region_name=cast(str, module.target_region),
),
)
resolve_params_for_checksum(
deployment_manifest=deployment_manifest_wip, module=module, group_name=group.name
)

module.manifest_md5 = hashlib.md5(json.dumps(module.dict(), sort_keys=True).encode("utf-8")).hexdigest()
module.deployspec_md5 = hashlib.md5(open(deployspec_path, "rb").read()).hexdigest()
Expand Down
62 changes: 61 additions & 1 deletion seedfarmer/commands/_parameter_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
from aws_codeseeder import EnvVar, EnvVarType

import seedfarmer.errors
import seedfarmer.mgmt.module_info as mi
from seedfarmer import config
from seedfarmer.mgmt.module_info import get_module_metadata
from seedfarmer.models.manifests import DeploymentManifest, ModuleParameter
from seedfarmer.models.manifests import DeploymentManifest, ModuleManifest, ModuleParameter
from seedfarmer.services.session_manager import SessionManager
from seedfarmer.utils import upper_snake_case

Expand Down Expand Up @@ -110,6 +111,65 @@ def load_parameter_values(
return parameter_values


def resolve_params_for_checksum(
deployment_manifest: DeploymentManifest, module: ModuleManifest, group_name: str
) -> None:
for param in module.parameters:
if param.value_from and param.value_from.parameter_store:
if ":" in param.value_from.parameter_store:
raise seedfarmer.errors.InvalidConfigurationError(
f"CodeBuild does not support Versioned SSM Parameters -- see {group_name}-{module.name}"
)
param.version = mi.get_ssm_parameter_version(
ssm_parameter_name=param.value_from.parameter_store,
session=SessionManager()
.get_or_create()
.get_deployment_session(
account_id=cast(str, module.get_target_account_id()),
region_name=cast(str, module.target_region),
),
)

elif param.value_from and param.value_from.secrets_manager:
param_name = param.value_from.secrets_manager
version_ref = None
if ":" in param_name:
parsed = param_name.split(":")
param_name = parsed[0]
version_ref = parsed[2] if len(parsed) == 3 else None

param.version = mi.get_secrets_version(
secret_name=param_name,
version_ref=version_ref,
session=SessionManager()
.get_or_create()
.get_deployment_session(
account_id=cast(str, module.get_target_account_id()),
region_name=cast(str, module.target_region),
),
)
elif param.value_from and param.value_from.parameter_value:
p_value = deployment_manifest.get_parameter_value(
parameter=param.value_from.parameter_value,
account_alias=module.target_account,
region=module.target_region,
)
if p_value is not None:
param.resolved_value = str(p_value) if isinstance(p_value, str) else json.dumps(p_value)
else:
raise seedfarmer.errors.InvalidManifestError(
f"The parameter value defined ({param.value_from.parameter_value}) is not available"
)

elif param.value_from and param.value_from.env_variable:
if param.value_from.env_variable in os.environ:
param.resolved_value = os.getenv(param.value_from.env_variable)
else:
raise seedfarmer.errors.InvalidManifestError(
f"The environment variable ({param.value_from.env_variable}) is not available"
)


def _get_param_value_cache(
d_name: str,
g_name: str,
Expand Down
1 change: 1 addition & 0 deletions seedfarmer/models/manifests/_module_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class ModuleParameter(ValueFromRef):
name: str
value: Optional[Any] = None
version: Optional[Any] = None
resolved_value: Optional[Any] = None

def __init__(self, **data: Any) -> None:
super().__init__(**data)
Expand Down

0 comments on commit f29f039

Please sign in to comment.