src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

resolvers.go (3403B)


      1 package config
      2 
      3 import (
      4 	"fmt"
      5 	"strings"
      6 )
      7 
      8 // ClientEnableState provides an enumeration if the client is enabled,
      9 // disabled, or default behavior.
     10 type ClientEnableState uint
     11 
     12 // Enumeration values for ClientEnableState
     13 const (
     14 	ClientDefaultEnableState ClientEnableState = iota
     15 	ClientDisabled
     16 	ClientEnabled
     17 )
     18 
     19 // EndpointModeState is the EC2 IMDS Endpoint Configuration Mode
     20 type EndpointModeState uint
     21 
     22 // Enumeration values for ClientEnableState
     23 const (
     24 	EndpointModeStateUnset EndpointModeState = iota
     25 	EndpointModeStateIPv4
     26 	EndpointModeStateIPv6
     27 )
     28 
     29 // SetFromString sets the EndpointModeState based on the provided string value. Unknown values will default to EndpointModeStateUnset
     30 func (e *EndpointModeState) SetFromString(v string) error {
     31 	v = strings.TrimSpace(v)
     32 
     33 	switch {
     34 	case len(v) == 0:
     35 		*e = EndpointModeStateUnset
     36 	case strings.EqualFold(v, "IPv6"):
     37 		*e = EndpointModeStateIPv6
     38 	case strings.EqualFold(v, "IPv4"):
     39 		*e = EndpointModeStateIPv4
     40 	default:
     41 		return fmt.Errorf("unknown EC2 IMDS endpoint mode, must be either IPv6 or IPv4")
     42 	}
     43 	return nil
     44 }
     45 
     46 // ClientEnableStateResolver is a config resolver interface for retrieving whether the IMDS client is disabled.
     47 type ClientEnableStateResolver interface {
     48 	GetEC2IMDSClientEnableState() (ClientEnableState, bool, error)
     49 }
     50 
     51 // EndpointModeResolver is a config resolver interface for retrieving the EndpointModeState configuration.
     52 type EndpointModeResolver interface {
     53 	GetEC2IMDSEndpointMode() (EndpointModeState, bool, error)
     54 }
     55 
     56 // EndpointResolver is a config resolver interface for retrieving the endpoint.
     57 type EndpointResolver interface {
     58 	GetEC2IMDSEndpoint() (string, bool, error)
     59 }
     60 
     61 type v1FallbackDisabledResolver interface {
     62 	GetEC2IMDSV1FallbackDisabled() (bool, bool)
     63 }
     64 
     65 // ResolveClientEnableState resolves the ClientEnableState from a list of configuration sources.
     66 func ResolveClientEnableState(sources []interface{}) (value ClientEnableState, found bool, err error) {
     67 	for _, source := range sources {
     68 		if resolver, ok := source.(ClientEnableStateResolver); ok {
     69 			value, found, err = resolver.GetEC2IMDSClientEnableState()
     70 			if err != nil || found {
     71 				return value, found, err
     72 			}
     73 		}
     74 	}
     75 	return value, found, err
     76 }
     77 
     78 // ResolveEndpointModeConfig resolves the EndpointModeState from a list of configuration sources.
     79 func ResolveEndpointModeConfig(sources []interface{}) (value EndpointModeState, found bool, err error) {
     80 	for _, source := range sources {
     81 		if resolver, ok := source.(EndpointModeResolver); ok {
     82 			value, found, err = resolver.GetEC2IMDSEndpointMode()
     83 			if err != nil || found {
     84 				return value, found, err
     85 			}
     86 		}
     87 	}
     88 	return value, found, err
     89 }
     90 
     91 // ResolveEndpointConfig resolves the endpoint from a list of configuration sources.
     92 func ResolveEndpointConfig(sources []interface{}) (value string, found bool, err error) {
     93 	for _, source := range sources {
     94 		if resolver, ok := source.(EndpointResolver); ok {
     95 			value, found, err = resolver.GetEC2IMDSEndpoint()
     96 			if err != nil || found {
     97 				return value, found, err
     98 			}
     99 		}
    100 	}
    101 	return value, found, err
    102 }
    103 
    104 // ResolveV1FallbackDisabled ...
    105 func ResolveV1FallbackDisabled(sources []interface{}) (bool, bool) {
    106 	for _, source := range sources {
    107 		if resolver, ok := source.(v1FallbackDisabledResolver); ok {
    108 			if v, found := resolver.GetEC2IMDSV1FallbackDisabled(); found {
    109 				return v, true
    110 			}
    111 		}
    112 	}
    113 	return false, false
    114 }