Skip to content

Commit c7f080a

Browse files
authored
Add basic rbac test (#21)
1 parent 2c7f93f commit c7f080a

File tree

1 file changed

+130
-3
lines changed

1 file changed

+130
-3
lines changed

testcases/smoke_test.sh

Lines changed: 130 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,23 @@ expectedCount=$((k6_log_events + log_events))
2727

2828
input_file=$PWD/input.json
2929

30-
curl_std_opts=( -sS --header 'Content-Type: application/json' -w '\n\n%{http_code}' -u "$username":"$password" )
31-
3230
alert_body='{"alerts":[{"message":"server side error occurred","name":"Status Alert","rule":{"config":{"column":"status","operator":"notEqualTo","repeats":2,"value":500},"type":"column"},"targets":[{"endpoint":"https://webhook.site/6b184e08-82c4-46dc-b344-5b85414c2a71","headers":{},"repeat":{"interval":"30s","times":5},"skip_tls_check":false,"type":"webhook"},{"endpoint":"https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX","repeat":{"interval":"3m 20s","times":5},"type":"slack"}]}],"version":"v1"}'
3331

3432
schema_body='{"fields":[{"name":"app_meta","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"bytes","data_type":"Int64","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"datetime","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"device_id","data_type":"Int64","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"host","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"level","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"location","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"message","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"method","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"os","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"p_metadata","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"p_tags","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"p_timestamp","data_type":{"Timestamp":["Millisecond",null]},"nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"process_id","data_type":"Int64","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"protocol","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"referer","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"request","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"request_body","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"response_time","data_type":"Int64","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"runtime","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"session_id","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"source_time","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"status","data_type":"Int64","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"status_code","data_type":"Int64","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"timezone","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"user-identifier","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"user_agent","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"user_id","data_type":"Int64","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"uuid","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}},{"name":"version","data_type":"Utf8","nullable":true,"dict_id":0,"dict_is_ordered":false,"metadata":{}}],"metadata":{}}'
3533

3634
retention_body='[{"description":"delete after 20 days","action":"delete","duration":"20d"}]'
3735

36+
test_user="alice"
37+
role_editor='[{"role": "editor"}]'
38+
39+
set_std_opts() {
40+
local username=$1
41+
local password=$2
42+
curl_std_opts=( -sS --header 'Content-Type: application/json' -w '\n\n%{http_code}' -u "$username":"$password" )
43+
}
44+
45+
set_std_opts "$username" "$password"
46+
3847
# Generate events using flog (https://github.com/mingrammer/flog) and store it in input.json file
3948
create_input_file () {
4049
flog -f json -n "$log_events" -t log -o "$input_file"
@@ -329,6 +338,118 @@ get_retention () {
329338
return 0
330339
}
331340

341+
# create User
342+
put_user () {
343+
response=$(curl "${curl_std_opts[@]}" --request PUT "$parseable_url"/api/v1/user/"$test_user")
344+
if [ $? -ne 0 ]; then
345+
printf "Failed to create user %s with exit code: %s\n" "$test_user" "$?"
346+
printf "Test create_user: failed\n"
347+
exit 1
348+
fi
349+
350+
http_code=$(tail -n1 <<< "$response")
351+
if [ "$http_code" -ne 200 ]; then
352+
printf "Failed to create user %s with http code: %s and response: %s\n" "$test_user" "$http_code" "$content"
353+
printf "Test set_retention: failed\n"
354+
exit 1
355+
fi
356+
357+
# set curl options to user incluse test user and the passphrase
358+
test_password=$(sed -n '1p' <<< "$response")
359+
360+
printf "Test create_user: successful\n"
361+
return 0
362+
}
363+
364+
put_role() {
365+
response=$(curl "${curl_std_opts[@]}" --request PUT "$parseable_url"/api/v1/user/"$test_user"/roles --data-raw "$role_editor")
366+
if [ $? -ne 0 ]; then
367+
printf "Failed put role for user %s with exit code: %s\n" "$test_user" "$?"
368+
printf "Test create_user: failed\n"
369+
exit 1
370+
fi
371+
372+
http_code=$(tail -n1 <<< "$response")
373+
if [ "$http_code" -ne 200 ]; then
374+
printf "Failed to put role for user %s with http code: %s and response: %s\n" "$test_user" "$http_code" "$response"
375+
printf "Test set_retention: failed\n"
376+
exit 1
377+
fi
378+
379+
printf "Test put_role: successful\n"
380+
return 0
381+
}
382+
383+
# check api access for this new user
384+
check_api_access() {
385+
# can call non protected api
386+
response=$(curl "${curl_std_opts[@]}" --request GET "$parseable_url"/api/v1/liveness)
387+
if [ $? -ne 0 ]; then
388+
printf "Failed to get liveness api for new user with exit code: %s\n" "$?"
389+
printf "Test check_api_access: failed\n"
390+
exit 1
391+
fi
392+
393+
http_code=$(tail -n1 <<< "$response")
394+
if [ "$http_code" -ne 200 ]; then
395+
printf "Failed to get liveness api for new user with http code: %s and response: %s", "$http_code" "$response"
396+
printf "Test check_api_access: failed\n"
397+
exit 1
398+
fi
399+
400+
# can call protected api with access
401+
response=$(curl "${curl_std_opts[@]}" --request GET "$parseable_url"/api/v1/logstream)
402+
if [ $? -ne 0 ]; then
403+
printf "Failed to get logstream api for new user with exit code: %s\n" "$?"
404+
printf "Test check_api_access: failed\n"
405+
exit 1
406+
fi
407+
408+
http_code=$(tail -n1 <<< "$response")
409+
if [ "$http_code" -ne 200 ]; then
410+
printf "Failed to get logstream api for new user with http code: %s and response: %s", "$http_code" "$response"
411+
printf "Test check_api_access: failed\n"
412+
exit 1
413+
fi
414+
415+
# cannot call protected api without access
416+
response=$(curl "${curl_std_opts[@]}" --request DELETE "$parseable_url"/api/v1/logstream/"$stream_name")
417+
if [ $? -ne 0 ]; then
418+
printf "Failed when calling delete stream api for new user with exit code: %s\n" "$?"
419+
printf "Test check_api_access: failed\n"
420+
exit 1
421+
fi
422+
423+
http_code=$(tail -n1 <<< "$response")
424+
if [ "$http_code" -ne 401 ]; then
425+
printf "Delete api did not return unauthorized (403) for user %s, http code: %s and response: %s", "$test_user", "$http_code" "$response"
426+
printf "Test check_api_access: failed\n"
427+
exit 1
428+
fi
429+
430+
printf "Test check_api_access: successful\n"
431+
return 0
432+
}
433+
434+
delete_user() {
435+
response=$(curl "${curl_std_opts[@]}" --request DELETE "$parseable_url"/api/v1/user/"$test_user")
436+
if [ $? -ne 0 ]; then
437+
printf "Failed delete user %s with exit code: %s\n" "$test_user" "$?"
438+
printf "Test delete_user: failed\n"
439+
exit 1
440+
fi
441+
442+
http_code=$(tail -n1 <<< "$response")
443+
if [ "$http_code" -ne 200 ]; then
444+
printf "Failed to delete user %s with http code: %s and response: %s\n" "$test_user" "$http_code" "$content"
445+
printf "Test set_retention: failed\n"
446+
exit 1
447+
fi
448+
449+
printf "Test delete_user: successful\n"
450+
return 0
451+
}
452+
332453
# Delete stream
333454
delete_stream () {
334455
response=$(curl "${curl_std_opts[@]}" --request DELETE "$parseable_url"/api/v1/logstream/"$stream_name")
@@ -373,13 +494,19 @@ create_stream
373494
post_event_data
374495
list_log_streams
375496
get_streams_schema
376-
## sleep for a minute to ensure all data is pushed to backend
497+
# sleep for a minute to ensure all data is pushed to backend
377498
sleep 65
378499
query_log_stream
379500
set_alert
380501
get_alert
381502
set_retention
382503
get_retention
504+
put_user
505+
put_role
506+
set_std_opts $test_user "$test_password"
507+
check_api_access
508+
set_std_opts $username "$password"
509+
delete_user
383510
delete_stream
384511
cleanup
385512
printf "======= Smoke tests completed ======\n"

0 commit comments

Comments
 (0)