1
0
mirror of https://github.com/gryf/openstack.git synced 2025-12-17 19:40:19 +01:00

Publish keystone federation with athenz

This commit is contained in:
Arun S A G
2018-10-09 14:52:50 -07:00
parent 64de20915c
commit 425d83e3d2
10 changed files with 813 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
# Oath Open Source Code of Conduct
## Summary
This Code of Conduct is our way to encourage good behavior and discourage bad behavior in our open source community. We invite participation from many people to bring different perspectives to support this project. We pledge to do our part to foster a welcoming and professional environment free of harassment. We expect participants to communicate professionally and thoughtfully during their involvement with this project.
Participants may lose their good standing by engaging in misconduct. For example: insulting, threatening, or conveying unwelcome sexual content. We ask participants who observe conduct issues to report the incident directly to the project's Response Team at opensource-conduct@oath.com. Oath will assign a respondent to address the issue. We may remove harassers from this project.
This code does not replace the terms of service or acceptable use policies of the websites used to support this project. We acknowledge that participants may be subject to additional conduct terms based on their employment which may govern their online expressions.
## Details
This Code of Conduct makes our expectations of participants in this community explicit.
* We forbid harassment and abusive speech within this community.
* We request participants to report misconduct to the projects Response Team.
* We urge participants to refrain from using discussion forums to play out a fight.
### Expected Behaviors
We expect participants in this community to conduct themselves professionally. Since our primary mode of communication is text on an online forum (e.g. issues, pull requests, comments, emails, or chats) devoid of vocal tone, gestures, or other context that is often vital to understanding, it is important that participants are attentive to their interaction style.
* **Assume positive intent.** We ask community members to assume positive intent on the part of other peoples communications. We may disagree on details, but we expect all suggestions to be supportive of the community goals.
* **Respect participants.** We expect participants will occasionally disagree. Even if we reject an idea, we welcome everyones participation. Open Source projects are learning experiences. Ask, explore, challenge, and then respectfully assert if you agree or disagree. If your idea is rejected, be more persuasive not bitter.
* **Welcoming to new members.** New members bring new perspectives. Some may raise questions that have been addressed before. Kindly point them to existing discussions. Everyone is new to every project once.
* **Be kind to beginners.** Beginners use open source projects to get experience. They might not be talented coders yet, and projects should not accept poor quality code. But we were all beginners once, and we need to engage kindly.
* **Consider your impact on others.** Your work will be used by others, and you depend on the work of others. We expect community members to be considerate and establish a balance their self-interest with communal interest.
* **Use words carefully.** We may not understand intent when you say something ironic. Poes Law suggests that without an emoticon people will misinterpret sarcasm. We ask community members to communicate plainly.
* **Leave with class.** When you wish to resign from participating in this project for any reason, you are free to fork the code and create a competitive project. Open Source explicitly allows this. Your exit should not be dramatic or bitter.
### Unacceptable Behaviors
Participants remain in good standing when they do not engage in misconduct or harassment. To elaborate:
* **Don't be a bigot.** Calling out project members by their identity or background in a negative or insulting manner. This includes, but is not limited to, slurs or insinuations related to protected or suspect classes e.g. race, color, citizenship, national origin, political belief, religion, sexual orientation, gender identity and expression, age, size, culture, ethnicity, genetic features, language, profession, national minority statue, mental or physical ability.
* **Don't insult.** Insulting remarks about a persons lifestyle practices.
* **Don't dox.** Revealing private information about other participants without explicit permission.
* **Don't intimidate.** Threats of violence or intimidation of any project member.
* **Don't creep.** Unwanted sexual attention or content unsuited for the subject of this project.
* **Don't disrupt.** Sustained disruptions in a discussion.
* **Let us help.** Refusal to assist the Response Team to resolve an issue in the community.
We do not list all forms of harassment, nor imply some forms of harassment are not worthy of action. Any participant who *feels* harassed or *observes* harassment, should report the incident. Victim of harassment should not address grievances in the public forum, as this often intensifies the problem. Report it, and let us address it off-line.
### Reporting Issues
If you experience or witness misconduct, or have any other concerns about the conduct of members of this project, please report it by contacting our Response Team at opensource-conduct@oath.com who will handle your report with discretion. Your report should include:
* Your preferred contact information. We cannot process anonymous reports.
* Names (real or usernames) of those involved in the incident.
* Your account of what occurred, and if the incident is ongoing. Please provide links to or transcripts of the publicly available records (e.g. a mailing list archive or a public IRC logger), so that we can review it.
* Any additional information that may be helpful to achieve resolution.
After filing a report, a representative will contact you directly to review the incident and ask additional questions. If a member of the Oath Response Team is named in an incident report, that member will be recused from handling your incident. If the complaint originates from a member of the Response Team, it will be addressed by a different member of the Response Team. We will consider reports to be confidential for the purpose of protecting victims of abuse.
### Scope
Oath will assign a Response Team member with admin rights on the project and legal rights on the project copyright. The Response Team is empowered to restrict some privileges to the project as needed. Since this project is governed by an open source license, any participant may fork the code under the terms of the project license. The Response Teams goal is to preserve the project if possible, and will restrict or remove participation from those who disrupt the project.
This code does not replace the terms of service or acceptable use policies that are provided by the websites used to support this community. Nor does this code apply to communications or actions that take place outside of the context of this community. Many participants in this project are also subject to codes of conduct based on their employment. This code is a social-contract that informs participants of our social expectations. It is not a terms of service or legal contract.
## License and Acknowledgment.
This text is shared under the [CC-BY-4.0 license](https://creativecommons.org/licenses/by/4.0/). This code is based on a study conducted by the [TODO Group](https://todogroup.org/) of many codes used in the open source community. If you have feedback about this code, contact our Response Team at the address listed above.

View File

@@ -0,0 +1,26 @@
# How to contribute
First, thanks for taking the time to contribute to our project! The following information provides a guide for making contributions.
## Code of Conduct
By participating in this project, you agree to abide by the [Oath Code of Conduct](Code-of-Conduct.md). Everyone is welcome to submit a pull request or open an issue to improve the documentation, add improvements, or report bugs.
## How to Ask a Question
If you simply have a question that needs an answer, [create an issue](https://help.github.com/articles/creating-an-issue/), and label it as a question.
## How To Contribute
### Report a Bug or Request a Feature
If you encounter any bugs while using this software, or want to request a new feature or enhancement, feel free to [create an issue](https://help.github.com/articles/creating-an-issue/) to report it, make sure you add a label to indicate what type of issue it is.
### Contribute Code
Pull requests are welcome for bug fixes. If you want to implement something new, please [request a feature first](#report-a-bug-or-request-a-feature) so we can discuss it.
#### Creating a Pull Request
Before you submit any code, we need you to agree to our [Contributor License Agreement](https://yahoocla.herokuapp.com/); this ensures we can continue to protect your contributions under an open source license well into the future.
Please follow [best practices](https://github.com/trein/dev-best-practices/wiki/Git-Commit-Best-Practices) for creating git commits.
When your code is ready to be submitted, you can [submit a pull request](https://help.github.com/articles/creating-a-pull-request/) to begin the code review process.

View File

@@ -0,0 +1,176 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

View File

@@ -0,0 +1,42 @@
# Keystone Federation
> Oath's approach to Keystone federation
This repository contains a patch and plugin that adds a new
auth\_method to keystone called "athenz\_token." This auth method uses the Athenz plugin to authenticate users based on the Athenz token the OpenStack client passes.
Once the plugin validates and authenticates the token, the users, projects
and roles are created with consistent UUIDs using the following hash function
`uuid.uuid3(uuid.NAMESPACE_OID, str(string)).hex`. This results in the UUIDs being consistent
across the clusters that use this plugin, enabling the federation of other services like Glance.
## Table of Contents
- [Background](#background)
- [Install](#install)
- [Usage](#usage)
- [License](#license)
## Background
One of the discussions that occurred during the Edge Working Group meetings at the [OpenStack PTG in Denver](https://www.openstack.org/ptg/)related to keystone federation in
edge environments. Oath has used an external RBAC and integrated it into keystone
to federate various EDGE clusters. This repository has the plugin that integrates Athenz,
our external Role-Based Authorization (RBAC) system, with keystone.
## Install
To use this plugin, you will first need to install [Athenz](http://www.athenz.io/). Apply the patch to Keystone (stable/ocata) and include the plugin in the keystone source tree. Edit `/etc/keystone/keystone.conf` to add `athenz_token` as an auth\_method under [auth] section.
## Usage
### athenz\_token.py
This is a helper module that the plugin uses to validate the athenz\_token.
This script cryptographically validates and parses the athenz\_token to be used by the
plugin.
### athenz.py
This is the main plugin that implements the athenz\_token auth method. It validates the athenz\_token (from external IDP) and uses the attributes in the Athenz token to create users, projects and roles with consistent UUIDs if they don't already exist in Keystone. Finally, it proceeds to generate
the configured keystone token.
## License
This project is licensed under the terms of the [Apache 2.0](LICENSE-Apache-2.0) open source license. Please refer to [LICENSE](LICENSE) for the full terms.

View File

@@ -0,0 +1,112 @@
From 0dc19ff43d1824fa3d279f63a8b251761261b053 Mon Sep 17 00:00:00 2001
From: Arun S A G <saga@yahoo-inc.com>
Date: Fri, 6 Apr 2018 16:22:36 -0700
Subject: [PATCH] Add athenz token to fernet token
The athenz role token will now be inside the regular fernet
token so that it may be retrieved for calling outside services
on the user's behalf.
Do not validate scope data before authenticating the user
in case auth_method is athenz_token.
Be sure to create the user with the 'id' that is passed in
---
keystone/auth/controllers.py | 11 +++++++++--
keystone/identity/core.py | 4 +++-
keystone/tests/unit/test_backend_sql.py | 8 ++++++++
keystone/token/providers/common.py | 10 +++++++++-
4 files changed, 29 insertions(+), 4 deletions(-)
diff --git a/keystone/auth/controllers.py b/keystone/auth/controllers.py
index bdc7ba569..c73f141d2 100644
--- a/keystone/auth/controllers.py
+++ b/keystone/auth/controllers.py
@@ -108,13 +108,20 @@ class Auth(controller.V3Controller):
include_catalog = 'nocatalog' not in request.params
validate_issue_token_auth(auth)
+ authenticated = False
try:
- auth_info = core.AuthInfo.create(auth=auth)
+ auth_info_no_scope = core.AuthInfo(auth)
auth_context = core.AuthContext(extras={},
method_names=[],
bind={})
- self.authenticate(request, auth_info, auth_context)
+ if 'athenz_token' in auth['identity']['methods']:
+ self.authenticate(request, auth_info_no_scope, auth_context)
+ authenticated = True
+ auth_info = core.AuthInfo.create(auth=auth)
+
+ if not authenticated:
+ self.authenticate(request, auth_info, auth_context)
if auth_context.get('access_token_id'):
auth_info.set_scope(None, auth_context['project_id'], None)
self._check_and_set_default_scoping(auth_info, auth_context)
diff --git a/keystone/identity/core.py b/keystone/identity/core.py
index 5168bf01f..9d8f65c5b 100644
--- a/keystone/identity/core.py
+++ b/keystone/identity/core.py
@@ -935,7 +935,9 @@ class Manager(manager.Manager):
# Generate a local ID - in the future this might become a function of
# the underlying driver so that it could conform to rules set down by
# that particular driver type.
- user['id'] = uuid.uuid4().hex
+ # (saga): In case of athenz_token auth method, we pass in our own id
+ if 'id' not in user:
+ user['id'] = uuid.uuid4().hex
ref = driver.create_user(user['id'], user)
notifications.Audit.created(self._USER, user['id'], initiator)
return self._set_domain_id_and_mapping(
diff --git a/keystone/tests/unit/test_backend_sql.py b/keystone/tests/unit/test_backend_sql.py
index aee804573..1b727f446 100644
--- a/keystone/tests/unit/test_backend_sql.py
+++ b/keystone/tests/unit/test_backend_sql.py
@@ -290,6 +290,14 @@ class SqlIdentity(SqlTests,
# assign a new ID with the same name, but this time in uppercase
ref['name'] = ref['name'].upper()
+
+ # (saga) When 'id' is passed to create_user, we create user with that
+ # ID instead of generating a new one. In this case if the ID from ref
+ # object is not deleted, below call to create_user will result in
+ # conflict, because there is an user with same ID is already created
+ # above.
+ del ref['id']
+
self.identity_api.create_user(ref)
def test_create_project_case_sensitivity(self):
diff --git a/keystone/token/providers/common.py b/keystone/token/providers/common.py
index d29d6f832..9b015149c 100644
--- a/keystone/token/providers/common.py
+++ b/keystone/token/providers/common.py
@@ -390,7 +390,8 @@ class V3TokenDataHelper(object):
# We've probably already written these to the token
if token:
- for x in ('roles', 'user', 'catalog', 'project', 'domain'):
+ for x in ('roles', 'user', 'catalog', 'project', 'domain',
+ 'athenz_token'):
if x in token:
token_data[x] = token[x]
@@ -462,6 +463,13 @@ class BaseProvider(base.Provider):
token_ref = self._handle_mapped_tokens(
auth_context, project_id, domain_id)
+ if auth_context and 'athenz_token' in auth_context:
+ token_dict = {'athenz_token': auth_context['athenz_token']}
+ if token_ref is None:
+ token_ref = token_dict
+ else:
+ token_ref.update(token_dict)
+
access_token = None
if 'oauth1' in method_names:
access_token_id = auth_context['access_token_id']
--
2.14.3

View File

@@ -0,0 +1,230 @@
# Copyright 2018, Oath Inc
# Licensed under the terms of the Apache 2.0 license. See LICENSE file for terms.
import ast
import keystone.conf
import six
import sys
import uuid
from keystone.auth.plugins import base
from keystone.common import dependency
from keystone.common import driver_hints
from keystone import exception
from keystone.i18n import _
from oslo_log import log
from yahoo.contrib.ocata_openstack_yahoo_plugins.keystone.auth.plugins.athenz_token import AthenzToken # noqa
from yahoo.contrib.ocata_openstack_yahoo_plugins.keystone.auth.plugins.athenz_token import is_athenz_role_token # noqa
LOG = log.getLogger(__name__)
KEYSTONE_CONF = keystone.conf.CONF
METHOD_NAME = 'athenz_token'
class AthenzUserAuthInfo(object):
@classmethod
def create(cls, auth_payload, method_name):
user_auth_info = cls()
user_auth_info._validate_and_normalize_auth_data(auth_payload)
user_auth_info.METHOD_NAME = method_name
return user_auth_info
def __init__(self):
self.user_name = None
self.athenz_token = None
self.domain_id = None
self.domain_name = None
self.project_name = None
def _validate_and_normalize_auth_data(self, auth_payload):
if 'user' not in auth_payload:
raise exception.ValidationError(attribute='user',
target=self.METHOD_NAME)
user_info = auth_payload['user']
user_id = user_info.get('id')
self.user_name = user_info.get('name')
self.project_name = user_info.get('project_name')
if not user_id and not self.user_name:
raise exception.ValidationError(attribute='id or name',
target='user')
if not self.project_name:
raise exception.ValidationError(attribute='project name',
target='user')
if self.user_name:
if 'domain' not in user_info:
raise exception.ValidationError(attribute='domain',
target='user')
self.domain_id = user_info['domain'].get('id')
self.domain_name = user_info['domain'].get('name')
if not self.domain_id and not self.domain_name:
raise exception.ValidationError(attribute='domain id or name',
target='user')
if 'athenz_token' not in user_info:
LOG.error("athenz_token not found in user object: %s", user_info)
raise exception.ValidationError(attribute='athenz_token',
target='user')
athenz_token = ast.literal_eval(user_info['athenz_token'])['token']
self.athenz_token = athenz_token
@dependency.requires('identity_api', 'resource_api', 'role_api',
'assignment_api')
class AthenzAuthPlugin(base.AuthMethodHandler):
def generate_consistent_id(self, string):
"""Given string generate a consistent hash"""
return uuid.uuid3(uuid.NAMESPACE_OID, str(string)).hex
def _lookup_domain(self, domain_id):
try:
self.resource_api.assert_domain_enabled(domain_id)
except exception.DomainNotFound as e:
LOG.error("Domain not found: %s", domain_id)
LOG.warning(six.text_type(e))
raise exception.Unauthorized(e)
except AssertionError as e:
LOG.error("Domain is not enabled: %s", domain_id)
log.warning(six.text_type(e))
six.reraise(exception.Unauthorized, exception.Unauthorized(e),
sys.exc_info()[2])
def _create_project(self, request, tenant_ref):
try:
return self.resource_api.create_project(tenant_ref['id'],
tenant_ref,
request.audit_initiator)
except (exception.DomainNotFound, exception.ProjectNotFound) as e:
raise exception.ValidationError(e)
def _lookup_and_create_project(self, request, project_name, domain_id):
try:
return self.resource_api.get_project_by_name(project_name,
domain_id)
except exception.ProjectNotFound:
project_ref = {'id': self.generate_consistent_id(project_name),
'name': project_name,
'enabled': True,
'domain_id': domain_id,
'is_domain': False,
'parent_id': domain_id,
'description': 'Project created by athenz plugin',
}
return self._create_project(request, project_ref)
def _lookup_and_create_user(self, request, domain_id, uname):
try:
return self.identity_api.get_user_by_name(uname, domain_id)
except exception.UserNotFound:
user_ref = {'id': self.generate_consistent_id(uname),
'name': uname,
'enabled': True,
'domain_id': domain_id,
'description': 'User created by athenz plugin'
}
return self.identity_api.create_user(user_ref,
request.audit_initiator)
def _lookup_and_create_role(self, request, role_name, domain_id):
hints = driver_hints.Hints()
hints.add_filter("name", role_name, case_sensitive=True)
found_roles = self.role_api.list_roles(hints)
LOG.info("Found roles:", found_roles)
if not found_roles:
# Create the role
role_id = self.generate_consistent_id(role_name)
role_ref = {'id': role_id,
'name': role_name
}
return self.role_api.create_role(role_ref['id'],
role_ref,
initiator=request.audit_initiator)
elif len(found_roles) == 1:
return self.role_api.get_role(found_roles[0]['id'])
else:
raise exception.AmbiguityError(resource='role',
name=role_name)
def _create_project_and_assign_roles(self,
request,
atoken,
requested_project_name,
user_ref, domain_id):
""" If requested project has a role in athenz, then create the
requested project and role if necessary. Assign said role to user
on the created project
request: The request object
atoken: Athenz token object
requested_project_name: project_name from the request (OS_PROJECTNAME)
user_ref: User object
domain_id: ID of the domain
"""
created = False
for role, projects in atoken.projects.items():
# check requested_project_name is part of one of the roles
if requested_project_name.lower() in projects:
if not created:
project_ref = self._lookup_and_create_project(request,
requested_project_name, # noqa
domain_id)
created = True
role_ref = self._lookup_and_create_role(request,
role,
domain_id)
# Do grants
self.assignment_api.create_grant(role_ref['id'],
user_id=user_ref['id'],
project_id=project_ref['id'])
def authenticate(self, request, auth_payload):
"""Autenticate the athenz token
Validate the athenz token create projects/users if needed
"""
response_data = {}
user_info = AthenzUserAuthInfo.create(auth_payload, METHOD_NAME)
if not is_athenz_role_token(user_info.athenz_token):
LOG.error("Not a valid athenz role token")
raise exception.Unauthorized(_('Not a valid athenz role token'))
atoken = AthenzToken(user_info.athenz_token)
if atoken.validate(user_info.user_name) and atoken.user:
# We got a valid athenz token
LOG.debug("Athenz token is valid")
# Get domain ID from name if it is not part of the request
domain_id = user_info.domain_id
if not user_info.domain_id:
domain_ref = self.resource_api.get_domain_by_name(
user_info.domain_name)
domain_id = domain_ref['id']
# Assert domain isn't disabled
self._lookup_domain(domain_id)
# Create the user specified in athenz token if necessary
user_ref = self._lookup_and_create_user(request,
domain_id,
atoken.user)
# Create keystone project specified by the user in the request
# (user_info.project_name) after validating athenz role token has
# the same project name in its roles.
self._create_project_and_assign_roles(request,
atoken,
user_info.project_name,
user_ref,
domain_id)
response_data['user_id'] = user_ref['id']
response_data['athenz_token'] = user_info.athenz_token
return base.AuthHandlerResponse(status=True, response_body=None,
response_data=response_data)
msg = _('Invalid athenz token')
raise exception.Unauthorized(msg)

View File

@@ -0,0 +1,173 @@
# Copyright 2018, Oath Inc
# Licensed under the terms of the Apache 2.0 license. See LICENSE file in for terms.
import base64
import json
import time
import re
from collections import defaultdict
import asn1
import M2Crypto.m2 as m2
import M2Crypto.EVP as EVP
import M2Crypto.RSA as RSA
import M2Crypto.EC as EC
import M2Crypto.BIO as BIO
from oslo_log import log
ATHENZ_CONF = {}
# gap in seconds, to determine whether given token is about to expire
STALE_TOKEN_DURATION = 300
OWS_ATHENZ_DOMAIN = 'ows.projects'
OID_ALGORITHMS = {
"1.2.840.113549.1.1.1": RSA,
"1.2.840.10045.2.1": EC
}
LOG = log.getLogger(__name__)
def get_athenz_conf():
"""Return dict of Athenz public keys."""
if not ATHENZ_CONF:
with open('/etc/athenz/athenz.conf') as conf_file:
ATHENZ_CONF.update(json.load(conf_file))
ATHENZ_CONF['zmsPublicKeys'] = {x['id']: x['key'] for x in ATHENZ_CONF['zmsPublicKeys']}
ATHENZ_CONF['ztsPublicKeys'] = {x['id']: x['key'] for x in ATHENZ_CONF['ztsPublicKeys']}
return ATHENZ_CONF
def get_key_algorithm(public_key):
"""Given a public key, return the algorithm type it uses.
Currently only supports EC and RSA."""
key_data = '\n'.join(public_key.split('\n')[1:-1])
key_bytes = base64.b64decode(key_data)
decoder = asn1.Decoder()
decoder.start(key_bytes)
tag = decoder.peek()
while tag.nr != asn1.Numbers.ObjectIdentifier:
decoder.enter()
tag = decoder.peek()
_, oid = decoder.read()
return OID_ALGORITHMS[oid]
def decode_y64(b64_data):
"""Decode Yahoo's version of base64 and return result."""
return str(base64.b64decode(b64_data.replace('-', '=').replace('.', '+').replace('_', '/')))
def is_athenz_role_token(token):
"""Return True IFF token is a role token. Else False."""
return token.startswith('v=Z1;')
class YahooPKey(EVP.PKey):
def assign_key(self, key):
if hasattr(key, 'ec'):
self.assign_ec(key)
else:
self.assign_rsa(key)
def assign_ec(self, ec):
ret = m2.pkey_assign_ec(self.pkey, ec.ec)
if ret:
ec._pyfree = 0
return ret
class AthenzToken(object):
"""Wrapper around an Athenz Role Token."""
ROLE_PROJECT_REGEX = re.compile('^(.+?)\\.([^.]+?)$')
def __init__(self, token):
if not is_athenz_role_token(token):
raise ValueError("Must provide valid role token.")
self.raw_token = token
self.attrs = dict(a.split('=') for a in token.split(';'))
self._projects = defaultdict(set)
@property
def expire_time(self):
"""Returns int of the UTC Unix Time when the token will expire."""
return int(self.attrs['e'])
@property
def user(self):
"""Return the username if the principal is a user. Else return None."""
if self.attrs['p'].startswith('user.'):
return self.attrs['p'].split('.')[1]
else:
return self.attrs['p']
@property
def domain(self):
"""Return the Athenz Domain of the token."""
return self.attrs['d']
@property
def roles(self):
"""Return a list of Athenz Roles in this token."""
return self.attrs['r'].split(',')
@property
def signature(self):
"""Returns un-encoded signature of the token."""
return decode_y64(self.attrs['s'])
@property
def key_id(self):
"""Key ID for the athenz_config public key."""
return self.attrs['k']
@property
def projects(self):
"""Returns a defaultdict of type
`keystone role`->`set of keystone projects`."""
if not self._projects:
for role in self.roles:
matches = re.search(AthenzToken.ROLE_PROJECT_REGEX, role)
if matches:
keystone_project = matches.groups()[0]
keystone_role = matches.groups()[1]
self._projects[keystone_role].add(keystone_project)
return self._projects
@property
def unsigned_raw_token(self):
"""Return the raw unsigned athenz token."""
return str(self.raw_token[:self.raw_token.index(';s=')])
def validate(self, username):
"""Return True IFF this token has been signed by Athenz,
has not expired, and the given username matches. Else return False."""
# Ensure token hasn't expired
# We pad STALE_TOKEN_DURATION to the current time because we want to
# make sure that the token won't expire in the foreseable future
if self.expire_time < time.time() + STALE_TOKEN_DURATION:
return False
# Validate username matches
if self.user != username:
return False
# Only accept tokens in the ows.projects Athenz domain
if self.domain != OWS_ATHENZ_DOMAIN:
return False
# Retrieve proper pub key from athenz_config package
pub_keys = get_athenz_conf()
public_key = decode_y64(pub_keys['ztsPublicKeys'][self.key_id])
algorithm = get_key_algorithm(public_key)
loaded_pub_key = algorithm.load_pub_key_bio(BIO.MemoryBuffer(public_key))
# Verify cryptographic signature
unsigned_token = self.unsigned_raw_token
verifier = YahooPKey(md='sha256')
verifier.assign_key(loaded_pub_key)
verifier.verify_init()
verifier.verify_update(unsigned_token)
result = verifier.verify_final(self.signature)
return result == 1