Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improves auth plugin resolution. #4175

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions plugins/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"reflect"
Expand Down Expand Up @@ -65,23 +66,33 @@ func (c *Config) Equal(other *Config) bool {

func (c *Config) authPlugin(authPluginLookup func(string) HTTPAuthPlugin) (HTTPAuthPlugin, error) {
var candidate HTTPAuthPlugin
if c.Credentials.Plugin != nil && authPluginLookup != nil {
if c.Credentials.Plugin != nil {
if authPluginLookup == nil {
// if no authPluginLookup function is passed we can't resolve the plugin
return nil, errors.New("missing auth plugin lookup function")
}

candidate := authPluginLookup(*c.Credentials.Plugin)
if candidate != nil {
return candidate, nil
if candidate == nil {
return nil, fmt.Errorf("auth plugin %q not found", *c.Credentials.Plugin)
}

return candidate, nil
}
// reflection avoids need for this code to change as auth plugins are added
s := reflect.ValueOf(c.Credentials)
for i := 0; i < s.NumField(); i++ {
if s.Field(i).IsNil() {
continue
}

if candidate != nil {
return nil, errors.New("a maximum one credential method must be specified")
}

candidate = s.Field(i).Interface().(HTTPAuthPlugin)
}

if candidate == nil {
return &defaultAuthPlugin{}, nil
}
Expand Down
36 changes: 35 additions & 1 deletion plugins/rest/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,31 @@ import (

const keyID = "key1"

func TestNew(t *testing.T) {
func TestAuthPluginWithNoAuthPluginLookup(t *testing.T) {
authPlugin := "anything"
cfg := Config{
Credentials: struct {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to list all the fields again here...? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to only pass the field plugin but it fails because types aren't the same.

Bearer *bearerAuthPlugin `json:"bearer,omitempty"`
OAuth2 *oauth2ClientCredentialsAuthPlugin `json:"oauth2,omitempty"`
ClientTLS *clientTLSAuthPlugin `json:"client_tls,omitempty"`
S3Signing *awsSigningAuthPlugin `json:"s3_signing,omitempty"`
GCPMetadata *gcpMetadataAuthPlugin `json:"gcp_metadata,omitempty"`
AzureManagedIdentity *azureManagedIdentitiesAuthPlugin `json:"azure_managed_identity,omitempty"`
Plugin *string `json:"plugin,omitempty"`
}{
Plugin: &authPlugin,
},
}
_, err := cfg.authPlugin(nil)
if err == nil {
t.Error("Expected error but got nil")
}
if want, have := "missing auth plugin lookup function", err.Error(); want != have {
t.Errorf("Unexpected error, want %q, have %q", want, have)
}
}

func TestNew(t *testing.T) {
tests := []struct {
name string
input string
Expand Down Expand Up @@ -623,6 +646,17 @@ func TestNew(t *testing.T) {
}
}`,
},
{
name: "Unknown plugin",
input: `{
"name": "foo",
"url": "http://localhost",
"credentials": {
"plugin": "unknown_plugin"
}
}`,
wantErr: true,
},
}

var results []Client
Expand Down