Skip to content

Instantly share code, notes, and snippets.

@lancewf
Last active June 17, 2020 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lancewf/fca0594b11d66656006f335c7a5cc138 to your computer and use it in GitHub Desktop.
Save lancewf/fca0594b11d66656006f335c7a5cc138 to your computer and use it in GitHub Desktop.
Helper bash function when working with Chef Automate
function get_nodemanager_nodes() {
curl -s -f --insecure -H "api-token: $(get_admin_token)" https://localhost/api/v0/nodes/search -d '{
"filters": [
{"key": "manager_id", "values": ["automate"]}
]
}' | jq
}
function send_external_chef_run_example() {
install_if_missing core/curl curl
install_if_missing core/jq-static jq
local AUTOMATE_URL=${1}
local token=${2}
local examples_json_path=${JSON_FILE:-/src/components/ingest-service/examples/converge-success-report.json}
local endpoint="$AUTOMATE_URL/data-collector/v0"
uuid=$(uuidgen)
entity_uuid=$(uuidgen)
tmp_ccr_json="$(jq --arg id "$uuid" '.id = $id' <$examples_json_path)"
tmp_ccr_json="$(echo $tmp_ccr_json | jq --arg id "$entity_uuid" '.entity_uuid = $id')"
echo "$tmp_ccr_json" \
| curl -f --insecure -H "api-token: $token" \
--data "@-" ${endpoint}
}
function send_external_chef_action_example() {
local AUTOMATE_URL=${1}
local token=${2}
local event_types=(cookbook bag client node policyfile role scanjobs profile)
local event_type=${event_types[$(($RANDOM % 8))]}
local event_tasks=(update create delete)
local event_task=${event_tasks[$(($RANDOM % 3))]}
if [[ "$1" != "" ]]; then
event_type=$1
fi
if [[ "$2" != "" ]]; then
event_task=$2
fi
local endpoint="/events/data-collector"
local examples_path="/src/components/ingest-service/examples";
local tmp_action_json="/tmp/chef_action.json";
local rfc_time=$(date --rfc-3339=seconds -d "1 hour ago" | sed 's/ /T/' | sed 's/\+.*/Z/');
# Update the 'recorded_at' time from the ChefAction Example
tmp_action_json="$(sed 's/recorded_at.*/recorded_at": "'${rfc_time}'",/' $examples_path/chef_action.json)"
tmp_action_json="$(echo $tmp_action_json | sed 's/entity_type": "[0-9A-Za-z]*",/entity_type": "'$event_type'",/')"
tmp_action_json="$(echo $tmp_action_json | sed 's/task": "[0-9A-Za-z]*",/task": "'$event_task'",/')"
echo $tmp_action_json \
| curl -f -H "api-token: $token" \
--data "@-" "$AUTOMATE_URL/data-collector/v0"
}
function wstatus() {
install_if_missing core/procps-ng watch
watch -n 1 chef-automate status
}
function compliance_grpcurl_get() {
chef-automate dev grpcurl compliance-service -- "$@"
}
function number_of_compliance_docs(){
install_if_missing core/static-jq jq
curl -X GET $ELASTICSEARCH_URL/comp-2-*/_doc/_count -H 'Content-Type: application/json' -d '{"query": { "exists": {"field": "controls"}}}' | jq
}
function number_of_runs_docs() {
install_if_missing core/static-jq jq
curl -X GET $ELASTICSEARCH_URL/converge-history-*/_count -H 'Content-Type: application/json' -d '{"query": { "exists": {"field": "entity_uuid"}}}' | jq
}
function watch_number_of_compliance_docs(){
install_if_missing core/procps-ng watch
watch -n 5 "curl -s -X GET $ELASTICSEARCH_URL/comp-2-*/_doc/_count -H 'Content-Type: application/json' -d '{\"query\": { \"exists\": {\"field\": \"controls\"}}}' | jq ."
}
function authz_project_rules() {
chef-automate dev grpcurl authz-service -- chef.automate.domain.authz.v2.Projects.ListProjectRules
}
function ingest_debug_logs() {
chef-automate debug set-log-level ingest-service debug
}
function authz_create_project() {
chef-automate dev grpcurl authz-service -- chef.automate.domain.authz.v2.Projects.CreateProject -d '{"name": "project9", "id": "project9"}'
}
function authz_update_project() {
chef-automate dev grpcurl authz-service -- chef.automate.domain.authz.v2.Projects.UpdateProject -d '{"name": "project9", "id": "project9"}'
}
function es_job_status() {
install_if_missing core/static-jq jq
curl "$ELASTICSEARCH_URL/_tasks/$1" | jq
}
function data_lifecycle_trigger_purge() {
chef-automate dev grpcurl data-lifecycle-service -- chef.automate.domain.data_lifecycle.DataLifecycle.TriggerPurge -d '{}'
}
function event_publish_project_rules_update() {
uuid=$(uuidgen)
chef-automate dev grpcurl event-service -- -d '{"Msg": {"data": {"ProjectUpdateID": "'$uuid'"}, "Type": {"Name": "projectRulesUpdate"}}}' chef.automate.domain.event.api.EventService.Publish
}
function ingest_event_publish_project_rules_update() {
uuid=$(uuidgen)
chef-automate dev grpcurl ingest-service -- -d '{"data": {"ProjectUpdateID": "'$uuid'"}, "Type": {"Name": "projectRulesUpdate"}}' chef.automate.domain.ingest.EventHandler.HandleEvent
}
function a2TokenFactory() {
install_if_missing core/jo jo
default_token_mode=create
default_count=5
token_mode=${1:-$default_token_mode}
count=${2:-$default_count}
id_prefix=test-token
TARGET_HOST="https://localhost"
if [[ $token_mode == create ]]; then
echo "Creating $count tokens..."
for (( i = 1; i <= $count; i++ ))
do
jo -p id="$id_prefix-$i" name="$id_prefix $i" active=true | curl -sSkH "api-token: $(get_admin_token)" $TARGET_HOST/apis/iam/v2/tokens -X POST --data @- > /dev/null
done
elif [[ $token_mode == delete ]]; then
echo "Removing $count tokens..."
for (( i = 1; i <= $count; i++ ))
do
curl -sSkH "api-token: $(get_admin_token)" $TARGET_HOST/apis/iam/v2/tokens/$id_prefix-$i -X DELETE > /dev/null
done
else
echo 'usage: a2TokenFactory ( create | delete ) ( count )'
echo 'default: a2TokenFactory create 5'
fi
}
function a2UsersFactory() {
install_if_missing core/jo jo
default_token_mode=create
default_count=5
token_mode=${1:-$default_token_mode}
count=${2:-$default_count}
id_prefix="testuser"
TARGET_HOST="https://localhost"
if [[ $token_mode == create ]]; then
echo "Creating $count user..."
for (( i = 1; i <= $count; i++ ))
do
jo -p name="$id_prefix $i" id="$id_prefix-$i" password="password" | curl -sSkH "api-token: $(get_admin_token)" $TARGET_HOST/apis/iam/v2/users -X POST --data @- > /dev/null
done
elif [[ $token_mode == delete ]]; then
echo "Removing $count users..."
for (( i = 1; i <= $count; i++ ))
do
curl -sSkH "api-token: $(get_admin_token)" $TARGET_HOST/apis/iam/v2/users/$id_prefix-$i -X DELETE > /dev/null
done
else
echo 'usage: a2UsersFactory ( create | delete ) ( count )'
echo 'default: a2UsersFactory create 5'
fi
}
function a2TeamFactory() {
install_if_missing core/jo jo
default_token_mode=create
default_count=5
token_mode=${1:-$default_token_mode}
count=${2:-$default_count}
id_prefix="team"
TARGET_HOST="https://localhost"
if [[ $token_mode == create ]]; then
echo "Creating $count user..."
for (( i = 1; i <= $count; i++ ))
do
jo -p name="$id_prefix $i" id="$id_prefix-$i" | curl -sSkH "api-token: $(get_admin_token)" $TARGET_HOST/apis/iam/v2/teams -X POST --data @- > /dev/null
done
elif [[ $token_mode == delete ]]; then
echo "Removing $count users..."
for (( i = 1; i <= $count; i++ ))
do
curl -sSkH "api-token: $(get_admin_token)" $TARGET_HOST/apis/iam/v2/teams/$id_prefix-$i -X DELETE > /dev/null
done
else
echo 'usage: a2UsersFactory ( create | delete ) ( count )'
echo 'default: a2UsersFactory create 5'
fi
}
function unable_desktop() {
check_if_deployinate_started || return 1
printf "[deployment.v1.svc]\n products = [\"automate\"]\n" > /tmp/unable_desktop_patch.toml
chef-automate config patch /tmp/unable_desktop_patch.toml
}
function enable_chef_server() {
check_if_deployinate_started || return 1
printf "[deployment.v1.svc]\n products = [\"chef-server\"]\n" > /tmp/enable_chef_server_patch.toml
chef-automate config patch /tmp/enable_chef_server_patch.toml
}
function back_date_nodes() {
curl -X POST "$ELASTICSEARCH_URL/node-state/_update_by_query" -H 'Content-Type: application/json' -d'
{
"script": {
"source": "ctx._source.created = \"2020-03-30T02:00:49.111812725Z\"",
"lang": "painless"
}
}
' | jq
}
document "send_effortless_chef_run_example" <<DOC
Send the example effortless chef run message to the data collector rest endpoint.
DOC
function send_effortless_chef_run_example() {
JSON_FILE=/src/components/ingest-service/examples/effortless_ccr.json send_chef_run_example lb
}
document "send_inspec_example_to_dev" <<DOC
Send the example inspec message to the inspec rest endpoint.
DOC
function send_inspec_example_to_dev() {
local AUTOMATE_URL=${1}
local token=${2}
install_if_missing core/curl curl
local examples_json_path=${JSON_FILE:-/src/components/compliance-service/ingest/examples/compliance-success-tiny-report.json}
local uuid
local report_uuid
local rfc_time
uuid=$(uuidgen)
report_uuid=$(uuidgen)
rfc_time=$(date +%FT%TZ -d "$((RANDOM % 24)) hour ago")
tmp_inspec_json="$(jq --arg id "$uuid" --arg report_uuid "$report_uuid" --arg rfc_time "$rfc_time" '.node_uuid = $id | .report_uuid = $report_uuid | .end_time = $rfc_time' <$examples_json_path)"
echo "$tmp_inspec_json" | curl -f --insecure -H "api-token: $token" \
--data "@-" "$AUTOMATE_URL/data-collector/v0"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment