Skip to content

Commit

Permalink
Adding Dashboard Folder Permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
safaci2000 committed Apr 12, 2024
1 parent 4ceb66a commit b06083c
Show file tree
Hide file tree
Showing 29 changed files with 716 additions and 79 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/code_scanners.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ on:
- master

env:
GO_VERSION: 1.22.1
GO_VERSION: 1.22.2


permissions:
Expand All @@ -29,7 +29,7 @@ jobs:
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.22.1"
go-version: "1.22.2"
cache: false
- name: Install Task
run: sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
test:
strategy:
matrix:
go: [ {version: 1.22.1, token: 1}, {version: 1.22.1, token: 0}]
go: [ {version: 1.22.2, token: 1}, {version: 1.22.2, token: 0}]
grafana: [ 10.1.4 ]

env:
Expand Down Expand Up @@ -40,11 +40,11 @@ jobs:
run: |
echo "token IS $TEST_TOKEN_CONFIG"
- name: Calc coverage
if: "${{ matrix.go.version == '1.22.1' && matrix.grafana == '10.1.4' && matrix.go.token == '0' }}"
if: "${{ matrix.go.version == '1.22.2' && matrix.grafana == '10.1.4' && matrix.go.token == '0' }}"
run: |
go test -v -covermode=atomic -coverprofile=coverage.out ./...
- name: Convert coverage.out to coverage.lcov
if: "${{ matrix.go.version == '1.22.1' && matrix.grafana == '10.1.4' && matrix.go.token == '0' }}"
if: "${{ matrix.go.version == '1.22.2' && matrix.grafana == '10.1.4' && matrix.go.token == '0' }}"
uses: jandelgado/[email protected]
- name: Test
if: "${{ matrix.go.token == '1' }}"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22.1'
go-version: '1.22.2'
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
Expand Down
111 changes: 62 additions & 49 deletions cli/backup/dashboard_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/esnet/gdg/internal/tools"
"github.com/jedib0t/go-pretty/v6/table"
"log/slog"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -35,6 +36,15 @@ func newDashboardPermissionCmd() simplecobra.Commander {
}
}

// getConnectionTbWriter returns a table object for use with newConnectionsPermissionListCmd
func getDashboardPermTblWriter() table.Writer {
writer := table.NewWriter()
writer.SetOutputMirror(os.Stdout)
writer.SetStyle(table.StyleLight)
writer.AppendHeader(table.Row{"id", "name", "slug", "type", "uid", "url"}, table.RowConfig{AutoMerge: true})
return writer
}

func newDashboardPermissionListCmd() simplecobra.Commander {
description := "List Dashboard Permissions"
return &support.SimpleCommand{
Expand All @@ -45,38 +55,41 @@ func newDashboardPermissionListCmd() simplecobra.Commander {
cmd.Aliases = []string{"l"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
filters := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
slog.Info("Listing Dashboard Permissions for context", "context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"id", "uid", "name", "slug", "type", "default", "url"})
permissions := rootCmd.GrafanaSvc().ListDashboardPermissions(filters)
filters := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
permissions, err := rootCmd.GrafanaSvc().ListDashboardPermissions(filters)
if err != nil {
slog.Error("Failed to retrieve Dashboard Permissions", "error", err)
os.Exit(1)
}

if len(permissions) == 0 {
slog.Info("No Dashboards found")
} else {
rootCmd.TableObj.AppendHeader(table.Row{"id", "Title", "Slug", "Folder", "UID", "URL"})
for _, perms := range permissions {
writer := getDashboardPermTblWriter()
urlValue := getDashboardUrl(perms.Dashboard)
link := perms.Dashboard
rootCmd.TableObj.AppendRow(table.Row{
writer.AppendRow(table.Row{
link.ID, link.Title, link.Slug, link.FolderTitle,
link.UID, urlValue,
})
writer.Render()
if perms.Permissions != nil {
twConfigs := table.NewWriter()
twConfigs.SetOutputMirror(os.Stdout)
twConfigs.SetStyle(table.StyleColoredCyanWhiteOnBlack)
twConfigs.AppendHeader(table.Row{"Dashboard UID", "Dashboard Title", "UserId", "Team", "RoleName", "Permission"})
for _, dashPerm := range perms.Permissions {
twConfigs.AppendRow(table.Row{link.UID, link.Title, dashPerm.UserID, dashPerm.Team, dashPerm.Role, dashPerm.Permission})
}
if len(perms.Permissions) > 0 {

twConfigs.Render()
}
}
// if perms != nil && perms.Enabled {
// for _, perm := range perms.Permissions {
// rootCmd.TableObj.AppendRow(table.Row{link.ID, link.UID, " PERMISSION-->", perm.PermissionName, perm.Team, perm.UserEmail})
// }
// }
}
// rootCmd.Render(cd.CobraCommand, connections)
}
//else {

//}
return nil
},
}
Expand All @@ -91,24 +104,18 @@ func newDashboardPermissionClearCmd() simplecobra.Commander {
cmd.Aliases = []string{"c"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Clear all connections permissions")
tools.GetUserConfirmation(fmt.Sprintf("WARNING: this will clear all permission from all connections on: '%s' "+
"(Or all permission matching yoru --connection filter). Do you wish to continue (y/n) ", config.Config().GetGDGConfig().ContextName,
slog.Info("Clear all Dashboard permissions")
tools.GetUserConfirmation(fmt.Sprintf("WARNING: this will clear all permission from all Dashboards on: '%s' "+
"(Or all permission matching your filters). Do you wish to continue (y/n) ", config.Config().GetGDGConfig().ContextName,
), "", true)
rootCmd.TableObj.AppendHeader(table.Row{"cleared Dashboard permissions"})
//connectionFilter, _ := cd.CobraCommand.Flags().GetString("connection")
//filters := service.NewConnectionFilter(connectionFilter)
//connections := rootCmd.GrafanaSvc().DeleteAllConnectionPermissions(filters)

//if len(connections) == 0 {
// slog.Info("No connections found")
//} else {
// for _, connections := range connections {
// rootCmd.TableObj.AppendRow(table.Row{connections})
// }
// rootCmd.Render(cd.CobraCommand, connections)
//}

filters := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
err := rootCmd.GrafanaSvc().ClearDashboardPermissions(filters)
if err != nil {
slog.Error("Failed to retrieve Dashboard Permissions", "error", err)
} else {
slog.Info("All dashboard permissions have been cleared")
}
return nil
},
}
Expand All @@ -127,18 +134,21 @@ func newDashboardPermissionDownloadCmd() simplecobra.Commander {
slog.Info("Download Connections for context",
"context", config.Config().GetGDGConfig().GetContext())
rootCmd.TableObj.AppendHeader(table.Row{"filename"})
connectionFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(connectionFilter)
connections := rootCmd.GrafanaSvc().DownloadConnectionPermissions(filters)
slog.Info("Downloading connections permissions")
filters := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
permissions, err := rootCmd.GrafanaSvc().DownloadDashboardPermissions(filters)
if err != nil {
slog.Error("Failed to retrieve Dashboard Permissions", "error", err)
os.Exit(1)
}
slog.Info("Downloading Dashboard permissions")

if len(connections) == 0 {
slog.Info("No connections found")
if len(permissions) == 0 {
slog.Info("No Dashboard permissions")
} else {
for _, connections := range connections {
rootCmd.TableObj.AppendRow(table.Row{connections})
for _, perm := range permissions {
rootCmd.TableObj.AppendRow(table.Row{perm})
}
rootCmd.Render(cd.CobraCommand, connections)
rootCmd.Render(cd.CobraCommand, permissions)
}
return nil
},
Expand All @@ -154,19 +164,22 @@ func newDashboardPermissionUploadCmd() simplecobra.Commander {
cmd.Aliases = []string{"u"}
},
RunFunc: func(ctx context.Context, cd *simplecobra.Commandeer, rootCmd *support.RootCommand, args []string) error {
slog.Info("Uploading connections permissions")
rootCmd.TableObj.AppendHeader(table.Row{"connection permission"})
connectionFilter, _ := cd.CobraCommand.Flags().GetString("connection")
filters := service.NewConnectionFilter(connectionFilter)
connections := rootCmd.GrafanaSvc().UploadConnectionPermissions(filters)
slog.Info("Uploading dashboard permissions")
rootCmd.TableObj.AppendHeader(table.Row{"dashboard permission"})
filters := service.NewDashboardFilter(parseDashboardGlobalFlags(cd.CobraCommand)...)
permissions, err := rootCmd.GrafanaSvc().UploadDashboardPermissions(filters)
if err != nil {
slog.Error("Failed to retrieve Dashboard Permissions", "error", err)
os.Exit(1)
}

if len(connections) == 0 {
slog.Info("No connections found")
if len(permissions) == 0 {
slog.Info("No permissions found")
} else {
for _, connections := range connections {
rootCmd.TableObj.AppendRow(table.Row{connections})
for _, perm := range permissions {
rootCmd.TableObj.AppendRow(table.Row{perm})
}
rootCmd.Render(cd.CobraCommand, connections)
rootCmd.Render(cd.CobraCommand, permissions)
}
return nil
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/esnet/gdg

go 1.22.1
go 1.22.2

require (
github.com/AlecAivazis/survey/v2 v2.3.7
Expand Down
2 changes: 2 additions & 0 deletions internal/config/resource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type ResourceType string
const (
ConnectionPermissionResource ResourceType = "connections-permissions"
ConnectionResource ResourceType = "connections"
DashboardPermissionsResource ResourceType = "dashboards-permissions"
DashboardResource ResourceType = "dashboards"
FolderPermissionResource ResourceType = "folders-permissions"
FolderResource ResourceType = "folders"
Expand All @@ -25,6 +26,7 @@ const (

var orgNamespacedResource = map[ResourceType]bool{
ConnectionPermissionResource: true,
DashboardPermissionsResource: true,
ConnectionResource: true,
DashboardResource: true,
FolderPermissionResource: true,
Expand Down
2 changes: 1 addition & 1 deletion internal/service/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func updateSlug(board string) string {
// getFolderFromResourcePath if a use encodes a path separator in path, we can't determine the folder name. This strips away
// all the known components of a resource type leaving only the folder name.
func getFolderFromResourcePath(storageEngine string, filePath string, resourceType config.ResourceType) (string, error) {
basePath := fmt.Sprintf("%s/", config.Config().GetDefaultGrafanaConfig().GetPath(config.DashboardResource))
basePath := fmt.Sprintf("%s/", config.Config().GetDefaultGrafanaConfig().GetPath(resourceType))
//Take into account cloud prefix is enabled
if storageEngine != "" {
cloudType, data := config.Config().GetCloudConfiguration(storageEngine)
Expand Down
5 changes: 4 additions & 1 deletion internal/service/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ type DashboardsApi interface {
}

type DashboardPermissionsApi interface {
ListDashboardPermissions(filterReq filters.Filter) []DashboardAndPermissions
ListDashboardPermissions(filterReq filters.Filter) ([]DashboardAndPermissions, error)
DownloadDashboardPermissions(filterReq filters.Filter) ([]string, error)
ClearDashboardPermissions(filterReq filters.Filter) error
UploadDashboardPermissions(filterReq filters.Filter) ([]string, error)
}

// FoldersApi Contract definition
Expand Down
Loading

0 comments on commit b06083c

Please sign in to comment.