Coverage for k8sclient/sans_parser.py : 86%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# ============LICENSE_START=======================================================
2# org.onap.dcae
3# ================================================================================
4# Copyright (c) 2021 Nokia. All rights reserved.
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# ============LICENSE_END=========================================================
19from uritools import urisplit
20from fqdn import FQDN
21import validators
22from validators.utils import ValidationFailure
25class SansParser:
26 def parse_sans(self, sans):
27 """
28 Method for parsing sans. As input require SANs separated by comma (,)
29 Return Map with sorted SANs by type:
30 ips -> IPv4 or IPv6
31 dnss -> dns name
32 emails -> email
33 uris -> uri
35 Example usage:
36 SansParser().parse_sans("example.org,onap@onap.org,127.0.0.1,onap://cluster.local/")
37 Output: { "ips": [127.0.0.1],
38 "uris": [onap://cluster.local/],
39 "dnss": [example.org],
40 "emails": [onap@onap.org]}
41 """
42 sans_map = {"ips": [],
43 "uris": [],
44 "dnss": [],
45 "emails": []}
46 sans_arr = sans.split(",")
47 for san in sans_arr:
48 if self._is_email(san):
49 sans_map["emails"].append(san)
50 elif self._is_ip_v4(san) or self._is_ip_v6(san):
51 sans_map["ips"].append(san)
52 elif self._is_dns(san):
53 sans_map["dnss"].append(san)
54 elif self._is_uri(san): 54 ↛ 47line 54 didn't jump to line 47, because the condition on line 54 was never false
55 sans_map["uris"].append(san)
57 return sans_map
59 def _is_email(self, san):
60 try:
61 return validators.email(san)
62 except ValidationFailure:
63 return False
65 def _is_ip_v4(self, san):
66 try:
67 return validators.ipv4(san)
68 except ValidationFailure:
69 return False
71 def _is_ip_v6(self, san):
72 try:
73 return validators.ipv6(san)
74 except ValidationFailure:
75 return False
77 def _is_uri(self, san):
78 parts = urisplit(san)
79 return parts.isuri()
81 def _is_dns(self, san):
82 fqdn = FQDN(san, min_labels=1)
83 return fqdn.is_valid