Search code examples
ruby-on-railsrubygoogle-bigquerygoogle-api-ruby-client

How to pass keyfile as argument in Rails project


I'm using the bigQuery gem in my project. I initialize it with options client_id, service_email, key, project_id, and dataset. I have my .p12 file from Google that I want to pass in but I'm not sure where to put the file in my Rails project - I tried putting it in the same directory as my config file and passing the relative path name, but I'm getting an invalid passphrase error. Specifically, line 10 is throwing an error when load_key is being called:

key = Google::APIClient::PKCS12.load_key(
  opts['key'],
  "notasecret"
)

So clearly it's not loading the key file correctly. I'm terrible at Rails asset control - where should I put my keyfile and what pathname should I pass in my options hash?


Solution

  • You could place the keyfile into the config directory, then do the following:

    opts['key'] = Rails.root.join('config','nameofkeyfile.p12').to_s
    

    You don't want the key to be in a location that your application will serve up to the public, so config sounds like a good location to me.

    You can experiment with the block you have above in the Rails console:

    # run `rails c` then
    keypath = Rails.root.join('config','nameofkeyfile.p12').to_s
    key = Google::APIClient::PKCS12.load_key(keypath, "notasecret")
    

    Looking at the Google::APIClient documentation, I see load_key is deprecated. They recommend using Google::APIClient::KeyUtils instead.

    key = Google::APIClient::KeyUtils.load_from_pkcs12(keyfile, "notasecret")
    

    As for a quick overview of the Rails asset pipeline, see here. (Please pardon the "for dummies" part of that url, it appears to be good, quick info.)