-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
716 lines (608 loc) · 28.6 KB
/
server.py
File metadata and controls
716 lines (608 loc) · 28.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
"""
XSA Python buildpack app example
Author: Andrew Lunde
"""
from flask import Flask
from flask import request
from flask import Response
from flask import send_from_directory
#
import os
#import pyhdb
# Downloading pyhdb-0.3.3.tar.gz
import json
import datetime
import time
#import Crypto.PublicKey.RSA as RSA
#import jws.utils
#import python_jwt as jwt
#https://help.sap.com/viewer/4505d0bdaf4948449b7f7379d24d0f0d/2.0.03/en-US/8732609bd5314b51a17d6a3cc09110c3.html#loio8732609bd5314b51a17d6a3cc09110c3__section_atx_2vt_vt
from sap import xssec
from cfenv import AppEnv
#
#from sap.cf_logging import flask_logging
#
#https://help.sap.com/viewer/0eec0d68141541d1b07893a39944924e/2.0.03/en-US/d12c86af7cb442d1b9f8520e2aba7758.html
from hdbcli import dbapi
app = Flask(__name__)
env = AppEnv()
# Get port from environment variable or choose 9099 as local default
# If you are testing locally (i.e. not with xs or cf deployments,
# Be sure to pull all the python modules locally
# with pip using XS_PYTHON unzipped to /tmp
# mkdir -p local
# pip install -t local -r requirements.txt -f /tmp
port = int(os.getenv("PORT", 9099))
hanass = env.get_service(name='HEADLESS_SS')
ss_conn = ""
chrusr = ""
chrpwd = ""
loggedin = False
def attach(port, host):
try:
import pydevd
pydevd.stoptrace() #I.e.: disconnect if already connected
# pydevd.DebugInfoHolder.DEBUG_RECORD_SOCKET_READS = True
# pydevd.DebugInfoHolder.DEBUG_TRACE_BREAKPOINTS = 3
# pydevd.DebugInfoHolder.DEBUG_TRACE_LEVEL = 3
pydevd.settrace(
port=port,
host=host,
stdoutToServer=True,
stderrToServer=True,
overwrite_prev_trace=True,
suspend=False,
trace_only_current_thread=False,
patch_multiprocessing=False,
)
except:
import traceback;traceback.print_exc()
def get_conn(hana):
schema = hana.credentials['schema']
host = hana.credentials['host']
port = hana.credentials['port']
user = hana.credentials['user']
password = hana.credentials['password']
# The certificate will available for HANA service instances that require an encrypted connection
# Note: This was tested to work with python hdbcli-2.3.112 tar.gz package not hdbcli-2.3.14 provided in XS_PYTHON00_0-70003433.ZIP
if 'certificate' in hana.credentials:
haascert = hana.credentials['certificate']
#output += 'schema: ' + schema + '<br >\n'
#output += 'host: ' + host + '<br >\n'
#output += 'port: ' + port + '<br >\n'
#output += 'user: ' + user + '<br >\n'
#output += 'pass: ' + password + '<br >\n'
# # Connect to the python HANA DB driver using the connection info
# User for HANA as a Service instances
if 'certificate' in hana.credentials:
connection = dbapi.connect(
address=host,
port=int(port),
user=user,
password=password,
currentSchema=schema,
encrypt="true",
sslValidateCertificate="true",
sslCryptoProvider="openssl",
sslTrustStore=haascert
)
else:
connection = dbapi.connect(
address=host,
port=int(port),
user=user,
password=password,
currentSchema=schema
)
return connection
def get_unpw():
global chrusr
global chrpwd
output = ""
ss_conn = get_conn(hanass)
# Prep a cursor for SQL execution
cursor = ss_conn.cursor()
# Form an SQL statement to retrieve some data
#https://blogs.sap.com/2017/07/26/sap-hana-2.0-sps02-new-feature-updated-python-driver/
import codecs
hexvalue = cursor.callproc("SYS.USER_SECURESTORE_RETRIEVE", ("ChromeStore", False, "CHRUserName", None))
if hexvalue[3] is None:
output += 'key CHRUserName does not exist in store ChromeStore. Try inserting a value first.' + '<br >\n'
else:
retrieved = codecs.decode(hexvalue[3].hex(), "hex").decode()
chrusr = retrieved
output += 'key CHRUserName with value ' + retrieved + ' was retrieved from store ChromeStore.' + '<br >\n'
hexvalue = cursor.callproc("SYS.USER_SECURESTORE_RETRIEVE", ("ChromeStore", False, "CHRPassWord", None))
if hexvalue[3] is None:
output += 'key CHRPassWord does not exist in store ChromeStore. Try inserting a value first.' + '<br >\n'
else:
retrieved = codecs.decode(hexvalue[3].hex(), "hex").decode()
chrpwd = retrieved
retrieved = "*****"
output += 'key CHRPassWord with value ' + retrieved + ' was retrieved from store ChromeStore.' + '<br >\n'
# # Close the DB connection
ss_conn.close()
return output
# This module's Flask webserver will respond to these three routes (URL paths)
# If there is no path then just return Hello World and this module's instance number
# Requests passed through the app-router will never hit this route.
@app.route('/')
def hello_world():
output = '<strong>Hello World! I am instance ' + str(os.getenv("CF_INSTANCE_INDEX", 0)) + '</strong> Try these links.</br>\n'
output += '<a href="/env">/env</a><br />\n'
output += '<a href="/headless/test">/headless/test</a><br />\n'
output += '<a href="/headless/chrome">/headless/chrome</a><br />\n'
output += '<a href="/headless/db_only">/headless/db_only</a><br />\n'
output += '<a href="/auth_python/db_valid">/auth_python/db_valid</a><br />\n'
return output
# Satisfy browser requests for favicon.ico so that don't return 404
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, ''),'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/env')
def dump_env():
output = '\n Key Environment variables... \n'
output += 'PYTHONHOME: ' + str(os.getenv("PYTHONHOME", 0)) + '\n'
output += 'PYTHONPATH: ' + str(os.getenv("PYTHONPATH", 0)) + '\n'
output += 'VCAP_SERVICES: ' + str(os.getenv("VCAP_SERVICES", 0)) + '\n'
output += 'host: ' + hana.credentials['host'] + '\n'
output += 'port: ' + hana.credentials['port'] + '\n'
output += 'user: ' + hana.credentials['user'] + '\n'
output += 'pass: ' + hana.credentials['password'] + '\n'
output += '\n'
return output
# Coming through the app-router
@app.route('/headless/links')
@app.route('/headless/links/')
def headless_links():
output = '<strong>Hello World! I am instance ' + str(os.getenv("CF_INSTANCE_INDEX", 0)) + '</strong> Try these links.</br>\n'
output += '<a href="/headless/test">/headless/test</a><br />\n'
output += '<a href="/headless/admin/links">/headless/admin/links</a><br />\n'
output += '<a href="/headless/chrome">/headless/chrome</a><br />\n'
output += '<a href="/headless/db_only">/headless/db_only</a><br />\n'
output += '<a href="/auth_python/db_valid">/auth_python/db_valid</a><br />\n'
return output
# If there is a request for a python/test, return Testing message and module's instance number
@app.route('/headless/test')
def unauth_test():
return 'Python UnAuthorized Test, Yo! <br />\nI am instance ' + str(os.getenv("CF_INSTANCE_INDEX", 0))
@app.route('/headless/admin')
def admin_python_home():
output = '<strong>Password Administration</strong> Try these links.</br>\n'
output += '<a href="/headless/admin/links">/headless/admin/links</a><br />\n'
output += '<a href="/headless/test">/headless/test</a><br />\n'
return output
@app.route('/headless/admin/links')
def admin_links():
output = '<strong>Password Administration</strong> Try these links.</br>\n'
output += '<a href="/headless/admin/getpw">/headless/admin/getpw</a><br />\n'
output += '<a href="/headless/admin/setpw">/headless/admin/setpw</a><br />\n'
output += '<a href="/headless/admin/delpw">/headless/admin/delpw</a><br />\n'
output += '<a href="/headless/admin/authcheck">/headless/admin/authcheck</a><br />\n'
return output
@app.route('/headless/admin/getpw')
def admin_getpw():
output = '<strong>Password Administration Current</strong></br>\n'
global chrusr
global chrpwd
output += get_unpw()
output += '<a href="/headless/admin">Back to Admin</a><br />\n'
return output
@app.route('/headless/admin/setpw')
def admin_setpw():
output = '<strong>Password Administration</strong></br>\n'
output += '<form action="/headless/admin/setpw_result" method="post">\n'
output += '<table cellspacing="0" cellpadding="0">\n'
output += ' <tr><td align="right">User Name:</td><td align="left"><input type="text" name="username" size="30" maxlength="25"></td></tr>\n'
output += ' <tr><td align="right">Password:</td><td align="left"><input type="password" name="password" size="30" maxlength="25"></td></tr>\n'
output += ' <tr><td align="right"></td><td align="left"><button type="submit">Submit</button></td></tr>\n'
output += '</table>\n'
output += '</form>\n'
return output
@app.route('/headless/admin/setpw_result', methods=['POST'])
def admin_setpw_result():
output = '<strong>Password Administration Result</strong></br>\n'
global chrusr
global chrpwd
usr = 'unknown'
pwd = 'unknown'
if request.method == 'POST':
usr = request.form['username']
pwd = request.form['password']
output += 'User: ' + usr + '<br />\n'
output += 'Pass: ' + "*****" + '<br />\n'
ss_conn = get_conn(hanass)
# # Prep a cursor for SQL execution
cursor = ss_conn.cursor()
# # Form an SQL statement to retrieve some data
string2store = 'Whatever!'
import codecs
string2store = usr
hex2store = (codecs.encode(str.encode(string2store), "hex")).decode()
try:
cursor.callproc("SYS.USER_SECURESTORE_INSERT", ("ChromeStore", False, "CHRUserName", hex2store))
output += 'key CHRUserName with value ' + usr + '=' + hex2store + ' was inserted into store ChromeStore.' + '<br >\n'
chrusr = usr
except:
output += 'key CHRUserName likely already exists. Try deleting first.' + '<br >\n'
string2store = pwd
hex2store = (codecs.encode(str.encode(string2store), "hex")).decode()
try:
cursor.callproc("SYS.USER_SECURESTORE_INSERT", ("ChromeStore", False, "CHRPassWord", hex2store))
output += 'key CLIUserPass with value ' + "*****" + '=' + hex2store + ' was inserted into store ChromeStore.' + '<br >\n'
chrpwd = pwd
except:
output += 'key CLIUserPass likely already exists. Try deleting first.' + '<br >\n'
# # Close the DB connection
ss_conn.close()
output += '<a href="/headless/admin">Back to Admin</a><br />\n'
return output
@app.route('/headless/admin/delpw')
def admin_delpw():
output = '<strong>Password Deletion</strong></br>\n'
global chrusr
global chrpwd
ss_conn = get_conn(hanass)
# # Prep a cursor for SQL execution
cursor = ss_conn.cursor()
# # Form an SQL statement
cursor.callproc("SYS.USER_SECURESTORE_DELETE", ("ChromeStore", False, "CHRUserName"))
cursor.callproc("SYS.USER_SECURESTORE_DELETE", ("ChromeStore", False, "CHRPassWord"))
chrusr = ""
chrpwd = ""
# # Close the DB connection
ss_conn.close()
output += 'key CHRUserName and CHRPassWord were deleted from store ChromeStore.' + '<br />\n'
output += '<a href="/headless/admin">Back to Admin</a><br />\n'
return output
@app.route('/headless/chrome')
def headless_chrome():
output = "<!DOCTYPE HTML>\n"
output += "<html>\n"
output += "<head>\n"
output += "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8' />\n"
output += "<title>chrome</title>\n"
output += "</head>\n"
output += "<h4>Headless Chrome</h4><br />\n"
output += '<body style="font-family: Tahoma, Geneva, sans-serif">\n'
output += ' <a href="/headless/pagelist" target="chrome">Captured Pages</a><br />\n'
try:
from selenium import webdriver
#https://github.com/cryzed/Selenium-Requests
#from seleniumrequests import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as cond
from selenium.common.exceptions import ElementNotVisibleException
#https://w3c.github.io/webdriver/
options = webdriver.ChromeOptions()
options.binary_location = '/opt/google/chrome/chrome'
options.add_argument('headless')
options.add_argument('window-size=1200x600')
options.add_argument('no-sandbox')
options.add_argument('disable-dev-shm-usage')
driver = webdriver.Chrome(chrome_options=options)
driver.implicitly_wait(30)
#output += ' <p>' + request.args.get('page') + '</p><br />\n'
driver.get('https://account.us1.hana.ondemand.com/cockpit/#/globalaccount/aTeam/subaccounts')
#driver.get('https://www.conciletime.com')
#time.sleep(1)
try:
WebDriverWait(driver,10).until(cond.visibility_of_element_located((By.ID, "j_username")))
except (ElementNotVisibleException) as py_ex:
print("Element not visible.")
print (py_ex)
print (py_ex.args)
email = driver.find_element_by_id('j_username')
driver.get_screenshot_as_file('/root/app/pages/' + 'page01.png')
email.send_keys('andrew@lunde.com')
password = driver.find_element_by_id('j_password')
password.send_keys('Xxxx###!')
login = driver.find_element_by_id('logOnFormSubmit')
driver.get_screenshot_as_file('/root/app/pages/' + 'page02.png')
login.click()
#time.sleep(2)
try:
# Wait as long as required, or maximum of 10 sec for alert to appear
#WebDriverWait(driver,10).until(cond.alert_is_present())
WebDriverWait(driver,10).until(cond.visibility_of_element_located((By.ID, "__jsview1--addSubAccount")))
#Change over the control to the Alert window
#obj = driver.switch_to.alert
#Retrieve the message on the Alert window
#msg=obj.text
#print ("Alert shows following message: "+ msg )
#Use the accept() method to accept the alert
#obj.accept()
except (ElementNotVisibleException) as py_ex:
print("Element not visible.")
print (py_ex)
print (py_ex.args)
driver.get_screenshot_as_file('/root/app/pages/' + 'page03.png')
#__jsview1--addSubAccount
#$("#__jsview1--addSubAccount").tap();
addSubaccount = driver.find_element_by_id('__jsview1--addSubAccount')
addSubaccount.click()
#time.sleep(1)
try:
WebDriverWait(driver,10).until(cond.visibility_of_element_located((By.ID, "CreateNewSubAccountDialog--displayName-inner")))
except (ElementNotVisibleException) as py_ex:
print("Element not visible.")
print (py_ex)
print (py_ex.args)
driver.get_screenshot_as_file('/root/app/pages/' + 'page04.png')
#CreateNewSubAccountDialog--displayName-inner
displayName = driver.find_element_by_id('CreateNewSubAccountDialog--displayName-inner')
#$("#CreateNewSubAccountDialog--displayName-inner").val("aokheadless");
displayName.send_keys('abcheadless')
#CreateNewSubAccountDialog--description-inner
description = driver.find_element_by_id('CreateNewSubAccountDialog--description-inner')
#$("#CreateNewSubAccountDialog--description-inner").val("Test subaccount creation via headless browser.");
description.send_keys('Test subaccount creation via headless browser.')
#https://www.techbeamers.com/selenium-webdriver-coding-tips/
#Select dropdown = new Select(driver.findElement(By.xpath("//drop_down_x_path")));
#dropdown.deselectAll()
#dropdown.selectByVisibleText("selectLabel")
#CreateNewSubAccountDialog--environmentsCombo
#$("#CreateNewSubAccountDialog--environmentsCombo-hiddenInput").tap();
#$("#__item7-CreateNewSubAccountDialog--environmentsCombo-1").tap();
#environmentsCombo = driver.find_element_by_id('CreateNewSubAccountDialog--environmentsCombo')
#environmentsCombo.click()
#CreateNewSubAccountDialog--environmentsCombo-hiddenInput
#$("#CreateNewSubAccountDialog--environmentsCombo-labelText").html("Cloud Foundry")
#CreateNewSubAccountDialog--environmentsCombo
environmentsComboInput = driver.find_element_by_id('CreateNewSubAccountDialog--environmentsCombo')
#environmentsComboInput.send_keys('Cloud Foundry')
environmentsComboInput.click()
environmentsComboSelect = driver.find_element_by_id('__item7-CreateNewSubAccountDialog--environmentsCombo-1')
environmentsComboSelect.click()
#time.sleep(1)
try:
WebDriverWait(driver,10).until(cond.visibility_of_element_located((By.ID, "CreateNewSubAccountDialog--providersCombo")))
except (ElementNotVisibleException) as py_ex:
print("Element not visible.")
print (py_ex)
print (py_ex.args)
driver.get_screenshot_as_file('/root/app/pages/' + 'page05.png')
#CreateNewSubAccountDialog--providersCombo-hiddenInput
#$("#CreateNewSubAccountDialog--providersCombo-hiddenInput").tap();
#$("#__item8-CreateNewSubAccountDialog--providersCombo-0").tap(); # Amazon Web Services(AWS)
#$("#CreateNewSubAccountDialog--providersCombo-labelText").html("Amazon Web Services (AWS)")
providersComboInput = driver.find_element_by_id('CreateNewSubAccountDialog--providersCombo')
providersComboInput.click()
try:
WebDriverWait(driver,10).until(cond.visibility_of_element_located((By.ID, "__item8-CreateNewSubAccountDialog--providersCombo-0")))
except (ElementNotVisibleException) as py_ex:
print("Element not visible.")
print (py_ex)
print (py_ex.args)
providersComboSelect = driver.find_element_by_id('__item8-CreateNewSubAccountDialog--providersCombo-0')
providersComboSelect.click()
try:
WebDriverWait(driver,10).until(cond.visibility_of_element_located((By.ID, "CreateNewSubAccountDialog--regionsCombo")))
except (ElementNotVisibleException) as py_ex:
print("Element not visible.")
print (py_ex)
print (py_ex.args)
driver.get_screenshot_as_file('/root/app/pages/' + 'page06.png')
##Amazon Web Services (AWS)
#providersComboInput.send_keys('Amazon Web Services (AWS)')
##CreateNewSubAccountDialog--regionsCombo-hiddenInput
#$("#CreateNewSubAccountDialog--regionsCombo-hiddenInput").tap();
#$("#__item9-CreateNewSubAccountDialog--regionsCombo-6").tap();
time.sleep(1)
regionsComboInput = driver.find_element_by_id('CreateNewSubAccountDialog--regionsCombo')
regionsComboInput.click()
try:
WebDriverWait(driver,10).until(cond.visibility_of_element_located((By.ID, "__item9-CreateNewSubAccountDialog--regionsCombo-6")))
except (ElementNotVisibleException) as py_ex:
print("Element not visible.")
print (py_ex)
print (py_ex.args)
regionsComboSelect = driver.find_element_by_id('__item9-CreateNewSubAccountDialog--regionsCombo-6')
regionsComboSelect.click()
try:
WebDriverWait(driver,10).until(cond.visibility_of_element_located((By.ID, "CreateNewSubAccountDialog--subdomain-inner")))
except (ElementNotVisibleException) as py_ex:
print("Element not visible.")
print (py_ex)
print (py_ex.args)
driver.get_screenshot_as_file('/root/app/pages/' + 'page07.png')
##US East (VA)
#regionsComboInput.send_keys('US East (VA)')
##CreateNewSubAccountDialog--subdomain-inner
subdomain = driver.find_element_by_id('CreateNewSubAccountDialog--subdomain-inner')
#$("#CreateNewSubAccountDialog--subdomain-inner").val("abcheadless");
subdomain.send_keys('xyzheadless')
##CreateNewSubAccountDialog--betaEnabledCF-CB
#$("#CreateNewSubAccountDialog--betaEnabledCF-CB").tap();
#$("#CreateNewSubAccountDialog--subdomain-inner").focus();
#$("#CreateNewSubAccountDialog--subdomain-inner").next().next().next().focus();
#displayName.sendKeys(Keys.TAB)
subdomain.send_keys(Keys.TAB)
#try:
# WebDriverWait(driver,20).until(cond.visibility_of_element_located((By.ID, "CreateNewSubAccountDialog--betaEnabledCF-CB")))
#except (ElementNotVisibleException) as py_ex:
# print("Element not visible.")
# print (py_ex)
# print (py_ex.args)
driver.get_screenshot_as_file('/root/app/pages/' + 'page08.png')
#betaEnabledCF = driver.find_element_by_id('CreateNewSubAccountDialog--betaEnabledCF-CB')
#betaEnabledCF.click()
#try:
# WebDriverWait(driver,5).until(cond.visibility_of_element_located((By.ID, "__button11")))
#except (ElementNotVisibleException) as py_ex:
# print("Element not visible.")
# print (py_ex)
# print (py_ex.args)
##__button11 #Create Button
#$("#__button11").tap();
#__button24
#$("#__button24").mouseup();
createButton = driver.find_element_by_id('__button11')
#time.sleep(1)
createButton.click()
time.sleep(1)
try:
WebDriverWait(driver,2).until(cond.visibility_of_element_located((By.ID, "CreateNewSubAccountDialog--errorStrip")))
output += "subDomain taken!"
except (ElementNotVisibleException) as py_ex:
output += "subDomain is OK!"
driver.get_screenshot_as_file('/root/app/pages/' + 'page09.png')
##__popover8
#doneMessage = driver.find_element_by_id('__popover8')
#time.sleep(1)
##time.sleep(1)
#driver.get_screenshot_as_file('/root/app/pages/' + 'page09.png')
driver.quit()
except:
import traceback;traceback.print_exc()
output += "</body>\n"
output += "</html>\n"
output += '\n'
return Response(output, mimetype='text/html' , status=200,)
@app.route('/headless/pagelist')
def headless_page_list():
return send_from_directory('/root/app/pages', 'index.html', mimetype='text/html')
@app.route('/headless/pages')
def headless_pages():
return send_from_directory('/root/app/pages', 'page' + request.args.get('page') + '.png', mimetype='image/png')
@app.route('/headless/post', methods=['POST'])
def unauth_post():
output = 'Python Post to DB (Dangerous!). \n'
output += '\n'
output += 'Receiving module should check that it came from our approuter and verify or abort if otherwise.\n'
output += '\n'
content = request.json
output += content
return Response(output, mimetype='application/json' , status=201,)
@app.route('/headless/set_env')
def set_pyenv():
output = '\n Set Environment variable... \n'
if request.args.get('PATHS_FROM_ECLIPSE_TO_PYTHON'):
output += request.args.get('PATHS_FROM_ECLIPSE_TO_PYTHON')
os.environ["PATHS_FROM_ECLIPSE_TO_PYTHON"] = request.args.get('PATHS_FROM_ECLIPSE_TO_PYTHON')
output += '\n'
output += 'Eclipse paths set for debugging.\n'
output += '\n'
output += '\n'
return Response(output, mimetype='text/plain' , status=200,)
@app.route('/headless/env')
def dump_pyenv():
output = '\n Key Environment variables... \n'
output += 'PYTHONHOME: ' + str(os.getenv("PYTHONHOME", 0)) + '\n'
output += 'PYTHONPATH: ' + str(os.getenv("PYTHONPATH", 0)) + '\n'
output += 'PATHS_FROM_ECLIPSE_TO_PYTHON: ' + str(os.getenv("PATHS_FROM_ECLIPSE_TO_PYTHON", 0)) + '\n'
jsonok = json.loads(os.environ.get('PATHS_FROM_ECLIPSE_TO_PYTHON', '[]'))
if jsonok:
output += "JSON is OK" + '\n'
tuples = [tuple(x) for x in jsonok]
else:
output += "JSON is NOT OK" + '\n'
output += 'VCAP_SERVICES: ' + str(os.getenv("VCAP_SERVICES", 0)) + '\n'
output += 'host: ' + hana.credentials['host'] + '\n'
output += 'port: ' + hana.credentials['port'] + '\n'
output += 'user: ' + hana.credentials['user'] + '\n'
output += 'pass: ' + hana.credentials['password'] + '\n'
output += '\n'
return output
@app.route('/headless/attach')
def do_attach():
output = '\n Attaching to debugger... \n'
attach(5678,"localhost")
output += '\n Set some breakpoints...\n'
return output
# If there is a request for a python/test2, return Testing message and then check JWT and connect to the data service and retrieve some data
@app.route('/headless/db_only')
def unauth_db_only():
output = 'Python UnAuthorized DB Only. \n'
#Enable to trigger debugging
os.environ["PATHS_FROM_ECLIPSE_TO_PYTHON"] = "[['/Users/i830671/git/mta_python_dev_env/python','/home/vcap/app']]"
output += '\n'
output += 'Receiving module should check that it came from our approuter and verify or abort if otherwise.\n'
output += '\n'
svcs_json = str(os.getenv("VCAP_SERVICES", 0))
svcs = json.loads(svcs_json)
schema = hana.credentials['schema']
host = hana.credentials['host']
port = hana.credentials['port']
user = hana.credentials['user']
password = hana.credentials['password']
# The certificate will available for HANA service instances that require an encrypted connection
# Note: This was tested to work with python hdbcli-2.3.112 tar.gz package not hdbcli-2.3.14 provided in XS_PYTHON00_0-70003433.ZIP
if 'certificate' in hana.credentials:
haascert = hana.credentials['certificate']
output += 'schema: ' + schema + '\n'
output += 'host: ' + host + '\n'
output += 'port: ' + port + '\n'
output += 'user: ' + user + '\n'
output += 'pass: ' + password + '\n'
# # Connect to the python HANA DB driver using the connection info
# User for HANA as a Service instances
if 'certificate' in hana.credentials:
connection = dbapi.connect(
address=host,
port=int(port),
user=user,
password=password,
currentSchema=schema,
encrypt="true",
sslValidateCertificate="true",
sslCryptoProvider="openssl",
sslTrustStore=haascert
)
else:
connection = dbapi.connect(
address=host,
port=int(port),
user=user,
password=password,
currentSchema=schema
)
# # Prep a cursor for SQL execution
cursor = connection.cursor()
# # Form an SQL statement to retrieve some data
cursor.execute('SELECT "tempId", "tempVal", "ts", "created" FROM "data::sensors.temp"')
# # Execute the SQL and capture the result set
sensor_vals = cursor.fetchall()
#
# # Loop through the result set and output
for sensor_val in sensor_vals:
output += 'sensor_val: ' + str(sensor_val[1]) + ' at: ' + str(sensor_val[2]) + '\n'
#
# # Close the DB connection
connection.close()
#
# Return the results
return output
# If there is a request for a python/test2, return Testing message and then check JWT and connect to the data service and retrieve some data
@app.route('/headless/admin/authcheck')
def auth_check():
output = 'Python Authorized DB Validated Request. \n'
output += '\n'
output += 'Receiving module should check that it came from our approuter and verify or abort if otherwise.\n'
output += '\n'
svcs_json = str(os.getenv("VCAP_SERVICES", 0))
svcs = json.loads(svcs_json)
uaa_service = env.get_service(label='xsuaa').credentials
access_token = request.headers.get('authorization')[7:]
security_context = xssec.create_security_context(access_token, uaa_service)
isAuthorized = security_context.check_scope('openid')
if not isAuthorized:
abort(403)
output += 'get_logon_name: ' + security_context.get_logon_name() + '\n'
# output += 'get_given_name: ' + security_context.get_given_name() + '\n'
# output += 'get_family_name: ' + security_context.get_family_name() + '\n'
output += 'get_email: ' + security_context.get_email() + '\n'
output += 'get_subdomain: ' + security_context.get_subdomain() + '\n'
# output += 'get_clientid: ' + security_context.get_clientid() + '\n'
output += 'get_identity_zone: ' + security_context.get_identity_zone() + '\n'
output += 'get_grant_type: ' + security_context.get_grant_type() + '\n'
return output
if __name__ == '__main__':
# Run the app, listening on all IPs with our chosen port number
# Use this for production
#app.run(host='0.0.0.0', port=port)
# Use this for debugging
app.run(debug=True, host='0.0.0.0', port=port)