From 9ce54213b65c4c7d3c89b05f4a416b70c7af3745 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 23 Sep 2020 14:07:23 +0200 Subject: [PATCH] WIP GCP --- .../include/class/CredentialStore.class.php | 134 ++++++++++++++---- pandora_console/include/functions_html.php | 13 +- .../include/javascript/pandora_ui.js | 26 ++-- .../include/styles/credential_store.css | 11 +- 4 files changed, 136 insertions(+), 48 deletions(-) diff --git a/pandora_console/include/class/CredentialStore.class.php b/pandora_console/include/class/CredentialStore.class.php index 616c56d0cb..c609947e4a 100644 --- a/pandora_console/include/class/CredentialStore.class.php +++ b/pandora_console/include/class/CredentialStore.class.php @@ -248,7 +248,12 @@ class CredentialStore extends Wizard ); } else { $groups = [ $filter['filter_id_group'] ]; - $childrens = groups_get_childrens($id_group, null, true); + $childrens = groups_get_childrens( + $filter['filter_id_group'], + null, + true + ); + if (!empty($childrens)) { foreach ($childrens as $child) { $groups[] = (int) $child['id_grupo']; @@ -385,6 +390,8 @@ class CredentialStore extends Wizard // Decrypt content. $key['username'] = io_output_password($key['username']); $key['password'] = io_output_password($key['password']); + $key['extra_1'] = io_output_password($key['extra_1']); + $key['extra_2'] = io_output_password($key['extra_2']); return $key; } @@ -425,6 +432,8 @@ class CredentialStore extends Wizard function ($carry, $item) { $item['username'] = io_output_password($item['username']); $item['password'] = io_output_password($item['password']); + $item['extra_1'] = io_output_password($item['extra_1']); + $item['extra_2'] = io_output_password($item['extra_2']); $carry[$item['identifier']] = $item['identifier']; return $carry; } @@ -561,13 +570,28 @@ class CredentialStore extends Wizard $extra_1 = get_parameter('extra_1', null); $extra_2 = get_parameter('extra_2', null); - if (empty($identifier)) { + if ($product === 'GOOGLE') { + $google_creds = json_decode(io_safe_output($extra_1)); + + if (json_last_error() !== JSON_ERROR_NONE) { + $this->ajaxMsg( + 'error', + __('Not a valid JSON: %s', json_last_error_msg()) + ); + exit; + } + + $username = $google_creds->client_email; + $password = $google_creds->private_key_id; + } + + if (empty($identifier) === true) { $error = __('Key identifier is required'); } else if ($id_group === null) { $error = __('You must select a group where store this key!'); - } else if (empty($product)) { + } else if (empty($product) === true) { $error = __('You must specify a product type'); - } else if (empty($username) && (empty($password))) { + } else if (empty($username) === true && (empty($password) === true)) { $error = __('You must specify a username and/or password'); } @@ -583,8 +607,8 @@ class CredentialStore extends Wizard 'product' => $product, 'username' => io_input_password($username), 'password' => io_input_password($password), - 'extra_1' => $extra_1, - 'extra_2' => $extra_2, + 'extra_1' => io_input_password($extra_1), + 'extra_2' => io_input_password($extra_2), ]; // Spaces are not allowed. @@ -887,6 +911,9 @@ class CredentialStore extends Wizard $pass_label = __('Password'); $extra_1_label = __('Extra'); $extra_2_label = __('Extra (2)'); + $extra1_type = 'text'; + $user = true; + $pass = true; $extra1 = true; $extra2 = true; @@ -907,7 +934,14 @@ class CredentialStore extends Wizard break; case 'GOOGLE': - // Need further investigation. + $extra_1_label = __('Auth JSON'); + $user = false; + $pass = false; + $extra1 = true; + $extra2 = false; + $extra1_type = 'textarea'; + break; + case 'CUSTOM': case 'SAP': $user_label = __('Account ID'); @@ -919,29 +953,33 @@ class CredentialStore extends Wizard break; } - $inputs[] = [ - 'label' => $user_label, - 'id' => 'div-username', - 'arguments' => [ - 'name' => 'username', - 'input_class' => 'flex-row', - 'type' => 'text', - 'value' => $values['username'], - 'return' => true, - ], - ]; + if ($user) { + $inputs[] = [ + 'label' => $user_label, + 'id' => 'div-username', + 'arguments' => [ + 'name' => 'username', + 'input_class' => 'flex-row', + 'type' => 'text', + 'value' => $values['username'], + 'return' => true, + ], + ]; + } - $inputs[] = [ - 'label' => $pass_label, - 'id' => 'div-password', - 'arguments' => [ - 'name' => 'password', - 'input_class' => 'flex-row', - 'type' => 'password', - 'value' => $values['password'], - 'return' => true, - ], - ]; + if ($pass) { + $inputs[] = [ + 'label' => $pass_label, + 'id' => 'div-password', + 'arguments' => [ + 'name' => 'password', + 'input_class' => 'flex-row', + 'type' => 'password', + 'value' => $values['password'], + 'return' => true, + ], + ]; + } if ($extra1) { $inputs[] = [ @@ -949,8 +987,9 @@ class CredentialStore extends Wizard 'id' => 'div-extra_1', 'arguments' => [ 'name' => 'extra_1', + 'id' => 'text-extra_1', 'input_class' => 'flex-row', - 'type' => 'text', + 'type' => $extra1_type, 'value' => $values['extra_1'], 'return' => true, ], @@ -1031,14 +1070,30 @@ class CredentialStore extends Wizard * Handles inputs visibility based on selected product. */ function calculate_inputs() { + if ($('#product :selected').val() != "GOOGLE") { + // Restore text-extra_1. + var val = $('#text-extra_1').val(); + if(typeof val == 'undefined') { + val = ''; + } + $('#text-extra_1').remove(); + $('#div-extra_1').append( + $('') + ); + } + if ($('#product :selected').val() == "CUSTOM") { $('#div-username label').text(''); $('#div-password label').text(''); + $('#div-username').show(); + $('#div-password').show(); $('#div-extra_1').hide(); $('#div-extra_2').hide(); } else if ($('#product :selected').val() == "AWS") { $('#div-username label').text(''); $('#div-password label').text(''); + $('#div-username').show(); + $('#div-password').show(); $('#div-extra_1').hide(); $('#div-extra_2').hide(); } else if ($('#product :selected').val() == "AZURE") { @@ -1046,13 +1101,32 @@ class CredentialStore extends Wizard $('#div-password label').text(''); $('#div-extra_1 label').text(''); $('#div-extra_2 label').text(''); + $('#div-username').show(); + $('#div-password').show(); $('#div-extra_1').show(); $('#div-extra_2').show(); } else if ($('#product :selected').val() == "SAP") { $('#div-username label').text(''); $('#div-password label').text(''); + $('#div-username').show(); + $('#div-password').show(); $('#div-extra_1').hide(); $('#div-extra_2').hide(); + } else if ($('#product :selected').val() == "GOOGLE") { + $('#div-username').hide(); + $('#div-password').hide(); + $('#div-extra_2').hide(); + $('#div-extra_1 label').text(''); + var val = $('#text-extra_1').val(); + if(typeof val == 'undefined') { + val = ''; + } + + $('#text-extra_1').remove(); + $('#div-extra_1').append( + $('') + ); + $('#div-extra_1').show(); } } diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 6b66357652..44d88bdcc4 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -2681,13 +2681,17 @@ function html_print_textarea( $attributes='', $return=false, $class='', - $disable=false + $disable=false, + $id=false ) { $disabled = ($disable) ? 'disabled' : ''; - $output = ''; - if ($return) { return $output; } @@ -4361,7 +4365,8 @@ function html_print_input($data, $wrapper='div', $input_only=false) ((isset($data['attributes']) === true) ? $data['attributes'] : ''), ((isset($data['return']) === true) ? $data['return'] : false), ((isset($data['class']) === true) ? $data['class'] : ''), - ((isset($data['disabled']) === true) ? $data['disabled'] : false) + ((isset($data['disabled']) === true) ? $data['disabled'] : false), + ((isset($data['id']) === true) ? $data['id'] : false) ); break; diff --git a/pandora_console/include/javascript/pandora_ui.js b/pandora_console/include/javascript/pandora_ui.js index 2445473591..efebf39e9f 100644 --- a/pandora_console/include/javascript/pandora_ui.js +++ b/pandora_console/include/javascript/pandora_ui.js @@ -249,22 +249,24 @@ function load_modal(settings) { }); } else { settings.form.forEach(function(element) { - $("#" + element + " :input").each(function() { - // TODO VALIDATE ALL INPUTS. - if (this.type == "file") { - if ($(this).prop("files")[0]) { - formdata.append(this.name, $(this).prop("files")[0]); - } - } else { - if ($(this).attr("type") == "checkbox") { - if (this.checked) { - formdata.append(this.name, "on"); + $("#" + element + " :input, #" + element + " textarea").each( + function() { + // TODO VALIDATE ALL INPUTS. + if (this.type == "file") { + if ($(this).prop("files")[0]) { + formdata.append(this.name, $(this).prop("files")[0]); } } else { - formdata.append(this.name, $(this).val()); + if ($(this).attr("type") == "checkbox") { + if (this.checked) { + formdata.append(this.name, "on"); + } + } else { + formdata.append(this.name, $(this).val()); + } } } - }); + ); }); } diff --git a/pandora_console/include/styles/credential_store.css b/pandora_console/include/styles/credential_store.css index 5707d90780..3f5ff8d23c 100644 --- a/pandora_console/include/styles/credential_store.css +++ b/pandora_console/include/styles/credential_store.css @@ -24,7 +24,7 @@ ul.wizard li { margin-right: 1em; } -form.modal ul.wizard li { +form#modal_form ul.wizard li { display: flex; flex-direction: row; width: 90%; @@ -32,7 +32,7 @@ form.modal ul.wizard li { justify-items: center; } -form.modal ul.wizard li * { +form#modal_form ul.wizard li * { flex: 1; } @@ -40,3 +40,10 @@ ul.wizard li.flex-indep { flex: 1; margin: 0; } + +form#modal_form ul.wizard li textarea { + font-size: 0.8em; + font-family: "Courier New", Courier, monospace; + flex: 1 1 400px; + max-width: 400px; +}