ldap_settings.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. OrgRole m.RoleType `toml:"org_role"`
  41. }
  42. var LdapCfg LdapConfig
  43. var ldapLogger log.Logger = log.New("ldap")
  44. func loadLdapConfig() {
  45. if !setting.LdapEnabled {
  46. return
  47. }
  48. ldapLogger.Info("Ldap enabled, reading config file", "file", setting.LdapConfigFile)
  49. _, err := toml.DecodeFile(setting.LdapConfigFile, &LdapCfg)
  50. if err != nil {
  51. ldapLogger.Crit("Failed to load ldap config file", "error", err)
  52. os.Exit(1)
  53. }
  54. if len(LdapCfg.Servers) == 0 {
  55. ldapLogger.Crit("ldap enabled but no ldap servers defined in config file")
  56. os.Exit(1)
  57. }
  58. // set default org id
  59. for _, server := range LdapCfg.Servers {
  60. assertNotEmptyCfg(server.SearchFilter, "search_filter")
  61. assertNotEmptyCfg(server.SearchBaseDNs, "search_base_dns")
  62. for _, groupMap := range server.LdapGroups {
  63. if groupMap.OrgId == 0 {
  64. groupMap.OrgId = 1
  65. }
  66. }
  67. }
  68. }
  69. func assertNotEmptyCfg(val interface{}, propName string) {
  70. switch v := val.(type) {
  71. case string:
  72. if v == "" {
  73. ldapLogger.Crit("LDAP config file is missing option", "option", propName)
  74. os.Exit(1)
  75. }
  76. case []string:
  77. if len(v) == 0 {
  78. ldapLogger.Crit("LDAP config file is missing option", "option", propName)
  79. os.Exit(1)
  80. }
  81. default:
  82. fmt.Println("unknown")
  83. }
  84. }