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

Remove autorest dependency; wrapper structs/functions have been moved to a new module #154

Merged
merged 1 commit into from
Mar 15, 2022
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
6 changes: 0 additions & 6 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io/ioutil"
"strings"

"github.com/Azure/go-autorest/autorest"
"golang.org/x/crypto/pkcs12"
"golang.org/x/oauth2"

Expand Down Expand Up @@ -95,11 +94,6 @@ func (c *Config) NewAuthorizer(ctx context.Context, api environments.Api) (Autho
return nil, fmt.Errorf("no Authorizer could be configured, please check your configuration")
}

// NewAutorestAuthorizerWrapper returns an Authorizer that sources tokens from a supplied autorest.BearerAuthorizer
func NewAutorestAuthorizerWrapper(autorestAuthorizer autorest.Authorizer) (Authorizer, error) {
return &AutorestAuthorizerWrapper{authorizer: autorestAuthorizer}, nil
}

// NewAzureCliAuthorizer returns an Authorizer which authenticates using the Azure CLI.
func NewAzureCliAuthorizer(ctx context.Context, api environments.Api, tenantId string) (Authorizer, error) {
conf, err := NewAzureCliConfig(api, tenantId)
Expand Down
39 changes: 0 additions & 39 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"os"
"testing"

"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"golang.org/x/oauth2"

"github.com/manicminer/hamilton/auth"
Expand Down Expand Up @@ -176,43 +174,6 @@ func TestMsiAuthorizer(t *testing.T) {
}
}

func TestAutorestAuthorizerWrapper(t *testing.T) {
env, err := environments.EnvironmentFromString(environment)
if err != nil {
t.Fatal(err)
}

// adal.ServicePrincipalToken.refreshInternal() doesn't support v2 tokens
oauthConfig, err := adal.NewOAuthConfigWithAPIVersion(string(env.AzureADEndpoint), tenantId, utils.StringPtr("1.0"))
if err != nil {
t.Fatalf("adal.NewOAuthConfig(): %v", err)
}

spt, err := adal.NewServicePrincipalToken(*oauthConfig, clientId, clientSecret, string(env.MsGraph.Endpoint))
if err != nil {
t.Fatalf("adal.NewServicePrincipalToken(): %v", err)
}

auth, err := auth.NewAutorestAuthorizerWrapper(autorest.NewBearerAuthorizer(spt))
if err != nil {
t.Fatalf("NewAutorestAuthorizerWrapper(): %v", err)
}
if auth == nil {
t.Fatal("auth is nil, expected Authorizer")
}

token, err := auth.Token()
if err != nil {
t.Fatalf("auth.Token(): %v", err)
}
if token == nil {
t.Fatal("token was nil")
}
if token.AccessToken == "" {
t.Fatal("token.AccessToken was empty")
}
}

func TestGitHubOIDCAuthorizer(t *testing.T) {
if gitHubTokenURL == "" {
t.Skip("gitHubTokenURL was empty")
Expand Down
158 changes: 0 additions & 158 deletions auth/autorest.go

This file was deleted.

56 changes: 0 additions & 56 deletions auth/cache.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package auth

import (
"fmt"
"net/http"
"strings"
"sync"

"github.com/Azure/go-autorest/autorest"
"golang.org/x/oauth2"
)

Expand Down Expand Up @@ -64,58 +60,6 @@ func (c *CachedAuthorizer) AuxiliaryTokens() ([]*oauth2.Token, error) {
return c.auxTokens, nil
}

// WithAuthorization implements the autorest.Authorizer interface
func (c *CachedAuthorizer) WithAuthorization() autorest.PrepareDecorator {
return func(p autorest.Preparer) autorest.Preparer {
return autorest.PreparerFunc(func(req *http.Request) (*http.Request, error) {
var err error
req, err = p.Prepare(req)
if err == nil {
token, err := c.Token()
if err != nil {
return nil, err
}

req, err = autorest.Prepare(req, autorest.WithHeader("Authorization", fmt.Sprintf("Bearer %s", token.AccessToken)))
if err != nil {
return req, err
}

auxTokens, err := c.AuxiliaryTokens()
if err != nil {
return req, err
}

auxTokenList := make([]string, 0)
for _, a := range auxTokens {
if a != nil && a.AccessToken != "" {
auxTokenList = append(auxTokenList, fmt.Sprintf("%s %s", a.TokenType, a.AccessToken))
}
}

return autorest.Prepare(req, autorest.WithHeader("x-ms-authorization-auxiliary", strings.Join(auxTokenList, ", ")))
}

return req, err
})
}
}

// BearerAuthorizerCallback is a helper that returns an *autorest.BearerAuthorizerCallback for use in data plane API clients in the Azure SDK
func (c *CachedAuthorizer) BearerAuthorizerCallback() *autorest.BearerAuthorizerCallback {
return autorest.NewBearerAuthorizerCallback(nil, func(_, resource string) (*autorest.BearerAuthorizer, error) {
token, err := c.Token()
if err != nil {
return nil, fmt.Errorf("obtaining token: %v", err)
}

return autorest.NewBearerAuthorizer(&servicePrincipalTokenWrapper{
tokenType: "Bearer",
tokenValue: token.AccessToken,
}), nil
})
}

// NewCachedAuthorizer returns an Authorizer that caches an access token for the duration of its validity.
// If the cached token expires, a new one is acquired and cached.
func NewCachedAuthorizer(src Authorizer) Authorizer {
Expand Down
70 changes: 0 additions & 70 deletions environments/metadata.go

This file was deleted.

Loading