2022-12-02 08:10:03 -06:00
|
|
|
package clients
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-02-06 10:23:53 -06:00
|
|
|
"strings"
|
2022-12-02 08:10:03 -06:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2023-02-21 09:21:18 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/anonymous"
|
2022-12-02 08:10:03 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/authn"
|
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
2023-01-26 03:50:44 -06:00
|
|
|
var _ authn.ContextAwareClient = new(Anonymous)
|
2022-12-02 08:10:03 -06:00
|
|
|
|
2023-02-21 09:21:18 -06:00
|
|
|
func ProvideAnonymous(cfg *setting.Cfg, orgService org.Service, anonSessionService anonymous.Service) *Anonymous {
|
2022-12-02 08:10:03 -06:00
|
|
|
return &Anonymous{
|
2023-02-21 09:21:18 -06:00
|
|
|
cfg: cfg,
|
|
|
|
log: log.New("authn.anonymous"),
|
|
|
|
orgService: orgService,
|
|
|
|
anonSessionService: anonSessionService,
|
2022-12-02 08:10:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Anonymous struct {
|
2023-02-21 09:21:18 -06:00
|
|
|
cfg *setting.Cfg
|
|
|
|
log log.Logger
|
|
|
|
orgService org.Service
|
|
|
|
anonSessionService anonymous.Service
|
2022-12-02 08:10:03 -06:00
|
|
|
}
|
|
|
|
|
2023-01-26 03:50:44 -06:00
|
|
|
func (a *Anonymous) Name() string {
|
|
|
|
return authn.ClientAnonymous
|
|
|
|
}
|
|
|
|
|
2022-12-02 08:10:03 -06:00
|
|
|
func (a *Anonymous) Authenticate(ctx context.Context, r *authn.Request) (*authn.Identity, error) {
|
|
|
|
o, err := a.orgService.GetByName(ctx, &org.GetOrgByNameQuery{Name: a.cfg.AnonymousOrgName})
|
|
|
|
if err != nil {
|
|
|
|
a.log.FromContext(ctx).Error("failed to find organization", "name", a.cfg.AnonymousOrgName, "error", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-02-21 09:21:18 -06:00
|
|
|
go func() {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
a.log.Warn("tag anon session panic", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-02-22 07:31:08 -06:00
|
|
|
if err := a.anonSessionService.TagSession(context.Background(), r.HTTPRequest); err != nil {
|
2023-02-24 04:26:55 -06:00
|
|
|
a.log.Warn("failed to tag anonymous session", "error", err)
|
2023-02-21 09:21:18 -06:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-12-02 08:10:03 -06:00
|
|
|
return &authn.Identity{
|
2023-01-26 03:50:44 -06:00
|
|
|
IsAnonymous: true,
|
2023-01-05 13:17:41 -06:00
|
|
|
OrgID: o.ID,
|
|
|
|
OrgName: o.Name,
|
|
|
|
OrgRoles: map[int64]org.RoleType{o.ID: org.RoleType(a.cfg.AnonymousOrgRole)},
|
2023-03-08 06:35:54 -06:00
|
|
|
ClientParams: authn.ClientParams{SyncPermissions: true},
|
2022-12-02 08:10:03 -06:00
|
|
|
}, nil
|
|
|
|
}
|
2022-12-19 02:22:11 -06:00
|
|
|
|
|
|
|
func (a *Anonymous) Test(ctx context.Context, r *authn.Request) bool {
|
|
|
|
// If anonymous client is register it can always be used for authentication
|
|
|
|
return true
|
|
|
|
}
|
2023-01-26 03:50:44 -06:00
|
|
|
|
|
|
|
func (a *Anonymous) Priority() uint {
|
|
|
|
return 100
|
|
|
|
}
|
2023-02-06 10:23:53 -06:00
|
|
|
|
|
|
|
func (a *Anonymous) UsageStatFn(ctx context.Context) (map[string]interface{}, error) {
|
|
|
|
m := map[string]interface{}{}
|
|
|
|
|
|
|
|
// Add stats about anonymous auth
|
|
|
|
m["stats.anonymous.customized_role.count"] = 0
|
|
|
|
if !strings.EqualFold(a.cfg.AnonymousOrgRole, "Viewer") {
|
|
|
|
m["stats.anonymous.customized_role.count"] = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|