settings.go 4.0 KB

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