Fix improper restriction of XMl External Entity Reference, by using lxml
[osm/MON.git] / osm_mon / collector / utils / openstack.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright 2019 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
21 # For those usages not covered by the Apache License, Version 2.0 please
22 # contact: bdiaz@whitestack.com or glavado@whitestack.com
23 ##
24 import logging
25 from os import makedirs, path
26
27 from keystoneauth1 import session
28 from keystoneauth1.identity import v3
29
30 from osm_mon.core.exceptions import CertificateNotCreated
31
32 log = logging.getLogger(__name__)
33
34
35 class OpenstackUtils:
36 @staticmethod
37 def get_session(creds: dict):
38 verify_ssl = True
39 project_domain_name = "Default"
40 user_domain_name = "Default"
41 try:
42 if "config" in creds:
43 vim_config = creds["config"]
44 if "insecure" in vim_config and vim_config["insecure"]:
45 verify_ssl = False
46 if "ca_cert" in vim_config:
47 verify_ssl = vim_config["ca_cert"]
48 elif "ca_cert_content" in vim_config:
49 vim_config = OpenstackUtils._create_file_cert(
50 vim_config, creds["_id"]
51 )
52 verify_ssl = vim_config["ca_cert"]
53 if "project_domain_name" in vim_config:
54 project_domain_name = vim_config["project_domain_name"]
55 if "user_domain_name" in vim_config:
56 user_domain_name = vim_config["user_domain_name"]
57 auth = v3.Password(
58 auth_url=creds["vim_url"],
59 username=creds["vim_user"],
60 password=creds["vim_password"],
61 project_name=creds["vim_tenant_name"],
62 project_domain_name=project_domain_name,
63 user_domain_name=user_domain_name,
64 )
65 return session.Session(auth=auth, verify=verify_ssl, timeout=10)
66 except CertificateNotCreated as e:
67 log.error(e)
68
69 @staticmethod
70 def _create_file_cert(vim_config: dict, target_id: str) -> dict:
71 """
72 Process vim config, creating vim configuration files as ca_cert
73 Creates a folder '/app/osm_mon/certs/{target_id}' and the ca_cert inside
74 :param target_id: vim-id
75 :param db_vim: Vim dictionary obtained from database
76 :return: Modified vim configuration dictionary.
77 """
78
79 work_dir = f"/app/osm_mon/certs/{target_id}"
80 file_name = ""
81
82 try:
83 if vim_config.get("ca_cert_content"):
84 if not path.isdir(work_dir):
85 makedirs(work_dir)
86
87 file_name = f"{work_dir}/ca_cert"
88 with open(file_name, "w") as f:
89 f.write(vim_config["ca_cert_content"])
90 del vim_config["ca_cert_content"]
91 vim_config["ca_cert"] = file_name
92 return vim_config
93 except Exception as e:
94 if file_name:
95 raise CertificateNotCreated(f"Error writing to file '{file_name}': {e}")
96 else:
97 raise CertificateNotCreated(
98 f"Error creating the directory '{work_dir}': {e}"
99 )