blob: 9930aacce379f89153fbebac72980d69a53176fb [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
George Keishingfbd67002022-08-01 11:24:03 -050033 ${resp}= POST On Session openbmc ${uri} &{kwargs} expected_status=any
34 ${cert_id}= Set Variable If '${resp.status_code}' == '${HTTP_OK}' ${resp.json()["Id"]} -1
Rahul Maheshwari984791c2018-09-21 00:49:37 -050035
36 Run Keyword If '${status}' == 'ok'
George Keishingfbd67002022-08-01 11:24:03 -050037 ... Should Be Equal As Strings ${resp.status_code} ${HTTP_OK}
Rahul Maheshwari984791c2018-09-21 00:49:37 -050038 ... ELSE IF '${status}' == 'error'
George Keishingfbd67002022-08-01 11:24:03 -050039 ... Should Be Equal As Strings ${resp.status_code} ${HTTP_INTERNAL_SERVER_ERROR}
Rahul Maheshwari984791c2018-09-21 00:49:37 -050040
41 Delete All Sessions
42
Rahul Maheshwari6a849ad2020-02-26 03:31:19 -060043 [Return] ${cert_id}
44
Rahul Maheshwari984791c2018-09-21 00:49:37 -050045
46Get Certificate Content From BMC Via Openssl
47 [Documentation] Get certificate content from BMC via openssl.
48
49 Check If Openssl Tool Exist
50
51 ${openssl_cmd}= Catenate
Anusha Dathatrid334bdf2020-06-10 04:19:07 -050052 ... timeout 10 openssl s_client -connect ${OPENBMC_HOST}:${HTTPS_PORT} -showcerts
Rahul Maheshwari2a848cf2019-05-31 09:46:22 -050053 ${output}= Run ${openssl_cmd}
Rahul Maheshwari984791c2018-09-21 00:49:37 -050054
55 ${result}= Fetch From Left
56 ... ${output} -----END CERTIFICATE-----
57 ${result}= Fetch From Right ${result} -----BEGIN CERTIFICATE-----
58 [Return] ${result}
59
60
Rahul Maheshwari081eadb2018-10-26 03:11:10 -050061Get Certificate File Content From BMC
62 [Documentation] Get required certificate file content from BMC.
63 [Arguments] ${cert_type}=Client
Rahul Maheshwari984791c2018-09-21 00:49:37 -050064
Rahul Maheshwari081eadb2018-10-26 03:11:10 -050065 # Description of argument(s):
66 # cert_type Certificate type (e.g. "Client" or "CA").
67
68 ${certificate} ${stderr} ${rc}= Run Keyword If '${cert_type}' == 'Client'
69 ... BMC Execute Command cat /etc/nslcd/certs/cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -050070
71 [Return] ${certificate}
72
73
74Generate Certificate File Via Openssl
75 [Documentation] Create certificate file via openssl with required content
76 ... and returns its path.
Rahul Maheshwari665bc612018-10-24 04:57:53 -050077 [Arguments] ${cert_format} ${time}=365 ${cert_dir_name}=certificate_dir
Rahul Maheshwari984791c2018-09-21 00:49:37 -050078
79 # Description of argument(s):
80 # cert_format Certificate file format
81 # e.g. Valid_Certificate_Empty_Privatekey.
82 # time Number of days to certify the certificate for.
Rahul Maheshwari665bc612018-10-24 04:57:53 -050083 # cert_dir_name The name of the sub-directory where the certificate
84 # is stored.
Rahul Maheshwari984791c2018-09-21 00:49:37 -050085
86 Check If Openssl Tool Exist
87
88 ${openssl_cmd}= Catenate openssl req -x509 -sha256 -newkey rsa:2048
89 ... ${SPACE}-nodes -days ${time}
Rahul Maheshwari665bc612018-10-24 04:57:53 -050090 ... ${SPACE}-keyout ${cert_dir_name}/cert.pem -out ${cert_dir_name}/cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -050091 ... ${SPACE}-subj "/O=XYZ Corporation /CN=www.xyz.com"
92
93 ${rc} ${output}= Run And Return RC and Output ${openssl_cmd}
94 Should Be Equal ${rc} ${0} msg=${output}
Rahul Maheshwari665bc612018-10-24 04:57:53 -050095 OperatingSystem.File Should Exist
96 ... ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -050097
Rahul Maheshwari665bc612018-10-24 04:57:53 -050098 ${file_content}= OperatingSystem.Get File
99 ... ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500100 ${result}= Fetch From Left ${file_content} -----END CERTIFICATE-----
101 ${cert_content}= Fetch From Right ${result} -----BEGIN CERTIFICATE-----
102
103 ${result}= Fetch From Left ${file_content} -----END PRIVATE KEY-----
104 ${private_key_content}= Fetch From Right ${result} -----BEGIN PRIVATE KEY-----
105
106 ${cert_data}=
107 ... Run Keyword if '${cert_format}' == 'Valid Certificate Valid Privatekey'
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500108 ... OperatingSystem.Get File ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500109 ... ELSE IF '${cert_format}' == 'Empty Certificate Valid Privatekey'
110 ... Remove String ${file_content} ${cert_content}
111 ... ELSE IF '${cert_format}' == 'Valid Certificate Empty Privatekey'
112 ... Remove String ${file_content} ${private_key_content}
113 ... ELSE IF '${cert_format}' == 'Empty Certificate Empty Privatekey'
114 ... Remove String ${file_content} ${cert_content} ${private_key_content}
Anusha Dathatribc855642020-06-17 05:21:14 -0500115 ... ELSE IF '${cert_format}' == 'Expired Certificate' or '${cert_format}' == 'Not Yet Valid Certificate'
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500116 ... OperatingSystem.Get File ${EXECDIR}${/}${cert_dir_name}${/}cert.pem
Rahul Maheshwari081eadb2018-10-26 03:11:10 -0500117 ... ELSE IF '${cert_format}' == 'Valid Certificate'
118 ... Remove String ${file_content} ${private_key_content}
119 ... -----BEGIN PRIVATE KEY----- -----END PRIVATE KEY-----
120 ... ELSE IF '${cert_format}' == 'Empty Certificate'
121 ... Remove String ${file_content} ${cert_content}
122 ... ${private_key_content} -----BEGIN PRIVATE KEY-----
123 ... -----END PRIVATE KEY-----
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500124
125 ${random_name}= Generate Random String 8
126 ${cert_name}= Catenate SEPARATOR= ${random_name} .pem
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500127 Create File ${cert_dir_name}/${cert_name} ${cert_data}
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500128
Rahul Maheshwari665bc612018-10-24 04:57:53 -0500129 [Return] ${EXECDIR}${/}${cert_dir_name}${/}${cert_name}
Rahul Maheshwari984791c2018-09-21 00:49:37 -0500130
131
132Get Certificate Content From File
133 [Documentation] Get certificate content from certificate file.
134 [Arguments] ${cert_file_path}
135
136 # Description of argument(s):
137 # cert_file_path Downloaded certificate file path.
138
139 ${file_content}= OperatingSystem.Get File ${cert_file_path}
140 ${result}= Fetch From Left ${file_content} -----END CERTIFICATE-----
141 ${result}= Fetch From Right ${result} -----BEGIN CERTIFICATE-----
142 [Return] ${result}
143
144
145Check If Openssl Tool Exist
146 [Documentation] Check if openssl tool installed or not.
147
148 ${rc} ${output}= Run And Return RC and Output which openssl
149 Should Not Be Empty ${output} msg=Openssl tool not installed.
150
Rahul Maheshwaria6ae3c32019-09-05 08:52:01 -0500151
152Verify Certificate Visible Via OpenSSL
153 [Documentation] Checks if given certificate is visible via openssl's showcert command.
154 [Arguments] ${cert_file_path}
155
156 # Description of argument(s):
157 # cert_file_path Certificate file path.
158
159 ${cert_file_content}= OperatingSystem.Get File ${cert_file_path}
160 ${openssl_cert_content}= Get Certificate Content From BMC Via Openssl
161 Should Contain ${cert_file_content} ${openssl_cert_content}
162
manashsarmab9feda72020-10-05 10:40:12 -0500163
164Delete All CA Certificate Via Redfish
165 [Documentation] Delete all CA certificate via Redfish.
166 ${cert_list}= Redfish_Utils.Get Member List /redfish/v1/Managers/bmc/Truststore/Certificates
167 FOR ${cert} IN @{cert_list}
168 Redfish.Delete ${cert} valid_status_codes=[${HTTP_NO_CONTENT}]
George Keishing31888932022-07-19 23:05:50 -0500169 Log To Console Wait Time started in seconds ${wait_time}
170 Sleep ${wait_time}s
manashsarmab9feda72020-10-05 10:40:12 -0500171 END
manashsarmae07858a2020-10-16 06:09:46 -0500172
173
174Delete Certificate Via BMC CLI
175 [Documentation] Delete certificate via BMC CLI.
176 [Arguments] ${cert_type}
177
178 # Description of argument(s):
179 # cert_type Certificate type (e.g. "Client" or "CA").
180
181 ${certificate_file_path} ${certificate_service} ${certificate_uri}=
182 ... Run Keyword If '${cert_type}' == 'Client'
183 ... Set Variable /etc/nslcd/certs/cert.pem phosphor-certificate-manager@nslcd.service
184 ... ${REDFISH_LDAP_CERTIFICATE_URI}
185 ... ELSE IF '${cert_type}' == 'CA'
186 ... Set Variable ${ROOT_CA_FILE_PATH} phosphor-certificate-manager@authority.service
187 ... ${REDFISH_CA_CERTIFICATE_URI}
188
189 ${file_status} ${stderr} ${rc}= BMC Execute Command
190 ... [ -f ${certificate_file_path} ] && echo "Found" || echo "Not Found"
191
192 Return From Keyword If "${file_status}" != "Found"
193 BMC Execute Command rm ${certificate_file_path}
194 BMC Execute Command systemctl restart ${certificate_service}
195 BMC Execute Command systemctl daemon-reload
196 Wait Until Keyword Succeeds 1 min 10 sec Redfish.Get ${certificate_uri}/1
197 ... valid_status_codes=[${HTTP_NOT_FOUND}, ${HTTP_INTERNAL_SERVER_ERROR}]
Ashwini Chandrappa6b20ffa2021-08-04 03:18:37 -0500198
199
200Replace Certificate Via Redfish
201 [Documentation] Test 'replace certificate' operation in the BMC via Redfish.
202 [Arguments] ${cert_type} ${cert_format} ${expected_status}
203
204 # Description of argument(s):
205 # cert_type Certificate type (e.g. "Server" or "Client").
206 # cert_format Certificate file format
207 # (e.g. Valid_Certificate_Valid_Privatekey).
208 # expected_status Expected status of certificate replace Redfish
209 # request (i.e. "ok" or "error").
210
211 # Install certificate before replacing client or CA certificate.
212 ${cert_id}= Run Keyword If '${cert_type}' == 'Client'
213 ... Install And Verify Certificate Via Redfish ${cert_type} Valid Certificate Valid Privatekey ok
214 ... ELSE IF '${cert_type}' == 'CA'
215 ... Install And Verify Certificate Via Redfish ${cert_type} Valid Certificate ok
216
217 ${cert_file_path}= Generate Certificate File Via Openssl ${cert_format}
218
219 ${bytes}= OperatingSystem.Get Binary File ${cert_file_path}
220 ${file_data}= Decode Bytes To String ${bytes} UTF-8
221
222 Run Keyword If '${cert_format}' == 'Expired Certificate'
223 ... Modify BMC Date future
224 ... ELSE IF '${cert_format}' == 'Not Yet Valid Certificate'
225 ... Modify BMC Date old
226
227
228 ${certificate_uri}= Set Variable If
229 ... '${cert_type}' == 'Server' ${REDFISH_HTTPS_CERTIFICATE_URI}/1
230 ... '${cert_type}' == 'Client' ${REDFISH_LDAP_CERTIFICATE_URI}/1
231 ... '${cert_type}' == 'CA' ${REDFISH_CA_CERTIFICATE_URI}/${cert_id}
232
233 ${certificate_dict}= Create Dictionary @odata.id=${certificate_uri}
234 ${payload}= Create Dictionary CertificateString=${file_data}
235 ... CertificateType=PEM CertificateUri=${certificate_dict}
236
237 ${expected_resp}= Set Variable If '${expected_status}' == 'ok' ${HTTP_OK}
238 ... '${expected_status}' == 'error' ${HTTP_NOT_FOUND}, ${HTTP_INTERNAL_SERVER_ERROR}
239 ${resp}= redfish.Post /redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate
240 ... body=${payload} valid_status_codes=[${expected_resp}]
241
242 ${cert_file_content}= OperatingSystem.Get File ${cert_file_path}
243 ${bmc_cert_content}= redfish_utils.Get Attribute ${certificate_uri} CertificateString
244
245 Run Keyword If '${expected_status}' == 'ok'
246 ... Should Contain ${cert_file_content} ${bmc_cert_content}
247 ... ELSE
248 ... Should Not Contain ${cert_file_content} ${bmc_cert_content}
Rahul Maheshwarif689bb62022-04-22 05:00:32 -0500249
250
251Install And Verify Certificate Via Redfish
252 [Documentation] Install and verify certificate using Redfish.
253 [Arguments] ${cert_type} ${cert_format} ${expected_status} ${delete_cert}=${True}
254
255 # Description of argument(s):
256 # cert_type Certificate type (e.g. "Client" or "CA").
257 # cert_format Certificate file format
258 # (e.g. "Valid_Certificate_Valid_Privatekey").
259 # expected_status Expected status of certificate replace Redfish
260 # request (i.e. "ok" or "error").
261 # delete_cert Certificate will be deleted before installing if this True.
262
263 Run Keyword If '${cert_type}' == 'CA' and '${delete_cert}' == '${True}'
264 ... Delete All CA Certificate Via Redfish
265 ... ELSE IF '${cert_type}' == 'Client' and '${delete_cert}' == '${True}'
266 ... Delete Certificate Via BMC CLI ${cert_type}
267
268 ${cert_file_path}= Generate Certificate File Via Openssl ${cert_format}
269 ${bytes}= OperatingSystem.Get Binary File ${cert_file_path}
270 ${file_data}= Decode Bytes To String ${bytes} UTF-8
271
272 ${certificate_uri}= Set Variable If
273 ... '${cert_type}' == 'Client' ${REDFISH_LDAP_CERTIFICATE_URI}
274 ... '${cert_type}' == 'CA' ${REDFISH_CA_CERTIFICATE_URI}
275
276 Run Keyword If '${cert_format}' == 'Expired Certificate' Modify BMC Date future
277 ... ELSE IF '${cert_format}' == 'Not Yet Valid Certificate' Modify BMC Date old
278
279 ${cert_id}= Install Certificate File On BMC ${certificate_uri} ${expected_status} data=${file_data}
280 Logging Installed certificate id: ${cert_id}
281
282 # Adding delay after certificate installation.
George Keishing2dc28642022-07-19 13:43:54 -0500283 # Lesser wait timing causes bmcweb to restart quickly and breaks the web services.
284 Log To Console Wait Time started in seconds ${wait_time}
285 Sleep ${wait_time}s
Rahul Maheshwarif689bb62022-04-22 05:00:32 -0500286
287 ${cert_file_content}= OperatingSystem.Get File ${cert_file_path}
288 ${bmc_cert_content}= Run Keyword If '${expected_status}' == 'ok' redfish_utils.Get Attribute
289 ... ${certificate_uri}/${cert_id} CertificateString
290
291 Run Keyword If '${expected_status}' == 'ok' Should Contain ${cert_file_content} ${bmc_cert_content}
292 [Return] ${cert_id}
Rahul Maheshwaria4f334f2022-05-13 04:59:42 -0500293
294
295Modify BMC Date
296 [Documentation] Modify date in BMC.
297 [Arguments] ${date_set_type}=current
298
299 # Description of argument(s):
300 # date_set_type Set BMC date to a current, future, old date by 375 days.
301 # current - Sets date to local system date.
302 # future - Sets to a future date from current date.
303 # old - Sets to a old date from current date.
304
305 Redfish Power Off stack_mode=skip
306 ${current_date_time}= Get Current Date
307 ${new_time}= Run Keyword If '${date_set_type}' == 'current' Set Variable ${current_date_time}
308 ... ELSE IF '${date_set_type}' == 'future'
309 ... Add Time To Date ${current_date_time} 375 days
310 ... ELSE IF '${date_set_type}' == 'old'
311 ... Subtract Time From Date ${current_date_time} 375 days
312
313 # Enable manual mode.
314 Redfish.Patch ${REDFISH_NW_PROTOCOL_URI}
315 ... body={'NTP':{'ProtocolEnabled': ${False}}}
316 ... valid_status_codes=[${HTTP_OK}, ${HTTP_NO_CONTENT}]
George Keishingda8c7682022-07-07 15:39:19 -0500317
318 # NTP network takes few seconds to restart.
319 Wait Until Keyword Succeeds 30 sec 10 sec
320 ... Redfish.Patch ${REDFISH_BASE_URI}Managers/bmc body={'DateTime': '${new_time}'}
Rahul Maheshwaria4f334f2022-05-13 04:59:42 -0500321 ... valid_status_codes=[${HTTP_OK}]