Skip to content

Unable to authenticate with IAM when using Cloud Functions #301

@bpaskin

Description

@bpaskin

I am using a small Cloud Function Action that is trying to access Cloudant. It works from my desktop, however, when using the Action, it fails with:

            "error": "An error has occurred while invoking the action (see logs for details): java.lang.RuntimeException: Error while fetching access token from token service: "
        }
    },
    "logs": [
        "2022-06-02T13:51:41.093456Z    stderr: java.lang.RuntimeException: Error while fetching access token from token service:",
        "2022-06-02T13:51:41.093844Z    stderr: at com.ibm.cloud.sdk.core.security.TokenRequestBasedAuthenticator.getToken(TokenRequestBasedAuthenticator.java:272)",
        "2022-06-02T13:51:41.093995Z    stderr: at com.ibm.cloud.sdk.core.security.TokenRequestBasedAuthenticator.authenticate(TokenRequestBasedAuthenticator.java:210)",
        "2022-06-02T13:51:41.094140Z    stderr: at com.ibm.cloud.sdk.core.service.BaseService.setAuthentication(BaseService.java:414)",
        "2022-06-02T13:51:41.094283Z    stderr: at com.ibm.cloud.sdk.core.service.BaseService.createCall(BaseService.java:352)",
        "2022-06-02T13:51:41.094431Z    stderr: at com.ibm.cloud.sdk.core.service.BaseService.createServiceCall(BaseService.java:390)",
        "2022-06-02T13:51:41.094575Z    stderr: at com.ibm.cloud.cloudant.internal.CloudantBaseService.createServiceCall(CloudantBaseService.java:185)",
        "2022-06-02T13:51:41.094717Z    stderr: at com.ibm.cloud.cloudant.v1.Cloudant.getServerInformation(Cloudant.java:223)",
        "2022-06-02T13:51:41.094850Z    stderr: at com.ibm.cloud.cloudant.v1.Cloudant.getServerInformation(Cloudant.java:236)",
        "2022-06-02T13:51:41.094993Z    stderr: at com.ibm.example.CloudantJava.main(CloudantJava.java:22)",
        "2022-06-02T13:51:41.095145Z    stderr: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)",
        "2022-06-02T13:51:41.095291Z    stderr: at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)",
        "2022-06-02T13:51:41.095432Z    stderr: at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)",
        "2022-06-02T13:51:41.095575Z    stderr: at java.lang.reflect.Method.invoke(Method.java:498)",
        "2022-06-02T13:51:41.095718Z    stderr: at org.apache.openwhisk.runtime.java.action.JarLoader.invokeMain(JarLoader.java:76)",
        "2022-06-02T13:51:41.095861Z    stderr: at org.apache.openwhisk.runtime.java.action.Proxy$RunHandler.handle(Proxy.java:157)",
        "2022-06-02T13:51:41.096005Z    stderr: at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79)",
        "2022-06-02T13:51:41.096150Z    stderr: at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)",
        "2022-06-02T13:51:41.096291Z    stderr: at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:82)",
        "2022-06-02T13:51:41.096436Z    stderr: at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:705)",
        "2022-06-02T13:51:41.096584Z    stderr: at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79)",
        "2022-06-02T13:51:41.096757Z    stderr: at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:677)",
        "2022-06-02T13:51:41.096887Z    stderr: at sun.net.httpserver.ServerImpl$DefaultExecutor.execute(ServerImpl.java:158)",
        "2022-06-02T13:51:41.097039Z    stderr: at sun.net.httpserver.ServerImpl$Dispatcher.handle(ServerImpl.java:431)",
        "2022-06-02T13:51:41.097183Z    stderr: at sun.net.httpserver.ServerImpl$Dispatcher.run(ServerImpl.java:396)",
        "2022-06-02T13:51:41.097307Z    stderr: at java.lang.Thread.run(Thread.java:832)",
        "2022-06-02T13:51:41.097606Z    stderr: Caused by: java.lang.NoSuchMethodError: com/google/gson/GsonBuilder.setObjectToNumberStrategy(Lcom/google/gson/ToNumberStrategy;)Lcom/google/gson/GsonBuilder; (loaded from file:/javaAction/build/libs/javaAction-all.jar by sun.misc.Launcher$AppClassLoader@d37eb13) called from class com.ibm.cloud.sdk.core.util.GsonSingleton (loaded from file:/tmp/useraction7866942478414886060.jar by org.apache.openwhisk.runtime.java.action.JarLoader@5653395a).",
        "2022-06-02T13:51:41.097834Z    stderr: at com.ibm.cloud.sdk.core.util.GsonSingleton.createGson(GsonSingleton.java:50)",
        "2022-06-02T13:51:41.097967Z    stderr: at com.ibm.cloud.sdk.core.util.GsonSingleton.getGsonWithoutPrettyPrinting(GsonSingleton.java:102)",
        "2022-06-02T13:51:41.098109Z    stderr: at com.ibm.cloud.sdk.core.util.ResponseUtils.getValue(ResponseUtils.java:149)",
        "2022-06-02T13:51:41.098272Z    stderr: at com.ibm.cloud.sdk.core.util.ResponseConverterUtils$6.convert(ResponseConverterUtils.java:137)",
        "2022-06-02T13:51:41.098449Z    stderr: at com.ibm.cloud.sdk.core.security.TokenRequestBasedAuthenticator$2.run(TokenRequestBasedAuthenticator.java:323)",
        "2022-06-02T13:51:41.098582Z    stderr: ... 1 more",
        "2022-06-02T13:51:41.101Z       stderr: The action did not initialize or run as expected. Log data might be missing."

There is a lack of logging and the error message is not descriptive enough to understand what is wrong.

To Reproduce

I created a Cloudant instance with the name "cloudant-serverless" and an Action listed below:

public static JsonObject main(JsonObject args) {
		IamAuthenticator authenticator = new IamAuthenticator.Builder().apikey("<myAPiKey>").build();
		
		Cloudant service = new Cloudant("cloudant-serverless", authenticator);
		service.setServiceUrl("https://5de7608c-fce3-420d-b007-43f46e05d996-bluemix.cloudantnosqldb.appdomain.cloud");
			
		HttpConfigOptions options =  new HttpConfigOptions.Builder().loggingLevel(HttpConfigOptions.LoggingLevel.BASIC).build();
		service.configureClient(options);
			
		ServerInformation serverInfo = service.getServerInformation().execute().getResult();
		
		JsonObject result = new JsonObject();
		result.addProperty("Cloudant version", serverInfo.getVersion());
		return result;
	}

Maven details:

  	<dependency>
  		<groupId>com.ibm.cloud</groupId>
  		<artifactId>cloudant</artifactId>
  		<version>0.1.4</version>
  	</dependency>

Expected behavior
To get the IAM token and connect to the database and return information. The following is from my desktop:

Jun 02, 2022 10:20:09 AM okhttp3.internal.platform.Platform log
INFO: --> GET https://5de7608c-fce3-420d-b007-43f46e05d996-bluemix.cloudantnosqldb.appdomain.cloud/ h2
Jun 02, 2022 10:20:09 AM okhttp3.internal.platform.Platform log
INFO: <-- 200 https://5de7608c-fce3-420d-b007-43f46e05d996-bluemix.cloudantnosqldb.appdomain.cloud/ (85ms, unknown-length body)
Cloudant version: 3.2.1

Screenshots
N/A

Must gather (please complete the following information):

  • SDK Version 0.1.4
  • Java Version - Java 8 from Cloud Functions
  • Name of service that you're trying to use (if applicable) - cloudant-serverless
  • Name of operation that you're trying to invoke (if applicable) - service.getServerInformation().execute().getResult();

Additional context

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions