1*** Settings ***
2Documentation    Test IPMI and Redfish combinations for user management.
3
4Resource         ../../lib/openbmc_ffdc.robot
5Resource         ../../lib/ipmi_client.robot
6Resource         ../../lib/resource.robot
7Resource         ../../lib/bmc_redfish_resource.robot
8Resource         ../../lib/openbmc_ffdc.robot
9Resource         ../../lib/ipmi_client.robot
10Library          ../../lib/ipmi_utils.py
11
12Test Setup       Test Setup Execution
13Test Teardown    Test Teardown Execution
14
15
16*** Variables ***
17
18${valid_password}       0penBmc1
19${valid_password2}      0penBmc2
20
21
22*** Test Cases ***
23
24Create IPMI User Without Any Privilege And Verify Via Redfish
25    [Documentation]  Create user using IPMI without privilege and verify user privilege
26    ...  via Redfish.
27    [Tags]  Create_IPMI_User_Without_Any_Privilege_And_Verify_Via_Redfish
28
29    # Create IPMI user with random id and username.
30    ${random_userid}=  Evaluate  random.randint(2, 15)  modules=random
31    ${random_username}=  Generate Random String  8  [LETTERS]
32    Run IPMI Standard Command
33    ...  user set name ${random_userid} ${random_username}
34
35    # Verify new user privilege level via Redfish.
36    ${privilege}=  Redfish_Utils.Get Attribute
37    ...  /redfish/v1/AccountService/Accounts/${random_username}  RoleId
38    Valid Value  privilege  ['ReadOnly']
39
40
41Create Admin User Via Redfish And Verify Login Via IPMI
42    [Documentation]  Create user via redfish and verify via IPMI.
43    [Tags]  Create_Admin_User_Via_Redfish_And_Verify_Login_Via_IPMI
44
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    # Add delay for a new admin user password to set.
55    Sleep  5s
56
57    Enable IPMI Access To User Using Redfish  ${random_username}
58
59    # Update user password using Redfish.
60    ${payload}=  Create Dictionary  Password=${valid_password2}
61    Redfish.Patch  /redfish/v1/AccountService/Accounts/${random_username}  body=&{payload}
62
63    Verify IPMI Username And Password  ${random_username}  ${valid_password2}
64
65
66Delete User Via Redfish And Verify Using IPMI
67    [Documentation]  Delete user via redfish and verify using IPMI.
68    [Tags]  Delete_User_Via_Redfish_And_Verify_Using_IPMI
69
70    # Create user using Redfish.
71    ${random_username}=  Generate Random String  8  [LETTERS]
72    Set Test Variable  ${random_username}
73
74    ${payload}=  Create Dictionary
75    ...  UserName=${random_username}  Password=${valid_password}
76    ...  RoleId=Administrator  Enabled=${True}
77    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
78    ...  valid_status_codes=[${HTTP_CREATED}]
79
80    Enable IPMI Access To User Using Redfish  ${random_username}
81
82    # Update user password using Redfish.
83    ${payload}=  Create Dictionary  Password=${valid_password2}
84    Redfish.Patch  /redfish/v1/AccountService/Accounts/${random_username}  body=&{payload}
85
86    # Delete user using Redfish.
87    Redfish.Delete  /redfish/v1/AccountService/Accounts/${random_username}
88
89    # Verify that IPMI command fails with deleted user.
90    Run Keyword And Expect Error  *Error: Unable to establish IPMI*
91    ...  Verify IPMI Username And Password  ${random_username}  ${valid_password2}
92
93
94Update User Password Via Redfish And Verify Using IPMI
95    [Documentation]  Update user password via Redfish and verify using IPMI.
96    [Tags]  Update_User_Password_Via_Redfish_And_Verify_Using_IPMI
97
98    # Create user using Redfish.
99    ${random_username}=  Generate Random String  8  [LETTERS]
100    Set Test Variable  ${random_username}
101
102    ${payload}=  Create Dictionary
103    ...  UserName=${random_username}  Password=${valid_password}
104    ...  RoleId=Administrator  Enabled=${True}
105    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
106    ...  valid_status_codes=[${HTTP_CREATED}]
107
108    Enable IPMI Access To User Using Redfish  ${random_username}
109
110    # Update user password using Redfish.
111    ${payload}=  Create Dictionary  Password=${valid_password2}
112    Redfish.Patch  /redfish/v1/AccountService/Accounts/${random_username}  body=&{payload}
113
114    # Verify that IPMI command works with new password and fails with older password.
115    Verify IPMI Username And Password  ${random_username}  ${valid_password2}
116
117    Run Keyword And Expect Error  *Error: Unable to establish IPMI*
118    ...  Verify IPMI Username And Password  ${random_username}  ${valid_password}
119
120
121Update User Privilege Via Redfish And Verify Using IPMI
122    [Documentation]  Update user privilege via Redfish and verify using IPMI.
123    [Tags]  Update_User_Privilege_Via_Redfish_And_Verify_Using_IPMI
124
125    # Create user using Redfish with admin privilege.
126    ${random_username}=  Generate Random String  8  [LETTERS]
127    Set Test Variable  ${random_username}
128
129    ${payload}=  Create Dictionary
130    ...  UserName=${random_username}  Password=${valid_password}
131    ...  RoleId=Administrator  Enabled=${True}
132    Redfish.Post  /redfish/v1/AccountService/Accounts  body=&{payload}
133    ...  valid_status_codes=[${HTTP_CREATED}]
134
135    Enable IPMI Access To User Using Redfish  ${random_username}
136
137    # Update user password using Redfish.
138    ${payload}=  Create Dictionary  Password=${valid_password2}
139    Redfish.Patch  /redfish/v1/AccountService/Accounts/${random_username}  body=&{payload}
140
141    # Update user privilege to readonly using Redfish.
142    ${payload}=  Create Dictionary  RoleId=ReadOnly
143    Redfish.Patch  /redfish/v1/AccountService/Accounts/${random_username}  body=&{payload}
144
145    # Verify new user privilege level via IPMI.
146    ${resp}=  Run IPMI Standard Command  user list
147
148    # Example of response data:
149    # ID  Name             Callin  Link Auth  IPMI Msg   Channel Priv Limit
150    # 1   ipmi_admin       false   true       true       ADMINISTRATOR
151    # 2   OAvCxjMv         false   true       true       USER
152    # 3                    true    false      false      NO ACCESS
153    # ..
154    # ..
155    # 15                   true    false      false      NO ACCESS
156
157    ${user_info}=
158    ...  Get Lines Containing String  ${resp}  ${random_username}
159    Should Contain  ${user_info}  USER
160
161
162*** Keywords ***
163
164Create IPMI Random User With Password And Privilege
165    [Documentation]  Create random IPMI user with given password and privilege
166    ...  level.
167    [Arguments]  ${password}  ${privilege}=0
168
169    # Description of argument(s):
170    # password      Password to be assigned for the user.
171    # privilege     Privilege level for the user (e.g. "1", "2", "3", etc.).
172
173    # Create IPMI user.
174    ${random_username}=  Generate Random String  8  [LETTERS]
175    Set Suite Variable  ${random_username}
176
177    ${random_userid}=  Find And Return Free User Id
178    IPMI Create User  ${random_userid}  ${random_username}
179
180    # Set given password for newly created user.
181    Run IPMI Standard Command
182    ...  user set password ${random_userid} ${password}
183
184    # Enable IPMI user.
185    Run IPMI Standard Command  user enable ${random_userid}
186
187    # Set given privilege and enable IPMI messaging for newly created user.
188    Run Keyword If  '${privilege}' != '0'
189    ...  Set Channel Access  ${random_userid}  ipmi=on privilege=${privilege}
190
191    [Return]  ${random_username}  ${random_userid}
192
193
194Delete Users Via Redfish
195    [Documentation]  Delete all the users via redfish from given list.
196    [Arguments]  ${user_list}
197
198    # Description of argument(s):
199    # user_list    List of user which are to be deleted.
200
201    Redfish.Login
202
203    FOR  ${user}  IN  @{user_list}
204      Redfish.Delete  ${user}
205    END
206
207    Redfish.Logout
208
209
210Enable IPMI Access To User Using Redfish
211    [Documentation]  Add IPMI access to a user through Redfish.
212    [Arguments]  ${user_name}
213
214    # Description of argument(s):
215    # user_name  User name to which IPMI access is to be added.
216
217    # Adding IPMI access to user name.
218    Redfish.Patch    /redfish/v1/AccountService/Accounts/${user_name}
219    ...  body={"AccountTypes": ["Redfish", "HostConsole", "ManagerConsole", "WebUI", "IPMI"]}
220
221
222Test Setup Execution
223    [Documentation]  Do test case setup tasks.
224
225    Redfish.Login
226
227
228Test Teardown Execution
229    [Documentation]  Do the post test teardown.
230
231    FFDC On Test Case Fail
232    # Delete the test user.
233    Run Keyword And Ignore Error
234    ...  Redfish.Delete  /redfish/v1/AccountService/Accounts/${random_username}
235
236    Redfish.Logout
237
238
239Find And Return Free User Id
240    [Documentation]  Find and return userid that is not being used.
241
242    FOR    ${index}    IN RANGE    300
243        # IPMI maximum users count (i.e. 15 users).
244        ${random_userid}=  Evaluate  random.randint(1, ${ipmi_max_num_users})  modules=random
245        ${access_output}=  Run IPMI Standard Command  channel getaccess 1 ${random_userid}
246
247        ${name_line}=  Get Lines Containing String  ${access_output}  User Name
248        Log To Console  For ID ${random_userid}: ${name_line}
249        ${is_empty}=  Run Keyword And Return Status
250        ...  Should Match Regexp  ${name_line}  ${empty_name_pattern}
251
252        Exit For Loop If  ${is_empty} == ${True}
253    END
254    Run Keyword If  '${index}' == '299'  Fail  msg=A free user ID could not be found.
255    [Return]  ${random_userid}
256
257