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