blob: febe976ce340295ec94e1562a33482baccedb0fb [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
George Keishing2dc28642022-07-19 13:43:54 -05008*** Variables ***
9
10# Default wait sync time for certificate install and restart services.
11${wait_time} 30
Rahul Maheshwari984791c2018-09-21 00:49:37 -050012
13*** Keywords ***
14
15Install Certificate File On BMC
Rahul Maheshwari6a849ad2020-02-26 03:31:19 -060016 [Documentation] Install certificate file in BMC using POST operation.
17 [Arguments] ${uri} ${status}=ok &{kwargs}
Rahul Maheshwari984791c2018-09-21 00:49:37 -050018
19 # Description of argument(s):
Rahul Maheshwari6a849ad2020-02-26 03:31:19 -060020 # uri URI for installing certificate file via Redfish
21 # e.g. "/redfish/v1/AccountService/LDAP/Certificates".
22 # status Expected status of certificate installation via Redfish
Rahul Maheshwari984791c2018-09-21 00:49:37 -050023 # e.g. error, ok.
Rahul Maheshwari984791c2018-09-21 00:49:37 -050024 # kwargs A dictionary of keys/values to be passed directly to
Rahul Maheshwari6a849ad2020-02-26 03:31:19 -060025 # POST Request.
Rahul Maheshwari984791c2018-09-21 00:49:37 -050026
Rahul Maheshwari6a849ad2020-02-26 03:31:19 -060027 Initialize OpenBMC
Rahul Maheshwari984791c2018-09-21 00:49:37 -050028
29 ${headers}= Create Dictionary Content-Type=application/octet-stream
Sridevi Ramesheadeef02019-01-17 08:56:18 -060030 ... X-Auth-Token=${XAUTH_TOKEN}
Rahul Maheshwari984791c2018-09-21 00:49:37 -050031 Set To Dictionary ${kwargs} headers ${headers}
32
Rahul Maheshwari6a849ad2020-02-26 03:31:19 -060033 ${ret}= Post Request openbmc ${uri} &{kwargs}
34 ${content_json}= To JSON ${ret.content}
35 ${cert_id}= Set Variable If '${ret.status_code}' == '${HTTP_OK}' ${content_json["Id"]} -1
Rahul Maheshwari984791c2018-09-21 00:49:37 -050036
37 Run Keyword If '${status}' == 'ok'
38 ... Should Be Equal As Strings ${ret.status_code} ${HTTP_OK}
39 ... ELSE IF '${status}' == 'error'
Rahul Maheshwari6a849ad2020-02-26 03:31:19 -060040 ... Should Be Equal As Strings ${ret.status_code} ${HTTP_INTERNAL_SERVER_ERROR}
Rahul Maheshwari984791c2018-09-21 00:49:37 -050041
42 Delete All Sessions
43
Rahul Maheshwari6a849ad2020-02-26 03:31:19 -060044 [Return] ${cert_id}
45
Rahul Maheshwari984791c2018-09-21 00:49:37 -050046
47Get Certificate Content From BMC Via Openssl
48 [Documentation] Get certificate content from BMC via openssl.
49
50 Check If Openssl Tool Exist
51
52 ${openssl_cmd}= Catenate
Anusha Dathatrid334bdf2020-06-10 04:19:07 -050053 ... timeout 10 openssl s_client -connect ${OPENBMC_HOST}:${HTTPS_PORT} -showcerts
Rahul Maheshwari2a848cf2019-05-31 09:46:22 -050054 ${output}= Run ${openssl_cmd}
Rahul Maheshwari984791c2018-09-21 00:49:37 -050055
56 ${result}= Fetch From Left
57 ... ${output} -----END CERTIFICATE-----
58 ${result}= Fetch From Right ${result} -----BEGIN CERTIFICATE-----
59 [Return] ${result}
60
61
Rahul Maheshwari081eadb2018-10-26 03:11:10 -050062Get Certificate File Content From BMC
63 [Documentation] Get required certificate file content from BMC.
64 [Arguments] ${cert_type}=Client
Rahul Maheshwari984791c2018-09-21 00:49:37 -050065
Rahul Maheshwari081eadb2018-10-26 03:11:10 -050066 # Description of argument(s):
67 # cert_type Certificate type (e.g. "Client" or "CA").
68
69 ${certificate} ${stderr} ${rc}= Run Keyword If '${cert_type}' == 'Client'
70 ... BMC Execute Command cat /etc/nslcd/certs/cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -050071
72 [Return] ${certificate}
73
74
75Generate Certificate File Via Openssl
76 [Documentation] Create certificate file via openssl with required content
77 ... and returns its path.
Rahul Maheshwari665bc612018-10-24 04:57:53 -050078 [Arguments] ${cert_format} ${time}=365 ${cert_dir_name}=certificate_dir
Rahul Maheshwari984791c2018-09-21 00:49:37 -050079
80 # Description of argument(s):
81 # cert_format Certificate file format
82 # e.g. Valid_Certificate_Empty_Privatekey.
83 # time Number of days to certify the certificate for.
Rahul Maheshwari665bc612018-10-24 04:57:53 -050084 # cert_dir_name The name of the sub-directory where the certificate
85 # is stored.
Rahul Maheshwari984791c2018-09-21 00:49:37 -050086
87 Check If Openssl Tool Exist
88
89 ${openssl_cmd}= Catenate openssl req -x509 -sha256 -newkey rsa:2048
90 ... ${SPACE}-nodes -days ${time}
Rahul Maheshwari665bc612018-10-24 04:57:53 -050091 ... ${SPACE}-keyout ${cert_dir_name}/cert.pem -out ${cert_dir_name}/cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -050092 ... ${SPACE}-subj "/O=XYZ Corporation /CN=www.xyz.com"
93
94 ${rc} ${output}= Run And Return RC and Output ${openssl_cmd}
95 Should Be Equal ${rc} ${0} msg=${output}
Rahul Maheshwari665bc612018-10-24 04:57:53 -050096 OperatingSystem.File Should Exist
97 ... ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -050098
Rahul Maheshwari665bc612018-10-24 04:57:53 -050099 ${file_content}= OperatingSystem.Get File
100 ... ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500101 ${result}= Fetch From Left ${file_content} -----END CERTIFICATE-----
102 ${cert_content}= Fetch From Right ${result} -----BEGIN CERTIFICATE-----
103
104 ${result}= Fetch From Left ${file_content} -----END PRIVATE KEY-----
105 ${private_key_content}= Fetch From Right ${result} -----BEGIN PRIVATE KEY-----
106
107 ${cert_data}=
108 ... Run Keyword if '${cert_format}' == 'Valid Certificate Valid Privatekey'
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500109 ... OperatingSystem.Get File ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500110 ... ELSE IF '${cert_format}' == 'Empty Certificate Valid Privatekey'
111 ... Remove String ${file_content} ${cert_content}
112 ... ELSE IF '${cert_format}' == 'Valid Certificate Empty Privatekey'
113 ... Remove String ${file_content} ${private_key_content}
114 ... ELSE IF '${cert_format}' == 'Empty Certificate Empty Privatekey'
115 ... Remove String ${file_content} ${cert_content} ${private_key_content}
Anusha Dathatribc855642020-06-17 05:21:14 -0500116 ... ELSE IF '${cert_format}' == 'Expired Certificate' or '${cert_format}' == 'Not Yet Valid Certificate'
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500117 ... OperatingSystem.Get File ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari081eadb2018-10-26 03:11:10 -0500118 ... ELSE IF '${cert_format}' == 'Valid Certificate'
119 ... Remove String ${file_content} ${private_key_content}
120 ... -----BEGIN PRIVATE KEY----- -----END PRIVATE KEY-----
121 ... ELSE IF '${cert_format}' == 'Empty Certificate'
122 ... Remove String ${file_content} ${cert_content}
123 ... ${private_key_content} -----BEGIN PRIVATE KEY-----
124 ... -----END PRIVATE KEY-----
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500125
126 ${random_name}= Generate Random String 8
127 ${cert_name}= Catenate SEPARATOR= ${random_name} .pem
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500128 Create File ${cert_dir_name}/${cert_name} ${cert_data}
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500129
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500130 [Return] ${EXECDIR}${/}${cert_dir_name}${/}${cert_name}
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500131
132
133Get Certificate Content From File
134 [Documentation] Get certificate content from certificate file.
135 [Arguments] ${cert_file_path}
136
137 # Description of argument(s):
138 # cert_file_path Downloaded certificate file path.
139
140 ${file_content}= OperatingSystem.Get File ${cert_file_path}
141 ${result}= Fetch From Left ${file_content} -----END CERTIFICATE-----
142 ${result}= Fetch From Right ${result} -----BEGIN CERTIFICATE-----
143 [Return] ${result}
144
145
146Check If Openssl Tool Exist
147 [Documentation] Check if openssl tool installed or not.
148
149 ${rc} ${output}= Run And Return RC and Output which openssl
150 Should Not Be Empty ${output} msg=Openssl tool not installed.
151
Rahul Maheshwaria6ae3c32019-09-05 08:52:01 -0500152
153Verify Certificate Visible Via OpenSSL
154 [Documentation] Checks if given certificate is visible via openssl's showcert command.
155 [Arguments] ${cert_file_path}
156
157 # Description of argument(s):
158 # cert_file_path Certificate file path.
159
160 ${cert_file_content}= OperatingSystem.Get File ${cert_file_path}
161 ${openssl_cert_content}= Get Certificate Content From BMC Via Openssl
162 Should Contain ${cert_file_content} ${openssl_cert_content}
163
manashsarmab9feda72020-10-05 10:40:12 -0500164
165Delete All CA Certificate Via Redfish
166 [Documentation] Delete all CA certificate via Redfish.
167 ${cert_list}= Redfish_Utils.Get Member List /redfish/v1/Managers/bmc/Truststore/Certificates
168 FOR ${cert} IN @{cert_list}
169 Redfish.Delete ${cert} valid_status_codes=[${HTTP_NO_CONTENT}]
170 END
manashsarmae07858a2020-10-16 06:09:46 -0500171
172
173Delete Certificate Via BMC CLI
174 [Documentation] Delete certificate via BMC CLI.
175 [Arguments] ${cert_type}
176
177 # Description of argument(s):
178 # cert_type Certificate type (e.g. "Client" or "CA").
179
180 ${certificate_file_path} ${certificate_service} ${certificate_uri}=
181 ... Run Keyword If '${cert_type}' == 'Client'
182 ... Set Variable /etc/nslcd/certs/cert.pem phosphor-certificate-manager@nslcd.service
183 ... ${REDFISH_LDAP_CERTIFICATE_URI}
184 ... ELSE IF '${cert_type}' == 'CA'
185 ... Set Variable ${ROOT_CA_FILE_PATH} phosphor-certificate-manager@authority.service
186 ... ${REDFISH_CA_CERTIFICATE_URI}
187
188 ${file_status} ${stderr} ${rc}= BMC Execute Command
189 ... [ -f ${certificate_file_path} ] && echo "Found" || echo "Not Found"
190
191 Return From Keyword If "${file_status}" != "Found"
192 BMC Execute Command rm ${certificate_file_path}
193 BMC Execute Command systemctl restart ${certificate_service}
194 BMC Execute Command systemctl daemon-reload
195 Wait Until Keyword Succeeds 1 min 10 sec Redfish.Get ${certificate_uri}/1
196 ... valid_status_codes=[${HTTP_NOT_FOUND}, ${HTTP_INTERNAL_SERVER_ERROR}]
Ashwini Chandrappa6b20ffa2021-08-04 03:18:37 -0500197
198
199Replace Certificate Via Redfish
200 [Documentation] Test 'replace certificate' operation in the BMC via Redfish.
201 [Arguments] ${cert_type} ${cert_format} ${expected_status}
202
203 # Description of argument(s):
204 # cert_type Certificate type (e.g. "Server" or "Client").
205 # cert_format Certificate file format
206 # (e.g. Valid_Certificate_Valid_Privatekey).
207 # expected_status Expected status of certificate replace Redfish
208 # request (i.e. "ok" or "error").
209
210 # Install certificate before replacing client or CA certificate.
211 ${cert_id}= Run Keyword If '${cert_type}' == 'Client'
212 ... Install And Verify Certificate Via Redfish ${cert_type} Valid Certificate Valid Privatekey ok
213 ... ELSE IF '${cert_type}' == 'CA'
214 ... Install And Verify Certificate Via Redfish ${cert_type} Valid Certificate ok
215
216 ${cert_file_path}= Generate Certificate File Via Openssl ${cert_format}
217
218 ${bytes}= OperatingSystem.Get Binary File ${cert_file_path}
219 ${file_data}= Decode Bytes To String ${bytes} UTF-8
220
221 Run Keyword If '${cert_format}' == 'Expired Certificate'
222 ... Modify BMC Date future
223 ... ELSE IF '${cert_format}' == 'Not Yet Valid Certificate'
224 ... Modify BMC Date old
225
226
227 ${certificate_uri}= Set Variable If
228 ... '${cert_type}' == 'Server' ${REDFISH_HTTPS_CERTIFICATE_URI}/1
229 ... '${cert_type}' == 'Client' ${REDFISH_LDAP_CERTIFICATE_URI}/1
230 ... '${cert_type}' == 'CA' ${REDFISH_CA_CERTIFICATE_URI}/${cert_id}
231
232 ${certificate_dict}= Create Dictionary @odata.id=${certificate_uri}
233 ${payload}= Create Dictionary CertificateString=${file_data}
234 ... CertificateType=PEM CertificateUri=${certificate_dict}
235
236 ${expected_resp}= Set Variable If '${expected_status}' == 'ok' ${HTTP_OK}
237 ... '${expected_status}' == 'error' ${HTTP_NOT_FOUND}, ${HTTP_INTERNAL_SERVER_ERROR}
238 ${resp}= redfish.Post /redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate
239 ... body=${payload} valid_status_codes=[${expected_resp}]
240
241 ${cert_file_content}= OperatingSystem.Get File ${cert_file_path}
242 ${bmc_cert_content}= redfish_utils.Get Attribute ${certificate_uri} CertificateString
243
244 Run Keyword If '${expected_status}' == 'ok'
245 ... Should Contain ${cert_file_content} ${bmc_cert_content}
246 ... ELSE
247 ... Should Not Contain ${cert_file_content} ${bmc_cert_content}
Rahul Maheshwarif689bb62022-04-22 05:00:32 -0500248
249
250Install And Verify Certificate Via Redfish
251 [Documentation] Install and verify certificate using Redfish.
252 [Arguments] ${cert_type} ${cert_format} ${expected_status} ${delete_cert}=${True}
253
254 # Description of argument(s):
255 # cert_type Certificate type (e.g. "Client" or "CA").
256 # cert_format Certificate file format
257 # (e.g. "Valid_Certificate_Valid_Privatekey").
258 # expected_status Expected status of certificate replace Redfish
259 # request (i.e. "ok" or "error").
260 # delete_cert Certificate will be deleted before installing if this True.
261
262 Run Keyword If '${cert_type}' == 'CA' and '${delete_cert}' == '${True}'
263 ... Delete All CA Certificate Via Redfish
264 ... ELSE IF '${cert_type}' == 'Client' and '${delete_cert}' == '${True}'
265 ... Delete Certificate Via BMC CLI ${cert_type}
266
267 ${cert_file_path}= Generate Certificate File Via Openssl ${cert_format}
268 ${bytes}= OperatingSystem.Get Binary File ${cert_file_path}
269 ${file_data}= Decode Bytes To String ${bytes} UTF-8
270
271 ${certificate_uri}= Set Variable If
272 ... '${cert_type}' == 'Client' ${REDFISH_LDAP_CERTIFICATE_URI}
273 ... '${cert_type}' == 'CA' ${REDFISH_CA_CERTIFICATE_URI}
274
275 Run Keyword If '${cert_format}' == 'Expired Certificate' Modify BMC Date future
276 ... ELSE IF '${cert_format}' == 'Not Yet Valid Certificate' Modify BMC Date old
277
278 ${cert_id}= Install Certificate File On BMC ${certificate_uri} ${expected_status} data=${file_data}
279 Logging Installed certificate id: ${cert_id}
280
281 # Adding delay after certificate installation.
George Keishing2dc28642022-07-19 13:43:54 -0500282 # Lesser wait timing causes bmcweb to restart quickly and breaks the web services.
283 Log To Console Wait Time started in seconds ${wait_time}
284 Sleep ${wait_time}s
Rahul Maheshwarif689bb62022-04-22 05:00:32 -0500285
286 ${cert_file_content}= OperatingSystem.Get File ${cert_file_path}
287 ${bmc_cert_content}= Run Keyword If '${expected_status}' == 'ok' redfish_utils.Get Attribute
288 ... ${certificate_uri}/${cert_id} CertificateString
289
290 Run Keyword If '${expected_status}' == 'ok' Should Contain ${cert_file_content} ${bmc_cert_content}
291 [Return] ${cert_id}
Rahul Maheshwaria4f334f2022-05-13 04:59:42 -0500292
293
294Modify BMC Date
295 [Documentation] Modify date in BMC.
296 [Arguments] ${date_set_type}=current
297
298 # Description of argument(s):
299 # date_set_type Set BMC date to a current, future, old date by 375 days.
300 # current - Sets date to local system date.
301 # future - Sets to a future date from current date.
302 # old - Sets to a old date from current date.
303
304 Redfish Power Off stack_mode=skip
305 ${current_date_time}= Get Current Date
306 ${new_time}= Run Keyword If '${date_set_type}' == 'current' Set Variable ${current_date_time}
307 ... ELSE IF '${date_set_type}' == 'future'
308 ... Add Time To Date ${current_date_time} 375 days
309 ... ELSE IF '${date_set_type}' == 'old'
310 ... Subtract Time From Date ${current_date_time} 375 days
311
312 # Enable manual mode.
313 Redfish.Patch ${REDFISH_NW_PROTOCOL_URI}
314 ... body={'NTP':{'ProtocolEnabled': ${False}}}
315 ... valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
George Keishingda8c7682022-07-07 15:39:19 -0500316
317 # NTP network takes few seconds to restart.
318 Wait Until Keyword Succeeds 30 sec 10 sec
319 ... Redfish.Patch ${REDFISH_BASE_URI}Managers/bmc body={'DateTime': '${new_time}'}
Rahul Maheshwaria4f334f2022-05-13 04:59:42 -0500320 ... valid_status_codes=[${HTTP_OK}]