blob: f591a707a1c045dee7d4f5d1170072e05372bdc5 [file] [log] [blame]
tiernod125caf2018-11-22 16:05:54 +00001# Licensed under the Apache License, Version 2.0 (the "License");
2# you may not use this file except in compliance with the License.
3# You may obtain a copy of the License at
4#
5# http://www.apache.org/licenses/LICENSE-2.0
6#
7# Unless required by applicable law or agreed to in writing, software
8# distributed under the License is distributed on an "AS IS" BASIS,
9# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
10# implied.
11# See the License for the specific language governing permissions and
12# limitations under the License.
13
tiernoc94c3df2018-02-09 15:38:54 +010014"""
15Contains html text in variables to make and html response
16"""
17
18import yaml
19from http import HTTPStatus
tiernoef4e2242018-10-31 17:09:52 +010020from html import escape as html_escape
tiernoc94c3df2018-02-09 15:38:54 +010021
22__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
23
24html_start = """
25 <!DOCTYPE html>
26<html>
27<head>
28 <link href="/osm/static/style.css" rel="stylesheet">
K Sai Kiran8748af52020-06-25 16:23:32 +053029 <title>Welcome to OSM</title>
30 <link rel="shortcut icon" href="/osm/static/favicon.ico">
tiernoc94c3df2018-02-09 15:38:54 +010031</head>
32<body>
33 <div id="osm_topmenu">
34 <div>
tierno2236d202018-05-16 19:05:16 +020035 <a href="https://osm.etsi.org"> <img src="/osm/static/OSM-logo.png" height="42" width="100"
36 style="vertical-align:middle"> </a>
tiernoc94c3df2018-02-09 15:38:54 +010037 <a>( {} )</a>
tierno36ec8602018-11-02 17:27:11 +010038 <a href="/osm/pdu/v1/pdu_descriptors">PDUs </a>
tiernob24258a2018-10-04 18:39:49 +020039 <a href="/osm/vnfpkgm/v1/vnf_packages">VNFDs </a>
40 <a href="/osm/nsd/v1/ns_descriptors">NSDs </a>
41 <a href="/osm/nslcm/v1/ns_instances">NSs </a>
Felipe Vicensb57758d2018-10-16 16:00:20 +020042 <a href="/osm/nst/v1/netslice_templates">NSTDs </a>
Felipe Vicens07f31722018-10-29 15:16:44 +010043 <a href="/osm/nsilcm/v1/netslice_instances">NSIs </a>
tiernof27c79b2018-03-12 17:08:42 +010044 <a href="/osm/admin/v1/users">USERs </a>
45 <a href="/osm/admin/v1/projects">PROJECTs </a>
46 <a href="/osm/admin/v1/tokens">TOKENs </a>
tiernof759d822018-06-11 18:54:54 +020047 <a href="/osm/admin/v1/vim_accounts">VIMs </a>
tierno55ba2e62018-12-11 17:22:22 +000048 <a href="/osm/admin/v1/wim_accounts">WIMs </a>
tiernof759d822018-06-11 18:54:54 +020049 <a href="/osm/admin/v1/sdns">SDNs </a>
tierno7fd24872019-12-10 10:57:17 +000050 <a href="/osm/admin/v1/k8sclusters">K8s_clusters </a>
tiernoc67b0e92019-11-05 12:45:29 +000051 <a href="/osm/admin/v1/k8srepos">K8s_repos </a>
K Sai Kiran8748af52020-06-25 16:23:32 +053052 <a href="/osm/nslcm/v1/subscriptions">NS_Subs </a>
tiernof27c79b2018-03-12 17:08:42 +010053 <a href="/osm/admin/v1/tokens?METHOD=DELETE">logout </a>
tiernoc94c3df2018-02-09 15:38:54 +010054 </div>
55 </div>
56"""
57
58html_body = """
59<h1>{item}</h1>
60"""
61
62html_end = """
63</body>
tierno2236d202018-05-16 19:05:16 +020064</html>
tiernoc94c3df2018-02-09 15:38:54 +010065"""
66
67html_body_error = "<h2> Error <pre>{}</pre> </h2>"
68
69
tiernoc94c3df2018-02-09 15:38:54 +010070html_auth2 = """
71<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
72<html>
73<head><META http-equiv="Content-Type" content="text/html; charset=UTF-8">
74 <link href="/osm/static/style.css" rel="stylesheet">
75 <title>OSM Login</title>
K Sai Kiran8748af52020-06-25 16:23:32 +053076 <link rel="shortcut icon" href="/osm/static/favicon.ico">
tiernoc94c3df2018-02-09 15:38:54 +010077</head>
78<body>
79 <div id="osm_header">
80 <div>
81 <a href="https://osm.etsi.org"> <h1><img src="/osm/static/OSM-logo.png" style="vertical-align:middle"></h1> </a>
82 </div>
83 </div>
84 <div id="osm_error_message">
85 <h1>{error}</h1>
86 </div>
87 <div class="gerritBody" id="osm_body">
88 <h1>Sign in to OSM</h1>
tiernof27c79b2018-03-12 17:08:42 +010089 <form action="/osm/admin/v1/tokens" id="login_form" method="POST">
tiernoc94c3df2018-02-09 15:38:54 +010090 <table style="border: 0;">
91 <tr><th>Username</th><td><input id="f_user" name="username" size="25" tabindex="1" type="text"></td></tr>
92 <tr><th>Password</th><td><input id="f_pass" name="password" size="25" tabindex="2" type="password"></td></tr>
93 <tr><td><input tabindex="3" type="submit" value="Sign In"></td></tr>
94 </table>
95 </form>
96 <div style="clear: both; margin-top: 15px; padding-top: 2px; margin-bottom: 15px;">
97 <div id="osm_footer">
98 <div></div>
99 </div>
100 </div>
101 </div>
102 <script src="/osm/static/login.js"> </script>
103</body>
104</html>
105"""
106
107html_upload_body = """
108<form action="/osm{}" method="post" enctype="multipart/form-data">
109 <h3> <table style="border: 0;"> <tr>
110 <td> Upload {} descriptor (tar.gz) file: <input type="file" name="descriptor_file"/> </td>
111 <td> <input type="submit" value="Upload"/> </td>
112 </tr> </table> </h3>
113</form>
114"""
115
tierno65acb4d2018-04-06 16:42:40 +0200116html_nslcmop_body = """
117<a href="/osm/nslcm/v1/ns_lcm_op_occs?nsInstanceId={id}">nslcm operations </a>
tiernof759d822018-06-11 18:54:54 +0200118<a href="/osm/nslcm/v1/vnf_instances?nsr-id-ref={id}">VNFRS </a>
tierno65acb4d2018-04-06 16:42:40 +0200119<form action="/osm/nslcm/v1/ns_instances/{id}/terminate" method="post" enctype="multipart/form-data">
120 <h3> <table style="border: 0;"> <tr>
121 <td> <input type="submit" value="Terminate"/> </td>
122 </tr> </table> </h3>
123</form>
124"""
125
Felipe Vicens07f31722018-10-29 15:16:44 +0100126html_nsilcmop_body = """
Felipe Vicens126af572019-06-05 19:13:04 +0200127<a href="/osm/nsilcm/v1/nsi_lcm_op_occs?netsliceInstanceId={id}">nsilcm operations </a>
Felipe Vicens07f31722018-10-29 15:16:44 +0100128<form action="/osm/nsilcm/v1/netslice_instances/{id}/terminate" method="post" enctype="multipart/form-data">
129 <h3> <table style="border: 0;"> <tr>
130 <td> <input type="submit" value="Terminate"/> </td>
131 </tr> </table> </h3>
132</form>
133"""
134
garciadeblas4568a372021-03-24 09:19:48 +0100135html_vnfpackage_body = (
136 """<a href="/osm/vnfpkgm/v1/vnf_packages/{id}/artifacts">Artifacts </a>"""
137)
138html_nspackage_body = (
139 """<a href="/osm/nsd/v1/ns_descriptors/{id}/artifacts">Artifacts </a>"""
140)
tierno74b53582020-06-18 10:52:37 +0000141
tiernoc94c3df2018-02-09 15:38:54 +0100142
tierno701018c2019-06-25 11:13:14 +0000143def format(data, request, response, toke_info):
tiernoc94c3df2018-02-09 15:38:54 +0100144 """
145 Format a nice html response, depending on the data
146 :param data:
147 :param request: cherrypy request
148 :param response: cherrypy response
149 :return: string with teh html response
150 """
garciadeblas4568a372021-03-24 09:19:48 +0100151 response.headers["Content-Type"] = "text/html"
tiernoc94c3df2018-02-09 15:38:54 +0100152 if response.status == HTTPStatus.UNAUTHORIZED.value:
garciadeblas4568a372021-03-24 09:19:48 +0100153 if response.headers.get("WWW-Authenticate") and request.config.get(
154 "auth.allow_basic_authentication"
155 ):
156 response.headers["WWW-Authenticate"] = (
157 "Basic" + response.headers["WWW-Authenticate"][6:]
158 )
tiernoc94c3df2018-02-09 15:38:54 +0100159 return
160 else:
161 return html_auth2.format(error=data)
tierno5792d7d2019-08-30 15:37:12 +0000162 if request.path_info in ("/version", "/system"):
garciadeblas4568a372021-03-24 09:19:48 +0100163 return (
164 "<pre>"
165 + yaml.safe_dump(
166 data, explicit_start=False, indent=4, default_flow_style=False
167 )
168 + "</pre>"
169 )
tierno23453872020-07-21 12:01:02 +0000170 body = html_body.format(item=html_escape(request.path_info))
tiernoc94c3df2018-02-09 15:38:54 +0100171 if response.status and response.status > 202:
tierno23453872020-07-21 12:01:02 +0000172 # input request.path_info (URL) can contain XSS that are translated into output error detail
garciadeblas4568a372021-03-24 09:19:48 +0100173 body += html_body_error.format(
174 html_escape(
175 yaml.safe_dump(
176 data, explicit_start=True, indent=4, default_flow_style=False
177 )
178 )
179 )
tiernoc94c3df2018-02-09 15:38:54 +0100180 elif isinstance(data, (list, tuple)):
tiernob24258a2018-10-04 18:39:49 +0200181 if request.path_info == "/vnfpkgm/v1/vnf_packages":
tiernoc67b0e92019-11-05 12:45:29 +0000182 body += html_upload_body.format(request.path_info + "_content", "VNFD")
tiernob24258a2018-10-04 18:39:49 +0200183 elif request.path_info == "/nsd/v1/ns_descriptors":
184 body += html_upload_body.format(request.path_info + "_content", "NSD")
Felipe Vicensb57758d2018-10-16 16:00:20 +0200185 elif request.path_info == "/nst/v1/nst_templates":
186 body += html_upload_body.format(request.path_info + "_content", "NSTD")
tiernoc94c3df2018-02-09 15:38:54 +0100187 for k in data:
tiernof27c79b2018-03-12 17:08:42 +0100188 if isinstance(k, dict):
189 data_id = k.pop("_id", None)
190 elif isinstance(k, str):
191 data_id = k
garciadeblas4568a372021-03-24 09:19:48 +0100192 body += '<p> <a href="/osm/{url}/{id}">{id}</a>: {t} </p>'.format(
193 url=request.path_info, id=data_id, t=html_escape(str(k))
194 )
tiernoc94c3df2018-02-09 15:38:54 +0100195 elif isinstance(data, dict):
196 if "Location" in response.headers:
197 body += '<a href="{}"> show </a>'.format(response.headers["Location"])
198 else:
garciadeblasf2af4a12023-01-24 16:56:54 +0100199 _id = request.path_info[request.path_info.rfind("/") + 1 :]
bravofc26740a2021-11-08 09:44:54 -0300200 body += (
201 '<a href="/osm/{}?METHOD=DELETE"> '
202 '<img src="/osm/static/delete.png" height="25" width="25"> </a>'
garciadeblasf2af4a12023-01-24 16:56:54 +0100203 ).format(request.path_info)
garciadeblas4568a372021-03-24 09:19:48 +0100204 if request.path_info.startswith(
205 "/nslcm/v1/ns_instances_content/"
206 ) or request.path_info.startswith("/nslcm/v1/ns_instances/"):
tierno65acb4d2018-04-06 16:42:40 +0200207 body += html_nslcmop_body.format(id=_id)
garciadeblas4568a372021-03-24 09:19:48 +0100208 elif request.path_info.startswith(
209 "/nsilcm/v1/netslice_instances_content/"
210 ) or request.path_info.startswith("/nsilcm/v1/netslice_instances/"):
Felipe Vicens07f31722018-10-29 15:16:44 +0100211 body += html_nsilcmop_body.format(id=_id)
garciadeblas4568a372021-03-24 09:19:48 +0100212 elif request.path_info.startswith(
213 "/vnfpkgm/v1/vnf_packages/"
214 ) or request.path_info.startswith("/vnfpkgm/v1/vnf_packages_content/"):
tierno74b53582020-06-18 10:52:37 +0000215 body += html_vnfpackage_body.format(id=_id)
garciadeblas4568a372021-03-24 09:19:48 +0100216 elif request.path_info.startswith(
217 "/nsd/v1/ns_descriptors/"
218 ) or request.path_info.startswith("/nsd/v1/ns_descriptors_content/"):
tierno74b53582020-06-18 10:52:37 +0000219 body += html_nspackage_body.format(id=_id)
garciadeblas4568a372021-03-24 09:19:48 +0100220 body += (
221 "<pre>"
222 + html_escape(
223 yaml.safe_dump(
224 data, explicit_start=True, indent=4, default_flow_style=False
225 )
226 )
227 + "</pre>"
228 )
tierno0f98af52018-03-19 10:28:22 +0100229 elif data is None:
230 if request.method == "DELETE" or "METHOD=DELETE" in request.query_string:
231 body += "<pre> deleted </pre>"
tiernoc94c3df2018-02-09 15:38:54 +0100232 else:
tiernoef4e2242018-10-31 17:09:52 +0100233 body = html_escape(str(data))
tiernoc94c3df2018-02-09 15:38:54 +0100234 user_text = " "
tierno701018c2019-06-25 11:13:14 +0000235 if toke_info:
236 if toke_info.get("username"):
237 user_text += "user: {}".format(toke_info.get("username"))
238 if toke_info.get("project_id"):
tierno19ee3b02019-08-28 13:30:23 +0000239 user_text += ", project: {}".format(toke_info.get("project_name"))
tiernoc94c3df2018-02-09 15:38:54 +0100240 return html_start.format(user_text) + body + html_end
tierno2236d202018-05-16 19:05:16 +0200241 # yaml.safe_dump(data, explicit_start=True, indent=4, default_flow_style=False)
tiernoc94c3df2018-02-09 15:38:54 +0100242 # tags=False,
243 # encoding='utf-8', allow_unicode=True)