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