ip2k / google-api-ruby-client

Automatically exported from code.google.com/p/google-api-ruby-client
Apache License 2.0
0 stars 0 forks source link

The Google::APIClient::PKCS12.load_key fails when passed the contents of a key file instead of the file name #62

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Download a key file
2. Read the contents using File.read command
3. Pass it on to the function

What is the expected output? What do you see instead?

I expect a successful call and get an exception instead.

ArgumentError: string contains null byte
from 
/Users/timuckun/.rvm/gems/ree-1.8.7-2012.02/gems/google-api-client-0.4.5/lib/goo
gle/api_client/service_account.rb:38:in `exists?'

File.exists? is choking when passed the contents of a key file. I suggest you 
change that line to 

 content = File.read(keyfile) rescue keyfile

What version of the product are you using? On what operating system?

google-api-client-0.4.5
ruby 1.8.7

Please provide any additional information below.

Original issue reported on code.google.com by tim.uc...@digitaldialogue.com.au on 13 Sep 2012 at 12:19

GoogleCodeExporter commented 9 years ago
The error occurs on File.exists? not File.read 

the following fixes the problem

def self.load_key(keyfile, passphrase)
        begin
          begin
            content = File.read(keyfile)
          rescue Errno::ENOENT, ArgumentError
            content = keyfile
          end

          pkcs12 = OpenSSL::PKCS12.new(content, passphrase)
          return pkcs12.key
        rescue OpenSSL::PKCS12::PKCS12Error
          raise ArgumentError.new("Invalid keyfile or passphrase")
        end
      end

Original comment by tim.uc...@digitaldialogue.com.au on 13 Sep 2012 at 12:40

GoogleCodeExporter commented 9 years ago
I'm strongly inclined to say that this should either be split into two methods, 
load_key and load_key_file or it should use optional named parameters that 
explicitly indicate whether this is a raw key or a filename.

Original comment by bobaman@google.com on 13 Sep 2012 at 4:10

GoogleCodeExporter commented 9 years ago
I think the two call idea is great because the load_key_from_file method can 
call the load_key method

Original comment by tim.uc...@digitaldialogue.com.au on 13 Sep 2012 at 5:05

GoogleCodeExporter commented 9 years ago
This will be fixed in the next version. Did a bit more refactoring since we're 
likely moving to generating keys in PEM format instead. 

Original comment by sba...@google.com on 2 Nov 2012 at 8:58