Seed code for yarf
[ta/yarf.git] / src / yarf / authentication / keystone.py
1 # Copyright 2019 Nokia
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15
16 from keystoneauth1.identity import v3
17 from keystoneauth1 import session
18 from keystoneclient.v3 import client
19 from keystoneclient.v3.tokens import TokenManager
20 from keystoneauth1.exceptions.http import Unauthorized, NotFound
21
22 import yarf.restfullogger as logger
23
24 from yarf.authentication.base_auth import BaseAuthMethod
25 from yarf.restfulargs import RestConfig
26
27
28 class KeystoneAuth(BaseAuthMethod):
29     def __init__(self):
30         super(KeystoneAuth, self).__init__()
31         self.logger = logger.get_logger()
32         config = RestConfig()
33         config.parse()
34         conf = config.get_section("keystone", format='dict')
35         try:
36             self.user = conf["user"]
37             self.password = conf["password"]
38             self.uri = conf["auth_uri"] + '/v3'
39             self.domain = "default"
40         except KeyError as error:
41             self.logger.error("Failed to find all the needed parameters. Authentication with Keystone not possible: {}"
42                               .format(str(error)))
43         self.auth = v3.Password(auth_url=self.uri,
44                                 username=self.user,
45                                 password=self.password,
46                                 user_domain_id=self.domain)
47         self.sess = session.Session(auth=self.auth)
48         self.keystone = client.Client(session=self.sess)
49         self.tokenmanager = TokenManager(self.keystone)
50
51     def get_authentication(self, req):
52         try:
53             token = req.headers.get("X-Auth-Token", type=str)
54         except KeyError:
55             self.logger.error("Failed to get the authentication token from request")
56             return (False, "")
57
58         try:
59             tokeninfo = self.tokenmanager.validate(token)
60         except Unauthorized:
61             self.logger.error("Failed to authenticate with given credentials")
62             return (False, "")
63         except NotFound:
64             self.logger.error("Unauthorized token")
65             return (False, "")
66         except Exception as error:
67             self.logger.error("Failure: {}".format(str(error)))
68             return (False, "")
69
70         if 'admin' in tokeninfo.role_names:
71             return (True, 'admin')
72         return (False, "")