-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Azure AD Workload Identity support for Azure Service Bus scaler.
Signed-off-by: Vighnesh Shenoy <[email protected]>
- Loading branch information
Showing
7 changed files
with
162 additions
and
32 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
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,30 @@ | ||
/* | ||
Copyright 2022 The KEDA Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package azure | ||
|
||
// AADToken is the token from Azure AD | ||
type AADToken struct { | ||
AccessToken string `json:"access_token"` | ||
RefreshToken string `json:"refresh_token"` | ||
ExpiresIn string `json:"expires_in"` | ||
ExpiresOn string `json:"expires_on"` | ||
NotBefore string `json:"not_before"` | ||
Resource string `json:"resource"` | ||
TokenType string `json:"token_type"` | ||
GrantedScopes []string `json:"grantedScopes"` | ||
DeclinedScopes []string `json:"DeclinedScopes"` | ||
} |
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,88 @@ | ||
/* | ||
Copyright 2022 The KEDA Authors | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package azure | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
|
||
"github.com/AzureAD/microsoft-authentication-library-for-go/apps/confidential" | ||
) | ||
|
||
// Azure AD Workload Identity Webhook will inject the following environment variables. | ||
// * AZURE_CLIENT_ID - Client id set in the service account annotation | ||
// * AZURE_TENANT_ID - Tenant id set in the service account annotation. If not defined, then tenant id provided via | ||
// azure-wi-webhook-config will be used. | ||
// * AZURE_FEDERATED_TOKEN_FILE - Service account token file path | ||
// * AZURE_AUTHORITY_HOST - Azure Active Directory (AAD) endpoint. | ||
const ( | ||
azureClientIDEnv = "AZURE_CLIENT_ID" | ||
azureTenantIDEnv = "AZURE_TENANT_ID" | ||
azureFederatedTokenFileEnv = "AZURE_FEDERATED_TOKEN_FILE" | ||
azureAuthrityHostEnv = "AZURE_AUTHORITY_HOST" | ||
) | ||
|
||
// GetAzureADWorkloadIdentityToken returns the AADToken for resource | ||
func GetAzureADWorkloadIdentityToken(ctx context.Context, resource string) (AADToken, error) { | ||
clientID := os.Getenv(azureClientIDEnv) | ||
tenantID := os.Getenv(azureTenantIDEnv) | ||
tokenFilePath := os.Getenv(azureFederatedTokenFileEnv) | ||
authorityHost := os.Getenv(azureAuthrityHostEnv) | ||
|
||
signedAssertion, err := readJWTFromFileSystem(tokenFilePath) | ||
if err != nil { | ||
return AADToken{}, err | ||
} | ||
|
||
cred, err := confidential.NewCredFromAssertion(signedAssertion) | ||
if err != nil { | ||
return AADToken{}, err | ||
} | ||
|
||
authorityOption := confidential.WithAuthority(fmt.Sprintf("%s%s/oauth2/token", authorityHost, tenantID)) | ||
confidentialClient, err := confidential.New( | ||
clientID, | ||
cred, | ||
authorityOption, | ||
) | ||
if err != nil { | ||
return AADToken{}, err | ||
} | ||
|
||
result, err := confidentialClient.AcquireTokenByCredential(ctx, []string{resource}) | ||
if err != nil { | ||
return AADToken{}, err | ||
} | ||
|
||
return AADToken{ | ||
AccessToken: result.AccessToken, | ||
ExpiresOn: strconv.FormatInt(result.ExpiresOn.Unix(), 10), | ||
GrantedScopes: result.GrantedScopes, | ||
DeclinedScopes: result.DeclinedScopes, | ||
Resource: resource, | ||
}, nil | ||
} | ||
|
||
func readJWTFromFileSystem(tokenFilePath string) (string, error) { | ||
token, err := os.ReadFile(tokenFilePath) | ||
if err != nil { | ||
return "", fmt.Errorf("error reading service account token") | ||
} | ||
return string(token), nil | ||
} |
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