0

I am working on creating some scheduled jobs using the Java SDK for google cloud scheduler. Here is the link for the application code which is already posted as a part of another question. The application basically creates a Cloud Scheduler job, which every time it runs, triggers a custom training job on VertexAI. Now the call from the scheduler to VertexAI to create the custom job is authenticated using the service account. My question is about the authentication of the application code that creates the Cloud Scheduler job itself. I have set this application as a maven project and I create a single executable jar. The application itself runs on my local workstation. The following are my points/questions:

  1. When I create a docker image and copy this jar, and the service account key into the image, and then set the GOOGLE_APPLICATION_CREDENTIALS environment variable to point to the key within the container, then the application runs fine and the Cloud Scheduler job gets created.
  2. When I do the same as above, except I simply execute the jar in powershell (with GOOGLE_APPLICATION_CREDENTIALS environment variable pointing to the service account key), the permission is denied.
  3. Same as 2, except I simply run the application using the eclipse "Run App" button.

How can I authenticate to run the application without having to run in a docker container. And is there a way to authenticate without using the GOOGLE_APPLICATION_CREDENTIALS environment variable, i.e., directly in the application code itself. Links to sample code/examples will be helpful.

EDIT: For point 2, the problem was a typo in the name of the environment variable. For point 3, you can set environment variables directly in eclipse as mentioned in the answer by @RJC.

RJC
  • 1,224
  • 2
  • 12
racerX
  • 930
  • 9
  • 25
  • Your question is not enough precise. Are you trying to run your code on your workstation? With or without a container? With your own user credential or a service account credential (through a service account key file)? – guillaume blaquiere Sep 09 '21 at 07:31

1 Answers1

1

I don't have Eclipse on my machine, but I've found a related answer where you can add a specific environment variable within the IDE itself. I suggest that you try to do the following and see if it fixes the problem.

There is another way to authenticate without using GOOGLE_APPLICATION_CREDENTIALS, and that's through explicitly pointing to your service account file in your code. I've created a sample code that retrieves a Job Name without using the GOOGLE_APPLICATION_CREDENTIALS. Authentication is done by specifying a credential setting when initializing the CloudSchedulerClient.

Here's what I've done on my end:

  1. Use the gcloud iam service-accounts keys create serviceaccount.json --iam-account=NAME@PROJECT_ID.iam.gserviceaccount.com that will generate a JSON file for the service account that will be used in CredentialsProvider.
  2. Create a CredentialsProvider object that will call the created JSON file of the service account.
try {
    JobName name = JobName.of("[PROJECT]", "[LOCATION]", "[JOB]");
    
    CredentialsProvider credentialsProvider =
    FixedCredentialsProvider.create(
    ServiceAccountCredentials.fromStream(new FileInputStream("/path/to/serviceaccount.json")));
            
    CloudSchedulerSettings cloudSchedulerSettings = CloudSchedulerSettings.newBuilder().setCredentialsProvider(credentialsProvider).build();
    
    CloudSchedulerClient cloudSchedulerClient = CloudSchedulerClient.create(cloudSchedulerSettings);
    
    System.out.println(cloudSchedulerClient.getJob(name).toString()); // To display the output
    cloudSchedulerClient.close();
    
} catch (IOException e) {
    e.printStackTrace();       
}

For the additional guidance, here's an API reference to customize credentials.

Note that you are using a service account which can be read by unauthorized person if improperly mishandled. My suggestion is to only set your service accounts with permissions required to perform your task/s. You can also follow this best practices for managing your credentials moving forward.

RJC
  • 1,224
  • 2
  • 12