Grant service account admin permissions upon creating a datasource
This commit is contained in:
@@ -447,6 +447,7 @@ func (hs *HTTPServer) AddDataSource(c *contextmodel.ReqContext) response.Respons
|
||||
datasourcesLogger.Debug("Received command to add data source", "url", cmd.URL)
|
||||
cmd.OrgID = c.SignedInUser.GetOrgID()
|
||||
cmd.UserID = userID
|
||||
cmd.UserRequester = c.SignedInUser
|
||||
if cmd.URL != "" {
|
||||
if resp := validateURL(cmd.Type, cmd.URL); resp != nil {
|
||||
return resp
|
||||
|
||||
@@ -78,7 +78,7 @@ func (r ReceiverPermissionsService) SetDefaultPermissions(ctx context.Context, o
|
||||
r.log.Debug("Setting default permissions for receiver", "receiver_uid", uid)
|
||||
permissions := defaultPermissions()
|
||||
clearCache := false
|
||||
if user != nil && user.IsIdentityType(claims.TypeUser) {
|
||||
if user != nil && user.IsIdentityType(claims.TypeUser, claims.TypeServiceAccount) {
|
||||
userID, err := user.GetInternalID()
|
||||
if err != nil {
|
||||
r.log.Error("Could not make user admin", "receiver_uid", uid, "id", user.GetID(), "error", err)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/apimachinery/identity"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
"github.com/grafana/grafana/pkg/services/user"
|
||||
@@ -176,6 +177,9 @@ type AddDataSourceCommand struct {
|
||||
ReadOnly bool `json:"-"`
|
||||
EncryptedSecureJsonData map[string][]byte `json:"-"`
|
||||
UpdateSecretFn UpdateSecretFn `json:"-"`
|
||||
|
||||
// Refactor to use User field instead of UserRequester
|
||||
UserRequester identity.Requester
|
||||
}
|
||||
|
||||
// Also acts as api DTO
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/authlib/claims"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
|
||||
sdkproxy "github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
|
||||
@@ -290,7 +291,7 @@ func (s *Service) AddDataSource(ctx context.Context, cmd *datasources.AddDataSou
|
||||
{BuiltinRole: "Viewer", Permission: "Query"},
|
||||
{BuiltinRole: "Editor", Permission: "Query"},
|
||||
}
|
||||
if cmd.UserID != 0 {
|
||||
if cmd.UserID != 0 || (cmd.UserRequester.IsIdentityType(claims.TypeUser, claims.TypeServiceAccount)) {
|
||||
permissions = append(permissions, accesscontrol.SetResourcePermissionCommand{UserID: cmd.UserID, Permission: "Admin"})
|
||||
}
|
||||
if _, err = s.permissionsService.SetPermissions(ctx, cmd.OrgID, dataSource.UID, permissions...); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user