Best way to authenticate when testing a container in the local development environment

Hello everyone,

I’m developing a Cloud Run Job on Windows (in .NET/C#).
It is interacting with Cloud Storage so I must be authenticated.
I’ve set up Application Default Credentials via gcloud auth application-default login and it is running fine locally with dotnet run.

Then I containerize the code, but of course it won’t run inside Docker as the application_default_credentials.json is local to the host.
So I’ve copied this credentials file to the build directory, and added a COPY instruction in the Dockerfile: COPY application_default_credentials.json /root/.config/gcloud/
It is running fine.

But this solution is not satisfactory:

  • I have to copy the file manually first (as COPY can’t access files outside the Build Context which is the current directory)
  • This COPY instruction should be only for local build as it is not necessary once the container is run by Cloud Run in pre-production and production

Any input is welcome.

Hey @Pragmateek,

You’re hitting a classic containerisation issue.

I recommend using a bind mount to expose your ADC file to the container.

Optionally, you can use a dedicated service account with limited permissions and bind-mount its JSON key instead along with setting GOOGLE_APPLICATION_CREDENTIALS.

Also, worth to (re)check GCP Doc on How ADC works.

2 Likes

Thanks @LeoK for the pointers.
This issue should hit me again in the coming months. :grinning_face_with_smiling_eyes:

Hi @Pragmateek.

That’s not the best solution. You should not have to add the service account json file inside your container.

What you need to do is make sure the Cloud Run Default Service Account (Typically the default compute SA which has a name like xxxxxxxxxxx-compute@developer.gserviceaccount.com where xxxxxxxxx is the project number). Has the right permissions on the bucket.

If your app uses one of our auth libraries than Application Default Credentials should just work!

1 Like

Thanks for the clarification @abdelfettah :smiling_face:

@abdelfettah, I totally agree for deployed environments but OP’s question is specifically about local development.

I’m unsure how granting permissions to the default service account helps locally, since the application won’t use it unless explicitly impersonated or provided credentials. Am I missing something ?

Also, isn’t GCP recommended approach to use local ADC for Testing containerized applications locally ?

1 Like

@LeoK my comment was the production part of things. Locally with a container based environment the only way is the ADC file!

2 Likes

Ah yes, I thought so too :face_with_hand_over_mouth:

You made me doubt for a second!

2 Likes