1*** Settings ***
2Documentation    Test Redfish user account.
3
4Resource         ../../lib/resource.robot
5Resource         ../../lib/bmc_redfish_resource.robot
6Resource         ../../lib/openbmc_ffdc.robot
7Resource         ../../lib/bmc_redfish_utils.robot
8
9Test Setup       Redfish.Login
10Test Teardown    Test Teardown Execution
11
12*** Variables ***
13
14${account_lockout_duration}   ${30}
15${account_lockout_threshold}  ${3}
16
17** Test Cases **
18
19Verify AccountService Available
20    [Documentation]  Verify Redfish account service is available.
21    [Tags]  Verify_AccountService_Available
22
23    ${resp} =  Redfish_utils.Get Attribute  /redfish/v1/AccountService  ServiceEnabled
24    Should Be Equal As Strings  ${resp}  ${True}
25
26Verify Redfish User Persistence After Reboot
27    [Documentation]  Verify Redfish user persistence after reboot.
28    [Tags]  Verify_Redfish_User_Persistence_After_Reboot
29
30    # Create Redfish users.
31    Redfish Create User  admin_user     TestPwd123  Administrator   ${True}
32    Redfish Create User  operator_user  TestPwd123  Operator        ${True}
33    Redfish Create User  readonly_user  TestPwd123  ReadOnly        ${True}
34
35    # Reboot BMC.
36    Redfish OBMC Reboot (off)  stack_mode=normal
37
38    # Verify users after reboot.
39    Redfish Verify User  admin_user     TestPwd123  Administrator   ${True}
40    Redfish Verify User  operator_user  TestPwd123  Operator        ${True}
41    Redfish Verify User  readonly_user  TestPwd123  ReadOnly        ${True}
42
43    # Delete created users.
44    Redfish.Delete  /redfish/v1/AccountService/Accounts/admin_user
45    Redfish.Delete  /redfish/v1/AccountService/Accounts/operator_user
46    Redfish.Delete  /redfish/v1/AccountService/Accounts/readonly_user
47
48Redfish Create and Verify Users
49    [Documentation]  Create Redfish users with various roles.
50    [Tags]  Redfish_Create_and_Verify_Users
51    [Template]  Redfish Create And Verify User
52
53    #username      password    role_id         enabled
54    admin_user     TestPwd123  Administrator   ${True}
55    operator_user  TestPwd123  Operator        ${True}
56    readonly_user  TestPwd123  ReadOnly        ${True}
57
58Verify Redfish User with Wrong Password
59    [Documentation]  Verify Redfish User with Wrong Password.
60    [Tags]  Verify_Redfish_User_with_Wrong_Password
61    [Template]  Verify Redfish User with Wrong Password
62
63    #username      password    role_id         enabled  wrong_password
64    admin_user     TestPwd123  Administrator   ${True}  alskjhfwurh
65    operator_user  TestPwd123  Operator        ${True}  12j8a8uakjhdaosiruf024
66    readonly_user  TestPwd123  ReadOnly        ${True}  12
67
68Verify Login with Deleted Redfish Users
69    [Documentation]  Verify login with deleted Redfish Users.
70    [Tags]  Verify_Login_with_Deleted_Redfish_Users
71    [Template]  Verify Login with Deleted Redfish User
72
73    #username     password    role_id         enabled
74    admin_user     TestPwd123  Administrator   ${True}
75    operator_user  TestPwd123  Operator        ${True}
76    readonly_user  TestPwd123  ReadOnly        ${True}
77
78Verify User Creation Without Enabling It
79    [Documentation]  Verify User Creation Without Enabling it.
80    [Tags]  Verify_User_Creation_Without_Enabling_It
81    [Template]  Verify Create User Without Enabling
82
83    #username      password    role_id         enabled
84    admin_user     TestPwd123  Administrator   ${False}
85    operator_user  TestPwd123  Operator        ${False}
86    readonly_user  TestPwd123  ReadOnly        ${False}
87
88Verify User Creation With Invalid Role Id
89    [Documentation]  Verify user creation with invalid role ID.
90    [Tags]  Verify_User_Creation_With_Invalid_Role_Id
91
92    # Make sure the user account in question does not already exist.
93    Redfish.Delete  /redfish/v1/AccountService/Accounts/test_user
94    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NOT_FOUND}]
95
96    # Create specified user.
97    ${payload}=  Create Dictionary
98    ...  UserName=test_user  Password=TestPwd123  RoleId=wrongroleid  Enabled=${True}
99    Redfish.Post  /redfish/v1/AccountService/Accounts/  body=&{payload}
100    ...  valid_status_codes=[${HTTP_BAD_REQUEST}]
101
102Verify Error Upon Creating Same Users With Different Privileges
103    [Documentation]  Verify error upon creating same users with different privileges.
104    [Tags]  Verify_Error_Upon_Creating_Same_Users_With_Different_Privileges
105
106    Redfish Create User  test_user  TestPwd123  Administrator  ${True}
107
108    # Create specified user.
109    ${payload}=  Create Dictionary
110    ...  UserName=test_user  Password=TestPwd123  RoleId=Operator  Enabled=${True}
111    Redfish.Post  /redfish/v1/AccountService/Accounts/  body=&{payload}
112    ...  valid_status_codes=[${HTTP_BAD_REQUEST}]
113
114    Redfish.Delete  /redfish/v1/AccountService/Accounts/test_user
115
116Verify Modifying User Attributes
117    [Documentation]  Verify modifying user attributes.
118    [Tags]  Verify_Modifying_User_Attributes
119
120    # Create Redfish users.
121    Redfish Create User  admin_user     TestPwd123  Administrator   ${True}
122    Redfish Create User  operator_user  TestPwd123  Operator        ${True}
123    Redfish Create User  readonly_user  TestPwd123  ReadOnly        ${True}
124
125    # Make sure the new user account does not already exist.
126    Redfish.Delete  /redfish/v1/AccountService/Accounts/newadmin_user
127    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NOT_FOUND}]
128
129    # Update admin_user username using Redfish.
130    ${payload}=  Create Dictionary  UserName=newadmin_user
131    Redfish.Patch  /redfish/v1/AccountService/Accounts/admin_user  body=&{payload}
132
133    # Update operator_user password using Redfish.
134    ${payload}=  Create Dictionary  Password=NewTestPwd123
135    Redfish.Patch  /redfish/v1/AccountService/Accounts/operator_user  body=&{payload}
136
137    # Update readonly_user role using Redfish.
138    ${payload}=  Create Dictionary  RoleId=Operator
139    Redfish.Patch  /redfish/v1/AccountService/Accounts/readonly_user  body=&{payload}
140
141    # Verify users after updating
142    Redfish Verify User  newadmin_user  TestPwd123     Administrator   ${True}
143    Redfish Verify User  operator_user  NewTestPwd123  Operator        ${True}
144    Redfish Verify User  readonly_user  TestPwd123     Operator        ${True}
145
146    # Delete created users.
147    Redfish.Delete  /redfish/v1/AccountService/Accounts/newadmin_user
148    Redfish.Delete  /redfish/v1/AccountService/Accounts/operator_user
149    Redfish.Delete  /redfish/v1/AccountService/Accounts/readonly_user
150
151Verify User Account Locked
152    [Documentation]  Verify user account locked upon trying with invalid password.
153    [Tags]  Verify_User_Account_Locked
154
155    Redfish Create User  admin_user  TestPwd123  Administrator   ${True}
156
157    ${payload}=  Create Dictionary  AccountLockoutThreshold=${account_lockout_threshold}
158    ...  AccountLockoutDuration=${account_lockout_duration}
159    Redfish.Patch  ${REDFISH_ACCOUNTS_SERVICE_URI}  body=${payload}
160
161    Redfish.Logout
162
163    # Make ${account_lockout_threshold} failed login attempts.
164    Repeat Keyword  ${account_lockout_threshold} times
165    ...  Run Keyword And Expect Error  InvalidCredentialsError*  Redfish.Login  admin_user  abc123
166
167    # Verify that legitimate login fails due to lockout.
168    Run Keyword And Expect Error  InvalidCredentialsError*
169    ...  Redfish.Login  admin_user  TestPwd123
170
171    # Wait for lockout duration to expire and then verify that login works.
172    Sleep  ${account_lockout_duration}s
173    Redfish.Login  admin_user  TestPwd123
174
175    Redfish.Logout
176
177    Redfish.Login
178
179    Redfish.Delete  /redfish/v1/AccountService/Accounts/admin_user
180
181Verify Admin User Privilege
182    [Documentation]  Verify admin user privilege.
183    [Tags]  Verify_Admin_User_Privilege
184
185    Redfish Create User  admin_user  TestPwd123  Administrator  ${True}
186    Redfish Create User  operator_user  TestPwd123  Operator  ${True}
187    Redfish Create User  readonly_user  TestPwd123  ReadOnly  ${True}
188
189    Redfish.Logout
190
191    # Change role ID of operator user with admin user.
192    # Login with admin user.
193    Redfish.Login  admin_user  TestPwd123
194
195    # Modify Role ID of Operator user.
196    Redfish.Patch  /redfish/v1/AccountService/Accounts/operator_user  body={'RoleId': 'Administrator'}
197
198    # Verify modified user.
199    Redfish Verify User  operator_user  TestPwd123  Administrator  ${True}
200
201    Redfish.Logout
202    Redfish.Login  admin_user  TestPwd123
203
204    # Change password of 'user' user with admin user.
205    Redfish.Patch  /redfish/v1/AccountService/Accounts/readonly_user  body={'Password': 'NewTestPwd123'}
206
207    # Verify modified user.
208    Redfish Verify User  readonly_user  NewTestPwd123  ReadOnly  ${True}
209
210    Redfish.Delete  /redfish/v1/AccountService/Accounts/admin_user
211    Redfish.Delete  /redfish/v1/AccountService/Accounts/operator_user
212    Redfish.Delete  /redfish/v1/AccountService/Accounts/readonly_user
213
214Verify Operator User Privilege
215    [Documentation]  Verify operator user privilege.
216    [Tags]  Verify_operator_User_Privilege
217
218    Redfish Create User  admin_user  TestPwd123  Administrator  ${True}
219    Redfish Create User  operator_user  TestPwd123  Operator  ${True}
220
221    Redfish.Logout
222    # Login with operator user.
223    Redfish.Login  operator_user  TestPwd123
224
225    # Verify BMC reset.
226    Run Keyword And Expect Error  ValueError*  Redfish BMC Reset Operation
227
228    # Attempt to change password of admin user with operator user.
229    Redfish.Patch  /redfish/v1/AccountService/Accounts/admin_user  body={'Password': 'NewTestPwd123'}
230    ...  valid_status_codes=[${HTTP_FORBIDDEN}]
231
232    Redfish.Logout
233
234    Redfish.Login
235
236    Redfish.Delete  /redfish/v1/AccountService/Accounts/admin_user
237    Redfish.Delete  /redfish/v1/AccountService/Accounts/operator_user
238
239
240Verify ReadOnly User Privilege
241    [Documentation]  Verify ReadOnly user privilege.
242    [Tags]  Verify_ReadOnly_User_Privilege
243
244    Redfish Create User  readonly_user  TestPwd123  ReadOnly  ${True}
245    Redfish.Logout
246
247    # Login with read_only user.
248    Redfish.Login  readonly_user  TestPwd123
249
250    # Read system level data.
251    ${system_model}=  Redfish_Utils.Get Attribute
252    ...  ${SYSTEM_BASE_URI}  Model
253
254    Redfish.Logout
255    Redfish.Login
256    Redfish.Delete  ${REDFISH_ACCOUNTS_URI}readonly_user
257
258
259Verify Minimum Password Length For Redfish User
260    [Documentation]  Verify minimum password length for new and existing user.
261    [Tags]  Verify_Minimum_Password_Length_For_Redfish_User
262
263    ${user_name}=  Set Variable  testUser
264
265    # Make sure the user account in question does not already exist.
266    Redfish.Delete  /redfish/v1/AccountService/Accounts/${user_name}
267    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NOT_FOUND}]
268
269    # Try to create a user with invalid length password.
270    ${payload}=  Create Dictionary
271    ...  UserName=${user_name}  Password=UserPwd  RoleId=Administrator  Enabled=${True}
272    Redfish.Post  /redfish/v1/AccountService/Accounts/  body=&{payload}
273    ...  valid_status_codes=[${HTTP_BAD_REQUEST}]
274
275    # Create specified user with valid length password.
276    Set To Dictionary  ${payload}  Password  UserPwd1
277    Redfish.Post  /redfish/v1/AccountService/Accounts/  body=&{payload}
278    ...  valid_status_codes=[${HTTP_CREATED}]
279
280    # Try to change to an invalid password.
281    Redfish.Patch  /redfish/v1/AccountService/Accounts/${user_name}  body={'Password': 'UserPwd'}
282    ...  valid_status_codes=[${HTTP_BAD_REQUEST}]
283
284    # Change to a valid password.
285    Redfish.Patch  /redfish/v1/AccountService/Accounts/${user_name}  body={'Password': 'UserPwd1'}
286
287    # Verify login.
288    Redfish.Logout
289    Redfish.Login  ${user_name}  UserPwd1
290    Redfish.Logout
291    Redfish.Login
292    Redfish.Delete  /redfish/v1/AccountService/Accounts/${user_name}
293
294
295*** Keywords ***
296
297Test Teardown Execution
298    [Documentation]  Do the post test teardown.
299
300    Run Keyword And Ignore Error  Redfish.Logout
301    FFDC On Test Case Fail
302
303
304Redfish Create User
305    [Documentation]  Redfish create user.
306    [Arguments]   ${username}  ${password}  ${role_id}  ${enabled}  ${login_check}=${True}
307
308    # Description of argument(s):
309    # username            The username to be created.
310    # password            The password to be assigned.
311    # role_id             The role ID of the user to be created
312    #                     (e.g. "Administrator", "Operator", etc.).
313    # enabled             Indicates whether the username being created
314    #                     should be enabled (${True}, ${False}).
315    # login_check         Checks user login for created user.
316    #                     (e.g. ${True}, ${False}).
317
318    # Make sure the user account in question does not already exist.
319    Redfish.Delete  /redfish/v1/AccountService/Accounts/${userName}
320    ...  valid_status_codes=[${HTTP_OK}, ${HTTP_NOT_FOUND}]
321
322    # Create specified user.
323    ${payload}=  Create Dictionary
324    ...  UserName=${username}  Password=${password}  RoleId=${role_id}  Enabled=${enabled}
325    Redfish.Post  /redfish/v1/AccountService/Accounts/  body=&{payload}
326    ...  valid_status_codes=[${HTTP_CREATED}]
327
328    # Resetting pam tally count as a workaround for issue
329    # openbmc/phosphor-user-manager#4
330    ${cmd}=  Catenate  /usr/sbin/pam_tally2 -u ${username} --reset
331    Bmc Execute Command  ${cmd}
332
333    # Verify login with created user.
334    ${status}=  Run Keyword If  '${login_check}' == '${True}'
335    ...  Verify Redfish User Login  ${username}  ${password}
336    Run Keyword If  '${login_check}' == '${True}'  Should Be Equal  ${status}  ${enabled}
337
338    # Validate Role ID of created user.
339    ${role_config}=  Redfish_Utils.Get Attribute
340    ...  /redfish/v1/AccountService/Accounts/${username}  RoleId
341    Should Be Equal  ${role_id}  ${role_config}
342
343
344Redfish Verify User
345    [Documentation]  Redfish user verification.
346    [Arguments]   ${username}  ${password}  ${role_id}  ${enabled}
347
348    # Description of argument(s):
349    # username            The username to be created.
350    # password            The password to be assigned.
351    # role_id             The role ID of the user to be created
352    #                     (e.g. "Administrator", "Operator", etc.).
353    # enabled             Indicates whether the username being created
354    #                     should be enabled (${True}, ${False}).
355
356    ${status}=  Verify Redfish User Login  ${username}  ${password}
357    # Doing a check of the returned status.
358    Should Be Equal  ${status}  ${enabled}
359
360    # Validate Role Id of user.
361    ${role_config}=  Redfish_Utils.Get Attribute
362    ...  /redfish/v1/AccountService/Accounts/${username}  RoleId
363    Should Be Equal  ${role_id}  ${role_config}
364
365
366Verify Redfish User Login
367    [Documentation]  Verify Redfish login with given user id.
368    [Teardown]  Run Keywords  Run Keyword And Ignore Error  Redfish.Logout  AND  Redfish.Login
369    [Arguments]   ${username}  ${password}
370
371    # Description of argument(s):
372    # username            Login username.
373    # password            Login password.
374
375    # Logout from current Redfish session.
376    Redfish.Logout
377    ${status}=  Run Keyword And Return Status  Redfish.Login  ${username}  ${password}
378    [Return]  ${status}
379
380
381Redfish Create And Verify User
382    [Documentation]  Redfish create and verify user.
383    [Arguments]   ${username}  ${password}  ${role_id}  ${enabled}
384
385    # Description of argument(s):
386    # username            The username to be created.
387    # password            The password to be assigned.
388    # role_id             The role ID of the user to be created
389    #                     (e.g. "Administrator", "Operator", etc.).
390    # enabled             Indicates whether the username being created
391    #                     should be enabled (${True}, ${False}).
392
393    # Example:
394    #{
395    #"@odata.context": "/redfish/v1/$metadata#ManagerAccount.ManagerAccount",
396    #"@odata.id": "/redfish/v1/AccountService/Accounts/test1",
397    #"@odata.type": "#ManagerAccount.v1_0_3.ManagerAccount",
398    #"Description": "User Account",
399    #"Enabled": true,
400    #"Id": "test1",
401    #"Links": {
402    #  "Role": {
403    #    "@odata.id": "/redfish/v1/AccountService/Roles/Administrator"
404    #  }
405    #},
406
407    Redfish Create User  ${username}  ${password}  ${role_id}  ${enabled}
408
409    Redfish Verify User  ${username}  ${password}  ${role_id}  ${enabled}
410
411    # Delete Specified User
412    Redfish.Delete  /redfish/v1/AccountService/Accounts/${username}
413
414Verify Redfish User with Wrong Password
415    [Documentation]  Verify Redfish User with Wrong Password.
416    [Arguments]   ${username}  ${password}  ${role_id}  ${enabled}  ${wrong_password}
417
418    # Description of argument(s):
419    # username            The username to be created.
420    # password            The password to be assigned.
421    # role_id             The role ID of the user to be created
422    #                     (e.g. "Administrator", "Operator", etc.).
423    # enabled             Indicates whether the username being created
424    #                     should be enabled (${True}, ${False}).
425    # wrong_password      Any invalid password.
426
427    Redfish Create User  ${username}  ${password}  ${role_id}  ${enabled}
428
429    Redfish.Logout
430
431    # Attempt to login with created user with invalid password.
432    Run Keyword And Expect Error  InvalidCredentialsError*
433    ...  Redfish.Login  ${username}  ${wrong_password}
434
435    Redfish.Login
436
437    # Delete newly created user.
438    Redfish.Delete  /redfish/v1/AccountService/Accounts/${username}
439
440
441Verify Login with Deleted Redfish User
442    [Documentation]  Verify Login with Deleted Redfish User.
443    [Arguments]   ${username}  ${password}  ${role_id}  ${enabled}
444
445    # Description of argument(s):
446    # username            The username to be created.
447    # password            The password to be assigned.
448    # role_id             The role ID of the user to be created
449    #                     (e.g. "Administrator", "Operator", etc.).
450    # enabled             Indicates whether the username being created
451    #                     should be enabled (${True}, ${False}).
452
453    Redfish Create User  ${username}  ${password}  ${role_id}  ${enabled}
454
455    # Delete newly created user.
456    Redfish.Delete  /redfish/v1/AccountService/Accounts/${userName}
457
458    Redfish.Logout
459
460    # Attempt to login with deleted user account.
461    Run Keyword And Expect Error  InvalidCredentialsError*
462    ...  Redfish.Login  ${username}  ${password}
463
464    Redfish.Login
465
466
467Verify Create User Without Enabling
468    [Documentation]  Verify Create User Without Enabling.
469    [Arguments]   ${username}  ${password}  ${role_id}  ${enabled}
470
471    # Description of argument(s):
472    # username            The username to be created.
473    # password            The password to be assigned.
474    # role_id             The role ID of the user to be created
475    #                     (e.g. "Administrator", "Operator", etc.).
476    # enabled             Indicates whether the username being created
477    #                     should be enabled (${True}, ${False}).
478
479    Redfish Create User  ${username}  ${password}  ${role_id}  ${enabled}  ${False}
480
481    Redfish.Logout
482
483    # Login with created user.
484    Run Keyword And Expect Error  InvalidCredentialsError*
485    ...  Redfish.Login  ${username}  ${password}
486
487    Redfish.Login
488
489    # Delete newly created user.
490    Redfish.Delete  /redfish/v1/AccountService/Accounts/${username}
491
492