2019-04-26 07:47:16 -05:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-11-06 14:41:21 -06:00
|
|
|
"io/ioutil"
|
2019-05-03 07:53:07 -05:00
|
|
|
"sync"
|
2019-04-26 07:47:16 -05:00
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2020-02-29 06:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2019-04-26 07:47:16 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
2022-07-08 01:52:54 -05:00
|
|
|
const defaultTimeout = 10
|
|
|
|
|
2019-05-17 06:57:26 -05:00
|
|
|
// Config holds list of connections to LDAP
|
2019-04-26 07:47:16 -05:00
|
|
|
type Config struct {
|
|
|
|
Servers []*ServerConfig `toml:"servers"`
|
|
|
|
}
|
|
|
|
|
2019-05-17 06:57:26 -05:00
|
|
|
// ServerConfig holds connection data to LDAP
|
2019-04-26 07:47:16 -05:00
|
|
|
type ServerConfig struct {
|
|
|
|
Host string `toml:"host"`
|
|
|
|
Port int `toml:"port"`
|
|
|
|
UseSSL bool `toml:"use_ssl"`
|
|
|
|
StartTLS bool `toml:"start_tls"`
|
|
|
|
SkipVerifySSL bool `toml:"ssl_skip_verify"`
|
|
|
|
RootCACert string `toml:"root_ca_cert"`
|
|
|
|
ClientCert string `toml:"client_cert"`
|
|
|
|
ClientKey string `toml:"client_key"`
|
|
|
|
BindDN string `toml:"bind_dn"`
|
|
|
|
BindPassword string `toml:"bind_password"`
|
2022-07-08 01:52:54 -05:00
|
|
|
Timeout int `toml:"timeout"`
|
2019-04-26 07:47:16 -05:00
|
|
|
Attr AttributeMap `toml:"attributes"`
|
|
|
|
|
|
|
|
SearchFilter string `toml:"search_filter"`
|
|
|
|
SearchBaseDNs []string `toml:"search_base_dns"`
|
|
|
|
|
|
|
|
GroupSearchFilter string `toml:"group_search_filter"`
|
|
|
|
GroupSearchFilterUserAttribute string `toml:"group_search_filter_user_attribute"`
|
|
|
|
GroupSearchBaseDNs []string `toml:"group_search_base_dns"`
|
|
|
|
|
|
|
|
Groups []*GroupToOrgRole `toml:"group_mappings"`
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:47:52 -05:00
|
|
|
// AttributeMap is a struct representation for LDAP "attributes" setting
|
2019-04-26 07:47:16 -05:00
|
|
|
type AttributeMap struct {
|
|
|
|
Username string `toml:"username"`
|
|
|
|
Name string `toml:"name"`
|
|
|
|
Surname string `toml:"surname"`
|
|
|
|
Email string `toml:"email"`
|
|
|
|
MemberOf string `toml:"member_of"`
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:47:52 -05:00
|
|
|
// GroupToOrgRole is a struct representation of LDAP
|
|
|
|
// config "group_mappings" setting
|
2019-04-26 07:47:16 -05:00
|
|
|
type GroupToOrgRole struct {
|
2019-06-13 09:47:52 -05:00
|
|
|
GroupDN string `toml:"group_dn"`
|
2019-09-19 10:13:38 -05:00
|
|
|
OrgId int64 `toml:"org_id"`
|
2019-06-13 09:47:52 -05:00
|
|
|
|
|
|
|
// This pointer specifies if setting was set (for backwards compatibility)
|
|
|
|
IsGrafanaAdmin *bool `toml:"grafana_admin"`
|
|
|
|
|
2020-02-29 06:35:15 -06:00
|
|
|
OrgRole models.RoleType `toml:"org_role"`
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
|
|
|
|
2019-06-13 09:47:52 -05:00
|
|
|
// logger for all LDAP stuff
|
2019-04-26 07:47:16 -05:00
|
|
|
var logger = log.New("ldap")
|
|
|
|
|
2019-05-03 07:53:07 -05:00
|
|
|
// loadingMutex locks the reading of the config so multiple requests for reloading are sequential.
|
|
|
|
var loadingMutex = &sync.Mutex{}
|
|
|
|
|
2019-04-26 07:47:16 -05:00
|
|
|
// IsEnabled checks if ldap is enabled
|
|
|
|
func IsEnabled() bool {
|
2019-05-22 07:30:03 -05:00
|
|
|
return setting.LDAPEnabled
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
|
|
|
|
2021-01-19 11:57:09 -06:00
|
|
|
// ReloadConfig reads the config from the disk and caches it.
|
2019-05-03 07:53:07 -05:00
|
|
|
func ReloadConfig() error {
|
2019-05-14 02:18:28 -05:00
|
|
|
if !IsEnabled() {
|
2019-04-26 07:47:16 -05:00
|
|
|
return nil
|
|
|
|
}
|
2019-05-14 02:18:28 -05:00
|
|
|
|
2019-05-03 07:53:07 -05:00
|
|
|
loadingMutex.Lock()
|
|
|
|
defer loadingMutex.Unlock()
|
|
|
|
|
|
|
|
var err error
|
2019-05-22 07:30:03 -05:00
|
|
|
config, err = readConfig(setting.LDAPConfigFile)
|
2019-05-03 07:53:07 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-13 09:47:52 -05:00
|
|
|
// We need to define in this space so `GetConfig` fn
|
|
|
|
// could be defined as singleton
|
|
|
|
var config *Config
|
|
|
|
|
2019-05-03 07:53:07 -05:00
|
|
|
// GetConfig returns the LDAP config if LDAP is enabled otherwise it returns nil. It returns either cached value of
|
|
|
|
// the config or it reads it and caches it first.
|
2020-12-11 04:44:44 -06:00
|
|
|
func GetConfig(cfg *setting.Cfg) (*Config, error) {
|
|
|
|
if cfg != nil {
|
|
|
|
if !cfg.LDAPEnabled {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
} else if !IsEnabled() {
|
2019-05-03 07:53:07 -05:00
|
|
|
return nil, nil
|
|
|
|
}
|
2019-04-26 07:47:16 -05:00
|
|
|
|
|
|
|
// Make it a singleton
|
|
|
|
if config != nil {
|
2019-05-03 07:53:07 -05:00
|
|
|
return config, nil
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
|
|
|
|
2019-05-03 07:53:07 -05:00
|
|
|
loadingMutex.Lock()
|
|
|
|
defer loadingMutex.Unlock()
|
|
|
|
|
2020-12-02 09:57:16 -06:00
|
|
|
return readConfig(setting.LDAPConfigFile)
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
2019-05-03 07:53:07 -05:00
|
|
|
|
|
|
|
func readConfig(configFile string) (*Config, error) {
|
2019-04-26 07:47:16 -05:00
|
|
|
result := &Config{}
|
|
|
|
|
2019-05-22 07:30:03 -05:00
|
|
|
logger.Info("LDAP enabled, reading config file", "file", configFile)
|
2019-04-26 07:47:16 -05:00
|
|
|
|
2020-12-03 15:13:06 -06:00
|
|
|
// nolint:gosec
|
|
|
|
// We can ignore the gosec G304 warning on this one because `filename` comes from grafana configuration file
|
2019-11-06 14:41:21 -06:00
|
|
|
fileBytes, err := ioutil.ReadFile(configFile)
|
|
|
|
if err != nil {
|
2022-06-03 02:24:24 -05:00
|
|
|
return nil, fmt.Errorf("%v: %w", "Failed to load LDAP config file", err)
|
2019-11-06 14:41:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// interpolate full toml string (it can contain ENV variables)
|
2020-06-10 07:58:42 -05:00
|
|
|
stringContent, err := setting.ExpandVar(string(fileBytes))
|
|
|
|
if err != nil {
|
2022-06-03 02:24:24 -05:00
|
|
|
return nil, fmt.Errorf("%v: %w", "Failed to expand variables", err)
|
2020-06-10 07:58:42 -05:00
|
|
|
}
|
2019-11-06 14:41:21 -06:00
|
|
|
|
|
|
|
_, err = toml.Decode(stringContent, result)
|
2019-04-26 07:47:16 -05:00
|
|
|
if err != nil {
|
2022-06-03 02:24:24 -05:00
|
|
|
return nil, fmt.Errorf("%v: %w", "Failed to load LDAP config file", err)
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(result.Servers) == 0 {
|
2020-07-31 02:45:20 -05:00
|
|
|
return nil, fmt.Errorf("LDAP enabled but no LDAP servers defined in config file")
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, server := range result.Servers {
|
2022-07-08 01:52:54 -05:00
|
|
|
// set default org id
|
2019-05-03 07:53:07 -05:00
|
|
|
err = assertNotEmptyCfg(server.SearchFilter, "search_filter")
|
|
|
|
if err != nil {
|
2022-06-03 02:24:24 -05:00
|
|
|
return nil, fmt.Errorf("%v: %w", "Failed to validate SearchFilter section", err)
|
2019-05-03 07:53:07 -05:00
|
|
|
}
|
|
|
|
err = assertNotEmptyCfg(server.SearchBaseDNs, "search_base_dns")
|
|
|
|
if err != nil {
|
2022-06-03 02:24:24 -05:00
|
|
|
return nil, fmt.Errorf("%v: %w", "Failed to validate SearchBaseDNs section", err)
|
2019-05-03 07:53:07 -05:00
|
|
|
}
|
2019-04-26 07:47:16 -05:00
|
|
|
|
|
|
|
for _, groupMap := range server.Groups {
|
2022-05-04 02:35:10 -05:00
|
|
|
if groupMap.OrgRole == "" && groupMap.IsGrafanaAdmin == nil {
|
|
|
|
return nil, fmt.Errorf("LDAP group mapping: organization role or grafana admin status is required")
|
|
|
|
}
|
|
|
|
|
2019-09-19 10:13:38 -05:00
|
|
|
if groupMap.OrgId == 0 {
|
|
|
|
groupMap.OrgId = 1
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
|
|
|
}
|
2022-07-08 01:52:54 -05:00
|
|
|
|
|
|
|
// set default timeout if unspecified
|
|
|
|
if server.Timeout == 0 {
|
|
|
|
server.Timeout = defaultTimeout
|
|
|
|
}
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
|
|
|
|
2019-05-03 07:53:07 -05:00
|
|
|
return result, nil
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
|
|
|
|
2019-05-03 07:53:07 -05:00
|
|
|
func assertNotEmptyCfg(val interface{}, propName string) error {
|
2019-04-26 07:47:16 -05:00
|
|
|
switch v := val.(type) {
|
|
|
|
case string:
|
|
|
|
if v == "" {
|
2020-07-31 02:45:20 -05:00
|
|
|
return fmt.Errorf("LDAP config file is missing option: %q", propName)
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
|
|
|
case []string:
|
|
|
|
if len(v) == 0 {
|
2020-07-31 02:45:20 -05:00
|
|
|
return fmt.Errorf("LDAP config file is missing option: %q", propName)
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
fmt.Println("unknown")
|
|
|
|
}
|
2019-05-03 07:53:07 -05:00
|
|
|
return nil
|
2019-04-26 07:47:16 -05:00
|
|
|
}
|