diff --git a/seed/mariadb/templates/mariadb.sql b/seed/mariadb/templates/mariadb.sql index d77a973..7c3f065 100644 --- a/seed/mariadb/templates/mariadb.sql +++ b/seed/mariadb/templates/mariadb.sql @@ -10,4 +10,3 @@ CREATE DATABASE IF NOT EXISTS %%name CHARACTER SET utf8; GRANT ALL PRIVILEGES ON %%name.* TO '%%name'@'%%server' IDENTIFIED BY '%%password'; %end for FLUSH PRIVILEGES; - diff --git a/seed/postgresql/dictionaries/22_postgresql.xml b/seed/postgresql/dictionaries/22_postgresql.xml index 4cb925f..40e9bf8 100644 --- a/seed/postgresql/dictionaries/22_postgresql.xml +++ b/seed/postgresql/dictionaries/22_postgresql.xml @@ -13,6 +13,7 @@ /etc/pki/ca-trust/source/anchors/ca_PostgreSQL.crt /etc/pki/tls/certs/postgresql.crt /etc/pki/tls/private/postgresql.key + /tests/postgresql.yml diff --git a/seed/postgresql/templates/pg_hba.conf b/seed/postgresql/templates/pg_hba.conf index 7a48fb3..8987c7c 100644 --- a/seed/postgresql/templates/pg_hba.conf +++ b/seed/postgresql/templates/pg_hba.conf @@ -88,6 +88,7 @@ local all postgres ident map=pg_map # IPv4 local connections: #>GNUNUX # host all all 127.0.0.1/32 ident +hostssl rougail_test rougail_test %%gateway_eth0/32 md5 %for %%server in %%accounts.remotes hostssl %%normalize_family(%%server) %%normalize_family(%%server) %%server md5 %end for diff --git a/seed/postgresql/templates/postgresql.sql b/seed/postgresql/templates/postgresql.sql index 7f3892a..bb53c95 100644 --- a/seed/postgresql/templates/postgresql.sql +++ b/seed/postgresql/templates/postgresql.sql @@ -1,7 +1,12 @@ +%set %%new_accounts = [('rougail_test', %%get_password(server_name=%%domain_name_eth0, username='rougail_test', description="remote", type="cleartext", hide=%%hide_secret, temporary=True))] %for %%server in %%accounts.remotes %set %%name = %%normalize_family(%%server) + %set %%password = %%accounts["remote_" + %%name]["password_" + %%name] +%%new_accounts.append((%%name, %%password))%slurp +%end for +%for %%name, %%password in %%new_accounts CREATE DATABASE "%%name"; -CREATE ROLE "%%name" WITH LOGIN ENCRYPTED PASSWORD '%%accounts["remote_" + %%name]["password_" + %%name]'; -ALTER USER "%%name" PASSWORD '%%accounts["remote_" + %%name]["password_" + %%name]'; +CREATE ROLE "%%name" WITH LOGIN ENCRYPTED PASSWORD '%%password'; +ALTER USER "%%name" PASSWORD '%%password'; GRANT ALL PRIVILEGES ON DATABASE "%%name" TO "%%name"; %end for diff --git a/seed/postgresql/templates/postgresql.yml b/seed/postgresql/templates/postgresql.yml new file mode 100644 index 0000000..471b4cd --- /dev/null +++ b/seed/postgresql/templates/postgresql.yml @@ -0,0 +1,4 @@ +address: %%ip_eth0 +user: rougail_test +password: %%get_password(server_name=%%domain_name_eth0, username='rougail_test', description="remote", type="cleartext", hide=%%hide_secret, temporary=True) +dbname: rougail_test diff --git a/seed/postgresql/tests/test_postgresql.py b/seed/postgresql/tests/test_postgresql.py new file mode 100644 index 0000000..c89c2fc --- /dev/null +++ b/seed/postgresql/tests/test_postgresql.py @@ -0,0 +1,79 @@ +from yaml import load, SafeLoader +from os import environ +from pytest import raises + +from psycopg2 import connect, OperationalError + + +def test_postgresql_wrong_password(): + conf_file = f'{environ["MACHINE_TEST_DIR"]}/postgresql.yml' + with open(conf_file) as yaml: + data = load(yaml, Loader=SafeLoader) + with raises(OperationalError): + connect(host=data['address'], user=data['user'], password='a', database=data['dbname']) + + +def test_postgresql_connection(): + conf_file = f'{environ["MACHINE_TEST_DIR"]}/postgresql.yml' + with open(conf_file) as yaml: + data = load(yaml, Loader=SafeLoader) + db = connect(host=data['address'], user=data['user'], password=data['password'], database=data['dbname']) + db.close() + + +def test_postgresql_migration(): + conf_file = f'{environ["MACHINE_TEST_DIR"]}/postgresql.yml' + with open(conf_file) as yaml: + data = load(yaml, Loader=SafeLoader) + db = connect(host=data['address'], user=data['user'], password=data['password'], database=data['dbname']) + cursor = db.cursor() + if 'FIRST_RUN' in environ: + sql = """CREATE TABLE test (col CHAR(20) NOT NULL)""" + cursor.execute(sql) + sql = """INSERT INTO test (col) VALUES ('test')""" + cursor.execute(sql) + db.commit() + sql = """SELECT * FROM test""" + cursor.execute(sql) + results = cursor.fetchall() + assert len(results) == 1 + results[0] == ('test',) + cursor.close() + db.close() + + +def test_postgresql_insert(): + conf_file = f'{environ["MACHINE_TEST_DIR"]}/postgresql.yml' + with open(conf_file) as yaml: + data = load(yaml, Loader=SafeLoader) + db = connect(host=data['address'], user=data['user'], password=data['password'], database=data['dbname']) + cursor = db.cursor() + sql = """INSERT INTO test (col) VALUES ('test2')""" + cursor.execute(sql) + db.commit() + # + sql = """SELECT * FROM test WHERE col = 'test2'""" + cursor.execute(sql) + results = cursor.fetchall() + assert len(results) == 1 + results[0] == ('test2',) + cursor.close() + db.close() + + +def test_postgresql_delete(): + conf_file = f'{environ["MACHINE_TEST_DIR"]}/postgresql.yml' + with open(conf_file) as yaml: + data = load(yaml, Loader=SafeLoader) + db = connect(host=data['address'], user=data['user'], password=data['password'], database=data['dbname']) + cursor = db.cursor() + sql = """DELETE FROM test WHERE col = 'test2'""" + cursor.execute(sql) + db.commit() + # + sql = """SELECT * FROM test WHERE col = 'test2'""" + cursor.execute(sql) + results = cursor.fetchall() + assert len(results) == 0 + cursor.close() + db.close()