GeezerVison is showing github.com — Stackdriver logging - setCredentials not working, getting "PERMISSION_DENIED: The caller does not have permission" · Issue #3170 · googleapis/google-cloud-java
Stackdriver logging - setCredentials not working, getting "PERMISSION_DENIED: The caller does not have permission" #3170Labels authpriority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns. DescriptionI'm trying to use setCredentials in LoggingBuilder, however if I supply my key.json file from code I'm getting PERMISSION_DENIED on my logging call.
If I instead populate the environment variable GOOGLE_APPLICATION_CREDENTIALS with the same /path/to/key.json it works.
My main method is:
Logging logging = LoggingOptions
.newBuilder()
.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream("/path/to/key.json")))
.build()
.getService();
logging.setWriteSynchronicity(Synchronicity.SYNC);
logging.setFlushSeverity(Severity.DEBUG);
LogEntry entry = LogEntry
.newBuilder(Payload.StringPayload.of("Hello world"))
.setSeverity(Severity.INFO)
.setLogName("test-log")
.setResource(MonitoredResource
.newBuilder("global")
.build())
.build();
logging.write(Collections.singleton(entry)); // Throws PERMISSION_DENIED
My only dependencies:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-logging</artifactId>
<version>1.26.0</version>
</dependency>
Activityjberglund-BSFT commented on May 16, 2018 I'm seeing the same issue. I am using a project admin service account that has the roles/logging.logWriter role. pongad commented on May 21, 2018 Hmm! Assuming you're on Unix-like environment, could you run
<$GOOGLE_APPLICATION_CREDENTIALS grep type
?
The JSON files have two different kinds, "service_account" and "authorized_user". If the type is authorized_user, that'd explain why you're seeing this problem.
To fix, you could use com.google.auth.oauth2.GoogleCredentials.fromStream instead. I believe that method is used to read GOOGLE_APPLICATION_CREDENTIALS file behind the scenes, so it'd be surprising if that doesn't work. petergeneric commented on May 22, 2018 "type": "service_account"
Same issue with ServiceAccountCredentials.fromStream and GoogleCredentials.fromStream, also updated the test against version 1.30.0 and no change. Still works perfectly if I set an environment variable instead.
I poked around inside the library by setting a breakpoint at ClientCalls.onClose - looking at the callOptions of the call, credentials appeared to be set up exactly the same for both environment variable and ServiceAccountCredentials.fromStream approaches. pongad commented on May 30, 2018 I am not able to reproduce the original issue. In all cases, setting the env var and using ServiceAccountCredentials behave the same way. If they are set to the same file, they either both work or both fail.
@petergeneric Is there any chance you have gcloud installed on your machine? For some reason, the gcloud command seems to confuse the auth lib. I had to gcloud auth revoke before things start working. This should also be investigated. petergeneric commented on May 30, 2018 Aha! I figured out what is going on - gcloud auth revoke didn't work, but when I blew away ~/.config/gcloud it started complaining about a Project ID not being set - when I set it on the LoggingOptions.Builder everything worked... so I think what was happening is that the default project was being implicitly picked up from ~/.config/gcloud/configurations/config_default and used with the credentials json file I was explicitly specifying (and my default project for gcloud is different from this testing project for logging, which explains the permission denied error).
So perhaps a little too much magic going on in the library despite me trying to set things up manually!
I set up a simplified test-case at https://github.com/petergeneric/gcloud-logging-testcase but I don't think that's of any use at this point now that we've figured out the underlying issue. I'm happy to close this issue, since it's probably classified as working as intended! pongad commented on May 31, 2018 @petergeneric I would actually classify this as bug for "unnecessary confusing auth behavior", but I'll lower this down to P2. added type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2Moderately-important priority. Fix may not be included in next release. and removed on May 31, 2018yihanzhen commented on Jun 14, 2018 Seems the problem has been fixed here. @petergeneric I believe you can also use gcloud config set project to change projects if that's what is causing conflicts in your case. To folks who may see this issue in the future: we strongly encourage everyone to use the environment variable GOOGLE_APPLICATION_CREDENTIALS instead of gcloud auth login for authentication.
MetadataMetadataauthpriority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns. DevelopmentNo branches or pull requestsIssue actions
|