Skip to content

Commit

Permalink
Add dry run mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
digitall-it committed Jul 22, 2019
1 parent 5f17c38 commit d58884e
Show file tree
Hide file tree
Showing 27 changed files with 183 additions and 98 deletions.
1 change: 1 addition & 0 deletions config.yaml.template
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dryrun: true
servers:
exam:
username: user_exam
Expand Down
61 changes: 58 additions & 3 deletions digitall/Epp.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,26 @@ class Epp
* @var string|null Client transaction ID
*/
private $clTRID;
/**
* @var string EPP client name
*/
private $name;
/**
* @var bool Dry run mode
*/
private $dryrun;

/**
* Class constructor
*
* @param $name string Name of the Epp instance for the logs
* @param $credentials array username, password and server
*/
public function __construct($name, $credentials)
public function __construct($name, $credentials, $dryrun = false)
{

$this->name = $name;
$this->dryrun = $dryrun;
//if($dryrun) file_put_contents(__dir__ . '/logs/epp/'. $this->name . '-dry-run.txt', '');
try {
$this->log = new Logger('log-' . $name);
//ErrorHandler::register($this->log);
Expand Down Expand Up @@ -151,6 +161,8 @@ public function hello(): string
$response = $this->send($this->render('hello'));
$this->log->info('hello sent');

if ($this->dryrun) return '';

if ($this->nodeExists($response->greeting)) {
$this->log->info('Received a greeting after hello');
$status = 'OK';
Expand All @@ -172,6 +184,13 @@ public function hello(): string
*/
private function send(string $xml, bool $debug = false): SimpleXMLElement
{
if ($this->dryrun) {
file_put_contents(__dir__ . '/logs/epp/' . $this->name . '-dry-run.txt', $xml .
"\n---------------------------------------------------------------------------------------------\n",
FILE_APPEND);

return new SimpleXMLElement('<xml></xml>');
}
$this->log->debug('Sending... ' . preg_replace('!\s+!', ' ', $xml));
file_put_contents(__dir__ . '/logs/epp/' . microtime(true) . '-send.xml', $xml);
try {
Expand Down Expand Up @@ -272,6 +291,8 @@ public function login($newpassword = null, $testpassword = null): array
$response = $this->send($xml);
$this->log->info('login sent');

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error("No response to login");
throw new RuntimeException('No response to login');
Expand Down Expand Up @@ -403,6 +424,8 @@ public function logout(): array
$response = $this->send($xml);
$this->log->info('logout sent');

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error("No response to logout");
throw new RuntimeException('No response to logout');
Expand Down Expand Up @@ -441,6 +464,8 @@ public function contactsCheck($contacts): array
$handles = rtrim($handles, ', ');
$this->log->info('contact check sent for ' . $handles);

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error("No response to logout");
throw new RuntimeException('No response to logout');
Expand Down Expand Up @@ -494,6 +519,8 @@ function contactCreate(array $contact): array

$this->log->info('contact create sent for "' . $contact['handle'] . '"');

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down Expand Up @@ -532,6 +559,8 @@ function contactUpdate(array $contact): array

$this->log->info('contact update sent for "' . $contact['handle'] . '"');

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down Expand Up @@ -571,6 +600,8 @@ function contactGetInfo(string $handle): array

$this->log->info('contact info sent for "' . $handle . '"');

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down Expand Up @@ -619,6 +650,8 @@ function domainsCheck($domains): array
$names = rtrim($names, ', ');
$this->log->info('domain check sent for ' . $names);

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down Expand Up @@ -686,6 +719,8 @@ function domainCreate(array $domain): array

$this->log->info('domain create sent for "' . $domain['name'] . '"');

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down Expand Up @@ -723,6 +758,8 @@ function domainUpdate(array $domain)

$this->log->info('domain update sent for "' . $domain['name'] . '"');

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down Expand Up @@ -763,6 +800,8 @@ function domainGetInfo(string $name, string $authInfo = null)

$this->log->info('domain info request sent for "' . $name . '"');

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down Expand Up @@ -802,6 +841,8 @@ function domainGetInfo(string $name, string $authInfo = null)
function pollCheck(): void
{

if ($this->dryrun) return;

$result = $this->pollRequest();

if ($result['status']['code'] == self::RESPONSE_COMPLETED_QUEUE_HAS_MESSAGES) {
Expand All @@ -824,6 +865,8 @@ public function pollRequest()

$this->log->info('Poll req request sent');

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down Expand Up @@ -881,6 +924,8 @@ public function pollAck(string $id): array

$this->log->info('Poll ack request sent');

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down Expand Up @@ -919,12 +964,15 @@ function domainTransfer(array $domain, string $op, $extension = null): array
'domain' => $domain,
'op' => $op
];
if ($$extension !== null) $vars['extension'] = $$extension;
if ($extension !== null) $vars['extension'] = $extension;

$xml = $this->render('domain-transfer',
$vars
);
$response = $this->send($xml);
$this->log->info('Domain transfer request sent, operation ' . $op);

if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
Expand Down Expand Up @@ -962,6 +1010,9 @@ function contactDelete(string $handle): array
$xml = $this->render('contact-delete', ['handle' => $handle]);
$response = $this->send($xml);

$this->log->info('Contact delete for ' . $handle . ' request sent.');
if ($this->dryrun) return [];

if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down Expand Up @@ -996,6 +1047,10 @@ function domainDelete(string $name): array
$xml = $this->render('domain-delete', ['name' => $name]);
$response = $this->send($xml);

$this->log->info('Domain delete for ' . $name . ' request sent.');
if ($this->dryrun) return [];


if (!$this->nodeExists($response->response)) {
$this->log->error('No response.');
throw new RuntimeException('No response.');
Expand Down
13 changes: 7 additions & 6 deletions exam.yaml.template
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
dryrun: true
servers:
exam-a:
username: DIGITALL-IT-REG
password: "05digitall-it19"
uri: https://epp-acc1.nic.it
username: USERNAME
password: PASSWORD
uri: "https://epp-acc1.nic.it"
exam-b:
username: DIGITALL-IT-REG
password: "05digitall-it19"
uri: https://epp-acc1.nic.it
username: USERNAME2
password: PASSWORD2
uri: "https://epp-acc1.nic.it"
samplecontacts:
AA10:
handle: "AA10"
Expand Down
7 changes: 4 additions & 3 deletions exam/test-01.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use Symfony\Component\Yaml\Yaml;

$cfg = Yaml::parseFile(__DIR__ . '/../exam.yaml');
$dryrun = $cfg['dryrun'];

$epp_a = new Epp("epp-a", $cfg["servers"]["exam-a"]);
$epp_b = new Epp("epp-b", $cfg["servers"]["exam-b"]);
$epp_a = new Epp('epp-a', $cfg['servers']['exam-a'], $dryrun);
$epp_b = new Epp('epp-b', $cfg['servers']['exam-b'], $dryrun);

echo 'Test 1 - Handshake:';

$return = $return = $epp_a->hello();
if ($return !== 'OK') die('FAILED');
if (!$dryrun && $return !== 'OK') die('FAILED');

echo "OK\n";

Expand Down
9 changes: 5 additions & 4 deletions exam/test-02.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
use Symfony\Component\Yaml\Yaml;

$cfg = Yaml::parseFile(__DIR__ . '/../exam.yaml');
$dryrun = $cfg['dryrun'];

$epp_a = new Epp("epp-a", $cfg["servers"]["exam-a"]);
$epp_b = new Epp("epp-b", $cfg["servers"]["exam-b"]);
$epp_a = new Epp('epp-a', $cfg['servers']['exam-a'], $dryrun);
$epp_b = new Epp('epp-b', $cfg['servers']['exam-b'], $dryrun);

echo 'Test 2 - Authentication (by opening one or more simultaneous sessions):';

$return = $epp_a->login();
if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK-";

$return = $epp_b->login();
if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK\n";

unset($epp_a);
Expand Down
9 changes: 5 additions & 4 deletions exam/test-03.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
use Symfony\Component\Yaml\Yaml;

$cfg = Yaml::parseFile(__DIR__ . '/../exam.yaml');
$dryrun = $cfg['dryrun'];

$epp_a = new Epp("epp-a", $cfg["servers"]["exam-a"]);
$epp_b = new Epp("epp-b", $cfg["servers"]["exam-b"]);
$epp_a = new Epp('epp-a', $cfg['servers']['exam-a'], $dryrun);
$epp_b = new Epp('epp-b', $cfg['servers']['exam-b'], $dryrun);

echo 'Test 3 - Change password:';

$return = $epp_b->logout();
if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK-";

$return = $epp_b->login('FoobarB');
if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK\n";

unset($epp_a);
Expand Down
7 changes: 4 additions & 3 deletions exam/test-04.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use Symfony\Component\Yaml\Yaml;

$cfg = Yaml::parseFile(__DIR__ . '/../exam.yaml');
$dryrun = $cfg['dryrun'];

$epp_a = new Epp("epp-a", $cfg["servers"]["exam-a"]);
$epp_b = new Epp("epp-b", $cfg["servers"]["exam-b"]);
$epp_a = new Epp('epp-a', $cfg['servers']['exam-a'], $dryrun);
$epp_b = new Epp('epp-b', $cfg['servers']['exam-b'], $dryrun);

echo 'Test 4 - Check the availability of contact identifiers to be used during the accreditation test:';

Expand All @@ -20,7 +21,7 @@
'IL10' => $cfg["samplecontacts"]['IL10']
]
);
if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK\n";

unset($epp_a);
Expand Down
11 changes: 6 additions & 5 deletions exam/test-05.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,23 @@
use Symfony\Component\Yaml\Yaml;

$cfg = Yaml::parseFile(__DIR__ . '/../exam.yaml');
$dryrun = $cfg['dryrun'];

$epp_a = new Epp("epp-a", $cfg["servers"]["exam-a"]);
$epp_b = new Epp("epp-b", $cfg["servers"]["exam-b"]);
$epp_a = new Epp('epp-a', $cfg['servers']['exam-a'], $dryrun);
$epp_b = new Epp('epp-b', $cfg['servers']['exam-b'], $dryrun);

echo 'Test 5 - Creation of three registrant contacts:';

$return = $epp_a->contactCreate($cfg["samplecontacts"]['AA10']);
if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK-";

$return = $epp_a->contactCreate($cfg["samplecontacts"]['BB10']);
if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK-";

$return = $epp_a->contactCreate($cfg["samplecontacts"]['IL10']);
if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK\n";

unset($epp_a);
Expand Down
9 changes: 5 additions & 4 deletions exam/test-06.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
use Symfony\Component\Yaml\Yaml;

$cfg = Yaml::parseFile(__DIR__ . '/../exam.yaml');
$dryrun = $cfg['dryrun'];

$epp_a = new Epp("epp-a", $cfg["servers"]["exam-a"]);
$epp_b = new Epp("epp-b", $cfg["servers"]["exam-b"]);
$epp_a = new Epp('epp-a', $cfg['servers']['exam-a'], $dryrun);
$epp_b = new Epp('epp-b', $cfg['servers']['exam-b'], $dryrun);

echo 'Test 6 - Creation of two tech / admin contacts:';

$return = $epp_a->contactCreate($cfg["samplecontacts"]['CC01']);
if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK-";

$return = $epp_a->contactCreate($cfg["samplecontacts"]['DD01']);
if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK\n";

unset($epp_a);
Expand Down
7 changes: 4 additions & 3 deletions exam/test-07.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
use Symfony\Component\Yaml\Yaml;

$cfg = Yaml::parseFile(__DIR__ . '/../exam.yaml');
$dryrun = $cfg['dryrun'];

$epp_a = new Epp("epp-a", $cfg["servers"]["exam-a"]);
$epp_b = new Epp("epp-b", $cfg["servers"]["exam-b"]);
$epp_a = new Epp('epp-a', $cfg['servers']['exam-a'], $dryrun);
$epp_b = new Epp('epp-b', $cfg['servers']['exam-b'], $dryrun);

echo 'Test 7 - Updating one of the properties of a contact:';

Expand All @@ -18,7 +19,7 @@
]
);

if ($return['status']['code'] != 1000) die('FAILED');
if (!$dryrun && $return['status']['code'] != 1000) die('FAILED');
echo "OK\n";

unset($epp_a);
Expand Down
Loading

0 comments on commit d58884e

Please sign in to comment.