Created
January 15, 2025 17:25
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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