LBAC for datasources: Move validation of rules from datasources to LBAC Rules (#94622)

* FIX: Remove the checks for lbac rules inside of datasources

* Remove json validation for lbac rules

* Preserve lbac rules in updates

* Refactored test to remove the table structure

* refactor: change to allow naming and concise override instead of complex branching

* refactor to make sure we set an empty field for updates

* bugfix

* check for datasources.JsonData

* fix merge

* add datasource to check for field presence only

* add function call for readability
This commit is contained in:
Eric Leijonmarck
2024-10-25 10:07:53 +01:00
committed by GitHub
parent 226dcdde0f
commit b1e1297bb3
5 changed files with 284 additions and 244 deletions
+33 -20
View File
@@ -534,29 +534,15 @@ func (s *Service) UpdateDataSource(ctx context.Context, cmd *datasources.UpdateD
}
}
// TODO: we will eventually remove this check for moving the resource to it's separate API
if s.features != nil && s.features.IsEnabled(ctx, featuremgmt.FlagTeamHttpHeaders) && !cmd.OnlyUpdateLBACRulesFromAPI {
s.logger.Debug("Overriding LBAC rules with stored ones",
"reason", "update_lbac_rules_from_datasource_api",
"action", "use_updateLBACRules_API",
// preserve existing lbac rules when updating datasource if we're not updating lbac rules
// TODO: Refactor to store lbac rules separate from a datasource
if s.features != nil && s.features.IsEnabled(ctx, featuremgmt.FlagTeamHttpHeaders) && !cmd.AllowLBACRuleUpdates {
s.logger.Debug("Overriding LBAC rules with stored ones using updateLBACRules API",
"reason", "overriding_lbac_rules_from_datasource_api",
"datasource_id", dataSource.ID,
"datasource_uid", dataSource.UID)
if dataSource.JsonData != nil {
previousRules := dataSource.JsonData.Get("teamHttpHeaders").Interface()
if previousRules == nil {
if cmd.JsonData != nil {
cmd.JsonData.Del("teamHttpHeaders")
}
} else {
if cmd.JsonData == nil {
// It's fine to instantiate a new JsonData here
// Because it's done in the SQLStore.UpdateDataSource anyway
cmd.JsonData = simplejson.New()
}
cmd.JsonData.Set("teamHttpHeaders", previousRules)
}
}
cmd.JsonData = RetainExistingLBACRules(dataSource.JsonData, cmd.JsonData)
}
if cmd.Name != "" && cmd.Name != dataSource.Name {
@@ -1001,3 +987,30 @@ func (s *Service) CustomHeaders(ctx context.Context, ds *datasources.DataSource)
}
return s.getCustomHeaders(ds.JsonData, values), nil
}
func RetainExistingLBACRules(storedJsonData, cmdJsonData *simplejson.Json) *simplejson.Json {
// If there are no stored data, we should remove the key from the command json data
if storedJsonData == nil {
if cmdJsonData != nil {
cmdJsonData.Del("teamHttpHeaders")
}
return cmdJsonData
}
previousRules := storedJsonData.Get("teamHttpHeaders").Interface()
// If there are no previous rules, we should remove the key from the command json data
if previousRules == nil {
if cmdJsonData != nil {
cmdJsonData.Del("teamHttpHeaders")
}
return cmdJsonData
}
if cmdJsonData == nil {
// It's fine to instantiate a new JsonData here
// Because it's done in the SQLStore.UpdateDataSource anyway
cmdJsonData = simplejson.New()
}
cmdJsonData.Set("teamHttpHeaders", previousRules)
return cmdJsonData
}