-
Notifications
You must be signed in to change notification settings - Fork 0
/
gssapi-console.py
executable file
·99 lines (79 loc) · 3.1 KB
/
gssapi-console.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
#!/usr/bin/env python
# interactive console with easy access to krb5 test harness stuff
from __future__ import print_function
import argparse
import os
import sys
import copy
import logging
import pkg_resources as pkgres
from gssapi_console import GSSAPIConsole
parser = argparse.ArgumentParser(
description='An interactive Python console with krb5 setup')
parser.add_argument('file', metavar='FILE', nargs='?',
help='a file to run', default=None)
parser.add_argument('-i', default=False, dest='force_interactive',
action='store_const', const=True,
help='Force interactive mode when running with a file')
parser.add_argument('--realm-args', default=None,
help='A comma-separated list of key=(true|false) values '
'to pass to the realm constructor')
parser.add_argument('--mech', default='krb5',
help='Which environment to setup up '
'(supports krb5 [default])')
parser.add_argument('-a', '--attach', metavar='IDENTIFIER', nargs='?',
default=None, dest='attach',
help='Attach to an existing gssapi-console environment, '
'indicated by IDENTIFIER.')
parser.add_argument('-v' , '--verbose', default=False, action='store_const',
dest='verbose', const=True,
help='Use a more verbose logging level')
PARSED_ARGS = parser.parse_args()
if PARSED_ARGS.verbose:
logging.basicConfig(level=logging.DEBUG)
realm_args = {}
if PARSED_ARGS.realm_args:
for arg in PARSED_ARGS.realm_args.split(','):
key, raw_val = arg.split('=')
realm_args[key] = (raw_val.lower() == 'true')
try:
mech_cls_loader = next(
pkgres.iter_entry_points('gssapi_console.drivers',
name=PARSED_ARGS.mech))
except StopIteration:
print("The %s environment is not supported by the "
"GSSAPI console" % PARSED_ARGS.mech, file=sys.stderr)
print("If running from the source directory, you may need to "
"`pip install` first.", file=sys.stderr)
sys.exit(1)
mech_cls = mech_cls_loader.load()
SAVED_ENV = None
try:
console = None
# import the env
SAVED_ENV = copy.deepcopy(os.environ)
console = GSSAPIConsole(mech_cls, realm_args=realm_args, attach=PARSED_ARGS.attach)
for k, v in console.realm.env.items():
os.environ[k] = v
INTER = True
# run the interactive interpreter
if PARSED_ARGS.file is not None:
if not PARSED_ARGS.force_interactive:
INTER = False
print('Session: {0}'.format(console.session))
with open(PARSED_ARGS.file) as src:
console.runsource(src.read(), src.name, 'exec')
if INTER:
console.interact()
except (KeyboardInterrupt, EOFError):
pass
finally:
# restore the env
if SAVED_ENV is not None:
for k in copy.deepcopy(os.environ):
if k in SAVED_ENV:
os.environ[k] = SAVED_ENV[k]
else:
del os.environ[k]
if console is not None:
console.stop()