blob: a378a84725a141e33deffaab55bd595652893b5d [file] [log] [blame]
Rahul Maheshwari984791c2018-09-21 00:49:37 -05001*** Settings ***
2Documentation Certificate utilities keywords.
3
4Library OperatingSystem
5Resource rest_client.robot
Sandhya Somashekar839a0c22019-01-31 05:05:43 -06006Resource resource.robot
Rahul Maheshwari984791c2018-09-21 00:49:37 -05007
8
9*** Keywords ***
10
11Install Certificate File On BMC
12 [Documentation] Install certificate file in BMC using REST PUT operation.
13 [Arguments] ${uri} ${status}=ok ${quiet}=${1} &{kwargs}
14
15 # Description of argument(s):
16 # uri URI for installing certificate file via REST
17 # e.g. "/xyz/openbmc_project/certs/server/https".
18 # status Expected status of certificate installation via REST
19 # e.g. error, ok.
20 # quiet If enabled, turns off logging to console.
21 # kwargs A dictionary of keys/values to be passed directly to
22 # PUT Request.
23
24 Initialize OpenBMC quiet=${quiet}
25
26 ${headers}= Create Dictionary Content-Type=application/octet-stream
Sridevi Ramesheadeef02019-01-17 08:56:18 -060027 ... X-Auth-Token=${XAUTH_TOKEN}
Rahul Maheshwari984791c2018-09-21 00:49:37 -050028 Set To Dictionary ${kwargs} headers ${headers}
29
30 Run Keyword If '${quiet}' == '${0}' Log Request method=Put
31 ... base_uri=${uri} args=&{kwargs}
32
33 ${ret}= Put Request openbmc ${uri} &{kwargs}
34 Run Keyword If '${quiet}' == '${0}' Log Response ${ret}
35
36 Run Keyword If '${status}' == 'ok'
37 ... Should Be Equal As Strings ${ret.status_code} ${HTTP_OK}
38 ... ELSE IF '${status}' == 'error'
39 ... Should Be Equal As Strings ${ret.status_code} ${HTTP_BAD_REQUEST}
40
41 Delete All Sessions
42
43
44Get Certificate Content From BMC Via Openssl
45 [Documentation] Get certificate content from BMC via openssl.
46
47 Check If Openssl Tool Exist
48
49 ${openssl_cmd}= Catenate
Rahul Maheshwari2a848cf2019-05-31 09:46:22 -050050 ... timeout 10 openssl s_client -connect ${OPENBMC_HOST}:443 -showcerts
51 ${output}= Run ${openssl_cmd}
Rahul Maheshwari984791c2018-09-21 00:49:37 -050052
53 ${result}= Fetch From Left
54 ... ${output} -----END CERTIFICATE-----
55 ${result}= Fetch From Right ${result} -----BEGIN CERTIFICATE-----
56 [Return] ${result}
57
58
Rahul Maheshwari081eadb2018-10-26 03:11:10 -050059Get Certificate File Content From BMC
60 [Documentation] Get required certificate file content from BMC.
61 [Arguments] ${cert_type}=Client
Rahul Maheshwari984791c2018-09-21 00:49:37 -050062
Rahul Maheshwari081eadb2018-10-26 03:11:10 -050063 # Description of argument(s):
64 # cert_type Certificate type (e.g. "Client" or "CA").
65
66 ${certificate} ${stderr} ${rc}= Run Keyword If '${cert_type}' == 'Client'
67 ... BMC Execute Command cat /etc/nslcd/certs/cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -050068
69 [Return] ${certificate}
70
71
72Generate Certificate File Via Openssl
73 [Documentation] Create certificate file via openssl with required content
74 ... and returns its path.
Rahul Maheshwari665bc612018-10-24 04:57:53 -050075 [Arguments] ${cert_format} ${time}=365 ${cert_dir_name}=certificate_dir
Rahul Maheshwari984791c2018-09-21 00:49:37 -050076
77 # Description of argument(s):
78 # cert_format Certificate file format
79 # e.g. Valid_Certificate_Empty_Privatekey.
80 # time Number of days to certify the certificate for.
Rahul Maheshwari665bc612018-10-24 04:57:53 -050081 # cert_dir_name The name of the sub-directory where the certificate
82 # is stored.
Rahul Maheshwari984791c2018-09-21 00:49:37 -050083
84 Check If Openssl Tool Exist
85
86 ${openssl_cmd}= Catenate openssl req -x509 -sha256 -newkey rsa:2048
87 ... ${SPACE}-nodes -days ${time}
Rahul Maheshwari665bc612018-10-24 04:57:53 -050088 ... ${SPACE}-keyout ${cert_dir_name}/cert.pem -out ${cert_dir_name}/cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -050089 ... ${SPACE}-subj "/O=XYZ Corporation /CN=www.xyz.com"
90
91 ${rc} ${output}= Run And Return RC and Output ${openssl_cmd}
92 Should Be Equal ${rc} ${0} msg=${output}
Rahul Maheshwari665bc612018-10-24 04:57:53 -050093 OperatingSystem.File Should Exist
94 ... ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -050095
Rahul Maheshwari665bc612018-10-24 04:57:53 -050096 ${file_content}= OperatingSystem.Get File
97 ... ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -050098 ${result}= Fetch From Left ${file_content} -----END CERTIFICATE-----
99 ${cert_content}= Fetch From Right ${result} -----BEGIN CERTIFICATE-----
100
101 ${result}= Fetch From Left ${file_content} -----END PRIVATE KEY-----
102 ${private_key_content}= Fetch From Right ${result} -----BEGIN PRIVATE KEY-----
103
104 ${cert_data}=
105 ... Run Keyword if '${cert_format}' == 'Valid Certificate Valid Privatekey'
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500106 ... OperatingSystem.Get File ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500107 ... ELSE IF '${cert_format}' == 'Empty Certificate Valid Privatekey'
108 ... Remove String ${file_content} ${cert_content}
109 ... ELSE IF '${cert_format}' == 'Valid Certificate Empty Privatekey'
110 ... Remove String ${file_content} ${private_key_content}
111 ... ELSE IF '${cert_format}' == 'Empty Certificate Empty Privatekey'
112 ... Remove String ${file_content} ${cert_content} ${private_key_content}
113 ... ELSE IF '${cert_format}' == 'Expired Certificate'
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500114 ... OperatingSystem.Get File ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari081eadb2018-10-26 03:11:10 -0500115 ... ELSE IF '${cert_format}' == 'Valid Certificate'
116 ... Remove String ${file_content} ${private_key_content}
117 ... -----BEGIN PRIVATE KEY----- -----END PRIVATE KEY-----
118 ... ELSE IF '${cert_format}' == 'Empty Certificate'
119 ... Remove String ${file_content} ${cert_content}
120 ... ${private_key_content} -----BEGIN PRIVATE KEY-----
121 ... -----END PRIVATE KEY-----
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500122
123 ${random_name}= Generate Random String 8
124 ${cert_name}= Catenate SEPARATOR= ${random_name} .pem
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500125 Create File ${cert_dir_name}/${cert_name} ${cert_data}
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500126
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500127 [Return] ${EXECDIR}${/}${cert_dir_name}${/}${cert_name}
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500128
129
130Get Certificate Content From File
131 [Documentation] Get certificate content from certificate file.
132 [Arguments] ${cert_file_path}
133
134 # Description of argument(s):
135 # cert_file_path Downloaded certificate file path.
136
137 ${file_content}= OperatingSystem.Get File ${cert_file_path}
138 ${result}= Fetch From Left ${file_content} -----END CERTIFICATE-----
139 ${result}= Fetch From Right ${result} -----BEGIN CERTIFICATE-----
140 [Return] ${result}
141
142
143Check If Openssl Tool Exist
144 [Documentation] Check if openssl tool installed or not.
145
146 ${rc} ${output}= Run And Return RC and Output which openssl
147 Should Not Be Empty ${output} msg=Openssl tool not installed.
148
Rahul Maheshwaria6ae3c32019-09-05 08:52:01 -0500149
150Verify Certificate Visible Via OpenSSL
151 [Documentation] Checks if given certificate is visible via openssl's showcert command.
152 [Arguments] ${cert_file_path}
153
154 # Description of argument(s):
155 # cert_file_path Certificate file path.
156
157 ${cert_file_content}= OperatingSystem.Get File ${cert_file_path}
158 ${openssl_cert_content}= Get Certificate Content From BMC Via Openssl
159 Should Contain ${cert_file_content} ${openssl_cert_content}
160