atlassian / terraform-provider-artifactory

Terraform provider to manage Artifactory
Apache License 2.0
89 stars 42 forks source link

Authentication not working with a valid api_key #79

Open AshutoshNirkhe opened 4 years ago

AshutoshNirkhe commented 4 years ago

Community Note

Terraform Version

v0.12.24

Affected Resource(s)

Provider authentication using a valid api_key

Terraform Configuration Files

# Copy-paste your Terraform configurations here - for large Terraform configs,
# please use a service like Dropbox and share a link to the ZIP file.
provider "artifactory" {
  url   = "https://my-artifactory.domain.com/artifactory"
  api_key = var.admin_apikey    #valid API Key, because authentication works fine via curl -H 'X-JFrog-Art-Api:${api_key}'
}

Debug Output

Panic Output

Expected Behavior

Authntication through provider using api_key should have ben successful, just the way it works with REST API using header 'X-JFrog-Art-Api' e.g - curl -k -H "X-JFrog-Art-Api:${api_key}" https://localhost:443/artifactory/api/system/ping % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 2 0 2 0 0 5 0 --:--:-- --:--:-- --:--:-- 5 OK

Actual Behavior

Error: GET https://my-artifactory.domain.com/artifactory/api/system/ping: 401 [{Status:401 Message:Bad credentials}] on providers.tf line 1, in provider "artifactory": 1: provider "artifactory" {

Steps to Reproduce

  1. terraform apply

Important Factoids

References

AshutoshNirkhe commented 4 years ago

Ok so finally I could figure out what's going wrong here. Actually the issue is not as such in provider schema logic, but because the way we our use case is. We are using (another) existing Artifactory for storing tf state file and have ARTIFACTORY_USERNAME and ARTIFACTORY_PASSWORD set in env of our calling CI code. So in effect the Artifactory provider gets username and password from above 2 env variables (https://github.com/atlassian/terraform-provider-artifactory/blob/v2.0.0/pkg/artifactory/provider.go#L27 and line 34). And we are passing (a valid) api_key in provider too. Hence the conflict!!

So now I am passing ARTIFACTORY_USERNAME and ARTIFACTORY_PASSWORD explicitly during tf init with artifactory backend. And then making these variables empty before calling say terraform validate/plan/import/apply etc. This way provider gets only api_key and it stays happy without any conflicts.