Debian Codegraph @

doc/source/using-sessions.rst

raw
1 ==============
2 Using Sessions
3 ==============
4
5 Introduction
6 ============
7
8 The :py:class:`keystoneauth1.session.Session` class was introduced into
9 keystoneclient as an attempt to bring a unified interface to the various
10 OpenStack clients that share common authentication and request parameters
11 between a variety of services.
12
13 The model for using a Session and auth plugin as well as the general terms used
14 have been heavily inspired by the `requests <http://docs.python-requests.org>`_
15 library. However neither the Session class nor any of the authentication
16 plugins rely directly on those concepts from the requests library so you should
17 not expect a direct translation.
18
19 Features
20 --------
21
22 - Common client authentication
23
24 Authentication is handled by one of a variety of authentication plugins and
25 then this authentication information is shared between all the services that
26 use the same Session object.
27
28 - Security maintenance
29
30 Security code is maintained in a single place and reused between all
31 clients such that in the event of problems it can be fixed in a single
32 location.
33
34 - Standard discovery mechanisms
35
36 Clients are not expected to have any knowledge of an identity token or any
37 other form of identification credential. Service and endpoint discovery are
38 handled by the Session and plugins.
39
40
41 Sessions for Users
42 ==================
43
44 The Session object is the contact point to your OpenStack cloud services. It
45 stores the authentication credentials and connection information required to
46 communicate with OpenStack such that it can be reused to communicate with many
47 services. When creating services this Session object is passed to the client
48 so that it may use this information.
49
50 A Session will authenticate on demand. When a request that requires
51 authentication passes through the Session the authentication plugin will be
52 asked for a valid token. If a valid token is available it will be used
53 otherwise the authentication plugin may attempt to contact the authentication
54 service and fetch a new one.
55
56 An example from keystoneclient::
57
58 >>> from keystoneauth1.identity import v3
59 >>> from keystoneauth1 import session
60 >>> from keystoneclient.v3 import client
61
62 >>> auth = v3.Password(auth_url='https://my.keystone.com:5000/v3',
63 ... username='myuser',
64 ... password='mypassword',
65 ... project_id='proj',
66 ... user_domain_id='domain')
67 >>> sess = session.Session(auth=auth,
68 ... verify='/path/to/ca.cert')
69 >>> ks = client.Client(session=sess)
70 >>> users = ks.users.list()
71
72 As clients adopt this means of operating they will be created in a similar
73 fashion by passing the Session object to the client's constructor.
74
75
76 Migrating keystoneclient to use a Session
77 -----------------------------------------
78
79 By using a session with a keystoneclient Client we presume that you have opted
80 in to new behavior defined by the session. For example authentication is now
81 on-demand rather than on creation. To allow this change in behavior there are
82 a number of functions that have changed behavior or are no longer available.
83
84 For example the
85 :py:meth:`keystoneclient.httpclient.HTTPClient.authenticate` method used
86 to be able to always re-authenticate the current client and fetch a new token.
87 As this is now controlled by the Session and not the client this has changed,
88 however the function will still exist to provide compatibility with older
89 clients.
90
91 Likewise certain parameters such as ``user_id`` and ``auth_token`` that used to
92 be available on the client object post authentication will remain
93 uninitialized.
94
95 When converting an application to use a session object with keystoneclient you
96 should be aware of the possibility of changes to authentication and
97 authentication parameters and make sure to test your code thoroughly. It should
98 have no impact on the typical CRUD interaction with the client.
99
100
101 Sharing Authentication Plugins
102 ------------------------------
103
104 A session can only contain one authentication plugin however there is nothing
105 that specifically binds the authentication plugin to that session, a new
106 Session can be created that reuses the existing authentication plugin::
107
108 >>> new_sess = session.Session(auth=sess.auth,
109 verify='/path/to/different-cas.cert')
110
111 In this case we cannot know which session object will be used when the plugin
112 performs the authentication call so the command must be able to succeed with
113 either.
114
115 Authentication plugins can also be provided on a per-request basis. This will
116 be beneficial in a situation where a single session is juggling multiple
117 authentication credentials::
118
119 >>> sess.get('https://my.keystone.com:5000/v3',
120 auth=my_auth_plugin)
121
122 If an auth plugin is provided via parameter then it will override any auth
123 plugin on the session.
124
125 Sessions for Client Developers
126 ==============================
127
128 Sessions are intended to take away much of the hassle of dealing with
129 authentication data and token formats. Clients should be able to specify filter
130 parameters for selecting the endpoint and have the parsing of the catalog
131 managed for them.
132
133 Authentication
134 --------------
135
136 When making a request with a session object you can simply pass the keyword
137 parameter ``authenticated`` to indicate whether the argument should contain a
138 token, by default a token is included if an authentication plugin is available::
139
140 >>> # In keystone this route is unprotected by default
141 >>> resp = sess.get('https://my.keystone.com:5000/v3',
142 authenticated=False)
143
144
145 Service Discovery
146 -----------------
147
148 In OpenStack the URLs of available services are distributed to the user as a
149 part of the token they receive called the Service Catalog. Clients are expected
150 to use the URLs from the Service Catalog rather than have them provided.
151
152 In general a client does not need to know the full URL for the server that they
153 are communicating with, simply that it should send a request to a path
154 belonging to the correct service.
155
156 This is controlled by the ``endpoint_filter`` parameter to a request which
157 contains all the information an authentication plugin requires to determine the
158 correct URL to which to send a request. When using this mode only the path for
159 the request needs to be specified::
160
161 >>> resp = session.get('/v3/users',
162 endpoint_filter={'service_type': 'identity',
163 'interface': 'public',
164 'region_name': 'myregion'})
165
166 ``endpoint_filter`` accepts a number of arguments with which it can determine
167 an endpoint url:
168
169 - ``service_type``: the type of service. For example ``identity``, ``compute``,
170 ``volume`` or many other predefined identifiers.
171
172 - ``interface``: the network exposure the interface has. This will be one of:
173
174 - ``public``: An endpoint that is available to the wider internet or network.
175 - ``internal``: An endpoint that is only accessible within the private network.
176 - ``admin``: An endpoint to be used for administrative tasks.
177
178 - ``region_name``: the name of the region where the endpoint resides.
179
180 The endpoint filter is a simple key-value filter and can be provided with any
181 number of arguments. It is then up to the auth plugin to correctly use the
182 parameters it understands.
183
184 The session object determines the URL matching the filter and append to it the
185 provided path and so create a valid request. If multiple URL matches are found
186 then any one may be chosen.
187
188 While authentication plugins will endeavour to maintain a consistent set of
189 arguments for an ``endpoint_filter`` the concept of an authentication plugin is
190 purposefully generic and a specific mechanism may not know how to interpret
191 certain arguments and ignore them. For example the
192 :py:class:`keystoneauth1.identity.generic.token.Token` plugin (which is used
193 when you want to always use a specific endpoint and token combination) will
194 always return the same endpoint regardless of the parameters to
195 ``endpoint_filter`` or a custom OpenStack authentication mechanism may not have
196 the concept of multiple ``interface`` options and choose to ignore that
197 parameter.
198
199 There is some expectation on the user that they understand the limitations of
200 the authentication system they are using.