Skip to content

Commit 8289539

Browse files
committed
move cli code into bin/ directory, to eliminate issues with embedding cbapi into Splunk app
1 parent b82b2df commit 8289539

File tree

6 files changed

+291
-304
lines changed

6 files changed

+291
-304
lines changed

bin/cbapi-defense

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,93 @@
11
#!/usr/bin/env python
22

3-
from cbapi.defense.cli import main
3+
import argparse
4+
import contextlib
5+
6+
from cbapi.six import iteritems
7+
from cbapi.six.moves import input
8+
import os
49
import sys
10+
import cbapi.six as six
11+
if six.PY3:
12+
from io import StringIO as StringIO
13+
else:
14+
from cStringIO import StringIO
15+
16+
from cbapi.six.moves.configparser import RawConfigParser
17+
18+
19+
@contextlib.contextmanager
20+
def temp_umask(umask):
21+
oldmask = os.umask(umask)
22+
try:
23+
yield
24+
finally:
25+
os.umask(oldmask)
26+
27+
28+
def configure(opts):
29+
credential_path = os.path.join(os.path.expanduser("~"), ".carbonblack")
30+
credential_file = os.path.join(credential_path, "credentials.defense")
31+
32+
print("Welcome to the CbAPI.")
33+
if os.path.exists(credential_file):
34+
print("An existing credential file exists at {0}.".format(credential_file))
35+
resp = input("Do you want to continue and overwrite the existing configuration? [Y/N] ")
36+
if resp.strip().upper() != "Y":
37+
print("Exiting.")
38+
return 1
39+
40+
if not os.path.exists(credential_path):
41+
os.makedirs(credential_path, 0o700)
42+
43+
url = input("URL to the Cb Defense API server (do not include '/integrationServices') [https://hostname]: ")
44+
45+
ssl_verify = True
46+
47+
connector_id = input("Connector ID: ")
48+
token = input("API key: ")
49+
50+
config = RawConfigParser()
51+
config.readfp(StringIO('[default]'))
52+
config.set("default", "url", url)
53+
config.set("default", "token", "{0}/{1}".format(token, connector_id))
54+
config.set("default", "ssl_verify", ssl_verify)
55+
with temp_umask(0):
56+
with os.fdopen(os.open(credential_file, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0o600), 'w') as fp:
57+
os.chmod(credential_file, 0o600)
58+
config.write(fp)
59+
print("Successfully wrote credentials to {0}.".format(credential_file))
60+
61+
62+
command_map = {
63+
"configure": {
64+
"extra_args": {},
65+
"help": "Configure CbAPI",
66+
"method": configure
67+
}
68+
}
69+
70+
71+
def main(args):
72+
parser = argparse.ArgumentParser()
73+
commands = parser.add_subparsers(dest="command_name", help="CbAPI subcommand")
74+
75+
for cmd_name, cmd_config in iteritems(command_map):
76+
cmd_parser = commands.add_parser(cmd_name, help=cmd_config.get("help", None))
77+
for cmd_arg_name, cmd_arg_config in iteritems(cmd_config.get("extra_args", {})):
78+
cmd_parser.add_argument(cmd_arg_name, **cmd_arg_config)
79+
80+
opts = parser.parse_args(args)
81+
command = command_map.get(opts.command_name)
82+
if not command:
83+
parser.print_usage()
84+
return
85+
86+
command_method = command.get("method", None)
87+
if command_method:
88+
return command_method(opts)
89+
else:
90+
parser.print_usage()
591

692

793
if __name__ == '__main__':

bin/cbapi-protection

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,100 @@
11
#!/usr/bin/env python
22

3-
from cbapi.protection.cli import main
3+
import argparse
4+
import contextlib
5+
6+
from cbapi.six import iteritems
7+
from cbapi.six.moves import input
8+
import os
49
import sys
10+
import cbapi.six as six
11+
if six.PY3:
12+
from io import StringIO as StringIO
13+
else:
14+
from cStringIO import StringIO
15+
16+
from cbapi.six.moves.configparser import RawConfigParser
17+
18+
19+
@contextlib.contextmanager
20+
def temp_umask(umask):
21+
oldmask = os.umask(umask)
22+
try:
23+
yield
24+
finally:
25+
os.umask(oldmask)
26+
27+
28+
def configure(opts):
29+
credential_path = os.path.join(os.path.expanduser("~"), ".carbonblack")
30+
credential_file = os.path.join(credential_path, "credentials.protection")
31+
32+
print("Welcome to the CbAPI.")
33+
if os.path.exists(credential_file):
34+
print("An existing credential file exists at {0}.".format(credential_file))
35+
resp = input("Do you want to continue and overwrite the existing configuration? [Y/N] ")
36+
if resp.strip().upper() != "Y":
37+
print("Exiting.")
38+
return 1
39+
40+
if not os.path.exists(credential_path):
41+
os.makedirs(credential_path, 0o700)
42+
43+
url = input("URL to the Cb Protection server [https://hostname]: ")
44+
45+
ssl_verify = None
46+
while ssl_verify not in ["Y", "N"]:
47+
ssl_verify = input("Use SSL/TLS certificate validation (answer 'N' if using self-signed certs) [Y/N]: ")
48+
ssl_verify = ssl_verify.strip().upper()
49+
50+
if ssl_verify == "Y":
51+
ssl_verify = True
52+
else:
53+
ssl_verify = False
54+
55+
token = input("API token: ")
56+
57+
config = RawConfigParser()
58+
config.readfp(StringIO('[default]'))
59+
config.set("default", "url", url)
60+
config.set("default", "token", token)
61+
config.set("default", "ssl_verify", ssl_verify)
62+
with temp_umask(0):
63+
with os.fdopen(os.open(credential_file, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0o600), 'w') as fp:
64+
os.chmod(credential_file, 0o600)
65+
config.write(fp)
66+
print("Successfully wrote credentials to {0}.".format(credential_file))
67+
68+
69+
command_map = {
70+
"configure": {
71+
"extra_args": {},
72+
"help": "Configure CbAPI",
73+
"method": configure
74+
}
75+
}
76+
77+
78+
def main(args):
79+
parser = argparse.ArgumentParser()
80+
commands = parser.add_subparsers(dest="command_name", help="CbAPI subcommand")
81+
82+
for cmd_name, cmd_config in iteritems(command_map):
83+
cmd_parser = commands.add_parser(cmd_name, help=cmd_config.get("help", None))
84+
for cmd_arg_name, cmd_arg_config in iteritems(cmd_config.get("extra_args", {})):
85+
cmd_parser.add_argument(cmd_arg_name, **cmd_arg_config)
86+
87+
opts = parser.parse_args(args)
88+
command = command_map.get(opts.command_name)
89+
if not command:
90+
parser.print_usage()
91+
return
92+
93+
command_method = command.get("method", None)
94+
if command_method:
95+
return command_method(opts)
96+
else:
97+
parser.print_usage()
598

699

7100
if __name__ == '__main__':

bin/cbapi-response

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,116 @@
11
#!/usr/bin/env python
22

3-
from cbapi.response.cli import main
3+
import argparse
4+
import contextlib
5+
6+
from cbapi.six import iteritems
7+
from cbapi.six.moves import input
8+
import os
49
import sys
10+
import getpass
11+
import cbapi.six as six
12+
if six.PY3:
13+
from io import StringIO as StringIO
14+
else:
15+
from cStringIO import StringIO
16+
17+
from cbapi.response.rest_api import get_api_token
18+
from cbapi.six.moves.configparser import RawConfigParser
19+
20+
21+
@contextlib.contextmanager
22+
def temp_umask(umask):
23+
oldmask = os.umask(umask)
24+
try:
25+
yield
26+
finally:
27+
os.umask(oldmask)
28+
29+
30+
def configure(opts):
31+
credential_path = os.path.join(os.path.expanduser("~"), ".carbonblack")
32+
credential_file = os.path.join(credential_path, "credentials.response")
33+
34+
print("Welcome to the CbAPI.")
35+
if os.path.exists(credential_file):
36+
print("An existing credential file exists at {0}.".format(credential_file))
37+
resp = input("Do you want to continue and overwrite the existing configuration? [Y/N] ")
38+
if resp.strip().upper() != "Y":
39+
print("Exiting.")
40+
return 1
41+
42+
if not os.path.exists(credential_path):
43+
os.makedirs(credential_path, 0o700)
44+
45+
url = input("URL to the Cb Response server [https://hostname]: ")
46+
47+
ssl_verify = None
48+
while ssl_verify not in ["Y", "N"]:
49+
ssl_verify = input("Use SSL/TLS certificate validation (answer 'N' if using self-signed certs) [Y/N]: ")
50+
ssl_verify = ssl_verify.strip().upper()
51+
52+
if ssl_verify == "Y":
53+
ssl_verify = True
54+
else:
55+
ssl_verify = False
56+
57+
token = input("API token (if unknown, leave blank): ")
58+
if not token:
59+
print("No API token provided: we will look it up using your username & password")
60+
username = input("Username: ")
61+
password = getpass.getpass("Password: ")
62+
63+
print("Retrieving API token for {0}...".format(username))
64+
65+
try:
66+
token = get_api_token(url, username, password, verify=ssl_verify)
67+
except Exception as e:
68+
import traceback
69+
traceback.print_exc()
70+
print("Could not retrieve API token: {0}".format(str(e)))
71+
return 1
72+
73+
config = RawConfigParser()
74+
config.readfp(StringIO('[default]'))
75+
config.set("default", "url", url)
76+
config.set("default", "token", token)
77+
config.set("default", "ssl_verify", ssl_verify)
78+
with temp_umask(0):
79+
with os.fdopen(os.open(credential_file, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0o600), 'w') as fp:
80+
os.chmod(credential_file, 0o600)
81+
config.write(fp)
82+
print("Successfully wrote credentials to {0}.".format(credential_file))
83+
84+
85+
command_map = {
86+
"configure": {
87+
"extra_args": {},
88+
"help": "Configure CbAPI",
89+
"method": configure
90+
}
91+
}
92+
93+
94+
def main(args):
95+
parser = argparse.ArgumentParser()
96+
commands = parser.add_subparsers(dest="command_name", help="CbAPI subcommand")
97+
98+
for cmd_name, cmd_config in iteritems(command_map):
99+
cmd_parser = commands.add_parser(cmd_name, help=cmd_config.get("help", None))
100+
for cmd_arg_name, cmd_arg_config in iteritems(cmd_config.get("extra_args", {})):
101+
cmd_parser.add_argument(cmd_arg_name, **cmd_arg_config)
102+
103+
opts = parser.parse_args(args)
104+
command = command_map.get(opts.command_name)
105+
if not command:
106+
parser.print_usage()
107+
return
108+
109+
command_method = command.get("method", None)
110+
if command_method:
111+
return command_method(opts)
112+
else:
113+
parser.print_usage()
5114

6115

7116
if __name__ == '__main__':

0 commit comments

Comments
 (0)