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

Unable to import resources with "terraform import" - 401 (request "Unauthorized") #385

Closed
michaljrk opened this issue Jan 20, 2021 · 3 comments
Assignees

Comments

@michaljrk
Copy link

michaljrk commented Jan 20, 2021

Terraform CLI and Terraform MongoDB Atlas Provider Version

Terraform v0.13.6
+ provider registry.terraform.io/hashicorp/aws v3.24.0
+ provider registry.terraform.io/mongodb/mongodbatlas v0.7.0

Terraform Configuration File

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "3.24.0"
    }
    mongodbatlas = {
      source  = "mongodb/mongodbatlas"
      version = "0.7.0"
    }
  }
 provider "mongodbatlas" {
  public_key  = var.mongodb_atlas_api_pub_key
  private_key = var.mongodb_atlas_api_priv_key
}
resource "mongodbatlas_project" "atlas-test-project-imported" {
}

Steps to Reproduce

  1. Create a Project manually (from a web console or any other way but Terraform) under your Atlas cluster.
  2. Check the Project id.
  3. terraform init
  4. terraform import -var-file=input-vars.tfvars mongodbatlas_project.atlas-test-project-imported <project_id_from_point_2>

Expected Behavior

Atlas project successfully imported into Terraform state:

....
Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

Actual Behavior

401 (request "Unauthorized") error.

Debug Output

mongodbatlas_project.atlas-test-project-imported: Importing from ID "5a7xxxxx"...
mongodbatlas_project.atlas-test-project-imported: Import prepared!
  Prepared mongodbatlas_project for import
mongodbatlas_project.atlas-test-project-imported: Refreshing state... [id=5a7xxxxx]

Error: error getting project(5a7xxxxx): GET https://cloud.mongodb.com/api/atlas/v1.0/groups/5a7xxxxx: 401 (request "Unauthorized") You are not authorized for this resource.

Additional Context

Creating resources using the same Terraform provider (with the same api keys) works just fine.
Also, when trying to reproduce similar api call from curl, it seems to be working as well (please note 401 followed by 200, which I believe might be the cause of the original issue).

curl -i -u "pub-api:prv-api-key" --digest "https://cloud.mongodb.com/api/atlas/v1.0/groups/5a7xxxxx"
HTTP/2 401
www-authenticate: Digest realm="MMS Public API", domain="", nonce="xxxxxxx", algorithm=MD5, qop="auth", stale=false
content-type: application/json
content-length: 106
x-envoy-upstream-service-time: 1
date: Wed, 20 Jan 2021 10:16:01 GMT
server: envoy

HTTP/2 200
date: Wed, 20 Jan 2021 10:16:02 GMT
x-mongodb-service-version: gitHash=fb5d5ab24789939bc0d4578fff1deee70aece534; versionString=v20210105
content-type: application/json
strict-transport-security: max-age=31536000
x-frame-options: DENY
vary: Accept-Encoding, User-Agent
content-length: 1077
x-envoy-upstream-service-time: 71
server: envoy

{"clusterCount":2,"created":"2018-01-30T10:18:16Z","id":"5a7xxxxx","links":[{"href":"https://cloud.mongodb.com/api/atlas/v1.0/groups/5a7xxxxx","rel":"self"},{"href":"https://cloud.mongodb.com/api/atlas/v1.0/groups/5a7xxxxx/containers","rel":"http://cloud.mongodb.com/containers"},{"href":"https://cloud.mongodb.com/api/atlas/v1.0/groups/5a7xxxxx/clusters","rel":"http://cloud.mongodb.com/clusters"},{"href":"https://cloud.mongodb.com/api/atlas/v1.0/groups/5a7xxxxx/databaseUsers","rel":"http://cloud.mongodb.com/databaseUsers"},{"href":"https://cloud.mongodb.com/api/atlas/v1.0/groups/5a7xxxxx/peers","rel":"http://cloud.mongodb.com/peers"},{"href":"https://cloud.mongodb.com/api/atlas/v1.0/groups/5a7xxxxx/processes","rel":"http://cloud.mongodb.com/processes"},{"href":"https://cloud.mongodb.com/api/atlas/v1.0/groups/5a7xxxxx/whitelist","rel":"http://cloud.mongodb.com/whitelist"}],"name":"SecretName","orgId":"5aXXXXXX"}%

References

#192

@themantissa
Copy link
Collaborator

themantissa commented Jan 20, 2021

@nikhil-mongo can you take a look? (fyi reporter also commented in #192) so please try to repro and if an issue file an internal ticket. Thanks!

@nikhil-mongo nikhil-mongo self-assigned this Jan 21, 2021
@nikhil-mongo
Copy link
Collaborator

nikhil-mongo commented Jan 21, 2021

@michaljrk I could successfully import a project created using Atlas UI and imported through terraform.

Please confirm the below details for the further troubleshooting here:

  • The Project ID used
  • The API key username

Thanks.

@nikhil-mongo
Copy link
Collaborator

@michaljrk
As you confirmed, this is now working and you are not sure what really changed over the course of time since yesterday. Also, I could not repro this at my end and thus we decided to close this issue.

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

No branches or pull requests

3 participants