How to define a user with SELECT privileges with puppet? - postgresql

I want to create a user in postgres who can only made SELECT in all the tables of the current database.
How can specify ALL tables in puppet?
Here an extract of my puppet file:
...
postgresql::server::database_grant { 'PnBP':
privilege => 'CREATE',
db => 'db',
role => 'role',
}
postgresql::server::table_grant { 'SELECT':
privilege => 'SELECT',
table => 'ALL',
db => 'db',
role => 'role',
}
...
But when I specify the word ALL it doesn't work.
Here the error:
Error: /Stage[main]/Main/Node[default]/Postgresql::Server::Table_grant[PnBP]/Postgresql::Server::Grant[table:PnBP]/Postgresql_psql[grant:table:PnBP]: Could not evaluate: Error evaluating 'unless' clause, returned pid 30443 exit 1: 'ERROR: relation "all" does not exist
'
I check the doc, but it doesn't specify how to apply the privileges for all the tables.
table : Specifies the table to which you are granting access.
postgresql::server::table_grant

The Puppet documentation for Postgresql describes a postgresql::server::grant option that looks more flexible than the table_grant which assumes a single table.
Looks to be something like:
postgresql::server::grant{ 'SELECT':
object_type => 'ALL TABLES IN SCHEMA',
object_name => 'public',
privilege => 'SELECT',
db => 'db',
role => 'role',
}
https://github.com/puppetlabs/puppetlabs-postgresql#postgresqlservergrant

Related

Magento 2 - SQLSTATE[42S02]: Base table or view not found when running a reindex

I am getting the following error on terminal when running bin/magento indexer:reindex:
***SQLSTATE[42S02]: Base table or view not found: 1146 Table 'catalog_product_index_price_cfg_opt_temp' doesn't exist, query was: DESCRIBE `catalog_product_index_price_cfg_opt_temp.
I have checked the website database and can confirm the table does exist but with no data.
catalog_product_index_price_cfg_opt_tmp table
When I manually run the SQL query (DESCRIBE `catalog_product_index_price_cfg_opt_temp) it created data.
However, when I try re-running the bin/magento indexer:reindex command following this, it removed the data created when running the above SQL command and i still get the same below error on terminal
***SQLSTATE[42S02]: Base table or view not found: 1146 Table 'catalog_product_index_price_cfg_opt_temp' doesn't exist, query was: DESCRIBE `catalog_product_index_price_cfg_opt_temp.
Hopefully you can help me to resolve this. Thank you!
Check if you are setting the table prefix correct in app/etc/env.php
...
'db' => [
'table_prefix' => 'pref_',
'connection' => [
'default' => [
'host' => 'localhost',
'dbname' => 'magentodb',
'username' => 'user',
'password' => 'pass',
'model' => 'mysql5',
'engine' => 'innodb',
'initStatements' => 'SET NAMES utf8;',
'active' => '1',
]
]
],
...
And do bin/magento indexer:reset

TYPO3 v9: how to query additional external database (MSSQL)

I am trying to query an additional external database connection in one of my repositories. In LocalConfiguration.php I've defined two connections (Default, External).
[...]
'DB' => [
'Connections' => [
// Local MySQL database
'Default' => [
// ...
],
// External MSSQL database
'External' => [
'charset' => 'utf-8',
'dbname' => 'DBNAME',
'driver' => 'sqlsrv',
'host' => 'someExternalIP',
'password' => 'somePassword',
'port' => 1433,
'user' => 'someUser',
],
],
],
[...]
In my repository I want to query the external database (via Doctrine).
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable('dbo.SomeTable');
$queryBuilder->getRestrictions()->removeAll();
$queryBuilder
->select('*')
->from('dbo.SomeTable');
Do I have to explicitly tell the QueryBuilder to use that particular connection? Right now I am getting an Doctrine\DBAL\Exception\ConnectionException error, as the system tries to connect via the Default-Connection.
An exception occurred while executing 'SELECT * FROM `dbo`.`SomeTable`':
SELECT command denied to user 'myLocalUser'#'localhost' for table 'SomeTable'
Check out $GLOBALS['TYPO3_CONF_VARS']['DB']['TableMapping'] where you can explicitly define what tables are located in which database. See also this for some more details https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Database/Configuration/Index.html
The other option is actually to use ask the Connection by name, and create a query builder out of that.
GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionByName('External')->createQueryBuilder(...)
I personally would go with the latter, as it is more explicit within the actual callers code what is used.
To work with external DB, you have to :
configure the mapping with external database and table mapping in LocalConfiguration.php
define the TCA for external tables in myExt/Configuration/TCA/MyExternalTableName.php
configure the external tables/columns mapping in ext_typoscript_setup.txt
and then, the queries in repositories will work.
Sample LocalConfiguration.php :
'DB' => [
'Connections' => [
'Default' => [
'charset' => 'utf8',
'dbname' => 'LOCAL-DB',
'driver' => 'mysqli',
'host' => '127.0.0.1',
'password' => 'PWD',
'port' => 3306,
'user' => 'USER',
],
'externalDb' => [
'charset' => 'utf8',
'dbname' => 'EXTERNAL-DB',
'driver' => 'mysqli',
'host' => 'localhost',
'password' => 'PWD',
'port' => 3306,
'user' => 'USER',
],
],
'TableMapping' => [
'MyexternalTable1' => 'externalDb',
'MyexternalTable2' => 'externalDb',
...
]
]
Sample columns mapping in myExt/ext_typoscript_setup.txt :
plugin.tx_myext {
persistence {
classes {
Vendor\MyExt\Domain\Model\LocalModel {
mapping {
tableName = ExternalTableName
recordType = \Vendor\MyExt\Domain\Model\LocalModel
columns {
col1.mapOnProperty = uid
col2.mapOnProperty = name
...
}
}
}
}
}
}

How to grant a user a Mongodb database privilege with Puppet?

The user is created by the following Puppet resource:
mongodb::db { 'db1':
user => 'user1',
password => 'password',
roles => ["dbAdmin", "dbOwner"],
}
Recently we have added a role clusterMonitor#admin in the MongoDB database. The roles are like this:
["clusterMonitor#admin", "dbAdmin", "dbOwner"].
How can we do this in Puppet? Any alternative way to make it work is fine.
You add the user to the array in the roles attribute just like you wrote:
mongodb::db { 'db1':
user => 'user1',
password => 'password',
roles => ["clusterMonitor#admin", "dbAdmin", "dbOwner"],
}

PostgreSQL column does not exist when it does

I have the following PostgreSQL query...
WITH update_parts (id,id_finish)
AS (VALUES (1,42), (2,42), (3,30), (4,30))
UPDATE parts SET id = up.id, id_metal = up.id_metal
FROM update_parts up WHERE up.id = parts.id;
...and I get the error...
column up.id_metal does not exist LINE 1
I have verified I'm connected to the correct database, to the correct table and that the column exists. I'm only partially certain about permissions which I set via the following...
GRANT ALL(id_metal) ON parts TO public;
...and...
GRANT ALL(id_metal) ON parts TO postgres;
I plan to limit the permissions to SELECT, UPDATE, DELETE and INSERT after I've figured out the problem. For clarification it is the "parts" table and the "id_metal" column.
How do I resolve this error and UPDATE the rows?
After updating the permissions I'm not sure what else to consider?
I used the following query to get information about the column...
SELECT * FROM information_schema.columns
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
AND column_name='id_finish';
...which returns...
Array (
[table_catalog] => [database name]
[table_schema] => public
[table_name] => parts
[column_name] => id_metal
[ordinal_position] => 7
[column_default] =>
[is_nullable] => YES
[data_type] => bigint
[character_maximum_length] =>
[character_octet_length] =>
[numeric_precision] => 64
[numeric_precision_radix] => 2
[numeric_scale] => 0
[datetime_precision] =>
[interval_type] =>
[interval_precision] =>
[character_set_catalog] =>
[character_set_schema] =>
[character_set_name] =>
[collation_catalog] =>
[collation_schema] =>
[collation_name] =>
[domain_catalog] =>
[domain_schema] =>
[domain_name] =>
[udt_catalog] => [database name]
[udt_schema] => pg_catalog
[udt_name] => int8
[scope_catalog] =>
[scope_schema] =>
[scope_name] =>
[maximum_cardinality] =>
[dtd_identifier] => 7
[is_self_referencing] => NO
[is_identity] => NO
[identity_generation] =>
[identity_start] =>
[identity_increment] =>
[identity_maximum] =>
[identity_minimum] =>
[identity_cycle] =>
[is_generated] => NEVER
[generation_expression] =>
[is_updatable] => YES
)
You are going through a lot of hoops with permissions, but the problem is simple syntax:
WITH update_parts (id, id_finish) AS (
VALUES (1,42), (2,42), (3,30), (4,30)
)
UPDATE parts SET id_metal = up.id_finish
FROM update_parts up WHERE up.id = parts.id;
In your CTE you define the columns id and id_finish so your main query should refer to the same up.id_finish, not the id_metal from the parts table.
Further, it is usless to update parts.id with the value of update_parts.id because they are the same by definition: WHERE up.id = parts.id.

How to set table schema at table name with Cake?

PostgreSQL, Oracle and many other DBMS's use SCHEMA, so, the table name is
schema_name.table_name
But CakePHP manuals not say anithing about this. What about Model, View and Controller names in the CakePHP defaults? I can use a solution like prefix, that is, where the same schema name will be used at all database operations.
PS1: please not to be confused with method Modelschema, and questions about accessing this method.
PS2: the Bill's 2006 solution is not the better one, because is not updated (I am using CakePHP2) and is not a "official cakePHP solution".
PS3: database.php have some schema attribute? What the link to CakePHP documentation?
Good news for me, there are CakePHP 2.0 documentation about SQL-Schema... No other documentation or examples, but a starting point...
In CakePHP you must define more database config.
In CakePHP 2:
set the 'schema' param to your config
create new configs for all of your schema
use the right schema in your models
For example, database conf:
public $default = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'login' => 'my_db_user',
'password' => 'my_db_passw',
'database' => 'my_project_db',
'prefix' => '',
'encoding' => 'utf8',
'schema' => 'postgres'
);
public $other_schema = array(
'datasource' => 'Database/Postgres',
'persistent' => false,
'host' => 'localhost',
'login' => 'my_db_user',
'password' => 'my_db_passw',
'database' => 'my_project_db',
'prefix' => '',
'encoding' => 'utf8',
'schema' => 'other_schema'
);
If you want to use it in a model:
class AppModel extends AppModel {
public $useDbConfig = 'other_schema';
}
In CakePHP3 is the same way, just there the database is config/app.php and you must use
use Cake\Datasource\ConnectionManager;
$connection = ConnectionManager::get('default');