-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable multi-tenant authentication with auxiliary token provider
- Loading branch information
Showing
13 changed files
with
754 additions
and
585 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package armauth | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
"github.com/go-logr/logr" | ||
) | ||
|
||
// MultiTenantTokenProvider is the track1 multi-tenant token provider wrapper for track2 implementation. | ||
type MultiTenantTokenProvider struct { | ||
logger logr.Logger | ||
primaryCredential azcore.TokenCredential | ||
auxiliaryCredentials []azcore.TokenCredential | ||
timeout time.Duration | ||
scope string | ||
} | ||
|
||
func NewMultiTenantTokenProvider( | ||
logger logr.Logger, | ||
primaryCredential azcore.TokenCredential, | ||
auxiliaryCredentials []azcore.TokenCredential, | ||
scope string, | ||
) (*MultiTenantTokenProvider, error) { | ||
return &MultiTenantTokenProvider{ | ||
logger: logger, | ||
primaryCredential: primaryCredential, | ||
auxiliaryCredentials: auxiliaryCredentials, | ||
timeout: 10 * time.Second, | ||
scope: scope, | ||
}, nil | ||
} | ||
|
||
func (p *MultiTenantTokenProvider) PrimaryOAuthToken() string { | ||
p.logger.V(4).Info("Fetching primary oauth token") | ||
ctx, cancel := context.WithTimeout(context.Background(), p.timeout) | ||
defer cancel() | ||
|
||
token, err := p.primaryCredential.GetToken(ctx, policy.TokenRequestOptions{ | ||
Scopes: []string{p.scope}, | ||
}) | ||
if err != nil { | ||
p.logger.Error(err, "Failed to fetch primary OAuth token") | ||
return "" | ||
} | ||
return token.Token | ||
} | ||
|
||
func (p *MultiTenantTokenProvider) AuxiliaryOAuthTokens() []string { | ||
p.logger.V(4).Info("Fetching auxiliary oauth token", "num-credentials", len(p.auxiliaryCredentials)) | ||
ctx, cancel := context.WithTimeout(context.Background(), p.timeout) | ||
defer cancel() | ||
|
||
var tokens []string | ||
for _, cred := range p.auxiliaryCredentials { | ||
token, err := cred.GetToken(ctx, policy.TokenRequestOptions{ | ||
Scopes: []string{p.scope}, | ||
}) | ||
if err != nil { | ||
p.logger.Error(err, "Failed to fetch auxiliary OAuth token") | ||
return nil | ||
} | ||
|
||
tokens = append(tokens, token.Token) | ||
} | ||
|
||
return tokens | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package armauth | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
"github.com/go-logr/logr" | ||
) | ||
|
||
// TokenProvider is the track1 token provider wrapper for track2 implementation. | ||
type TokenProvider struct { | ||
logger logr.Logger | ||
credential azcore.TokenCredential | ||
timeout time.Duration | ||
scope string | ||
} | ||
|
||
func NewTokenProvider( | ||
logger logr.Logger, | ||
credential azcore.TokenCredential, | ||
scope string, | ||
) (*TokenProvider, error) { | ||
return &TokenProvider{ | ||
logger: logger, | ||
credential: credential, | ||
timeout: 10 * time.Second, | ||
scope: scope, | ||
}, nil | ||
} | ||
|
||
func (p *TokenProvider) OAuthToken() string { | ||
p.logger.V(4).Info("Fetching OAuth token") | ||
ctx, cancel := context.WithTimeout(context.Background(), p.timeout) | ||
defer cancel() | ||
|
||
token, err := p.credential.GetToken(ctx, policy.TokenRequestOptions{ | ||
Scopes: []string{p.scope}, | ||
}) | ||
if err != nil { | ||
p.logger.Error(err, "Failed to fetch OAuth token") | ||
return "" | ||
} | ||
p.logger.V(4).Info("Fetched OAuth token successfully", "token", token.Token) | ||
return token.Token | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.