Auth: Separate anonymous settings to its own struct (#97791)
separate anonymous settings to its own struct
This commit is contained in:
@@ -39,9 +39,9 @@ func (a *Anonymous) Name() string {
|
||||
}
|
||||
|
||||
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})
|
||||
o, err := a.orgService.GetByName(ctx, &org.GetOrgByNameQuery{Name: a.cfg.Anonymous.OrgName})
|
||||
if err != nil {
|
||||
a.log.FromContext(ctx).Error("Failed to find organization", "name", a.cfg.AnonymousOrgName, "error", err)
|
||||
a.log.FromContext(ctx).Error("Failed to find organization", "name", a.cfg.Anonymous.OrgName, "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ func (a *Anonymous) Authenticate(ctx context.Context, r *authn.Request) (*authn.
|
||||
}
|
||||
|
||||
func (a *Anonymous) IsEnabled() bool {
|
||||
return a.cfg.AnonymousEnabled
|
||||
return a.cfg.Anonymous.Enabled
|
||||
}
|
||||
|
||||
func (a *Anonymous) Test(ctx context.Context, r *authn.Request) bool {
|
||||
@@ -77,7 +77,7 @@ func (a *Anonymous) IdentityType() claims.IdentityType {
|
||||
}
|
||||
|
||||
func (a *Anonymous) ResolveIdentity(ctx context.Context, orgID int64, typ claims.IdentityType, id string) (*authn.Identity, error) {
|
||||
o, err := a.orgService.GetByName(ctx, &org.GetOrgByNameQuery{Name: a.cfg.AnonymousOrgName})
|
||||
o, err := a.orgService.GetByName(ctx, &org.GetOrgByNameQuery{Name: a.cfg.Anonymous.OrgName})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -99,7 +99,7 @@ func (a *Anonymous) UsageStatFn(ctx context.Context) (map[string]any, error) {
|
||||
|
||||
// Add stats about anonymous auth
|
||||
m["stats.anonymous.customized_role.count"] = 0
|
||||
if !strings.EqualFold(a.cfg.AnonymousOrgRole, "Viewer") {
|
||||
if !strings.EqualFold(a.cfg.Anonymous.OrgRole, "Viewer") {
|
||||
m["stats.anonymous.customized_role.count"] = 1
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ func (a *Anonymous) newAnonymousIdentity(o *org.Org) *authn.Identity {
|
||||
Type: claims.TypeAnonymous,
|
||||
OrgID: o.ID,
|
||||
OrgName: o.Name,
|
||||
OrgRoles: map[int64]org.RoleType{o.ID: org.RoleType(a.cfg.AnonymousOrgRole)},
|
||||
OrgRoles: map[int64]org.RoleType{o.ID: org.RoleType(a.cfg.Anonymous.OrgRole)},
|
||||
ClientParams: authn.ClientParams{SyncPermissions: true},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,16 +30,20 @@ func TestAnonymous_Authenticate(t *testing.T) {
|
||||
desc: "should success with valid org configured",
|
||||
org: &org.Org{ID: 1, Name: "some org"},
|
||||
cfg: &setting.Cfg{
|
||||
AnonymousOrgName: "some org",
|
||||
AnonymousOrgRole: "Viewer",
|
||||
Anonymous: setting.AnonymousSettings{
|
||||
OrgRole: "Viewer",
|
||||
OrgName: "some org",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
desc: "should return error if any error occurs during org lookup",
|
||||
err: fmt.Errorf("some error"),
|
||||
cfg: &setting.Cfg{
|
||||
AnonymousOrgName: "some org",
|
||||
AnonymousOrgRole: "Viewer",
|
||||
Anonymous: setting.AnonymousSettings{
|
||||
OrgRole: "Viewer",
|
||||
OrgName: "some org",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -63,7 +67,7 @@ func TestAnonymous_Authenticate(t *testing.T) {
|
||||
assert.Equal(t, "anonymous:0", user.GetID())
|
||||
assert.Equal(t, tt.org.ID, user.OrgID)
|
||||
assert.Equal(t, tt.org.Name, user.OrgName)
|
||||
assert.Equal(t, tt.cfg.AnonymousOrgRole, string(user.GetOrgRole()))
|
||||
assert.Equal(t, tt.cfg.Anonymous.OrgRole, string(user.GetOrgRole()))
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -86,7 +90,9 @@ func TestAnonymous_ResolveIdentity(t *testing.T) {
|
||||
desc: "should return error when org id is not the configured one",
|
||||
org: &org.Org{ID: 2, Name: "some org"},
|
||||
cfg: &setting.Cfg{
|
||||
AnonymousOrgName: "some org",
|
||||
Anonymous: setting.AnonymousSettings{
|
||||
OrgName: "some org",
|
||||
},
|
||||
},
|
||||
orgID: 1,
|
||||
typ: claims.TypeAnonymous,
|
||||
@@ -97,7 +103,9 @@ func TestAnonymous_ResolveIdentity(t *testing.T) {
|
||||
desc: "should return error when namespace id does not match anonymous namespace id",
|
||||
org: &org.Org{ID: 1, Name: "some org"},
|
||||
cfg: &setting.Cfg{
|
||||
AnonymousOrgName: "some org",
|
||||
Anonymous: setting.AnonymousSettings{
|
||||
OrgName: "some org",
|
||||
},
|
||||
},
|
||||
orgID: 1,
|
||||
typ: claims.TypeAnonymous,
|
||||
@@ -108,7 +116,9 @@ func TestAnonymous_ResolveIdentity(t *testing.T) {
|
||||
desc: "should resolve identity",
|
||||
org: &org.Org{ID: 1, Name: "some org"},
|
||||
cfg: &setting.Cfg{
|
||||
AnonymousOrgName: "some org",
|
||||
Anonymous: setting.AnonymousSettings{
|
||||
OrgName: "some org",
|
||||
},
|
||||
},
|
||||
orgID: 1,
|
||||
typ: claims.TypeAnonymous,
|
||||
|
||||
@@ -45,7 +45,7 @@ func ProvideAnonymousDeviceService(usageStats usagestats.Service, authBroker aut
|
||||
a := &AnonDeviceService{
|
||||
log: log.New("anonymous-session-service"),
|
||||
localCache: localcache.New(29*time.Minute, 15*time.Minute),
|
||||
anonStore: anonstore.ProvideAnonDBStore(sqlStore, cfg.AnonymousDeviceLimit),
|
||||
anonStore: anonstore.ProvideAnonDBStore(sqlStore, cfg.Anonymous.DeviceLimit),
|
||||
serverLock: serverLockService,
|
||||
cfg: cfg,
|
||||
limitValidator: validator,
|
||||
@@ -60,7 +60,7 @@ func ProvideAnonymousDeviceService(usageStats usagestats.Service, authBroker aut
|
||||
anonDeviceService: a,
|
||||
}
|
||||
|
||||
if cfg.AnonymousEnabled {
|
||||
if cfg.Anonymous.Enabled {
|
||||
authBroker.RegisterClient(anonClient)
|
||||
authBroker.RegisterPostLoginHook(a.untagDevice, 100)
|
||||
}
|
||||
@@ -171,7 +171,7 @@ func (a *AnonDeviceService) TagDevice(ctx context.Context, httpReq *http.Request
|
||||
|
||||
// ListDevices returns all devices that have been updated between the given times.
|
||||
func (a *AnonDeviceService) ListDevices(ctx context.Context, from *time.Time, to *time.Time) ([]*anonstore.Device, error) {
|
||||
if !a.cfg.AnonymousEnabled {
|
||||
if !a.cfg.Anonymous.Enabled {
|
||||
a.log.Debug("Anonymous access is disabled, returning empty result")
|
||||
return []*anonstore.Device{}, nil
|
||||
}
|
||||
@@ -181,7 +181,7 @@ func (a *AnonDeviceService) ListDevices(ctx context.Context, from *time.Time, to
|
||||
|
||||
// CountDevices returns the number of devices that have been updated between the given times.
|
||||
func (a *AnonDeviceService) CountDevices(ctx context.Context, from time.Time, to time.Time) (int64, error) {
|
||||
if !a.cfg.AnonymousEnabled {
|
||||
if !a.cfg.Anonymous.Enabled {
|
||||
a.log.Debug("Anonymous access is disabled, returning empty result")
|
||||
return 0, nil
|
||||
}
|
||||
@@ -190,7 +190,7 @@ func (a *AnonDeviceService) CountDevices(ctx context.Context, from time.Time, to
|
||||
}
|
||||
|
||||
func (a *AnonDeviceService) SearchDevices(ctx context.Context, query *anonstore.SearchDeviceQuery) (*anonstore.SearchDeviceQueryResult, error) {
|
||||
if !a.cfg.AnonymousEnabled {
|
||||
if !a.cfg.Anonymous.Enabled {
|
||||
a.log.Debug("Anonymous access is disabled, returning empty result")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -259,7 +259,7 @@ func TestIntegrationDeviceService_SearchDevice(t *testing.T) {
|
||||
}
|
||||
store := db.InitTestDB(t)
|
||||
cfg := setting.NewCfg()
|
||||
cfg.AnonymousEnabled = true
|
||||
cfg.Anonymous.Enabled = true
|
||||
anonService := ProvideAnonymousDeviceService(&usagestats.UsageStatsMock{}, &authntest.FakeService{}, store, cfg, orgtest.NewOrgServiceFake(), nil, actest.FakeAccessControl{}, &routing.RouteRegisterImpl{}, validator.FakeAnonUserLimitValidator{})
|
||||
|
||||
for _, tc := range testCases {
|
||||
@@ -291,7 +291,7 @@ func TestIntegrationAnonDeviceService_DeviceLimitWithCache(t *testing.T) {
|
||||
// Setup test environment
|
||||
store := db.InitTestDB(t)
|
||||
cfg := setting.NewCfg()
|
||||
cfg.AnonymousDeviceLimit = 1 // Set device limit to 1 for testing
|
||||
cfg.Anonymous.DeviceLimit = 1 // Set device limit to 1 for testing
|
||||
anonService := ProvideAnonymousDeviceService(
|
||||
&usagestats.UsageStatsMock{},
|
||||
&authntest.FakeService{},
|
||||
|
||||
Reference in New Issue
Block a user