Path: blob/master/src/SentinelUtilities/SentinelWidgets/widget_view_helper.py
3255 views
# -------------------------------------------------------------------------1# Copyright (c) Microsoft Corporation. All rights reserved.2# Licensed under the MIT License. See License.txt in the project root for3# license information.4# --------------------------------------------------------------------------5"""6Widget View Helper:7This module provides helper methods for UI controls and components.8"""910import os11import ipywidgets as widgets12from IPython.display import HTML1314# pylint: disable-msg=R090415# pylint: disable-msg=E060216class WidgetViewHelper():17""" This classes provides helper methods for UI controls and components. """1819def __init__(self):20self.variable = None2122def set_env(self, env_dir, var):23""" Set notebook environmental variable """24val = None25if var in env_dir.keys():26val = env_dir[var]2728self.variable = var29user_input = widgets.Text(value=val, description=var + ': ')30user_input.observe(self.save_env, 'value')31display(user_input)3233def save_env(self, val):34""" Save notebook environmental variable """35os.environ[self.variable] = val.new3637@staticmethod38def set_env1(reset, env_dir, env_dict):39""" Set notebook environmental variables """40for key in env_dict.keys():41user_input = ''42if not reset and key in env_dir:43env_dict[key] = env_dir[key]44print(key + '=' + env_dir[key])45else:46user_input = widgets.Text(description=key + ': ')47os.environ[key] = user_input.value48env_dict[key] = user_input.value49return env_dict5051@staticmethod52def select_vm(compute):53""" Select a VM """54vm_names = sorted(list(vm.name for vm in list(compute.get_vm_list())))55return widgets.Dropdown(options=vm_names, value=vm_names[0], description='VM:')5657@staticmethod58def select_managed_disk(compute, vm_name):59""" Select a managed disk """60disk_list = compute.get_vm_disk_names(vm_name)61return widgets.Dropdown(options=disk_list, value=disk_list[0], description='Disk:')6263@staticmethod64def select_account_creation():65""" Create a new account or use existing account """66storage_account_creation = ['Creating new account', 'Using exist account']67return widgets.Dropdown(options=storage_account_creation,68value=storage_account_creation[0],69description='Storage Account Creation:')7071@staticmethod72def select_blob_container_creation():73""" Create a new container or use existing container """74blob_container_creation = ['Creating new container', 'Using exist container']75return widgets.Dropdown(options=blob_container_creation,76value=blob_container_creation[0],77description='Blob Container Creation:')7879@staticmethod80def select_os():81""" Select Windows or Linux """82os_type_list = ['Windows', 'Linux']83return widgets.Dropdown(options=os_type_list, value=os_type_list[0], description='OS Type:')8485@staticmethod86def check_storage_account_name_availability(storage):87""" Check if a storage account name is available to use """88# usert input storage account name89storage_account_name = input('Storage Account Name:')90name_availability = storage.is_storage_account_name_available(storage_account_name)91return storage_account_name if name_availability.name_available else None9293@staticmethod94def create_storage_account_and_get_key(storage,95storage_account_name,96resource_group_for_storage):97""" Create storage account """98storage_location = input('Storage Location:')99storage.create_storage_account_async(100storage_account_name,101resource_group_for_storage,102**{'storage_location' : storage_location})103return storage.get_storage_account_key(storage_account_name, resource_group_for_storage)104105@staticmethod106def select_storage_account(storage, resource_group_for_storage):107""" Select a storage account """108storage_account_list = storage.get_storage_account_names(resource_group_for_storage)109return widgets.Dropdown(options=storage_account_list,110value=storage_account_list[0],111description='Existing Storage Accounts:')112113@staticmethod114def select_blob_container(storage, resource_group_for_storage, storage_account_name):115""" Select a blob container """116blob_container_list = storage.get_container_name_list(resource_group_for_storage,117storage_account_name,118None)119return widgets.Dropdown(options=blob_container_list,120value=blob_container_list[0],121description='Blob Containers:')122123@staticmethod124def select_log_analytics_workspace(loganalytics):125""" Select a LA workspace """126workspace_name_list = loganalytics.get_workspace_name_list()127return widgets.Dropdown(options=workspace_name_list,128value=workspace_name_list[0],129description='Workspace:')130131@staticmethod132def select_multiple_tables(anomaly_lookup):133""" Select data tables """134table_list = anomaly_lookup.query_table_list()135tables = sorted(table_list.SentinelTableName.tolist())136return widgets.SelectMultiple(options=tables,137row=len(tables),138value=[],139description='Tables:')140141@staticmethod142def generate_upload_container_path(storage, os_type, sas_expiration_in_days):143""" Generate a upload container path """144sas_url = storage.generate_blob_container_sas_url(sas_expiration_in_days)145upload_container_path = storage.build_upload_container_path(os_type, sas_url)146return upload_container_path147148@staticmethod149# pylint: disable=line-too-long150def get_vm_extension_properties(os_type, upload_container_path, user_id=None):151""" Get VM extensions properties """152if os_type == 'Windows':153command_to_execute = 'powershell -File installNotebookExtension.ps1 "{0}" >> out.txt'.format(upload_container_path)154file_list = ['https://sentinelnotebooks.blob.core.windows.net/piwindowsstorage/installNotebookExtension.ps1',155'https://sentinelnotebooks.blob.core.windows.net/piwindowsstorage/piextension.zip']156elif os_type == 'Linux':157command_to_execute = './piondemand.sh "' + upload_container_path + '"'158file_list = ['https://sentinelnotebooks.blob.core.windows.net/pilinuxstorage/release/ondemand/stable/piondemand.sh',159'https://sentinelnotebooks.blob.core.windows.net/pilinuxstorage/release/ondemand/stable/pilinux.ondemand.tar.bz2']160161elif os_type == 'DSVM':162command_to_execute = './azureforensics.sh {0}'.format(user_id)163file_list = ['https://sentinelnotebooks.blob.core.windows.net/forensicsnotebooks/azureforensics.sh',164'https://sentinelnotebooks.blob.core.windows.net/forensicsnotebooks/vhdexplorer.tar']165166return command_to_execute, file_list167168@staticmethod169def define_int_progress_bar():170""" Define a progress bar """171return widgets.IntProgress(value=0,172min=0,173max=10,174step=1,175description='Loading:',176bar_style='success',177orientation='horizontal',178position='top')179180@staticmethod181# pylint: disable=line-too-long182def copy_to_clipboard(url, text_body, label_text):183""" Copy text to Clipboard """184html_str = (185"""<!DOCTYPE html>186<html><body style="height:20px">187<input id="sentinel_text_for_copy" type="text" readonly style="font-weight: bold; border: none; max-height:10px; width:1px;" size = '"""188+ str(len(text_body))189+ """' value='"""190+ text_body191+ """'>192<button style="border: 2px solid #4CAF50;" onclick="sentinel_copy()">""" + label_text + """</button>193<script>194function sentinel_copy() {195var copyText = document.getElementById("sentinel_text_for_copy");196copyText.select();197document.execCommand("copy");198}199</script>200</body></html>"""201)202203return html_str204205@staticmethod206# pylint: disable=line-too-long207def construct_url_for_log_analytics_logs(tenant_domain,208subscription_id,209resource_group,210workspace_name):211""" Generate URL for LA logs """212return 'https://portal.azure.com/#blade/Microsoft_Azure_Security_Insights/MainMenuBlade/7/subscriptionId/{0}/resourceGroup/{1}/workspaceName/{2}'.format(subscription_id, resource_group, workspace_name)213214@staticmethod215# pylint: disable=undefined-variable216def display_html(inner_html):217""" Display HTML """218display(HTML(inner_html))219220@staticmethod221def pick_start_and_end_date():222""" Pick dates """223start_date = widgets.DatePicker(description='Pick a start date', disabled=False)224end_date = widgets.DatePicker(description='Pick a end date', disabled=False)225# pylint: disable=undefined-variable226display(start_date)227# pylint: disable=undefined-variable228display(end_date)229return start_date, end_date230231@staticmethod232# pylint: disable=line-too-long233# pylint: disable=undefined-variable234def select_multiple_items(label, item_name):235""" Select multiple items """236label_item = widgets.Label(value=label)237items = widgets.Textarea(value='', placeholder='One per line: \n 0x7ae3 \n 0x7ae6', description=item_name, disabled=False, rows=5)238display(label_item)239display(items)240return items241242243