-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsslo_o365_update.py
1437 lines (1213 loc) · 72.8 KB
/
sslo_o365_update.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
#!/bin/python
# -*- coding: utf-8 -*-
# O365 URL/IP update automation for BIG-IP
version = "7.2.1"
# Last Modified: June 2021
# Update author: Kevin Stewart, Sr. SSA F5 Networks
# Contributors: Regan Anderson, Brett Smith, F5 Networks
# Original author: Makoto Omura, F5 Networks Japan G.K.
#
# >>> NOTE: THIS VERSION OF THE OFFICE 365 SCRIPT IS SUPPORTED BY SSL ORCHESTRATOR 5.0 OR HIGHER <<<
#
# Updated for SSL Orchestrator by Kevin Stewart, SSA, F5 Networks
# Update 20210601 - to support additional enhancements (by Kevin Stewart)
# - Updated to class-based Python script
# - Updated to support --config (serialized JSON string input) and --configfile (JSON file) install options
# - Updated to support --full_uninstall option to delete all configurations (local files, URL categories, datagroups)
# - Updated to support --printconfig option to show the running configuration (JSON)
# - Updated to support using system proxy settings (System : Configuration : Device : Upstream Proxy)
# - Updated to support /etc/cron.d/0hourly scheduler (replaces iCall periodic) for more granular m/d/Y HH:mm scheduling
# - Updated to support more comprehensive config input validation
# Update 20210119 - added support for explicit proxy gateway if required for Internet access (by Kevin Stewart)
# Update 20201104 - resolved URL category format issue (by Kevin Stewart)
# Update 20201008 - to support additional enhancements (by Kevin Stewart)
# - Updated to support HA isolation mode (both peers perform updates and do not an trigger out-of-sync)
# - Updated to resolve issue if multiple versions of configuration iFile exists (takes latest)
# - Updated to include --force option to force a manual update (irrespective of config force_o365_record_refresh value)
# Update 20200925 - to support additional enhancements (by Kevin Stewart)
# - Updated to support O365 optimize/allow/default categories as separate outputs
# - Updated to support options to output to URL categories and/or URL data groups
# - Updated to support included URLs
# - Updated to support configuration stored in iFile
# - Updated to include install|uninstall functions
# Update 20200207 - to support additional enhancements (by Kevin Stewart)
# - Updated VERSION check routine to extract desired customer endpoint URI (previously was static "Worldwide")
# - Updated to remove excluded_urls and excluded_ips
# Update 20200130 - to support the following new functionality (by Regan Anderson)
# - Changed default working directory to /shared/o365/
# - Removed deprecated Yammer service area
# - Added ability to import only "Required" records (and set as default)
# - Added URL and IP exclusion lists to manually exclude entries from being imported
# - Adjusted URL category records to support HTTP schemes in SSLOv7 (http:// and https://)
# - Added external data group support for URLs
# - Added external data group import function for IPv4/IPv6
#
# To install:
# - Download the script onto the F5 BIG-IP:
# curl -k https://raw.githubusercontent.com/kevingstewart/sslo_o365_update/main/sslo_o365_update.py -o sslo_o365_update.py
#
# - Run the script with the --install option
# python sslo_o365_update.py --install
#
# - Optionally add a custom configuration (JSON) via the following options (with --install). See json_config_data variable below for defaults.
# - Load the default configuration -> python sslo_o365_update.py --install
# - Load configuration from serialized JSON string -> python sslo_o365_update.py --install --config <${JSON_STRING}>
# - Load configuration from JSON file -> python sslo_o365_update.py --install --configfile <config.json>
#
# To modify the configuration.
# - Initiate the --install process again with a new --config or --configfile argument
#
# To uninstall:
# - Two options:
# - Run the script with the --uninstall option. This will remove the running configuration.
# - Run the script with the --full_uninstall option. This will remove the running configuration, URL categories, and datagroups.
#
# The installed script creates a working directory (default: /shared/o365), a configuration (iFile) json file, and scheduler.
#
# The configuration json file controls the various settings of the script. See json_config_data variable below for defaults.
#
# Microsoft Web Service Customer endpoints (ENABLE ONLY ONE ENDPOINT)
# These are the set of URLs defined by customer endpoints as described here: https://docs.microsoft.com/en-us/office365/enterprise/urls-and-ip-address-ranges
# Valid values are [Worldwide, USGovDoD, USGovGCCHigh, China, Germany]
# "endpoint": "Worldwide"
#
# O365 "SeviceArea" (O365 endpoints) to consume, as described here: https://docs.microsoft.com/en-us/office365/enterprise/urls-and-ip-address-ranges
# "service_areas":
# "common": true|false -> Microsoft 365 Common and Office Online"
# "exchange": true|false -> Exchange Online
# "sharepoint": true|false -> SharePoint Online and OneDrive for Business
# "skype": true|false -> Skype for Business Online and Microsoft Teams
#
# O365 Record objects to create
# "outputs":
# "url_categories": true|false -> Create URL categories
# "url_datagroups": true|false -> Create URL data groups
# "ip4_datagroups": true|false -> Create IPv4 data groups
# "ip6_datagroups": true|false -> Create IPv6 data groups
#
# O365 Category creation, create a single URL data set, and/or separate data sets for O365 Optimize/Default/Allow categories
# "o365_categories":
# "all": true|false -> Create a single date set containing all URLs (all categories)
# "optimize": true|false -> Create a data set containing O365 Optimize category URLs (note that the optimized URLs are in Exchange and SharePoint service areas)
# "default": true|false -> Create a data set containing O365 Allow category URLs
# "allow": true|false -> Create a data set containing O365 Default category URLs
#
# O365 Endpoints to import - O365 required endpoints or all endpoints
# WARNING: "import all" includes non-O365 URLs that one may not want to bypass (ex. www.youtube.com)
# "only_required": true|false -> false=import all URLs, true=Office 365 required only URLs
#
# Excluded URLs (URL pattern matching is supported)
# Provide URLs in list format - ex. ["m.facebook.com", ".itunes.apple.com", "bit.ly"]
# "excluded_urls": []
#
# Included URLs (URL must be exact match to URL as it exists in JSON record - pattern matching not supported)
# Provide URLs in list format - ex. ["m.facebook.com", ".itunes.apple.com", "bit.ly"]
# "included_urls": []
#
# Excluded IPs (IP must be exact match to IP as it exists in JSON record - IP/CIDR mask cannot be modified)
# Provide IPs in list format - ex. ["191.234.140.0/22", "2620:1ec:a92::152/128"]
# "excluded_ips": []
#
# "system":
# "log_level": 1 -> 0=none, 1=normal, 2=verbose
# "working_directory":"/shared/o365" -> Working directory for running configuration files.
#
# "schedule":
# "periods":"monthly|weekly" -> When to trigger updates (monthly or weekly) -- default(monthly)
# "run_date":0 -> Day of week or day of month to run the script, as controlled by /etc/cron.d/0hourly -- default(0 Sunday)
# "run_time":"04:00" -> 24-hour time to run the script, as controlled by /etc/cron.d/0hourly -- default("04:00")
# "start_date":"" -> Standard "m/d/Y" date format to control when script is allowed to run
# "start_time":"" -> Standard 24-hour "HH:mm" time format to control when script is allowed to run
#
#
# This Sample Software provided by the author is for illustrative
# purposes only which provides customers with programming information
# regarding the products. This software is supplied "AS IS" without any
# warranties and support.
#
# The author(s) assume no responsibility or liability for the use of the
# software, conveys no license or title under any patent, copyright, or
# mask work right to the product.
#
# The author(s) reserve the right to make changes in the software without
# notification. The author also make no representation or warranty that
# such application will be suitable for the specified use without
# further testing or modification.
#-----------------------------------------------------------------------
import urllib2, fnmatch, uuid, os, re, json, commands, datetime, sys, argparse, copy, base64, fileinput
#-----------------------------------------------------------------------
# Default JSON configuration
#-----------------------------------------------------------------------
json_config_data = {
"endpoint": "Worldwide",
"service_areas": {
"common": True,
"exchange": True,
"sharepoint": True,
"skype": True
},
"outputs": {
"url_categories": True,
"url_datagroups": True,
"ip4_datagroups": True,
"ip6_datagroups": True
},
"o365_categories": {
"all": True,
"optimize": True,
"default": True,
"allow": True
},
"only_required": True,
"excluded_urls": [
".symcd.com",
".symcb.com",
".entrust.net",
".digicert.com",
".identrust.com",
".verisign.net",
".globalsign.net",
".globalsign.com",
".geotrust.com",
".omniroot.com",
".letsencrypt.org",
".public-trust.com",
"platform.linkedin.com"
],
"included_urls": [],
"excluded_ips": [],
"system": {
"log_level": 1,
"working_directory": "/shared/o365"
},
"schedule":{
"periods":"monthly",
"run_date":1,
"run_time":"04:00",
"start_date":"",
"start_time":""
}
}
#-----------------------------------------------------------------------
# System Options - Modify only when necessary
#-----------------------------------------------------------------------
# O365 custom URL category names
o365_category = "Office_365_Managed_All"
o365_category_optimized = "Office_365_Managed_Optimized"
o365_category_default = "Office_365_Managed_Default"
o365_category_allow = "Office_365_Managed_Allow"
# O365 data group names
dg_name_urls = "O365_URLs"
dg_name_ipv4 = "O365_IPv4"
dg_name_ipv6 = "O365_IPv6"
o365_dg = "Office_365_Managed_All"
o365_dg_optimize = "Office_365_Managed_Optimized"
o365_dg_default = "Office_365_Managed_Default"
o365_dg_allow = "Office_365_Managed_Allow"
o365_dg_ipv4 = "Office_365_Managed_IPv4"
o365_dg_ipv6 = "Office_365_Managed_IPv6"
# Microsoft Web Service URLs
url_ms_o365_endpoints = "endpoints.office.com"
url_ms_o365_version = "endpoints.office.com"
uri_ms_o365_version = "/version?ClientRequestId="
# Log file destination
log_dest_file = "/var/log/o365_update"
class o365UrlManagement:
# Init function (set local variables)
def __init__(self):
self.customer_endpoint = ""
self.service_areas_common = ""
self.service_areas_exchange = ""
self.service_areas_sharepoint = ""
self.service_areas_skype = ""
self.output_url_categories = ""
self.output_url_datagroups = ""
self.output_ip4_datagroups = ""
self.output_ip6_datagroups = ""
self.o365_categories_all = ""
self.o365_categories_optimize = ""
self.o365_categories_default = ""
self.o365_categories_allow = ""
self.only_required = ""
self.excluded_urls = ""
self.included_urls = ""
self.excluded_ips = ""
self.log_level = ""
self.work_directory = ""
self.json_config = ""
self.json_config_file = ""
self.force_update = False
self.config_data = ""
# Logging function
def log(self, lev, log_lev, msg):
if int(log_lev) >= int(lev):
log_string = "{0:%Y-%m-%d %H:%M:%S}".format(datetime.datetime.now()) + " " + msg + "\n"
f = open(log_dest_file, "a")
f.write(log_string)
f.flush()
f.close()
return
# Show help function
def show_help(self):
print("Office 365 URL Management Script. Version: " + version)
print("\nCommand line options for this application are:\n")
print("--help -> Show this help message and exit.")
print("--install -> Install the script.")
print("--uninstall -> Uninstall the script.")
print("--full_uninstall -> Uninstall the script. Remove everything.")
print("--force -> Force an update.\n")
print("--config CONFIG -> Used with --install. Provide alternate JSON configuration information from a serialized JSON string object.")
print("--config_file CONFIG_FILE -> Used with --install. Provide alternate JSON configuration information from a JSON file.\n")
print("--printconfig -> Show the running configuration.\n")
print("Examples:")
print("Install with default configuration -> python " + os.path.basename(__file__) + " --install")
print("Install with serialized JSON configuration -> python " + os.path.basename(__file__) + " --install --config ${json_string}")
print("Install with JSON file: -> python " + os.path.basename(__file__) + " --install --configfile file.json")
print("Install and force immediate URL update -> python " + os.path.basename(__file__) + " --install --force")
print("Force an update -> python " + os.path.basename(__file__) + " --force")
print("Uninstall but keep categories/datagroups -> python " + os.path.basename(__file__) + " --uninstall")
print("Uninstall and remove categories/datagroups -> python " + os.path.basename(__file__) + " --full_uninstall\n\n")
sys.exit(0)
# Get config function
def get_config(self):
try:
# Find all versions of the configuration iFile
o365_config = ""
entry_array = []
fileList = os.listdir('/config/filestore/files_d/Common_d/ifile_d/')
pattern = "*o365_config.json*"
for entry in fileList:
if fnmatch.fnmatch(entry, pattern):
entry_array.append("/config/filestore/files_d/Common_d/ifile_d/" + entry)
## Find the latest version of the configuration iFile
if entry_array:
o365_config = max(entry_array, key=os.path.getctime)
if o365_config == "":
print("\nIt appears this script has not been installed yet. Aborting.\nTo install this script, issue the command \"" + os.path.basename(__file__) + " --install\"\n")
self.show_help()
try:
f = open(o365_config, "r")
f_content = f.read()
f.close()
self.config_data = json.loads(f_content)
# Read configuration parameters from the json config
self.customer_endpoint = self.config_data["endpoint"]
self.service_area_common = self.config_data["service_areas"]["common"]
self.service_area_exchange = self.config_data["service_areas"]["exchange"]
self.service_area_sharepoint = self.config_data["service_areas"]["sharepoint"]
self.service_area_skype = self.config_data["service_areas"]["skype"]
self.output_url_categories = self.config_data["outputs"]["url_categories"]
self.output_url_datagroups = self.config_data["outputs"]["url_datagroups"]
self.output_ip4_datagroups = self.config_data["outputs"]["ip4_datagroups"]
self.output_ip6_datagroups = self.config_data["outputs"]["ip6_datagroups"]
self.o365_categories_all = self.config_data["o365_categories"]["all"]
self.o365_categories_optimize = self.config_data["o365_categories"]["optimize"]
self.o365_categories_default = self.config_data["o365_categories"]["default"]
self.o365_categories_allow = self.config_data["o365_categories"]["allow"]
self.only_required = self.config_data["only_required"]
self.excluded_urls = self.config_data["excluded_urls"]
self.included_urls = self.config_data["included_urls"]
self.excluded_ips = self.config_data["excluded_ips"]
self.log_level = self.config_data["system"]["log_level"]
self.work_directory = self.config_data["system"]["working_directory"]
self.schedule_periods = self.config_data["schedule"]["periods"]
self.schedule_run_date = self.config_data["schedule"]["run_date"]
self.schedule_run_time = self.config_data["schedule"]["run_time"]
self.schedule_start_date = self.config_data["schedule"]["start_date"]
self.schedule_start_time = self.config_data["schedule"]["start_time"]
except:
print("\nIt appears the JSON configuration file is either missing or corrupt. Aborting.\nRun the script again with the --install option to repair.")
self.show_help()
except:
print("\nIt appears this script has not been installed yet. Aborting.\nTo install this script, issue the command \"" + os.path.basename(__file__) + " --install\"\n")
self.show_help()
# Show running configuration function
def print_config(self):
self.get_config()
this_json = json.dumps(self.config_data, indent = 4)
print(this_json)
sys.exit(1)
# JSON update function. Update imported json data to include required attributes and/or defaults if keys are omitted
def update_json(self, jsonstr):
json_data = copy.deepcopy(json_config_data)
## endpoint
if "endpoint" in jsonstr:
json_data["endpoint"] = jsonstr["endpoint"]
## input validation: ensure value is one of: Worldwide, USGovDoD, USGovGCCHigh, China, or Germany
if json_data["endpoint"] not in {"Worldwide", "USGovDoD", "USGovGCCHigh", "China", "Germany"}:
raise Exception('Endpoint value must be one of: \"Worldwide\", \"USGovDoD\", \"USGovGCCHigh\", \"China\", or \"Germany\". [1007]')
sys.exit(0)
## service_areas
if "service_areas" in jsonstr:
## service_areas:common
if "common" in jsonstr["service_areas"]:
json_data["service_areas"]["common"] = jsonstr["service_areas"]["common"]
## input validation: ensure value is boolean
if type(json_data["service_areas"]["common"]) != bool:
raise Exception('Service Areas "common" value must be a Boolean True or False. [1006]')
sys.exit(0)
else:
## default True
json_data["service_areas"]["common"] = True
## service_areas:exchange
if "exchange" in jsonstr["service_areas"]:
json_data["service_areas"]["exchange"] = jsonstr["service_areas"]["exchange"]
## input validation: ensure value is boolean
if type(json_data["service_areas"]["exchange"]) != bool:
raise Exception('Service Areas "exchange" value must be a Boolean True or False. [1006]')
sys.exit(0)
else:
## default False
json_data["service_areas"]["exchange"] = False
## service_areas:sharepoint
if "sharepoint" in jsonstr["service_areas"]:
json_data["service_areas"]["sharepoint"] = jsonstr["service_areas"]["sharepoint"]
## input validation: ensure value is boolean
if type(json_data["service_areas"]["sharepoint"]) != bool:
raise Exception('Service Areas "sharepoint" value must be a Boolean True or False. [1006]')
sys.exit(0)
else:
## default False
json_data["service_areas"]["sharepoint"] = False
## service_areas:skype
if "skype" in jsonstr["service_areas"]:
json_data["service_areas"]["skype"] = jsonstr["service_areas"]["skype"]
## input validation: ensure value is boolean
if type(json_data["service_areas"]["skype"]) != bool:
raise Exception('Service Areas "skype" value must be a Boolean True or False. [1008]')
sys.exit(0)
else:
## default False
json_data["service_areas"]["skype"] = False
else:
## no service_areas block defined, set defaults
json_data["service_areas"]["common"] = True
json_data["service_areas"]["exchange"] = False
json_data["service_areas"]["sharepoint"] = False
json_data["service_areas"]["skype"] = False
## outputs
if "outputs" in jsonstr:
## outputs:url_categories
if "url_categories" in jsonstr["outputs"]:
json_data["outputs"]["url_categories"] = jsonstr["outputs"]["url_categories"]
## input validation: ensure value is boolean
if type(json_data["outputs"]["url_categories"]) != bool:
raise Exception('Outputs "url_categories" value must be a Boolean True or False. [1009]')
sys.exit(0)
else:
## default True
json_data["outputs"]["url_categories"] = True
## outputs:url_datagroups
if "url_datagroups" in jsonstr["outputs"]:
json_data["outputs"]["url_datagroups"] = jsonstr["outputs"]["url_datagroups"]
## input validation: ensure value is boolean
if type(json_data["outputs"]["url_datagroups"]) != bool:
raise Exception('Outputs "url_datagroups" value must be a Boolean True or False. [1010]')
sys.exit(0)
else:
## default False
json_data["outputs"]["url_datagroups"] = False
## outputs:ip4_categories
if "ip4_categories" in jsonstr["outputs"]:
json_data["outputs"]["ip4_categories"] = jsonstr["outputs"]["ip4_categories"]
## input validation: ensure value is boolean
if type(json_data["outputs"]["ip4_categories"]) != bool:
raise Exception('Outputs "ip4_categories" value must be a Boolean True or False. [1011]')
sys.exit(0)
else:
## default False
json_data["outputs"]["ip4_datagroups"] = False
## outputs:ip6_categories
if "ip6_categories" in jsonstr["outputs"]:
json_data["outputs"]["ip6_categories"] = jsonstr["outputs"]["ip6_categories"]
## input validation: ensure value is boolean
if type(json_data["outputs"]["ip6_categories"]) != bool:
raise Exception('Outputs "ip6_categories" value must be a Boolean True or False. [1012]')
sys.exit(0)
else:
## default False
json_data["outputs"]["ip6_datagroups"] = False
else:
## no outputs block defined, set defaults
json_data["outputs"]["url_categories"] = True
json_data["outputs"]["url_datagroups"] = False
json_data["outputs"]["ip4_datagroups"] = False
json_data["outputs"]["ip6_datagroups"] = False
## o365_categories
if "o365_categories" in jsonstr:
## o365_categories:all
if "all" in jsonstr["o365_categories"]:
json_data["o365_categories"]["all"] = jsonstr["o365_categories"]["all"]
## input validation: ensure value is boolean
if type(json_data["o365_categories"]["all"]) != bool:
raise Exception('O365 Categories "all" value must be a Boolean True or False. [1013]')
sys.exit(0)
else:
## default True
json_data["o365_categories"]["all"] = True
## o365_categories:optimize
if "optimize" in jsonstr["o365_categories"]:
json_data["o365_categories"]["optimize"] = jsonstr["o365_categories"]["optimize"]
## input validation: ensure value is boolean
if type(json_data["o365_categories"]["optimize"]) != bool:
raise Exception('O365 Categories "optimize" value must be a Boolean True or False. [1014]')
sys.exit(0)
else:
## default False
json_data["o365_categories"]["optimize"] = False
## o365_categories:default
if "default" in jsonstr["o365_categories"]:
json_data["o365_categories"]["default"] = jsonstr["o365_categories"]["default"]
## input validation: ensure value is boolean
if type(json_data["o365_categories"]["default"]) != bool:
raise Exception('O365 Categories "default" value must be a Boolean True or False. [1015]')
sys.exit(0)
else:
## default False
json_data["o365_categories"]["default"] = False
## o365_categories:allow
if "allow" in jsonstr["o365_categories"]:
json_data["o365_categories"]["allow"] = jsonstr["o365_categories"]["allow"]
## input validation: ensure value is boolean
if type(json_data["o365_categories"]["allow"]) != bool:
raise Exception('O365 Categories "allow" value must be a Boolean True or False. [1016]')
sys.exit(0)
else:
## default False
json_data["o365_categories"]["allow"] = False
else:
## no o365_categories block defined, set defaults
json_data["o365_categories"]["all"] = True
json_data["o365_categories"]["optimize"] = False
json_data["o365_categories"]["default"] = False
json_data["o365_categories"]["allow"] = False
## only_required
if "only_required" in jsonstr:
json_data["only_required"] = jsonstr["only_required"]
## input validation: ensure value is boolean
if type(json_data["only_required"]) != bool:
raise Exception('The "only_required" value must be a Boolean True or False. [1017]')
sys.exit(0)
else:
## default True
json_data["only_required"] = True
## excluded_urls
if "excluded_urls" in jsonstr:
json_data["excluded_urls"] = jsonstr["excluded_urls"]
else:
## default []
json_data["excluded_urls"] = []
## included_urls
if "included_urls" in jsonstr:
json_data["included_urls"] = jsonstr["included_urls"]
else:
## default []
json_data["included_urls"] = []
## excluded_ips
if "excluded_ips" in jsonstr:
json_data["excluded_ips"] = jsonstr["excluded_ips"]
else:
## default []
json_data["excluded_ips"] = []
## system
if "system" in jsonstr:
## system:log_level
if "log_level" in jsonstr["system"]:
json_data["system"]["log_level"] = jsonstr["system"]["log_level"]
## input validation: ensure value is an integer
if type(json_data["system"]["log_level"]) != int:
raise Exception('The System "log level" value must be an integer between 0 and 2. [1018]')
sys.exit(0)
## input validation: ensure value is an integer between 0 and 2
if json_data["system"]["log_level"] < 0 or json_data["system"]["log_level"] > 2:
raise Exception('The System "log level" value must be an integer between 0 and 2. [1019]')
sys.exit(0)
else:
## default 1
json_data["system"]["log_level"] = 1
## system:working_directory
if "working_directory" in jsonstr["system"]:
json_data["system"]["working_directory"] = jsonstr["system"]["working_directory"]
else:
## default /shared/o365
json_data["system"]["working_directory"] = "/shared/o365"
else:
## no system block defined, set defaults
json_data["system"]["log_level"] = 1
json_data["system"]["working_directory"] = "/shared/o365"
## schedule
if "schedule" in jsonstr:
## schedule:periods
if "periods" in jsonstr["schedule"]:
json_data["schedule"]["periods"] = jsonstr["schedule"]["periods"]
## input validation: ensure value is one of: monthly, weekly
if json_data["schedule"]["periods"] not in {"monthly", "weekly"}:
raise Exception('The Schedule "periods" value must be one of: \"monthly\" or \"weekly\". [1020]')
sys.exit(0)
else:
## default monthly
json_data["schedule"]["periods"] = "monthly"
## schedule:run_date
if "run_date" in jsonstr["schedule"]:
json_data["schedule"]["run_date"] = jsonstr["schedule"]["run_date"]
if json_data["schedule"]["run_date"] == "":
## value empty, set default 1
json_data["schedule"]["run_date"] = 1
## input validation: validate weekly/monthly values
elif json_data["schedule"]["periods"] == "monthly":
## input validation: ensure run_date is an integer
if type(json_data["schedule"]["run_date"]) != int:
raise Exception('Schedule "run_date" value for period(monthly) must be an integer between 1 and 31. [1004]')
sys.exit(0)
## input validation: ensure monthly run_date is a value between 1 and 31
if json_data["schedule"]["run_date"] <= 0 or json_data["schedule"]["run_date"] > 31:
raise Exception('Schedule "run_date" value for period(monthly) must be an integer between 1 and 31. [1005]')
sys.exit(0)
elif json_data["schedule"]["periods"] == "weekly":
## input validation: ensure run_date is an integer
if type(json_data["schedule"]["run_date"]) != int:
raise Exception('Schedule "run_date" value for period(weekly) must be an integer between 0 (Sunday) and 6 (Saturday). [1002]')
sys.exit(0)
## input validation: ensure weekly run_date is a value between 1 and 7
if json_data["schedule"]["run_date"] < 0 or json_data["schedule"]["run_date"] > 6:
raise Exception('Schedule "run_date" value for period(weekly) must be an integer between 0 (Sunday) and 6 (Saturday). [1003]')
sys.exit(0)
else:
## default 1
json_data["schedule"]["run_date"] = 1
## schedule:run_time
if "run_time" in jsonstr["schedule"]:
json_data["schedule"]["run_time"] = jsonstr["schedule"]["run_time"]
if json_data["schedule"]["run_time"] == "":
## value empty, set default "04:00"
json_data["schedule"]["run_time"] = "04:00"
## input validation: ensure correct time format
elif not re.match(r"[0-9]{1,2}:[0-9]{2}", json_data["schedule"]["run_time"]):
raise Exception('Schedule "run_time" value must be a valid 24-hour time (ex. 14:30). [1021]')
sys.exit(0)
## input validation: ensure first number (hour) between 0 and 23, and second number (minutes) between 0 and 59
this_time = json_data["schedule"]["run_time"].split(":")
if int(this_time[0]) < 0 or int(this_time[0]) > 23:
raise Exception('Schedule "run_time" hour value must be a valid 24-hour integer between 0 and 23. [1022]')
sys.exit(0)
if int(this_time[1]) < 0 or int(this_time[1]) > 59:
raise Exception('Schedule "run_time" minute value must be a valid integer between 0 and 59. [1022]')
sys.exit(0)
else:
## default "04:00"
json_data["schedule"]["run_time"] = "04:00"
## schedule:start_date
if "start_date" in jsonstr["schedule"]:
json_data["schedule"]["start_date"] = jsonstr["schedule"]["start_date"]
if json_data["schedule"]["start_date"] == "":
pass
## input validation: ensure correct m/d/Y format
elif not re.match(r"[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}", json_data["schedule"]["start_date"]):
raise Exception('Schedule "start_date" value must be in month/day/year format (ex. 03/29/2021). [1023]')
sys.exit(0)
else:
## default ""
json_data["schedule"]["start_date"] = ""
## schedule:start_time
if "start_time" in jsonstr["schedule"]:
json_data["schedule"]["start_time"] = jsonstr["schedule"]["start_time"]
if json_data["schedule"]["start_time"] == "":
pass
## input validation: ensure correct time format
elif not re.match(r"[0-9]{1,2}:[0-9]{2}", json_data["schedule"]["start_time"]):
raise Exception('Schedule "start_time" value must be a valid 24-hour time (ex. 14:30). [1021]')
sys.exit(0)
## input validation: ensure first number (hour) between 0 and 23, and second number (minutes) between 0 and 59
else:
this_time = json_data["schedule"]["start_time"].split(":")
if int(this_time[0]) < 0 or int(this_time[0]) > 23:
raise Exception('Schedule "start_time" hour value must be a valid 24-hour integer between 0 and 23. [1022]')
sys.exit(0)
if int(this_time[1]) < 0 or int(this_time[1]) > 59:
raise Exception('Schedule "start_time" minute value must be a valid integer between 0 and 59. [1022]')
sys.exit(0)
else:
## default ""
json_data["schedule"]["start_time"] = ""
else:
## no schedule block defined, set defaults
json_data["schedule"]["periods"] = "monthly"
json_data["schedule"]["run_date"] = 1
json_data["schedule"]["run_time"] = "04:00"
json_data["schedule"]["start_date"] = ""
json_data["schedule"]["start_time"] = ""
return(json_data)
# Create URL categories function
def create_url_categories (self, url_file, url_list, version_latest):
# Initialize the url string
str_urls_to_bypass = ""
# Create new or clean out existing URL category - add the latest version as first entry
result = commands.getoutput("tmsh list sys application service o365_update.app/o365_update")
if "was not found" in result:
result2 = commands.getoutput("tmsh create sys application service o365_update traffic-group traffic-group-local-only device-group none")
self.log(2, self.log_level, "Application service not found. Creating o365_update.app/o365_update")
result = commands.getoutput("tmsh list sys url-db url-category o365_update.app/" + url_file)
if "was not found" in result:
result2 = commands.getoutput("tmsh create /sys url-db url-category " + url_file + " display-name " + url_file + " app-service o365_update.app/o365_update urls replace-all-with { https://" + version_latest + "/ { type exact-match } } default-action allow")
self.log(2, self.log_level, "O365 custom URL category (" + url_file + ") not found. Created new O365 custom category.")
else:
result2 = commands.getoutput("tmsh modify /sys url-db url-category " + url_file + " app-service o365_update.app/o365_update urls replace-all-with { https://" + version_latest + "/ { type exact-match } }")
self.log(2, self.log_level, "O365 custom URL category (" + url_file + ") exists. Clearing entries for new data.")
# Loop through URLs and insert into URL category
for url in url_list:
# Force URL to lower case
url = url.lower()
# If URL starts with an asterisk, set as a glob-match URL, otherwise exact-match. Send to a string.
if ('*' in url):
# Escaping any asterisk characters
url_processed = re.sub('\*', '\\*', url)
str_urls_to_bypass = str_urls_to_bypass + " urls add { \"https://" + url_processed + "/\" { type glob-match } } urls add { \"http://" + url_processed + "/\" { type glob-match } }"
else:
str_urls_to_bypass = str_urls_to_bypass + " urls add { \"https://" + url + "/\" { type exact-match } } urls add { \"http://" + url + "/\" { type exact-match } }"
# Import the URL entries
result = commands.getoutput("tmsh modify /sys url-db url-category " + url_file + " app-service o365_update.app/o365_update" + str_urls_to_bypass)
# Create URL datagroups function
def create_url_datagroups (self, url_file, url_list):
# Write data to a file for import into data group
fout = open(self.work_directory + "/" + url_file, 'w')
for url in (list(sorted(set(url_list)))):
# Replace any asterisk characters with a dot
url_processed = re.sub('\*', '', url)
fout.write("\"" + str(url_processed.lower()) + "\" := \"\",\n")
fout.flush()
fout.close()
# Create URL data group files in TMSH if they don't already exist
result = commands.getoutput("tmsh list sys application service o365_update.app/o365_update")
if "was not found" in result:
result2 = commands.getoutput("tmsh create sys application service o365_update traffic-group traffic-group-local-only device-group none")
self.log(2, self.log_level, "Application service not found. Creating o365_update.app/o365_update")
result = commands.getoutput("tmsh list /sys file data-group o365_update.app/" + url_file)
if "was not found" in result:
# Create (sys) external data group
result2 = commands.getoutput("tmsh create /sys file data-group o365_update.app/" + url_file + " separator \":=\" source-path file:" + self.work_directory + "/" + url_file + " type string")
# Create (ltm) link to external data group
result3 = commands.getoutput("tmsh create /ltm data-group external o365_update.app/" + url_file + " external-file-name o365_update.app/" + url_file)
self.log(2, self.log_level, "O365 URL data group (" + url_file + ") not found. Created new data group.")
else:
# Update (sys) external data group
result2 = commands.getoutput("tmsh modify /sys file data-group o365_update.app/" + url_file + " source-path file:" + self.work_directory + "/" + url_file)
# Update (ltm) link to external data group
result3 = commands.getoutput("tmsh create /ltm data-group external o365_update.app/" + url_file + " external-file-name o365_update.app/" + url_file)
self.log(2, self.log_level, "O365 URL data group (" + url_file + ") exists. Updated existing data group.")
os.remove(self.work_directory + "/" + url_file)
# Create IP datagroups function
def create_ip_datagroups (self, url_file, url_list):
# Write data to a file for import into data group
fout = open(self.work_directory + "/" + url_file, 'w')
for ip in (list(sorted(url_list))):
fout.write("network " + str(ip) + ",\n")
fout.flush()
fout.close()
# Create URL data group files in TMSH if they don't already exist
result = commands.getoutput("tmsh list sys application service o365_update.app/o365_update")
if "was not found" in result:
result2 = commands.getoutput("tmsh create sys application service o365_update traffic-group traffic-group-local-only device-group none")
self.log(2, self.log_level, "Application service not found. Creating o365_update.app/o365_update")
result = commands.getoutput("tmsh list /sys file data-group o365_update.app/" + url_file)
if "was not found" in result:
result2 = commands.getoutput("tmsh create /sys file data-group o365_update.app/" + url_file + " source-path file:" + self.work_directory + "/" + url_file + " type ip")
result3 = commands.getoutput("tmsh create /ltm data-group external o365_update.app/" + url_file + " external-file-name o365_update.app/" + url_file)
self.log(2, self.log_level, "O365 IPv4 data group (" + url_file + ") not found. Created new data group.")
else:
result2 = commands.getoutput("tmsh modify /sys file data-group o365_update.app/" + url_file + " source-path file:" + self.work_directory + "/" + url_file)
result3 = commands.getoutput("tmsh create /ltm data-group external o365_update.app/" + url_file + " external-file-name o365_update.app/" + url_file)
self.log(2, self.log_level, "O365 IPv4 data group (" + url_file + ") exists. Updated existing data group.")
os.remove(self.work_directory + "/" + url_file)
# Main work function. Fetches O365 URLs and updates URL categories and datagroups
def update_o365(self):
list_urls_to_bypass = []
list_optimized_urls_to_bypass = []
list_default_urls_to_bypass = []
list_allow_urls_to_bypass = []
list_ipv4_to_pbr = []
list_ipv6_to_pbr = []
self.get_config()
if self.work_directory != "":
# -----------------------------------------------------------------------
# Scheduling:
# - /etc/cron.d/0hourly manages the frequency (periods, run_date, run_time)
# - Make sure here that current date/time >= start_data, start_time
# - If no start_date/start_time defined, just proceed
# -----------------------------------------------------------------------
if self.schedule_start_date != "":
## start_date/start_time defined - process datetime logic
if self.schedule_start_time == "":
self.schedule_start_time != "00:00"
## extract day, month, year, hour, minute from start_date and start_time
inmonth, inday, inyear = [int(x) for x in self.schedule_start_date.split('/')]
inhour, inminute = [int(x) for x in self.schedule_start_time.split(':')]
## define start and current datetimes
start = datetime.datetime(inyear, inmonth, inday, inhour, inminute, 0)
present = datetime.datetime.now()
## if start is > current datetime, do not proceed
if start > present:
self.log(1, self.log_level, "Defined start date/time greater than current time - Aborting.")
sys.exit()
# -----------------------------------------------------------------------
# System Proxy Detection (System : Configuration : Devices : Upstream Proxy)
# -----------------------------------------------------------------------
result = commands.getoutput("tmsh list sys management-proxy-config proxy-ip-addr proxy-port")
if result != "":
for line in result.split('\n'):
if "proxy-ip-addr" in line:
self.proxyip = line.strip().split()[1]
if "proxy-port" in line:
self.proxyport = line.strip().split()[1]
else:
self.proxyip = None
self.proxyport = None
# -----------------------------------------------------------------------
# GUID management
# -----------------------------------------------------------------------
# Create the guid file if it doesn't exist
if not os.path.isdir(self.work_directory):
os.mkdir(self.work_directory)
self.log(1, self.log_level, "Created work directory " + self.work_directory + " because it did not exist.")
if not os.path.exists(self.work_directory + "/guid.txt"):
f = open(self.work_directory + "/guid.txt", "w")
f.write("\n")
f.flush()
f.close()
self.log(1, self.log_level, "Created GUID file " + self.work_directory + "/guid.txt because it did not exist.")
# Read guid from file and validate. Create one if not existent
f = open(self.work_directory + "/guid.txt", "r")
f_content = f.readline()
f.close()
if re.match('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}', f_content):
guid = f_content
self.log(2, self.log_level, "Valid GUID is read from local file " + self.work_directory + "/guid.txt.")
else:
guid = str(uuid.uuid4())
f = open(self.work_directory + "/guid.txt", "w")
f.write(guid)
f.flush()
f.close()
self.log(1, self.log_level, "Generated a new GUID, and saved it to " + self.work_directory + "/guid.txt.")
# -----------------------------------------------------------------------
# O365 endpoints list version check
# -----------------------------------------------------------------------
# Ensure that a local version exists
if os.path.isfile(self.work_directory + "/o365_version.txt"):
f = open(self.work_directory + "/o365_version.txt", "r")
f_content = f.readline()
f.close()
# Check if the VERSION record format is valid
if re.match('[0-9]{10}', f_content):
ms_o365_version_previous = f_content
self.log(2, self.log_level, "Valid previous VERSION found in " + self.work_directory + "/o365_version.txt.")
else:
ms_o365_version_previous = "1970010200"
f = open(self.work_directory + "/o365_version.txt", "w")
f.write(ms_o365_version_previous)
f.flush()
f.close()
self.log(1, self.log_level, "Valid previous VERSION was not found. Wrote dummy value in " + self.work_directory + "/o365_version.txt.")
else:
ms_o365_version_previous = "1970010200"
f = open(self.work_directory + "/o365_version.txt", "w")
f.write(ms_o365_version_previous)
f.flush()
f.close()
self.log(1, self.log_level, "Valid previous VERSION was not found. Wrote dummy value in " + self.work_directory + "/o365_version.txt.")
# -----------------------------------------------------------------------
# O365 endpoints list VERSION check
# -----------------------------------------------------------------------
# Read the version of previously received records. If different than stored information, then data is assumed new/changed
request_string = uri_ms_o365_version + guid
req_string = "https://" + url_ms_o365_version + request_string
try:
if self.proxyip != None:
localproxy = 'http://' + self.proxyip + ':' + self.proxyport
proxyctl = urllib2.ProxyHandler({'https': localproxy})
opener = urllib2.build_opener(proxyctl)
urllib2.install_opener(opener)
res = urllib2.urlopen(req_string)
else:
res = urllib2.urlopen(req_string)
except urllib2.URLError, e:
self.log(1, self.log_level, "ERROR: Request to fetch O365 information failed. Aborting. (1): " + str(e.reason))
sys.exit(0)
except:
self.log(1, self.log_level, "ERROR: Request to fetch O365 information failed. Aborting. (2): " + str(e.reason))
sys.exit(0)
if res.getcode() != 200:
# MS O365 version request failed - abort
self.log(1, self.log_level, "ERROR: VERSION request to MS web service failed. Aborting.")
sys.exit(0)
else:
# MS O365 version request succeeded
self.log(2, self.log_level, "VERSION request to MS web service was successful.")
dict_o365_version = json.loads(res.read())
ms_o365_version_latest = ""
for record in dict_o365_version:
if record.has_key('instance'):
if record["instance"] == self.customer_endpoint and record.has_key("latest"):
latest = record["latest"]
if re.match('[0-9]{10}', latest):
ms_o365_version_latest = latest
f = open(self.work_directory + "/o365_version.txt", "w")
f.write(ms_o365_version_latest)
f.flush()
f.close()
self.log(2, self.log_level, "Previous VERSION is " + ms_o365_version_previous)
self.log(2, self.log_level, "Latest VERSION is " + ms_o365_version_latest)
if self.force_update:
self.log(1, self.log_level, "Command called with \"--force\" option. Manual update initiated.")
pass
elif ms_o365_version_latest == ms_o365_version_previous:
self.log(1, self.log_level, "You already have the latest MS O365 URL/IP Address list: " + ms_o365_version_latest + ". Aborting.")
sys.exit(0)
# -----------------------------------------------------------------------
# Request O365 endpoints list & put it in dictionaries
# -----------------------------------------------------------------------
# Make the request to fetch JSON data from Microsoft
request_string = "/endpoints/" + self.customer_endpoint + "?ClientRequestId=" + guid
req_string = "https://" + url_ms_o365_endpoints + request_string
try:
if self.proxyip != None:
localproxy = 'http://' + self.proxyip + ':' + self.proxyport