Skip to content

Commit 4b6ca3a

Browse files
authored
Refactor setup for plugin_servcheck with backups and encryption (Cacti#64)
Added checks for the existence of the 'plugin_servcheck_restapi_method' table before creating backups and processing records. Refactored credential handling for different types and ensured proper encryption and database updates.
1 parent 1b3ad1c commit 4b6ca3a

1 file changed

Lines changed: 60 additions & 52 deletions

File tree

setup.php

Lines changed: 60 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,18 @@ function plugin_servcheck_upgrade() {
8282
db_execute('CREATE TABLE plugin_servcheck_test_backup AS SELECT * FROM plugin_servcheck_test');
8383
db_execute('CREATE TABLE plugin_servcheck_log_backup AS SELECT * FROM plugin_servcheck_log');
8484
db_execute('CREATE TABLE plugin_servcheck_proxies_backup AS SELECT * FROM plugin_servcheck_proxies');
85-
db_execute('CREATE TABLE plugin_servcheck_restapi_method_backup AS SELECT * FROM plugin_servcheck_restapi_method');
85+
if (db_table_exists('plugin_servcheck_restapi_method')) {
86+
db_execute('CREATE TABLE plugin_servcheck_restapi_method_backup AS SELECT * FROM plugin_servcheck_restapi_method');
87+
}
8688

8789
if (db_column_exists('plugin_servcheck_test', 'display_name')) {
8890
db_execute('ALTER TABLE plugin_servcheck_test RENAME COLUMN display_name TO name');
8991
}
9092

93+
if (db_column_exists('plugin_servcheck_test', 'ca')) {
94+
db_execute('ALTER TABLE plugin_servcheck_test RENAME COLUMN ca TO ca_id');
95+
}
96+
9197
if (db_column_exists('plugin_servcheck_test', 'proxy_server')) {
9298
db_execute('ALTER TABLE plugin_servcheck_test RENAME COLUMN proxy_server TO proxy_id');
9399
}
@@ -204,58 +210,60 @@ function plugin_servcheck_upgrade() {
204210
}
205211
}
206212

207-
$records = db_fetch_assoc("SELECT * FROM plugin_servcheck_restapi_method");
208-
if (cacti_sizeof($records)) {
209-
foreach ($records as $record) {
210-
if ($record['type'] == 'no') {
211-
continue;
212-
}
213-
214-
$cred = array();
215-
216-
if ($record['type'] == 'basic') {
217-
$cred['type'] = 'basic';
218-
$cred['username'] = servcheck_show_text($record['username']);
219-
$cred['password'] = servcheck_show_text($record['password']);
220-
$cred['data_url'] = $record['data_url'];
221-
} elseif ($record['type'] == 'apikey') {
222-
$cred['type'] = 'apikey';
223-
$cred['option_apikey'] = 'post';
224-
$cred['token_name'] = $record['username'];
225-
$cred['data_url'] = $record['data_url'];
226-
$cred['token_value'] = servcheck_show_text($record['cred_value']);
227-
} elseif ($record['type'] == 'oauth2') {
228-
$cred['type'] = 'oauth2';
229-
$cred['oauth_client_id'] = servcheck_show_text($record['username']);
230-
$cred['oauth_client_secret'] = servcheck_show_text($record['password']);
231-
$cred['token_value'] = servcheck_show_text($record['cred_value']);
232-
$cred['token_name'] = $record['cred_name'];
233-
$cred['cred_valiedity'] = $record['cred_validity'];
234-
$cred['data_url'] = $record['data_url'];
235-
$cred['login_url'] = $record['login_url'];
236-
} elseif ($record['type'] == 'cookie') {
237-
$cred['type'] = 'cookie';
238-
$cred['option_cookie'] = 'json';
239-
$cred['username'] = servcheck_show_text($record['username']);
240-
$cred['password'] = servcheck_show_text($record['password']);
241-
$cred['data_url'] = $record['data_url'];
242-
$cred['login_url'] = $record['login_url'];
213+
if (db_table_exists('plugin_servcheck_restapi_method')) {
214+
$records = db_fetch_assoc("SELECT * FROM plugin_servcheck_restapi_method");
215+
if (cacti_sizeof($records)) {
216+
foreach ($records as $record) {
217+
if ($record['type'] == 'no') {
218+
continue;
219+
}
220+
221+
$cred = array();
222+
223+
if ($record['type'] == 'basic') {
224+
$cred['type'] = 'basic';
225+
$cred['username'] = servcheck_show_text($record['username']);
226+
$cred['password'] = servcheck_show_text($record['password']);
227+
$cred['data_url'] = $record['data_url'];
228+
} elseif ($record['type'] == 'apikey') {
229+
$cred['type'] = 'apikey';
230+
$cred['option_apikey'] = 'post';
231+
$cred['token_name'] = $record['username'];
232+
$cred['data_url'] = $record['data_url'];
233+
$cred['token_value'] = servcheck_show_text($record['cred_value']);
234+
} elseif ($record['type'] == 'oauth2') {
235+
$cred['type'] = 'oauth2';
236+
$cred['oauth_client_id'] = servcheck_show_text($record['username']);
237+
$cred['oauth_client_secret'] = servcheck_show_text($record['password']);
238+
$cred['token_value'] = servcheck_show_text($record['cred_value']);
239+
$cred['token_name'] = $record['cred_name'];
240+
$cred['cred_valiedity'] = $record['cred_validity'];
241+
$cred['data_url'] = $record['data_url'];
242+
$cred['login_url'] = $record['login_url'];
243+
} elseif ($record['type'] == 'cookie') {
244+
$cred['type'] = 'cookie';
245+
$cred['option_cookie'] = 'json';
246+
$cred['username'] = servcheck_show_text($record['username']);
247+
$cred['password'] = servcheck_show_text($record['password']);
248+
$cred['data_url'] = $record['data_url'];
249+
$cred['login_url'] = $record['login_url'];
250+
}
251+
252+
$enc = servcheck_encrypt_credential($cred);
253+
254+
$test_id = db_fetch_cell_prepared ('SELECT id FROM plugin_servcheck_test
255+
WHERE restapi_id = ? LIMIT 1',
256+
array($record['id']));
257+
258+
db_execute_prepared('INSERT INTO plugin_servcheck_credential
259+
(name, type, data) VALUES (?, ?, ?)',
260+
array('upgrade/convert_restapi_' . $record['id'], $cred['type'], $enc));
261+
262+
db_execute_prepared('UPDATE plugin_servcheck_test
263+
set type = ?, cred_id = ?
264+
WHERE id = ?',
265+
array('rest_' . $cred['type'], db_fetch_insert_id(), $test_id));
243266
}
244-
245-
$enc = servcheck_encrypt_credential($cred);
246-
247-
$test_id = db_fetch_cell_prepared ('SELECT id FROM plugin_servcheck_test
248-
WHERE restapi_id = ? LIMIT 1',
249-
array($record['id']));
250-
251-
db_execute_prepared('INSERT INTO plugin_servcheck_credential
252-
(name, type, data) VALUES (?, ?, ?)',
253-
array('upgrade/convert_restapi_' . $record['id'], $cred['type'], $enc));
254-
255-
db_execute_prepared('UPDATE plugin_servcheck_test
256-
set type = ?, cred_id = ?
257-
WHERE id = ?',
258-
array('rest_' . $cred['type'], db_fetch_insert_id(), $test_id));
259267
}
260268
}
261269

0 commit comments

Comments
 (0)