testcontainers mysql DB failed when population DB - java

I am trying to run integration tests with testcontainers.
Lunch the testconiners with follow properties:
MySQLContainer database = (MySQLContainer) new MySQLContainer("mysql:8.0.27")
.withUsername("test")
.withPassword("test")
.withEnv("MYSQL_ROOT_PASSWORD", "test")
.withReuse(true);
database.withInitScript("init.sql");
database.start();
but it fails when run the script: Access denied for user 'test'#'%' to database 'test_scheme'
the init.sql script contains command for creating several DB
CREATE SCHEMA IF NOT EXISTS test_scheme
CREATE SCHEMA IF NOT EXISTS onothere_scheme

Judging by the error message, you are connecting as user 'test', not 'root'.
In that case you will have to add something like this to your init script:
GRANT ALL PRIVILEGES ON test_scheme.* TO 'test'#'%'
Since you are only using this for testing purposes, granting all privileges should be fine. Of course you could reduce this if you want to enable only read access, for instance.
You could also avoid this completely by connecting as the root user, who has access to all schemas by default.

The below works:
#Test
public void testExplicitInitScript() throws Exception {
try (MySQLContainer<?> container = new MySQLContainer<>(DockerImageName.parse("mysql:8.0.24"))
.withUsername("root").withPassword("")
.withInitScript("init_mysql.sql")
.withLogConsumer(new Slf4jLogConsumer(log))) {
container.start();
}
}
src/test/resources/init_mysql.sql
CREATE SCHEMA IF NOT EXISTS test_scheme
CREATE SCHEMA IF NOT EXISTS onothere_scheme

Related

Storage account connection string for 'AzureWebJobsAzureCosmosDBConnection' is invalid

I have this problen when I try to run a function with BlobTrigger.
Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.myFunction'. Microsoft.Azure.WebJobs.Extensions.Storage:
Storage account connection string for 'AzureWebJobsAzureCosmosDBConnection' is invalid.
The variables are:
AzureWebJobsAzureCosmosDBConnection = AccountEndpoint=https://example.com/;AccountKey=YYYYYYYYYYY;
AzureWebJobsStorage = UseDevelopmentStorage=true
AzureCosmosDBConnection = AccountEndpoint=https://example.com/;AccountKey=YYYYYYYYYYY;
I don't know why this function throws exception....
Not Sure, if you have written your local.settings.json configuration will be in the format of key = value or just mentioned in the question.
The format of local.settings.json configuration to any Azure Function will be "key":"value":
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=pravustorageac88;AccountKey=<alpha-numeric-symbolic_access_key>;EndpointSuffix=core.windows.net",
"FUNCTIONS_WORKER_RUNTIME": "java",
"MAIN_CLASS":"com.example.DemoApplication",
"AzureWebJobsDashboard": "DefaultEndpointsProtocol=https;AccountName=pravustorageac88;AccountKey=<alpha-numeric-symbolic_access_key>;EndpointSuffix=core.windows.net"
"AzureCosmosdBConnStr":"Cosmos_db_conn_str"
}
}
If you are using Cosmos DB Connection string, then you have to configure in such a way:
public HttpResponseMessage execute(
#HttpTrigger(name = "request", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<User>> request,
#CosmosDBOutput(name="database", databaseName = "db_name", collectionName = "collection_name", connectionStringSetting = "AzureCosmosDBConnection")
ExecutionContext context)
Make Sure the cosmos database connection string present in the local.settigns.json file should be published to Azure Function App Configuration Menu > Application Settings.
For that either uncomment local.settings.json from .gitignore file or add the configuration settings manually to the Azure Function App Configuration:
I have uncommented the local.settings.json in .gitignore file and then published to Azure Function App as the cosmos database connection string also updated in the configuration:
Note:
If you have a proxy in the system, then you have to add the proxy settings in func.exe configuration file, given here by #p31415926
In two of the ways, you can configure the Cosmos DB Connection in the Azure Function Java Stack: Bindings (as code given above) and using the SDK given in this MS Doc.

Postgres connectivity via JDBC: org.postgresql.util.PSQLException: ERROR: relation "prescriptions" does not exist

I'm having a ton of trouble connecting to a dockerized postgres instance. The error I get looks like this:
Exception in thread "main" org.postgresql.util.PSQLException: ERROR: relation "prescriptions" does not exist
Position: 29
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424)
at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:321)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:313)
Here are the steps to recreate the issue. First, I set up the database like so:
docker run -p 7654:5432 --name zoo_postgres -e POSTGRES_PASSWORD="stranger" -e POSTGRES_USER=stranger -d postgres
And run a setup script, that looks like this:
create DATABASE stranger;
create SCHEMA stranger;
CREATE TABLE prescriptions (
prescription_id BIGINT NOT NULL,
PRIMARY KEY (prescription_id)
);
CREATE USER stranger_user WITH ENCRYPTED password 'stranger_user';
grant CONNECT on DATABASE stranger to stranger_user;
grant USAGE on SCHEMA stranger to stranger_user;
GRANT SELECT ON ALL TABLES IN SCHEMA stranger TO stranger_user;
GRANT INSERT ON ALL TABLES IN SCHEMA stranger TO stranger_user;
GRANT DELETE ON ALL TABLES IN SCHEMA stranger TO stranger_user;
GRANT UPDATE ON ALL TABLES IN SCHEMA stranger TO stranger_user;
-- I've tried several variations of the line below, without any effect
ALTER USER stranger_user SET SEARCH_PATH to stranger,stranger_user,public;
On the Java side, the code is extremely simple and looks like the following:
public static void main(String[] args) throws Exception {
Class.forName("org.postgresql.Driver");
Connection connection = DriverManager.getConnection("jdbc:postgresql://localhost:7654/stranger","stranger_user", "stranger_user"); // this succeeds (suggesting the connection string is correct)
connection.setCatalog("stranger");
connection.setSchema("stranger");
Statement stmt = connection.createStatement();
stmt.execute("select prescription_id from prescriptions");
// code dies before getting to the close line.
connection.close();
System.out.println("Successfully connected using JDBC directly");
}
Setting the catalog and schema has no effect and neither does changing the search path. Everything is lower case, so I don't think it has anything to do with escaping the table names. I've tried using the real user (and skipping the user account), omitting the database name and several other tricks. I suspect that there's something wrong with the way that I'm connecting to the database, but I can connect to is using psql, like so:
% psql -h localhost -p 7654 -d postgres -U stranger_user
From this point, I can access the tables with normal sql:
% psql -h localhost -p 7654 -d postgres -U stranger_user
Password for user stranger_user:
psql (9.4.6, server 9.5.2)
WARNING: psql major version 9.4, server major version 9.5.
Some psql features might not work.
Type "help" for help.
postgres=> \d
List of relations
Schema | Name | Type | Owner
----------+---------------+-------+----------
stranger | prescriptions | table | stranger
(1 row)
postgres=> select * from prescriptions;
prescription_id
-----------------
(0 rows)
postgres=> select * from stranger.prescriptions;
prescription_id
-----------------
(0 rows)
I'm using the latest version of the postgres driver: "org.postgresql:postgresql:9.4+", and can't see anything else that would cause this issue. At this point I'm out of ideas and can't figure out what's wrong.
You're connecting with psql to port 7654, and then in your JDBC URL to port 6543. Also, you're connecting to the postgres database with psql, and the stranger database in your JDBC URL.
Could these differences be the source of the exception you're seeing?
https://jdbc.postgresql.org/documentation/94/connect.html
If you use pgAdmin or psql, and log in to server localhost:6543, database stranger with user stranger_user and password stranger_user, what do you see?
I expect that you see an empty public schema.
Running create DATABASE stranger does not make it the active database.
So, running create SCHEMA stranger will create that schema in the postgres database. It also won't make the new schema active.
So, running CREATE TABLE prescriptions will create that table in the public schema of the postgres database.
Of course, all that is not even in the PostgreSQL database instance/cluster served on port 6543, because you did all that on a different instance/cluster on port 7654.

MongoDB: how to create an authenticated db via java driver

I'm trying to create an authenticate database in MongoDB 2.6 using java driver v 2.12.
In particular I need to create a user accessing to admin collection.
Any suggestion?
Thanks.
Here my solution:
MongoClient mcAdmin = new MongoClient(
configuration.getServerAddresses(),
Arrays.asList(MongoCredential.createMongoCRCredential(
MONGODB_ADMIN_USERNAME, "admin",
MONGODB_ADMIN_PASSWORD.toCharArray())));
try {
mcAdmin.setWriteConcern(WriteConcern.JOURNALED);
DB db = mcAdmin.getDB(userDbName);
BasicDBObject commandArguments = new BasicDBObject();
commandArguments.put("user", userUsername);
commandArguments.put("pwd", userPassword);
String[] roles = { "readWrite" };
commandArguments.put("roles", roles);
BasicDBObject command = new BasicDBObject("createUser",
commandArguments);
db.command(command);
} finally {
mcAdmin.close();
}
Doing this in Java code is not the best way to do it, and except for very rare use cases (writing an admin application for MongoDB) even one I would strongly advice against.
Security risk
First of all, your application would need extremely high privileges, namely userAdminAnyDatabase or userAdmin on the admin database, which more or less grants the same rights: creating a superuser at will. To put it in other words: this code would be a high security risk.
Granting roles and rights on a database is an administrative task and for good reasons should be decoupled from an application accessible by arbitrary users.
Technical problems
Activating authentication from a client simply is impossible. The mongod instance in question has to be started with authentication enabled. Furthermore, you would have to save to create a user with the mentioned roles before you could have your app administer users. The problem: you would have to store the password for that user somewhere. Unless you encrypt it, you basically store the most powerful password for your MongoDB databases and cluster in cleartext. And if you encrypt it, you have to pass the key for decryption to your application at some point in a secure manner. And all this to break best practices ("Separation of concerns")?

not authorized for query on databasename.collection on mongodb using eclipse

I gave role for DB purp is UserAdmin,I run server with mongod --auth --dbpath c:\mongodb\data\db.
First I done a java file in eclipse to connect DB,it worked properly.
After I create and run the below file in eclipse:
try {
MongoClient mongoClient = new MongoClient("localhost", 27017);
DB db = mongoClient.getDB("purplista");
boolean auth =db.authenticate("purp","123".toCharArray());
System.out.println("Connect to database successfully");
DBCollection doc = db.getCollection("test");
System.out.println("Collection test selected successfully..");
DBCursor cursor = doc.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
}
} catch (Exception e) {
e.printStackTrace();
}
got error like this:
Connect to database successfully..
Collection test selected successfully..
com.mongodb.MongoException: not authorized for query on purplista.test
at
com.mongodb.QueryResultIterator.throwOnQueryFailure(QueryResultIterator.java:214)
at com.mongodb.QueryResultIterator.init(QueryResultIterator.java:198)
at
com.mongodb.QueryResultIterator.initFromQueryResponse(QueryResultIterator.java:176)
at
com.mongodb.QueryResultIterator.(QueryResultIterator.java:64)
at com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:80) at
com.mongodb.DBCollectionImpl.find(DBCollectionImpl.java:61) at
com.mongodb.DBCursor._check(DBCursor.java:458) at
com.mongodb.DBCursor._hasNext(DBCursor.java:546) at
com.mongodb.DBCursor.hasNext(DBCursor.java:571) at
purplista.FindDoc.main(FindDoc.java:34)
A couple of issues I see are:
You are not checking the return value of db.authenticate() which may be returning false if it fails to authenticate.
Even if authenticated, you may not have appropriate permissions to perform the intended operations (in your case read) on the specified database.
A few things to check:
Is this a valid user / password - may be try mongo shell using the
same credentials
If valid, does it have appropriate permissions on the database you are using? Since you can create users in "admin" database that can be used to authenticate, however those users may not be setup for additional access that you need for your operations.
If you are using MongoDB v2.6, all the users must be in "admin" database.
You can check for "system.users" collection under the database of interest (test) and admin database to check where the user is setup.

code working with sa account but not with other

I have a jdbc properties file with
database.username=mfuser
database.password=mfuser
If I changed this to user/pass --> sa/pass then my web application works perfectly but with mfuser it does. I have create a user in SQL Server Security with name mfsuer and password mfuser but it still not working....
How I need to create the user mfuser so that it works? Any special privileges I need to provide for this user? I want it same like sa
Here is the error I am getting with mfuser
org.springframework.dao.PermissionDeniedDataAccessException: PreparedStatementCallback; SQL [select lm.login_id,lm.login_name,lm.login_password,lm.full_name, lm.email_address,lm.phone,lm.notes,lm.delete_status,lm.create_date,lm.create_by,lm.modify_date,lm.modify_by FROM Login_Master lm where lm.login_id=(select lm1.login_id from Login_master lm1 where lm1.login_name=? and lm1.login_password =?) ]; The SELECT permission was denied on the object 'Login_Master', database 'MCData', schema 'dbo'.; nested exception is java.sql.SQLException: The SELECT permission was denied on the object 'Login_Master', database 'MCData', schema 'dbo'.
The user mfuser doesn't have enough privileges to alter data in your tables, schema or database. You should access to your SQL Server 2008 using the sa user and grant enough privileges to the mfuser.
Follow the steps in this post to get your problem solved.
Looks like that the user mfuser doesn't have access on table your application needs to access.
If you created the database schema using sa user you have to give access to mfuser.

Categories

Resources