Setting GCP environment variable in Spring boot with vs code - java

I'm trying to set up GOOGLE_APPLICATION_CREDENTIALS but keep getting this error
java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credential
I have defined in my application properties:
spring.cloud.gcp.project-id=PROJECT_ID
spring.cloud.gcp.credentials.location=lasspath:/src/main/resources/key.json
Added the dependency in the pom file:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-dialogflow</artifactId>
<version>1.0.0</version>
</dependency>
Tried to do it from the terminal(windows) as well no luck:
> set GOOGLE_APPLICATION_CREDENTIALS="C:\Users\user\Desktop\****\******\src\main\resources\key.json"

If you have Google Cloud SDK installed you can run this command in the console to set up application default credentials:
gcloud auth application-default login
Please, see more details in the documentation:
https://cloud.google.com/sdk/gcloud/reference/auth/application-default

Solved by running in the terminal:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\user\Desktop\*****\****\src\main\resources\*****.json"
In addition to setting the relevant role in IAM section of GCP since I've encountered a PERMISSION_DENIED Error after

Related

Spring Boot read environment variables inside application-properties

So I have been trying to figure out how to use environment-variables in either application.properties or application.yaml. Since this works in countless frameworks (such as Laravel for example), I assumed this would also work in Spring Boot. Now as I have found out, this is not the case.
I saw a post about Spring Environment API, that started that the environment-variable SPRING_DATASOURCE_URL would be equivalent to setting spring.datasource.url inside my application. However, this also didn't work for me.
Is there any quick way that allows using variables that are declared inside a .env file?
I'd like to use it like this inside the application.properties if possible.
spring.datasource.url=jdbc:postgresql://${DB_HOST}:${DB_PORT}/${DB_NAME}
.env is way of Python. If you use spring cloud, you can read env variable from configServer then inject them into application.properties.
add some dependency into pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
define a yml to locate config-server. for example, called myBootstrap.yml
spring:
cloud:
config:
fail-fast: true
uri: http://[config-server-git]
name: cc
profile: config
label: maste
define a file named cc-config.properties and push it into config-server git. The env variables are written in this properties.
use below way to run application jar
java -jar [your-application-jar] --spring.cloud.bootstrap.location=myBootstrap.yml

Spring config for deployment on google app engine flex connecting to cloud sql

I know there is a lot of issues like that in here but I couldn't find the solution to my problem.
I'm trying to find a way to make that project working on google app engine flex connecting to cloud sql in the same app engine project.
For that I use the code from git
I have the 3 files where to set the config: pom.xml, app.yaml, application.properties
I tried to add properties and dependencies to my pom.xml as said on google
<properties>
<INSTANCE_CONNECTION_NAME>project:region:instance</INSTANCE_CONNECTION_NAME>
<user>myUser</user>
<password>myPassword</password>
<database>accounts</database>
<sqlURL>jdbc:mysql://google/${database}?cloudSqlInstance=${INSTANCE_CONNECTION_NAME}&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=${user}&password=${password}&useSSL=false</sqlURL>
</properties>
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>mysql-socket-factory-connector-j-6</artifactId>
<version>1.0.5</version>
</dependency>
I have my application.properties that contain value for the spring data-source (urls with IP and port works fine locally)
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://google/myDatabase?cloudSqlInstance=project:region:instance=com.google.cloud.sql.mysql.SocketFactory
#jdbc.url=jdbc:mysql://IP_ADDRESS:PORT/myDatabase? useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
#jdbc.url=mysql:unix_socket=/cloudsql/project:region:instance;dbname=myDatabase
jdbc.username=myUser
jdbc.password=myPassword
I tried different url as you can see.
I also tried to add some lines in my app.yaml
resources:
memory_gb: 2.3
handlers:
- url: /.*
script: this field is required, but ignored
or these
env_variables:
MYSQL_DNS: mysql:unix_socket=/cloudsql/project:region:instance;dbname=myDatabase
MYSQL_USER: myUser
MYSQL_PASSWORD: myPassword
or both.
I run out of idea. anyone can help me?

How to set GOOGLE_APPLICATION_CREDENTIALS in spring boot app

I am attempting to use the google vision library in java. The steps specify that I need to setup my auth credentials in order to start using the this library . I was able to generate my json property file from API Console Credentials page and I placed it in my spring boot app in the resources folder.
I think updated my application.properties file to include the value like so:
GOOGLE_APPLICATION_CREDENTIALS=datg-avatar-generator-9dc9155cd5bd.json
I'm also setting my property source in my controller like so:
#PropertySource("${GOOGLE_APPLICATION_CREDENTIALS}")
However, after doing that I'm still getting an error saying:
java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. See https://developers.google.com/accounts/docs/application-default-credentials for more information.
I was able to configure this property using Spring Cloud GCP spring-cloud-gcp-starter-data-datastore, creating a service account project owner, copy the JSON private key to the main resources directory, and setting the following properties in the application.properties
spring.cloud.gcp.project-id=<project-id>
spring.cloud.gcp.credentials.location=classpath:<credentials-private-key>.json
from the documentation
You can find the project id by visiting this page https://support.google.com/googleapi/answer/7014113?hl=en
Go to the API Console.
From the projects list, select Manage all projects. The names and IDs for all the projects you're a member of are displayed.
You can also select the project go the settings and see the project ID
You can use application properties, but you need to use a different StorageOptions builder.
You are probably using
private static Storage storage = StorageOptions.getDefaultInstance().getService();
But if you want to skip the environment variable you need to use:
Credentials credentials = GoogleCredentials
.fromStream(new FileInputStream("path/to/file"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials)
.setProjectId("my-project-id").build().getService();
Note that the default builder (using environment variables) is better if you are going to deploy your applications to cloud, because then this is automatically filled for you.
You need to set the shell variable. Run this command before mvn run.
export GOOGLE_APPLICATION_CREDENTIALS="/Users/ronnyshibley/Dev/eddress-service-key.json"
For authentication using the service account key, you can set the Environment Variable in your shell.
export GOOGLE_APPLICATION_CREDENTIALS="/Users/username/directory/service-key-file-name.json"
Then you need to start your IDE from the same session. I was stuck after exporting and setting up the environment variable and was still unable to use it.
I tried quitting the current IDE window and restarted the IDE again from the same session.
In my case it was Intellij, so in the terminal itself,
cd project directory
idea .
Or you can also add the environment variable in your bash profile and then source it.
I have tried several ways to do this, and none of them worked. Maven plugin
environmentVariables is the last thing that worked without any problem.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<executable>true</executable>
<environmentVariables>
<GOOGLE_APPLICATION_CREDENTIALS>/path/to/the/service-account.json</GOOGLE_APPLICATION_CREDENTIALS>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
You need to add env variable
GOOGLE_APPLICATION_CREDENTIALS=<path to google project json file >
If you are using IntelliJ idea, Edit the Project configuration and add the Environment variable
For images check this
https://www.twilio.com/blog/set-up-env-variables-intellij-idea-java

Spring Cloud config authentication with AWS Code Commit

I'm novice to use spring cloud config server, so is first time for me and i hope i can receive help, thanks in advance for this.
What i can try to do is to configure Spring cloud Configuration Server as central server for configurations among all Microservices.
We are using AWS CodeCommit as SCM repository.
The problem is that i receive always the same error once i try to inve an existing file on AWS CC.
I suppose is a problem of authentication, but i'm not sure
Error screenshot
This is the bootstrap configuration on Spring CSC
server.port=9090
management.security.enabled=false
logging.level.org.springframework.web: TRACE
spring.application.name=config-service
spring.cloud.config.server.git.uri: https://git-codecommit.eu-west-1.amazonaws.com/v1/repos/ServiceLayer
This is the file in my SCM repository that i'm trying to call:
configuration:
projectName: ms-login
server:
port: 8000
message:
greeting: Hello from the configuration
I've make a several try:
Placed the AWS GIT Credential (NOT the AWS Secret key) in bootstrap properties, but without result. Example:
spring.cloud.config.server.git.username: sergio.greco-at-682088819034
spring.cloud.config.server.git.password: xxxxxxx
Placed the AWS Secret Key \ Identity ID as java parameter once start, as follow:
-Daws.accessKeyId=xxxxxxxxYNOA -Daws.secretKey=xxxxxxxxxxxxxxxx
Set environment variables AWS_ACCESS_KEY_ID \ AWS_SECRET_ACCESS_KEY.
Can someone help me?
You need has the AWS SDK in the classpath.
Relevant documentation on this: http://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.1.RELEASE/
Search for “Authentication with AWS CodeCommit”.
Note: The aws-java-sdk-core jar is an optional dependency. If the aws-java-sdk-core jar is not on your classpath, then the AWS Code Commit credential provider will not be created regardless of the git server URI.

Searching for Weblogic class: MessageLoggerRegistryListener

Currently I'm working with EJB services running on Weblogic 12c Server. When I write a JUnit test to call EJB services, an error appears with:
java.lang.NoClassDefFoundError:
weblogic/i18n/logging/MessageLoggerRegistryListener
Anyone knows where is this class from? I googled it but got no result.
This class can be found in the /wlserver/modules/features/weblogic.server.merged.jar and in the /wlserver/server/lib/wlclient.jar files.
Additionally if you are doing this in a maven project, and you're using the Oracle Maven repository, you can use
<dependency>
<groupId>com.oracle.weblogic</groupId>
<artifactId>wlclient</artifactId>
<version>12.1.3-0-0</version>
<scope>provided</scope>
</dependency>
Set WL_HOME in your environment variable correctly:
Variable name: WL_HOME
Variable value:
your weblogic 12c installation path like: C:/Oracle/Middleware/Oracle_Home/wlserver/server

Categories

Resources