Skip to content

Commit 622f4fd

Browse files
committed
Initial Commit
gssapi-console.py was extracted from python-gssapi and split into several different parts. A README was added, as well as a setup.py file which installs a runnable script.
0 parents  commit 622f4fd

File tree

10 files changed

+274
-0
lines changed

10 files changed

+274
-0
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
*.egg-info
2+
*.egg
3+
*~
4+
*.pyc
5+
/build/
6+
*.swp
7+
*.swo
8+
*.so
9+
.tox
10+
dist
11+
**/__pycache__
12+
.eggs
13+
.venv

LICENSE.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Copyright (c) 2015, The Python GSSAPI Team
2+
3+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
4+
5+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

MANIFEST.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
include *.txt
2+
include *.md

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
GSSAPI Console
2+
==============
3+
4+
*GSSAPI Console* allows provides an easy way to test out Python applications
5+
using GSSAPI in a self-contained environment.
6+
7+
By default, the console will set up a self-contained MIT krb5 environment.
8+
However, other GSSAPI environments are planned.
9+
10+
Requirements
11+
------------
12+
13+
* [python-gssapi](https://pypi.python.org/pypi/gssapi) >= 1.1.0
14+
* The required components for the environment in used
15+
(for krb5, the full set of MIT krb5 executables are needed,
16+
including those required to set up a KDC).
17+
18+
Usage
19+
-----
20+
21+
*[further information is available by running `gssapi-console.py --help`]*
22+
23+
The main executable of GSSAPI Console is `gssapi-console.py`. The basic
24+
invocation works just like launching `python` -- use `gssapi-console.py FILE`
25+
to run a file in the established environment, or just run `gssapi-console.py`
26+
or `gssapi-console.py -i` to get an interactive environment (the `-i` flag
27+
can be used with a file, just like `python FILE -i`).
28+
29+
You can use `--realm-args key1=true,key2=false,...` to pass specific arguments
30+
to the realm constructor (the set of such keys is driver-dependent), and
31+
`--mech MECH_NAME` can be used to specify a different driver (currently
32+
only `--mech krb5` will work).

gssapi-console.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env python
2+
3+
# interactive console with easy access to krb5 test harness stuff
4+
5+
import argparse
6+
import os
7+
import sys
8+
import copy
9+
10+
import pkg_resources as pkgres
11+
12+
from gssapi_console import GSSAPIConsole
13+
14+
15+
parser = argparse.ArgumentParser(
16+
description='An interactive Python console with krb5 setup')
17+
parser.add_argument('file', metavar='FILE', nargs='?',
18+
help='a file to run', default=None)
19+
parser.add_argument('-i', default=False, dest='force_interactive',
20+
action='store_const', const=True,
21+
help='Force interactive mode when running with a file')
22+
parser.add_argument('--realm-args', default=None,
23+
help='A comma-separated list of key=(true|false) values '
24+
'to pass to the realm constructor')
25+
parser.add_argument('--mech', default='krb5',
26+
help='Which environment to setup up '
27+
'(supports krb5 [default])')
28+
29+
PARSED_ARGS = parser.parse_args()
30+
31+
realm_args = {}
32+
if PARSED_ARGS.realm_args:
33+
for arg in PARSED_ARGS.realm_args.split(','):
34+
key, raw_val = arg.split('=')
35+
realm_args[key] = (raw_val.lower() == 'true')
36+
37+
try:
38+
mech_cls_loader = next(
39+
pkgres.iter_entry_points('gssapi_console.drivers',
40+
name=PARSED_ARGS.mech))
41+
except StopIteration:
42+
sys.exit('The %s environment is not supported by the '
43+
'GSSAPI console' % PARSED_ARGS.mech)
44+
45+
mech_cls = mech_cls_loader.load()
46+
console = GSSAPIConsole(mech_cls, realm_args=realm_args)
47+
48+
SAVED_ENV = None
49+
50+
try:
51+
# import the env
52+
SAVED_ENV = copy.deepcopy(os.environ)
53+
for k, v in console.realm.env.items():
54+
os.environ[k] = v
55+
56+
INTER = True
57+
# run the interactive interpreter
58+
if PARSED_ARGS.file is not None:
59+
if not PARSED_ARGS.force_interactive:
60+
INTER = False
61+
62+
with open(PARSED_ARGS.file) as src:
63+
console.runsource(src.read(), src.name, 'exec')
64+
65+
if INTER:
66+
console.interact()
67+
68+
except (KeyboardInterrupt, EOFError):
69+
pass
70+
finally:
71+
# restore the env
72+
if SAVED_ENV is not None:
73+
for k in copy.deepcopy(os.environ):
74+
if k in SAVED_ENV:
75+
os.environ[k] = SAVED_ENV[k]
76+
else:
77+
del os.environ[k]
78+
79+
console.realm.stop()

gssapi_console/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from gssapi_console.core import GSSAPIConsole # noqa
2+
from gssapi_console.drivers import GSSAPIConsoleDriver # noqa

gssapi_console/core.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import code
2+
import os
3+
import sys
4+
5+
6+
READLINE_SRC = """
7+
# python startup file
8+
import readline
9+
import rlcompleter
10+
import atexit
11+
import os
12+
13+
completer = rlcompleter.Completer(globals())
14+
readline.set_completer(completer.complete)
15+
16+
# tab completion
17+
readline.parse_and_bind('Control-space: complete')
18+
# history file
19+
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
20+
try:
21+
readline.read_history_file(histfile)
22+
except IOError:
23+
pass
24+
25+
atexit.register(readline.write_history_file, histfile)
26+
del os, histfile, readline, rlcompleter
27+
"""
28+
29+
BANNER = """GSSAPI Interactive console
30+
Python {ver} on {platform}
31+
Type "help", "copyright", "credits" or "license" for more information about Python.
32+
33+
Functions for controlling the realm are available in `REALM`.
34+
Mechansim: {mech} ({driver}), Realm: {realm}, User: {user}, Host: {host}"""
35+
36+
37+
class GSSAPIConsole(code.InteractiveConsole):
38+
def __init__(self, driver_cls, use_readline=True, realm_args={}, *args, **kwargs):
39+
code.InteractiveConsole.__init__(self, *args, **kwargs)
40+
41+
self._driver = driver_cls()
42+
self.realm = self._driver.create_realm(realm_args)
43+
self.locals['REALM'] = self.realm
44+
45+
self.runsource('import gssapi')
46+
self.runsource('import gssapi.raw as gb')
47+
48+
if use_readline:
49+
self._add_readline()
50+
51+
if os.environ.get('LD_LIBRARY_PATH'):
52+
self.realm.env['LD_LIBRARY_PATH'] = os.environ['LD_LIBRARY_PATH']
53+
54+
def _add_readline(self):
55+
self.runsource(READLINE_SRC, '<readline setup>', 'exec')
56+
57+
@property
58+
def banner_text(self):
59+
return BANNER.format(ver=sys.version, platform=sys.platform,
60+
mech=self._driver.MECH_NAME,
61+
driver=self._driver.PROVIDER_NAME,
62+
realm=self.realm.realm,
63+
user=self.realm.user_princ,
64+
host=self.realm.host_princ)
65+
66+
def interact(self, banner=None):
67+
if banner is None:
68+
banner = self.banner_text
69+
70+
super(GSSAPIConsole, self).interact(banner)

gssapi_console/drivers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import sys
2+
3+
4+
class GSSAPIConsoleDriver(object):
5+
MECH_NAME = ''
6+
PROVIDER_NAME = ''
7+
8+
def create_realm(self, realm_args):
9+
return None
10+
11+
12+
class Krb5Console(GSSAPIConsoleDriver):
13+
MECH_NAME = 'krb5'
14+
PROVIDER_NAME = 'MIT Kerberos 5'
15+
16+
def __init__(self):
17+
__import__('gssapi.tests.k5test')
18+
self._k5test = sys.modules['gssapi.tests.k5test']
19+
20+
def create_realm(self, realm_args):
21+
return self._k5test.K5Realm(**realm_args)

setup.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
from setuptools import setup
3+
4+
5+
setup(
6+
name='gssapi_console',
7+
version='1.0.0',
8+
author='The Python GSSAPI Team',
9+
author_email='sross@redhat.com',
10+
packages=['gssapi_console'],
11+
scripts=['gssapi-console.py'],
12+
description='An interactive tool for testing Python GSSAPI programs',
13+
long_description=open('README.md').read(),
14+
license='LICENSE.txt',
15+
url="https://github.com/pythongssapi/python-gssapi",
16+
classifiers=[
17+
'Development Status :: 4 - Beta',
18+
'Programming Language :: Python',
19+
'Programming Language :: Python :: 2.7',
20+
'Programming Language :: Python :: 3',
21+
'Programming Language :: Python :: 3.3',
22+
'Intended Audience :: Developers',
23+
'License :: OSI Approved :: ISC License (ISCL)',
24+
'Programming Language :: Python :: Implementation :: CPython',
25+
'Topic :: Security',
26+
],
27+
keywords=['gssapi', 'security'],
28+
entry_points={
29+
'gssapi_console.drivers': [
30+
'krb5 = gssapi_console.drivers:Krb5Console',
31+
],
32+
},
33+
install_requires=[
34+
'gssapi',
35+
'six'
36+
]
37+
)

tox.ini

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Tox (http://tox.testrun.org/) is a tool for running tests
2+
# in multiple virtualenvs. This configuration file will run the
3+
# test suite on all supported python versions. To use it, "pip install tox"
4+
# and then run "tox" from this directory.
5+
6+
[tox]
7+
envlist = py27,py33,py34
8+
9+
[testenv]
10+
# NB(sross): disabling E225,E226,E227,E901 make pep8 think Cython is ok
11+
# NB(sross): disable E402 because of the GSSAPI="blah" lines needed by Cython
12+
commands =
13+
./gssapi-console.py []

0 commit comments

Comments
 (0)