From a02547a11320fe352f210f2a92972c0d53b230bc Mon Sep 17 00:00:00 2001 From: Mark Unwin Date: Wed, 17 Apr 2024 11:31:28 +1000 Subject: [PATCH] Use consistent function calls for simpleEncrypt and simpleDecrypt in code. --- app/Controllers/Cli.php | 9 +++------ app/Models/CloudsModel.php | 10 ++++------ app/Models/ComponentsModel.php | 4 ++-- app/Models/CredentialsModel.php | 10 ++++------ app/Models/DatabaseModel.php | 3 +-- app/Models/LogonModel.php | 2 +- 6 files changed, 15 insertions(+), 23 deletions(-) diff --git a/app/Controllers/Cli.php b/app/Controllers/Cli.php index 961a2fcd5..7b38c5cab 100644 --- a/app/Controllers/Cli.php +++ b/app/Controllers/Cli.php @@ -272,8 +272,7 @@ public function amazon($cloud) helper('security'); helper('network'); $db = db_connect(); - $cloud->credentials = simpleDecrypt($cloud->credentials, config('Encryption')->key); - $cloud->credentials = json_decode($cloud->credentials); + $cloud->credentials = json_decode(simpleDecrypt($cloud->credentials, config('Encryption')->key)); $response = new stdClass(); $projects = array(); @@ -567,8 +566,7 @@ public function microsoft($cloud) helper('security'); helper('network'); $db = db_connect(); - $cloud->credentials = simpleDecrypt($cloud->credentials, config('Encryption')->key); - $cloud->credentials = json_decode($cloud->credentials); + $cloud->credentials = json_decode(simpleDecrypt($cloud->credentials, config('Encryption')->key)); $projects = array(); $projects[0] = new stdClass(); @@ -796,8 +794,7 @@ public function google($cloud) helper('security'); helper('network'); $db = db_connect(); - $cloud->credentials = simpleDecrypt($cloud->credentials, config('Encryption')->key); - $cloud->credentials = json_decode($cloud->credentials, true); + $cloud->credentials = json_decode(simpleDecrypt($cloud->credentials, config('Encryption')->key), true); $jsonKey = json_decode($cloud->credentials['json'], true); diff --git a/app/Models/CloudsModel.php b/app/Models/CloudsModel.php index 4ee79c0cd..8fc2bc521 100644 --- a/app/Models/CloudsModel.php +++ b/app/Models/CloudsModel.php @@ -51,8 +51,7 @@ public function collection(object $resp): array $count = count($query); for ($i=0; $i < $count; $i++) { if (!empty($query[$i]->credentials)) { - $query[$i]->credentials = simpleDecrypt($query[$i]->credentials, config('Encryption')->key); - $query[$i]->credentials = json_decode($query[$i]->credentials); + $query[$i]->credentials = json_decode(simpleDecrypt($query[$i]->credentials, config('Encryption')->key)); } } } @@ -72,9 +71,9 @@ public function create($data = null): ?int return null; } if (! empty($data->credentials) && is_string($data->credentials)) { - $data->credentials = simpleEncrypt($data->credentials, config('Encryption')->key); + $data->credentials = (string)simpleEncrypt($data->credentials, config('Encryption')->key); } else { - $data->credentials = simpleEncrypt(json_encode($data->credentials), config('Encryption')->key); + $data->credentials = (string)simpleEncrypt(json_encode($data->credentials), config('Encryption')->key); } if (! empty($data->options)) { $data->options = json_encode($data->options); @@ -320,8 +319,7 @@ public function read(int $id = 0): array } if ($config->decrypt_credentials === 'y') { if (!empty($cloud[0]->credentials)) { - $cloud[0]->credentials = simpleDecrypt($cloud[0]->credentials, config('Encryption')->key); - $cloud[0]->credentials = json_decode($cloud[0]->credentials); + $cloud[0]->credentials = json_decode(simpleDecrypt($cloud[0]->credentials, config('Encryption')->key)); } } return format_data($cloud, 'clouds'); diff --git a/app/Models/ComponentsModel.php b/app/Models/ComponentsModel.php index 4d28d6a52..adfa59891 100644 --- a/app/Models/ComponentsModel.php +++ b/app/Models/ComponentsModel.php @@ -180,9 +180,9 @@ public function create($data = null): ?int $this->db->query($sql, [intval($id), (string)$data->type]); if (!empty($data->credentials) && is_string($data->credentials)) { - $data->credentials = simpleEncrypt($data->credentials, config('Encryption')->key); + $data->credentials = (string)simpleEncrypt($data->credentials, config('Encryption')->key); } else { - $data->credentials = simpleEncrypt(json_encode($data->credentials), config('Encryption')->key); + $data->credentials = (string)simpleEncrypt(json_encode($data->credentials), config('Encryption')->key); } $sql = "INSERT INTO `credential` VALUES (null, ?, 'y', ?, ?, ?, ?, ?, NOW())"; $query = $this->db->query($sql, [$id, $data->name, $data->description, $data->type, $data->credentials, $instance->user->full_name]); diff --git a/app/Models/CredentialsModel.php b/app/Models/CredentialsModel.php index 12d7dab9c..169c64f74 100644 --- a/app/Models/CredentialsModel.php +++ b/app/Models/CredentialsModel.php @@ -51,8 +51,7 @@ public function collection(object $resp): array $count = count($query); for ($i=0; $i < $count; $i++) { if (!empty($query[$i]->credentials)) { - $query[$i]->credentials = simpleDecrypt($query[$i]->credentials, config('Encryption')->key); - $query[$i]->credentials = json_decode($query[$i]->credentials); + $query[$i]->credentials = json_decode(simpleDecrypt($query[$i]->credentials, config('Encryption')->key)); } } } @@ -72,9 +71,9 @@ public function create($data = null): ?int return null; } if (! empty($data->credentials) && is_string($data->credentials)) { - $data->credentials = simpleEncrypt($data->credentials, config('Encryption')->key); + $data->credentials = (string)simpleEncrypt($data->credentials, config('Encryption')->key); } else { - $data->credentials = simpleEncrypt(json_encode($data->credentials), config('Encryption')->key); + $data->credentials = (string)simpleEncrypt(json_encode($data->credentials), config('Encryption')->key); } $data = $this->createFieldData('credentials', $data); if (empty($data)) { @@ -190,8 +189,7 @@ public function read(int $id = 0): array $credentials = $query->getResult(); if ($config->decrypt_credentials === 'y') { if (!empty($credentials[0]->credentials)) { - $credentials[0]->credentials = simpleDecrypt($credentials[0]->credentials, config('Encryption')->key); - $credentials[0]->credentials = json_decode($credentials[0]->credentials); + $credentials[0]->credentials = json_decode(simpleDecrypt($credentials[0]->credentials, config('Encryption')->key)); } } return format_data($credentials, 'credentials'); diff --git a/app/Models/DatabaseModel.php b/app/Models/DatabaseModel.php index 21bc9ac28..add637ee0 100644 --- a/app/Models/DatabaseModel.php +++ b/app/Models/DatabaseModel.php @@ -100,8 +100,7 @@ public function export(string $table = ''): array if ($table === 'clouds' or $table === 'credential' or $table === 'credentials') { if ($instance->config->decrypt_credentials === 'y') { for ($i=0; $i < $count; $i++) { - $result[$i]->credentials = simpleDecrypt($result[$i]->credentials, config('Encryption')->key); - $result[$i]->credentials = json_decode($result[$i]->credentials); + $result[$i]->credentials = json_decode(simpleDecrypt($result[$i]->credentials, config('Encryption')->key)); } } } diff --git a/app/Models/LogonModel.php b/app/Models/LogonModel.php index f81b2d92a..8a16869f5 100644 --- a/app/Models/LogonModel.php +++ b/app/Models/LogonModel.php @@ -113,7 +113,7 @@ public function logon($username = '', $password = '') } else { log_message('debug', 'Successful bind using credentials for LDAP server at ' . $ldap_connect_string . ' : ' . $bind_string); } - $ldap->dn_password = (string)simpleDecrypt($ldap->dn_password); + $ldap->dn_password = (string)simpleDecrypt($ldap->dn_password, config('Encryption')->key); if (!empty($ldap->dn_account) && empty($ldap->dn_password)) { $message = 'DN Account set, but no DN Password.'; // \Config\Services::session()->setFlashdata('warning', $message);