Skip to content

Commit f4f2687

Browse files
Merge pull request #67 from paragonie/public-replicas
Public replicas
2 parents f6d8bed + 0fc36c9 commit f4f2687

22 files changed

+395
-42
lines changed

bin/add-mirror.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use GetOpt\{
5+
GetOpt,
6+
Option
7+
};
8+
use ParagonIE\EasyDB\Factory;
9+
use ParagonIE\Chronicle\Chronicle;
10+
use ParagonIE\Chronicle\Exception\InstanceNotFoundException;
11+
12+
$root = \dirname(__DIR__);
13+
/** @psalm-suppress UnresolvableInclude */
14+
require_once $root . '/cli-autoload.php';
15+
16+
if (!\is_readable($root . '/local/settings.json')) {
17+
echo 'Settings are not loaded.', PHP_EOL;
18+
exit(1);
19+
}
20+
21+
/** @var array $settings */
22+
$settings = \json_decode(
23+
(string) \file_get_contents($root . '/local/settings.json'),
24+
true
25+
);
26+
27+
$db = Factory::create(
28+
$settings['database']['dsn'],
29+
$settings['database']['username'] ?? '',
30+
$settings['database']['password'] ?? '',
31+
$settings['database']['options'] ?? []
32+
);
33+
34+
// Pass database instance to Chronicle
35+
Chronicle::setDatabase($db);
36+
37+
/**
38+
* This defines the Command Line options.
39+
*
40+
* These two are equivalent:
41+
* php add-mirror.php -u chronicle.example.com -p foo
42+
* php add-mirror.php --url chronicle.example.com --publickey=foo
43+
*/
44+
$getopt = new Getopt([
45+
new Option('p', 'publickey', Getopt::REQUIRED_ARGUMENT),
46+
new Option('u', 'url', Getopt::REQUIRED_ARGUMENT),
47+
new Option('c', 'comment', Getopt::OPTIONAL_ARGUMENT),
48+
new Option('s', 'sort', Getopt::OPTIONAL_ARGUMENT),
49+
new Option('i', 'instance', Getopt::OPTIONAL_ARGUMENT),
50+
]);
51+
$getopt->process();
52+
53+
/** @var string $url */
54+
$url = $getopt->getOption('url');
55+
/** @var string $publicKey */
56+
$publicKey = $getopt->getOption('publickey');
57+
/** @var string|null $comment */
58+
$comment = $getopt->getOption('comment');
59+
$sort = (int) ($getopt->getOption('sort') ?? 0);
60+
$instance = (string) $getopt->getOption('instance');
61+
62+
try {
63+
if (!empty($instance)) {
64+
if (!\array_key_exists($instance, $settings['instances'])) {
65+
throw new InstanceNotFoundException(
66+
'Instance ' . $instance . ' not found'
67+
);
68+
}
69+
Chronicle::setTablePrefix($settings['instances'][$instance]);
70+
}
71+
} catch (InstanceNotFoundException $ex) {
72+
echo $ex->getMessage(), PHP_EOL;
73+
exit(1);
74+
}
75+
76+
if (empty($publicKey)) {
77+
echo 'Usage:', PHP_EOL, "\t",
78+
'php add-mirror.php -u url -p publickeygoeshere [-c comment]', PHP_EOL, PHP_EOL;
79+
exit(1);
80+
}
81+
82+
$isSQLite = strpos($settings['database']['dsn'] ?? '', 'sqlite:') !== false;
83+
84+
$db->beginTransaction();
85+
$db->insert(
86+
Chronicle::getTableNameUnquoted('mirrors', $isSQLite),
87+
[
88+
'url' => $url,
89+
'publickey' => $publicKey,
90+
'comment' => $comment,
91+
'sortpriority' => $sort
92+
]
93+
);
94+
if (!$db->commit()) {
95+
$db->rollBack();
96+
/** @var array<int, string> $errorInfo */
97+
$errorInfo = $db->errorInfo();
98+
echo $errorInfo[0], PHP_EOL;
99+
exit(1);
100+
}
101+
echo 'Mirror added successfully!', PHP_EOL;

bin/allow-null-prevhash.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
(string) \file_get_contents($root . '/local/settings.json'),
2626
true
2727
);
28-
/** @var EasyDB $db */
2928
$db = Factory::create(
3029
$settings['database']['dsn'],
3130
$settings['database']['username'] ?? '',

bin/change-replica-source-publickey.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,8 @@
1313
EasyDB,
1414
Factory
1515
};
16-
use GuzzleHttp\Client;
1716
use ParagonIE\Chronicle\Chronicle;
1817
use ParagonIE\Chronicle\Exception\InstanceNotFoundException;
19-
use ParagonIE\ConstantTime\Base64UrlSafe;
20-
use ParagonIE\Sapient\CryptographyKeys\SigningPublicKey;
2118

2219
$root = \dirname(__DIR__);
2320
/** @psalm-suppress UnresolvableInclude */
@@ -43,8 +40,6 @@
4340
Chronicle::setDatabase($db);
4441

4542
/**
46-
* @var Getopt
47-
*
4843
* This defines the Command Line options.
4944
*/
5045
$getopt = new GetOpt([

bin/create-client.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
*
4747
* These two are equivalent:
4848
* php create-client.php -p foo
49-
* php create-client php --public=key=foo
49+
* php create-client.php --publickey=foo
5050
*/
5151
$getopt = new Getopt([
5252
new Option('p', 'publickey', Getopt::REQUIRED_ARGUMENT),

bin/cross-sign.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
);
4343

4444
/**
45-
* @var GetOpt $getopt
46-
*
4745
* This defines the Command Line options.
4846
*/
4947
$getopt = new GetOpt([
@@ -113,7 +111,6 @@
113111
}
114112
if (is_string($publicKey)) {
115113
try {
116-
/** @var SigningPublicKey $publicKeyObj */
117114
$publicKeyObj = new SigningPublicKey(
118115
Base64UrlSafe::decode($publicKey)
119116
);
@@ -173,7 +170,6 @@
173170

174171
// Write to database...
175172
$db->beginTransaction();
176-
/** @var string $table */
177173
$table = Chronicle::getTableName('xsign_targets');
178174
if ($db->exists('SELECT * FROM ' . $table . ' WHERE name = ?', $name)) {
179175
// Update an existing cross-sign target

bin/fix-nulls.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
use ParagonIE\EasyDB\{
5-
EasyDB,
6-
Factory
7-
};
4+
use ParagonIE\EasyDB\Factory;
85

96
$root = \dirname(__DIR__);
107
/** @psalm-suppress UnresolvableInclude */
@@ -21,7 +18,6 @@
2118
true
2219
);
2320

24-
/** @var EasyDB $db */
2521
$db = Factory::create(
2622
$settings['database']['dsn'],
2723
$settings['database']['username'] ?? '',

bin/install.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
};
88
use ParagonIE\Sapient\CryptographyKeys\SigningSecretKey;
99

10-
/** @var string $root */
1110
$root = \dirname(__DIR__);
1211
/** @psalm-suppress UnresolvableInclude */
1312
require_once $root . '/cli-autoload.php';
1413

1514
// Generate a signing key.
16-
/** @var SigningSecretKey $signingKey */
1715
$signingKey = SigningSecretKey::generate();
1816

1917
// Store the signing key:
@@ -23,8 +21,6 @@
2321
);
2422

2523
/**
26-
* @var Getopt $getopt
27-
*
2824
* This defines the Command Line options.
2925
*
3026
* These are many examples:

bin/keygen.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
require_once $root . '/cli-autoload.php';
99

1010
/* This generates a new secret key from your kernel's CSPRNG */
11-
/** @var SigningSecretKey $signingSecretKey */
1211
$signingSecretKey = SigningSecretKey::generate();
1312

14-
echo (string) json_encode(
13+
echo json_encode(
1514
[
1615
'secret-key' => $signingSecretKey->getString(),
1716
'public-key' => $signingSecretKey->getPublicKey()->getString()

bin/list-mirrors.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use GetOpt\{
5+
GetOpt,
6+
Option
7+
};
8+
use ParagonIE\EasyDB\Factory;
9+
use ParagonIE\Chronicle\Chronicle;
10+
use ParagonIE\Chronicle\Exception\InstanceNotFoundException;
11+
12+
$root = \dirname(__DIR__);
13+
/** @psalm-suppress UnresolvableInclude */
14+
require_once $root . '/cli-autoload.php';
15+
16+
if (!\is_readable($root . '/local/settings.json')) {
17+
echo 'Settings are not loaded.', PHP_EOL;
18+
exit(1);
19+
}
20+
21+
/** @var array $settings */
22+
$settings = \json_decode(
23+
(string) \file_get_contents($root . '/local/settings.json'),
24+
true
25+
);
26+
27+
$db = Factory::create(
28+
$settings['database']['dsn'],
29+
$settings['database']['username'] ?? '',
30+
$settings['database']['password'] ?? '',
31+
$settings['database']['options'] ?? []
32+
);
33+
34+
// Pass database instance to Chronicle
35+
Chronicle::setDatabase($db);
36+
37+
/**
38+
* This defines the Command Line options.
39+
*
40+
* The only parameter supported is the instance.
41+
*/
42+
$getopt = new Getopt([
43+
new Option('i', 'instance', Getopt::OPTIONAL_ARGUMENT),
44+
]);
45+
$getopt->process();
46+
47+
$instance = (string) $getopt->getOption('instance');
48+
try {
49+
if (!empty($instance)) {
50+
if (!\array_key_exists($instance, $settings['instances'])) {
51+
throw new InstanceNotFoundException(
52+
'Instance ' . $instance . ' not found'
53+
);
54+
}
55+
Chronicle::setTablePrefix($settings['instances'][$instance]);
56+
}
57+
} catch (InstanceNotFoundException $ex) {
58+
echo $ex->getMessage(), PHP_EOL;
59+
exit(1);
60+
}
61+
62+
$isSQLite = strpos($settings['database']['dsn'] ?? '', 'sqlite:') !== false;
63+
64+
$db->beginTransaction();
65+
/** @var array<array<string, string|int|bool>> $mirrors */
66+
$mirrors = $db->run(
67+
"SELECT * FROM " .
68+
Chronicle::getTableNameUnquoted('clients', $isSQLite) .
69+
" ORDER BY sortpriority ASC"
70+
);
71+
72+
echo json_encode([
73+
'count' => count($mirrors),
74+
'mirrors' => $mirrors
75+
], JSON_PRETTY_PRINT), PHP_EOL;
76+
exit(0);

bin/make-tables.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
GetOpt,
66
Option
77
};
8+
use Slim\App;
89
use ParagonIE\Chronicle\Chronicle;
910
use ParagonIE\Chronicle\Exception\InstanceNotFoundException;
1011

@@ -16,9 +17,8 @@
1617

1718
/**
1819
* @var array $settings
19-
* @var \Slim\App $app
2020
*/
21-
$app = new \Slim\App($settings);
21+
$app = new App($settings);
2222

2323
if (!isset($app)) {
2424
throw new Error('Variable $app is not defined');
@@ -54,7 +54,6 @@
5454
exit(1);
5555
}
5656

57-
/** @var \ParagonIE\EasyDB\EasyDB $db */
5857
$db = ParagonIE\EasyDB\Factory::create(
5958
$settings['database']['dsn'],
6059
$settings['database']['username'] ?? null,

0 commit comments

Comments
 (0)