resolvers.go (2990B)
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 // ResolveClientEnableState resolves the ClientEnableState from a list of configuration sources. 62 func ResolveClientEnableState(sources []interface{}) (value ClientEnableState, found bool, err error) { 63 for _, source := range sources { 64 if resolver, ok := source.(ClientEnableStateResolver); ok { 65 value, found, err = resolver.GetEC2IMDSClientEnableState() 66 if err != nil || found { 67 return value, found, err 68 } 69 } 70 } 71 return value, found, err 72 } 73 74 // ResolveEndpointModeConfig resolves the EndpointModeState from a list of configuration sources. 75 func ResolveEndpointModeConfig(sources []interface{}) (value EndpointModeState, found bool, err error) { 76 for _, source := range sources { 77 if resolver, ok := source.(EndpointModeResolver); ok { 78 value, found, err = resolver.GetEC2IMDSEndpointMode() 79 if err != nil || found { 80 return value, found, err 81 } 82 } 83 } 84 return value, found, err 85 } 86 87 // ResolveEndpointConfig resolves the endpoint from a list of configuration sources. 88 func ResolveEndpointConfig(sources []interface{}) (value string, found bool, err error) { 89 for _, source := range sources { 90 if resolver, ok := source.(EndpointResolver); ok { 91 value, found, err = resolver.GetEC2IMDSEndpoint() 92 if err != nil || found { 93 return value, found, err 94 } 95 } 96 } 97 return value, found, err 98 }