settings.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package ldap
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/BurntSushi/toml"
  6. "github.com/grafana/grafana/pkg/log"
  7. m "github.com/grafana/grafana/pkg/models"
  8. "github.com/grafana/grafana/pkg/setting"
  9. )
  10. type Config struct {
  11. Servers []*ServerConfig `toml:"servers"`
  12. }
  13. type ServerConfig struct {
  14. Host string `toml:"host"`
  15. Port int `toml:"port"`
  16. UseSSL bool `toml:"use_ssl"`
  17. StartTLS bool `toml:"start_tls"`
  18. SkipVerifySSL bool `toml:"ssl_skip_verify"`
  19. RootCACert string `toml:"root_ca_cert"`
  20. ClientCert string `toml:"client_cert"`
  21. ClientKey string `toml:"client_key"`
  22. BindDN string `toml:"bind_dn"`
  23. BindPassword string `toml:"bind_password"`
  24. Attr AttributeMap `toml:"attributes"`
  25. SearchFilter string `toml:"search_filter"`
  26. SearchBaseDNs []string `toml:"search_base_dns"`
  27. GroupSearchFilter string `toml:"group_search_filter"`
  28. GroupSearchFilterUserAttribute string `toml:"group_search_filter_user_attribute"`
  29. GroupSearchBaseDNs []string `toml:"group_search_base_dns"`
  30. Groups []*GroupToOrgRole `toml:"group_mappings"`
  31. }
  32. type AttributeMap struct {
  33. Username string `toml:"username"`
  34. Name string `toml:"name"`
  35. Surname string `toml:"surname"`
  36. Email string `toml:"email"`
  37. MemberOf string `toml:"member_of"`
  38. }
  39. type GroupToOrgRole struct {
  40. GroupDN string `toml:"group_dn"`
  41. OrgId int64 `toml:"org_id"`
  42. IsGrafanaAdmin *bool `toml:"grafana_admin"` // This is a pointer to know if it was set or not (for backwards compatibility)
  43. OrgRole m.RoleType `toml:"org_role"`
  44. }
  45. var config *Config
  46. var logger = log.New("ldap")
  47. // IsEnabled checks if ldap is enabled
  48. func IsEnabled() bool {
  49. return setting.LdapEnabled
  50. }
  51. // ReadConfig reads the config if
  52. // ldap is enabled otherwise it will return nil
  53. func ReadConfig() *Config {
  54. if IsEnabled() == false {
  55. return nil
  56. }
  57. // Make it a singleton
  58. if config != nil {
  59. return config
  60. }
  61. config = getConfig(setting.LdapConfigFile)
  62. return config
  63. }
  64. func getConfig(configFile string) *Config {
  65. result := &Config{}
  66. logger.Info("Ldap enabled, reading config file", "file", configFile)
  67. _, err := toml.DecodeFile(configFile, result)
  68. if err != nil {
  69. logger.Crit("Failed to load ldap config file", "error", err)
  70. os.Exit(1)
  71. }
  72. if len(result.Servers) == 0 {
  73. logger.Crit("ldap enabled but no ldap servers defined in config file")
  74. os.Exit(1)
  75. }
  76. // set default org id
  77. for _, server := range result.Servers {
  78. assertNotEmptyCfg(server.SearchFilter, "search_filter")
  79. assertNotEmptyCfg(server.SearchBaseDNs, "search_base_dns")
  80. for _, groupMap := range server.Groups {
  81. if groupMap.OrgId == 0 {
  82. groupMap.OrgId = 1
  83. }
  84. }
  85. }
  86. return result
  87. }
  88. func assertNotEmptyCfg(val interface{}, propName string) {
  89. switch v := val.(type) {
  90. case string:
  91. if v == "" {
  92. logger.Crit("LDAP config file is missing option", "option", propName)
  93. os.Exit(1)
  94. }
  95. case []string:
  96. if len(v) == 0 {
  97. logger.Crit("LDAP config file is missing option", "option", propName)
  98. os.Exit(1)
  99. }
  100. default:
  101. fmt.Println("unknown")
  102. }
  103. }