1*** Settings ***
2Documentation    Test IPMI and Redfish combinations for user management.
3
4Resource         ../../lib/resource.robot
5Resource         ../../lib/bmc_redfish_resource.robot
6Resource         ../../lib/openbmc_ffdc.robot
7Resource         ../../lib/ipmi_client.robot
8Library          ../lib/ipmi_utils.py
9
10Test Setup       Test Setup Execution
11Test Teardown    Test Teardown Execution
12
13
14*** Variables ***
15
16${valid_password}       0penBmc1
17${valid_password2}      0penBmc2
18${admin_level_priv}     4
19${operator_level_priv}  3
20${max_num_users}        ${15}
21
22** Test Cases **
23
24Create Admin Redfish User And Verify Login Via IPMI
25    [Documentation]  Create user using redfish and verify via IPMI.
26    [Tags]  Create_Admin_Redfish_User_And_Verify_Login_Via_IPMI
27
28    ${random_username}=  Generate Random String  8  [LETTERS]
29    Set Test Variable  ${random_username}
30
31    ${payload}=  Create Dictionary
32    ...  UserName=${random_username}  Password=${valid_password}
33    ...  RoleId=Administrator  Enabled=${True}
34    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
35    ...  valid_status_codes=[${HTTP_CREATED}]
36
37    Verify IPMI Username And Password  ${random_username}  ${valid_password}
38
39
40Update User Password Via Redfish And Verify Using IPMI
41    [Documentation]  Update user password via Redfish and verify using IPMI.
42    [Tags]  Update_User_Password_Via_Redfish_And_Verify_Using_IPMI
43
44    # Create user using Redfish.
45    ${random_username}=  Generate Random String  8  [LETTERS]
46    Set Test Variable  ${random_username}
47
48    ${payload}=  Create Dictionary
49    ...  UserName=${random_username}  Password=${valid_password}
50    ...  RoleId=Administrator  Enabled=${True}
51    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
52    ...  valid_status_codes=[${HTTP_CREATED}]
53
54    # Update user password using Redfish.
55    ${payload}=  Create Dictionary  Password=${valid_password2}
56    Redfish.Patch  /redfish/v1/AccountService/Accounts/${random_username}  body=&{payload}
57
58    # Verify that IPMI command works with new password and fails with older password.
59    Verify IPMI Username And Password  ${random_username}  ${valid_password2}
60
61    Run Keyword And Expect Error  Error: Unable to establish IPMI*
62    ...  Verify IPMI Username And Password  ${random_username}  ${valid_password}
63
64
65Update User Privilege Via Redfish And Verify Using IPMI
66    [Documentation]  Update user privilege via Redfish and verify using IPMI.
67    [Tags]  Update_User_Privilege_Via_Redfish_And_Verify_Using_IPMI
68
69    # Create user using Redfish with admin privilege.
70    ${random_username}=  Generate Random String  8  [LETTERS]
71    Set Test Variable  ${random_username}
72
73    ${payload}=  Create Dictionary
74    ...  UserName=${random_username}  Password=${valid_password}
75    ...  RoleId=Administrator  Enabled=${True}
76    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
77    ...  valid_status_codes=[${HTTP_CREATED}]
78
79    # Update user privilege to operator using Redfish.
80    ${payload}=  Create Dictionary  RoleId=Operator
81    Redfish.Patch  /redfish/v1/AccountService/Accounts/${random_username}  body=&{payload}
82
83    # Verify new user privilege level via IPMI.
84    ${resp}=  Run IPMI Standard Command  user list
85
86    # Example of response data:
87    # ID  Name             Callin  Link Auth  IPMI Msg   Channel Priv Limit
88    # 1   root             false   true       true       ADMINISTRATOR
89    # 2   OAvCxjMv         false   true       true       OPERATOR
90    # 3                    true    false      false      NO ACCESS
91    # ..
92    # ..
93    # 15                   true    false      false      NO ACCESS
94
95    ${user_info}=
96    ...  Get Lines Containing String  ${resp}  ${random_username}
97    Should Contain  ${user_info}  OPERATOR
98
99
100Delete User Via Redfish And Verify Using IPMI
101    [Documentation]  Delete user via redfish and verify using IPMI.
102    [Tags]  Delete_User_Via_Redfish_And_Verify_Using_IPMI
103
104    # Create user using Redfish.
105    ${random_username}=  Generate Random String  8  [LETTERS]
106    Set Test Variable  ${random_username}
107
108    ${payload}=  Create Dictionary
109    ...  UserName=${random_username}  Password=${valid_password}
110    ...  RoleId=Administrator  Enabled=${True}
111    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
112    ...  valid_status_codes=[${HTTP_CREATED}]
113
114    # Delete user using Redfish.
115    Redfish.Delete  /redfish/v1/AccountService/Accounts/${random_username}
116
117    # Verify that IPMI command fails with deleted user.
118    Run Keyword And Expect Error  Error: Unable to establish IPMI*
119    ...  Verify IPMI Username And Password  ${random_username}  ${valid_password}
120
121
122Create IPMI User And Verify Login Via Redfish
123    [Documentation]  Create user using IPMI and verify user login via Redfish.
124    [Tags]  Create_IPMI_User_And_Verify_Login_Via_Redfish
125
126    ${username}  ${userid}=  IPMI Create Random User Plus Password And Privilege
127    ...  ${valid_password}  ${admin_level_priv}
128
129    # Verify user login using Redfish.
130    Redfish.Login  ${username}  ${valid_password}
131
132
133Update User Password Via IPMI And Verify Using Redfish
134    [Documentation]  Update user password using IPMI and verify user
135    ...  login via Redfish.
136    [Tags]  Update_User_Password_Via_IPMI_And_Verify_Using_Redfish
137
138    ${username}  ${userid}=  IPMI Create Random User Plus Password And Privilege
139    ...  ${valid_password}  ${admin_level_priv}
140
141    # Update user password using IPMI.
142    Run IPMI Standard Command
143    ...  user set password ${userid} ${valid_password2}
144
145    # Verify that user login works with new password using Redfish.
146    Redfish.Login  ${username}  ${valid_password2}
147
148
149Update User Privilege Via IPMI And Verify Using Redfish
150    [Documentation]  Update user privilege via IPMI and verify using Redfish.
151    [Tags]  Update_User_Privilege_Via_IPMI_And_Verify_Using_Redfish
152
153    # Create user using IPMI with admin privilege.
154    ${username}  ${userid}=  IPMI Create Random User Plus Password And Privilege
155    ...  ${valid_password}  ${admin_level_priv}
156
157    # Change user privilege to opetrator using IPMI.
158    Run IPMI Standard Command
159    ...  user priv ${userid} ${operator_level_priv}
160
161    # Verify new user privilege level via Redfish.
162    ${privilege}=  Redfish_Utils.Get Attribute
163    ...  /redfish/v1/AccountService/Accounts/${username}  RoleId
164    Should Be Equal  ${privilege}  Operator
165
166
167Delete User Via IPMI And Verify Using Redfish
168    [Documentation]  Delete user using IPMI and verify error while doing
169    ...  user login with deleted user via Redfish.
170    [Tags]  Delete_User_Via_IPMI_And_Verify_Using_Redfish
171
172    ${username}  ${userid}=  IPMI Create Random User Plus Password And Privilege
173    ...  ${valid_password}  ${admin_level_priv}
174
175    # Delete IPMI User.
176    Run IPMI Standard Command  user set name ${userid} ""
177
178    # Verify that Redfish login fails with deleted user.
179    Run Keyword And Expect Error  *InvalidCredentialsError*
180    ...  Redfish.Login  ${username}  ${valid_password}
181
182
183Verify Failure To Exceed Max Number Of Users
184    [Documentation]  Verify failure attempting to exceed the max number of user accounts.
185    [Tags]  Verify_Failure_To_Exceed_Max_Number_Of_Users
186    [Teardown]  Run Keywords  Test Teardown Execution  AND  Delete All Non Root IPMI User
187
188    # Get existing user count.
189    ${resp}=  Redfish.Get  /redfish/v1/AccountService/Accounts/
190    ${current_user_count}=  Get From Dictionary  ${resp.dict}  Members@odata.count
191
192    ${payload}=  Create Dictionary  Password=${valid_password}
193    ...  RoleId=Administrator  Enabled=${True}
194
195    # Create users to reach maximum users count (i.e. 15 users).
196    FOR  ${INDEX}  IN RANGE  ${current_user_count}  ${max_num_users}
197      ${random_username}=  Generate Random String  8  [LETTERS]
198      Set To Dictionary  ${payload}  UserName  ${random_username}
199      Redfish.Post  ${REDFISH_ACCOUNTS_URI}  body=&{payload}
200      ...  valid_status_codes=[${HTTP_CREATED}]
201    END
202
203    # Verify error while creating 16th user.
204    ${random_username}=  Generate Random String  8  [LETTERS]
205    Set To Dictionary  ${payload}  UserName  ${random_username}
206    Redfish.Post  ${REDFISH_ACCOUNTS_URI}  body=&{payload}
207    ...  valid_status_codes=[${HTTP_BAD_REQUEST}]
208
209
210*** Keywords ***
211
212IPMI Create Random User Plus Password And Privilege
213    [Documentation]  Create random IPMI user with given password and privilege
214    ...  level.
215    [Arguments]  ${password}  ${privilege}
216
217    # Description of argument(s):
218    # password      Password to be assigned for the user.
219    # privilege     Privilege level for the user (e.g. "1", "2", "3", etc.).
220
221    # Create IPMI user.
222    ${random_username}=  Generate Random String  8  [LETTERS]
223    Set Suite Variable  ${random_username}
224
225    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
226    IPMI Create User  ${random_userid}  ${random_username}
227
228    # Set given password for newly created user.
229    Run IPMI Standard Command
230    ...  user set password ${random_userid} ${password}
231
232    # Enable IPMI user.
233    Run IPMI Standard Command  user enable ${random_userid}
234
235    # Set given privilege and enable IPMI messaging for newly created user.
236    Set Channel Access  ${random_userid}  ipmi=on privilege=${privilege}
237
238    [Return]  ${random_username}  ${random_userid}
239
240
241Test Setup Execution
242    [Documentation]  Do test case setup tasks.
243
244    Redfish.Login
245
246
247Test Teardown Execution
248    [Documentation]  Do the post test teardown.
249
250    FFDC On Test Case Fail
251    # Delete the test user.
252    Run Keyword And Ignore Error
253    ...  Redfish.Delete  /redfish/v1/AccountService/Accounts/${random_username}
254
255    Redfish.Logout
256