settings.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package service
  2. import (
  3. "log"
  4. "time"
  5. "gopkg.in/jcmturner/gokrb5.v7/keytab"
  6. "gopkg.in/jcmturner/gokrb5.v7/types"
  7. )
  8. // Settings defines service side configuration settings.
  9. type Settings struct {
  10. Keytab *keytab.Keytab
  11. ktprinc *types.PrincipalName
  12. sname string
  13. requireHostAddr bool
  14. disablePACDecoding bool
  15. cAddr types.HostAddress
  16. maxClockSkew time.Duration
  17. logger *log.Logger
  18. }
  19. // NewSettings creates a new service Settings.
  20. func NewSettings(kt *keytab.Keytab, settings ...func(*Settings)) *Settings {
  21. s := new(Settings)
  22. s.Keytab = kt
  23. for _, set := range settings {
  24. set(s)
  25. }
  26. return s
  27. }
  28. // RequireHostAddr used to configure service side to required host addresses to be specified in Kerberos tickets.
  29. //
  30. // s := NewSettings(kt, RequireHostAddr(true))
  31. func RequireHostAddr(b bool) func(*Settings) {
  32. return func(s *Settings) {
  33. s.requireHostAddr = b
  34. }
  35. }
  36. // RequireHostAddr indicates if the service should require the host address to be included in the ticket.
  37. func (s *Settings) RequireHostAddr() bool {
  38. return s.requireHostAddr
  39. }
  40. // DecodePAC used to configure service side to enable/disable PAC decoding if the PAC is present.
  41. // Defaults to enabled if not specified.
  42. //
  43. // s := NewSettings(kt, DecodePAC(false))
  44. func DecodePAC(b bool) func(*Settings) {
  45. return func(s *Settings) {
  46. s.disablePACDecoding = !b
  47. }
  48. }
  49. // DecodePAC indicates whether the service should decode any PAC information present in the ticket.
  50. func (s *Settings) DecodePAC() bool {
  51. return !s.disablePACDecoding
  52. }
  53. // ClientAddress used to configure service side with the clients host address to be used during validation.
  54. //
  55. // s := NewSettings(kt, ClientAddress(h))
  56. func ClientAddress(h types.HostAddress) func(*Settings) {
  57. return func(s *Settings) {
  58. s.cAddr = h
  59. }
  60. }
  61. // ClientAddress returns the client host address which has been provided to the service.
  62. func (s *Settings) ClientAddress() types.HostAddress {
  63. return s.cAddr
  64. }
  65. // Logger used to configure service side with a logger.
  66. //
  67. // s := NewSettings(kt, Logger(l))
  68. func Logger(l *log.Logger) func(*Settings) {
  69. return func(s *Settings) {
  70. s.logger = l
  71. }
  72. }
  73. // Logger returns the logger instances configured for the service. If none is configured nill will be returned.
  74. func (s *Settings) Logger() *log.Logger {
  75. return s.logger
  76. }
  77. // KeytabPrincipal used to override the principal name used to find the key in the keytab.
  78. //
  79. // s := NewSettings(kt, KeytabPrincipal("someaccount"))
  80. func KeytabPrincipal(p string) func(*Settings) {
  81. return func(s *Settings) {
  82. pn, _ := types.ParseSPNString(p)
  83. s.ktprinc = &pn
  84. }
  85. }
  86. // KeytabPrincipal returns the principal name used to find the key in the keytab if it has been overridden.
  87. func (s *Settings) KeytabPrincipal() *types.PrincipalName {
  88. return s.ktprinc
  89. }
  90. // MaxClockSkew used to configure service side with the maximum acceptable clock skew
  91. // between the service and the issue time of kerberos tickets
  92. //
  93. // s := NewSettings(kt, MaxClockSkew(d))
  94. func MaxClockSkew(d time.Duration) func(*Settings) {
  95. return func(s *Settings) {
  96. s.maxClockSkew = d
  97. }
  98. }
  99. // MaxClockSkew returns the maximum acceptable clock skew between the service and the issue time of kerberos tickets.
  100. // If none is defined a duration of 5 minutes is returned.
  101. func (s *Settings) MaxClockSkew() time.Duration {
  102. if s.maxClockSkew.Nanoseconds() == 0 {
  103. return time.Duration(5) * time.Minute
  104. }
  105. return s.maxClockSkew
  106. }
  107. // SName used provide a specific service name to the service settings.
  108. //
  109. // s := NewSettings(kt, SName("HTTP/some.service.com"))
  110. func SName(sname string) func(*Settings) {
  111. return func(s *Settings) {
  112. s.sname = sname
  113. }
  114. }
  115. // SName returns the specific service name to the service.
  116. func (s *Settings) SName() string {
  117. return s.sname
  118. }