ldap_settings.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package login
  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 LdapConfig struct {
  11. Servers []*LdapServerConf `toml:"servers"`
  12. }
  13. type LdapServerConf 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. BindDN string `toml:"bind_dn"`
  21. BindPassword string `toml:"bind_password"`
  22. Attr LdapAttributeMap `toml:"attributes"`
  23. SearchFilter string `toml:"search_filter"`
  24. SearchBaseDNs []string `toml:"search_base_dns"`
  25. GroupSearchFilter string `toml:"group_search_filter"`
  26. GroupSearchFilterUserAttribute string `toml:"group_search_filter_user_attribute"`
  27. GroupSearchBaseDNs []string `toml:"group_search_base_dns"`
  28. LdapGroups []*LdapGroupToOrgRole `toml:"group_mappings"`
  29. }
  30. type LdapAttributeMap struct {
  31. Username string `toml:"username"`
  32. Name string `toml:"name"`
  33. Surname string `toml:"surname"`
  34. Email string `toml:"email"`
  35. MemberOf string `toml:"member_of"`
  36. }
  37. type LdapGroupToOrgRole struct {
  38. GroupDN string `toml:"group_dn"`
  39. OrgId int64 `toml:"org_id"`
  40. IsGrafanaAdmin *bool `toml:"grafana_admin"` // This is a pointer to know if it was set or not (for backwards compatability)
  41. OrgRole m.RoleType `toml:"org_role"`
  42. }
  43. var LdapCfg LdapConfig
  44. var ldapLogger log.Logger = log.New("ldap")
  45. func loadLdapConfig() {
  46. if !setting.LdapEnabled {
  47. return
  48. }
  49. ldapLogger.Info("Ldap enabled, reading config file", "file", setting.LdapConfigFile)
  50. _, err := toml.DecodeFile(setting.LdapConfigFile, &LdapCfg)
  51. if err != nil {
  52. ldapLogger.Crit("Failed to load ldap config file", "error", err)
  53. os.Exit(1)
  54. }
  55. if len(LdapCfg.Servers) == 0 {
  56. ldapLogger.Crit("ldap enabled but no ldap servers defined in config file")
  57. os.Exit(1)
  58. }
  59. // set default org id
  60. for _, server := range LdapCfg.Servers {
  61. assertNotEmptyCfg(server.SearchFilter, "search_filter")
  62. assertNotEmptyCfg(server.SearchBaseDNs, "search_base_dns")
  63. for _, groupMap := range server.LdapGroups {
  64. if groupMap.OrgId == 0 {
  65. groupMap.OrgId = 1
  66. }
  67. }
  68. }
  69. }
  70. func assertNotEmptyCfg(val interface{}, propName string) {
  71. switch v := val.(type) {
  72. case string:
  73. if v == "" {
  74. ldapLogger.Crit("LDAP config file is missing option", "option", propName)
  75. os.Exit(1)
  76. }
  77. case []string:
  78. if len(v) == 0 {
  79. ldapLogger.Crit("LDAP config file is missing option", "option", propName)
  80. os.Exit(1)
  81. }
  82. default:
  83. fmt.Println("unknown")
  84. }
  85. }