github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/internal/credproviders/static_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  package credproviders
     8  
     9  import (
    10  	"errors"
    11  
    12  	"go.mongodb.org/mongo-driver/internal/aws/credentials"
    13  )
    14  
    15  // staticProviderName provides a name of Static provider
    16  const staticProviderName = "StaticProvider"
    17  
    18  // A StaticProvider is a set of credentials which are set programmatically,
    19  // and will never expire.
    20  type StaticProvider struct {
    21  	credentials.Value
    22  
    23  	verified bool
    24  	err      error
    25  }
    26  
    27  func verify(v credentials.Value) error {
    28  	if !v.HasKeys() {
    29  		return errors.New("failed to retrieve ACCESS_KEY_ID and SECRET_ACCESS_KEY")
    30  	}
    31  	if v.AccessKeyID != "" && v.SecretAccessKey == "" {
    32  		return errors.New("ACCESS_KEY_ID is set, but SECRET_ACCESS_KEY is missing")
    33  	}
    34  	if v.AccessKeyID == "" && v.SecretAccessKey != "" {
    35  		return errors.New("SECRET_ACCESS_KEY is set, but ACCESS_KEY_ID is missing")
    36  	}
    37  	if v.AccessKeyID == "" && v.SecretAccessKey == "" && v.SessionToken != "" {
    38  		return errors.New("AWS_SESSION_TOKEN is set, but ACCESS_KEY_ID and SECRET_ACCESS_KEY are missing")
    39  	}
    40  	return nil
    41  
    42  }
    43  
    44  // Retrieve returns the credentials or error if the credentials are invalid.
    45  func (s *StaticProvider) Retrieve() (credentials.Value, error) {
    46  	if !s.verified {
    47  		s.err = verify(s.Value)
    48  		s.Value.ProviderName = staticProviderName
    49  		s.verified = true
    50  	}
    51  	return s.Value, s.err
    52  }
    53  
    54  // IsExpired returns if the credentials are expired.
    55  //
    56  // For StaticProvider, the credentials never expired.
    57  func (s *StaticProvider) IsExpired() bool {
    58  	return false
    59  }