github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/internal/aws/credentials/chain_provider.go (about)

     1  // Copyright (C) MongoDB, Inc. 2023-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Based on github.com/aws/aws-sdk-go by Amazon.com, Inc. with code from:
     8  // - github.com/aws/aws-sdk-go/blob/v1.44.225/aws/credentials/chain_provider.go
     9  // See THIRD-PARTY-NOTICES for original license terms
    10  
    11  package credentials
    12  
    13  import (
    14  	"go.mongodb.org/mongo-driver/internal/aws/awserr"
    15  )
    16  
    17  // A ChainProvider will search for a provider which returns credentials
    18  // and cache that provider until Retrieve is called again.
    19  //
    20  // The ChainProvider provides a way of chaining multiple providers together
    21  // which will pick the first available using priority order of the Providers
    22  // in the list.
    23  //
    24  // If none of the Providers retrieve valid credentials Value, ChainProvider's
    25  // Retrieve() will return the error ErrNoValidProvidersFoundInChain.
    26  //
    27  // If a Provider is found which returns valid credentials Value ChainProvider
    28  // will cache that Provider for all calls to IsExpired(), until Retrieve is
    29  // called again.
    30  type ChainProvider struct {
    31  	Providers []Provider
    32  	curr      Provider
    33  }
    34  
    35  // NewChainCredentials returns a pointer to a new Credentials object
    36  // wrapping a chain of providers.
    37  func NewChainCredentials(providers []Provider) *Credentials {
    38  	return NewCredentials(&ChainProvider{
    39  		Providers: append([]Provider{}, providers...),
    40  	})
    41  }
    42  
    43  // Retrieve returns the credentials value or error if no provider returned
    44  // without error.
    45  //
    46  // If a provider is found it will be cached and any calls to IsExpired()
    47  // will return the expired state of the cached provider.
    48  func (c *ChainProvider) Retrieve() (Value, error) {
    49  	var errs = make([]error, 0, len(c.Providers))
    50  	for _, p := range c.Providers {
    51  		creds, err := p.Retrieve()
    52  		if err == nil {
    53  			c.curr = p
    54  			return creds, nil
    55  		}
    56  		errs = append(errs, err)
    57  	}
    58  	c.curr = nil
    59  
    60  	var err = awserr.NewBatchError("NoCredentialProviders", "no valid providers in chain", errs)
    61  	return Value{}, err
    62  }
    63  
    64  // IsExpired will returned the expired state of the currently cached provider
    65  // if there is one.  If there is no current provider, true will be returned.
    66  func (c *ChainProvider) IsExpired() bool {
    67  	if c.curr != nil {
    68  		return c.curr.IsExpired()
    69  	}
    70  
    71  	return true
    72  }