Skip to content

Checking for permissions of users and teams #100

@meyerder

Description

@meyerder

This is a work in progress but you seem to be a major WIZ at json (I stink at it) This is something I am working on as well.. The output needs to be worked on and the for each loop for each dashboard is not quite right yet either. You might be able to modify this.

Ideally, I would like to know if any users or teams have access to the folder and or the dashboards. I have NOT thought about doing this down at the data source levels (In my case that may be overkill but others MIGHT like it).

#!/bin/bash

GRAFANA_API_URL="https://xxx/api"
API_KEY="xxxxx"

get_permissions() {
    local uid="$1"
    local endpoint="$2"
    curl -s -H "Authorization: Bearer ${API_KEY}" "$GRAFANA_API_URL/$endpoint/$uid/permissions"
}

response=$(curl -s -H "Authorization: Bearer ${API_KEY}" "$GRAFANA_API_URL/search")

IFS=$'\n' dash_folders=($(echo "$response" | jq -r '.[] | select(.type=="dash-folder") | .uid'))
IFS=$'\n' dash_folder_titles=($(echo "$response" | jq -r '.[] | select(.type=="dash-folder") | .title'))

for index in "${!dash_folders[@]}"; do
    folder_uid=${dash_folders[$index]}
    folder_title=${dash_folder_titles[$index]}
    echo "$folder_title, $folder_uid,"

    permissions=$(get_permissions "$folder_uid" "folders")
    length=$(echo "$permissions" | jq length)
    for ((i=0; i<$length; i++)); do
        team=$(echo "$permissions" | jq -r ".[$i].team // \"N/A\"")
        user=$(echo "$permissions" | jq -r ".[$i].user // \"N/A\"")
        permissionName=$(echo "$permissions" | jq -r ".[$i].permissionName // \"N/A\"")

        if [[ "$team" != "N/A" ]]; then
            echo "Team $team - $permissionName"
        fi

        if [[ "$user" != "N/A" ]]; then
            echo "User $user - $permissionName"
        fi
    done

    echo "-------Dashboards in Folder ---"
    IFS=$'\n' dash_dbs_in_folder=($(echo "$response" | jq -r ".[] | select(.type==\"dash-db\" and .folderId == ${dash_folders[$index]}) | .title"))
    IFS=$'\n' dash_dbs_uids=($(echo "$response" | jq -r ".[] | select(.type==\"dash-db\" and .folderId == ${dash_folders[$index]}) | .uid"))

    for dash_index in "${!dash_dbs_in_folder[@]}"; do
        dashboard_title="${dash_dbs_in_folder[$dash_index]}"
        dashboard_uid="${dash_dbs_uids[$dash_index]}"
        echo "$folder_title - $dashboard_title"

        permissions=$(get_permissions "$dashboard_uid" "dashboards")
        length=$(echo "$permissions" | jq length)
        for ((i=0; i<$length; i++)); do
            team=$(echo "$permissions" | jq -r ".[$i].team // \"N/A\"")
            user=$(echo "$permissions" | jq -r ".[$i].user // \"N/A\"")
            permissionName=$(echo "$permissions" | jq -r ".[$i].permissionName // \"N/A\"")

            if [[ "$team" != "N/A" ]]; then
                echo "Team $team - $permissionName"
            fi

            if [[ "$user" != "N/A" ]]; then
                echo "User $user - $permissionName"
            fi
        done
    done
    echo "----------------------"
done
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    pitchA pitch for a new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions