JHipster H2 DB Non-admin User - spring-boot

I am trying to run my spring-boot/liquibase/H2 database with a non-admin user and am having some problems understanding how to do this.
First off, I have seen some information here and tried to set up my application.yml this way.
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:test
username: USERLIMITED
password: user_limited_password
liquibase:
contexts: dev, faker
user: THELIQUIBASEUSER
password: THELIQUIBASEPASSWORD
Also put these sql statements in the changelog to run so that the user I want is created and given proper access controls:
<sql>DROP USER IF EXISTS USERLIMITED</sql>
<sql>CREATE USER USERLIMITED PASSWORD 'user_limited_password'</sql>
<sql>GRANT ALL ON APP TO USERLIMITED</sql>
When trying to start up the app, I get the following error:
2020-10-21 14:41:18.532 DEBUG 8704 --- [ restartedMain] c.c.config.LiquibaseConfiguration : Configuring Liquibase
2020-10-21 14:41:18.617 WARN 8704 --- [ test-task-1] i.g.j.c.liquibase.AsyncSpringLiquibase : Starting Liquibase asynchronously, your database might not be
ready at startup!
2020-10-21 14:41:20.226 ERROR 8704 --- [ restartedMain] com.zaxxer.hikari.pool.HikariPool : Hikari - Exception during pool initialization.
org.h2.jdbc.JdbcSQLInvalidAuthorizationSpecException: Wrong user name or password [28000-200]
What is interesting is if I change the LiquibaseConfiguration file to use synchronous DB configuration vs. the async by default I do not get an error.
// If you don't want Liquibase to start asynchronously, substitute by this:
SpringLiquibase liquibase = SpringLiquibaseUtil.createSpringLiquibase(liquibaseDataSource.getIfAvailable(), liquibaseProperties, dataSource.getIfUnique(), dataSourceProperties);
// SpringLiquibase liquibase = SpringLiquibaseUtil.createAsyncSpringLiquibase(this.env, executor, liquibaseDataSource.getIfAvailable(), liquibaseProperties, dataSource.getIfUnique(), dataSourceProperties);
Then if I go to the H2 console and perform a query to see my users I only have the one admin user (which should be a non-admin).
Trying to log in as the liquibase user that I set up in the yml
user: THELIQUIBASEUSER
password: THELIQUIBASEPASSWORD
is not there and I get the Wrong user name or password [28000-200] error.
This leads me to believe that it is something with how the application starts up and asynchronous task execution priority.
Any help is very much appreciated!

Related

AWS Secret Manager with Spring Boot Application

I tried to get secret manager value use this answer
How to integrate AWS Secret Manager with Spring Boot Application
But my application get secrets 2 times, first as I want with local profile, but second without any profile. Why application go to secret second time and how can I off this?
2021-08-19 11:40:01.214 INFO 9141 --- [ restartedMain] s.AwsSecretsManagerPropertySourceLocator : Loading secrets from AWS Secret Manager secret with name: secret/test_local, optional: false
2021-08-19 11:40:02.702 INFO 9141 --- [ restartedMain] s.AwsSecretsManagerPropertySourceLocator : Loading secrets from AWS Secret Manager secret with name: secret/test, optional: false
2021-08-19 11:40:02.956 ERROR 9141 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed
my config in bootstrap.yaml
aws:
secretsmanager:
prefix: secret
defaultContext: application
profileSeparator: _
name: test
start application with -Dspring.profiles.active=local
upd. If I create secret for secret/test I have the next one
s.AwsSecretsManagerPropertySourceLocator : Loading secrets from AWS Secret Manager secret with name: secret/application_local, optional: false
Currently there is no way to disable prefix/defaultContext look up.
If you take a look here you will see that prefix/ + defaultcontext of loading is always added and used.
You can check docs as well, to have more clear way what is being loaded and in what order.
My recommendation is to switch to spring.config.import since that will be the way we are going to take Secrets Manager importing. Big difference is that, it gives users a lot more control of which secrets they want to import since you can specify each key individually. spring.config.import can be found explained in docs or you can check the project sample .

Why doesn't h2-console default db name match the db name defined in application.properties file?

I am new to spring boot and taking a beginner course on udemy developing a project.
Below is my application.properties file -
application.properties
#logging
logging.level.com.vega.springit=DEBUG
info.application.name=Springit
info.application.description=Reddit clone using spring boot 2
info.application.version=0.0.1
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#h2
spring.h2.console.enabled=true
#datasource (default to testdb)
spring.datasource.name=springit
The default database on the h2-console is still testdb
And this is what I see on the console -
2020-11-29 01:19:29.315 INFO 28396 --- [ restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:358023a0-911e-4064-b2ec-e7046556c517'
My questions are -
Why doesn't h2-console display the database name that is set in the app.properties file?
Why does the console say database available at 'jdbc:hs:mem:xxx'?
When I try to connect to testdb or springit (as set in the file), I get following error -
Database "C:/Users/zankh/test" not found, either pre-create it or allow remote database creation (not recommended in secure environments) [90149-200] 90149/90149 (Help)
Also tried, connecting to 'jdbc:h2:mem:springit', but still get the same error.
I can connect to 'jdbc:hs:mem:xxx' but that's not how it works for the instructor in the course. What am I doing wrong here?
TIA
I fixed the problem by adding spring.datasource.url to the app.properties file
application.properties
#logging
logging.level.com.vega.springit=DEBUG
info.application.name=Springit
info.application.description=Reddit clone using spring boot 2
info.application.version=0.0.1
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
#h2
spring.h2.console.enabled=true
#datasource (default to testdb)
spring.datasource.name=springit
spring.datasource.url=jdbc:h2:mem:springit

TestContainer Oracle

according to https://www.testcontainers.org/modules/databases/jdbc/#database-containers-launched-via-jdbc-url-scheme , i'm trying to create an Oracle Container with Quarkus using jdbc url scheme.
After provided a valid docker image ("store/oracle/database-instantclient:12.2.0.1") and set this properties:
"%test":
quarkus:
datasource:
jdbc:
driver: org.testcontainers.jdbc.ContainerDatabaseDriver
url: jdbc:tc:oracle:///databasename
db-kind: other
i get this error:
Container is started (JDBC URL: jdbc:oracle:thin:system/oracle#localhost:32827:xe)
2020-11-09 17:33:06,719 INFO [🐳 .2.0.1]] (Agroal_13889837441) Container store/oracle/database-instantclient:12.2.0.1 started in PT4M7.8483772S
2020-11-09 17:33:06,738 WARN [io.agr.pool] (Agroal_13889837441) Datasource '<default>': Could not create new connection
2020-11-09 17:33:06,805 ERROR [io.qua.application] (main) Failed to start application (with profile test): org.flywaydb.core.internal.exception.FlywaySqlException:
Unable to obtain connection from database: Could not create new connection
--------------------------------------------------------------------------
SQL State : null
Error Code : 0
Message : Could not create new connection
at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:65)
at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:80)
at org.flywaydb.core.Flyway.execute(Flyway.java:453)
at org.flywaydb.core.Flyway.migrate(Flyway.java:158)
Can someone help me?
If I remember correctly, Flyway community editions (that ships with Quarkus) does not have support for Oracle. You need to use the Enterprise edition.
You need to replace the Quarkus Flyway dependency. Just exclude the one included and add the enterprise one (either in Maven or Gradle).

Failed to restore flyway version from base

I having troubles to set up my new dev environment.
I was working with flyway for an simple web app. The process worked well since now. I have a new work environment and I've used pg_dump and pg_sql to restore the base like the qualification environment to get back with the good set of datas.
even thought my public.schema_version on my local environment is well backed up (with all the line regarding previous migrations) my server won't start and keep saying this :
2017-11-27 15:48:55.476 INFO 12857 --- [ main] o.f.c.i.dbsupport.DbSupportFactory : Database: jdbc:postgresql://localhost:5432/volt (PostgreSQL 9.4)
2017-11-27 15:48:55.572 INFO 12857 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 110 migrations (execution time 00:00.050s)
2017-11-27 15:48:55.584 INFO 12857 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema "public": << Empty Schema >>
2017-11-27 15:48:55.587 INFO 12857 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema "public" to version 1.2 - Changing object type report
2017-11-27 15:48:55.595 ERROR 12857 --- [ main] o.f.core.internal.command.DbMigrate : Migration of schema "public" to version 1.2 - Changing object type report failed! Changes successfully rolled back.
Here 1.2 is the first screep that i've created.. And if I'm looking up to my local base I've all my flyway lines with the success column set to true (included the 1.2 one).
Does flyway keep the current version elsewhere than in the table "schema_version" ?
How do I tell flyway that my schema version is up to date regarding the migrations ?
PS : I'm using a spring-boot environment with only the flyway-core dependency in my pom.xml and this line in my spring-boot properties file
flyway:
baseline-on-migrate: true
I found out that i've used another user existing only in qualification to restore my base.. So my user in my development environment was not on the good schema of my base.
I've re-created my base with the proper user and everything was working again.

permission denied to create pg_catalog.sample_table using spring-boot

I'm using Spring-boot and configured database in application.properties file like below
spring.datasource.primary.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.primary.username=postgres
spring.datasource.primary.password=pass
spring.datasource.primary.driver-class-name=org.postgresql.Driver
spring.datasource.primary.maxActive=10
spring.datasource.primary.testWhileIdle = true
spring.datasource.primary.validationQuery = SELECT 1
spring.jpa.properties.hibernate.default-schema=public
spring.jpa.hibernate.ddl-auto:update
It works perfect in windows, but when I run it in Mac it shows some errors and does not create table
[ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate : HHH000388: Unsuccessful:
create table sample_table (table_id serial not null, name varchar(255))
2017-07-07 10:52:51.145 ERROR 1446 --- [ost-startStop-1] org.hibernate.tool.hbm2ddl.SchemaUpdate :
ERROR: permission denied to create "pg_catalog.sample_table"
Detail: System catalog modifications are currently disallowed.
I tried below things to resolve the issue
By changing default schema in properties to something else.
By changing ddl-auto to create, create-drop and none.
but have no success.
I don't know why it is trying to create table in pg_catalog schema?
I've solved it by changing below line from application.properties file
spring.jpa.properties.hibernate.default-schema=public
to
spring.jpa.properties.hibernate.default_schema=public
and it worked.

Resources