Problems on Accessing Secret Manager on Google Cloud - google-cloud-platform

I am trying to find a way to access Secret Manager Version (Service accountkey file) into an App hosted On-Premise. This link has an example on how to access it through C# via secret manager client API but the problem here is it requires having an ADC which is not possible on those servers.
I couldn't find anywhere but does SecretManager Client API supports API Key ? if not what all options I have ?
Also there is Workload Identity Federation service but there is no .net Client API's available
//Uses ADC
SecretManagerServiceClient client = SecretManagerServiceClient.Create();
Also there is a SecretManagerServiceClientBuilder but there is no documentation on how it can be used

To use ADC, you don't need to install gcloud SDK on your servers (answer to your comment). You need to set en environment variable GOOGLE_APPLICATION_CREDENTIALS with the absolute path to the service account key file as value. That's all, the library will automatically detect this env var and use the file as credential.
If you don't put this file, and if your app doesn't run on Google Cloud, the libraries look into the "well-known location". This location is the standard location of the user credential file created with the command google auth application-default login.
If you have read carefully, the ADC with the env var use a service account credential, and the well known location use a user credential. You use user credential in your personal computer/workstation. In production and on app, you use "app identity", a "service identity", therefore a service account.
Service account key file are made for that: provide a service identity to app running outside GCP and authenticate it to interact with GCP. However, this file contain a secret, and you need to secure it cautiously, and to rotate the file as soon as you can in your process.

Related

How can I create a firebase function that disables billing on my application if the bill is above a threashhold?

I've read much documentation but all of them require me to oauth2 tokens or set environment variables to a json file that contains my credentials... That's nice when I run this locally, but how would I run this in the cloud? Such as firebase functions?
There is an example to cap billing on the public docs. Notice that you'll need to assign the Billing Account Administrator role to the runtime service account (generally the App Engine's default service account, but you can change that) for your Cloud Functions for it to work. As using the client libraries will handle the authentication and authorization automatically based on the permissions assigned to that particular service account.

How to setup properly Google Cloud Shell?

I know this question is probably a bit vague. I was trying to run one of the examples of Google NLP Library in Google Shell.
I have 0 experience with using API, JSON, Nodejs... I don't understand what they are and how to use them.
Please help
Here is the snapshot of the error:
The error message means that you are using user credentials instead of service account credentials.
When you connect to Google Cloud Shell, you are using your Google Accounts User Credentials. Those credentials are the ones that you used to log in to the Google Cloud Console. When you run an application in Google Cloud Shell, your application is using those credentials unless you explicitly specify different credentials.
The solution is to create a service account in the Google Cloud Console. Then in your program use the service account for credentials for your application.
Google Cloud Service Accounts
When you do not specify the application credentials, the Google Client libraries use a method to locate credentials called ADC (Application Default Credentials). I wrote an article that might help you understand ADC:
Google Cloud Application Default Credentials
The simplest method for you is to create the environment variable GOOGLE_APPLICATION_CREDENTIALS to point to the service account full path location before running your application. Change the path below to point to where the service account is stored on Cloud Shell. You will need to first create the service acount, download it and then upload to Cloud Shell.
export GOOGLE_APPLICATION_CREDENTIALS="$HOME/service-account.json"
Managing files with Cloud Shell
This link will provide more information on how to write applications that use service accounts.
Setting Up Authentication for Server to Server Production Applications

Authenticate Google Storage object with access token in python

I am new to Google Cloud. I am trying to access google buckets to upload files. I use Google Storage object for accessing the bucket programmatically in Python. I am able to authenticate the storage object with 'key.json'. But I am unsure when the application will run in cloud how will it access 'key.json' file securely ? Also is there a way to authenticate storage object using access token in python ?
Thanks in advance!
But I am unsure when the application will run in cloud how will it
access 'key.json' file securely ?
Review the details that I wrote below. Once you have selected your environment you might not need to use a service account JSON file at all because the metadata server is available to provide your code with credentials. This is the best case and secure. On my personal website, I have written many articles that show how to create, manage and store Google credentials and secrets.
Also is there a way to authenticate storage object using access token
in python ?
All access is via an OAuth Access Token. The following link shows details using the metadata server which I cover in more detail below.
Authenticating applications directly with access tokens
There are three items to consider:
My code is not running in Google Cloud
My code is running in Google Cloud on a "compute" type of service with access to the metadata server
My code is running in Google Cloud without access to the metadata server.
1) My code is not running in Google Cloud
This means your code is running on your desktop or even in another cloud such as AWS. You are responsible for providing the method of authorization. There are two primary methods: 1) Service Account JSON key file; 2) Google OAuth User Authorization.
Service Account JSON key file
This is what you are using now with key.json. The credentials are stored in the file and are used to generate an OAuth Access Token. You must protect that file as it contains your Google Cloud secrets. You can specify the key.json directly in your code or via the environment variable GOOGLE_APPLICATION_CREDENTIALS
Google OAuth User Authorization
This method requires the user to log in to Google Accounts requesting an OAuth scope for Cloud Storage. The end result is an OAuth Access Token (just like a Service Account) that authorizes access to Cloud Storage.
Getting Started with Authentication
2) My code is running in Google Cloud on a "compute" type of service with access to the metadata server
Notice the word "metadata" server. For Google Cloud compute services, Google provides a metadata server that provides applications running on that compute service (Compute Engine, Cloud Functions, Cloud Run, etc) with credentials. If you use Google SDK Client libraries for your code, the libraries will automatically select the credentials for you. The metadata server can be disabled (denied access through role/scope removal), so you need to evaluate what you are running on.
Storing and retrieving instance metadata
3) My code is running in Google Cloud without access to the metadata server.
This is a similar scenario to #1. However, now you are limited to only using a service account unless this is a web server type of service that can present the Google Accounts authorization service to the user.

GCP IAP Authentication won't get service account credentials unless using environment var pointing at local json credentials

I tried Google's Java IAP authentication example for service accounts: https://cloud.google.com/iap/docs/authentication-howto#iap_make_request-java
This works well when there is a local service account json credentials file which is mentioned in the env var GOOGLE_APPLICATION_CREDENTIALS=<path-to-file>.
In another documentation page https://cloud.google.com/docs/authentication/production#auth-cloud-implicit-java it says the following:
If the environment variable isn't set, ADC uses the default service
account that Compute Engine, Kubernetes Engine, App Engine, and Cloud
Functions provide, for applications that run on those services.
If I don't provide the env var though then the auth library in the example seems to generate a different type of credentials than ServiceAccountCredentials, which leads to an error being thrown. I logged the credentials that are created and they are resolved to ComputeEngineCredentials which leads to this part of Google's example throwing the error:
if (credentials == null || !(credentials instanceof ServiceAccountCredentials)) {
throw new Exception("Google credentials : service accounts credentials expected");
}
In the scenarios I am working on I won't always have the option of uploading the json credentials file and running with that env variable and at any rate I would expect that a compute engine can identify as the service account in its configuration. Is there anything that I am missing?

Using the defaut application credentials to access the Google Play API?

I'm building an appengine app that requires access to the Google Play Developer API. I've seen in the sample code that it's possible to authenticate using a service account in addition to Oauth.
Is there any chance this could work with the default service account without having to generate a json key ? That would make the setup a bit easier.
Edit: be more explicit about not using a json key but really using the default application credentials instead.
For App Engine Standard environment:
You can generate a Service Account key file from default service
account. Follow the link sample code you provided, then click on
the link shown in the "Getting Started" section, you'll get in
the Google Developer Console. If logged in with the correct account
(you should see your project name at the top), then go to Credentials
-> Create credentials -> Service Account key. In the service account dropdown list, choose "App Engine Default Service Account", choose
JSON as key type and you should be good to go to follow the last
instructions on the Github page.
For App Engine Flexible environment:
The default service account isn't listed in the Service Account page, as explained here. You can't generate a service account key with it. You'd need to use a custom service account.