chef-solo not updating postgres pg_hba.conf - postgresql

I am using Chef Solo to provision a Vagrant Virtual Machine. Here is the relevant Vagrantfile snippet:
chef.run_list = [
"databox::default",
"mydbstuff"
]
chef.json = {
"postgresql": {
"config" : {
"listen_addresses": "*"
},
"pg_hba": [
{"type": "local", "db": "all", "user": "postgres", "addr": null, "method": "ident"},
{"type": "local", "db": "all", "user": "all", "addr": null, "method": "md5"},
{"type": "host", "db": "all", "user": "all", "addr": "127.0.0.1/32", "method": "md5"},
{"type": "host", "db": "all", "user": "all", "addr": "::1/128", "method": "md5"},
{"type": "local", "db": "all", "user": "vagrant", "addr": null, "method": "ident"},
{"type": "host", "db": "all", "user": "all", "addr": "192.168.248.1/24", "method": "md5"}
]
},
"databox": {
"db_root_password": "abc123",
"databases": {
"postgresql": [
{ "username": "db1", "password": "abc123", "database_name": "db1" },
{ "username": "db2", "password": "abc123", "database_name": "db2" }
]
}
}
}
The mydbstuff::default recipe looks like this:
postgresql_connection_info = {
:host => "localhost",
:port => node['postgresql']['config']['port'],
:username => 'postgres',
:password => node['postgresql']['password']['postgres']
}
postgresql_database_user 'vagrant' do
connection postgresql_connection_info
password 'vagrant'
action :create
end
node['databox']['databases']['postgresql'].each do |db|
postgresql_database_user 'vagrant' do
connection postgresql_connection_info
action :grant
database_name db.database_name
end
end
I am trying to allow connections by the local vagrant user without a password, and by any user from the VirtualBox private network. The pg_hba array in my chef.json has four lines that are copied from the default configuration and two lines to do the other stuff that I want to do. If I add these two lines to the pg_hba.conf file manually, they work just fine.
The problem is that my changes aren't actually written to the pg_hba.conf file. What's preventing them from being written?

It appears that the databox cookbook overwrites the Postgres permissions array using node.set instead of just modifying the part that it needs.
I have submitted a pull request to the project to change this behavior so that additional entries can be added to the file.

I faced same problem with chef-solo. My way out was to create a template for pg_hba.conf and replaced at the end of execution of recipe.

Related

Connect strapi with mongoose to a MongoDb (mLab)

I tried to connect Strapi to mLab with this database.js config but it doesn't work. I get the error :
ConnectorError: connector "strapi-hook-mongoose" not found: Cannot find module 'strapi-connector-strapi-hook-mongoose'
Here is my database.js config file :
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "strapi-hook-mongoose",
"settings": {
"database": "strapi-test",
"host": "ds131914.mlab.com",
"srv": false,
"port": "31914",
"username": "root",
"password": "root010101"
},
"options": {
"authenticationDatabase": "strapi-test"
}
}
}
}
What should I do ?
After some search, it appers to me that this database.js config was from an old tutorial (this one). So to solve this probleme, you first need to install npm i -S strapi-connector-mongoose in order to install the right connecter.
Now, you need to change you database.js config for the desire environement. In my case, it was production. So edit config/environement/production/database.js like this :
{
"defaultConnection": "default",
"connections": {
"default": {
"connector": "mongoose",
"settings": {
"client": "mongo",
"host": "ds131914.mlab.com",
"port": "31914",
"srv": false,
"database": "strapi-test",
"username": "root",
"password": "root010101"
},
"options": {
"authenticationDatabase": "strapi-test",
"ssl": false
}
}
}
}
Like this, it should work !

loopback w/ table schemas, not identifying schema in options

Nature of the issue
My db2 database makes wide use of table schemas for organization, so the table in question is LIVE.TBLADDRESS -
My model uses the "options" to specify the table schema
"options": {
"idInjection": false,
"db2": {
"schema": "LIVE",
"table": "TBLADDRESS"
}
}
the model is in the model-config.json using
,"Tbladdress": {
"dataSource": "x3",
"public": true
}
I get an error when I try to use the explorer to do a simple 'get' or any other API call.
"statusCode": 500,
"name": "Error",
"message": "[IBM][CLI Driver][DB2/LINUXX8664] SQL0204N "DB2X.TBLADDRESS" is an undefined name. SQLSTATE=42704\r\n",
Expected behavior
Once I specified the schema - I'd expect the API to resolve correctly
Actual behavior
The default schema for db user is used at all times...regardless of specified schema in options.
Suggested resolution
Maybe I set it in the wrong place, I will continue to look for the information, It is possible I am missing something.
This is what I "see" using DB Viewer...so you have an idea what I'm referring to.
DEV - host:50000/DEV
-schemas
|-AAA
|-BBB
|-DB2X (this is the schema that the error is referring to...but NOT the one specified in the model)
|-DDD
|-LIVE (this is the correct schema)
|--Tables
|--|-TBLA
|--|-TBLADDRESS
|-ZZZ
If it helps - this happens with manually create models or models generated by discovery scripts.
These are my config files, and model
/common/models/Tbladdress.json
{
"name": "Tbladdress",
"options": {
"idInjection": false,
"db2": {
"schema": "LIVE",
"table": "TBLADDRESS"
}
},
"properties": {
...
}
}
/datasources.json
{
"db": {
"name": "db",
"connector": "memory"
},
"x3": {
"name": "x3",
"connector": "db2",
"username": "...",
"password": "...",
"database": "...",
"hostname": "...",
"port": 50000
}
}
/model-config.json
{
"_meta": {
...
},
"User": {
"dataSource": "db"
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false,
"options": {
"strictObjectIDCoercion": true
}
},
"Role": {
"dataSource": "db",
"public": false
}
,"Tbladdress": {
"dataSource": "x3",
"public": true
}
}
http://localhost:3000/explorer/#!/Tbladdress/Tbladdress_findById
{
"error": {
"statusCode": 500,
"name": "Error",
"message": "[IBM][CLI Driver][DB2/LINUXX8664] SQL0204N \"DB2X.TBLADDRESS\" is an undefined name. SQLSTATE=42704\r\n",
"errors": [],
"error": "[node-ibm_db] SQL_ERROR",
"state": "42S02",
"stack": "Error: [IBM][CLI Driver][DB2/LINUXX8664] SQL0204N \"DB2X.TBLADDRESS\" is an undefined name. SQLSTATE=42704\r\n"
}
}
...Headers...
{
"date": "Sun, 18 Feb 2018 05:20:36 GMT",
"x-content-type-options": "nosniff",
"x-download-options": "noopen",
"x-frame-options": "DENY",
"content-type": "application/json; charset=utf-8",
"transfer-encoding": "chunked",
"connection": "keep-alive",
"access-control-allow-credentials": "true",
"vary": "Origin, Accept-Encoding",
"x-xss-protection": "1; mode=block"
}
USING:
loopback-cli v3 to generate express app
loopback-connector-db2 to connect to DB2 v10
Node v8.9.2
Package.JSON dependencies looks like this (as mentioned it's a default install, with one model added - to see if I could get it to work)
"dependencies": {
"compression": "^1.0.3",
"cors": "^2.5.2",
"helmet": "^1.3.0",
"loopback": "^3.0.0",
"loopback-boot": "^2.6.5",
"loopback-component-explorer": "^5.0.0",
"loopback-connector-db2": "^2.1.1",
"serve-favicon": "^2.0.1",
"strong-error-handler": "^2.0.0"
},
Yes - the DB2 connector worked fine when I specified the "LIVE" schema on data discovery - but it does NOT seem to be working when I use the API. I don't know if it's the connector or the loopback app.
For loopback-connector-db2, you must define SCHEMA in the datasources.json config file.
{
"x3": {
"name": "x3",
"connector": "db2",
"username": "...",
"password": "...",
"database": "...",
"hostname": "...",
"port": 50000
},
"x3Live": {
"name": "x3Live",
"connector": "db2",
"schema": "LIVE",
"username": "...",
"password": "...",
"database": "...",
"hostname": "...",
"port": 50000
}
}
Unfortunately, you will need to create a new datasource (e.g. x3Live). Use the old x3 datasource for the models using the DB2X schema, and the new x3Live datasource for the models using the LIVE schema.

How to make a post API for mongodb with loopback

I am newbie with mongodb and loopback. I want to send and save data from my app to database. how can I do that?
Update
shops model:
{
"shopname": "string",
"tel": "string",
"latlng": "string",
"address": "string",
"id": "string",
"personId": "string"
}
CURL:
curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \
"shopname": "spring", \
"tel": "12345678", \
"latlng": "52.1106986,21.7768998", \
"address": "05-319 Skwarne, Poland" \
}' 'http://localhost:3000/api/shops'
Now what should I write in shops.js to give an api for using in app to send data to database ?
'use strict';
module.exports = function(Shops) {
};
you should have provided more info about the steps you have already done.
let me begin with the first step:
download and install mongodb on your server: link
after running mongodb, add your desired database info to datasources.json file. e.g.
{
"db": {
"name": "db",
"connector": "memory"
},
"shopDB": {
"host": "localhost",
"port": 27017,
"url": "mongodb://localhost:27017/shopDB",
"database": "shopDB",
"password": "",
"name": "shopDB",
"user": "",
"connector": "mongodb"
}
}
add loopback-connector-mongodb to your project via npm.
now define your model(you can utilize loopback's user friendly command line interface to do so. call command "slc loopback:model" in your projects root folder)
after you finish the step 4, loopback will create 2files for you: shop.js and shop.json => these files are located in your projectFolder/common/models/ directory. note that it's a good practice to follow the loopback's convention in naming models and name your model in singular form(shop). (it uses the plural forms of the model names in other parts of the project). your shop.json should look like the below code:
{
"name": "shop",
"plural": "shops",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"shopname": {
"type": "string",
"required": true
},
"tel": {
"type": "string",
"required": true
},
"latlng": {
"type": "string",
"required": true
},
"address": {
"type": "string"
},
"personId": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
now you can post your shop json to http://localhost:3000/api/shops/ . note that our shop model inherits from PersistedModel base model and has some built-in functions to do crud operations. and if you want just create some shop instances in your db, you won't need to add anything to your shop.js file!

Replicating a remote database from Bluemix

Database URL:
https://$USERNAME:$PASSWORD#$REMOTE_USERNAME.Cloudant.com/$DATABASE_NAME
What is the value of $USERNAME、$PASSWORD、$REMOTE_USERNAME?
The current Cloudant account:
VCAP_SERVICES
{
"cloudantNoSQLDB": [
{
"credentials": {
"username": "c39cexxx-bluemix",
"password": "xxxxxxx",
"host": "c39cexxx-bluemix.cloudant.com",
"port": 443,
"url": "https://c39cexxx-bluemix:xxxxxxx#c39cexxx-bluemix.cloudant.com"
},
}
]
}
The other Cloudant accounts in a database
VCAP_SERVICES
{
"cloudantNoSQLDB": [
{
"credentials": {
"username": "f39c4xxx-bluemix",
"password": "xxxxxxx",
"host": "f39c4xxx-bluemix.cloudant.com",
"port": 443,
"url": "https://f39c4xxx-bluemix:xxxxxxx#f39c4xxx- bluemix.cloudant.com"
},
}
]
}
Please give an example of replicating a remote database from Bluemix.
If you want to replicate from a remote target into a local database, your Database URL will be:
https://$remote_username:$remote_password#$remote_username.cloudant.com/$remote_database
E.g.
https://f39c45g0-bluemix:0ebdc6c7#f39c45g0-bluemix.cloudant.com/the_remote_database
You can find more information here: https://developer.ibm.com/clouddataservices/cloudant-replication/
NOTE: I'm assuming you didn't post your actual credentials, if you did you should at least change your passwords.

Loopback: Connect with xampp server using Windows

I am trying to connect to my xampp server but it is not being connected.
Here is my datasources.json
{
"db": {
"name": "db",
"connector": "memory"
}
"test": {
"host": "localhost",
"port": 3306,
"database": "test",
"name": "test",
"debug": false,
"connector": "mysql",
"socketPath" : "C:/xampp/mysql/bin/my.ini"
}
}
I do not know that my socket path is having an issue.
Error:
Error:connect ENOTSOCK C:/xampp/mysql/bin/my.ini
In your config file socketpath should be given socket file that is .sock extension commonly
C:/xampp/mysql/mysql.sock
You are not gaving the password and username of mysql database
"test": {
"host": "127.0.0.1",
"port": 3306,
"database": "mysqltest",
"password": "", ///password
"name": "test",
"connector": "mysql",
"user": "root" ///username
}