github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/creds/credhub/credhub.go (about) 1 package credhub 2 3 import ( 4 "path" 5 "time" 6 7 "github.com/pf-qiu/concourse/v6/atc/creds" 8 9 "code.cloudfoundry.org/credhub-cli/credhub" 10 "code.cloudfoundry.org/credhub-cli/credhub/credentials" 11 "code.cloudfoundry.org/lager" 12 ) 13 14 type CredHubAtc struct { 15 CredHub *LazyCredhub 16 logger lager.Logger 17 prefix string 18 } 19 20 // NewSecretLookupPaths defines how variables will be searched in the underlying secret manager 21 func (c CredHubAtc) NewSecretLookupPaths(teamName string, pipelineName string, allowRootPath bool) []creds.SecretLookupPath { 22 lookupPaths := []creds.SecretLookupPath{} 23 if len(pipelineName) > 0 { 24 lookupPaths = append(lookupPaths, creds.NewSecretLookupWithPrefix(path.Join(c.prefix, teamName, pipelineName)+"/")) 25 } 26 lookupPaths = append(lookupPaths, creds.NewSecretLookupWithPrefix(path.Join(c.prefix, teamName)+"/")) 27 if allowRootPath { 28 lookupPaths = append(lookupPaths, creds.NewSecretLookupWithPrefix(c.prefix+"/")) 29 } 30 return lookupPaths 31 } 32 33 // Get retrieves the value and expiration of an individual secret 34 func (c CredHubAtc) Get(secretPath string) (interface{}, *time.Time, bool, error) { 35 var cred credentials.Credential 36 var found bool 37 var err error 38 39 cred, found, err = c.findCred(secretPath) 40 if err != nil { 41 c.logger.Error("unable to retrieve credhub secret", err) 42 return nil, nil, false, err 43 } 44 45 if !found { 46 return nil, nil, false, nil 47 } 48 49 return cred.Value, nil, true, nil 50 } 51 52 func (c CredHubAtc) findCred(path string) (credentials.Credential, bool, error) { 53 var cred credentials.Credential 54 var err error 55 56 ch, err := c.CredHub.CredHub() 57 if err != nil { 58 return cred, false, err 59 } 60 61 _, err = ch.FindByPath(path) 62 if err != nil { 63 return cred, false, err 64 } 65 66 cred, err = ch.GetLatestVersion(path) 67 if _, ok := err.(*credhub.Error); ok { 68 return cred, false, nil 69 } 70 71 if err != nil { 72 return cred, false, err 73 } 74 75 return cred, true, nil 76 }