ARTIFACTORY: What to do when you want to resolve a Directory Location but also Files within the Directory in Artifactory Remote Repositories
There are some use cases where you want to get the result of a folder from an upstream repository for use in a script or some other tool. The problem with this is that requesting a folder instead of a file will return and cache the HTML output of that location into the remote repository.
Subsequent requests further into that location will cause an issue as now Artifactory has cached a file there when it is actually a folder.
To resolve this issue we will be configuring our repositories in a particular way so that we will be able to resolve both folders and files from an upstream site.
In this article, we will be utilizing Artifactory 7.55.6 and the screenshots that you see will be from this version. The upstream site where we will be demonstrating this use case is “https://releases.hashicorp.com”.
Walking through an ExampleThe Problem
There may be a use case where your automation or script wants to look at the returned result of a folder to parse through what is available and perform further action or analysis on the result. For example, a script that takes a folder records the available links in that folder, then follows through and requests particular items from those links.
Sending requests to folder locations of remote repositories will cache the returned result. In this case, it will catch the HTML response of the upstream repository. The following example is just a generic remote repository called “hashicorp-folders-remote”.
I.e. A curl request to a folder in the upstream
$ curl -uadmin "<ART_URL>/artifactory/hashicorp-folders-remote/terraform-provider-template/2.2.0"
<!DOCTYPE html>
<html>
<head>
<title>Terraform Provider Template v2.2.0 Binaries | HashiCorp Releases</title>
<meta name=viewport content="width=device-width, initial-scale=1">
<style type="text/css">
html {
position: relative;
min-height: 100%;
…
<li>
<a data-product="terraform-provider-template" data-version="2.2.0" data-os="darwin" data-arch="amd64" href="https://releases.hashicorp.com/terraform-provider-template/2.2.0/terraform-provider-template_2.2.0_darwin_amd64.zip">terraform-provider-template_2.2.0_darwin_amd64.zip</a>
</li>
…
In Artifactory you will see that the HTML file has been cached at the 2.2.0 folder location.
On the upstream site, there are actual artifacts located within the “2.2.0” folder.
A request to one of those items will now fail since the “2.2.0” HTML response has been saved as a file
i.e. The Upstream contents of the “2.2.0” folder from the upstream site.
i.e. A request to Artifactory using the same repository will now fail since the earlier “2.2.0” request saved a file in the location of a folder upstream.
$ curl -uadmin "<ART_URL>/artifactory/hashicorp-folders-remote/terraform-provider-template/2.2.0/terraform-provider-template_2.2.0_darwin_amd64.zip"
{
"errors" : [ {
"status" : 400,
"message" : "Parent terraform-provider-template/2.2.0 must be a folder"
} ]
}
This is intended behavior. To remedy this we will have to account for both use cases; resolving folder locations and resolving files within those folders.
The SolutionThere will be three sections needed to accomplish this use case.
- A remote repository that resolves the actual artifacts.
- A remote repository that resolves the folders and does not resolve the actual artifacts.
- A virtual repository to funnel requests to the above two remote repositories.
To set up 1. we will create another remote repository in the same location. However, with this repository, we will need to add an include rule so that this repository will only resolve the actual artifacts.
The default includes patterns (**/*) allowing for the resolution of anything. For my particular use case, I am only interested in storing the actual artifacts so I will refine the included pattern so that this repository will only resolve items with extensions that I am interested in (**/*.zip, **/*.sig).
To set up 2. we will use the same “hashicorp-folders-remote” remote repository but will adjust the configurations.
We will add an exclude pattern for files with extensions so that those requests will go to the other remote repository.
We will also disable the “Store Artifacts Locally” setting in this repository in order for requests to be able to go into a folder within a folder. This setting will cause items not to be cached in Artifactory for this repository which will prevent the 400 error in the case of a Folder within a Folder scenario.
Finally for 3. we will create a virtual repository containing these two remote repositories in order to have one location to resolve the items from.
TestingNow that the setup has been completed we can test our previous use case.
$ curl -uadmin "<ART_URL>/artifactory/hashicorp-generic-virtual/terraform-provider-template/2.2.0"
Will still return the html output.
$ curl -uadmin "<ART_URL>/artifactory/hashicorp-generic-virtual/terraform-provider-template/2.2.0/terraform-provider-template_2.2.0_darwin_amd64.zip" -O
The follow-up request to an actual artifact is also able to resolve successfully and download and cache the file.