ldap_settings.go 2.9 KB

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