settings.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. type AttributeMap struct {
  37. Username string `toml:"username"`
  38. Name string `toml:"name"`
  39. Surname string `toml:"surname"`
  40. Email string `toml:"email"`
  41. MemberOf string `toml:"member_of"`
  42. }
  43. type GroupToOrgRole struct {
  44. GroupDN string `toml:"group_dn"`
  45. OrgId int64 `toml:"org_id"`
  46. IsGrafanaAdmin *bool `toml:"grafana_admin"` // This is a pointer to know if it was set or not (for backwards compatibility)
  47. OrgRole m.RoleType `toml:"org_role"`
  48. }
  49. var config *Config
  50. var logger = log.New("ldap")
  51. // loadingMutex locks the reading of the config so multiple requests for reloading are sequential.
  52. var loadingMutex = &sync.Mutex{}
  53. // IsEnabled checks if ldap is enabled
  54. func IsEnabled() bool {
  55. return setting.LDAPEnabled
  56. }
  57. // ReloadConfig reads the config from the disc and caches it.
  58. func ReloadConfig() error {
  59. if !IsEnabled() {
  60. return nil
  61. }
  62. loadingMutex.Lock()
  63. defer loadingMutex.Unlock()
  64. var err error
  65. config, err = readConfig(setting.LDAPConfigFile)
  66. return err
  67. }
  68. // GetConfig returns the LDAP config if LDAP is enabled otherwise it returns nil. It returns either cached value of
  69. // the config or it reads it and caches it first.
  70. func GetConfig() (*Config, error) {
  71. if !IsEnabled() {
  72. return nil, nil
  73. }
  74. // Make it a singleton
  75. if config != nil {
  76. return config, nil
  77. }
  78. loadingMutex.Lock()
  79. defer loadingMutex.Unlock()
  80. var err error
  81. config, err = readConfig(setting.LDAPConfigFile)
  82. return config, err
  83. }
  84. func readConfig(configFile string) (*Config, error) {
  85. result := &Config{}
  86. logger.Info("LDAP enabled, reading config file", "file", configFile)
  87. _, err := toml.DecodeFile(configFile, result)
  88. if err != nil {
  89. return nil, errutil.Wrap("Failed to load LDAP config file", err)
  90. }
  91. if len(result.Servers) == 0 {
  92. return nil, xerrors.New("LDAP enabled but no LDAP servers defined in config file")
  93. }
  94. // set default org id
  95. for _, server := range result.Servers {
  96. err = assertNotEmptyCfg(server.SearchFilter, "search_filter")
  97. if err != nil {
  98. return nil, errutil.Wrap("Failed to validate SearchFilter section", err)
  99. }
  100. err = assertNotEmptyCfg(server.SearchBaseDNs, "search_base_dns")
  101. if err != nil {
  102. return nil, errutil.Wrap("Failed to validate SearchBaseDNs section", err)
  103. }
  104. for _, groupMap := range server.Groups {
  105. if groupMap.OrgId == 0 {
  106. groupMap.OrgId = 1
  107. }
  108. }
  109. }
  110. return result, nil
  111. }
  112. func assertNotEmptyCfg(val interface{}, propName string) error {
  113. switch v := val.(type) {
  114. case string:
  115. if v == "" {
  116. return xerrors.Errorf("LDAP config file is missing option: %v", propName)
  117. }
  118. case []string:
  119. if len(v) == 0 {
  120. return xerrors.Errorf("LDAP config file is missing option: %v", propName)
  121. }
  122. default:
  123. fmt.Println("unknown")
  124. }
  125. return nil
  126. }