Skip to content

Commit

Permalink
Merge pull request #36 from adamsb6/fix/34
Browse files Browse the repository at this point in the history
dynamically install rest-client fix #34
joekiller committed Dec 9, 2014
2 parents 2f19036 + b09536d commit cf45245
Showing 2 changed files with 18 additions and 10 deletions.
18 changes: 12 additions & 6 deletions libraries/s3_file.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
require 'rest-client'

require 'time'
require 'openssl'
require 'base64'

module S3FileLib
BLOCKSIZE_TO_READ = 1024 * 1000 unless const_defined?(:BLOCKSIZE_TO_READ)
RestClient.proxy = ENV['http_proxy']

def self.build_headers(date, authorization, token)
headers = {
@@ -24,13 +23,14 @@ def self.get_md5_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key
end

def self.get_digests_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key,token)
client = self.client
now, auth_string = get_s3_auth("HEAD", bucket,path,aws_access_key_id,aws_secret_access_key, token)

headers = build_headers(now, auth_string, token)

url = "https://#{bucket}.s3.amazonaws.com" if url.nil?

response = RestClient.head("#{url}#{path}", headers)
response = client.head("#{url}#{path}", headers)

etag = response.headers[:etag].gsub('"','')
digest = response.headers[:x_amz_meta_digest]
@@ -39,14 +39,14 @@ def self.get_digests_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access
return {"md5" => etag}.merge(digests)
end

def self.get_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key,token)
def self.get_from_s3(bucket,url,path,aws_access_key_id,aws_secret_access_key,token)
client = self.client
now, auth_string = get_s3_auth("GET", bucket,path,aws_access_key_id,aws_secret_access_key, token)

url = "https://#{bucket}.s3.amazonaws.com" if url.nil?

headers = build_headers(now, auth_string, token)
# response = RestClient.get('https://%s.s3.amazonaws.com%s' % [bucket,path], headers)
response = RestClient::Request.execute(:method => :get, :url => "#{url}#{path}", :raw_response => true, :headers => headers)
response = client::Request.execute(:method => :get, :url => "#{url}#{path}", :raw_response => true, :headers => headers)

return response
end
@@ -122,4 +122,10 @@ def self.verify_md5_checksum(checksum, file)

local_md5.hexdigest == s3_md5
end

def self.client
require 'rest-client'
RestClient.proxy = ENV['http_proxy']
RestClient
end
end
10 changes: 6 additions & 4 deletions providers/default.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require 'digest/md5'
require 'rest-client'
require 'json'

use_inline_resources

action :create do
rest_client_gem = Chef::Resource::ChefGem.new('rest-client', @run_context)
rest_client_gem.run_action :install
client = S3FileLib::client
download = true

# handle key specified without leading slash
@@ -20,12 +22,12 @@
if aws_access_key_id.nil? && aws_secret_access_key.nil? && token.nil?
instance_profile_base_url = 'http://169.254.169.254/latest/meta-data/iam/security-credentials/'
begin
instance_profiles = RestClient.get(instance_profile_base_url)
rescue RestClient::ResourceNotFound, Errno::ETIMEDOUT # we can either 404 on an EC2 instance, or timeout on non-EC2
instance_profiles = client.get(instance_profile_base_url)
rescue client::ResourceNotFound, Errno::ETIMEDOUT # we can either 404 on an EC2 instance, or timeout on non-EC2
raise ArgumentError.new 'No credentials provided and no instance profile on this machine.'
end
instance_profile_name = instance_profiles.split.first
instance_profile = JSON.load(RestClient.get(instance_profile_base_url + instance_profile_name))
instance_profile = JSON.load(client.get(instance_profile_base_url + instance_profile_name))

aws_access_key_id = instance_profile['AccessKeyId']
aws_secret_access_key = instance_profile['SecretAccessKey']

0 comments on commit cf45245

Please sign in to comment.