I have a grails project where I need to create an encrypted H2 database, but I'm not sure how to make it work. Here is what I have in DataSource.groovy:
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
dialect = "org.hibernate.dialect.H2Dialect"
dbCreate = "update" // one of 'create', 'create-drop','update'
url = "jdbc:h2:/opt/viewpoint/data/h2/viewpoint;MODE=MYSQL;CIPHER=AES"
user = "sa"
pwds = "filepwd password"
}
When I run it, I get the following:
Caused by: org.h2.jdbc.JdbcSQLException: Wrong password format, must be: file password <space> user password [90050-117]
at org.h2.message.Message.getSQLException(Message.java:105)
at org.h2.message.Message.getSQLException(Message.java:116)
at org.h2.message.Message.getSQLException(Message.java:75)
at org.h2.message.Message.getSQLException(Message.java:151)
at org.h2.engine.ConnectionInfo.convertPasswords(ConnectionInfo.java:264)
at org.h2.engine.ConnectionInfo.<init>(ConnectionInfo.java:72)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:94)
at org.h2.Driver.connect(Driver.java:58)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at $Proxy46.getMetaData(Unknown Source)
... 23 more
I'm not sure where you got your datasource configuration example from, but you need to use username and password instead of user and pwds:
dataSource {
pooled = true
// ...
username = "sa"
password = "filepwd password"
}
Related
I want to add other datasource to use with GORM.
DataSource.groovy
development {
dataSource {
dbCreate = "update"
driverClassName = "org.postgresql.Driver"
username = "usrcastor"
url = "jdbc:postgresql://localhost:5432/dbcastor_master"
password = "castor"
dialect = net.sf.hibernate.dialect.PostgreSQLDialect
pooled = true
loggingSql = false
}
dataSource_otherDataSource {
dbCreate = "update"
driverClassName = "org.postgresql.Driver"
username = "usrcastor"
url = "jdbc:postgresql://localhost:5432/dbcastor_child_a"
password = "castor"
dialect = net.sf.hibernate.dialect.PostgreSQLDialect
pooled = true
loggingSql = false
}
}
But already have a lot of domain classes and I no have time to define static mapping in each one.
Domain.groovy
static mapping {
datasource 'ALL'
}
Is posible to configurate grails to set datasource 'ALL' for all domains?
You can add something like this to grails-app/conf/application.groovy
grails.gorm.default.mapping = {
datasource 'ALL'
}
See the documentation for Grails mappings.
When ever I start my Grails application, I get these errors for all my domain classes.
ERROR hbm2ddl.SchemaExport - HHH000389: Unsuccessful: drop table domain_class cascade constraints
ERROR hbm2ddl.SchemaExport -ORA-01031: insufficient privileges
and then
ERROR hbm2ddl.SchemaExport -ORA-00942: table or view does not exist
I am using in-memory database for the application and my DataSource.groovy has this in it:
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
local {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
development {
dataSource {
dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:h2:mem:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
Is there something wrong with the settings in DataSource?
I start my application with grails -Dgrails.env=local run-app -https
I tried to create objects during startup using BootStrap.groovy and even they fail. I use GGTS for development. What privileges is this talking about?
We found the answer. The dataSource in file instanceConfig.local.properties in the resources folder was pointing to an Oracle database and I am not configured to a role to create or drop tables there. Hence the insufficient privileges error.
Even though the DataSource.groovy had the in-memory database setting, I assume that the instanceConfig.local.properties overrides it. Thanks anyways for the help guys!
By the reference of the question Is it possible to use the Grails Jasypt plugin outside the GORM layer for simple String encryption and decryption?
i tried to implement it for my password encryption / decryption .
But every-time its giving different encrypted value for the same password . so how can i use the following code and the jasypt configuration ?
def authenticate(){
def jasyptConfig = grailsApplication.config.jasypt
org.jasypt.encryption.pbe.StandardPBEStringEncryptor stringEncryptor =
new org.jasypt.encryption.pbe.StandardPBEStringEncryptor(jasyptConfig)
def encrypted = stringEncryptor.encrypt(params.password)
}
jasypt {
algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC"
providerName = "BC"
password = "test"
keyObtentionIterations = 1000
}
The salt!
If you don't care, just set a ZeroSaltGenerator instance to your Encryptor. By default, it is RandomSaltGenerator, so the outputs are different.
If you want to use ZeroSaltGenerator then you have to make following changes:
Inside your config.groovy:
jasypt {
encryptorRegisteredName = "gormEncryptor"
}
And in your resources.groovy:
beans = {
hibernateStringEncryptor(HibernatePBEStringEncryptor) {
registeredName = "gormEncryptor"
algorithm = "PBEWITHSHA256AND256BITAES-CBC-BC"
providerName = "BC"
password = "test"
keyObtentionIterations = 1
saltGenerator = new org.jasypt.salt.ZeroSaltGenerator()
}
}
And it will generate the same encrypted values everytime.
My understanding is by default Grails using the H2 embedded database for testing.
My Datasource.groovy configures a local Postgres database as a root datasoure
dataSource {
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
url = ...
pooled = true
...
But I don't want this used in integration testing. I would prefer to use the H2 embedded option. So how do I override this for integration test environment and make it use embedded H2 database?
Thanks
Place the baseline configuration in the dataSource block and override it with an environment block as explained in section 4.2 Environments of the Grails documentation.
dataSource {
pooled = true
driverClassName = "org.h2.Driver"
username = "sa"
password = ""
}
environments {
development {
dataSource {
dbCreate = "create-drop"
url = "jdbc:h2:mem:devDb:MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:h2:mem:testDb:MVCC=TRUE;LOCK_TIMEOUT=10000"
}
}
production {
dataSource {
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
dbCreate = ...
url = ...
}
}
}
I have the same question, but until now i didn't find an answer. Perhaps setting the field dialect inside Datasource.groovy like this:
test {
dataSource {
dbCreate = "update"
dialect='org.hibernate.dialect.H2DialectPatch'
url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
}
}
I'm trying to deploy a Grails application on Openshift.
I'm deploying the app using a locally built war file.
Right now, I am using the url given by the rhc-app-show command in the DataSource.groovy file for my Database configurations.
Whenever I try with the environment variables of Openshift (eg. $OPENSHIFT_MYSQL_DB_HOST), it crashes. Any idea or pointers on how to use those in the config file?
Thanks.
I haven't used OpenShift but Google led me to the FAQ which shows these environment variables:
OPENSHIFT_MYSQL_DB_HOST
OPENSHIFT_MYSQL_DB_PASSWORD
OPENSHIFT_MYSQL_DB_USERNAME
OPENSHIFT_MYSQL_DB_URL
OPENSHIFT_MYSQL_DB_PORT
so it looks like this would work:
production {
dataSource {
driverClassName = 'com.mysql.jdbc.Driver'
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
String host = System.getenv('OPENSHIFT_MYSQL_DB_HOST')
String port = System.getenv('OPENSHIFT_MYSQL_DB_PORT')
String dbName = System.getenv('OPENSHIFT_APP_NAME')
url = "jdbc:mysql://$host:$port/$dbName"
username = System.getenv('OPENSHIFT_MYSQL_DB_USERNAME')
password = System.getenv('OPENSHIFT_MYSQL_DB_PASSWORD')
properties {
...
}
}
}
The missing bit is the database name - is that something that you would have available? I'm not sure of the format of OPENSHIFT_MYSQL_DB_URL but it looks like you might just be able to use url = "jdbc:${System.getenv('OPENSHIFT_MYSQL_DB_URL')}"
POSTGRES SQL ALSO ...there is a nice , tutorial for that on this link ...but the datasource configuration must be configured like this ....
production {
dataSource {
dbCreate = "update"
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
uri = new URI(System.getenv('OPENSHIFT_POSTGRESQL_DB_URL'))
url = "jdbc:postgresql://"+uri.host+uri.path+"/"+System.getenv('OPENSHIFT_APP_NAME')
username = System.getenv('OPENSHIFT_POSTGRESQL_DB_USERNAME')
password = System.getenv('OPENSHIFT_POSTGRESQL_DB_PASSWORD')
}
second Alternative . . .
production {
dataSource {
dbCreate = "update"
driverClassName = "org.postgresql.Driver"
dialect = org.hibernate.dialect.PostgreSQLDialect
uri = new URI(System.getenv('OPENSHIFT_POSTGRESQL_DB_URL').toString())
appname = System.getenv('OPENSHIFT_APP_NAME').toString()
url = "jdbc:postgresql://"+uri.host.toString()+uri.path.toString()+"/"+appname
username = System.getenv('OPENSHIFT_POSTGRESQL_DB_USERNAME').toString()
password = System.getenv('OPENSHIFT_POSTGRESQL_DB_PASSWORD').toString()
}
In Grails 3.0.11, File application.yml
System.getenv doesn't work, so i had to configure manually the url connection for database in mysql.... Use the command of documentation https://developers.openshift.com/en/managing-port-forwarding.html
dataSource:
dbCreate: update
url : jdbc:mysql://thisIsTheUrlGetWithPort-ForwardCommandAndThePort:48381/server?verifyServerCertificate=false&autoReconnect=true&useSSL=false&requireSSL=false
driverClassName: com.mysql.jdbc.Driver
dialect: org.hibernate.dialect.MySQL5InnoDBDialect
username : admin9CzsS #this is a username example
password : RR7y9uKw3t #this is a password example
properties:
jmxEnabled: true
initialSize: 5
maxActive: 50
minIdle: 5
maxIdle: 25
maxWait: 10000
maxAge: 600000
timeBetweenEvictionRunsMillis: 5000
minEvictableIdleTimeMillis: 60000
validationQuery: SELECT 1
validationQueryTimeout: 3
validationInterval: 15000
testOnBorrow: true
testWhileIdle: true
testOnReturn: false
jdbcInterceptors: ConnectionState
defaultTransactionIsolation: 2 # TRANSACTION_READ_COMMITTED