Access GCP Services within Container
I get this error when running tests accessing any GCP services that require auth within a container. The Builder Service Account has Edit permissions to BigQuery & GCS.
Should this be possible?
Here's a lengthy stack trace for one of the errors:
Step #2: __________________________ ERROR at setup of test_get __________________________
Step #2:
Step #2: project = '[--]', bucket_name = '[--]'
Step #2: blob_name_expanded = 'tests/test_650575/blob_415687', data = 'abcdef'
Step #2:
Step #2: @pytest.fixture()
Step #2: def existing_blob_name(project, bucket_name, blob_name_expanded, data):
Step #2: > blob = gcs.get_client(project).get_bucket(bucket_name).blob(blob_name_expanded)
Step #2:
Step #2: /src/sixty/tests/test_hooks/test_gcs.py:43:
Step #2: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Step #2: /usr/local/lib/python2.7/dist-packages/google/cloud/storage/client.py:173: in get_bucket
Step #2: bucket.reload(client=self)
Step #2: /usr/local/lib/python2.7/dist-packages/google/cloud/storage/_helpers.py:99: in reload
Step #2: _target_object=self)
Step #2: /usr/local/lib/python2.7/dist-packages/google/cloud/_http.py:299: in api_request
Step #2: headers=headers, target_object=_target_object)
Step #2: /usr/local/lib/python2.7/dist-packages/google/cloud/_http.py:193: in _make_request
Step #2: return self._do_request(method, url, headers, data, target_object)
Step #2: /usr/local/lib/python2.7/dist-packages/google/cloud/_http.py:223: in _do_request
Step #2: body=data)
Step #2: /usr/local/lib/python2.7/dist-packages/google_auth_httplib2.py:187: in request
Step #2: self._request, method, uri, request_headers)
Step #2: /usr/local/lib/python2.7/dist-packages/google/auth/credentials.py:121: in before_request
Step #2: self.refresh(request)
Step #2: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Step #2:
Step #2: self = <google.auth.compute_engine.credentials.Credentials object at 0x7f1098b6ba90>
Step #2: request = <google_auth_httplib2.Request object at 0x7f1098b6b410>
Step #2:
Step #2: def refresh(self, request):
Step #2: """Refresh the access token and scopes.
Step #2:
Step #2: Args:
Step #2: request (google.auth.transport.Request): The object used to make
Step #2: HTTP requests.
Step #2:
Step #2: Raises:
Step #2: google.auth.exceptions.RefreshError: If the Compute Engine metadata
Step #2: service can't be reached if if the instance has not
Step #2: credentials.
Step #2: """
Step #2: try:
Step #2: self._retrieve_info(request)
Step #2: self.token, self.expiry = _metadata.get_service_account_token(
Step #2: request,
Step #2: service_account=self._service_account_email)
Step #2: except exceptions.TransportError as exc:
Step #2: > raise exceptions.RefreshError(exc)
Step #2: E RefreshError: ('Failed to retrieve http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true from the Google Compute Enginemetadata service. Status: 404 Response:\n<!DOCTYPE html>\n<html lang=en>\n <meta charset=utf-8>\n <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">\n <title>Error 404 (Not Found)!!1</title>\n <style>\n *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}\n </style>\n <a href=//www.google.com/><span id=logo aria-label=Google></span></a>\n <p><b>404.</b> <ins>That\xe2\x80\x99s an error.</ins>\n <p>The requested URL <code>/computeMetadata/v1/instance/service-accounts/default/?recursive=true</code> was not found on this server. <ins>That\xe2\x80\x99s all we know.</ins>\n', <google_auth_httplib2._Response object at 0x7f1098b6b9d0>)
Step #2:
Step #2: /usr/local/lib/python2.7/dist-packages/google/auth/compute_engine/credentials.py:93: RefreshError
This is an unexpected failure, is this repeatable? We have numerous tests that rely on this behavior, which pass successfully, so if you could provide more repro steps that would help us get to the bottom of the issue.
By any chance, are you running a container within a build step? eg
name: 'gcr.io/cloud-builders/docker'
args: ['run', 'gcr.io/$PROJECT_ID/my-container-image']
If so, then currently we don't forward credentials inside automatically. We hope to address this issue at some point. In the mean time you can forward them yourself with --link metadata:metadata.google.internal as one of the args after run.
Yes, it's within a step.
That's fine re adding the link. But it doesn't seem to work - I add this to docker-compose, but no luck.
external_links:
- metadata:metadata.google.internal
build-id 31e9c761-4062-40f2-aafa-4115a7fb9511. I can contact support if that's easier? Or leave it if it's really not supported
There is no value in contacting support if you're already contacting us - we're the eng team for container builder. :)
If you fetch http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true directly from inside the step, do you get something that looks like a token?
For instance, this works for me:
steps:
- name: 'gcr.io/cloud-builders/docker'
args:
- 'run'
- '--link=metadata:metadata.google.internal'
- '--entrypoint=ash'
- 'alpine'
- '-c'
- |
apk add --update curl
curl http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/?recursive=true
It does work with --link, thanks! IMO That's a very acceptable work around, assuming it's documented.
I'm not sure why --link works but not --external-links - not on your team though.
I've done a bit of research, but the difference between external links and normal links is not crystal clear to me. Neither will be necessary in our glorious future, though the feature is currently unscheduled.
Neither will be necessary in our glorious future
🥇
This isn't crucial, so happy to leave it. I think the answer is something to do with this: https://stackoverflow.com/a/39068043/3064736
Do you happen to know what networks are in the current docker setup? I can then connect my container to one and it should find the metadata container through that
The glorious future is when we actually start creating a network :) I'm going to reopen this issue and use it to track this feature.
Does this workaround also apply for the java/mvn image? I have a couple of tests that communicate with other cloud services and it would be very neat to have it all within container builder.
@james-woods This workaround should work for most cases where you run a docker container inside of a build step. Specifically, if the inner container (the one being run inside the build step) is able to access credentialed GCP services when running as a build step (i.e., not inside another build step container), then this workaround will work. (For some build steps, even running on their own they can't access credentialed GCP services, and for these the workaround won't help.)
@bendory - so to clarify, it will not work with the official java/mvn image - as I don't run a docker container there I just execute a command from within a pre-existing one.
The workaround is specifically for this situation:
- You have a container that accesses credentialed GCP services and works fine when used as a build step.
- You are running that container inside another container, and it gets permission errors when run there.
Hi @skelterjohn - is there any update here? The --link=metadata:metadata.google.internal work-around seems to have stopped working a couple months ago (though we turned it off so potentially it's come back recently)
Sorry about not updating this - Instead of --link= you should use --network=cloudbuild for the same effect.
so i am having a hell of a time trying to figure out why the following is not working
curl http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/
i want to access the identity endpoint but the above just returns a 404 on the docker image... running something simple as
- name: 'gcr.io/cloud-builders/docker'
id: set-env
entrypoint: 'bash'
args:
- '-c'
- |
curl http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/
I am essentially trying to generate a JWT by hitting the endpoint so i can authenticate to Hashicorp Vault to pull some secrets during cloudbuild step.
curl --get --data-urlencode "audience=https://example.com" --data-urlencode "format=full" "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity
@zbutt-muvaki This seems to be a new bug report or feature request, separate from the initial bug report. We also ask that questions/issues with the GCB API service get filed to the public issue tracker at https://issuetracker.google.com/issues/new?component=190802&template=1162743 (if you'd created a new issue you would have been prompted to do this).
That being said, I can explain what's happening. GCB workers spoof the metadata server to inject builder service account credentials and other relevant information. That endpoint is, probably due to oversight, not spoofed by GCB's metadata server.
What information are you trying to ascertain? Hitting that endpoint on a "real" metadata server results in a directory listing of sub-paths that you can hit:
$ curl -H "Metadata-Flavor: google" http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/
email/
scopes/
token/
Each of these endpoints are correctly spoofed, and return the expected responses about the builder service account.
@ImJasonH
i am trying to generate a JWT token
curl --get --data-urlencode "audience=https://example.com" --data-urlencode "format=full" "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity
The above returns a JWT on a compute instance, however, on cloudbuild i get "not found" as a result... matter of fact the above endpoint that you curled returns "not found" to me... it doesn't return the sub-paths
- name: 'gcr.io/cloud-builders/curl'
id: set-env
entrypoint: 'bash'
args:
- '-c'
- |
curl -H "Metadata-Flavor: google" --get --data-urlencode "audience=http://example.com" --data-urlencode "format=full" "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity"
running the above config in cloud-build-local gives the same error as running it in cloudbuild itself.
P.S. i created a ticket as suggested to the above tracker (https://issuetracker.google.com/issues/139011732).
@zbutt-muvaki @ImJasonH im running into the same issue, when i try to hit the metadata server for a http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity?audience=myblablablablblabl` i get a 404 inside of cloud build
This GitHub issue tracker is specifically intended for bugs with the officially supported builder images.
To report an issue with the Google Cloud Build service, please report it to our public issue tracker at https://issuetracker.google.com/issues/new?component=190802&template=1162743