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

add keystone auth backend #1732

Merged
merged 11 commits into from
Jul 21, 2015
64 changes: 64 additions & 0 deletions st2auth/st2auth/backends/keystone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


from st2common import log as logging
from st2auth.backends.base import BaseAuthenticationBackend
import requests

__all__ = [
'KeystoneAuthenticationBackend'
]

LOG = logging.getLogger(__name__)


class KeystoneAuthenticationBackend(BaseAuthenticationBackend):
"""
Backend which reads authentication information from keystone

Note: This backend depends on the "requests" library.
"""

def __init__(self, keystone_url, keystone_version=2):
"""
:param keystone_url: Url of the Keystone server to authenticate against.
:type keystone_url: ``str``
:param keystone_version: Keystone version to authenticate against (default to 2).
:type keystone_version: ``int``
"""
self._keystone_url = keystone_url
self._keystone_version = keystone_version
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this argument is unused and we only support v2 right now?

If so, we should make that clear and throw if an unsupported version is provided.

Copy link
Member

Choose a reason for hiding this comment

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

On a related note, it might be better to just use python-keystoneclient.

Ideally, eventually we would also switch to using setup.py entry_points for plugins and provide plugins as separate Python packages with it's own requirements.txt file, but for now I'm OK with putting keystone-client dependency to the shared requirements.txt file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, Im doing the v2/v3 split now and some other checks. I wanted the PR to go up quick due to the testing question.

Re: keystone client. Totally against that tbh. It turns a simple authentication plugin with one dependency into dependency hell and also ties into a big external projects, while requests is one package with minimal dependencies and probably already installed on every decent python env which needs to do a network call.

And this auth backend just needs to do one simple call. To have keystoneclient as dependency is overkill IMO.


def authenticate(self, username, password):
creds = {
"auth": {
"passwordCredentials": {
"username": username,
"password": password
}
}
}
login = requests.post(self._keystone_url, json=creds)

if login.status_code == 200:
LOG.debug('Authentication for user "{}" successful'.format(username))
return True
else:
LOG.debug('Authentication for user "{}" failed: {}'.format(username, login.content))
return False

def get_user(self, username):
pass