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

Add support for loading TokenAudience from clouds.config #307

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ require (
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
gopkg.in/ini.v1 v1.67.0 // indirect
)
4 changes: 3 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/cheggaaa/pb.v1 v1.0.27/go.mod h1:V/YB90LKu/1FcN3WVnfiiE5oMCibMjukxqG/qStrOgw=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand All @@ -723,4 +725,4 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
34 changes: 31 additions & 3 deletions internal/clients/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package clients
import (
"context"
"fmt"
"github.com/mitchellh/go-homedir"
"gopkg.in/ini.v1"

"github.com/Azure/go-autorest/autorest"
"github.com/hashicorp/go-azure-helpers/authentication"
Expand All @@ -23,12 +25,38 @@ type ClientBuilder struct {
Features features.UserFeatures
}

// GetResourceIDFromCloudsConfig attempts to read the endpoint_active_directory_resource_id from clouds.config
func GetResourceIDFromCloudsConfig() (string, error) {
cloudsConfigPath, err := homedir.Expand("~/.azure/clouds.config")
if err != nil {
return "", fmt.Errorf("expanding clouds.config path: %v", err)
}

cloudsConfig, err := ini.Load(cloudsConfigPath)
if err != nil {
return "", nil // ignore errors if file doesn't exist
}

for _, section := range cloudsConfig.Sections() {
if section.HasKey("endpoint_active_directory_resource_id") {
return section.Key("endpoint_active_directory_resource_id").String(), nil
}
}

return "", nil
}

func Build(ctx context.Context, builder ClientBuilder) (*Client, error) {
env, err := authentication.AzureEnvironmentByNameFromEndpoint(ctx, builder.AuthConfig.MetadataHost, builder.AuthConfig.Environment)
if err != nil {
return nil, fmt.Errorf("determining environment: %v", err)
}

// Try to get TokenAudience from clouds.config
if resourceID, err := GetResourceIDFromCloudsConfig(); err == nil && resourceID != "" {
env.TokenAudience = resourceID
}

// client declarations:
account, err := NewResourceManagerAccount(ctx, *builder.AuthConfig, *env, builder.SkipProviderRegistration)
if err != nil {
Expand Down Expand Up @@ -66,7 +94,7 @@ func Build(ctx context.Context, builder ClientBuilder) (*Client, error) {
}

// Storage Endpoints
storageAuth, err := builder.AuthConfig.GetADALToken(ctx, sender, oauthConfig, endpoint)
storageAuth, err := builder.AuthConfig.GetADALToken(ctx, sender, oauthConfig, env.TokenAudience)
if err != nil {
return nil, fmt.Errorf("unable to get authorization token for storage endpoints: %+v", err)
}
Expand All @@ -89,7 +117,7 @@ func Build(ctx context.Context, builder ClientBuilder) (*Client, error) {
CustomCorrelationRequestID: builder.CustomCorrelationRequestID,
Environment: *env,
TokenFunc: func(endpoint string) (autorest.Authorizer, error) {
authorizer, err := builder.AuthConfig.GetADALToken(ctx, sender, oauthConfig, endpoint)
authorizer, err := builder.AuthConfig.GetADALToken(ctx, sender, oauthConfig, env.TokenAudience)
if err != nil {
return nil, fmt.Errorf("getting authorization token for endpoint %s: %+v", endpoint, err)
}
Expand All @@ -107,4 +135,4 @@ func Build(ctx context.Context, builder ClientBuilder) (*Client, error) {
}*/

return &client, nil
}
}
3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,6 @@ google.golang.org/protobuf/types/known/anypb
google.golang.org/protobuf/types/known/durationpb
google.golang.org/protobuf/types/known/emptypb
google.golang.org/protobuf/types/known/timestamppb
# gopkg.in/ini.v1 v1.67.0
## explicit
gopkg.in/ini.v1
Loading