Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save obahareth/dacaa989bd6b70d131624184f5a55d29 to your computer and use it in GitHub Desktop.
Save obahareth/dacaa989bd6b70d131624184f5a55d29 to your computer and use it in GitHub Desktop.
Converts service account credentials to YML for adding to Rails encrypted credentials in an easier way.
require "json"
require "yaml"
# Read the JSON key file
credentials_path = "path/to/file.json"
json_key = JSON.parse(File.read(credentials_path)
# Structure the data for Rails credentials
credentials = {
"gcs" => {
"bucket_name" => {
"type" => json_key["type"],
"project_id" => json_key["project_id"],
"private_key_id" => json_key["private_key_id"],
"private_key" => json_key["private_key"],
"client_email" => json_key["client_email"],
"client_id" => json_key["client_id"],
"auth_uri" => json_key["auth_uri"],
"token_uri" => json_key["token_uri"],
"auth_provider_x509_cert_url" => json_key["auth_provider_x509_cert_url"],
"client_x509_cert_url" => json_key["client_x509_cert_url"]
}
}
}
# Convert to YAML with proper formatting
yaml_output = credentials.to_yaml
# Update the yaml to format private_key with literal block scalar (|)
yaml_output.gsub!(/private_key: "(.+?)"/m, 'private_key: |\n\1')
# Remove quotes around the private key content
yaml_output.gsub!(/^(\s+)(-----[^\n]+)$/m, '\1\2')
puts yaml_output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment