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

Crt key and get artifacts by path #302

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions lib/jenkins_api_client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class Client
"http_open_timeout",
"http_read_timeout",
"ssl",
"crt",
"key",
"verify_mode",
"pkcs_file_path",
"pass_phrase",
"ca_file",
Expand Down Expand Up @@ -95,6 +98,10 @@ class Client
# @option args [String] :proxy_protocol the proxy protocol ('socks' or 'http' (defaults to HTTP)
# @option args [String] :jenkins_path ("/") the optional context path for Jenkins
# @option args [Boolean] :ssl (false) indicates if Jenkins is accessible over HTTPS
# @option args [String] :crt path to the crt pem for authenticating with Jenkins. See :key options
# @option args [String] :key path to the key pem for authenticating with Jenkins. The Crt and Key files are usefull for authenticating with untrusted host
# and would be set to http.key and http.crt
# @option args [String] :verify_mode one of OpenSSL::SSL::VERIFY_* or nil to leave for default values
# @option args [String] :pkcs_file_path ("/") the optional context path for pfx or p12 binary certificate file
# @option args [String] :pass_phrase password for pkcs_file_path certificate file
# @option args [String] :ca_file the path to a PEM encoded file containing trusted certificates used to verify peer certificate
Expand Down Expand Up @@ -293,6 +300,26 @@ def get_artifact(job_name,filename)
end
end

# Connects to the server and downloads artifacts with a given path to a specified location
#
# @param [String] job_name
# @param [String] build_number
# @param [String] artifact_path
# @param [String] filename location to save artifact
#
def get_artifact_by_path(job_name,build_number,artifact_path,filename)
build_path = job.build_path(job_name, build_number)
uri= "#{@jenkins_path}/#{build_path}/#{artifact_path}"
response = make_http_request(Net::HTTP::Get.new(uri))
if response.code == "200"
File.open(File.expand_path(filename),"wb") do |file|
file.write(response.body)
end
else
raise "Couldn't get the artifact"
end
end

# Connects to the server and download all artifacts of a build to a specified location
#
# @param [String] job_name
Expand All @@ -306,7 +333,7 @@ def get_artifacts(job_name, dldir, build_number = nil)
@artifacts.each do |artifact|
uri = URI.parse(artifact)
http = Net::HTTP.new(uri.host, uri.port)
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.verify_mode = @verify_mode || OpenSSL::SSL::VERIFY_NONE
http.use_ssl = @ssl
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth(@username, @password)
Expand Down Expand Up @@ -358,17 +385,22 @@ def make_http_request(request, follow_redirect = @follow_redirects)
pkcs12 =OpenSSL::PKCS12.new(File.binread(@pkcs_file_path), @pass_phrase!=nil ? @pass_phrase : "")
http.cert = pkcs12.certificate
http.key = pkcs12.key
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.verify_mode = @verify_mode || OpenSSL::SSL::VERIFY_NONE
elsif @ssl
http.use_ssl = true

http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.cert = OpenSSL::X509::Certificate.new(@crt) if @crt
http.key = OpenSSL::PKey::RSA.new(@key) if @key

# Why is this here a VERIFY_PEER and the rest are VERIFY_NONE?
http.verify_mode = @verify_mode || OpenSSL::SSL::VERIFY_PEER
http.ca_file = @ca_file if @ca_file
end
http.open_timeout = @http_open_timeout
http.read_timeout = @http_read_timeout

response = http.request(request)
@cookies = response["set-cookie"]
case response
when Net::HTTPRedirection then
# If we got a redirect request, follow it (if flag set), but don't
Expand Down
4 changes: 4 additions & 0 deletions lib/jenkins_api_client/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,10 @@ def find_artifact(job_name, build_number = 0)
find_artifacts(job_name, build_number).first
end

def build_path(job_name, build_number = 0)
"job/#{path_encode job_name}/#{build_number}"
end

#A Method to check artifact exists path from the Current Build
#
# @param [String] job_name
Expand Down