mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
LDAP: Remove duplicate code (#90066)
remove duplicate readConfig() function from LDAP
This commit is contained in:
parent
8bc8b4b31f
commit
dfabc878f3
@ -3,91 +3,11 @@ package service
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/cmd/grafana-cli/logger"
|
|
||||||
"github.com/grafana/grafana/pkg/services/ldap"
|
"github.com/grafana/grafana/pkg/services/ldap"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultTimeout = 10
|
|
||||||
|
|
||||||
func readConfig(configFile string) (*ldap.ServersConfig, error) {
|
|
||||||
result := &ldap.ServersConfig{}
|
|
||||||
|
|
||||||
logger.Info("LDAP enabled, reading config file", "file", configFile)
|
|
||||||
|
|
||||||
// nolint:gosec
|
|
||||||
// We can ignore the gosec G304 warning on this one because `filename` comes from grafana configuration file
|
|
||||||
fileBytes, err := os.ReadFile(configFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v: %w", "Failed to load LDAP config file", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// interpolate full toml string (it can contain ENV variables)
|
|
||||||
stringContent, err := setting.ExpandVar(string(fileBytes))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v: %w", "Failed to expand variables", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = toml.Decode(stringContent, result)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v: %w", "Failed to load LDAP config file", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(result.Servers) == 0 {
|
|
||||||
return nil, fmt.Errorf("LDAP enabled but no LDAP servers defined in config file")
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, server := range result.Servers {
|
|
||||||
// set default org id
|
|
||||||
err = assertNotEmptyCfg(server.SearchFilter, "search_filter")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v: %w", "Failed to validate SearchFilter section", err)
|
|
||||||
}
|
|
||||||
err = assertNotEmptyCfg(server.SearchBaseDNs, "search_base_dns")
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("%v: %w", "Failed to validate SearchBaseDNs section", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, groupMap := range server.Groups {
|
|
||||||
if groupMap.OrgRole == "" && groupMap.IsGrafanaAdmin == nil {
|
|
||||||
return nil, fmt.Errorf("LDAP group mapping: organization role or grafana admin status is required")
|
|
||||||
}
|
|
||||||
|
|
||||||
if groupMap.OrgId == 0 {
|
|
||||||
groupMap.OrgId = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// set default timeout if unspecified
|
|
||||||
if server.Timeout == 0 {
|
|
||||||
server.Timeout = defaultTimeout
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func assertNotEmptyCfg(val any, propName string) error {
|
|
||||||
switch v := val.(type) {
|
|
||||||
case string:
|
|
||||||
if v == "" {
|
|
||||||
return fmt.Errorf("LDAP config file is missing option: %q", propName)
|
|
||||||
}
|
|
||||||
case []string:
|
|
||||||
if len(v) == 0 {
|
|
||||||
return fmt.Errorf("LDAP config file is missing option: %q", propName)
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
fmt.Println("unknown")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func resolveBool(input any, defaultValue bool) bool {
|
func resolveBool(input any, defaultValue bool) bool {
|
||||||
strInput := fmt.Sprintf("%v", input)
|
strInput := fmt.Sprintf("%v", input)
|
||||||
result, err := strconv.ParseBool(strInput)
|
result, err := strconv.ParseBool(strInput)
|
||||||
|
@ -170,7 +170,7 @@ func (s *LDAPImpl) ReloadConfig() error {
|
|||||||
s.loadingMutex.Lock()
|
s.loadingMutex.Lock()
|
||||||
defer s.loadingMutex.Unlock()
|
defer s.loadingMutex.Unlock()
|
||||||
|
|
||||||
config, err := readConfig(s.cfg.ConfigFilePath)
|
config, err := ldap.GetConfig(s.cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user