GeezerVison is showing github.com — gcloud.exceptions.Forbidden: 403 Missing or insufficient permissions. · Issue #2524 · googleapis/google-cloud-python
gcloud.exceptions.Forbidden: 403 Missing or insufficient permissions. #2524DescriptionHi @dhermes
I am a new user to Google Cloud Platform. I have setup a Google VM Instance. I am facing an authentication issue on Local Machine while running the command.
python manage.py makemigrations
Can you please suggest steps to resolve the same ?
Error Trace:
File "/constants.py", line 18, in <module>
table_data = datastore_fetch(project_id, entity_kind)
File "/datastore_helper.py", line 23, in datastore_fetch
results = list(query.fetch())
File "/venv/local/lib/python2.7/site-packages/gcloud/datastore/query.py", line 463, in __iter__
self.next_page()
File "/venv/local/lib/python2.7/site-packages/gcloud/datastore/query.py", line 434, in next_page
transaction_id=transaction and transaction.id,
File "/venv/local/lib/python2.7/site-packages/gcloud/datastore/connection.py", line 286, in run_query
_datastore_pb2.RunQueryResponse)
File "/venv/local/lib/python2.7/site-packages/gcloud/datastore/connection.py", line 124, in _rpc
data=request_pb.SerializeToString())
File "/venv/local/lib/python2.7/site-packages/gcloud/datastore/connection.py", line 98, in _request
raise make_exception(headers, error_status.message, use_json=False)
gcloud.exceptions.Forbidden: 403 Missing or insufficient permissions.
Other Info:
gcloud auth list
Credentialed Accounts:
- [email protected] ACTIVE
To set the active account, run:
$ gcloud config set account `ACCOUNT`
gcloud config list
Your active configuration is: [default]
[core]
account = [email protected]
disable_usage_reporting = True
project = user_project
Also,
Input:
from gcloud import datastore
project_id = settings.GOOGLE_PROJECT_ID
client = datastore.Client(project_id)
print(vars(client.connection.credentials))
Output:
{'scopes': set([]), 'revoke_uri': 'https://accounts.google.com/o/oauth2/revoke', 'access_token': None, 'token_uri': 'https://www.googleapis.com/oauth2/v4/token', 'token_info_uri': None, 'token_response': None, 'invalid': False, 'refresh_token': u'1/t-V_pZicXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'client_id': u'3XXXXXXXX9.apps.googleusercontent.com', 'id_token': None, 'client_secret': u'ZXXXXXXXXXXXXXXXXXXX2', 'token_expiry': None, 'store': None, 'user_agent': 'Python client library'}
Thanks, Activityjgeewax commented on Oct 11, 2016 Hi @naveensinghal -- did you take a look at the scopes you created the VM with? It needs to have Cloud Datastore enabled (and User info as well I think).
If you go to the "view instance" page in cloud.google.com/console, you should see it towards the bottom of the page. Here's what I see for one of my instances:

naveensinghal commented on Oct 11, 2016 Hi @jgeewax
I can't see all these things. But I can see following:
Additional disks
None
Network
default
Subnetwork
default
Firewalls
Allow HTTP traffic
Allow HTTPS traffic
Availability policies
Preemptibility Off (recommended)
Automatic restart
On (recommended)
On host maintenance
Migrate VM instance (recommended)
Custom metadata
None
SSH Keys
Block project-wide SSH keys
None
Service account
service-account@user_project.iam.gserviceaccount.com
Cloud API access scopes
This instance has full API access to all Google Cloud services.
daspecster commented on Oct 11, 2016 naveensinghal commented on Oct 11, 2016 @daspecster
Yes, Google Cloud Datastore API & Google Cloud Datastore API (v1beta2) Both are present in the list of APIs in the given link.
If you look at the output value by printing 'vars(client.connection.credentials)', it says 'scopes' = set([]) . I think this issue can be resolved if we can have some permissions within this set. But don't know how and from where to set it.
Currently, I am using User Account on Local Machine & Service Account on VM Instance. Is there any change required here ?
I can run "python manage.py makemigrations" successfully on VM Instance but not on Local Machine.
(The User Account has 'Owner' Permissions and the Service Account has 'Editor' permissions.) daspecster commented on Oct 11, 2016 Hmmm,
What version of the google-cloud-python are you using? pip freeze output?
I'm wondering if possibly the datastore grpc/gax library could be out of date?
Also in your second example with printing out the credentials, are you able to access datastore from the REPL or other APIs possibly? naveensinghal commented on Oct 11, 2016 Command: pip freeze
Output:
gapic-google-logging-v2==0.10.1
gapic-google-pubsub-v1==0.10.1
gax-google-logging-v2==0.8.1
gax-google-pubsub-v1==0.8.1
gcloud==0.18.1
gcs-oauth2-boto-plugin==1.9
google-api-python-client==1.5.2
google-cloud==0.20.0
google-cloud-bigquery==0.20.0
google-cloud-bigtable==0.20.0
google-cloud-core==0.20.0
google-cloud-datastore==0.20.0
google-cloud-dns==0.20.0
google-cloud-error-reporting==0.20.0
google-cloud-language==0.20.0
google-cloud-logging==0.20.0
google-cloud-monitoring==0.20.0
google-cloud-pubsub==0.20.0
google-cloud-resource-manager==0.20.0
google-cloud-storage==0.20.0
google-cloud-translate==0.20.0
google-cloud-vision==0.20.0
google-gax==0.14.1
googleapis-common-protos==1.3.5
grpc-google-iam-v1==0.10.1
grpc-google-logging-v2==0.10.1
grpc-google-pubsub-v1==0.10.1
Not understood "are you able to access datastore from the REPL or other APIs possibly?" exactly. Can you please elaborate ? daspecster commented on Oct 11, 2016 @naveensinghal, at version 0.19.0 google-cloud-python's packaging changed. So with versions >= 0.19.0, you would import datastore like this from google.cloud import datastore.
The latest release of the old packaging (from gcloud import datastore) is actually 0.18.3.
Possible cause of issue:
You seem to have them both installed currently. gcloud==0.18.1 and the new umbrella package google-cloud==0.20.0.
Could you try making a fresh virtual environment and pip install google-cloud then trying to access datastore from google.cloud import datastore? daspecster commented on Oct 11, 2016
Not understood "are you able to access datastore from the REPL or other APIs possibly?" exactly. Can you please elaborate ?
Sorry, if I wasn't clear, I should have given you an example.
Something like this...
$ python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 26 2016, 12:10:39)
[GCC 4.2.1]
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from google.cloud import datastore
>>> client = datastore.Client('your-project-id')
>>> query = client.query(kind='MyKind')
>>> query_iter = query.fetch()
>>> entities, more_results, cursor = query_iter.next_page()
>>> entities
[<list of Entity unmarshalled from protobuf>]
It looks like you're using a virtualenv so you may have to run something like $ /venv/bin/python to use the same interpreter as you application(Django project it looks like?). naveensinghal commented on Oct 11, 2016 Installed Fresh Copy of Virtual Environment and used 'from google.cloud import datastore'
Command:
python manage.py makemigrations
Error Trace:
File "/constants.py", line 18, in <module>
table_data = datastore_fetch(project_id, entity_kind)
File "/datastore_helper.py", line 24, in datastore_fetch
results = list(query.fetch())
File "/venv/local/lib/python2.7/site-packages/google/cloud/datastore/query.py", line 463, in __iter__
self.next_page()
File "/venv/local/lib/python2.7/site-packages/google/cloud/datastore/query.py", line 434, in next_page
transaction_id=transaction and transaction.id,
File "/venv/local/lib/python2.7/site-packages/google/cloud/datastore/connection.py", line 534, in run_query
response = self._datastore_api.run_query(project, request)
File "/venv/local/lib/python2.7/site-packages/google/cloud/datastore/connection.py", line 281, in run_query
return self._stub.RunQuery(request_pb)
File "/venv/local/lib/python2.7/site-packages/grpc/_channel.py", line 481, in __call__
return _end_unary_response_blocking(state, False, deadline)
File "/venv/local/lib/python2.7/site-packages/grpc/_channel.py", line 432, in _end_unary_response_blocking
raise _Rendezvous(state, None, None, deadline)
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.PERMISSION_DENIED, Missing or insufficient permissions.)>
Input:
from google.cloud import datastore
client = datastore.Client('user_project')
print(vars(client.connection.credentials))
Output:
{'scopes': set([]), 'revoke_uri': 'https://accounts.google.com/o/oauth2/revoke', 'access_token': None, 'token_uri': 'https://www.googleapis.com/oauth2/v4/token', 'token_info_uri': None, 'token_response': None, 'invalid': False, 'refresh_token': u'1/t-V_pZicXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', 'client_id': u'3XXXXXXXX9.apps.googleusercontent.com', 'id_token': None, 'client_secret': u'ZXXXXXXXXXXXXXXXXXXX2', 'token_expiry': None, 'store': None, 'user_agent': 'Python client library'}
Input:
from google.cloud import datastore
client = datastore.Client('user_project')
query = client.query(kind='MyKind')
query_iter = query.fetch()
entities, more_results, cursor = query_iter.next_page()
print(entities)
Output:
<_Rendezvous of RPC that terminated with (StatusCode.PERMISSION_DENIED, Missing or insufficient permissions.)>
daspecster commented on Oct 11, 2016 naveensinghal commented on Oct 11, 2016 I tried the above link. No significant change. Permission Issue still there.
Is there any signifcance of this warning here while running 'gcloud auth login' ?
WARNING: `gcloud auth login` no longer writes application default credentials.
If you need to use ADC, see:
gcloud auth application-default --help
daspecster commented on Oct 11, 2016 Do you have a JSON credentials file?
To run your code outside Google Cloud Platform, such as in your own private datacenter or in another public cloud, you should use Application Default Credentials with explicitly created service accounts:
Use the Google Cloud Platform Console to create a service account, and download its credentials JSON file to your servers. Make sure to keep the file secure.
Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of your downloaded credentials JSON file.
See: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow naveensinghal commented on Oct 11, 2016 Yes. I have.
I have client_secrets.json & client_secrets.p12 files. The Code uses Service Account & the key file path to these two files. daspecster commented on Oct 11, 2016 Glad to hear!
Looking back, since your access_token wasn't set that makes sense.
Let us know if you run into any other issues. naveensinghal commented on Oct 11, 2016 @daspecster
I am using json file Credentials for my Google Project. By any chance can this be avoided ?
(Maybe by storing this file/credentials in the Metadata or somewhere else such that these values can be fetched in settings.py)
PS: Copying this file on every local Dev machine seems like a highly inefficient and error-prone task dhermes commented on Oct 11, 2016 @naveensinghal GCE credentials are supported out of the box provided you have the correct scopes set. You can see which scopes are set just by using curl to talk to the GCE metadata server.
curl --header "Metadata-Flavor: Google" \
http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true
Reacted by Thomas Schultz naveensinghal commented on Oct 11, 2016 @dhermes
Input: (From GCE VM)
curl --header "Metadata-Flavor: Google" http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true
Output:
{"aliases":["default"],"email":"service_account@user_project.iam.gserviceaccount.com","scopes":["https://www.googleapis.com/auth/cloud-platform"]} dhermes commented on Oct 11, 2016 Did you run this from your GCE VM? naveensinghal commented on Oct 11, 2016 Updated the output.
Can you please explain this in a bit more detail, if possible ?
How can I get rid of the JSON Credential File from Local Machine Developer's Code (and GCE VM too)? dhermes commented on Oct 11, 2016 You have the "https://www.googleapis.com/auth/cloud-platform" scope, which should be totally sufficient to use v1 of the datastore API. You should be able to just use the API without needing a JSON credentials file or doing any other configuration. Our client objects will just pick the config up directly from GCE:
from google.cloud import datastore
client = datastore.Client() dhermes commented on Oct 11, 2016 Have you enabled the Datastore API service in the cloud console? naveensinghal commented on Oct 12, 2016 Hi @dhermes
Yes, I have enabled Datastore API and can execute datastore.Client().
The thing I am trying to ask is if I could enter some information/secret keys(such as that in JSON File) in VM's metadata(or somewhere else on Google Cloud) and fetch those keys/values directly in settings.py.
For Example: Let's say my settings.py file is:
secret_key = "XXXXXXXX"
client_key_path = "../client_key.json"
Now, Is there a way such that I can store secret_key key/value pair in VM's Metadata(or somewhere else on Google Cloud) and client_key.json in google CloudStorage(or somewhere else on Google Cloud) and fetch these values directly in settings.py ?
Thanks, dhermes commented on Oct 12, 2016 Not really. The idea is that you don't need a JSON keyfile on GCE. naveensinghal commented on Oct 12, 2016 @dhermes
The Original Problem of this thread got solved basically by running the command:
export GOOGLE_APPLICATION_CREDENTIALS='/<path_to_json>/client_secrets.json'
So, how can we get rid of JSON keyfile ?
[These questions might seem like stupid but please try to resolve them, if possible.] dhermes commented on Oct 12, 2016 You don't need the JSON keyfile on GCE. As I mentioned above, on GCE it is sufficient to just create a client.
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|