dbt-platform-helper 12.4.1__py3-none-any.whl → 12.5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of dbt-platform-helper might be problematic. Click here for more details.

Files changed (28) hide show
  1. dbt_platform_helper/COMMANDS.md +0 -3
  2. dbt_platform_helper/commands/config.py +2 -2
  3. dbt_platform_helper/commands/copilot.py +47 -28
  4. dbt_platform_helper/commands/environment.py +16 -178
  5. dbt_platform_helper/commands/pipeline.py +4 -5
  6. dbt_platform_helper/constants.py +9 -0
  7. dbt_platform_helper/domain/config_validator.py +242 -0
  8. dbt_platform_helper/domain/copilot_environment.py +204 -0
  9. dbt_platform_helper/domain/database_copy.py +7 -5
  10. dbt_platform_helper/domain/terraform_environment.py +53 -0
  11. dbt_platform_helper/jinja2_tags.py +1 -1
  12. dbt_platform_helper/providers/cache.py +15 -21
  13. dbt_platform_helper/providers/cloudformation.py +0 -1
  14. dbt_platform_helper/providers/config.py +90 -0
  15. dbt_platform_helper/providers/opensearch.py +36 -0
  16. dbt_platform_helper/providers/platform_config_schema.py +589 -527
  17. dbt_platform_helper/providers/redis.py +34 -0
  18. dbt_platform_helper/providers/yaml_file.py +83 -0
  19. dbt_platform_helper/templates/addons/svc/s3-cross-account-policy.yml +67 -0
  20. dbt_platform_helper/utils/aws.py +1 -57
  21. dbt_platform_helper/utils/files.py +0 -36
  22. dbt_platform_helper/utils/template.py +10 -0
  23. dbt_platform_helper/utils/validation.py +5 -327
  24. {dbt_platform_helper-12.4.1.dist-info → dbt_platform_helper-12.5.0.dist-info}/METADATA +2 -2
  25. {dbt_platform_helper-12.4.1.dist-info → dbt_platform_helper-12.5.0.dist-info}/RECORD +28 -20
  26. {dbt_platform_helper-12.4.1.dist-info → dbt_platform_helper-12.5.0.dist-info}/WHEEL +1 -1
  27. {dbt_platform_helper-12.4.1.dist-info → dbt_platform_helper-12.5.0.dist-info}/LICENSE +0 -0
  28. {dbt_platform_helper-12.4.1.dist-info → dbt_platform_helper-12.5.0.dist-info}/entry_points.txt +0 -0
@@ -551,9 +551,6 @@ platform-helper pipeline generate
551
551
  This command does the following in relation to the codebase pipelines:
552
552
  - Generates the copilot pipeline manifest.yml for copilot/pipelines/<codebase_pipeline_name>
553
553
 
554
- (Deprecated) This command does the following for non terraform projects (legacy AWS Copilot):
555
- - Generates the copilot manifest.yml for copilot/environments/<environment>
556
-
557
554
  ## Usage
558
555
 
559
556
  ```
@@ -8,11 +8,11 @@ import botocore
8
8
  import click
9
9
  from prettytable import PrettyTable
10
10
 
11
+ from dbt_platform_helper.providers.config import ConfigProvider
11
12
  from dbt_platform_helper.providers.validation import IncompatibleMajorVersionException
12
13
  from dbt_platform_helper.providers.validation import ValidationException
13
14
  from dbt_platform_helper.utils import versioning
14
15
  from dbt_platform_helper.utils.click import ClickDocOptGroup
15
- from dbt_platform_helper.utils.validation import config_file_check
16
16
  from dbt_platform_helper.utils.versioning import get_platform_helper_versions
17
17
 
18
18
  yes = "\033[92m✔\033[0m"
@@ -98,7 +98,7 @@ def deployment():
98
98
 
99
99
  recommendations = {}
100
100
 
101
- config_file_check()
101
+ ConfigProvider.config_file_check()
102
102
 
103
103
  for template_file in addons_templates:
104
104
  generated_with_version = maybe
@@ -7,17 +7,22 @@ from pathlib import PosixPath
7
7
 
8
8
  import click
9
9
  import yaml
10
+ from schema import SchemaError
10
11
 
11
12
  from dbt_platform_helper.constants import PLATFORM_CONFIG_FILE
13
+ from dbt_platform_helper.domain.config_validator import ConfigValidator
14
+ from dbt_platform_helper.domain.copilot_environment import CopilotTemplating
15
+ from dbt_platform_helper.providers.config import ConfigProvider
12
16
  from dbt_platform_helper.utils.application import get_application_name
13
17
  from dbt_platform_helper.utils.application import load_application
14
18
  from dbt_platform_helper.utils.aws import get_aws_session_or_abort
15
19
  from dbt_platform_helper.utils.click import ClickDocOptGroup
16
20
  from dbt_platform_helper.utils.files import generate_override_files
17
21
  from dbt_platform_helper.utils.files import mkfile
22
+ from dbt_platform_helper.utils.messages import abort_with_error
23
+ from dbt_platform_helper.utils.template import ADDON_TEMPLATE_MAP
18
24
  from dbt_platform_helper.utils.template import camel_case
19
25
  from dbt_platform_helper.utils.template import setup_templates
20
- from dbt_platform_helper.utils.validation import config_file_check
21
26
  from dbt_platform_helper.utils.validation import validate_addons
22
27
  from dbt_platform_helper.utils.versioning import (
23
28
  check_platform_helper_version_needs_update,
@@ -35,14 +40,6 @@ SERVICE_TYPES = [
35
40
  "Worker Service",
36
41
  ]
37
42
 
38
- ADDON_TEMPLATE_MAP = {
39
- "s3": ["addons/svc/s3-policy.yml"],
40
- "s3-policy": ["addons/svc/s3-policy.yml"],
41
- "appconfig-ipfilter": ["addons/svc/appconfig-ipfilter.yml"],
42
- "subscription-filter": ["addons/svc/subscription-filter.yml"],
43
- "prometheus-policy": ["addons/svc/prometheus-policy.yml"],
44
- }
45
-
46
43
 
47
44
  def list_copilot_local_environments():
48
45
  return [
@@ -244,25 +241,42 @@ def _get_s3_kms_alias_arns(session, application_name, config):
244
241
  return arns
245
242
 
246
243
 
244
+ def copilot_provider():
245
+ return CopilotTemplating()
246
+
247
+
247
248
  @copilot.command()
248
249
  def make_addons():
249
250
  """Generate addons CloudFormation for each environment."""
250
- output_dir = Path(".").absolute()
251
- config_file_check()
251
+ try:
252
+ config_provider = ConfigProvider(ConfigValidator())
253
+ _make_addons(config_provider)
254
+ except Exception as exc:
255
+ abort_with_error(exc)
256
+
257
+
258
+ def _make_addons(config_provider: ConfigProvider):
259
+ config_provider.config_file_check()
260
+ try:
261
+ config = config_provider.load_and_validate_platform_config()
262
+ except SchemaError as ex:
263
+ click.secho(f"Invalid `{PLATFORM_CONFIG_FILE}` file: {str(ex)}", fg="red")
264
+ raise click.Abort
252
265
 
253
266
  templates = setup_templates()
254
- config = _get_config()
267
+ extensions = _get_extensions()
255
268
  session = get_aws_session_or_abort()
256
269
 
257
270
  application_name = get_application_name()
258
271
 
259
272
  click.echo("\n>>> Generating Terraform compatible addons CloudFormation\n")
260
273
 
274
+ output_dir = Path(".").absolute()
261
275
  env_path = Path(f"copilot/environments/")
262
276
  env_addons_path = env_path / "addons"
263
277
  env_overrides_path = env_path / "overrides"
264
278
 
265
- _cleanup_old_files(config, output_dir, env_addons_path, env_overrides_path)
279
+ _cleanup_old_files(extensions, output_dir, env_addons_path, env_overrides_path)
266
280
  _generate_env_overrides(output_dir)
267
281
 
268
282
  svc_names = list_copilot_local_services()
@@ -271,33 +285,33 @@ def make_addons():
271
285
  _generate_svc_overrides(base_path, templates, svc_name)
272
286
 
273
287
  services = []
274
- for addon_name, addon_config in config.items():
275
- print(f">>>>>>>>> {addon_name}")
276
- addon_type = addon_config.pop("type")
277
- environments = addon_config.pop("environments")
288
+ for ext_name, ext_data in extensions.items():
289
+ extension = {**ext_data}
290
+ addon_type = extension.pop("type")
291
+ environments = extension.pop("environments")
278
292
  environment_addon_config = {
279
293
  "addon_type": addon_type,
280
294
  "environments": environments,
281
- "name": addon_config.get("name", None) or addon_name,
282
- "prefix": camel_case(addon_name),
283
- "secret_name": addon_name.upper().replace("-", "_"),
284
- **addon_config,
295
+ "name": extension.get("name", None) or ext_name,
296
+ "prefix": camel_case(ext_name),
297
+ "secret_name": ext_name.upper().replace("-", "_"),
298
+ **extension,
285
299
  }
286
300
 
287
301
  services.append(environment_addon_config)
288
302
 
289
303
  service_addon_config = {
290
304
  "application_name": application_name,
291
- "name": addon_config.get("name", None) or addon_name,
292
- "prefix": camel_case(addon_name),
305
+ "name": extension.get("name", None) or ext_name,
306
+ "prefix": camel_case(ext_name),
293
307
  "environments": environments,
294
- **addon_config,
308
+ **extension,
295
309
  }
296
310
 
297
311
  log_destination_arns = get_log_destination_arn()
298
312
 
299
313
  if addon_type in ["s3", "s3-policy"]:
300
- if config[addon_name].get("serve_static_content"):
314
+ if extensions[ext_name].get("serve_static_content"):
301
315
  continue
302
316
 
303
317
  s3_kms_arns = _get_s3_kms_alias_arns(session, application_name, environments)
@@ -307,8 +321,8 @@ def make_addons():
307
321
  )
308
322
 
309
323
  _generate_service_addons(
310
- addon_config,
311
- addon_name,
324
+ extension,
325
+ ext_name,
312
326
  addon_type,
313
327
  output_dir,
314
328
  service_addon_config,
@@ -316,10 +330,15 @@ def make_addons():
316
330
  log_destination_arns,
317
331
  )
318
332
 
333
+ environments = config_provider.apply_environment_defaults(config)["environments"]
334
+
335
+ provider = copilot_provider()
336
+ provider.generate_cross_account_s3_policies(environments, extensions)
337
+
319
338
  click.echo(templates.get_template("addon-instructions.txt").render(services=services))
320
339
 
321
340
 
322
- def _get_config():
341
+ def _get_extensions():
323
342
  config = _validate_and_normalise_extensions_config(PACKAGE_DIR / "default-extensions.yml")
324
343
  project_config = _validate_and_normalise_extensions_config(PLATFORM_CONFIG_FILE, "extensions")
325
344
  config.update(project_config)
@@ -1,18 +1,15 @@
1
- import boto3
2
1
  import click
3
2
  from schema import SchemaError
4
3
 
5
4
  from dbt_platform_helper.constants import DEFAULT_TERRAFORM_PLATFORM_MODULES_VERSION
6
5
  from dbt_platform_helper.constants import PLATFORM_CONFIG_FILE
6
+ from dbt_platform_helper.domain.config_validator import ConfigValidator
7
+ from dbt_platform_helper.domain.copilot_environment import CopilotEnvironment
7
8
  from dbt_platform_helper.domain.maintenance_page import MaintenancePageProvider
9
+ from dbt_platform_helper.domain.terraform_environment import TerraformEnvironment
8
10
  from dbt_platform_helper.platform_exception import PlatformException
9
- from dbt_platform_helper.providers.load_balancers import find_https_listener
10
- from dbt_platform_helper.utils.aws import get_aws_session_or_abort
11
+ from dbt_platform_helper.providers.config import ConfigProvider
11
12
  from dbt_platform_helper.utils.click import ClickDocOptGroup
12
- from dbt_platform_helper.utils.files import apply_environment_defaults
13
- from dbt_platform_helper.utils.files import mkfile
14
- from dbt_platform_helper.utils.template import setup_templates
15
- from dbt_platform_helper.utils.validation import load_and_validate_platform_config
16
13
  from dbt_platform_helper.utils.versioning import (
17
14
  check_platform_helper_version_needs_update,
18
15
  )
@@ -39,8 +36,11 @@ def environment():
39
36
  @click.option("--vpc", type=str)
40
37
  def offline(app, env, svc, template, vpc):
41
38
  """Take load-balanced web services offline with a maintenance page."""
42
- maintenance_page = MaintenancePageProvider()
43
- maintenance_page.activate(app, env, svc, template, vpc)
39
+ try:
40
+ MaintenancePageProvider().activate(app, env, svc, template, vpc)
41
+ except PlatformException as err:
42
+ click.secho(str(err), fg="red")
43
+ raise click.Abort
44
44
 
45
45
 
46
46
  @environment.command()
@@ -48,88 +48,12 @@ def offline(app, env, svc, template, vpc):
48
48
  @click.option("--env", type=str, required=True)
49
49
  def online(app, env):
50
50
  """Remove a maintenance page from an environment."""
51
- maintenance_page = MaintenancePageProvider()
52
- maintenance_page.deactivate(app, env)
53
-
54
-
55
- def get_vpc_id(session, env_name, vpc_name=None):
56
- if not vpc_name:
57
- vpc_name = f"{session.profile_name}-{env_name}"
58
-
59
- filters = [{"Name": "tag:Name", "Values": [vpc_name]}]
60
- vpcs = session.client("ec2").describe_vpcs(Filters=filters)["Vpcs"]
61
-
62
- if not vpcs:
63
- filters[0]["Values"] = [session.profile_name]
64
- vpcs = session.client("ec2").describe_vpcs(Filters=filters)["Vpcs"]
65
-
66
- if not vpcs:
67
- click.secho(
68
- f"No VPC found with name {vpc_name} in AWS account {session.profile_name}.", fg="red"
69
- )
70
- raise click.Abort
71
-
72
- return vpcs[0]["VpcId"]
73
-
74
-
75
- def get_subnet_ids(session, vpc_id, environment_name):
76
- subnets = session.client("ec2").describe_subnets(
77
- Filters=[{"Name": "vpc-id", "Values": [vpc_id]}]
78
- )["Subnets"]
79
-
80
- if not subnets:
81
- click.secho(f"No subnets found for VPC with id: {vpc_id}.", fg="red")
82
- raise click.Abort
83
-
84
- public_tag = {"Key": "subnet_type", "Value": "public"}
85
- public_subnets = [subnet["SubnetId"] for subnet in subnets if public_tag in subnet["Tags"]]
86
- private_tag = {"Key": "subnet_type", "Value": "private"}
87
- private_subnets = [subnet["SubnetId"] for subnet in subnets if private_tag in subnet["Tags"]]
88
-
89
- # This call and the method declaration can be removed when we stop using AWS Copilot to deploy the services
90
- public_subnets, private_subnets = _match_subnet_id_order_to_cloudformation_exports(
91
- session,
92
- environment_name,
93
- public_subnets,
94
- private_subnets,
95
- )
96
-
97
- return public_subnets, private_subnets
98
-
99
-
100
- def _match_subnet_id_order_to_cloudformation_exports(
101
- session, environment_name, public_subnets, private_subnets
102
- ):
103
- public_subnet_exports = []
104
- private_subnet_exports = []
105
- for page in session.client("cloudformation").get_paginator("list_exports").paginate():
106
- for export in page["Exports"]:
107
- if f"-{environment_name}-" in export["Name"]:
108
- if export["Name"].endswith("-PublicSubnets"):
109
- public_subnet_exports = export["Value"].split(",")
110
- if export["Name"].endswith("-PrivateSubnets"):
111
- private_subnet_exports = export["Value"].split(",")
112
-
113
- # If the elements match, regardless of order, use the list from the CloudFormation exports
114
- if set(public_subnets) == set(public_subnet_exports):
115
- public_subnets = public_subnet_exports
116
- if set(private_subnets) == set(private_subnet_exports):
117
- private_subnets = private_subnet_exports
118
-
119
- return public_subnets, private_subnets
120
-
121
-
122
- def get_cert_arn(session, application, env_name):
123
51
  try:
124
- arn = find_https_certificate(session, application, env_name)
125
- except:
126
- click.secho(
127
- f"No certificate found with domain name matching environment {env_name}.", fg="red"
128
- )
52
+ MaintenancePageProvider().deactivate(app, env)
53
+ except PlatformException as err:
54
+ click.secho(str(err), fg="red")
129
55
  raise click.Abort
130
56
 
131
- return arn
132
-
133
57
 
134
58
  @environment.command()
135
59
  @click.option("--vpc-name", hidden=True)
@@ -141,20 +65,13 @@ def generate(name, vpc_name):
141
65
  fg="red",
142
66
  )
143
67
  raise click.Abort
144
-
145
68
  try:
146
- conf = load_and_validate_platform_config()
69
+ config_provider = ConfigProvider(ConfigValidator())
70
+ CopilotEnvironment(config_provider).generate(name)
147
71
  except SchemaError as ex:
148
72
  click.secho(f"Invalid `{PLATFORM_CONFIG_FILE}` file: {str(ex)}", fg="red")
149
73
  raise click.Abort
150
74
 
151
- env_config = apply_environment_defaults(conf)["environments"][name]
152
- profile_for_environment = env_config.get("accounts", {}).get("deploy", {}).get("name")
153
- click.secho(f"Using {profile_for_environment} for this AWS session")
154
- session = get_aws_session_or_abort(profile_for_environment)
155
-
156
- _generate_copilot_environment_manifests(name, conf["application"], env_config, session)
157
-
158
75
 
159
76
  @environment.command(help="Generate terraform manifest for the specified environment.")
160
77
  @click.option(
@@ -165,84 +82,5 @@ def generate(name, vpc_name):
165
82
  help=f"Override the default version of terraform-platform-modules. (Default version is '{DEFAULT_TERRAFORM_PLATFORM_MODULES_VERSION}').",
166
83
  )
167
84
  def generate_terraform(name, terraform_platform_modules_version):
168
- conf = load_and_validate_platform_config()
169
-
170
- env_config = apply_environment_defaults(conf)["environments"][name]
171
- _generate_terraform_environment_manifests(
172
- conf["application"], name, env_config, terraform_platform_modules_version
173
- )
174
-
175
-
176
- def _generate_copilot_environment_manifests(environment_name, application, env_config, session):
177
- env_template = setup_templates().get_template("env/manifest.yml")
178
- vpc_name = env_config.get("vpc", None)
179
- vpc_id = get_vpc_id(session, environment_name, vpc_name)
180
- pub_subnet_ids, priv_subnet_ids = get_subnet_ids(session, vpc_id, environment_name)
181
- cert_arn = get_cert_arn(session, application, environment_name)
182
- contents = env_template.render(
183
- {
184
- "name": environment_name,
185
- "vpc_id": vpc_id,
186
- "pub_subnet_ids": pub_subnet_ids,
187
- "priv_subnet_ids": priv_subnet_ids,
188
- "certificate_arn": cert_arn,
189
- }
190
- )
191
- click.echo(
192
- mkfile(
193
- ".", f"copilot/environments/{environment_name}/manifest.yml", contents, overwrite=True
194
- )
195
- )
196
-
197
-
198
- def _generate_terraform_environment_manifests(
199
- application, env, env_config, cli_terraform_platform_modules_version
200
- ):
201
- env_template = setup_templates().get_template("environments/main.tf")
202
-
203
- terraform_platform_modules_version = _determine_terraform_platform_modules_version(
204
- env_config, cli_terraform_platform_modules_version
205
- )
206
-
207
- contents = env_template.render(
208
- {
209
- "application": application,
210
- "environment": env,
211
- "config": env_config,
212
- "terraform_platform_modules_version": terraform_platform_modules_version,
213
- }
214
- )
215
-
216
- click.echo(mkfile(".", f"terraform/environments/{env}/main.tf", contents, overwrite=True))
217
-
218
-
219
- def _determine_terraform_platform_modules_version(env_conf, cli_terraform_platform_modules_version):
220
- cli_terraform_platform_modules_version = cli_terraform_platform_modules_version
221
- env_conf_terraform_platform_modules_version = env_conf.get("versions", {}).get(
222
- "terraform-platform-modules"
223
- )
224
- version_preference_order = [
225
- cli_terraform_platform_modules_version,
226
- env_conf_terraform_platform_modules_version,
227
- DEFAULT_TERRAFORM_PLATFORM_MODULES_VERSION,
228
- ]
229
- return [version for version in version_preference_order if version][0]
230
-
231
-
232
- def find_https_certificate(session: boto3.Session, app: str, env: str) -> str:
233
- listener_arn = find_https_listener(session, app, env)
234
- cert_client = session.client("elbv2")
235
- certificates = cert_client.describe_listener_certificates(ListenerArn=listener_arn)[
236
- "Certificates"
237
- ]
238
-
239
- try:
240
- certificate_arn = next(c["CertificateArn"] for c in certificates if c["IsDefault"])
241
- except StopIteration:
242
- raise CertificateNotFoundException()
243
-
244
- return certificate_arn
245
-
246
-
247
- class CertificateNotFoundException(PlatformException):
248
- pass
85
+ config_provider = ConfigProvider(ConfigValidator())
86
+ TerraformEnvironment(config_provider).generate(name, terraform_platform_modules_version)
@@ -6,6 +6,8 @@ from shutil import rmtree
6
6
  import click
7
7
 
8
8
  from dbt_platform_helper.constants import DEFAULT_TERRAFORM_PLATFORM_MODULES_VERSION
9
+ from dbt_platform_helper.domain.config_validator import ConfigValidator
10
+ from dbt_platform_helper.providers.config import ConfigProvider
9
11
  from dbt_platform_helper.utils.application import get_application_name
10
12
  from dbt_platform_helper.utils.aws import get_account_details
11
13
  from dbt_platform_helper.utils.aws import get_codestar_connection_arn
@@ -16,7 +18,6 @@ from dbt_platform_helper.utils.files import mkfile
16
18
  from dbt_platform_helper.utils.git import git_remote
17
19
  from dbt_platform_helper.utils.messages import abort_with_error
18
20
  from dbt_platform_helper.utils.template import setup_templates
19
- from dbt_platform_helper.utils.validation import load_and_validate_platform_config
20
21
  from dbt_platform_helper.utils.versioning import (
21
22
  check_platform_helper_version_needs_update,
22
23
  )
@@ -60,11 +61,9 @@ def generate(terraform_platform_modules_version, deploy_branch):
60
61
 
61
62
  This command does the following in relation to the codebase pipelines:
62
63
  - Generates the copilot pipeline manifest.yml for copilot/pipelines/<codebase_pipeline_name>
63
-
64
- (Deprecated) This command does the following for non terraform projects (legacy AWS Copilot):
65
- - Generates the copilot manifest.yml for copilot/environments/<environment>
66
64
  """
67
- pipeline_config = load_and_validate_platform_config()
65
+ config_provider = ConfigProvider(ConfigValidator())
66
+ pipeline_config = config_provider.load_and_validate_platform_config()
68
67
 
69
68
  has_codebase_pipelines = CODEBASE_PIPELINES_KEY in pipeline_config
70
69
  has_environment_pipelines = ENVIRONMENT_PIPELINES_KEY in pipeline_config
@@ -16,3 +16,12 @@ CONDUIT_ADDON_TYPES = [
16
16
  "redis",
17
17
  ]
18
18
  CONDUIT_DOCKER_IMAGE_LOCATION = "public.ecr.aws/uktrade/tunnel"
19
+ HYPHENATED_APPLICATION_NAME = "hyphenated-application-name"
20
+ ALPHANUMERIC_ENVIRONMENT_NAME = "alphanumericenvironmentname123"
21
+ ALPHANUMERIC_SERVICE_NAME = "alphanumericservicename123"
22
+ COPILOT_IDENTIFIER = "c0PIlotiD3ntIF3r"
23
+ CLUSTER_NAME_SUFFIX = f"Cluster-{COPILOT_IDENTIFIER}"
24
+ SERVICE_NAME_SUFFIX = f"Service-{COPILOT_IDENTIFIER}"
25
+ REFRESH_TOKEN_MESSAGE = (
26
+ "To refresh this SSO session run `aws sso login` with the corresponding profile"
27
+ )