Skip to content

Commit

Permalink
In integrations controller, use the config encryption key when decryp…
Browse files Browse the repository at this point in the history
…ting credentials.
  • Loading branch information
mark-unwin committed Apr 17, 2024
1 parent e98b5f3 commit bc3d034
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions app/Models/IntegrationsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -81,7 +80,7 @@ public function create(object $data = null): ?int
}

if (empty($data->type)) {
$data->type = '';
$data->type = 'nmis';
}

if (is_array($data->fields) or is_object($data->fields)) {
Expand Down Expand Up @@ -1013,11 +1012,10 @@ public function getLocalDevices($integration)
$device->credentials = new \stdClass();
foreach ($credentials as $credential) {
if (is_array($retrieved_credentials)) {
foreach ($retrieved_credentials as $key => $value) {
foreach ($retrieved_credentials as $value) {
if (intval($value) === intval($credential->id) && ! empty($credential->credentials)) {
if (is_string($credential->credentials)) {
$credential->credentials = simpleDecrypt($credential->credentials);
$credential->credentials = json_decode($credential->credentials);
$credential->credentials = json_decode(simpleDecrypt($credential->credentials, config('Encryption')->key));
}
if (!empty($credential->credentials)) {
foreach ($credential->credentials as $key2 => $value2) {
Expand All @@ -1039,7 +1037,7 @@ public function getLocalDevices($integration)
foreach ($devices as $device) {
foreach ($credentials as $credential) {
if (intval($credential->device_id) === intval($device->id)) {
$credential->credentials = json_decode(simpleDecrypt($credential->credentials));
$credential->credentials = json_decode(simpleDecrypt($credential->credentials, config('Encryption')->key));
foreach ($credential->credentials as $key => $value) {
$device->credentials->{$credential->type . '_' . $key} = $value;
}
Expand Down Expand Up @@ -1374,8 +1372,7 @@ public function read(int $id = 0): array
}
if ($config->decrypt_credentials === 'y') {
if (!empty($query[0]->credentials)) {
$query[0]->credentials = simpleDecrypt($query[0]->credentials, config('Encryption')->key);
$query[0]->credentials = json_decode($query[0]->credentials);
$query[0]->credentials = json_decode(simpleDecrypt($query[0]->credentials, config('Encryption')->key));
}
}
if ($query[0]->select_internal_type === 'group') {
Expand Down Expand Up @@ -1491,7 +1488,7 @@ public function upsertCredentials($integration, $devices)

$credentials = new \stdClass();
$credentials->community = $device->credentials->snmp_community;
$credentials = (string)simpleEncrypt(json_encode($credentials));
$credentials = (string)simpleEncrypt(json_encode($credentials), config('Encryption')->key);

if (intval($result[0]->count) === 0) {
$sql = "INSERT INTO credential VALUES (null, ?, 'y', ?, ?, 'snmp', ?, 'system', NOW())";
Expand All @@ -1515,7 +1512,7 @@ public function upsertCredentials($integration, $devices)
$credentials = new \stdClass();
$credentials->username = $device->credentials->windows_username;
$credentials->password = $device->credentials->windows_password;
$credentials = (string)simpleEncrypt(json_encode($credentials));
$credentials = (string)simpleEncrypt(json_encode($credentials), config('Encryption')->key);

if (intval($result[0]->count) === 0) {
$sql = "INSERT INTO credential VALUES (null, ?, 'y', ?, ?, 'snmp', ?, 'system', NOW())";
Expand Down Expand Up @@ -1549,7 +1546,7 @@ public function upsertCredentials($integration, $devices)
if (!empty($credentials->privacy_passphrase)) {
$device->credentials->security_level = 'authPriv';
}
$credentials = (string)simpleEncrypt(json_encode($credentials));
$credentials = (string)simpleEncrypt(json_encode($credentials), config('Encryption')->key);

if (intval($result[0]->count) === 0) {
$sql = "INSERT INTO credential VALUES (null, ?, 'y', ?, ?, 'snmp_v3', ?, 'system', NOW())";
Expand All @@ -1573,7 +1570,7 @@ public function upsertCredentials($integration, $devices)
$credentials = new \stdClass();
$credentials->username = $device->credentials->ssh_username;
$credentials->password = $device->credentials->ssh_password;
$credentials = (string)simpleEncrypt(json_encode($credentials));
$credentials = (string)simpleEncrypt(json_encode($credentials), config('Encryption')->key);

if (intval($result[0]->count) === 0) {
$sql = "INSERT INTO credential VALUES (null, ?, 'y', ?, ?, 'ssh', ?, 'system', NOW())";
Expand Down

0 comments on commit bc3d034

Please sign in to comment.