Skip to content

Commit 00e8d3f

Browse files
committed
add two examples to the defense folder
1 parent b04ab39 commit 00e8d3f

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

examples/defense/move_sensor.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
from cbapi.defense.models import Device
5+
from cbapi.example_helpers import build_cli_parser, get_cb_defense_object
6+
7+
8+
def main():
9+
parser = build_cli_parser("Move a device into a new security policy")
10+
device_options = parser.add_mutually_exclusive_group(required=True)
11+
device_options.add_argument("-i", "--id", type=int, help="Device ID of sensor to move")
12+
device_options.add_argument("-n", "--hostname", help="Hostname to move")
13+
14+
policy_options = parser.add_mutually_exclusive_group(required=True)
15+
policy_options.add_argument("--policyid", type=int, help="Policy ID")
16+
policy_options.add_argument("--policyname", help="Policy name")
17+
18+
args = parser.parse_args()
19+
cb = get_cb_defense_object(args)
20+
21+
if args.id:
22+
devices = [cb.select(Device, args.id)]
23+
else:
24+
devices = list(cb.select(Device).where("hostNameExact:{0}".format(args.hostname)))
25+
26+
for device in devices:
27+
if args.policyid:
28+
destpolicy = int(args.policyid)
29+
device.policyId = int(args.policyid)
30+
else:
31+
destpolicy = args.policyname
32+
device.policyName = args.policyname
33+
34+
device.save()
35+
print("Moved device id {0} (hostname {1}) into policy {2}".format(device.deviceId, device.name, destpolicy))
36+
37+
38+
if __name__ == "__main__":
39+
sys.exit(main())

examples/defense/notifications.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python
2+
3+
import sys
4+
from cbapi.example_helpers import build_cli_parser, get_cb_defense_object
5+
import json
6+
7+
8+
def main():
9+
parser = build_cli_parser("Listen to real-time notifications")
10+
parser.add_argument("-s", type=int, help="# of seconds to sleep between polls", default=30)
11+
12+
args = parser.parse_args()
13+
cb = get_cb_defense_object(args)
14+
15+
while True:
16+
for notification in cb.notification_listener(args.s):
17+
print(json.dumps(notification, indent=2))
18+
19+
20+
if __name__ == "__main__":
21+
sys.exit(main())

src/cbapi/example_helpers.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import argparse
22
from cbapi.response import CbEnterpriseResponseAPI
33
from cbapi.protection import CbEnterpriseProtectionAPI
4+
from cbapi.defense import CbDefenseAPI
45
import codecs
56
import sys
67

@@ -57,6 +58,21 @@ def get_cb_protection_object(args):
5758
return cb
5859

5960

61+
def get_cb_defense_object(args):
62+
if args.verbose:
63+
import logging
64+
logging.basicConfig()
65+
logging.getLogger("cbapi").setLevel(logging.DEBUG)
66+
logging.getLogger("__main__").setLevel(logging.DEBUG)
67+
68+
if args.cburl and args.apitoken:
69+
cb = CbDefenseAPI(url=args.cburl, token=args.apitoken, ssl_verify=(not args.no_ssl_verify))
70+
else:
71+
cb = CbDefenseAPI(profile=args.profile)
72+
73+
return cb
74+
75+
6076
def get_object_by_name_or_id(cb, cls, name_field="name", id=None, name=None, force_init=True):
6177
clsname = cls.__name__
6278
try:

0 commit comments

Comments
 (0)