Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Azure
GitHub Repository: Azure/Azure-Sentinel-Notebooks
Path: blob/master/src/SentinelUtilities/SentinelWidgets/widget_view_helper.py
3255 views
1
# -------------------------------------------------------------------------
2
# Copyright (c) Microsoft Corporation. All rights reserved.
3
# Licensed under the MIT License. See License.txt in the project root for
4
# license information.
5
# --------------------------------------------------------------------------
6
"""
7
Widget View Helper:
8
This module provides helper methods for UI controls and components.
9
"""
10
11
import os
12
import ipywidgets as widgets
13
from IPython.display import HTML
14
15
# pylint: disable-msg=R0904
16
# pylint: disable-msg=E0602
17
class WidgetViewHelper():
18
""" This classes provides helper methods for UI controls and components. """
19
20
def __init__(self):
21
self.variable = None
22
23
def set_env(self, env_dir, var):
24
""" Set notebook environmental variable """
25
val = None
26
if var in env_dir.keys():
27
val = env_dir[var]
28
29
self.variable = var
30
user_input = widgets.Text(value=val, description=var + ': ')
31
user_input.observe(self.save_env, 'value')
32
display(user_input)
33
34
def save_env(self, val):
35
""" Save notebook environmental variable """
36
os.environ[self.variable] = val.new
37
38
@staticmethod
39
def set_env1(reset, env_dir, env_dict):
40
""" Set notebook environmental variables """
41
for key in env_dict.keys():
42
user_input = ''
43
if not reset and key in env_dir:
44
env_dict[key] = env_dir[key]
45
print(key + '=' + env_dir[key])
46
else:
47
user_input = widgets.Text(description=key + ': ')
48
os.environ[key] = user_input.value
49
env_dict[key] = user_input.value
50
return env_dict
51
52
@staticmethod
53
def select_vm(compute):
54
""" Select a VM """
55
vm_names = sorted(list(vm.name for vm in list(compute.get_vm_list())))
56
return widgets.Dropdown(options=vm_names, value=vm_names[0], description='VM:')
57
58
@staticmethod
59
def select_managed_disk(compute, vm_name):
60
""" Select a managed disk """
61
disk_list = compute.get_vm_disk_names(vm_name)
62
return widgets.Dropdown(options=disk_list, value=disk_list[0], description='Disk:')
63
64
@staticmethod
65
def select_account_creation():
66
""" Create a new account or use existing account """
67
storage_account_creation = ['Creating new account', 'Using exist account']
68
return widgets.Dropdown(options=storage_account_creation,
69
value=storage_account_creation[0],
70
description='Storage Account Creation:')
71
72
@staticmethod
73
def select_blob_container_creation():
74
""" Create a new container or use existing container """
75
blob_container_creation = ['Creating new container', 'Using exist container']
76
return widgets.Dropdown(options=blob_container_creation,
77
value=blob_container_creation[0],
78
description='Blob Container Creation:')
79
80
@staticmethod
81
def select_os():
82
""" Select Windows or Linux """
83
os_type_list = ['Windows', 'Linux']
84
return widgets.Dropdown(options=os_type_list, value=os_type_list[0], description='OS Type:')
85
86
@staticmethod
87
def check_storage_account_name_availability(storage):
88
""" Check if a storage account name is available to use """
89
# usert input storage account name
90
storage_account_name = input('Storage Account Name:')
91
name_availability = storage.is_storage_account_name_available(storage_account_name)
92
return storage_account_name if name_availability.name_available else None
93
94
@staticmethod
95
def create_storage_account_and_get_key(storage,
96
storage_account_name,
97
resource_group_for_storage):
98
""" Create storage account """
99
storage_location = input('Storage Location:')
100
storage.create_storage_account_async(
101
storage_account_name,
102
resource_group_for_storage,
103
**{'storage_location' : storage_location})
104
return storage.get_storage_account_key(storage_account_name, resource_group_for_storage)
105
106
@staticmethod
107
def select_storage_account(storage, resource_group_for_storage):
108
""" Select a storage account """
109
storage_account_list = storage.get_storage_account_names(resource_group_for_storage)
110
return widgets.Dropdown(options=storage_account_list,
111
value=storage_account_list[0],
112
description='Existing Storage Accounts:')
113
114
@staticmethod
115
def select_blob_container(storage, resource_group_for_storage, storage_account_name):
116
""" Select a blob container """
117
blob_container_list = storage.get_container_name_list(resource_group_for_storage,
118
storage_account_name,
119
None)
120
return widgets.Dropdown(options=blob_container_list,
121
value=blob_container_list[0],
122
description='Blob Containers:')
123
124
@staticmethod
125
def select_log_analytics_workspace(loganalytics):
126
""" Select a LA workspace """
127
workspace_name_list = loganalytics.get_workspace_name_list()
128
return widgets.Dropdown(options=workspace_name_list,
129
value=workspace_name_list[0],
130
description='Workspace:')
131
132
@staticmethod
133
def select_multiple_tables(anomaly_lookup):
134
""" Select data tables """
135
table_list = anomaly_lookup.query_table_list()
136
tables = sorted(table_list.SentinelTableName.tolist())
137
return widgets.SelectMultiple(options=tables,
138
row=len(tables),
139
value=[],
140
description='Tables:')
141
142
@staticmethod
143
def generate_upload_container_path(storage, os_type, sas_expiration_in_days):
144
""" Generate a upload container path """
145
sas_url = storage.generate_blob_container_sas_url(sas_expiration_in_days)
146
upload_container_path = storage.build_upload_container_path(os_type, sas_url)
147
return upload_container_path
148
149
@staticmethod
150
# pylint: disable=line-too-long
151
def get_vm_extension_properties(os_type, upload_container_path, user_id=None):
152
""" Get VM extensions properties """
153
if os_type == 'Windows':
154
command_to_execute = 'powershell -File installNotebookExtension.ps1 "{0}" >> out.txt'.format(upload_container_path)
155
file_list = ['https://sentinelnotebooks.blob.core.windows.net/piwindowsstorage/installNotebookExtension.ps1',
156
'https://sentinelnotebooks.blob.core.windows.net/piwindowsstorage/piextension.zip']
157
elif os_type == 'Linux':
158
command_to_execute = './piondemand.sh "' + upload_container_path + '"'
159
file_list = ['https://sentinelnotebooks.blob.core.windows.net/pilinuxstorage/release/ondemand/stable/piondemand.sh',
160
'https://sentinelnotebooks.blob.core.windows.net/pilinuxstorage/release/ondemand/stable/pilinux.ondemand.tar.bz2']
161
162
elif os_type == 'DSVM':
163
command_to_execute = './azureforensics.sh {0}'.format(user_id)
164
file_list = ['https://sentinelnotebooks.blob.core.windows.net/forensicsnotebooks/azureforensics.sh',
165
'https://sentinelnotebooks.blob.core.windows.net/forensicsnotebooks/vhdexplorer.tar']
166
167
return command_to_execute, file_list
168
169
@staticmethod
170
def define_int_progress_bar():
171
""" Define a progress bar """
172
return widgets.IntProgress(value=0,
173
min=0,
174
max=10,
175
step=1,
176
description='Loading:',
177
bar_style='success',
178
orientation='horizontal',
179
position='top')
180
181
@staticmethod
182
# pylint: disable=line-too-long
183
def copy_to_clipboard(url, text_body, label_text):
184
""" Copy text to Clipboard """
185
html_str = (
186
"""<!DOCTYPE html>
187
<html><body style="height:20px">
188
<input id="sentinel_text_for_copy" type="text" readonly style="font-weight: bold; border: none; max-height:10px; width:1px;" size = '"""
189
+ str(len(text_body))
190
+ """' value='"""
191
+ text_body
192
+ """'>
193
<button style="border: 2px solid #4CAF50;" onclick="sentinel_copy()">""" + label_text + """</button>
194
<script>
195
function sentinel_copy() {
196
var copyText = document.getElementById("sentinel_text_for_copy");
197
copyText.select();
198
document.execCommand("copy");
199
}
200
</script>
201
</body></html>"""
202
)
203
204
return html_str
205
206
@staticmethod
207
# pylint: disable=line-too-long
208
def construct_url_for_log_analytics_logs(tenant_domain,
209
subscription_id,
210
resource_group,
211
workspace_name):
212
""" Generate URL for LA logs """
213
return 'https://portal.azure.com/#blade/Microsoft_Azure_Security_Insights/MainMenuBlade/7/subscriptionId/{0}/resourceGroup/{1}/workspaceName/{2}'.format(subscription_id, resource_group, workspace_name)
214
215
@staticmethod
216
# pylint: disable=undefined-variable
217
def display_html(inner_html):
218
""" Display HTML """
219
display(HTML(inner_html))
220
221
@staticmethod
222
def pick_start_and_end_date():
223
""" Pick dates """
224
start_date = widgets.DatePicker(description='Pick a start date', disabled=False)
225
end_date = widgets.DatePicker(description='Pick a end date', disabled=False)
226
# pylint: disable=undefined-variable
227
display(start_date)
228
# pylint: disable=undefined-variable
229
display(end_date)
230
return start_date, end_date
231
232
@staticmethod
233
# pylint: disable=line-too-long
234
# pylint: disable=undefined-variable
235
def select_multiple_items(label, item_name):
236
""" Select multiple items """
237
label_item = widgets.Label(value=label)
238
items = widgets.Textarea(value='', placeholder='One per line: \n 0x7ae3 \n 0x7ae6', description=item_name, disabled=False, rows=5)
239
display(label_item)
240
display(items)
241
return items
242
243