Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use existing identity provider in temporary environments #717

Merged
merged 7 commits into from
Aug 14, 2024

Conversation

rocketnova
Copy link
Contributor

@rocketnova rocketnova commented Aug 5, 2024

Ticket

Resolves #719

Changes

What was added, updated, or removed in this PR.

  • Updates /infra/app/service to use existing idp resources for temporary environments
  • Re-organizes identity provider modules into a shared folder
  • Adds a new existing-identity-provider module as a wrapper for multiple data source calls
  • Refactors variable names out of the identity-provider-client module and into app-config to ensure the same name (e.g. ssm name or access policy) is used across modules
  • Removes prefix from identity_provider_name

Context for reviewers

Testing instructions, background context, more in-depth details of the implementation, and anything else you'd like to call out or ask reviewers.

In temporary environments, such as PR preview environments, we want to use the same Cognito User Pool as the default terraform workspace. That allows QA and UAT to use existing users instead of having to go through an account creation flow on every PR.

This PR checks to see if it's a temporary environment. If it is, it uses data sources to fetch existing resources. If it's not, it creates a new identity provider, just as before.

Testing

Provide evidence that the code works as expected. Explain what was done for testing and the results of the test plan. Include screenshots, GIF demos, shell commands or output to help show the changes working as expected. ProTip: you can drag and drop or paste images into this textbox.

Tested at navapbc/platform-test#128

IAM role for the preview environment has default workspace access policy successfully attached:

ECS task definition for the preview environment service has correct cognito environment variables (I know I redacted the details out, but wanted to give the gist):

ECS task definition for the preview environment service has correct cognito secret arn:
CleanShot 2024-08-06 at 19 03 37@2x

Preview environment successfully applied the updates:
CleanShot 2024-08-06 at 19 05 26@2x

@rocketnova rocketnova changed the title Proof of concept re-using existing idp for temporary environments Use existing identity provider in temporary environments Aug 7, 2024
@rocketnova rocketnova marked this pull request as ready for review August 7, 2024 02:32
Copy link
Contributor

@lorenyu lorenyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, had a comment about the new pattern of grouping related modules

infra/app/app-config/env-config/identity-provider.tf Outdated Show resolved Hide resolved
infra/modules/identity-provider/README.md Outdated Show resolved Hide resolved
infra/modules/identity-provider/README.md Outdated Show resolved Hide resolved
Comment on lines 37 to 38
client_secret_ssm_name = "/${var.app_name}-${var.environment}/identity-provider/client-secret"
user_pool_access_policy_name = "${var.app_name}-${var.environment}-cognito-access"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at the comment I left on infra/modules/identity-provider/README.md, but I think we can have a new pattern that eliminates the need for config variables that are used primarily for dependency management rather than for actual configuration. tldr; I think we can move the definition of these things to something like a modules/identity/interface/ module that can be used by both the module with data sources and the module that actually creates the resources

Copy link
Contributor

@coilysiren coilysiren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great layout here 👍🏼 👍🏼 👍🏼

infra/app/service/main.tf Outdated Show resolved Hide resolved
@rocketnova
Copy link
Contributor Author

@lorenyu @coilysiren Re-requesting review. This PR has been re-architected to incorporate the feedback around module structure. The test can be seen: navapbc/platform-test#128

Copy link
Contributor

@lorenyu lorenyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good just left some nits.

Wish we had platform-test-rails up so we can see it in action in the PR environment. Otherwise it's a little bit more of a leap of faith. But I think it's still ok.

@@ -22,12 +22,20 @@ data "aws_subnets" "private" {
}

locals {
# The prefix key/value pair is used for Terraform Workspaces, which is useful for projects with multiple infrastructure developers.
# By default, Terraform creates a workspace named “default.” If a non-default workspace is not created this prefix will equal “default”,
# if you choose not to use workspaces set this value to "dev"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment. What does it mean to "choose not to use workspaces"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh funny. I copy/pasted this directly from

# The prefix key/value pair is used for Terraform Workspaces, which is useful for projects with multiple infrastructure developers.
# By default, Terraform creates a workspace named “default.” If a non-default workspace is not created this prefix will equal “default”,
# if you choose not to use workspaces set this value to "dev"
prefix = terraform.workspace == "default" ? "" : "${terraform.workspace}-"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was i thinking

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we change the comment here and in /infra/app/app-config/env-config/main.tf to this:

locals {
  # The prefix is used to create uniquely named resources per terraform workspace, which 
  # are needed in CI/CD for preview environments and tests. 
  #
  # To isolate changes during infrastructure development by using manually created 
  # terraform workspaces, see: /docs/infra/develop-and-test-infrastructure-in-isolation-using-workspaces.md.
  prefix = terraform.workspace == "default" ? "" : "${terraform.workspace}-"

  bucket_name = "${local.prefix}${var.project_name}-${var.app_name}-${var.environment}"
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great

infra/app/service/main.tf Outdated Show resolved Hide resolved
infra/app/service/main.tf Outdated Show resolved Hide resolved
infra/app/service/main.tf Outdated Show resolved Hide resolved
infra/app/service/main.tf Outdated Show resolved Hide resolved
@rocketnova
Copy link
Contributor Author

Looks good just left some nits.

No objections to your nits. Let me do a quick test over at platform-test to verify everything is working as expected.

Wish we had platform-test-rails up so we can see it in action in the PR environment. Otherwise it's a little bit more of a leap of faith. But I think it's still ok.

Agree about platform-test-rails. I'll add some screenshots from navapbc/platform-test#128 to demonstrate what it's doing on the test environment. Apologies for failing to remember to attach these to both this PR and the one in platform-test.

@rocketnova
Copy link
Contributor Author

Oh I did add screenshots to this PR already. Here they are again from the latest platform-test run.

First, here's the dev environment's user pool app-dev:

Then, here's the platform-test CI service creating a client app in the same user pool and the PR preview branch also creating a client app in the same user pool.

CleanShot 2024-08-14 at 12 05 22@2x

@lorenyu
Copy link
Contributor

lorenyu commented Aug 14, 2024

I see an error in the template CI
image

I think you may need to set identity_provider_user_pool_id to null if enable_identity_provider is false

@rocketnova
Copy link
Contributor Author

I see an error in the template CI
I think you may need to set identity_provider_user_pool_id to null if enable_identity_provider is false

Yeah, I'm looking at that error, too. Sadly, I think it's more complicated than that. I think there's a problem with using temporary environments to control whether or not to use an existing user pool.

@rocketnova
Copy link
Contributor Author

I see an error in the template CI
I think you may need to set identity_provider_user_pool_id to null if enable_identity_provider is false

Yeah, I'm looking at that error, too. Sadly, I think it's more complicated than that. I think there's a problem with using temporary environments to control whether or not to use an existing user pool.

Loren and I chatted about this offline and I fixed the error. I was confusing this issue with a different problem.

@rocketnova rocketnova merged commit da5e865 into main Aug 14, 2024
9 checks passed
@rocketnova rocketnova deleted the rocket/idp-preview branch August 14, 2024 22:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Connect to existing identity provider in temporary environments
3 participants