Google Cloud KMS error when getting public key

Viewed 28

I'm following this Google tutorial to access my KMS public key value in Nodejs app, apparently I have an error due to missing permissions (because I can read the value from the console gcloud kms keys versions get-public-key). The error :

Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
    at GoogleAuth.getApplicationDefaultAsync

I created a service account with the required permissions (to read), how can I instantiate a KMS client with it ? (const client = new KeyManagementServiceClient();)

Thanks

EDIT 1 : I can get the key when I set an env var with the location of the SA : export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

How can I use the file itself (service-account-file.json) instead of env var ?

1 Answers

I believe it should be as simple as

const client = new KeyManagementServiceClient({
  keyFilename: '/path/to/keyfile.json'
}); 

Sorry this isn't totally clear: this isn't documented with a sample anywhere as far as I can tell, and the keyFilename property comes from three levels up the inheritance hierarchy of ClientOptions.

Related