This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathgsa_admin.py
executable file
·1356 lines (1183 loc) · 49.4 KB
/
gsa_admin.py
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This code is not supported by Google
#
"""Classes for administering the Google Search Appliance.
Currently, the classes available can be used to import/export/rewrite
the config file. This module is designed to be expandable so that
other functionality can be easily included.
gsaConfig: Class for handling XML from GSA export/import.
gsaWebInterface: Class for interacting with GSA Web Interface.
Example usage:
1. Export the config file:
gsa_admin.py -n <host> --port 8000 -u admin -p <pw> -e --sign-password
hellohello -o ~/tmp/o.xml -v
2. Make a change to the config file and sign:
./gsa_admin.py -n <host> -u admin -p <pw> -s --sign-password hellohello
-f ~/tmp/o.xml -v -o ~/tmp/o2.xml
3. Import the new config file:
./gsa_admin.py -n <host> --port 8000 -u admin -p <pw> -i --sign-password
hellohello -f ~/tmp/o2.xml -v
Note that you must use the same password to sign a file that you used
when exporting. You will get an error when importing if you do not do
this.
4. Export all the URLs to a file:
./gsa_admin.py --hostname=<host> --username=admin
--password=<pw> --all_urls --output=/tmp/all_urls
5. Retrieve GSA^n (mirroring) status from the admin console
./gsa_admin.py -z -n <host> -u admin -p <pw>
6. Trigger database synchronization
./gsa_admin.py -n YOUR_GSA --port 8000 -u admin -p YOUR_PASSWORD --database_sync --sources=DB_NAME
7. Run custom support script provided by Google Support
./gsa_admin.py -n YOUR_GSA --port 8000 -u admin -p YOUR_PASSWORD -m -f ./sscript.txt -o ./out.txt -t 300
8. Pause crawl
./gsa_admin.py -n YOUR_GSA --port 8000 -u admin -p YOUR_PASSWORD --pause_crawl
9. Resume crawl
./gsa_admin.py -n YOUR_GSA --port 8000 -u admin -p YOUR_PASSWORD --resume_crawl
TODO(jlowry): add in functionality from adminconsole.py:
get crawl status, shutdown.
"""
__author__ = "[email protected] (Alastair McCormack)"
import cgi
import os.path
import logging
import sys
import xml.dom.minidom
import hashlib
import hmac
import json
import codecs
import urllib2
import urllib
import cookielib
import re
import time
import urlparse
from optparse import OptionParser, OptionGroup
# Required for utf-8 file compatibility
reload(sys)
sys.setdefaultencoding("utf-8")
del sys.setdefaultencoding
class NullHandler(logging.Handler):
def emit(self, record):
pass
DEFAULTLOGLEVEL=logging.DEBUG
log = logging.getLogger(__name__)
log.addHandler(NullHandler())
class gsaConfig:
"Google Search Appliance XML configuration tool"
configXMLString = None
def __init__(self, fileName=None):
if fileName:
self.openFile(fileName)
def __str__(self):
return self.configXMLString
def openFile(self, fileName):
"Read in file as string"
if not os.path.exists(fileName):
log.error("Input file does not exist")
sys.exit(1)
configXMLdoc = open(fileName)
self.configXMLString = configXMLdoc.read()
configXMLdoc.close()
def setXMLContents(self, xmlString):
"Sets the runtime XML contents"
self.configXMLString = xmlString.encode("utf-8")
#log.warning("Signature maybe invalid. Please verify before uploading or saving")
def getXMLContents(self):
"Returns the contents of the XML file"
return self.configXMLString.encode("utf-8")
def computeSignature(self, password):
configXMLString = self.getXMLContents()
# ugly removal of spaces because minidom cannot remove them automatically when removing a node
configXMLString = re.sub(' <uam_dir>', '<uam_dir>', configXMLString)
configXMLString = re.sub('</uam_dir>\n', '</uam_dir>', configXMLString)
doc = xml.dom.minidom.parseString(configXMLString)
# Remove <uam_dir> node because new GSAs expect so
uamdirNode = doc.getElementsByTagName("uam_dir").item(0)
uamdirNode.parentNode.removeChild(uamdirNode)
uardataNode = doc.getElementsByTagName("uar_data").item(0)
uardataB64contents = uardataNode.firstChild.nodeValue.strip()+'\n'
if uardataB64contents != "\n":
log.debug("UAR data contains data. Must be 7.0 or newer")
# replace <uar_data> node with "/tmp/tmp_uar_data_dir,hash"
# 1: Strip additional spaces at the end but we need the new line
# to compute hash.
# "AAAAAAAAAA==\n ]]></uar_data>" <-- 10 spaces
uardataHash = hmac.new(password, uardataB64contents, hashlib.sha1).hexdigest()
# 2: Replace to <dummy file name, hash> with additional whitespaces.
uardataNode.firstChild.nodeValue = ("\n/tmp/tmp_uar_data_dir,"
+ "%s\n ") % (''+uardataHash)
log.debug("uar_data is replaced to %s" % uardataNode.toxml())
# Get <config> node
configNode = doc.getElementsByTagName("config").item(0)
# get string of Node and children (as utf-8)
configNodeXML = configNode.toxml()
# Create new HMAC using user password and configXML as sum contents
return hmac.new(password, configNodeXML, hashlib.sha1).hexdigest()
def sign(self, password):
computedSignature=self.computeSignature(password)
configXMLString = self.getXMLContents()
doc = xml.dom.minidom.parseString(configXMLString)
# Get <signature> node
signatureNode = doc.getElementsByTagName("signature").item(0)
signatureCDATANode = signatureNode.firstChild
# Set CDATA/Text area to new HMAC
signatureCDATANode.nodeValue = computedSignature
self.setXMLContents(doc.toxml())
def writeFile(self, filename):
if os.path.exists(filename):
log.error("Output file exists")
sys.exit(1)
doc = xml.dom.minidom.parseString(self.configXMLString)
outputXMLFile = codecs.open(filename, 'w', "utf-8")
log.debug("Writing XML to %s" % filename)
# GSA newer than 6.? expects '<eef>' to be on the second line.
outputXMLFile.write(doc.toxml().replace("<eef>", "\n<eef>", 1))
def verifySignature(self, password):
computedSignature = self.computeSignature(password)
configXMLString = self.getXMLContents()
doc = xml.dom.minidom.parseString(configXMLString)
# Get <signature> node
signatureNode = doc.getElementsByTagName("signature").item(0)
signatureCDATANode = signatureNode.firstChild
signatureValue = signatureNode.firstChild.nodeValue
# signatureValue may contain whitespace and linefeeds so we'll just ensure that
# our HMAC is found within
if signatureValue.count(computedSignature) :
log.debug("Signature matches")
return 1
else:
log.debug("Signature does not match %s vs %s" %
(signatureValue, computedSignature))
return None
class gsaWebInterface:
"Google Search Appliance Web Interface Wrapper)"
baseURL = None
username = None
password = None
hostName = None
loggedIn = None
_url_opener = None
def __init__(self, hostName, username, password, port=8000, use_ssl=False):
protocol = 'https' if use_ssl else 'http'
self.baseURL = '%s://%s:%s/EnterpriseController' % (protocol, hostName, port)
self.hostName = hostName
self.username = username
self.password = password
log.debug("Using a base URL of '%s'" % self.baseURL)
# build cookie jar for this web instance only. Should allow for GSAs port mapped behind a reverse proxy.
cookieJar = cookielib.CookieJar()
self._url_opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookieJar))
def _openurl(self, request):
"""Args:
request: urllib2 request object or URL string.
"""
return self._url_opener.open(request)
def _encode_multipart_formdata(self, fields, files):
"""
fields: a sequence of (name, value) elements for regular form fields.
files: a sequence of (name, filename, value) elements for data to be uploaded as files
"""
BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = '\r\n'
lines = []
for (key, value) in fields:
lines.append('--' + BOUNDARY)
lines.append('Content-Disposition: form-data; name="%s"' % key)
lines.append('')
lines.append(value)
for (key, filename, value) in files:
lines.append('--' + BOUNDARY)
lines.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
lines.append('Content-Type: text/xml')
lines.append('')
lines.append(value)
lines.append('--' + BOUNDARY + '--')
lines.append('')
body = CRLF.join(lines)
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
def _login(self):
if not self.loggedIn:
log.debug("Fetching initial page for new cookie")
self._openurl(self.baseURL)
request = urllib2.Request(self.baseURL,
urllib.urlencode(
{'actionType' : 'authenticateUser',
# for 7.0 or older
'userName' : self.username,
'password' : self.password,
# for 7.2 and newer. Having both doesn't hurt
'reqObj' : json.dumps([None, self.username, self.password, None, 1]),
}))
log.debug("Logging in as %s..." % self.username)
result = self._openurl(request)
resultString = result.read()
# Pre 7.2 has "Google Search Appliance >Home"
# 7.2 and later returns JSON like object
home = re.compile("Google Search Appliance\s*>\s*Home")
home72 = re.compile('"xsrf": \[null,"security_token","')
if home.search(resultString):
log.debug("7.0 or older")
self.is72 = False
elif home72.search(resultString):
log.debug("7.2 or newer")
# The first line is junk to prevent some action on browsers: )]}',
# Just skip it.
response = json.loads(resultString[5:])
log.info("Security token is: " + response["xsrf"][2])
self.is72 = True
else:
log.error("Login failed: " + resultString)
sys.exit(2)
log.debug("Successfully logged in")
self.loggedIn = True
def _logout(self):
request = urllib2.Request(self.baseURL + "?" + urllib.urlencode({'actionType' : 'logout'}))
self._openurl(request)
self.loggedIn = False
def __del__(self):
self._logout()
def importConfig(self, gsaConfig, configPassword):
fields = [("actionType", "importExport"), ("passwordIn", configPassword),
("import", " Import Configuration ")]
files = [("importFileName", "config.xml", gsaConfig.getXMLContents() )]
content_type, body = self._encode_multipart_formdata(fields,files)
headers = {'User-Agent': 'python-urllib2', 'Content-Type': content_type}
self._login()
security_token = self.getSecurityToken('cache')
request = urllib2.Request(self.baseURL + "?" +
urllib.urlencode({'actionType': 'importExport',
'export': ' Import Configuration ',
'security_token' : security_token,
'a' : '1',
'passwordIn': configPassword}),
body, headers)
log.info("Sending XML...")
result = self._openurl(request)
content = result.read()
if content.count("Invalid file"):
log.error("Invalid configuration file")
sys.exit(2)
elif content.count("Wrong passphrase or the file is corrupt"):
log.error("Wrong passphrase or the file is corrupt. Try ")
sys.exit(2)
elif content.count("Passphrase should be at least 8 characters long"):
log.error("Passphrase should be at least 8 characters long")
sys.exit(2)
elif content.count("File does not exist"):
log.error("Configuration file does not exist")
sys.exit(2)
elif not content.count("Configuration imported successfully"):
log.error("Import failed")
sys.exit(2)
else:
log.info("Import successful")
def exportConfig(self, configPassword):
self._login()
security_token = self.getSecurityToken('cache')
request = urllib2.Request(self.baseURL + "?" +
urllib.urlencode({'actionType': 'importExport',
'export': ' Export Configuration ',
'security_token': security_token,
'a': '1',
'password1': configPassword,
'password2': configPassword}))
log.debug("Fetching config XML")
result = self._openurl(request)
content = result.read()
if content.count("Passphrase should be at least 8 characters long"):
log.error("Passphrase should be at least 8 characters long. You entered: '%s'" % (configPassword))
sys.exit(2)
gsac = gsaConfig()
log.debug("Returning gsaConfig object")
gsac.setXMLContents(content)
return gsac
def getSecurityTokenFromContents(self, content):
"""Gets the value of the security_token hidden form parameter.
Args:
content: a string containing HTML contents
Returns:
A long string, required as a parameter when submitting the form.
Returns an empty string if security_token does not exist.
"""
token_re = re.compile('name="security_token"[^>]*value="([^"]*)"', re.I)
match = token_re.search(content)
if match:
security_token = match.group(1)
log.debug('Security token is: %s' % (security_token))
return security_token
else:
return ""
def getSecurityToken(self, actionType):
"""Gets the value of the security_token hidden form parameter.
Args:
actionType: a string, used to fetch the Admin Console form.
Returns:
A long string, required as a parameter when submitting the form.
Returns an empty string if security_token does not exist.
"""
self._login()
# request needs to be a GET not POST
url = "%s?actionType=%s&a=1" % (self.baseURL, actionType)
log.debug('Fetching url: %s' % (url))
result = self._openurl(url)
content = result.read()
token_re = re.compile('name="security_token"[^>]*value="([^"]*)"', re.I)
match = token_re.search(content)
if match:
security_token = match.group(1)
log.debug('Security token is: %s' % (security_token))
return security_token
else:
return ""
def setAccessControl(self, maxhostload=10, urlCacheTimeout=3600):
# Tested on 6.8. Will not work on previous versions unless the form
# parameters are modified.
self._login()
security_token = self.getSecurityToken('cache')
# Sample body of a POST from a 6.8 machine:
# security_token=Vaup237Rd5jXE6ZC0Iy6BeVo4h0%3A1290533850660&
# actionType=cache&
# basicAuthChallengeType=auto&
# authzServiceUrl=&
# overallAuthzTimeout=20.0&
# requestBatchTimeout=5.0&
# singleRequestTimeout=2.5&
# maxHostload=10&
# urlCacheTimeout=3600&
# saveSettings=Save+Settings
request = urllib2.Request(self.baseURL,
urllib.urlencode({'security_token': security_token,
'a': '1',
'actionType': 'cache',
'basicAuthChallengeType': 'auto',
'authzServiceUrl': '',
'queryProcessingTime': '20.0',
'requestBatchTimeout': '5.0',
'singleRequestTimeout': '2.5',
'maxHostload': maxhostload,
'urlCacheTimeout': urlCacheTimeout,
'saveSettings': 'Save Settings'}))
result = self._openurl(request)
# Form submit did not work if content contains this string: "Forgot Your Password" or
# <font color="red"> unless multiple users are logged in.
# content = result.read()
# log.info(content)
def _unescape(self, s):
s = s.replace('&', '&')
return s
def syncDatabases(self, database_list):
"""Sync databases in the GSA.
Args:
database_list: a List of String, a list of database name to sync
"""
self._login()
for database in database_list:
log.info("Syncing %s ..." % database)
param = urllib.urlencode({"a": "1",
"actionType": "syncDatabase",
"entryName": database})
request = urllib2.Request(self.baseURL + "?" + param)
try:
result = self._openurl(request)
except:
log.error("Unable to sync %s properly" % database)
def pauseCrawl(self):
"""Pause crawl on the GSA.
Supports only 7.0 and higher.
Args:
None
"""
self._login()
security_token = self.getSecurityToken('crawlStatus')
log.info("Pausing crawl...")
param = urllib.urlencode({'security_token' : security_token,
'a' : '1',
'actionType' : 'crawlStatus',
'pauseCrawl' : 'Pause Crawl',
})
request = urllib2.Request(self.baseURL, param)
try:
result = self._openurl(request)
except:
log.error("Failed to pause crawl.")
def resumeCrawl(self):
"""Resume crawl on the GSA.
Supports only 7.0 and higher.
Args:
None
"""
self._login()
security_token = self.getSecurityToken('crawlStatus')
log.info("Resuming crawl...")
param = urllib.urlencode({'security_token' : security_token,
'a' : '1',
'actionType' : 'crawlStatus',
'resumeCrawl' : 'Resume Crawl',
})
request = urllib2.Request(self.baseURL, param)
try:
result = self._openurl(request)
except:
log.error("Failed to resume crawl.")
def recrawlPattern(self, collection, pattern):
""" Recrawl pattern
Args:
collection: string representing valid collection
pattern: string representing valid pattern
"""
self._login()
security_token = self.getSecurityToken('contentDiagnostics')
log.info("Recrawling collection %s with pattern %s " % (collection, pattern))
params = urllib.urlencode({'security_token' : security_token,
'a' : '1',
'actionType' : 'contentDiagnostics',
'resumeCrawl' : 'Resume Crawl',
'uriAt' : pattern,
'recrawlUrl' : 'false',
'recrawlAction' : 'recrawl',
'collection' : collection,
'recrawlThis' : "Recrawl this pattern"
})
request = urllib2.Request(self.baseURL, params)
try:
result = self._openurl(request)
except:
log.error("Failed to recrawl in collection: %s url: %s" % (collection,pattern))
def exportAllUrls(self, out):
"""Export the list of all URLs
Args:
out: a File, the file to write to.
"""
self._login()
security_token = self.getSecurityToken('exportAllUrls')
log.info("Generating the list of all URLs")
if self.is72:
param = urllib.urlencode({'security_token' : security_token,
'a' : '1',
'filterMode' : 'all_urls',
'goodURLs' : '',
'actionType' : 'exportAllUrls',
'exportAction' : 'generate',
'generate' : 'Generate the gzip file',
})
else:
param = urllib.urlencode({'actionType' : 'exportAllUrls',
'action' : 'generate',
'goodURLs' : '',
'security_token' : security_token,
'filterMode' : 'all_urls'})
request = urllib2.Request(self.baseURL, param)
try:
result = self._openurl(request)
seurity_token = self.getSecurityTokenFromContents(result.read())
#output = result.read()
#out.write(output)
except Exception, e:
log.error("Unable to generate the list of All URLs")
log.error(e)
while 1:
param = urllib.urlencode({'actionType' : 'exportAllUrls',
'security_token' : security_token,
'a' : '1'})
request = urllib2.Request(self.baseURL, param)
result = self._openurl(request)
if self.is72:
generating_msg = '<input type="submit" name="generate" id="generate" disabled class="hb-r-N nd-Ld-re" value="Generating...">'
else:
generating_msg = '<input type="submit" name="generate" id="generate" disabled value="Generating...">'
content = result.read()
security_token = self.getSecurityTokenFromContents(content)
if content.find(generating_msg) == -1:
log.info("The list has been generated.")
log.debug("content is " + content)
break
else:
log.info("Still generating the list. Sleep for 10 seconds...")
time.sleep(10)
# 7.0 or older default
exportActionStr = 'action'
if self.is72:
exportActionStr = 'exportAction'
log.info("Downloading the list of all URLs")
param = urllib.urlencode({'actionType' : 'exportAllUrls',
exportActionStr : 'download',
'security_token' : security_token,
'a' : '1'})
request = urllib2.Request(self.baseURL, param)
try:
result = self._openurl(request)
output = result.read()
out.write(output)
except Exception, e:
log.error("Unable to download the list")
log.error(e)
def exportKeymatches(self, frontend, out):
"""Export all keymatches for a frontend.
Args:
frontend: a String, the frontend name.
out: a File, the file to write to.
"""
self._login()
security_token = self.getSecurityToken('viewFrontends')
log.info("Retrieving the keymatch file for %s" % frontend)
param = urllib.urlencode({'actionType' : 'frontKeymatchImport',
'security_token' : security_token,
'a' : '1',
'frontend' : frontend,
'frontKeymatchExportNow': 'Export KeyMatches Now',
'startRow' : '1', 'search' : ''})
if self.is72:
request = urllib2.Request(self.baseURL, param)
else:
request = urllib2.Request(self.baseURL + "?" + param)
try:
result = self._openurl(request)
output = result.read()
out.write(output)
except Exception, e:
log.error("Unable to retrieve Keymatches for %s" % frontend)
log.error(e)
def exportSynonyms(self, frontend, out):
"""Export all Related Queries for a frontend.
Args:
frontend: a String, the frontend name.
out: a File, the file to write to.
"""
self._login()
security_token = self.getSecurityToken('viewFrontends')
log.info("Retrieving the Related Queries file for %s" % frontend)
param = urllib.urlencode({'actionType' : 'frontSynonymsImport',
'security_token' : security_token,
'a' : '1',
'frontend' : frontend,
'frontSynonymsExportNow': 'Export Related Queries Now',
'startRow' : '1', 'search' : ''})
if self.is72:
request = urllib2.Request(self.baseURL, param)
else:
request = urllib2.Request(self.baseURL + "?" + param)
try:
result = self._openurl(request)
output = result.read()
out.write(output)
except:
log.error("Unable to retrieve Related Queries for %s" % frontend)
def getAllUrls(self, out):
"""Retrieve all the URLs in the Crawl Diagnostics.
The URLs can be extracted from the crawl diagnostics URL with
actionType=contentStatus. For example, the URL in link
/EnterpriseController?actionType=contentStatus&...&uriAt=http%3A%2F%2Fwww.google.com%2F
is http://www.google.com/
We only follow crawl diagnostic URLs that contain actionType=contentStatus
"""
self._login()
log.debug("Retrieving URLs from Crawl Diagostics")
tocrawl = set([self.baseURL + '?actionType=contentDiagnostics&sort=crawled'])
crawled = set([])
doc_urls = set([])
href_regex = re.compile(r'<a href="(.*?)"')
while 1:
try:
log.debug('have %i links to crawl' % len(tocrawl))
crawling = tocrawl.pop()
log.debug('crawling %s' % crawling)
except KeyError:
raise StopIteration
url = urlparse.urlparse(crawling)
request = urllib2.Request(crawling)
try:
result = self._openurl(request)
except:
print 'unable to open url'
continue
content = result.read()
crawled.add(crawling)
links = href_regex.findall(content)
log.debug('found %i links' % len(links))
for link in (links.pop(0) for _ in xrange(len(links))):
log.debug('found a link: %s' % link)
if link.startswith('/'):
link = url[0] + '://' + url[1] + link
link = self._unescape(link)
if link not in crawled:
log.debug('this links has not been crawled')
if (link.find('actionType=contentDiagnostics') != -1 and
link.find('sort=excluded') == -1 and
link.find('sort=errors') == -1 and
link.find('view=excluded') == -1 and
link.find('view=successful') == -1 and
link.find('view=errors') == -1):
tocrawl.add(link)
#print 'add this link to my tocrawl list'
elif link.find('actionType=contentStatus') != -1:
# extract the document URL
doc_url = ''.join(cgi.parse_qs(urlparse.urlsplit(link)[3])['uriAt'])
if doc_url not in doc_urls:
out.write(doc_url + '\n')
doc_urls.add(doc_url)
if len(doc_urls) % 100 == 0:
print len(doc_urls)
else:
log.debug('we are not going to crawl this link %s' % link)
pass
else:
log.debug('already crawled %s' % link)
pass
else:
log.debug('we are not going to crawl this link %s' % link)
pass
def getStatus(self):
"""Get System Status and mirroring if enabled
The GSA sends out daily email but it doesn't inlcude mirroing status
Temporary solution -- only tested with 6.2.0.G.44
"""
self._login()
log.info("Retrieving GSA^n network diagnostics status from: %s", self.hostName)
request = urllib2.Request(self.baseURL + "?" +
urllib.urlencode({'a': 1,
'actionType': 'gsanDiagnostics'}))
result = self._openurl(request)
content = result.read()
nodes = re.findall("row.*(<b>.*</b>)", content)
if self.is72 and "nd-ue-re" in content:
print "This is 7.2 or newer. Just printing the whole output contents."
print content
return
if not nodes:
log.error("Could not find any replicas...\n%s" % content)
exit(3)
log.debug(nodes)
connStatus = re.findall("(green|red) button", content)
log.debug(connStatus)
numErrs = 0
for index, val in enumerate(connStatus):
if val == "green":
connStatus[index] = "OK"
else:
connStatus[index] = "ERROR - Test FAILED"
numErrs += 1
pos = 0
print "========================================="
for node in nodes:
print "Node: " + re.sub(r'<[^<]*?/?>', '', node)
print "Ping Status: " + connStatus[0+pos]
print "Stunnel Listener up: ", connStatus[1+pos]
print "Stunnel Connection: ", connStatus[2+pos]
print "PPP Connection Status: ", connStatus[3+pos]
print "Application Connection Status: ", connStatus[4+pos]
pos += 5
if pos < len(connStatus):
print "----------------------------------------"
print "========================================="
if numErrs:
print numErrs, "ERROR(s) detected. Please review mirroing status"
else:
print "All Tests passes successfully"
print "=========================================\n"
detailStats = re.search("Detailed Status(.*)\"Balls\">", content, re.DOTALL)
if detailStats:
#Check if this is primary node and display sync info
detailStats = re.sub(r'</td>\n<td ', ': <', detailStats.group(), re.DOTALL)
detailStats = re.sub(r'</td> <td ',' | <', detailStats, re.DOTALL)
detailStats = re.sub(r'<[^<]*?/?>', '', detailStats)
prettyStats = detailStats.split("\n")
for row in prettyStats:
cols = row.split(": ")
if len(cols) > 1:
cols[0] = cols[0] + ":" + " " * (40 - len(cols[0]))
print ''.join(cols)
print "==========================================================="
def getCollection (self,collection):
"""Get Collection statistics for daily processing."""
self._login()
log.debug("Retrieving GSA's collection information from: %s, collection name %s",
self.hostName, collection)
request = urllib2.Request(self.baseURL + "?" +
urllib.urlencode({'actionType': 'contentDiagnostics',
'sort': 'crawled',
'collection': collection}))
result = self._openurl(request)
content = result.read()
urlall = re.findall("view=all.>(.*)</a>",content)
urlsuccessful = re.findall("view=successful.>(.*)</a>",content)
urlerrors = re.findall("view=errors.>(.*)</a>",content)
urlexcluded = re.findall("view=excluded.>(.*)</a>",content)
numsurls = 0
numeurls = 0
for i in range(len(urlall)):
log.debug("URL %s Successful URLs %s Errored URLs %s ",urlall[i], urlsuccessful[i], urlerrors[i])
numsurls = numsurls + int(urlsuccessful[i].replace(",", ""))
numeurls = numeurls + int(urlerrors[i].replace(",", ""))
log.debug("Collection Totals: %s URLs, %s Error URLs", numsurls, numeurls)
# Here you can plug any type of MySQL logging method/etc
def runCusSscript(self, sscript_file, out_fd, timeout=180):
"""Run custom support script.
Args:
sscript_file: file containing the encrypted custom support script
out_fd: file descriptor for the output file
timeout: timeout value in secs to wait for support script to complete
"""
if not os.path.exists(sscript_file):
log.error("File %s does not exist", sscript_file)
sys.exit(1)
ssfd = open(sscript_file)
ss_str = ssfd.read()
ssfd.close()
self._login()
security_token = self.getSecurityToken('cache')
fields = [('security_token', security_token),
('actionType', 'supportScripts'),
('doaction', 'run'),
('scriptType', 'customFile'),
('run', 'Run support script'),
('a', '1')]
files = [('importFileName', 'cus_sscript_file', ss_str)]
content_type, body = self._encode_multipart_formdata(fields,files)
headers = {'User-Agent': 'python-urllib2', 'Content-Type': content_type}
request = urllib2.Request(self.baseURL, body, headers)
log.info("Submitting support script...")
result = self._openurl(request)
content = result.read()
if content.count("Support script submission failed"):
log.error("Support script submission failed")
sys.exit(2)
log.info("Support script submitted")
# support script submitted, check whether output is available
param = urllib.urlencode({"actionType": "supportScripts", "a": "1"})
request = urllib2.Request(self.baseURL + "?" + param)
tm = 0
sleeptime = 4
while True:
result = self._openurl(request)
content = result.read()
if not content.count("A support script is running"):
log.info("output is ready")
break
log.info("Support script still running...")
time.sleep(sleeptime)
tm += sleeptime
if tm > timeout:
log.error("Support script timed out")
sys.exit(1)
# support script run is done, download the output
param = urllib.urlencode({"actionType": "supportScripts",
"security_token": security_token,
"download": "Download results from previous run",
"doaction": "download"})
request = urllib2.Request(self.baseURL, param)
result = self._openurl(request)
content = result.read()
if content.count("Unable to download results"):
log.error("Unable to download results")
sys.exit(1)
elif content.count("Error when trying to retrieve support script output"):
log.error("Error when trying to retrieve support script output")
sys.exit(1)
out_fd.write(content)
###############################################################################
# MAIN
###############################################################################
if __name__ == "__main__":
log.setLevel(DEFAULTLOGLEVEL)
logStreamHandler = logging.StreamHandler(sys.stdout)
logStreamHandler.setFormatter(logging.Formatter("%(asctime)s %(levelname)5s %(name)s %(lineno)d: %(message)s"))
log.addHandler(logStreamHandler)
# Get command options
parser = OptionParser()
parser.add_option("-f", "--input-file", dest="inputFile",
help="Input XML file", metavar="FILE")
parser.add_option("-o", "--output", dest="outputFile",
help="Output file name", metavar="FILE")
parser.add_option("-g", "--sign-password", dest="signpassword",
help="Sign password for signing/import/export")
parser.add_option("-t", "--timeout", dest="timeout",
help="Timeout value (for Authz cache and support script)")
parser.add_option("--max-hostload", dest="maxhostload",
help="Value for max number of concurrent authz requests per server")
parser.add_option("--sources", dest="sources",
help="List of databases to sync (database1,database2,database3)")
parser.add_option("--frontend", dest="frontend",
help="Frontend used to export keymatches or related queries")
parser.add_option("--collection", dest="collection",
help="Collection name")
parser.add_option("--urlpattern", dest="urlpattern",
help="Url pattern for recrawl")
# actionsOptions
actionOptionsGrp = OptionGroup(parser, "Actions:")
actionOptionsGrp.add_option("-i", "--import", dest="actimport",
help="Import config file to GSA", action="store_true")
actionOptionsGrp.add_option("-e", "--export", dest="export",
help="Export GSA config file from GSA", action="store_true")
actionOptionsGrp.add_option("-s", "--sign", dest="sign", action="store_true",
help="Sign input XML file")
actionOptionsGrp.add_option("-r", "--verify", dest="verify", action="store_true",
help="Verify signature/HMAC in XML Config")
actionOptionsGrp.add_option("-a", "--set", dest="setaccesscontrol", action="store_true",
help="Set Access Control settings")
actionOptionsGrp.add_option("-l", "--all_urls", dest="all_urls",
help="Export all URLs from GSA using Crawl Diagnostics", action="store_true")
actionOptionsGrp.add_option("-L", "--export_all_urls", dest="export_all_urls",
help="Export All URLs using Export URLs", action="store_true")
actionOptionsGrp.add_option("-d" ,"--database_sync", dest="database_sync",
help="Sync databases", action="store_true")
actionOptionsGrp.add_option("" ,"--pause_crawl", dest="pause_crawl",
help="Pause crawl", action="store_true")
actionOptionsGrp.add_option("" ,"--resume_crawl", dest="resume_crawl",
help="Pause crawl", action="store_true")
actionOptionsGrp.add_option("-k" ,"--keymatches_export", dest="keymatches_export",
help="Export All Keymatches", action="store_true")
actionOptionsGrp.add_option("-y" ,"--synonyms_export", dest="synonyms_export",
help="Export All Related Queries", action="store_true")
actionOptionsGrp.add_option("-z", "--get-status", dest="getstatus",
action="store_true", help="Get GSA Status")
actionOptionsGrp.add_option("-c", "--get-collection-report", dest="getcollection",
action="store_true", help="Get GSA Collection Statistics")
actionOptionsGrp.add_option("-m", "--custom-sscript", dest="cus_sscript",
action="store_true", help="Run custom support script")
actionOptionsGrp.add_option("-R", "--recrawl", dest="recrawl_pattern",
action="store_true", help="Recrawl patern in collection")
parser.add_option_group(actionOptionsGrp)
# gsaHostOptions
gsaHostOptions = OptionGroup(parser, "GSA info")
gsaHostOptions.add_option("-n", "--hostname", dest="gsaHostName",
help="GSA hostname")
gsaHostOptions.add_option("--port", dest="port",
help="GSA port. Defaults to 8000", default="8000")