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

Conditional Artifactory usage requires access token and URL while they are not used #1175

Closed
tomvmeer opened this issue Jan 20, 2025 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@tomvmeer
Copy link

tomvmeer commented Jan 20, 2025

Describe the bug

artifactory_file data source with count of 0 causes Terraform plan to try to authenticate with Artifactory.

Requirements for and issue

  • For deploying my terraform in the dev environment for testing I do not want to use Artifactory files but rather local files. I achieve this by dynamically setting the count of Artifactory files to 0 in my Terraform code. This works as expected and Artifactory is not used at all during my plan/apply (I can use fake artifact repositories and paths, which makes me believe the code blocks are ignored). The problem is that I do need to provide a real Artifactory URL and token, even though Terraform does not need them!
  • Example code:
terraform {
  required_providers {
    artifactory = {
      source  = "jfrog/artifactory"
      version = "12.8.1"
    }
  }
}
provider "artifactory" {
  url           = null
  access_token  = null
}
data "artifactory_file" "jar-file" {
  count       = 0
  repository  = "foo"
  path        = "bar"
  output_path = ""
}
  • Jfrog Enterprise Plus 7.77.11 rev 77711900
  • Terraform v1.7.5
  • provider v12.8.1

Expected behavior
The provider should not try to contact the Artifactory to Authenticate when the Artifactory is not being used at all.

@tomvmeer tomvmeer added the bug Something isn't working label Jan 20, 2025
@alexhung
Copy link
Member

@tomvmeer The checks/usages for the Artifactory URL and token are part of the provider initialization process. At that time, the provider does not know if there are any resources being used so it is not possible to skip the check/usage.

@tomvmeer
Copy link
Author

@alexhung thanks for your response! What I then don't understand is why this still works:

terraform {
  required_providers {
    artifactory = {
      source  = "jfrog/artifactory"
      version = "12.8.1"
    }
  }
}
provider "artifactory" {
  url           = null
  access_token  = null
}

... some none Artifactory related code

The provider seems to understand no resources will be created using it and it does not initialize?

@alexhung
Copy link
Member

@tomvmeer This is most likely Terraform itself sees that no artifactory resources are defined in the configuration and therefore there's no need to initialize the provider.

@tomvmeer
Copy link
Author

tomvmeer commented Jan 21, 2025

I understand, I see other providers solve this issue by only trying to create the connection to the external system during the apply stage of a specific resource (see for example This one). Given that resources with count=0 are not included in the apply, the issue presented here does not occur.

Is this something the Artifactory Terraform provider could implement too?

@alexhung
Copy link
Member

@tomvmeer It is a conscious design decision and trade off. The other providers don't check for empty URL/token and makes API requests regardless whether they are set. This will generate authentication error during apply time.

In JFrog providers, we check the URL/token during provider initialization so that the unset/empty values is caught as soon as possible. This benefits most of our users as empty URL/token is very likely a mistake and unintended.

You may be interested in using OpenTofu instead. Looks like they are adding optional/conditional provider: https://github.com/opentofu/opentofu/releases/tag/v1.9.0 HashiCorp added similar concept but only to their HCP offering as Stacks.

@tomvmeer
Copy link
Author

Thanks for the discussion @alexhung I will close this issue now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants