settings.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package ldap
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/BurntSushi/toml"
  6. "golang.org/x/xerrors"
  7. "github.com/grafana/grafana/pkg/infra/log"
  8. m "github.com/grafana/grafana/pkg/models"
  9. "github.com/grafana/grafana/pkg/setting"
  10. "github.com/grafana/grafana/pkg/util/errutil"
  11. )
  12. // Config holds list of connections to LDAP
  13. type Config struct {
  14. Servers []*ServerConfig `toml:"servers"`
  15. }
  16. // ServerConfig holds connection data to LDAP
  17. type ServerConfig struct {
  18. Host string `toml:"host"`
  19. Port int `toml:"port"`
  20. UseSSL bool `toml:"use_ssl"`
  21. StartTLS bool `toml:"start_tls"`
  22. SkipVerifySSL bool `toml:"ssl_skip_verify"`
  23. RootCACert string `toml:"root_ca_cert"`
  24. ClientCert string `toml:"client_cert"`
  25. ClientKey string `toml:"client_key"`
  26. BindDN string `toml:"bind_dn"`
  27. BindPassword string `toml:"bind_password"`
  28. Attr AttributeMap `toml:"attributes"`
  29. SearchFilter string `toml:"search_filter"`
  30. SearchBaseDNs []string `toml:"search_base_dns"`
  31. GroupSearchFilter string `toml:"group_search_filter"`
  32. GroupSearchFilterUserAttribute string `toml:"group_search_filter_user_attribute"`
  33. GroupSearchBaseDNs []string `toml:"group_search_base_dns"`
  34. Groups []*GroupToOrgRole `toml:"group_mappings"`
  35. }
  36. // AttributeMap is a struct representation for LDAP "attributes" setting
  37. type AttributeMap struct {
  38. Username string `toml:"username"`
  39. Name string `toml:"name"`
  40. Surname string `toml:"surname"`
  41. Email string `toml:"email"`
  42. MemberOf string `toml:"member_of"`
  43. }
  44. // GroupToOrgRole is a struct representation of LDAP
  45. // config "group_mappings" setting
  46. type GroupToOrgRole struct {
  47. GroupDN string `toml:"group_dn"`
  48. OrgId int64 `toml:"org_id"`
  49. // This pointer specifies if setting was set (for backwards compatibility)
  50. IsGrafanaAdmin *bool `toml:"grafana_admin"`
  51. OrgRole m.RoleType `toml:"org_role"`
  52. }
  53. // logger for all LDAP stuff
  54. var logger = log.New("ldap")
  55. // loadingMutex locks the reading of the config so multiple requests for reloading are sequential.
  56. var loadingMutex = &sync.Mutex{}
  57. // IsEnabled checks if ldap is enabled
  58. func IsEnabled() bool {
  59. return setting.LDAPEnabled
  60. }
  61. // ReloadConfig reads the config from the disc and caches it.
  62. func ReloadConfig() error {
  63. if !IsEnabled() {
  64. return nil
  65. }
  66. loadingMutex.Lock()
  67. defer loadingMutex.Unlock()
  68. var err error
  69. config, err = readConfig(setting.LDAPConfigFile)
  70. return err
  71. }
  72. // We need to define in this space so `GetConfig` fn
  73. // could be defined as singleton
  74. var config *Config
  75. // GetConfig returns the LDAP config if LDAP is enabled otherwise it returns nil. It returns either cached value of
  76. // the config or it reads it and caches it first.
  77. func GetConfig() (*Config, error) {
  78. if !IsEnabled() {
  79. return nil, nil
  80. }
  81. // Make it a singleton
  82. if config != nil {
  83. return config, nil
  84. }
  85. loadingMutex.Lock()
  86. defer loadingMutex.Unlock()
  87. var err error
  88. config, err = readConfig(setting.LDAPConfigFile)
  89. return config, err
  90. }
  91. func readConfig(configFile string) (*Config, error) {
  92. result := &Config{}
  93. logger.Info("LDAP enabled, reading config file", "file", configFile)
  94. _, err := toml.DecodeFile(configFile, result)
  95. if err != nil {
  96. return nil, errutil.Wrap("Failed to load LDAP config file", err)
  97. }
  98. if len(result.Servers) == 0 {
  99. return nil, xerrors.New("LDAP enabled but no LDAP servers defined in config file")
  100. }
  101. // set default org id
  102. for _, server := range result.Servers {
  103. err = assertNotEmptyCfg(server.SearchFilter, "search_filter")
  104. if err != nil {
  105. return nil, errutil.Wrap("Failed to validate SearchFilter section", err)
  106. }
  107. err = assertNotEmptyCfg(server.SearchBaseDNs, "search_base_dns")
  108. if err != nil {
  109. return nil, errutil.Wrap("Failed to validate SearchBaseDNs section", err)
  110. }
  111. for _, groupMap := range server.Groups {
  112. if groupMap.OrgId == 0 {
  113. groupMap.OrgId = 1
  114. }
  115. }
  116. }
  117. return result, nil
  118. }
  119. func assertNotEmptyCfg(val interface{}, propName string) error {
  120. switch v := val.(type) {
  121. case string:
  122. if v == "" {
  123. return xerrors.Errorf("LDAP config file is missing option: %v", propName)
  124. }
  125. case []string:
  126. if len(v) == 0 {
  127. return xerrors.Errorf("LDAP config file is missing option: %v", propName)
  128. }
  129. default:
  130. fmt.Println("unknown")
  131. }
  132. return nil
  133. }