-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from cyberark/170-improve-unit-tests
170 improve unit tests
- Loading branch information
Showing
15 changed files
with
663 additions
and
485 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'conjur/puppet_module/ssl' | ||
|
||
module Conjur | ||
module PuppetModule | ||
# This module is in charge of interacting with the Conjur endpoints | ||
module HTTP | ||
class << self | ||
def directory_uri(url) | ||
url += '/' unless url.end_with? '/' | ||
URI url | ||
end | ||
|
||
def get(host_url, path, ssl_certificate, token) | ||
uri = directory_uri(host_url) + path | ||
use_ssl = uri.scheme == 'https' | ||
certs = Conjur::PuppetModule::SSL.load(ssl_certificate) | ||
|
||
headers = {} | ||
if token | ||
encoded_token = Base64.urlsafe_encode64(token) | ||
headers['Authorization'] = "Token token=\"#{encoded_token}\"" | ||
end | ||
|
||
Net::HTTP.start uri.host, uri.port, use_ssl: use_ssl, cert_store: certs do |http| | ||
response = http.get(uri.request_uri, headers) | ||
|
||
raise Net::HTTPError.new response.message, response unless response.code.match?(%r{^2}) | ||
|
||
response.body | ||
end | ||
end | ||
|
||
def post(host_url, path, ssl_certificate, data) | ||
uri = directory_uri(host_url) + path | ||
|
||
raise(ArgumentError, "POST data to #{uri} must not be empty!") \ | ||
if data.nil? || data.empty? | ||
|
||
use_ssl = uri.scheme == 'https' | ||
certs = Conjur::PuppetModule::SSL.load(ssl_certificate) | ||
|
||
Net::HTTP.start uri.host, uri.port, use_ssl: use_ssl, cert_store: certs do |http| | ||
response = http.post(uri.request_uri, data) | ||
|
||
raise Net::HTTPError.new response.message, response unless response.code.match?(%r{^2}) | ||
|
||
response.body | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module Conjur | ||
module PuppetModule | ||
# This module is a bundle of helper methods for handling the SSL and certificate | ||
# logic | ||
module SSL | ||
class << self | ||
def load(ssl_certificate) | ||
if ssl_certificate.nil? || ssl_certificate.empty? | ||
Puppet.warning('No Conjur SSL certificate - YOU ARE VULNERABLE TO MITM ATTACKS!') | ||
return [] | ||
end | ||
|
||
cert_store = OpenSSL::X509::Store.new | ||
parsed_certs = parse_certs(ssl_certificate) | ||
|
||
Puppet.info("Parsed #{parsed_certs.length} certificate(s) from SSL cert chain") | ||
|
||
parsed_certs.each do |x509_cert| | ||
cert_store.add_cert x509_cert | ||
end | ||
|
||
cert_store | ||
end | ||
|
||
def parse_certs(certs) | ||
cert_header = '-----BEGIN CERTIFICATE-----' | ||
cert_footer = '-----END CERTIFICATE-----' | ||
cert_re = %r{#{cert_header}\r?\n.*?\r?\n#{cert_footer}}m | ||
|
||
certs.scan(cert_re).map(&OpenSSL::X509::Certificate.method(:new)) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.