<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>golang, artifactory on gavincampbell.dev</title>
		<link>https://gavincampbell.dev/tags/golang-artifactory/</link>
		<description>Recent content in golang, artifactory on gavincampbell.dev</description>
		<generator>Hugo 0.121.2</generator>
		<language>en-gb</language>
		<managingEditor>gavin@gavincampbell.dev (Gavin Campbell)</managingEditor>
		<webMaster>gavin@gavincampbell.dev (Gavin Campbell)</webMaster>
		<copyright>
Except as otherwise noted, all blog posts, photos, graphics, presentations and other media on &lt;a property=&#34;dct:title&#34; rel=&#34;cc:attributionURL&#34; href=&#34;https://gavincampbell.dev&#34;&gt;gavincampbell.dev&lt;/a&gt; by &lt;a rel=&#34;cc:attributionURL dct:creator&#34; property=&#34;cc:attributionName&#34; href=&#34;https://gavincampbell.dev&#34;&gt;Gavin Campbell&lt;/a&gt; are licensed under &lt;a href=&#34;http://creativecommons.org/licenses/by/4.0/?ref=chooser-v1&#34; target=&#34;_blank&#34; rel=&#34;license noopener noreferrer&#34; style=&#34;display:inline-block;&#34;&gt;Attribution 4.0 International&lt;/a&gt;. Except as otherwise noted, all code snippets on &lt;a property=&#34;dct:title&#34; rel=&#34;cc:attributionURL&#34; href=&#34;https://gavincampbell.dev&#34;&gt;gavincampbell.dev&lt;/a&gt; by &lt;a rel=&#34;cc:attributionURL dct:creator&#34; property=&#34;cc:attributionName&#34; href=&#34;https://gavincampbell.dev&#34;&gt;Gavin Campbell&lt;/a&gt; are licensed under &lt;a href=&#34;http://creativecommons.org/publicdomain/zero/1.0?ref=chooser-v1&#34; target=&#34;_blank&#34; rel=&#34;license noopener noreferrer&#34; style=&#34;display:inline-block;&#34;&gt;CC0 1.0 Universal&lt;/a&gt;. Built with the &lt;a href=&#34;https://github.com/spookey/slick&#34;&gt;Slick&lt;/a&gt; theme for &lt;a href=&#34;https://gohugo.io/&#34;&gt;Hugo&lt;/a&gt;, hosted in the UK by &lt;a href=&#34;https://www.mythic-beasts.com//&#34;&gt;Mythic Beasts&lt;/a&gt;</copyright>
		<lastBuildDate>Mon, 24 Jan 2022 00:00:00 +0000</lastBuildDate>
		<atom:link href="https://gavincampbell.dev/tags/golang-artifactory/index.xml" rel="self" type="application/rss+xml"/>
		<item>
			<title>Setting up Jfrog Artifactory as a go proxy</title>
			<link>https://gavincampbell.dev/post/golang-proxy-artifactory/</link>
			<pubDate>Mon, 24 Jan 2022 00:00:00 +0000</pubDate>
			<author>gavin@gavincampbell.dev (Gavin Campbell)</author>
			<guid isPermaLink="true">https://gavincampbell.dev/post/golang-proxy-artifactory/</guid>
			<description>&lt;p&gt;In many environments, it isn&amp;rsquo;t really feasible to take build-time or even run-time dependencies on public repositories such as &lt;a href=&#34;https://www.npmjs.com/&#34;&gt;&lt;code&gt;npmjs&lt;/code&gt;&lt;/a&gt;, &lt;a href=&#34;https://www.nuget.org/&#34;&gt;&lt;code&gt;nuget&lt;/code&gt;&lt;/a&gt;, or &lt;a href=&#34;https://repo1.maven.org/maven2/&#34;&gt;&lt;code&gt;maven&lt;/code&gt; central&lt;/a&gt;,generally due to concerns about security, performance, availability, or all of the above.&lt;/p&gt;
&lt;p&gt;Historically the story for &lt;code&gt;go&lt;/code&gt; has been slightly different than for these other platforms, preferring to fetch dependencies directly from the source - often GitHub - rather than create a centralised registry of &lt;code&gt;go&lt;/code&gt; packages. For the last few versions however, the default behaviour of &lt;code&gt;go get&lt;/code&gt;, &lt;code&gt;go build&lt;/code&gt; and friends has been to use a publicly hosted proxy &lt;code&gt;proxy.golang.org&lt;/code&gt; when fetching dependencies.&lt;/p&gt;
&lt;p&gt;From an end user point of view, the &lt;a href=&#34;https://proxy.golang.org/&#34;&gt;golang proxy&lt;/a&gt; is functionally similar to those centralised registries and many organisations will want to create a local proxy using &lt;a href=&#34;https://jfrog.com/artifactory/&#34;&gt;JFrog Artifactory&lt;/a&gt;, &lt;a href=&#34;https://www.sonatype.com/products/repository-pro&#34;&gt;SonaType Nexus&lt;/a&gt;, or similar products built into CI/CD platforms such as &lt;a href=&#34;https://github.com/features/packages&#34;&gt;GitHub Packages&lt;/a&gt; or the &lt;a href=&#34;https://docs.gitlab.com/ee/user/packages/package_registry/index.html&#34;&gt;Gitlab Package Registry&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;I managed to get this working using Artifactory, but a couple of the steps were non-obvious, so I thought I&amp;rsquo;d write them down here for future reference. I am using the &lt;a href=&#34;https://jfrog.com/start-free/&#34;&gt;Artifactory Free Tier&lt;/a&gt; on AWS, but I don&amp;rsquo;t suppose this makes much difference.&lt;/p&gt;
&lt;p&gt;In summary, you have to create a remote repository pointing to the golang proxy, a virtual repository pointing to your remote repository, and conigure your client correctly. More detailed instructions follow.&lt;/p&gt;
&lt;h2 id=&#34;a-sample-application&#34;&gt;A sample application&lt;/h2&gt;
&lt;p&gt;I created a small application with a couple of external dependencies, which you can clone from &lt;a href=&#34;https://github.com/gavincampbell-dev-example-repos/gcgreeter&#34;&gt;https://github.com/gavincampbell-dev-example-repos/gcgreeter&lt;/a&gt; if you want to play along. It is &amp;ldquo;heavily inspired&amp;rdquo; by the &lt;a href=&#34;https://go.dev/doc/tutorial/getting-started#call&#34;&gt;example in the go tutorial&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;step-1-create-a-remote-repository-pointing-to-proxygolangorg&#34;&gt;Step 1 Create a remote repository pointing to &lt;code&gt;proxy.golang.org&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;If you don&amp;rsquo;t have a &amp;ldquo;project&amp;rdquo; created in Artifactory you&amp;rsquo;ll have to create one for the subsequent steps to work.&lt;/p&gt;
&lt;p&gt;The first step is to create the remote repository, which will point at the golang proxy. Everything here is left at its default value, the only items I set were the repository key and the remote url.&lt;/p&gt;
&lt;figure&gt;&lt;img src=&#34;remote-repo-settings.png&#34;
         alt=&#34;Settings for the remote repository&#34;/&gt;&lt;figcaption&gt;
            &lt;p&gt;Settings for the remote repository&lt;/p&gt;
        &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;This is where I ran into the first gotcha, the &amp;ldquo;Set Me Up&amp;rdquo; dialog looks as if it will let you use &lt;code&gt;go get&lt;/code&gt; to fetch from this repository, but this turns out not to be the case.&lt;/p&gt;
&lt;figure&gt;&lt;img src=&#34;artifactory-set-me-up-remote.png&#34;
         alt=&#34;&amp;amp;ldquo;Set Me Up&amp;amp;rdquo;. And no, that&amp;amp;rsquo;s not a working API key.&#34;/&gt;&lt;figcaption&gt;
            &lt;p&gt;&amp;ldquo;Set Me Up&amp;rdquo;. And no, that&amp;rsquo;s not a working API key.&lt;/p&gt;
        &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Following these steps will lead to the following error:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;&lt;span style=&#34;color:#f92672&#34;&gt;[&lt;/span&gt;liveuser@localhost-live gcgreeter&lt;span style=&#34;color:#f92672&#34;&gt;]&lt;/span&gt;$ go get
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;go: github.com/fsnotify/fsnotify@v1.5.1: reading https://gavin%40gavincampbell.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;dev:xxxxx@washbrook.jfrog.io/artifactory/api/go/gcd-goproxy/github.com/fsnotify/fsnotify/@v/v1.
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;5.1.mod:400 Bad Request
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;hr&gt;
&lt;p&gt;It turns out, quite a long way down the instructions page, that











  
  
  
  





      
      
      
    
     
      
    
    

    
    
         
         
         

         
        
        
        
        
        
        
      
      
    
  


&lt;blockquote&gt;
  &lt;p&gt;Artifactory only supports resolution of Go packages from virtual Go repositories. To resolve Go from other local or remote Go repositories, you need to aggregate them in a virtual Go repository.&lt;/p&gt;
  &lt;footer&gt;
    &lt;strong&gt;&lt;/strong&gt;
    
      
        &lt;cite&gt;
          &lt;a href=&#34;https://www.jfrog.com/confluence/display/JFROG/Go&amp;#43;Registry#GoRegistry-ResolvingGoProjects&#34; title=&#34;https://www.jfrog.com/confluence/display/JFROG/Go&amp;#43;Registry#GoRegistry-ResolvingGoProjects&#34;&gt;jfrog.com/confluence/display/...&lt;/a&gt; 
        &lt;/cite&gt;
      
    
  &lt;/footer&gt;
&lt;/blockquote&gt;
&lt;/p&gt;
&lt;h2 id=&#34;step-2-create-a-virtual-repo-pointing-to-your-remote-repo&#34;&gt;Step 2: Create a virtual repo pointing to your remote repo&lt;/h2&gt;
&lt;p&gt;This is the configuration screen for the virtual repository.&lt;/p&gt;
&lt;figure&gt;&lt;img src=&#34;virtual-repo-setup.png&#34;
         alt=&#34;Setting up the virtual repository in Artifactory&#34;/&gt;&lt;figcaption&gt;
            &lt;p&gt;Setting up the virtual repository in Artifactory&lt;/p&gt;
        &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Again, everything here is the default, I just supplied the repository key. There&amp;rsquo;s another gotcha here though, my &lt;code&gt;gcd-goproxy&lt;/code&gt;repo is &lt;em&gt;selected&lt;/em&gt;, but  it hasn&amp;rsquo;t been &lt;em&gt;included&lt;/em&gt; in the list of repositories backing the virtual repository.&lt;/p&gt;
&lt;p&gt;The symptom of this is a 404 error trying to retrieve packages&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go get
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;go: github.com/fsnotify/fsnotify@v1.5.1: reading https://gavin%40gavincampbell.dev:xxxxx@washbrook.jfrog.io/artifactory/api/go/gcd-golang-virtual/github.com/fsnotify/fsnotify/@v/v1.5.1.mod: &lt;span style=&#34;color:#ae81ff&#34;&gt;404&lt;/span&gt; Not Found
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This wasn&amp;rsquo;t obvious to me, but you have to &lt;em&gt;move&lt;/em&gt; the name of your remote repository into the &amp;ldquo;Included&amp;rdquo; column for this to work.&lt;/p&gt;
&lt;figure&gt;&lt;img src=&#34;move-remote-to-included.png&#34;
         alt=&#34;Setting up the virtual repository in Artifactory, second attempt&#34;/&gt;&lt;figcaption&gt;
            &lt;p&gt;Setting up the virtual repository in Artifactory, second attempt&lt;/p&gt;
        &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;If you have any private go repositories set up in artifactory, you can add them here too so that all your requests go to the same endpoint.&lt;/p&gt;
&lt;h2 id=&#34;step-3-using-the-virtual-repository&#34;&gt;Step 3: Using the virtual repository&lt;/h2&gt;
&lt;p&gt;To test this, I made a few changes in the &lt;code&gt;/etc/hosts&lt;/code&gt; file:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ cat /etc/hosts
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;0.0.0.0     proxy.golang.org
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;0.0.0.0     github.com
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;0.0.0.0     sum.golang.org
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;0.0.0.0     index.golang.org
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will make requests to all of these urls fail- if you are following along at home you might want to clone the repo &lt;em&gt;before&lt;/em&gt; you do this.&lt;/p&gt;
&lt;p&gt;Next, I emptied the module cache to make sure we weren&amp;rsquo;t cheating.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go clean -modcache
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Assuming $GOPROXY is not set (&lt;code&gt;unset $GOPROXY&lt;/code&gt; to make sure), go will attempt to download dependencies from &lt;code&gt;proxy.golang.org&lt;/code&gt; and this will fail:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go get
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;go: github.com/fsnotify/fsnotify@v1.5.1: Get &lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;https://proxy.golang.org/github.com/fsnotify/fsnotify/@v/v1.5.1.mod&amp;#34;&lt;/span&gt;: dial tcp 0.0.0.0:443: connect: connection refused
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Finally, we can return to the &amp;ldquo;Set Me Up&amp;rdquo; dialog in Artifactory and copy the correct configuration.&lt;/p&gt;
&lt;figure&gt;&lt;img src=&#34;artifactory-set-me-up-correct.png&#34;
         alt=&#34;&amp;amp;ldquo;Set Me Up&amp;amp;rdquo;. Still not a working API key.&#34;/&gt;&lt;figcaption&gt;
            &lt;p&gt;&amp;ldquo;Set Me Up&amp;rdquo;. Still not a working API key.&lt;/p&gt;
        &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Having done this, we are able to build and run the application successfully.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;&#34;&gt;&lt;code class=&#34;language-bash&#34; data-lang=&#34;bash&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;export GOPROXY&lt;span style=&#34;color:#f92672&#34;&gt;=&lt;/span&gt;&lt;span style=&#34;color:#e6db74&#34;&gt;&amp;#34;https://gavin@gavincampbell.dev:AKCp8k...ssg@washbrook.jfrog.io/artifactory/api/go/gcd-golang-virtual&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ go build
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;go: downloading rsc.io/quote v1.5.2
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;go: downloading github.com/spf13/cobra v1.3.0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;go: downloading rsc.io/sampler v1.3.0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;go: downloading golang.org/x/text v0.3.7
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;go: downloading github.com/spf13/pflag v1.0.5
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;$ ./gcgreeter greet
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;Hello, world.
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
		</item>
	</channel>
</rss>
