From a328390977f3089c023e2b2a7e50cc6053b4fb02 Mon Sep 17 00:00:00 2001 From: Miccah Castorina Date: Thu, 19 Sep 2024 10:18:15 -0700 Subject: [PATCH] WIP: Update EndpointSetter --- pkg/detectors/detectors.go | 3 ++- pkg/detectors/endpoint_customizer.go | 38 ++++++++++++++++++---------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/pkg/detectors/detectors.go b/pkg/detectors/detectors.go index 5bf35b1d8..d3cf2f980 100644 --- a/pkg/detectors/detectors.go +++ b/pkg/detectors/detectors.go @@ -72,7 +72,8 @@ type MultiPartCredentialProvider interface { // EndpointCustomizer is an optional interface that a detector can implement to // support verifying against user-supplied endpoints. type EndpointCustomizer interface { - SetEndpoints(...string) error + SetConfiguredEndpoints(...string) error + SetCloudEndpoint(string) UseCloudEndpoint(bool) UseFoundEndpoints(bool) } diff --git a/pkg/detectors/endpoint_customizer.go b/pkg/detectors/endpoint_customizer.go index 85510cd9e..e4c6ccfb1 100644 --- a/pkg/detectors/endpoint_customizer.go +++ b/pkg/detectors/endpoint_customizer.go @@ -10,27 +10,39 @@ import ( // of the EndpointCustomizer interface. A detector can embed this struct to // gain the functionality. type EndpointSetter struct { - endpoints []string + configuredEndpoints []string + cloudEndpoint string + useCloudEndpoint bool + useFoundEndpoints bool } -func (e *EndpointSetter) SetEndpoints(endpoints ...string) error { - if len(endpoints) == 0 { +func (e *EndpointSetter) SetConfiguredEndpoints(userConfiguredEndpoints ...string) error { + if len(userConfiguredEndpoints) == 0 { return fmt.Errorf("at least one endpoint required") } - deduped := make([]string, 0, len(endpoints)) - for _, endpoint := range endpoints { + deduped := make([]string, 0, len(userConfiguredEndpoints)) + for _, endpoint := range userConfiguredEndpoints { common.AddStringSliceItem(endpoint, &deduped) } - e.endpoints = deduped + e.configuredEndpoints = deduped return nil } -func (e *EndpointSetter) Endpoints(defaultEndpoint string) []string { - // The only valid time len(e.endpoints) == 0 is when EndpointSetter is - // initializetd to its default state. That means SetEndpoints was never - // called and we should use the default. - if len(e.endpoints) == 0 { - return []string{defaultEndpoint} +func (e *EndpointSetter) UseCloudEndpoint(enabled bool) { + e.useCloudEndpoint = true +} + +func (e *EndpointSetter) UseFoundEndpoints(enabled bool) { + e.useFoundEndpoints = true +} + +func (e *EndpointSetter) Endpoints(foundEndpoints ...string) []string { + endpoints := e.configuredEndpoints + if e.useCloudEndpoint { + endpoints = append(endpoints, e.cloudEndpoint) } - return e.endpoints + if e.useFoundEndpoints { + endpoints = append(endpoints, foundEndpoints...) + } + return endpoints }