Rahul Maheshwari | 984791c | 2018-09-21 00:49:37 -0500 | [diff] [blame] | 1 | *** Settings *** |
| 2 | Documentation Certificate utilities keywords. |
| 3 | |
| 4 | Library OperatingSystem |
| 5 | Resource rest_client.robot |
| 6 | Resource resource.txt |
| 7 | |
| 8 | |
| 9 | *** Keywords *** |
| 10 | |
| 11 | Install 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 |
| 27 | Set To Dictionary ${kwargs} headers ${headers} |
| 28 | |
| 29 | Run Keyword If '${quiet}' == '${0}' Log Request method=Put |
| 30 | ... base_uri=${uri} args=&{kwargs} |
| 31 | |
| 32 | ${ret}= Put Request openbmc ${uri} &{kwargs} |
| 33 | Run Keyword If '${quiet}' == '${0}' Log Response ${ret} |
| 34 | |
| 35 | Run Keyword If '${status}' == 'ok' |
| 36 | ... Should Be Equal As Strings ${ret.status_code} ${HTTP_OK} |
| 37 | ... ELSE IF '${status}' == 'error' |
| 38 | ... Should Be Equal As Strings ${ret.status_code} ${HTTP_BAD_REQUEST} |
| 39 | |
| 40 | Delete All Sessions |
| 41 | |
| 42 | |
| 43 | Get Certificate Content From BMC Via Openssl |
| 44 | [Documentation] Get certificate content from BMC via openssl. |
| 45 | |
| 46 | Check If Openssl Tool Exist |
| 47 | |
| 48 | ${openssl_cmd}= Catenate |
| 49 | ... openssl s_client -connect ${OPENBMC_HOST}:443 -showcerts |
| 50 | ${rc} ${output}= Run And Return RC and Output ${openssl_cmd} |
| 51 | Should Be Equal ${rc} ${0} msg=${output} |
| 52 | |
| 53 | ${result}= Fetch From Left |
| 54 | ... ${output} -----END CERTIFICATE----- |
| 55 | ${result}= Fetch From Right ${result} -----BEGIN CERTIFICATE----- |
| 56 | [Return] ${result} |
| 57 | |
| 58 | |
| 59 | Get Client Certificate File Content From BMC |
| 60 | [Documentation] Get client certificate file content from BMC. |
| 61 | |
| 62 | ${certificate} ${stderr} ${rc}= BMC Execute Command |
| 63 | ... cat /etc/nslcd/certs/cert.pem |
| 64 | Should Be Equal ${rc} ${0} msg=${stderr} |
| 65 | |
| 66 | [Return] ${certificate} |
| 67 | |
| 68 | |
| 69 | Generate Certificate File Via Openssl |
| 70 | [Documentation] Create certificate file via openssl with required content |
| 71 | ... and returns its path. |
| 72 | [Arguments] ${cert_format} ${time}=365 |
| 73 | |
| 74 | # Description of argument(s): |
| 75 | # cert_format Certificate file format |
| 76 | # e.g. Valid_Certificate_Empty_Privatekey. |
| 77 | # time Number of days to certify the certificate for. |
| 78 | |
| 79 | Check If Openssl Tool Exist |
| 80 | |
| 81 | ${openssl_cmd}= Catenate openssl req -x509 -sha256 -newkey rsa:2048 |
| 82 | ... ${SPACE}-nodes -days ${time} |
| 83 | ... ${SPACE}-keyout cert.pem -out cert.pem |
| 84 | ... ${SPACE}-subj "/O=XYZ Corporation /CN=www.xyz.com" |
| 85 | |
| 86 | ${rc} ${output}= Run And Return RC and Output ${openssl_cmd} |
| 87 | Should Be Equal ${rc} ${0} msg=${output} |
| 88 | OperatingSystem.File Should Exist ${EXECDIR}${/}cert.pem |
| 89 | |
| 90 | ${file_content}= OperatingSystem.Get File ${EXECDIR}${/}cert.pem |
| 91 | ${result}= Fetch From Left ${file_content} -----END CERTIFICATE----- |
| 92 | ${cert_content}= Fetch From Right ${result} -----BEGIN CERTIFICATE----- |
| 93 | |
| 94 | ${result}= Fetch From Left ${file_content} -----END PRIVATE KEY----- |
| 95 | ${private_key_content}= Fetch From Right ${result} -----BEGIN PRIVATE KEY----- |
| 96 | |
| 97 | ${cert_data}= |
| 98 | ... Run Keyword if '${cert_format}' == 'Valid Certificate Valid Privatekey' |
| 99 | ... OperatingSystem.Get File ${EXECDIR}${/}cert.pem |
| 100 | ... ELSE IF '${cert_format}' == 'Empty Certificate Valid Privatekey' |
| 101 | ... Remove String ${file_content} ${cert_content} |
| 102 | ... ELSE IF '${cert_format}' == 'Valid Certificate Empty Privatekey' |
| 103 | ... Remove String ${file_content} ${private_key_content} |
| 104 | ... ELSE IF '${cert_format}' == 'Empty Certificate Empty Privatekey' |
| 105 | ... Remove String ${file_content} ${cert_content} ${private_key_content} |
| 106 | ... ELSE IF '${cert_format}' == 'Expired Certificate' |
| 107 | ... OperatingSystem.Get File ${EXECDIR}${/}cert.pem |
| 108 | |
| 109 | ${random_name}= Generate Random String 8 |
| 110 | ${cert_name}= Catenate SEPARATOR= ${random_name} .pem |
| 111 | Create File ${cert_name} ${cert_data} |
| 112 | |
| 113 | [Return] ${EXECDIR}${/}${cert_name} |
| 114 | |
| 115 | |
| 116 | Get Certificate Content From File |
| 117 | [Documentation] Get certificate content from certificate file. |
| 118 | [Arguments] ${cert_file_path} |
| 119 | |
| 120 | # Description of argument(s): |
| 121 | # cert_file_path Downloaded certificate file path. |
| 122 | |
| 123 | ${file_content}= OperatingSystem.Get File ${cert_file_path} |
| 124 | ${result}= Fetch From Left ${file_content} -----END CERTIFICATE----- |
| 125 | ${result}= Fetch From Right ${result} -----BEGIN CERTIFICATE----- |
| 126 | [Return] ${result} |
| 127 | |
| 128 | |
| 129 | Check If Openssl Tool Exist |
| 130 | [Documentation] Check if openssl tool installed or not. |
| 131 | |
| 132 | ${rc} ${output}= Run And Return RC and Output which openssl |
| 133 | Should Not Be Empty ${output} msg=Openssl tool not installed. |
| 134 | |