Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic getter for tenant and user details #28

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions openstackinabox/models/keystone/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,29 @@ def bool_to_database(value):
return 1
return 0

def add_admin_tenant_details(self):
self.__admin_tenant_args = {
'name': 'system',
'description': 'system administrator',
}

def add_admin_user_details(self):
self.__admin_user_args = {
'tenantid': self.__admin_tenant_id,
'username': 'system',
'email': 'system@stackinabox',
'password': 'stackinabox',
'apikey': '537461636b496e41426f78',
}

@property
def get_admin_tenant_details(self):
return self.get_tenant_by_id(self.__admin_tenant_id)

@property
def get_admin_user_details(self):
return self.get_user_by_id(self.__admin_tenant_id, self.__admin_user_id)

def init_database(self):
self.log_info('Initializing database')
dbcursor = self.database.cursor()
Expand All @@ -314,11 +337,13 @@ def init_database(self):
# Create an admin user and add the admin token to that user
self.__admin_tenant_id = self.add_tenant('system',
'system administrator')

self.__admin_user_id = self.add_user(self.__admin_tenant_id,
'system',
'system@stackinabox',
'stackinabox',
'537461636b496e41426f78')

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as https://github.com/BenjamenMeyer/openstackinabox/pull/28/files#r31276988 but with respect to the user information...

roles = [
KeystoneModel.IDENTITY_ADMIN_ROLE,
KeystoneModel.IDENTITY_VIEWER_ROLE,
Expand Down
67 changes: 67 additions & 0 deletions openstackinabox/services/keystone/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,18 @@ class KeystoneV2Service(BaseService):
'^\/users\/{0}/OS-KSADM/credentials$'
.format(USER_ID_REGEX))

USER_ID_ADMIN_PATH_REGEX = re.compile(
'^\/users\/{0}/RAX-AUTH/admins$'
.format(USER_ID_REGEX))

@staticmethod
def get_user_id_from_path(uri_path):
uri_matcher = None

regexes = [
KeystoneV2Service.USER_ID_PATH_REGEX,
KeystoneV2Service.USER_ID_KSADM_CREDENTIAL_PATH_REGEX,
KeystoneV2Service.USER_ID_ADMIN_PATH_REGEX,
]

for r in regexes:
Expand Down Expand Up @@ -81,6 +86,9 @@ def __init__(self):
self.register(BaseService.POST,
KeystoneV2Service.USER_ID_KSADM_CREDENTIAL_PATH_REGEX,
KeystoneV2Service.handle_add_credentials_to_user)
self.register(BaseService.GET,
KeystoneV2Service.USER_ID_ADMIN_PATH_REGEX,
KeystoneV2Service.handle_get_admin_user)
self.log_info('initialized')

@property
Expand Down Expand Up @@ -579,3 +587,62 @@ def handle_add_credentials_to_user(self, request, uri, headers):
return (503, headers, 'Server error')

return (201, headers, '')

def handle_get_admin_user(self, request, uri, headers):
'''
200 -> OK
400 -> Bad Request
403 -> Forbidden
404 -> Not Found
405 -> Invalid Method
413 -> Over Limit
415 -> Bad Media Type
503 -> Service Fault

No body

Response
{
"users": [
{
"RAX-AUTH:defaultRegion": <region>,
"RAX-AUTH:domainId": <tenantid>,
"email": <email>
"enabled": True/False,
"id": <userid>,
"username": <username>
}
]
}
'''
self.log_request(uri, request)
req_headers = request.headers

user_data = self.helper_authenticate(req_headers,
headers,
True,
False)
if isinstance(user_data, tuple):
return user_data

try:
user_id = KeystoneV2Service.get_user_id_from_path(uri)
self.log_debug('Lookup of user id {0} requested'
.format(user_id))

except Exception as ex: # pragma: no cover
self.log_exception('Failed to get user id from path')
return (400, headers, 'bad request')

try:
user_info = self.model.get_user_by_id(user_data['tenantid'],
user_id)
except:
self.log_exception('failed to get user data')
return (404, headers, 'Not found')

del user_info['password']
del user_info['apikey']
user_info['RAX-AUTH:DomainID'] = user_info['tenantid']

return (200, headers, json.dumps(user_info))
117 changes: 117 additions & 0 deletions openstackinabox/tests/test_get_admin_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""
Stack-In-A-Box: Basic Test
"""
import json
import unittest

import httpretty
import mock
import requests
import stackinabox.util_httpretty
from stackinabox.stack import StackInABox

from openstackinabox.models.keystone.model import KeystoneModel
from openstackinabox.services.keystone import KeystoneV2Service


@httpretty.activate
class TestKeystoneV2GetAdmin(unittest.TestCase):

def setUp(self):
super(TestKeystoneV2GetAdmin, self).setUp()
self.keystone = KeystoneV2Service()

self.headers = {
'x-auth-token': self.keystone.model.get_admin_token()
}
self.tenant_id = self.keystone.model.add_tenant(tenantname='neo',
description='The One')
self.user_info = {
'user': {
'username': 'trinity',
'enabled': True,
'email': '[email protected]',
'password': 'Inl0veWithNeo'
}
}
self.user_info['user']['userid'] =\
self.keystone.model.add_user(tenantid=self.tenant_id,
username=self.user_info['user'][
'username'],
email=self.user_info['user']['email'],
password=self.user_info['user'][
'password'],
enabled=self.user_info['user'][
'enabled'])
self.keystone.model.add_token(self.tenant_id,
self.user_info['user']['userid'])
self.keystone.model.add_user_role_by_rolename(
tenantid=self.tenant_id,
userid=self.user_info['user']['userid'],
rolename=self.keystone.model.IDENTITY_ADMIN_ROLE)
StackInABox.register_service(self.keystone)


def tearDown(self):
super(TestKeystoneV2GetAdmin, self).tearDown()
StackInABox.reset_services()

@staticmethod
def get_userid_url(host, userid):
return 'http://{0}/keystone/v2.0/users/{1}/RAX-AUTH/admins'\
.format(host, userid)

def test_get_admin_user_basic(self):
stackinabox.util_httpretty.httpretty_registration('localhost')

user_data = self.keystone.model.get_token_by_userid(
self.user_info['user']['userid'])

url = TestKeystoneV2GetAdmin.get_userid_url(
'localhost',
self.user_info['user']['userid'])

self.headers['x-auth-token'] = user_data['token']
res = requests.get(url,
headers=self.headers,
data='')
self.assertEqual(res.status_code, 200)

def test_get_admin_user_incorrect_request(self):
stackinabox.util_httpretty.httpretty_registration('localhost')

user_data = self.keystone.model.get_token_by_userid(
self.user_info['user']['userid'])

url = TestKeystoneV2GetAdmin.get_userid_url(
'localhost',
self.user_info['user']['userid'])

res = requests.get(url,
headers=self.headers,
data='')
self.assertEqual(res.status_code, 404)

def test_get_admin_user_no_token(self):
stackinabox.util_httpretty.httpretty_registration('localhost')

url = TestKeystoneV2GetAdmin.get_userid_url(
'localhost',
self.user_info['user']['userid'])

res = requests.get(url, headers=None, data='')
self.assertEqual(res.status_code, 403)

def test_get_admin_user_invalid_token(self):
stackinabox.util_httpretty.httpretty_registration('localhost')

url = TestKeystoneV2GetAdmin.get_userid_url(
'localhost',
self.user_info['user']['userid'])
self.headers['x-auth-token'] = 'new_token'
res = requests.get(url,
headers=self.headers,
data='')
self.assertEqual(res.status_code, 401)


48 changes: 48 additions & 0 deletions openstackinabox/tests/test_model_basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""
Stack-In-A-Box: Basic Test
"""
import json
import unittest

import httpretty
import mock
import requests
import stackinabox.util_httpretty
from stackinabox.stack import StackInABox

from openstackinabox.models.keystone.model import KeystoneModel
from openstackinabox.services.keystone import KeystoneV2Service


@httpretty.activate
class TestKeystoneModel(unittest.TestCase):

def setUp(self):
super(TestKeystoneModel, self).setUp()
self.keystone = KeystoneV2Service()
self.headers = {
'x-auth-token': self.keystone.model.get_admin_token()
}

def tearDown(self):
super(TestKeystoneModel, self).tearDown()
StackInABox.reset_services()

def test_keystone_set_model(self):
with self.assertRaises(TypeError):
self.keystone.model = None

self.keystone.model = KeystoneModel()

def test_get_tenant_details(self):
stackinabox.util_httpretty.httpretty_registration('localhost')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to register stackinabox here; that's only necessary for HTTP calls to the Service

tenant_details = self.keystone.model.get_admin_tenant_details
self.assertEqual(tenant_details['name'], 'system')
self.assertEqual(tenant_details['description'], 'system administrator')

def test_get_user_details(self):
user_details = self.keystone.model.get_admin_user_details
self.assertEqual(user_details['username'], 'system')
self.assertEqual(user_details['email'], 'system@stackinabox')
self.assertEqual(user_details['password'], 'stackinabox')
self.assertEqual(user_details['apikey'], '537461636b496e41426f78')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split the second half for the user_details into a separate test function