github.com/drone/runner-go@v1.12.0/environ/provider/external.go (about) 1 // Copyright 2019 Drone.IO Inc. All rights reserved. 2 // Use of this source code is governed by the Polyform License 3 // that can be found in the LICENSE file. 4 5 package provider 6 7 import ( 8 "context" 9 "time" 10 11 "github.com/drone/drone-go/plugin/environ" 12 "github.com/drone/runner-go/logger" 13 ) 14 15 // MultiExternal returns a new environment provider that 16 // is comprised of multiple external providers, and 17 // aggregates their results. 18 func MultiExternal(endpoints []string, token string, insecure bool) Provider { 19 var sources []Provider 20 for _, endpoint := range endpoints { 21 sources = append(sources, External( 22 endpoint, token, insecure, 23 )) 24 } 25 return Combine(sources...) 26 } 27 28 // External returns a new external environment variable 29 // provider. This provider makes an external API call to 30 // list and return environment variables. 31 func External(endpoint, token string, insecure bool) Provider { 32 provider := &external{} 33 if endpoint != "" { 34 provider.client = environ.Client(endpoint, token, insecure) 35 } 36 return provider 37 } 38 39 type external struct { 40 client environ.Plugin 41 } 42 43 func (p *external) List(ctx context.Context, in *Request) ([]*Variable, error) { 44 if p.client == nil { 45 return nil, nil 46 } 47 48 logger := logger.FromContext(ctx) 49 50 // include a timeout to prevent an API call from 51 // hanging the build process indefinitely. The 52 // external service must return a request within 53 // one minute. 54 ctx, cancel := context.WithTimeout(ctx, time.Minute) 55 defer cancel() 56 57 req := &environ.Request{ 58 Repo: *in.Repo, 59 Build: *in.Build, 60 } 61 res, err := p.client.List(ctx, req) 62 if err != nil { 63 logger.WithError(err).Debug("environment: external: cannot get environment variable list") 64 return nil, err 65 } 66 67 // if no error is returned and the list is empty, 68 // this indicates the client returned No Content, 69 // and we should exit with no credentials, but no error. 70 if len(res) == 0 { 71 logger.Trace("environment: external: environment variable list is empty") 72 return nil, nil 73 } 74 75 logger.Trace("environment: external: environment variable list returned") 76 77 var out []*Variable 78 for _, v := range res { 79 out = append(out, &Variable{ 80 Name: v.Name, 81 Data: v.Data, 82 Mask: v.Mask, 83 }) 84 } 85 return out, nil 86 }