Fix improper restriction of XMl External Entity Reference, by using lxml
[osm/MON.git] / osm_mon / core / keystone.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2021 Whitestack, LLC
4 # *************************************************************
5
6 # This file is part of OSM Monitoring module
7 # All Rights Reserved to Whitestack, LLC
8
9 # Licensed under the Apache License, Version 2.0 (the "License"); you may
10 # not use this file except in compliance with the License. You may obtain
11 # a copy of the License at
12
13 # http://www.apache.org/licenses/LICENSE-2.0
14
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
18 # License for the specific language governing permissions and limitations
19 # under the License.
20 # For those usages not covered by the Apache License, Version 2.0 please
21 # contact: fbravo@whitestack.com
22 ##
23
24 from keystoneauth1.identity import v3
25 from keystoneauth1 import session
26 from keystoneclient.v3 import client
27
28
29 class KeystoneConnection:
30 """
31 Object representing a connection with keystone, it's main use is to collect
32 projects and users from the OSM platform stored in keystone instead MongoDB
33 """
34
35 def __init__(self, config):
36 self.auth_url = config.get("keystone", "url")
37 self.username = config.get("keystone", "service_user")
38 self.project_name = config.get("keystone", "service_project")
39 self.project_domain_name_list = config.get(
40 "keystone", "service_project_domain_name"
41 ).split(",")
42 self.password = config.get("keystone", "service_password")
43 self.user_domain_name_list = config.get("keystone", "domain_name").split(",")
44
45 self.auth = v3.Password(
46 auth_url=self.auth_url,
47 user_domain_name=self.user_domain_name_list[0],
48 username=self.username,
49 password=self.password,
50 project_domain_name=self.project_domain_name_list[0],
51 project_name=self.project_name,
52 )
53
54 self.keystone_session = session.Session(auth=self.auth)
55 self.keystone_client = client.Client(
56 session=self.keystone_session, endpoint_override=self.auth_url
57 )
58
59 def getProjects(self):
60 """
61 Grabs projects from keystone using the client and session build in the constructor
62 """
63 return self.keystone_client.projects.list()
64
65 def getProjectsById(self, user_id):
66 """
67 Grabs projects filtered by user ID from keystone using the client and session build in the constructor
68 """
69 return self.keystone_client.projects.list(user=user_id)
70
71 def getUsers(self):
72 """
73 Grabs users from keystone using the client and session build in the constructor
74 """
75 domain_list = self.keystone_client.domains.list()
76 users = []
77 for domain in domain_list:
78 users.extend(self.keystone_client.users.list(domain=domain.id))
79 return users
80
81 def getUserById(self, user_id):
82 """
83 Grabs user object from keystone using user id
84 """
85 return self.keystone_client.users.get(user_id)
86
87 def getRoleById(self, role_id):
88 """
89 Grabs role object from keystone using id
90 """
91 return self.keystone_client.roles.get(role_id)
92
93 def getRoleByName(self, role):
94 """
95 Grabs role object from keystone using name
96 """
97 return self.keystone_client.roles.list(name=role)
98
99 def getProjectsByProjectId(self, project_id):
100 """
101 Grabs projects object from keystone using id
102 """
103 return self.keystone_client.projects.get(project_id)
104
105 def getProjectsByProjectName(self, project):
106 """
107 Grabs projects object from keystone name
108 """
109 return self.keystone_client.projects.list(name=project)