Skip to content

Commit 34b7535

Browse files
Aliaksei SarychauAliaksei Sarychau
Aliaksei Sarychau
authored and
Aliaksei Sarychau
committed
artifactory auth for latest url and checksum
1 parent 68bea2d commit 34b7535

File tree

7 files changed

+43
-12
lines changed

7 files changed

+43
-12
lines changed

Diff for: REFERENCE.md

+14-2
Original file line numberDiff line numberDiff line change
@@ -1094,7 +1094,7 @@ Type: Ruby 4.x API
10941094

10951095
A function that returns the checksum value of an artifact stored in Artifactory
10961096

1097-
#### `archive::artifactory_checksum(Stdlib::HTTPUrl $url, Optional[Enum['sha1','sha256','md5']] $checksum_type)`
1097+
#### `archive::artifactory_checksum(Stdlib::HTTPUrl $url, Optional[Enum['sha1','sha256','md5']] $checksum_type, Optional[Array] $headers)`
10981098

10991099
The archive::artifactory_checksum function.
11001100

@@ -1113,13 +1113,19 @@ Data type: `Optional[Enum['sha1','sha256','md5']]`
11131113
The checksum type.
11141114
Note the function will raise an error if you ask for sha256 but your artifactory instance doesn't have the sha256 value calculated.
11151115

1116+
##### `headers`
1117+
1118+
Data type: `Optional[Array]`
1119+
1120+
Array of headers to pass source, like an authentication token
1121+
11161122
### <a name="archive--artifactory_latest_url"></a>`archive::artifactory_latest_url`
11171123

11181124
Type: Ruby 4.x API
11191125

11201126
The archive::artifactory_latest_url function.
11211127

1122-
#### `archive::artifactory_latest_url(Variant[Stdlib::HTTPUrl, Stdlib::HTTPSUrl] $url, Hash $maven_data)`
1128+
#### `archive::artifactory_latest_url(Variant[Stdlib::HTTPUrl, Stdlib::HTTPSUrl] $url, Hash $maven_data, Optional[Array ] $headers)`
11231129

11241130
The archive::artifactory_latest_url function.
11251131

@@ -1137,6 +1143,12 @@ Data type: `Hash`
11371143

11381144

11391145

1146+
##### `headers`
1147+
1148+
Data type: `Optional[Array ]`
1149+
1150+
1151+
11401152
### <a name="archive--parse_artifactory_url"></a>`archive::parse_artifactory_url`
11411153

11421154
Type: Ruby 4.x API

Diff for: lib/puppet/functions/archive/artifactory_checksum.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@
88
# @param url The URL of the artifact.
99
# @param checksum_type The checksum type.
1010
# Note the function will raise an error if you ask for sha256 but your artifactory instance doesn't have the sha256 value calculated.
11+
# @param headers Array of headers to pass source, like an authentication token
1112
# @return [String] Returns the checksum.
1213
dispatch :artifactory_checksum do
1314
param 'Stdlib::HTTPUrl', :url
1415
optional_param "Enum['sha1','sha256','md5']", :checksum_type
16+
optional_param 'Array', :headers
1517
return_type 'String'
1618
end
1719

18-
def artifactory_checksum(url, checksum_type = 'sha1')
20+
def artifactory_checksum(url, checksum_type = 'sha1', headers = [])
1921
uri = URI(url.sub('/artifactory/', '/artifactory/api/storage/'))
2022

21-
response = PuppetX::Bodeco::Util.content(uri)
23+
options = {}
24+
options[:headers] = headers if headers != []
25+
response = PuppetX::Bodeco::Util.content(uri, options)
2226
content = JSON.parse(response)
2327

2428
checksum = content['checksums'] && content['checksums'][checksum_type]

Diff for: lib/puppet/functions/archive/artifactory_latest_url.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
dispatch :artifactory_latest_url do
88
param 'Variant[Stdlib::HTTPUrl, Stdlib::HTTPSUrl]', :url
99
param 'Hash', :maven_data
10+
optional_param 'Array ', :headers
1011
end
1112

12-
def artifactory_latest_url(url, maven_data)
13+
def artifactory_latest_url(url, maven_data, headers = [])
1314
# Turn provided artifactory URL into the fileinfo API endpoint of the parent directory
1415
uri = URI(url.sub('/artifactory/', '/artifactory/api/storage/')[%r{^(.*)/.*$}, 1])
1516

16-
response = PuppetX::Bodeco::Util.content(uri)
17+
options = {}
18+
options[:headers] = headers if headers != []
19+
20+
response = PuppetX::Bodeco::Util.content(uri, options)
1721
content = JSON.parse(response)
1822

1923
uris = if maven_data['classifier']
@@ -33,7 +37,7 @@ def artifactory_latest_url(url, maven_data)
3337

3438
# Now GET the fileinfo endpoint of the resolved latest version file
3539
uri = URI("#{content['uri']}#{latest}")
36-
response = PuppetX::Bodeco::Util.content(uri)
40+
response = PuppetX::Bodeco::Util.content(uri, options)
3741
content = JSON.parse(response)
3842

3943
url = content['downloadUri']

Diff for: lib/puppet_x/bodeco/util.rb

+5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def initialize(_url, options)
7272
@password = options[:password]
7373
@cookie = options[:cookie]
7474
@insecure = options[:insecure]
75+
@headers = options[:headers].nil? ? [] : options[:headers]
7576

7677
if options[:proxy_server]
7778
uri = URI(options[:proxy_server])
@@ -87,6 +88,10 @@ def generate_request(uri)
8788
header = @cookie && { 'Cookie' => @cookie }
8889

8990
request = Net::HTTP::Get.new(uri.request_uri, header)
91+
@headers.each do |h|
92+
h_split = h.split(':')
93+
request[h_split[0].strip] = h_split[1].strip if h_split.length == 2
94+
end
9095
request.basic_auth(@username, @password) if @username && @password
9196
request
9297
end

Diff for: manifests/artifactory.pp

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@
8484
# Only Artifactory Pro downloads this directly but the corresponding file endpoint (where the sha1 checksum is published) doesn't exist
8585
# This means we can't use the artifactory_sha1 function
8686

87-
$latest_url_data = archive::artifactory_latest_url($url, $maven2_data)
87+
$latest_url_data = archive::artifactory_latest_url($url, $maven2_data, $headers)
8888

8989
$file_url = $latest_url_data['url']
9090
$sha1 = $latest_url_data['sha1']
9191
} else {
9292
$file_url = $url
93-
$sha1 = archive::artifactory_checksum($url,'sha1')
93+
$sha1 = archive::artifactory_checksum($url,'sha1', $headers)
9494
}
9595

9696
archive { $file_path:

Diff for: spec/defines/artifactory_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# Mock Puppet V4 API ruby function with a puppet language function equivalent
99
let(:pre_condition) do
10-
'function archive::artifactory_checksum($url,$type) { return \'0d4f4b4b039c10917cfc49f6f6be71e4\' }'
10+
'function archive::artifactory_checksum($url,$type,$headers) { return \'0d4f4b4b039c10917cfc49f6f6be71e4\' }'
1111
end
1212

1313
context 'artifactory archive with defaults' do

Diff for: spec/functions/artifactory_checksum_spec.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,24 @@
66
let(:example_json) { File.read(fixtures('checksum', 'artifactory.json')) }
77
let(:url) { 'https://repo.jfrog.org/artifactory/distributions/images/Artifactory_120x75.png' }
88
let(:uri) { URI(url.sub('/artifactory/', '/artifactory/api/storage/')) }
9+
let(:headers) { ['X-JFrog-Art-Api: ABC123'] }
910

1011
it { is_expected.not_to be_nil }
1112
it { is_expected.to run.with_params.and_raise_error(ArgumentError) }
1213
it { is_expected.to run.with_params('not_a_url').and_raise_error(ArgumentError) }
1314

1415
it 'defaults to and parses sha1' do
15-
allow(PuppetX::Bodeco::Util).to receive(:content).with(uri).and_return(example_json)
16+
allow(PuppetX::Bodeco::Util).to receive(:content).with(uri, {}).and_return(example_json)
1617
expect(subject).to run.with_params(url).and_return('a359e93636e81f9dd844b2dfb4b89fa876e5d4fa')
1718
end
1819

1920
it 'parses md5' do
20-
allow(PuppetX::Bodeco::Util).to receive(:content).with(uri).and_return(example_json)
21+
allow(PuppetX::Bodeco::Util).to receive(:content).with(uri, {}).and_return(example_json)
2122
expect(subject).to run.with_params(url, 'md5').and_return('00f32568be85929fe95be38f9f5f3519')
2223
end
24+
25+
it 'uses auth headers' do
26+
allow(PuppetX::Bodeco::Util).to receive(:content).with(uri, {}).and_return(example_json)
27+
expect(subject).to run.with_params(url, 'sha1', headers).and_return('a359e93636e81f9dd844b2dfb4b89fa876e5d4fa')
28+
end
2329
end

0 commit comments

Comments
 (0)