jfrog / terraform-provider-platform

Terraform provider to manage JFrog Platform
https://jfrog.com
Apache License 2.0
1 stars 1 forks source link

"Invalid token, parse" error while trying to run `terraform import` #44

Closed madelaney closed 4 months ago

madelaney commented 4 months ago

Describe the bug

While trying to run "terraform import" on a platform_permission resource, I'm getting the following error:

Error: {
  "errors" : [ {
    "code" : "UNAUTHORIZED",
    "message" : "Invalid token, parse"
  } ]
}

I should note that my token, that I'm passing in, has let me create repository resources so the token is value.

I've created a gist https://gist.github.com/madelaney/3ea8e36c0789fa96341b7b7dd2d89622 which should reproduce the error

Artifatory Version

jf rt curl api/system/version | jq .version
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   975    0   975    0     0  12981      0 --:--:-- --:--:-- --:--:-- 12828
"7.71.11"

Requirements for and issue

Expected behavior I'd expect the resource to get imported but instead, I get the error.

Additional context Add any other context about the problem here.

alexhung commented 4 months ago

@madelaney I am unable to reproduce this issue locally.

Using an admin token (set in env var JFROG_ACCESS_TOKEN), I am able to import a permission from my local Artifactory instance.

terraform {
  required_providers {
    platform = {
      source  = "jfrog/platform"
      version = "1.4.1"
    }
  }
}

provider "platform" {
  // supply JFROG_ACCESS_TOKEN as env var
}

resource "platform_permission" "my-permission" {
  name = "my-permission-name"

  artifact = {
    targets = [
      {
        name = "alexh-docker-local"
        include_patterns = ["**"]
      },
      {
        name = "ALL-LOCAL"
        include_patterns = ["**", "*.js"]
      },
      {
        name = "ALL-REMOTE"
        include_patterns = ["**", "*.js"]
      },
      {
        name = "ALL-DISTRIBUTION"
        include_patterns = ["**", "*.js"]
      }
    ]
  }
}

Then:

terraform import platform_permission.my-permission my-permission-name

Screenshot 2024-03-20 at 1 07 27 PM

I don't see the variable artifactory_access_token being set to any value in your snippet, I assume that is set somewhere. I suggest making sure that variable is set to the correct token.

alexhung commented 4 months ago

@madelaney When I use curl to make the API request with incorrect token, I get the expected error:

Screenshot 2024-03-20 at 1 18 27 PM

madelaney commented 4 months ago

I don't see the variable artifactory_access_token being set to any value in your snippet, I assume that is set somewhere. I suggest making sure that variable is set to the correct token.

The token is being passed in via terraform.tfvars. I also tried via setting JFROG_ACCESS_TOKEN, I get the same issue.

The token seems correct as the artifactory provider was happy with it when I was able to create groups / users / repositories. It's only the platform provider that seems unhappy.

madelaney commented 4 months ago

@alexhung , can I use the same access token for both the Artifactory and Platform provider?

alexhung commented 4 months ago

@madelaney Per the REST API, the platform_permission resource requires access token with admin permission. This is likely the cause of the error.

madelaney commented 4 months ago

@alexhung , I'm pretty sure it has admin right. I shouldn't be able to create users / groups / repositories if it wasn't an admin token, right?

(I don't mean to sound like a jerk, I'm just confused)

alexhung commented 4 months ago

@madelaney I am using a token scoped to admin user and did not get any error per screenshot above. I don't believe it's an issue from the provider or the resource but I also don't have any answer to your issue.

madelaney commented 4 months ago

@alexhung , okay, I found it.

I was using an API token that was generated from the admin user originally. The API token was not working with the platform provider. If I create an admin access scoped token that value works with both.

I did see that the API token was being deprecated (https://registry.terraform.io/providers/jfrog/artifactory/latest/docs#api-key-deprecated) but since the Artifactory provider, I didn't think much of it. Maybe this is a side-effect.

alexhung commented 4 months ago

@madelaney The API key will be deprecated by end of this year. When its support is removed by end of 2024, the artifactory_api_key resource will be removed as well.

If you are still using API key, please make plans to migrate to access tokens. Our development and testing process has been using access token since the announcement of API key deprecate in 2022(?).