github.com/drone/runner-go@v1.12.0/registry/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 registry
     6  
     7  import (
     8  	"context"
     9  	"time"
    10  
    11  	"github.com/drone/drone-go/drone"
    12  	"github.com/drone/drone-go/plugin/registry"
    13  	"github.com/drone/runner-go/logger"
    14  )
    15  
    16  // External returns a new external registry credentials
    17  // provider. The external credentials provider makes an
    18  // external API call to list and return credentials.
    19  func External(endpoint, token string, insecure bool) Provider {
    20  	provider := &external{}
    21  	if endpoint != "" {
    22  		provider.client = registry.Client(endpoint, token, insecure)
    23  	}
    24  	return provider
    25  }
    26  
    27  type external struct {
    28  	client registry.Plugin
    29  }
    30  
    31  func (p *external) List(ctx context.Context, in *Request) ([]*drone.Registry, error) {
    32  	if p.client == nil {
    33  		return nil, nil
    34  	}
    35  
    36  	logger := logger.FromContext(ctx)
    37  
    38  	// include a timeout to prevent an API call from
    39  	// hanging the build process indefinitely. The
    40  	// external service must return a request within
    41  	// one minute.
    42  	ctx, cancel := context.WithTimeout(ctx, time.Minute)
    43  	defer cancel()
    44  
    45  	req := &registry.Request{
    46  		Repo:  *in.Repo,
    47  		Build: *in.Build,
    48  	}
    49  	res, err := p.client.List(ctx, req)
    50  	if err != nil {
    51  		logger.WithError(err).Debug("registry: external: cannot get credentials")
    52  		return nil, err
    53  	}
    54  
    55  	// if no error is returned and the list is empty,
    56  	// this indicates the client returned No Content,
    57  	// and we should exit with no credentials, but no error.
    58  	if len(res) == 0 {
    59  		logger.Trace("registry: external: credential list is empty")
    60  		return nil, nil
    61  	}
    62  
    63  	for _, v := range res {
    64  		logger.
    65  			WithField("address", v.Address).
    66  			WithField("username", v.Username).
    67  			Trace("registry: external: received credentials")
    68  	}
    69  
    70  	return res, nil
    71  }