secrets_agent.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Implements an agent to read and reload the secrets.
  14. package config
  15. import (
  16. "os"
  17. "sync"
  18. "time"
  19. "github.com/sirupsen/logrus"
  20. )
  21. // SecretAgent watches a path and automatically loads the secrets stored.
  22. type SecretAgent struct {
  23. sync.Mutex
  24. secretsMap map[string][]byte
  25. }
  26. // Start will begin polling the secret file at the path. If the first load
  27. // fails, Start with return the error and abort. Future load failures will log
  28. // the failure message but continue attempting to load.
  29. func (sa *SecretAgent) Start(paths []string) error {
  30. secretsMap, err := LoadSecrets(paths)
  31. if err != nil {
  32. return err
  33. }
  34. sa.secretsMap = secretsMap
  35. // Start one goroutine for each file to monitor and update the secret's values.
  36. for secretPath := range secretsMap {
  37. go sa.reloadSecret(secretPath)
  38. }
  39. return nil
  40. }
  41. func (sa *SecretAgent) reloadSecret(secretPath string) {
  42. var lastModTime time.Time
  43. logger := logrus.NewEntry(logrus.StandardLogger())
  44. skips := 0
  45. for range time.Tick(1 * time.Second) {
  46. if skips < 600 {
  47. // Check if the file changed to see if it needs to be re-read.
  48. secretStat, err := os.Stat(secretPath)
  49. if err != nil {
  50. logger.WithField("secret-path", secretPath).
  51. WithError(err).Error("Error loading secret file.")
  52. continue
  53. }
  54. recentModTime := secretStat.ModTime()
  55. if !recentModTime.After(lastModTime) {
  56. skips++
  57. continue // file hasn't been modified
  58. }
  59. lastModTime = recentModTime
  60. }
  61. if secretValue, err := LoadSingleSecret(secretPath); err != nil {
  62. logger.WithField("secret-path: ", secretPath).
  63. WithError(err).Error("Error loading secret.")
  64. } else {
  65. sa.SetSecret(secretPath, secretValue)
  66. }
  67. }
  68. }
  69. // GetSecret returns the value of a secret stored in a map.
  70. func (sa *SecretAgent) GetSecret(secretPath string) []byte {
  71. sa.Lock()
  72. defer sa.Unlock()
  73. return sa.secretsMap[secretPath]
  74. }
  75. // Set sets the map of secrets.
  76. func (sa *SecretAgent) SetSecret(secretPath string, secretValue []byte) {
  77. sa.Lock()
  78. defer sa.Unlock()
  79. sa.secretsMap[secretPath] = secretValue
  80. }
  81. // GetTokenGenerator returns a function that gets the value of a given secret.
  82. func (sa *SecretAgent) GetTokenGenerator(secretPath string) func() []byte {
  83. return func() []byte {
  84. return sa.GetSecret(secretPath)
  85. }
  86. }