Compare commits

...

5 Commits

Author SHA1 Message Date
tracer 4199faecb4 initial commit 2022-10-07 19:16:26 +02:00
tracer 059e3934f9 adapted namespace 2022-10-07 12:56:28 +02:00
tracer f45dd8fbc1 moved namespace 2022-10-07 11:33:54 +02:00
tracer c1a54aa23e initial commit 2022-10-07 11:33:08 +02:00
tracer d2f733d8c2 added test support 2022-10-06 16:21:11 +02:00
5 changed files with 154 additions and 95 deletions

11
Jenkinsfile vendored Normal file
View File

@ -0,0 +1,11 @@
pipeline {
agent any
stages {
stage('Do nothing') {
steps {
sh '/bin/true'
}
}
}
}

View File

@ -13,39 +13,41 @@ use PDOException;
*/ */
class DatabaseConnection class DatabaseConnection
{ {
private PDO $dbConnection; private PDO $dbConnection;
const TABLE_PREFIX = ''; const TABLE_PREFIX = '';
const TABLE_DOMAINS = self::TABLE_PREFIX . "domains"; const TABLE_DOMAINS = self::TABLE_PREFIX . "domains";
const TABLE_NAMESERVERS = self::TABLE_PREFIX . "nameservers"; const TABLE_NAMESERVERS = self::TABLE_PREFIX . "nameservers";
const TABLE_PANELS = self::TABLE_PREFIX . "panels"; const TABLE_PANELS = self::TABLE_PREFIX . "panels";
const TABLE_APIKEYS = self::TABLE_PREFIX . "apikeys"; const TABLE_APIKEYS = self::TABLE_PREFIX . "apikeys";
const TABLE_DYNDNS = self::TABLE_PREFIX . "dyndns"; const TABLE_DYNDNS = self::TABLE_PREFIX . "dyndns";
public function __construct(private readonly ConfigController $configController) public function __construct(private readonly ConfigController $configController)
{ {
$dbHost = $this->configController->getConfig(configKey: 'dbHost'); $dbHost = $this->configController->getConfig(configKey: 'dbHost');
$dbPort = $this->configController->getConfig(configKey: 'dbPort'); $dbPort = $this->configController->getConfig(configKey: 'dbPort');
$dbDatabase = $this->configController->getConfig(configKey: 'dbDatabase'); $dbDatabase = $this->configController->getConfig(configKey: 'dbDatabase');
$dbUser = $this->configController->getConfig(configKey: 'dbUser'); $dbUser = $this->configController->getConfig(configKey: 'dbUser');
$dbPassword = $this->configController->getConfig(configKey: 'dbPassword'); $dbPassword = $this->configController->getConfig(configKey: 'dbPassword');
// TODO create config => encryption key
try { if (!$this->configController->getConfig(configKey: 'test')) {
$this->dbConnection = new PDO( // TODO create config => encryption key
dsn : "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase", try {
username: $dbUser, $this->dbConnection = new PDO(
password: $dbPassword dsn: "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",
); username: $dbUser,
$sql = "SHOW TABLES"; password: $dbPassword
$statement = $this->dbConnection->prepare(query: $sql); );
$statement->execute(); $sql = "SHOW TABLES";
$result = $statement->fetch(); $statement = $this->dbConnection->prepare(query: $sql);
if (empty($result)) { $statement->execute();
// ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`; $result = $statement->fetch();
echo 'Error: Cannot find tables.' . PHP_EOL; if (empty($result)) {
if (confirm(message: 'Should I try to create them?')) { // ALTER TABLE `domains` ADD `panel_id` INT NULL AFTER `id`;
$sql = " echo 'Error: Cannot find tables.' . PHP_EOL;
if (confirm(message: 'Should I try to create them?')) {
$sql = "
CREATE TABLE `apikeys` ( CREATE TABLE `apikeys` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
@ -53,20 +55,20 @@ class DatabaseConnection
`api_token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `api_token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
CREATE TABLE `domains` ( CREATE TABLE `domains` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`panel` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `panel` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
CREATE TABLE `nameservers` ( CREATE TABLE `nameservers` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
@ -75,10 +77,10 @@ class DatabaseConnection
`apikey` varbinary(255) DEFAULT NULL, `apikey` varbinary(255) DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
CREATE TABLE `panels` ( CREATE TABLE `panels` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
@ -87,10 +89,10 @@ class DatabaseConnection
`apikey` varbinary(255) DEFAULT NULL, `apikey` varbinary(255) DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
$sql = " $sql = "
CREATE TABLE `dyndns` ( CREATE TABLE `dyndns` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL, `name` VARCHAR(255) NOT NULL,
@ -99,47 +101,48 @@ class DatabaseConnection
`last_update` TIMESTAMP NOT NULL, `last_update` TIMESTAMP NOT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci"; ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
$statement = $this->dbConnection->prepare(query: $sql); $statement = $this->dbConnection->prepare(query: $sql);
$statement->execute(); $statement->execute();
echo 'Tables have been created.' . PHP_EOL; echo 'Tables have been created.' . PHP_EOL;
} }
exit(1); exit(1);
} }
} catch (PDOException $exception) { } catch (PDOException $exception) {
echo $exception->getMessage() . PHP_EOL; echo $exception->getMessage() . PHP_EOL;
echo 'Did you create the database and adjust the config file?' . PHP_EOL; echo 'Did you create the database and adjust the config file?' . PHP_EOL;
echo PHP_EOL . 'You can create database an user via a panel or manually in mysql shell:' . PHP_EOL; echo PHP_EOL . 'You can create database an user via a panel or manually in mysql shell:' . PHP_EOL;
$password = $this->generatePassword(); $password = $this->generatePassword();
echo 'Created an initial password: ' . $password . PHP_EOL; echo 'Created an initial password: ' . $password . PHP_EOL;
echo 'CREATE DATABASE bindAPI;' . PHP_EOL; echo 'CREATE DATABASE bindAPI;' . PHP_EOL;
echo "CREATE USER 'bindAPI'@'localhost' IDENTIFIED BY '$password';" . PHP_EOL; echo "CREATE USER 'bindAPI'@'localhost' IDENTIFIED BY '$password';" . PHP_EOL;
echo "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON bindAPI.* TO 'bindAPI'@'localhost';" . PHP_EOL; echo "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON bindAPI.* TO 'bindAPI'@'localhost';" . PHP_EOL;
echo 'There is no need to run FLUSH PRIVILEGES when using GRANT!' . PHP_EOL; echo 'There is no need to run FLUSH PRIVILEGES when using GRANT!' . PHP_EOL;
exit(1); exit(1);
} }
} }
}
/**
* @param int $length /**
* * @param int $length
* @return string *
*/ * @return string
function generatePassword(int $length = 8): string */
{ function generatePassword(int $length = 8): string
$chars = '23456789bcdfhkmnprstvzBCDFHJKLMNPRSTVZ'; {
$shuffled = str_shuffle(string: $chars); $chars = '23456789bcdfhkmnprstvzBCDFHJKLMNPRSTVZ';
return mb_substr(string: $shuffled, start: 0, length: $length); $shuffled = str_shuffle(string: $chars);
} return mb_substr(string: $shuffled, start: 0, length: $length);
}
/**
* @return \PDO /**
*/ * @return PDO
public function getConnection(): PDO */
{ public function getConnection(): PDO
return $this->dbConnection; {
} return $this->dbConnection;
}
} }

View File

@ -1,10 +1,12 @@
<?php <?php
namespace App\Controller; namespace Unit\Controller;
use App\Controller\DatabaseConnection;
use App\Repository\NameserverRepository; use App\Repository\NameserverRepository;
use DI\Container; use DI\Container;
use DI\ContainerBuilder; use DI\ContainerBuilder;
use Exception;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use function DI\autowire; use function DI\autowire;
@ -19,7 +21,7 @@ class BindApiTestController extends TestCase
/** /**
* @param int|string $dataName * @param int|string $dataName
* *
* @throws \Exception * @throws Exception
* @internal This method is not covered by the backward compatibility promise for PHPUnit * @internal This method is not covered by the backward compatibility promise for PHPUnit
*/ */
public function __construct(?string $name = null, array $data = [], $dataName = '') public function __construct(?string $name = null, array $data = [], $dataName = '')

View File

@ -0,0 +1,43 @@
<?php
namespace Unit\Controller;
use App\Controller\ConfigController;
use App\Controller\DatabaseConnection;
use PDO;
use PDOException;
/**
* @covers \App\Controller\DatabaseConnection
* @covers \App\Controller\ConfigController
*/
class DatabaseConnectionTest extends BindApiTestController
{
private PDO $dbConnection;
public function testGetConnection()
{
$configController = new ConfigController(test: true);
$dbHost = $configController->getConfig(configKey: 'dbHost');
$dbPort = $configController->getConfig(configKey: 'dbPort');
$dbDatabase = $configController->getConfig(configKey: 'dbDatabase');
$dbUser = $configController->getConfig(configKey: 'dbUser');
$dbPassword = $configController->getConfig(configKey: 'dbPassword');
try {
$this->dbConnection = new PDO(
dsn: "mysql:host=$dbHost;port=$dbPort;charset=utf8mb4;dbname=$dbDatabase",
username: $dbUser,
password: $dbPassword
);
} catch (PDOException $e) {
$this->fail(message: $e->getMessage());
}
$databaseConnection = new DatabaseConnection(configController: $configController);
self::assertEquals(expected: $databaseConnection->getConnection(), actual: $this->dbConnection);
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace App\Controller; namespace Unit\Controller;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;