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: : Provider crash when an invalid role name is specified to mongodbatlas_project_api_key #1720

Merged
merged 3 commits into from
Dec 11, 2023

Conversation

andreaangiolillo
Copy link
Collaborator

@andreaangiolillo andreaangiolillo commented Dec 7, 2023

Description

Ticket: CLOUDP-216402
Issue: Provider crashes when an invalid role name is specified to mongodbatlas_project_api_key

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.

Testing

Local testing

I reproduced the error with v1.13.1

│ Error: Plugin did not respond
--
│
│ The plugin encountered an error, and failed to respond to the
│ plugin6.(*GRPCProvider).ApplyResourceChange call. The plugin logs may
│ contain more details.

I run the same TF config with my changes

│ Error: POST https://cloud-dev.mongodb.com/api/atlas/v1.0/groups/6571a9637db22d780f3cec0c/apiKeys: 400 (request "INVALID_ENUM_VALUE") An invalid enumeration value GROUP_s was specified.

Acceptance Test

I added the acceptance test TestAccConfigRSProjectAPIKey_Invalid_Role

--- PASS: TestAccConfigRSProjectAPIKey_Invalid_Role (407.53s)
PASS
ok      github.com/mongodb/terraform-provider-mongodbatlas/internal/service/projectapikey       407.977s

@github-actions github-actions bot added the bug label Dec 7, 2023
@mongodb mongodb deleted a comment from github-actions bot Dec 7, 2023
@mongodb mongodb deleted a comment from github-actions bot Dec 7, 2023
@mongodb mongodb deleted a comment from github-actions bot Dec 7, 2023
@andreaangiolillo
Copy link
Collaborator Author

Failing test is related to another resource

Error: -08T10:39:12.578Z [ERROR] sdk.helper_resource: Error running import: test_name=TestAccConfigRSCustomDBRoles_importBasic test_terraform_path=/home/runner/work/_temp/035a6132-4f93-460b-8080-7c833c37c596/terraform test_working_directory=/tmp/plugintest409873591 test_step_number=2
  error=
  | ImportStateVerify attributes not equivalent. Difference is shown below. The - symbol indicates attributes missing after import.
  | 
  |   map[string]string{
  | + \t"actions.0.resources.0.cluster": "false",
  |   }
  
=== NAME  TestAccConfigRSCustomDBRoles_importBasic
    resource_custom_db_role_test.go:441: Step 2/2 error running import: ImportStateVerify attributes not equivalent. Difference is shown below. The - symbol indicates attributes missing after import.
        
          map[string]string{
        + 	"actions.0.resources.0.cluster": "false",
          }
--- FAIL: TestAccConfigRSCustomDBRoles_importBasic (417.32s)

@andreaangiolillo andreaangiolillo marked this pull request as ready for review December 8, 2023 11:18
@andreaangiolillo andreaangiolillo requested a review from a team as a code owner December 8, 2023 11:18
_, err = conn.APIKeys.Delete(ctx, orgID, apiKeyID)
if err != nil {
return diag.FromErr(fmt.Errorf("error unable to delete Key (%s): %s", apiKeyID, err))
if orgID != "" {
Copy link
Collaborator

Choose a reason for hiding this comment

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

In what case will orgID be ""? I believe this is what crashes the provider right (empty orgId passed to Delete API call)?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The error in the help ticket was caused by the missing return statement at line 108: the endpoint was returning an error which was different than 404 and so line 126 was trying to access apiKey via apiKey.PublicKey but apiKey was null

I discovered this scenario on missing orgID with this failing test TestAccConfigRSProjectAPIKey_RecreateWhenDeletedExternally:

=== NAME  TestAccConfigRSProjectAPIKey_RecreateWhenDeletedExternally
    testing_new.go:91: Error running post-test destroy, there may be dangling resources: exit status 1
        
        Error: error unable to delete Key (6571df9493323a7823188b59): DELETE https://cloud-dev.mongodb.com/api/atlas/v1.0/orgs//apiKeys/6571df9493323a7823188b59: 400 (request "") 

The test was deleting the API key manually as part of deleteAPIKeyManually and then the post-destroy was trying to delete the API key that was already deleted.

@andreaangiolillo andreaangiolillo requested review from maastha and a team December 8, 2023 11:57
@andreaangiolillo andreaangiolillo merged commit 8d06929 into master Dec 11, 2023
31 of 32 checks passed
@andreaangiolillo andreaangiolillo deleted the CLOUDP-216402 branch December 11, 2023 09:07
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.

5 participants