CAL refactoring
[osm/SO.git] / rwcal / plugins / vala / rwcal_openstack / rift / rwcal / openstack / session / auth_drv.py
1 #!/usr/bin/python
2
3 #
4 # Copyright 2017 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 from keystoneauth1.identity import v3
19 from keystoneauth1.identity import v2
20 import logging
21
22
23 class TokenDriver(object):
24 """
25 Class for token based authentication for openstack.
26
27 This is just placeholder for now
28 """
29 def __init__(self, version, logger=None, **kwargs):
30 """
31 Constructor for class
32 """
33 if logger is None:
34 self.log = logging.getLogger('rwcal.openstack.keystone.token')
35 self.log.setLevel(logging.DEBUG)
36 else:
37 self.log = logger
38
39
40 @property
41 def auth_handle(self):
42 return None
43
44 class PasswordDriver(object):
45 """
46 Class for password based authentication for openstack
47 """
48 def __init__(self, version, logger=None, **kwargs):
49 """
50 Constructor for class
51 Arguments:
52 version (str): Keystone API version to use
53 logger (instance of logging.Logger)
54 A dictionary of following key-value pairs
55 {
56 auth_url (string) : Keystone Auth URL
57 username (string) : Username for authentication
58 password (string) : Password for authentication
59 project_name (string) : Name of the project or tenant
60 project_domain_name (string) : Name of the project domain
61 user_domain_name (string) : Name of the user domain
62 logger (instance of logging.Logger)
63 }
64 Returns:
65 None
66 """
67 if logger is None:
68 self.log = logging.getLogger('rwcal.openstack.keystone.password')
69 self.log.setLevel(logging.DEBUG)
70 else:
71 self.log = logger
72
73 self.log = logger
74 version = int(float(version))
75
76 if version == 3:
77 self.log.info("Using keystone version 3 for authentication at URL: %s", kwargs['auth_url'])
78 self._auth = v3.Password(auth_url = kwargs['auth_url'],
79 username = kwargs['username'],
80 password = kwargs['password'],
81 project_name = kwargs['project_name'],
82 project_domain_name = kwargs['project_domain_name'],
83 user_domain_name = kwargs['user_domain_name'])
84 elif version == 2:
85 self.log.info("Using keystone version 2 for authentication at URL: %s", kwargs['auth_url'])
86 self._auth = v2.Password(auth_url = kwargs['auth_url'],
87 username = kwargs['username'],
88 password = kwargs['password'],
89 tenant_name = kwargs['project_name'])
90 @property
91 def auth_handle(self):
92 return self._auth
93
94
95 class AuthDriver(object):
96 """
97 Driver class for handling authentication plugins for openstack
98 """
99 AuthMethod = dict(
100 password=PasswordDriver,
101 token=TokenDriver,
102 )
103 def __init__(self, auth_type, version, logger = None, **kwargs):
104 """
105 auth_type (string): At this point, only "password" based
106 authentication is supported.
107 version (string): Keystone API version
108 logger (instance of logging.Logger)
109
110 kwargs a dictionary of following key/value pairs
111 {
112 username (string) : Username
113 password (string) : Password
114 auth_url (string) : Authentication URL
115 tenant_name(string): Tenant Name
116 user_domain_name (string) : User domain name
117 project_domain_name (string): Project domain name
118 region (string) : Region name
119 }
120 """
121 if logger is None:
122 self.log = logging.getLogger('rwcal.openstack.auth')
123 self.log.setLevel(logging.DEBUG)
124 else:
125 self.log = logger
126
127
128 self.log.info("Using %s authentication method", auth_type)
129 if auth_type not in AuthDriver.AuthMethod:
130 self.log.error("Unsupported authentication method %s", auth_type)
131 raise KeyError("Unsupported authentication method %s", auth_type)
132 else:
133 self._auth_method = AuthDriver.AuthMethod[auth_type](version, self.log, **kwargs)
134
135 @property
136 def auth_handle(self):
137 return self._auth_method.auth_handle
138
139
140
141