Search code examples
environment-variablesgoogle-cloud-storageelixirproduction

How to put google credential json in ENV variable for production?


I cant find how to inject the google Json credential file into an ENV variable for production. The dependency I use is called goth for elixir, and I am supposed to feed it with those credentials on app startup.

For the json, I encoded Google's file with Poison.encode!() and use it as a text in my ENV variables.

in my dev config file : it works

config :goth, json: {:system, "GCP_CREDENTIALS"}

in my prod config file : it doens't work and I keep getting a syntax error at the beginning of the json

config :goth, json: {:system, '${GCP_CREDENTIALS}'}

Any suggestion or anybody who made this working and could share about his experience ?


Solution

  • Looking at the goth README it looks like you have a couple options.

    1. Load the file into the config via: config :goth, json: "path/to/google/json/creds.json" |> File.read!
    2. Have the entire contents of the file in an env var and do: config :goth, json: {:system, "GCP_CREDENTIALS"}
    3. Via your own module: config :goth, config_module: MyConfigMod and then
    
    defmodule MyConfigMod do
      use Goth.Config
    
      def init(config) do
        {:ok, Keyword.put(config, :json, System.get_env("MY_GCP_JSON_CREDENTIALS"))}
      end
    end
    

    I could be wrong about the "${GCP_CREDENTIALS}" in the config, but the only place I have seen "${...}" used before is in distillery when you use REPLACE_OS_VARS=true. If you are indeed using distillery, then you do not need the {:system, "${GCP_CREDENTIALS}"} tuple, you can just put config :goth, json: "${GCP_CREDENTIALS}" where the GCP_CREDENTIALS env var contains the contents of the credentials json file.

    Source of examples: https://github.com/peburrows/goth#installation

    Distillery docs: https://hexdocs.pm/distillery/runtime-configuration.html#vm-args