IDP:
=====
Note: This feature requires installation of the xmlsec1 tool via your distribution packaging system (for instance apt or yum)Example for apt:$ apt-get install xmlsec1
keystone.conf
===========
[saml]
...
需要配置的选项:
certfile = /etc/keystone/ssl/certs/ca.pem
keyfile = /etc/keystone/ssl/private/cakey.pem
idp_entity_id = https://openstack-dev.sh.intel.com/v3/OS-FEDERATION/saml2/idp
idp_sso_endpoint = https://openstack-dev.sh.intel.com/v3/OS-FEDERATION/saml2/sso
idp_organization_name = Intel
idp_organization_url = http://www.intel.com
idp_contact_company = Intel
idp_contact_name = davechen
idp_contact_surname = chen
idp_contact_email = [email protected]
idp_contact_telephone = 555-55-5555
idp_contact_type = technical
idp_metadata_path = /etc/keystone/saml2_idp_metadata.xml
Generate Metadata
===============
用于与SP进行交换以建立IDP与SP之间的信任关系。
$ keystone-manage saml_idp_metadata > /etc/keystone/saml2_idp_metadata.xml
$ cat /etc/keystone/saml2_idp_metadata.xml
MIIDgTCCAmmgAwIBAgIJANl0Hwqcej8tMA0GCSqGSIb3DQEBCwUAMFcxCzAJBgNVBAYTAlVTMQ4wDAYDVQQIDAVVbnNldDEOMAwGA1UEBwwFVW5zZXQxDjAMBgNVBAoMBVVuc2V0MRgwFgYDVQQDDA93d3cuZXhhbXBsZS5jb20wHhcNMTYwNjI3MDYyNTQ5WhcNMjYwNjI1MDYyNTQ5WjBXMQswCQYDVQQGEwJVUzEOMAwGA1UECAwFVW5zZXQxDjAMBgNVBAcMBVVuc2V0MQ4wDAYDVQQKDAVVbnNldDEYMBYGA1UEAwwPd3d3LmV4YW1wbGUuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuZcup+BCt6J17qZB1GuZ4Ktd54LfvVSfxu6R0HIqdFl++1OE/xvXctw/cTO8WN2wFOD4U84EJsvLq8XPZzhMLiNiGhAfpPaP6tHcbCWHYQlsdFIi9TutoFwcmhMSBktFH+UfRdMuHxoU4IK7QJOazathDPNNtpzXGZm7z9vWxLx907NDWXwtrumSz+adklpmkoddQh2s8luRSRer5HKX6uS8YGUekPqKDsMWC2XLQ7VnQv8RRF9QL3/AKxbAUF232khynNuw5vHQRf/cnh6pXDlU83EdadeEI3qkhkNXcvUrOCFN9/miJrk2t7SyqduxoaUThFiq4NL+UgKSirY7TwIDAQABo1AwTjAMBgNVHRMEBTADAQH/MB0GA1UdDgQWBBTCzPqmyZ+DR3D7OykFvuCWi4q5RzAfBgNVHSMEGDAWgBTCzPqmyZ+DR3D7OykFvuCWi4q5RzANBgkqhkiG9w0BAQsFAAOCAQEALI5wyYx6hHxU0HZ/h9/J3Jw+9xCafGrjGHFhrPCi/eQ7aXSKNCzFK3nEnbAtrP7sKuXHpnjds+1JdDIpIBwmuvkYe6ZMfP0TI2jbxAfpHUI2zzWYVccYhHQI1afMwZ1NB5aIjUrIpDhBvp8soZgcXgZGkYNLXh/CDrsm4/DhsidUi6v9KUmr+z0lD3wcqunjqpDxoZYZw0QzMyiVYgak6XhUfeJ2jeJPu64LsJ3GgfMQZGIbMAPS/+52PjIPSdkPz70PHL8MtqLY4Avl3+JLLBB0quCjyeO+8qnuEpSy1my9cpNMVU4bgNFlibi41OX/kk5F5QvAbxU4mWAGUgHihg== Intel OpenStack SAML Identity Provider http://www.intel.com Intel davechen chen [email protected] 555-55-5555 urn:oasis:names:tc:SAML:2.0:nameid-format:transient
DB (Create a Service Provider (SP))
============================
The sp_url will be used when creating a SAML assertion for BETA and signed by the current keystone IdP. The auth_url is used to retrieve the token for BETA once the SAML assertion is sent. Although the enabled field is optional we are passing it set to true otherwise it will be set to false by default.
创建Service Provider
---------------------------
curl -s -X PUT -H "X-Auth-Token: 8d4bdadd037d4476a5b3afaa80ed4a2d" -H "Content-Type: application/json" -d '{"service_provider": {"auth_url": "http://10.239.48.152:5000/v3/OS-FEDERATION/identity_providers/beta/protocols/saml2/auth", "sp_url": "https://10.239.48.152:5000/Shibboleth.sso/SAML2/ECP", "enabled": true}}' http://10.239.159.68:5000/v3/OS-FEDERATION/service_providers/keystone-sp
auth_url指向SP, SP会对发过来的saml assertion进行验证,验证通过后会返回一个token,调用auth_url时通过对SP URL的访问进行重定向来完成的。
mysql> select * from service_provider;1 row in set (0.00 sec)
Test:
Get SAML assertion
--------------------------
curl -i -X POST -H "Content-Type:application/json" -d '{"auth": {"scope":{"service_provider": {"id": "keystone-sp"}},"identity": {"token": {"id":"8d4bdadd037d4476a5b3afaa80ed4a2d"}, "methods":["token"]}}}' http://10.239.159.68:5000/v3/auth/OS-FEDERATION/saml2/ecp
SP:
=====
keystone.conf
===========
[auth]
#
# From keystone
#
# Allowed authentication methods. (list value)
methods = external,password,token,oauth1,saml2
Setup Shibboleth
=============
/etc/shibboleth/shibboleth2.xml:
(与IDP数据库中SP的定义sp_url一致 )
SAML2 SAML1
SAML2 Local
(这个要与apache中的配置以及数据库中的配置相一致,这份配置中,由于用到的是默认的配置,所以ApplicationOverride id会是default, 而不是keystone-idp)
(entityID与DB中表idp_remote_idp的remote_id字段保持一致)
SAML2 SAML1
SAML2 Local
移除remote user,因为keystone external 认证方法,需要读到这个变量。
sudo sed -r 's/REMOTE_USER="\w*"//' -i /etc/shibboleth/shibboleth2.xml
virtual host配置
===========
Listen 5000
Listen 35357
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\" %D(us)" keystone_combined
ServerName shldeOTCopen005.sh.intel.com
WSGIDaemonProcess keystone-public processes=5 threads=1 user=dave display-name=%{GROUP}
WSGIProcessGroup keystone-public
WSGIScriptAlias / /var/www/keystone/main
WSGIScriptAliasMatch ^(/v3/OS-FEDERATION/identity_providers/.*?/protocols/.*?/auth)$ /var/www/keystone/main/$1
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
ErrorLogFormat "%{cu}t %M"
SetEnv Shib-Identity-Provider https://openstack-dev.sh.intel.com/v3/OS-FEDERATION/saml2/idp (keystone需要读到这个数据)
#SetEnv Shib-Identity-Provider keystone-idp-remote
ErrorLog /var/log/apache2/keystone.log
CustomLog /var/log/apache2/keystone_access.log keystone_combined
...
Alias /identity_admin /var/www/keystone/admin
SetHandler wsgi-script
Options +ExecCGI
WSGIProcessGroup keystone-admin
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
ProxyPass !
SetHandler shib
ShibRequestSetting requireSession 1
AuthType shibboleth
#ShibRequestSetting applicationId keystone-idp
ShibRequestSetting applicationId default (因为用到的是default设置,这里的idp的ID也是"default")
#ShibRequireAll On
#ShibRequireSession On
ShibExportAssertion Off
Require valid-user
AuthType shibboleth
Require valid-user
AuthType shibboleth
Require valid-user
配置‘remote_id_attribute’, 可选,编辑/etc/keystone/keystone.conf,在federation group下加入此配置,例如:
remote_id_attribute = Shib-Identity-Provider
DB
========
通过CURL或者OpenStack CLI创建IDP (略),mapping, protocols。
NOTE: 创建IDP需要指定remote ID,或者后期进行更新:
openstack identity provider set keystone-idp --remote-idhttps://openstack-dev.sh.intel.com/v3/OS-FEDERATION/saml2/idp.
在后期颁发令牌时,需要对remote ID进行验证。
Create mapping
--------------------
curl -g -i -X PUT http://10.239.48.152:35357/v3/OS-FEDERATION/mappings/map_1-H "Content-Type: application/json" -H "Accept:application/json" -H "X-Auth-Token:36182c0f39bd4121abab6be136ee5335" -d '{"mapping":{"rules": [{"local": [{"user": {"name":"{0}"}, "group": {"id":"44dc305d549f400293ffbe1c4e3c9be2"}}],"remote":[{"type": "openstack_user"}, {"type":"OTC_INTEL_GROUPIDS"}]}]}}'
Create Protocols
-----------------------
curl -g -i -X PUT http://10.239.48.152:35357/v3/OS-FEDERATION/identity_providers/keystone-idp/protocols/saml2-H "Content-Type: application/json" -H "Accept:application/json" -H "X-Auth-Token:f85031562b3645169c980f0a91e35c57" -d '{"protocol":{"mapping_id": "map_1"}}'
mysql> select * from identity_provider;
+--------------+---------+-------------+
| id | enabled | description |
+--------------+---------+-------------+
| idp_fake | 1 | NULL |
| keystone-idp | 1 | NULL |
+--------------+---------+-------------+
2 rows in set (0.01 sec)
mysql> select * from idp_remote_ids;
+--------------+---------------------------------------------------------------+
| idp_id | remote_id |
+--------------+---------------------------------------------------------------+
| keystone-idp | https://openstack-dev.sh.intel.com/v3/OS-FEDERATION/saml2/idp |
+--------------+---------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select * from federation_protocol;
+---------------+--------------+------------+
| id | idp_id | mapping_id |
+---------------+--------------+------------+
| protocol_fake | idp_fake | map_fake |
| saml2 | keystone-idp | map_1 |
+---------------+--------------+------------+
2 rows in set (0.00 sec)
mysql> select * from mapping;
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | rules |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| map_1 | "[{\"remote\": [{\"type\": \"openstack_user\"}, {\"type\": \"OTC_INTEL_GROUPIDS\"}], \"local\": [{\"group\": {\"id\": \"f25dbfb31c384a339d7318e361f08bf6\"}, \"user\": {\"name\": \"{0}\"}}]}]" |
| map_fake | "[{\"remote\": [{\"type\": \"UserName\"}, {\"type\": \"orgPersonType\", \"not_any_of\": [\"Contractor\", \"Guest\"]}], \"local\": [{\"user\": {\"name\": \"{0}\"}}, {\"group\": {\"id\": \"0cd5e9\"}}]}]" |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
测试脚本 (用于得到一个unscoped token)
=================================
import json
import os
from keystoneclient import session as ksc_session
from keystoneclient.auth.identity import v3
from keystoneclient.v3 import client as keystone_v3
class K2KClient(object):
def __init__(self):
# os sp id need set manually
#self.sp_id = os.environ.get('OS_SP_ID')
self.sp_id = 'keystone-sp'
self.token_id = os.environ.get('OS_TOKEN')
self.auth_url = os.environ.get('OS_AUTH_URL')
self.project_id = os.environ.get('OS_PROJECT_ID')
self.username = os.environ.get('OS_USERNAME')
self.password = os.environ.get('OS_PASSWORD')
#self.domain_id = os.environ.get('OS_DOMAIN_ID')
self.domain = os.environ.get('OS_DOMAIN')
def v3_authenticate(self):
auth = v3.Password(auth_url=self.auth_url,
username=self.username,
password=self.password,
user_domain_id='default',
project_id=self.project_id)
self.session = ksc_session.Session(auth=auth, verify=False)
self.session.auth.get_auth_ref(self.session)
self.token = self.session.auth.get_token(self.session)
def _generate_token_json(self):
return {
"auth": {
"identity": {
"methods": [
"token"
],
"token": {
#"id": self.token
"id": "6b627c39ba05469c9e3a8d52d732709d"
}
},
"scope": {
"service_provider": {
"id": self.sp_id
}
}
}
}
def _check_response(self, response):
if not response.ok:
raise Exception("Something went wrong, %s" % response.__dict__)
def get_saml2_ecp_assertion(self):
token = json.dumps(self._generate_token_json())
url = self.auth_url + '/auth/OS-FEDERATION/saml2/ecp'
print "url is: %s" % url
print "token is: %s" % token
r = self.session.post(url=url, data=token, verify=False)
self._check_response(r)
#import pdb
#pdb.set_trace()
self.assertion = str(r.text)
def _get_sp(self):
url = self.auth_url + '/OS-FEDERATION/service_providers/' + self.sp_id
r = self.session.get(url=url, verify=False)
self._check_response(r)
sp = json.loads(r.text)[u'service_provider']
return sp
def _handle_http_302_ecp_redirect(self, session, response, location, method, **kwargs):
#return session.get(location, authenticated=False, data=self.assertion, **kwargs)
return session.get(location, authenticated=False, **kwargs)
#location = response.headers['location']
#print "header is... %s" % location
#location = response.headers['location']
#return session.request(location, method, authenticated=False,
# **kwargs)
def exchange_assertion(self):
"""Send assertion to a Keystone SP and get token."""
sp = self._get_sp()
print "#################sp url is: %s" % sp[u'sp_url']
print "type is... %s" % type(self.assertion)
print "we need to stop here..."
response = self.session.post(
sp[u'sp_url'],
headers={'Content-Type': 'application/vnd.paos+xml'},
data=self.assertion,
authenticated=False,
redirect=False)
#import pdb
#pdb.set_trace()
self._check_response(response)
#location = response.headers['location']
#print "header is... %s" % location
#r = self._handle_http_302_ecp_redirect(r, sp[u'auth_url'],
# headers={'Content-Type':
# 'application/vnd.paos+xml'})
r = self._handle_http_302_ecp_redirect(self.session, response, sp[u'auth_url'],
method='GET',
headers={'Content-Type':
'application/vnd.paos+xml'})
self.fed_token_id = r.headers['X-Subject-Token']
self.fed_token = r.text
def main():
client = K2KClient()
print "step1"
client.v3_authenticate()
print "step2"
client.get_saml2_ecp_assertion()
print "step3"
print('ECP wrapped SAML assertion: %s' % client.assertion)
client.exchange_assertion()
print('Unscoped token id: %s' % client.fed_token_id)
if __name__ == "__main__":
main()
Trouble Shooting
-----------------------
1. Warning message:
InsecurePlatformWarning: A true SSLContext object is not
available. This prevents urllib3 from configuring SSL appropriately and
may cause certain SSL connections to fail
Solve it by:
pip install'requests[security]'
2. OpenSSL: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
错误发生在读取shibboleth的metadata的时候。
dave@shldeOTCopen005:~$ sudo wget --no-check-certificate -O metadata https://10.239.48.36/Shibboleth.sso/Metadata
Solve it by:
> su root
> a2dismod ssl
> a2enmod ssl (then it will be okay)
3. 交换SAML assertion的时候,如果遇到其它SSL的问题,可以将请求改为HTTP协议来规避一下。
Solve it by:
Update the SP url to: http://10.239.48.152/Shibboleth.sso/SAML2/ECP
4. mapping中不能包含一些未知的变量,否则在evaluate SAML assertion的时候,会失败。
例如:
更新mapping为如下形式:
mysql> select * from mapping;
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| id | rules |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
| map_1 | "[{\"remote\": [{\"type\": \"openstack_user\"}], \"local\": [{\"group\": {\"id\": \"44dc305d549f400293ffbe1c4e3c9be2\"}, \"user\": {\"name\": \"{0}\"}}]}]" |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
5. 找不到remote ID identifier,这个不配置也可以,但是如果配置,则需要在apache2的配置文件中进行定义。
例如:
[federation]
#
# From keystone
#
# environment. For `mod_shib`, this would be `Shib-Identity-Provider`. For For
# `mod_auth_openidc`, this could be `HTTP_OIDC_ISS`. For `mod_auth_mellon`,
# this could be `MELLON_IDP`. (string value)
#remote_id_attribute =
remote_id_attribute = Shib-Identity-Provider
6. OpenSAML.SecurityPolicyRule.MessageFlow [1]: rejected expired message, timestamp (1435761510), oldest allowed
这些信息是在shibboleth文件中找到的,终端抛出的是Internal Error.
这主要是SP和IDP的时间不一致导致的,可以通过调整时间,或者跟新shibboleth的配置来解决。
例如:修改配置 clockSkew="7200"
Reference:
http://shibboleth.net/pipermail/users/2015-July/022528.html
Shibboleth的日志文件存放位置:
/var/log/shibboleth/shibd.log
参考资料:
【1】http://docs.openstack.org/developer/keystone/configure_federation.html
【2】Useful docs:http://docs.openstack.org/developer/keystone/federation/websso.html
【3】shibboleth configure:http://docs.openstack.org/developer/keystone/federation/shibboleth.html