Skip to content

Commit

Permalink
Prevent long UIDs from being used
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Jul 26, 2021
1 parent 92a793a commit f6dfc44
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion dashboard/dashboard.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dashboard

import (
"crypto/sha1"
"encoding/hex"
"encoding/json"

"github.com/K-Phoen/grabana/row"
Expand Down Expand Up @@ -120,7 +122,14 @@ func ID(id uint) Option {
// UID sets the UID used by the dashboard.
func UID(uid string) Option {
return func(builder *Builder) {
builder.board.UID = uid
validUid := uid

if len(uid) > 40 {
sha := sha1.Sum([]byte(uid))
validUid = hex.EncodeToString(sha[:])
}

builder.board.UID = validUid
}
}

Expand Down
10 changes: 10 additions & 0 deletions dashboard/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ func TestDashboardUIDCanBeSet(t *testing.T) {
req.Equal("foo", panel.board.UID)
}

func TestDashboardUIDWillBeHashedWhenTooLongForGrafana(t *testing.T) {
req := require.New(t)

originalUid := "this-uid-is-more-than-forty-characters-and-grafana-does-not-like-it"
panel := New("", UID(originalUid))

req.NotEqual(originalUid, panel.board.UID)
req.Len(panel.board.UID, 40)
}

func TestDashboardCanBeMadeReadOnly(t *testing.T) {
req := require.New(t)

Expand Down

0 comments on commit f6dfc44

Please sign in to comment.