Skip to content

Commit 35ccd8c

Browse files
committed
Remove vendor dependencies and cleanup
Signed-off-by: Jorge Boncompte <[email protected]>
1 parent ef18503 commit 35ccd8c

30 files changed

+70
-3363
lines changed

application/forms/Config/GeneralConfigForm.php

-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Icinga\Module\Grafana\Forms\Config;
44

55
use Icinga\Module\Grafana\Helpers\Timeranges;
6-
use Icinga\Module\Grafana\Helpers\JwtToken;
76
use Icinga\Forms\ConfigForm;
87

98
/**
@@ -380,15 +379,5 @@ public function createElements(array $formData)
380379
);
381380
}
382381
}
383-
384-
$this->addElement(
385-
'checkbox',
386-
'grafana_debug',
387-
array(
388-
'value'=> false,
389-
'label' => $this->translate('Show debug'),
390-
'description' => $this->translate('Show debuging information.'),
391-
)
392-
);
393382
}
394383
}

library/Grafana/Helpers/JwtToken.php

+67-23
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
namespace Icinga\Module\Grafana\Helpers;
44

5-
use Firebase\JWT\JWT;
6-
use Firebase\JWT\Key;
5+
use OpenSSLAsymmetricKey;
6+
use InvalidArgumentException;
7+
use RuntimeException;
78

8-
class JwtToken {
9+
class JwtToken
10+
{
911
const RSA_KEY_BITS = 2048;
1012
const JWT_PRIVATEKEY_FILE = '/etc/icingaweb2/modules/grafana/jwt.key.priv';
1113
const JWT_PUBLICKEY_FILE = '/etc/icingaweb2/modules/grafana/jwt.key.pub';
1214

13-
1415
/**
1516
* Create JWT Token
1617
*/
17-
public static function create(string $sub, int $exp = 0, string $iss = null, array $claims = null) : string {
18+
public static function create(string $sub, int $exp = 0, string $iss = null, array $claims = null): string
19+
{
1820
$privateKeyFile = JwtToken::JWT_PRIVATEKEY_FILE;
1921

2022
$privateKey = openssl_pkey_get_private(
@@ -27,38 +29,80 @@ public static function create(string $sub, int $exp = 0, string $iss = null, arr
2729
'nbf' => time(),
2830
];
2931

30-
if(isset($claims)) {
32+
if (isset($claims)) {
3133
$payload = array_merge($payload, $claims);
3234
}
3335

3436
if (!empty($iss)) {
3537
$payload['iss'] = $iss;
3638
}
37-
if ($exp > 0) {
38-
$payload['exp'] = $exp;
39-
}
4039

41-
return JWT::encode($payload, $privateKey, 'RS256');
40+
return JwtToken::encode($payload, $privateKey, 'RS256', $exp);
4241
}
4342

4443
/**
4544
* Generate Private and Public RSA Keys
4645
*/
4746
public static function generateRsaKeys()
4847
{
49-
if(!file_exists(JwtToken::JWT_PRIVATEKEY_FILE)) {
50-
$config = array(
51-
"private_key_bits" => JwtToken::RSA_KEY_BITS,
52-
"private_key_type" => OPENSSL_KEYTYPE_RSA,
53-
);
54-
55-
$res = openssl_pkey_new($config);
56-
openssl_pkey_export($res, $privKey);
57-
$pubKey = openssl_pkey_get_details($res);
58-
$pubKey = $pubKey["key"];
59-
60-
file_put_contents(JwtToken::JWT_PRIVATEKEY_FILE, $privKey);
61-
file_put_contents(JwtToken::JWT_PUBLICKEY_FILE, $pubKey);
48+
$ret = file_exists(JwtToken::JWT_PRIVATEKEY_FILE);
49+
if ($ret) {
50+
return;
51+
}
52+
53+
$config = array(
54+
"private_key_bits" => JwtToken::RSA_KEY_BITS,
55+
"private_key_type" => OPENSSL_KEYTYPE_RSA,
56+
);
57+
58+
$res = openssl_pkey_new($config);
59+
openssl_pkey_export($res, $privKey);
60+
$pubKey = openssl_pkey_get_details($res);
61+
$pubKey = $pubKey["key"];
62+
63+
file_put_contents(JwtToken::JWT_PRIVATEKEY_FILE, $privKey);
64+
file_put_contents(JwtToken::JWT_PUBLICKEY_FILE, $pubKey);
65+
}
66+
67+
private static function encode(array $payload, OpenSSLAsymmetricKey $privateKey, string $algorithm = 'RS256', int $expiration = 3600): string
68+
{
69+
// Verify that the algorithm is compatible with asymmetric keys
70+
if ($algorithm !== 'RS256' && $algorithm !== 'RS512') {
71+
throw new InvalidArgumentException("Unsupported algorithm for assymmetric keys: $algorithm");
6272
}
73+
74+
// Define the JWT header
75+
$header = json_encode([
76+
'alg' => $algorithm,
77+
'typ' => 'JWT'
78+
]);
79+
80+
// Add expiration time to the payload
81+
if ($expiration > 0) {
82+
$payload['exp'] = time() + $expiration;
83+
}
84+
85+
// Encode header and payload to base64 URL
86+
$base64Header = JwtToken::base64UrlEncode($header);
87+
$base64Payload = JwtToken::base64UrlEncode(json_encode($payload));
88+
89+
// Create the signature
90+
$dataToSign = "$base64Header.$base64Payload";
91+
$signature = '';
92+
$success = openssl_sign($dataToSign, $signature, $privateKey, OPENSSL_ALGO_SHA256);
93+
if (!$success) {
94+
throw new RuntimeException("Failed to sign the JWT with the private key.");
95+
}
96+
97+
// Encode signature to base64 URL
98+
$base64Signature = JwtToken::base64UrlEncode($signature);
99+
100+
// Return the complete token
101+
return "$base64Header.$base64Payload.$base64Signature";
102+
}
103+
104+
private static function base64UrlEncode(string $data): string
105+
{
106+
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
63107
}
64108
}

library/Grafana/ProvidedHook/Icingadb/GeneralConfigFormHook.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function appliesTo(Form $form)
1717

1818
public function onSuccess(Form $form)
1919
{
20-
if($form->getElement('grafana_jwtEnable')->getValue()) {
20+
if ($form->getElement('grafana_jwtEnable')->getValue()) {
2121
JwtToken::generateRsaKeys();
2222
}
2323
}

library/Grafana/ProvidedHook/Icingadb/IcingaDbGrapher.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ private function getMyPreviewHtml($serviceName, $hostName, HtmlDocument $preview
310310
urlencode($this->timerangeto)
311311
);
312312

313-
if($this->jwtEnable) {
314-
$authToken = JwtToken::create($this->jwtUser, time()+$this->jwtExpires, !empty($this->jwtIssuer)?$this->jwtIssuer:null, [ 'roles' => [ 'Viewer' ] ]);
313+
if ($this->jwtEnable) {
314+
$authToken = JwtToken::create($this->jwtUser, $this->jwtExpires, !empty($this->jwtIssuer) ? $this->jwtIssuer:null, [ 'roles' => [ 'Viewer' ] ]);
315315
$iFramesrc .= sprintf("&auth_token=%s", urlencode($authToken));
316316
}
317317

run.php

-2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,3 @@
88
$this->provideHook('icingadb/HostDetailExtension');
99
$this->provideHook('icingadb/ServiceDetailExtension');
1010
$this->provideHook('ConfigFormEvents', GeneralConfigFormHook::class);
11-
12-
require_once __DIR__ . '/vendor/autoload.php';

vendor/autoload.php

-25
This file was deleted.

vendor/composer.json

-5
This file was deleted.

vendor/composer.lock

-82
This file was deleted.

0 commit comments

Comments
 (0)