Argument parsing and checking - allow token authentication

This expands the argument parser and ensure_auth_info method to
provide alternative means of authentication. Up to now the CLI
required username, password, tenant and Keystone URL. Now user can
also provide auth token and Tuskar URL (alternatively Keystone URL
instead of Tuskar URL, in that case the Tuskar URL would be fetched
from Keystone).
This commit is contained in:
Jiri Stransky
2013-08-02 14:50:09 +02:00
parent 9efb9f1993
commit 3e2415b6d2
3 changed files with 75 additions and 13 deletions

View File

@@ -40,22 +40,33 @@ class TuskarShell(object):
self._ensure_auth_info(args)
def _ensure_auth_info(self, args):
if not args.os_username:
raise UsageError("You must provide username via either "
"--os-username or env[OS_USERNAME]")
'''Ensure that authentication information is provided. Two variants
of authentication are supported:
- provide username, password, tenant and auth url
- provide token and tuskar url (or auth url instead of tuskar url)
'''
if not args.os_auth_token:
if not args.os_username:
raise UsageError("You must provide username via either "
"--os-username or env[OS_USERNAME]")
if not args.os_password:
raise UsageError("You must provide password via either "
"--os-password or env[OS_PASSWORD]")
if not args.os_password:
raise UsageError("You must provide password via either "
"--os-password or env[OS_PASSWORD]")
if not args.os_tenant_id and not args.os_tenant_name:
raise UsageError("You must provide tenant via either "
"--os-tenant-name or --os-tenant-id or "
"env[OS_TENANT_NAME] or env[OS_TENANT_ID]")
if not args.os_tenant_id and not args.os_tenant_name:
raise UsageError("You must provide tenant via either "
"--os-tenant-name or --os-tenant-id or "
"env[OS_TENANT_NAME] or env[OS_TENANT_ID]")
if not args.os_auth_url:
raise UsageError("You must provide auth URL via either "
"--os-auth-url or env[OS_AUTH_URL]")
if not args.os_auth_url:
raise UsageError("You must provide auth URL via either "
"--os-auth-url or env[OS_AUTH_URL]")
else:
if not args.tuskar_url and not args.os_auth_url:
raise UsageError("You must provide either "
"--tuskar_url or --os_auth_url or "
"env[TUSKAR_URL] or env[OS_AUTH_URL]")
class UsageError(Exception):

View File

@@ -10,6 +10,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from tuskarclient import shell
import tuskarclient.tests.utils as tutils
@@ -17,3 +18,39 @@ class ShellTest(tutils.TestCase):
def setUp(self):
super(ShellTest, self).setUp()
self.s = shell.TuskarShell({})
def empty_args(self):
args = lambda: None # i'd use object(), but it can't have attributes
args_attributes = [
'os_username', 'os_password', 'os_tenant_name', 'os_tenant_id',
'os_auth_url', 'os_auth_token', 'tuskar_url',
]
for attr in args_attributes:
setattr(args, attr, None)
return args
def test_ensure_auth_info_with_credentials(self):
ensure = self.s._ensure_auth_info
usage_error = shell.UsageError
args = self.empty_args()
args.os_username = 'user'
args.os_password = 'pass'
args.os_tenant_name = 'tenant'
self.assertRaises(usage_error, ensure, args)
args.os_auth_url = 'keystone'
ensure(args) # doesn't raise
def test_ensure_auth_info_with_token(self):
ensure = self.s._ensure_auth_info
usage_error = shell.UsageError
args = self.empty_args()
args.os_auth_token = 'token'
self.assertRaises(usage_error, ensure, args)
args.tuskar_url = 'tuskar'
ensure(args) # doesn't raise

View File

@@ -70,4 +70,18 @@ def create_top_parser():
help=argparse.SUPPRESS,
)
parser.add_argument('--os-auth-token',
default=utils.env('OS_AUTH_TOKEN'),
help='Defaults to env[OS_AUTH_TOKEN]')
parser.add_argument('--os_auth_token',
help=argparse.SUPPRESS)
parser.add_argument('--tuskar-url',
default=utils.env('TUSKAR_URL'),
help='Defaults to env[TUSKAR_URL]')
parser.add_argument('--tuskar_url',
help=argparse.SUPPRESS)
return parser