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

fix: Avoids sending database user password in update request if the value has not changed #2005

Merged
merged 4 commits into from
Mar 12, 2024

Conversation

AgustinBettati
Copy link
Member

@AgustinBettati AgustinBettati commented Mar 11, 2024

Description

Link to any related issue(s): CLOUDP-235738

This change aligns with our behaviour prior to version 1.12.0.
Additionally a warning is included to bring visibility on the suggested course of action after a database user is created:

mongodbatlas_database_user.static_user: Creation complete after 7s [id=YXV0aF9kYXRhYmFzZV9uYW1l:YWRtaW4=-cHJvamVjdF9pZA==:NjUyNTE0NDZhZTVmM2Y2ZWM3OTY4YjEz-dXNlcm5hbWU=:dXNlci10aGF0LWNhbi1iZS1kZWxldGVkLTM=]
╷
│ Warning: If the password value will be managed externally it is advised to remove the attribute
│ 
│   with mongodbatlas_database_user.static_user,
│   on main.tf line 72, in resource "mongodbatlas_database_user" "static_user":
│   72: resource "mongodbatlas_database_user" "static_user" {
│ 
│ More details can be found in resource documentation under the 'password' attribute

Manual verification that PATCH request is being generated correctly

Generated patch request when updating a labels value but password remains the same, password is not sent:

PATCH /api/atlas/v2/groups/65251446ae5f3f6ec7968b13/databaseUsers/admin/user-that-can-be-deleted-2 HTTP/1.1
{
 "awsIAMType": "NONE",
 "databaseName": "admin",
 "groupId": "65251446ae5f3f6ec7968b13",
 "labels": [
  {
   "key": "My Key2",
   "value": "My Value"
  }
 ],
 "ldapAuthType": "NONE",
 "oidcAuthType": "NONE",
 "roles": [
  {
   "databaseName": "admin",
   "roleName": "readAnyDatabase"
  }
 ],
 "scopes": [],
 "username": "user-that-can-be-deleted-2",
 "x509Type": "NONE"
}

Generated patch request when updating password value, password is sent:

PATCH /api/atlas/v2/groups/65251446ae5f3f6ec7968b13/databaseUsers/admin/user-that-can-be-deleted-2 HTTP/1.1
{
 "awsIAMType": "NONE",
 "databaseName": "admin",
 "groupId": "65251446ae5f3f6ec7968b13",
 "labels": [
  {
   "key": "My Key3",
   "value": "My Value"
  }
 ],
 "ldapAuthType": "NONE",
 "oidcAuthType": "NONE",
 "password": "udpatedpassword",
 "roles": [
  {
   "databaseName": "admin",
   "roleName": "readAnyDatabase"
  }
 ],
 "scopes": [],
 "username": "user-that-can-be-deleted-2",
 "x509Type": "NONE"
}

Type of change:

  • Bug fix (non-breaking change which fixes an issue). Please, add the "bug" label to the PR.
  • New feature (non-breaking change which adds functionality). Please, add the "enhancement" label to the PR.
  • Breaking change (fix or feature that would cause existing functionality to not work as expected). Please, add the "breaking change" label to the PR.
  • This change requires a documentation update
  • Documentation fix/enhancement

Required Checklist:

  • I have signed the MongoDB CLA
  • I have read the contribution guidelines
  • I have checked that this change does not generate any credentials and that they are NOT accidentally logged anywhere.
  • I have added tests that prove my fix is effective or that my feature works per HashiCorp requirements
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code
  • If changes include deprecations or removals, I defined an isolated PR with a relevant title as it will be used in the auto-generated changelog.
  • If changes include removal or addition of 3rd party GitHub actions, I updated our internal document. Reach out to the APIx Integration slack channel to get access to the internal document.

Further comments

@github-actions github-actions bot added the bug label Mar 11, 2024
@AgustinBettati AgustinBettati force-pushed the CLOUDP-235738 branch 2 times, most recently from e55360d to 436dca6 Compare March 11, 2024 13:01
@AgustinBettati AgustinBettati marked this pull request as ready for review March 11, 2024 13:24
@AgustinBettati AgustinBettati requested a review from a team as a code owner March 11, 2024 13:24
}, nil
}

if statePasswordValue != dbUserModel.Password {
Copy link
Member

Choose a reason for hiding this comment

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

i guess this also covers if the password attribute is deleted, then we set nil so it's not sent

Copy link
Member Author

Choose a reason for hiding this comment

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

correct, added an additional unit test to cover this case as well, thanks

@@ -231,6 +231,8 @@ func (r *databaseUserRS) Create(ctx context.Context, req resource.CreateRequest,
return
}

resp.Diagnostics.AddWarning("If the password value will be managed externally it is advised to remove the attribute", "More details can be found in resource documentation under the 'password' attribute")
Copy link
Member

Choose a reason for hiding this comment

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

is this warning shown always or it has some condition?

Copy link
Member Author

Choose a reason for hiding this comment

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

will always be shown, not really any particular condition we can use because password will always be defined for create operation (fails in API otherwise)

Copy link
Collaborator

@andreaangiolillo andreaangiolillo left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 132 to +133
* `username` - (Required) Username for authenticating to MongoDB. USER_ARN or ROLE_ARN if `aws_iam_type` is USER or ROLE.
* `password` - (Required) User's initial password. A value is required to create the database user, however the argument but may be removed from your Terraform configuration after user creation without impacting the user, password or Terraform management. IMPORTANT --- Passwords may show up in Terraform related logs and it will be stored in the Terraform state file as plain-text. Password can be changed after creation using your preferred method, e.g. via the MongoDB Atlas UI, to ensure security. If you do change management of the password to outside of Terraform be sure to remove the argument from the Terraform configuration so it is not inadvertently updated to the original password.
* `password` - (Required) User's initial password. A value is required to create the database user, however the argument may be removed from your Terraform configuration after user creation without impacting the user, password or Terraform management. If you do change management of the password to outside of Terraform it is advised to remove the argument from the Terraform configuration. IMPORTANT --- Passwords may show up in Terraform related logs and it will be stored in the Terraform state file as plain-text. Password can be changed after creation using your preferred method, e.g. via the MongoDB Atlas UI, to ensure security.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we add a warning at the top of this page to make sure that this warning message is not missed?

Copy link
Member Author

Choose a reason for hiding this comment

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

makes sense, included.

@AgustinBettati AgustinBettati merged commit 29d4268 into master Mar 12, 2024
45 checks passed
@AgustinBettati AgustinBettati deleted the CLOUDP-235738 branch March 12, 2024 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants