天天看點

[openstack][G版]keystone源碼學習

       Keystone項目的主要目的是給整個openstack的各個元件(項目)提供一個統一的驗證方式。使用者管理,身份驗證是幾乎所有的軟體管理都要處理的問題,Keystone對于通常的應用場景所不同的是他要解決分布式環境下的統一認證。

       Keystone的程式結構也是采用openstack通常所用的manager,driver方式,這中方式的一個好處是driver可以被靈活的替換,通過配置的方式動态替換。Keystone 的實作方式同其他openstack項目不太一樣的是,他使用了了一個python語言的特性   

def__getattr__(self, name):
        """Forward calls to theunderlying driver."""
        # NOTE(termie): context is the firstargument, we're going to strip
        #               that for now, in the futurewe'll probably do some
        #               logging and whatnot in thisclass
        f = getattr(self.driver, name)
 
        @functools.wraps(f)
        def _wrapper(context, *args, **kw):
            return f(*args, **kw)
        setattr(self, name, _wrapper)
        return _wrapper
           

讓driver中的方法直接融入進manager類。在keystone中,驗證資料的存儲方式都有很多種,有sql資料庫形式,kvs形式(Dict在記憶體中存儲)等,一種driver就是對應一種存儲形式。

      簡單介紹一下各個子產品的功能,auth實作通過插件的方式實作驗證功能,Keystone/auth/plugins目錄下password.py,token.py實作了密碼和token兩個驗證方法,你也可以通過繼承AuthMethodHandler類實作自己的驗證方法,作為插件插入驗證鍊中。Catalog主要實作如構造Catalog等相關功能,實作endpoints的注冊,提供endpoints的發現機制。對應于每個服務(如:identity,volume,image,compute等)都會有一個對應的endpoints,如對應compute服務的endpoints為如下格式

。policy提供了基于規則的驗證引擎和驗證管理接口。Token提供對于Token的生成删除等相關功能。Trust提供級聯授權的相關功能,感興趣的朋友可以看下這個文檔https://github.com/openstack/identity-api/blob/master/openstack-identity-api/src/markdown/identity-api-v3-os-trust-ext.md。

        實際的openstack的項目,對于keystone的應用是通過WSGImiddleware 這種可插拔的方式來實作的,具體就是通過paste 配置實作keystone驗證功能的filter或app。以nova對于keystone的使用為例,nova實際是加了authtoken這個filter,authtoken的實際是在keystone的client api (python-keystoneclient-master)keystoneclient/middleware/auth_token.py檔案中實作的,其中filter_factory,app_factory調用AuthProtocol類來具體實作相關功能。(注:這裡對于WSGI,和paste,以及openstack對其的實作的方式不作具體講解了)。

       Keystone V3 版本的api改動比較大,v3 api現在還沒有完全實作,這裡有一個v3api的文檔https://github.com/openstack/identity-api/blob/master/openstack-identity-api/src/markdown/identity-api-v3.md。

繼續閱讀