1 # ============LICENSE_START===================================================
2 # Copyright (C) 2019-2021 Nordix Foundation.
3 # ============================================================================
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 # SPDX-License-Identifier: Apache-2.0
17 # ============LICENSE_END=====================================================
23 def add_onap_ca_cert():
24 cafile = certifi.where()
25 dir_path = os.path.dirname(os.path.realpath(__file__))
26 datarouter_ca = dir_path + '/onap_ca_cert.pem'
27 with open(datarouter_ca, 'rb') as infile:
28 customca = infile.read()
30 with open(cafile, 'ab') as outfile:
31 outfile.write(customca)
33 print("Added DR Cert to CA")
36 def remove_onap_ca_cert():
37 cafile = certifi.where()
38 number_of_lines_to_delete = 40
40 dr_cert_exists = False
42 with open(cafile, 'r+b', buffering=0) as outfile:
43 for line in outfile.readlines()[-36:-35]:
44 if '# Serial: 0x9EAEEDC0A7CEB59D'.encode() in line:
47 outfile.seek(0, os.SEEK_END)
49 while outfile.tell() > 0:
50 outfile.seek(-1, os.SEEK_CUR)
51 char = outfile.read(1)
54 if count == number_of_lines_to_delete:
57 "Removed " + str(number_of_lines_to_delete) + " lines from end of CA File")
59 outfile.seek(-1, os.SEEK_CUR)
61 print("No DR cert in CA File to remove")
63 if count < number_of_lines_to_delete + 1:
64 print("Number of lines in file less than number of lines to delete. Exiting...")