-
Notifications
You must be signed in to change notification settings - Fork 67
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
refactor: rework options flow to have a more consistent pattern #390
Draft
comtalyst
wants to merge
9
commits into
main
Choose a base branch
from
comtalyst/options-flow-rework
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c4dfa88
refactor: rework aged authentication config flow
comtalyst a914946
refactor: rename userAssignedIdentityID to kubeletIdentityClientID in…
comtalyst 69f83e2
chore: not supporting legacy envs
comtalyst 133ffec
chore: rename "credential-from-environment" to "workload-identity"
comtalyst 70c2e8e
chore: make presubmit
comtalyst c3615b8
refactor: rename AuthMethod to ArmAuthMethod for clarity
comtalyst 3a18375
refactor: rework options/env flow
comtalyst 4b4b3c4
fix: incorrect if
comtalyst 02a046c
chore: move node identities defaulting away for now
comtalyst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
Portions Copyright (c) Microsoft Corporation. | ||
|
||
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 auth | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Azure/go-autorest/autorest" | ||
"github.com/Azure/go-autorest/autorest/adal" | ||
"github.com/Azure/go-autorest/autorest/azure" | ||
"k8s.io/klog/v2" | ||
|
||
"github.com/Azure/azure-sdk-for-go/sdk/azcore" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
"github.com/jongio/azidext/go/azidext" | ||
) | ||
|
||
const ( | ||
// auth methods | ||
AuthMethodSysMSI = "system-assigned-msi" | ||
AuthMethodWorkloadIdentity = "workload-identity" | ||
) | ||
|
||
// AuthManager manages the authentication logic for Azure clients used by Karpenter to make requests | ||
type AuthManager struct { | ||
authMethod string | ||
location string | ||
} | ||
|
||
func NewAuthManagerWorkloadIdentity(location string) *AuthManager { | ||
return &AuthManager{ | ||
authMethod: AuthMethodWorkloadIdentity, | ||
location: location, | ||
} | ||
|
||
} | ||
|
||
func NewAuthManagerSystemAssignedMSI(location string) *AuthManager { | ||
return &AuthManager{ | ||
authMethod: AuthMethodSysMSI, | ||
location: location, | ||
} | ||
} | ||
|
||
// NewCredential provides a token credential | ||
func (am AuthManager) NewCredential() (azcore.TokenCredential, error) { | ||
if am.authMethod == AuthMethodWorkloadIdentity { | ||
klog.V(2).Infoln("cred: using workload identity for new credential") | ||
return azidentity.NewDefaultAzureCredential(nil) | ||
} | ||
|
||
if am.authMethod == AuthMethodSysMSI { | ||
klog.V(2).Infoln("cred: using system assigned MSI for new credential") | ||
msiCred, err := azidentity.NewManagedIdentityCredential(nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return msiCred, nil | ||
} | ||
|
||
return nil, fmt.Errorf("cred: unsupported auth method: %s", am.authMethod) | ||
} | ||
Comment on lines
+59
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this belong in the shared SDK? For my token fix i imagine other ppl may want the same fix. Uing azure sdk for go extensions may be a good idea for our auth so we can share that logic too. Should we move this there? |
||
|
||
func (am AuthManager) NewAutorestAuthorizer() (autorest.Authorizer, error) { | ||
// TODO (charliedmcb): need to get track 2 support for the skewer API, and align all auth under workload identity in the same way within cred.go | ||
if am.authMethod == AuthMethodWorkloadIdentity { | ||
klog.V(2).Infoln("auth: using workload identity for new authorizer") | ||
cred, err := azidentity.NewDefaultAzureCredential(nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("default cred: %w", err) | ||
} | ||
return azidext.NewTokenCredentialAdapter(cred, []string{azidext.DefaultManagementScope}), nil | ||
} | ||
|
||
if am.authMethod == AuthMethodWorkloadIdentity { | ||
comtalyst marked this conversation as resolved.
Show resolved
Hide resolved
|
||
klog.V(2).Infoln("auth: using system assigned MSI to retrieve access token") | ||
msiEndpoint, err := adal.GetMSIVMEndpoint() | ||
if err != nil { | ||
return nil, fmt.Errorf("getting the managed service identity endpoint: %w", err) | ||
} | ||
|
||
azureEnvironment, err := azure.EnvironmentFromName(am.location) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get AzureEnvironment: %w", err) | ||
} | ||
|
||
token, err := adal.NewServicePrincipalTokenFromMSI( | ||
msiEndpoint, | ||
azureEnvironment.ServiceManagementEndpoint) | ||
if err != nil { | ||
return nil, fmt.Errorf("retrieve service principal token: %w", err) | ||
} | ||
return autorest.NewBearerAuthorizer(token), nil | ||
} | ||
|
||
return nil, fmt.Errorf("auth: unsupported auth method %s", am.authMethod) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: the reason its called "from environment" is because its actually using a more generic/general auth option that finds the creds in the environment. This is the flow that works for workload-identity. However, I think the naming should probably keep the more generic term since that is what its actually doing. Unless you have more tied to this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, by theory, there could be "from environment" auth option that is not workload identity, right?
If that's the case then I think keeping "from-environment" would be a better choice, given we doesn't seem to have ties to workload identity (right?).