Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. #114

Closed
yihong0618 opened this issue Dec 14, 2023 · 13 comments
Labels
component:python sdk Issue/PR related to Python SDK type:bug Something isn't working

Comments

@yihong0618
Copy link
Contributor

Description of the bug:

when use low google-auth package will raise this problem
image

Actual vs expected behavior:

No response

Any other information you'd like to share?

No response

@yihong0618 yihong0618 added component:python sdk Issue/PR related to Python SDK type:bug Something isn't working labels Dec 14, 2023
@reconsumeralization
Copy link

reconsumeralization commented Dec 14, 2023

it's not a bug. The requirement to explicitly specify credentials or set the GOOGLE_APPLICATION_CREDENTIALS environment variable is not a bug but a deliberate design choice for security reasons. Google Cloud enforces this practice to ensure secure authentication and proper access control. Explicitly specifying credentials helps prevent unintentional exposure of sensitive information, such as service account key files.

This design choice aligns with best practices for security in cloud computing environments, where controlled and authorized access is critical. By requiring users or applications to explicitly manage and provide their credentials, Google Cloud ensures a more secure authentication process.

If you encounter issues with credential management, it's important to review your setup, check that the specified path to the JSON key file is accurate, and follow the recommended practices outlined in the Google Cloud documentation for authentication.

#The inability to automatically apply credentials is often related to the security and access control mechanisms implemented by Google Cloud services. Google Cloud expects applications to use authentication mechanisms to ensure secure and authorized access to its services.

Description of the bug:

when use low google-auth package will raise this problem image

Actual vs expected behavior:

No response

Any other information you'd like to share?

No response

The two main reasons why automatic credential detection might fail are:

Security and Authorization:

Google Cloud services require proper authentication to ensure that only authorized users or applications can access sensitive resources.
Automatically applying credentials without user or application consent could compromise security.
Service Account and Key File Requirement:

Google Cloud typically uses service accounts and JSON key files for authentication. These key files contain sensitive information and must be managed securely.

To ensure secure access, users or applications need to provide the location of the JSON key file explicitly or set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to the file.
By requiring explicit specification of credentials, Google Cloud ensures that:

Users or applications explicitly grant consent for accessing resources.
Sensitive information (such as the service account key file) is securely managed and not accidentally exposed.
This approach aligns with best practices for secure authentication and access control.

In summary, the need for explicit credential specification is a security measure, and it ensures that users or applications follow secure practices when interacting with Google Cloud services.

Can we mark thiis as resolved? Did I make my firsrt Google Open Source Contribution?

@markmcd
Copy link
Member

markmcd commented Jan 8, 2024

@yihong0618, could you provide a repro for this issue? As @reconsumeralization points out, we will need to see that you have the relevant env vars set to rule out a bad environment.

I can see in #115 you have suggested downgrading google-auth to an older version - that's potentially a harmful change, and not sustainable long-term. With a repro we can either help you identify if there is an issue is in your setup, or find the right place to fix the issue (e.g. in the auth library).

@markmcd markmcd added the status:awaiting user response Awaiting a response from the author label Jan 8, 2024
@yihong0618
Copy link
Contributor Author

yihong0618 commented Jan 8, 2024

@yihong0618, could you provide a repro for this issue? As @reconsumeralization points out, we will need to see that you have the relevant env vars set to rule out a bad environment.

I can see in #115 you have suggested downgrading google-auth to an older version - that's potentially a harmful change, and not sustainable long-term. With a repro we can either help you identify if there is an issue is in your setup, or find the right place to fix the issue (e.g. in the auth library).

sorry its not download its update google-auth version.
if you had installed older google-auth version before you may have this issue.

Will I need to update the google-auth version to the latest in my PR?

@markmcd
Copy link
Member

markmcd commented Jan 8, 2024

sorry its not download its update google-auth version. if you had installed older google-auth version before you may have this issue.

Ah interesting - can you explain what the issue is then? Ideally with a repro. As @reconsumeralization pointed out, the error in the original report can be expected in some cases, so it would help us to understand what is actually wrong.

Will I need to update the google-auth version to the latest in my PR?

This is what we want to avoid, but using a >= constraint would be OK. To approve the PR we need to understand why though.

I've tried to repro your issue but I can't:

$ virtualenv ...
$ pip install 'google-auth==2.25.1' google-generativeai
$ python
Python 3.9.12 (main, Jun  2 2023, 07:29:00) 
[GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.generativeai as genai
>>> pro = genai.GenerativeModel('models/gemini-pro')
>>> pro.generate_content('the opposite of hot is').text
'cold'

@yihong0618
Copy link
Contributor Author

yihong0618 commented Jan 8, 2024

@markmcd
you can repro by using python -m pip install google-auth==2.0
I fixed the version 2.25 caused seems its not conflict and OK for the repo

and for me, change PR code to >= a certain version is OK

repro:
image

image

@markmcd
Copy link
Member

markmcd commented Jan 8, 2024

I still can't reproduce, tried with python 3.9 and 3.11 (as you are using):

$ virtualenv ...
$ pip install 'google-auth==2.0' google-generativeai

$ pip freeze |grep google
google-ai-generativelanguage==0.4.0
google-api-core==1.34.0
google-auth==2.0.0
google-generativeai==0.3.2
googleapis-common-protos==1.62.0

$ python
Python 3.11.6 (main, Oct  8 2023, 05:06:43) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import google.generativeai as genai
>>> genai.configure(api_key="...")
>>> pro = genai.GenerativeModel('models/gemini-pro')
>>> (r := pro.generate_content('the opposite of hot is')).text
'cold'

Are you using a clean environment? e.g. an empty venv?

@yihong0618
Copy link
Contributor Author

sorry will using a venv test and let you know later.

@yihong0618
Copy link
Contributor Author

@markmcd
can you reproduce as this

python -m venv venv
source venv/bin/activate
cat t.txt
# output
google-ai-generativelanguage==0.4.0
google-api-core==1.34.0
google-auth==2.0.0
google-generativeai==0.3.2
googleapis-common-protos==1.59.1

pip install -r t.txt

import google.generativeai as genai
from os import environ

genai.configure(api_key=environ.get("GOOGLE_API_KEY"))
pro = genai.GenerativeModel('models/gemini-pro')

print(pro.generate_content('the opposite of hot is').text)

python test.py

image

image

@markmcd
Copy link
Member

markmcd commented Jan 8, 2024

Ah - I believe it's working for me because I still have credentials available (i.e. I've run gcloud auth). When I remove $HOME/.config/gcloud/application_default_credentials.json I can repro.

I think I've found the issue - this commit added API key auth support to google-auth. That commit was shipped in 2.15.0, so I think you can set that as the lower bound and we should be good.

For context, the auth lib expects some credentials to be present, but versions older than 2.15.0 don't recognise API keys as credentials, so you got that error (and I did not, because I had other supported credentials present).

Edit: I've confirmed by testing with 2.15.0 (works), and with 2.14.1 (does not work).

@markmcd markmcd removed the status:awaiting user response Awaiting a response from the author label Jan 8, 2024
@yihong0618
Copy link
Contributor Author

Ah thanks for the explain and the commit link its very interesting and learned something from it.
and if this PR can continue can we just set google-auth>= 2.15.0
or we can do some other things and I can close that

@yihong0618
Copy link
Contributor Author

@markmcd just found you continue commit, thanks a lot.
its fun and kind to contribute this repo, thanks again.

@markmcd markmcd closed this as completed in bad9517 Jan 8, 2024
@markmcd
Copy link
Member

markmcd commented Jan 8, 2024

Thanks for the report and your patience!

@reconsumeralization
Copy link

Ah thanks for the explain and the commit link its very interesting and learned something from it. and if this PR can continue can we just set google-auth>= 2.15.0 or we can do some other things and I can close that

Sorry for not getting back to you sooner.

I couldn't repro it, I'm sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:python sdk Issue/PR related to Python SDK type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants