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# Refer:  #openbmc/phosphor-user-manager/blob/master/user_mgr.cpp
21# ipmiMaxUserNameLen = 16;    <-- IPMI
22# systemMaxUserNameLen = 30;  <-- Redfish account users
23${max_num_users}        ${30}
24${empty_name_pattern}   ^User Name\\s.*\\s:\\s$
25
26** Test Cases **
27
28Create Admin Redfish User And Verify Login Via IPMI
29    [Documentation]  Create user using redfish and verify via IPMI.
30    [Tags]  Create_Admin_Redfish_User_And_Verify_Login_Via_IPMI
31
32    ${random_username}=  Generate Random String  8  [LETTERS]
33    Set Test Variable  ${random_username}
34
35    ${payload}=  Create Dictionary
36    ...  UserName=${random_username}  Password=${valid_password}
37    ...  RoleId=Administrator  Enabled=${True}
38    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
39    ...  valid_status_codes=[${HTTP_CREATED}]
40
41    # Delay added for created new user password to get set.
42    Sleep  5s
43
44    Verify IPMI Username And Password  ${random_username}  ${valid_password}
45
46
47Update User Password Via Redfish And Verify Using IPMI
48    [Documentation]  Update user password via Redfish and verify using IPMI.
49    [Tags]  Update_User_Password_Via_Redfish_And_Verify_Using_IPMI
50
51    # Create user using Redfish.
52    ${random_username}=  Generate Random String  8  [LETTERS]
53    Set Test Variable  ${random_username}
54
55    ${payload}=  Create Dictionary
56    ...  UserName=${random_username}  Password=${valid_password}
57    ...  RoleId=Administrator  Enabled=${True}
58    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
59    ...  valid_status_codes=[${HTTP_CREATED}]
60
61    # Update user password using Redfish.
62    ${payload}=  Create Dictionary  Password=${valid_password2}
63    Redfish.Patch  /redfish/v1/AccountService/Accounts/${random_username}  body=&{payload}
64
65    # Verify that IPMI command works with new password and fails with older password.
66    Verify IPMI Username And Password  ${random_username}  ${valid_password2}
67
68    Run Keyword And Expect Error  *Error: Unable to establish IPMI*
69    ...  Verify IPMI Username And Password  ${random_username}  ${valid_password}
70
71
72Update User Privilege Via Redfish And Verify Using IPMI
73    [Documentation]  Update user privilege via Redfish and verify using IPMI.
74    [Tags]  Update_User_Privilege_Via_Redfish_And_Verify_Using_IPMI
75
76    # Create user using Redfish with admin privilege.
77    ${random_username}=  Generate Random String  8  [LETTERS]
78    Set Test Variable  ${random_username}
79
80    ${payload}=  Create Dictionary
81    ...  UserName=${random_username}  Password=${valid_password}
82    ...  RoleId=Administrator  Enabled=${True}
83    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
84    ...  valid_status_codes=[${HTTP_CREATED}]
85
86    # Update user privilege to operator using Redfish.
87    ${payload}=  Create Dictionary  RoleId=Operator
88    Redfish.Patch  /redfish/v1/AccountService/Accounts/${random_username}  body=&{payload}
89
90    # Verify new user privilege level via IPMI.
91    ${resp}=  Run IPMI Standard Command  user list ${CHANNEL_NUMBER}
92
93    # Example of response data:
94    # ID  Name             Callin  Link Auth  IPMI Msg   Channel Priv Limit
95    # 1   root             false   true       true       ADMINISTRATOR
96    # 2   OAvCxjMv         false   true       true       OPERATOR
97    # 3                    true    false      false      NO ACCESS
98    # ..
99    # ..
100    # 15                   true    false      false      NO ACCESS
101
102    ${user_info}=
103    ...  Get Lines Containing String  ${resp}  ${random_username}
104    Should Contain  ${user_info}  OPERATOR
105
106
107Delete User Via Redfish And Verify Using IPMI
108    [Documentation]  Delete user via redfish and verify using IPMI.
109    [Tags]  Delete_User_Via_Redfish_And_Verify_Using_IPMI
110
111    # Create user using Redfish.
112    ${random_username}=  Generate Random String  8  [LETTERS]
113    Set Test Variable  ${random_username}
114
115    ${payload}=  Create Dictionary
116    ...  UserName=${random_username}  Password=${valid_password}
117    ...  RoleId=Administrator  Enabled=${True}
118    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
119    ...  valid_status_codes=[${HTTP_CREATED}]
120
121    # Delete user using Redfish.
122    Redfish.Delete  /redfish/v1/AccountService/Accounts/${random_username}
123
124    # Verify that IPMI command fails with deleted user.
125    Run Keyword And Expect Error  *Error: Unable to establish IPMI*
126    ...  Verify IPMI Username And Password  ${random_username}  ${valid_password}
127
128
129Create IPMI User And Verify Login Via Redfish
130    [Documentation]  Create user using IPMI and verify user login via Redfish.
131    [Tags]  Create_IPMI_User_And_Verify_Login_Via_Redfish
132
133    ${username}  ${userid}=  IPMI Create Random User Plus Password And Privilege
134    ...  ${valid_password}  ${admin_level_priv}
135
136    Redfish.Logout
137
138    # Verify user login using Redfish.
139    Redfish.Login  ${username}  ${valid_password}
140    Redfish.Logout
141
142    Redfish.Login
143
144
145Update User Password Via IPMI And Verify Using Redfish
146    [Documentation]  Update user password using IPMI and verify user
147    ...  login via Redfish.
148    [Tags]  Update_User_Password_Via_IPMI_And_Verify_Using_Redfish
149
150    ${username}  ${userid}=  IPMI Create Random User Plus Password And Privilege
151    ...  ${valid_password}  ${admin_level_priv}
152
153    # Update user password using IPMI.
154    Run IPMI Standard Command
155    ...  user set password ${userid} ${valid_password2}
156
157    Redfish.Logout
158
159    # Verify that user login works with new password using Redfish.
160    Redfish.Login  ${username}  ${valid_password2}
161    Redfish.Logout
162
163    Redfish.Login
164
165
166Update User Privilege Via IPMI And Verify Using Redfish
167    [Documentation]  Update user privilege via IPMI and verify using Redfish.
168    [Tags]  Update_User_Privilege_Via_IPMI_And_Verify_Using_Redfish
169
170    # Create user using IPMI with admin privilege.
171    ${username}  ${userid}=  IPMI Create Random User Plus Password And Privilege
172    ...  ${valid_password}  ${admin_level_priv}
173
174    # Change user privilege to opetrator using IPMI.
175    Run IPMI Standard Command
176    ...  user priv ${userid} ${operator_level_priv} ${CHANNEL_NUMBER}
177
178    # Verify new user privilege level via Redfish.
179    ${privilege}=  Redfish_Utils.Get Attribute
180    ...  /redfish/v1/AccountService/Accounts/${username}  RoleId
181    Should Be Equal  ${privilege}  Operator
182
183
184Delete User Via IPMI And Verify Using Redfish
185    [Documentation]  Delete user using IPMI and verify error while doing
186    ...  user login with deleted user via Redfish.
187    [Tags]  Delete_User_Via_IPMI_And_Verify_Using_Redfish
188
189    ${username}  ${userid}=  IPMI Create Random User Plus Password And Privilege
190    ...  ${valid_password}  ${admin_level_priv}
191
192    # Delete IPMI User.
193    Run IPMI Standard Command  user set name ${userid} ""
194
195    # Verify that Redfish login fails with deleted user.
196    Run Keyword And Expect Error  *InvalidCredentialsError*
197    ...  Redfish.Login  ${username}  ${valid_password}
198
199
200Verify Failure To Exceed Max Number Of Users
201    [Documentation]  Verify failure attempting to exceed the max number of user accounts.
202    [Tags]  Verify_Failure_To_Exceed_Max_Number_Of_Users
203
204    # Get existing user count.
205    ${resp}=  Redfish.Get  /redfish/v1/AccountService/Accounts/
206    ${current_user_count}=  Get From Dictionary  ${resp.dict}  Members@odata.count
207
208    ${payload}=  Create Dictionary  Password=${valid_password}
209    ...  RoleId=Administrator  Enabled=${True}
210
211    @{username_list}=  Create List
212
213    # Create users to reach maximum users count (i.e. 15 users).
214    FOR  ${INDEX}  IN RANGE  ${current_user_count}  ${max_num_users}
215      ${random_username}=  Generate Random String  8  [LETTERS]
216      Set To Dictionary  ${payload}  UserName  ${random_username}
217      Redfish.Post  ${REDFISH_ACCOUNTS_URI}  body=&{payload}
218      ...  valid_status_codes=[${HTTP_CREATED}]
219      Append To List  ${username_list}  /redfish/v1/AccountService/Accounts/${random_username}
220    END
221
222    # Verify error while creating 16th user.
223    ${random_username}=  Generate Random String  8  [LETTERS]
224    Set To Dictionary  ${payload}  UserName  ${random_username}
225    Redfish.Post  ${REDFISH_ACCOUNTS_URI}  body=&{payload}
226    ...  valid_status_codes=[${HTTP_BAD_REQUEST}]
227
228    FOR  ${saved_user_list}  IN  @{username_list}
229      Redfish.Delete  ${saved_user_list}
230    END
231
232
233Create IPMI User Without Any Privilege And Verify Via Redfish
234    [Documentation]  Create user using IPMI without privilege and verify via redfish.
235    [Tags]  Create_IPMI_User_Without_Any_Privilege_And_Verify_Via_Redfish
236
237    ${username}  ${userid}=  IPMI Create Random User Plus Password And Privilege
238    ...  ${valid_password}
239
240    # Verify new user privilege level via Redfish.
241    ${privilege}=  Redfish_Utils.Get Attribute
242    ...  /redfish/v1/AccountService/Accounts/${username}  RoleId
243    Valid Value  privilege  ['NoAccess']
244
245*** Keywords ***
246
247IPMI Create Random User Plus Password And Privilege
248    [Documentation]  Create random IPMI user with given password and privilege
249    ...  level.
250    [Arguments]  ${password}  ${privilege}=0
251
252    # Description of argument(s):
253    # password      Password to be assigned for the user.
254    # privilege     Privilege level for the user (e.g. "1", "2", "3", etc.).
255
256    # Create IPMI user.
257    ${random_username}=  Generate Random String  8  [LETTERS]
258    Set Suite Variable  ${random_username}
259
260    ${random_userid}=  Find Free User Id
261    IPMI Create User  ${random_userid}  ${random_username}
262
263    # Set given password for newly created user.
264    Run IPMI Standard Command
265    ...  user set password ${random_userid} ${password}
266
267    # Enable IPMI user.
268    Run IPMI Standard Command  user enable ${random_userid}
269
270    # Set given privilege and enable IPMI messaging for newly created user.
271    Run Keyword If  '${privilege}' != '0'
272    ...  Set Channel Access  ${random_userid}  ipmi=on privilege=${privilege}
273
274    [Return]  ${random_username}  ${random_userid}
275
276
277Test Setup Execution
278    [Documentation]  Do test case setup tasks.
279
280    Redfish.Login
281
282
283Test Teardown Execution
284    [Documentation]  Do the post test teardown.
285
286    FFDC On Test Case Fail
287    # Delete the test user.
288    Run Keyword And Ignore Error
289    ...  Redfish.Delete  /redfish/v1/AccountService/Accounts/${random_username}
290
291    Redfish.Logout
292
293
294Find Free User Id
295    [Documentation]  Find a userid that is not being used.
296    FOR    ${jj}    IN RANGE    300
297        ${random_userid}=  Evaluate  random.randint(1, ${max_num_users})  modules=random
298        ${access}=  Run IPMI Standard Command  channel getaccess 1 ${random_userid}
299
300        ${name_line}=  Get Lines Containing String  ${access}  User Name
301        Log To Console  For ID ${random_userid}: ${name_line}
302        ${is_empty}=  Run Keyword And Return Status
303        ...  Should Match Regexp  ${name_line}  ${empty_name_pattern}
304
305        Exit For Loop If  ${is_empty} == ${True}
306    END
307    Run Keyword If  '${jj}' == '299'  Fail  msg=A free user ID could not be found.
308    [Return]  ${random_userid}
309