-
Notifications
You must be signed in to change notification settings - Fork 252
feat: Add support for Azure SQL AAD Users #5104
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
Open
TheiLLeniumStudios
wants to merge
6
commits into
Azure:main
Choose a base branch
from
TheiLLeniumStudios:feat/aad-users-azure-sql
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
170bdc4
feat(sql): add AADUserSpec to User CRD types
TheiLLeniumStudios f2e5766
feat(sql): add webhook validation for AAD users
TheiLLeniumStudios bf3af24
feat(sql): add AAD user connector for Azure SQL
TheiLLeniumStudios b1fb526
feat(sql): update reconciler to support AAD users
TheiLLeniumStudios 5e456ad
test(sql): add integration tests for AAD users
TheiLLeniumStudios 1219643
test(sql): add webhook unit tests for User resource
TheiLLeniumStudios 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 hidden or 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 hidden or 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,193 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT license. | ||
| package webhook | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| . "github.com/onsi/gomega" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
|
||
| v1 "github.com/Azure/azure-service-operator/v2/api/sql/v1" | ||
| "github.com/Azure/azure-service-operator/v2/pkg/genruntime" | ||
| ) | ||
|
|
||
| func Test_UserWebhook_ValidateIsLocalOrAAD(t *testing.T) { | ||
| t.Parallel() | ||
| g := NewGomegaWithT(t) | ||
| webhook := &User_Webhook{} | ||
| ctx := context.Background() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| user *v1.User | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "valid local user", | ||
| user: &v1.User{ | ||
| Spec: v1.UserSpec{ | ||
| LocalUser: &v1.LocalUserSpec{ | ||
| ServerAdminUsername: "admin", | ||
| ServerAdminPassword: &genruntime.SecretReference{Name: "secret", Key: "password"}, | ||
| Password: &genruntime.SecretReference{Name: "secret", Key: "userpass"}, | ||
| }, | ||
| }, | ||
| }, | ||
| wantErr: "", | ||
| }, | ||
| { | ||
| name: "valid AAD user", | ||
| user: &v1.User{ | ||
| Spec: v1.UserSpec{ | ||
| AADUser: &v1.AADUserSpec{ | ||
| ServerAdminUsername: "admin", | ||
| }, | ||
| }, | ||
| }, | ||
| wantErr: "", | ||
| }, | ||
| { | ||
| name: "neither specified", | ||
| user: &v1.User{Spec: v1.UserSpec{}}, | ||
| wantErr: "exactly one of spec.localUser or spec.aadUser must be set", | ||
| }, | ||
| { | ||
| name: "both specified", | ||
| user: &v1.User{ | ||
| Spec: v1.UserSpec{ | ||
| LocalUser: &v1.LocalUserSpec{ServerAdminUsername: "admin"}, | ||
| AADUser: &v1.AADUserSpec{ServerAdminUsername: "admin"}, | ||
| }, | ||
| }, | ||
| wantErr: "exactly one of spec.localUser or spec.aadUser must be set", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, err := webhook.validateIsLocalOrAAD(ctx, tt.user) | ||
| if tt.wantErr == "" { | ||
| g.Expect(err).To(BeNil()) | ||
| } else { | ||
| g.Expect(err).ToNot(BeNil()) | ||
| g.Expect(err.Error()).To(ContainSubstring(tt.wantErr)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_UserWebhook_ValidateUserTypeNotChanged(t *testing.T) { | ||
| t.Parallel() | ||
| g := NewGomegaWithT(t) | ||
| webhook := &User_Webhook{} | ||
| ctx := context.Background() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| oldUser *v1.User | ||
| newUser *v1.User | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "local to local - allowed", | ||
| oldUser: &v1.User{Spec: v1.UserSpec{LocalUser: &v1.LocalUserSpec{ServerAdminUsername: "admin"}}}, | ||
| newUser: &v1.User{Spec: v1.UserSpec{LocalUser: &v1.LocalUserSpec{ServerAdminUsername: "admin2"}}}, | ||
| wantErr: "", | ||
| }, | ||
| { | ||
| name: "aad to aad - allowed", | ||
| oldUser: &v1.User{Spec: v1.UserSpec{AADUser: &v1.AADUserSpec{ServerAdminUsername: "admin"}}}, | ||
| newUser: &v1.User{Spec: v1.UserSpec{AADUser: &v1.AADUserSpec{ServerAdminUsername: "admin2"}}}, | ||
| wantErr: "", | ||
| }, | ||
| { | ||
| name: "local to aad - not allowed", | ||
| oldUser: &v1.User{Spec: v1.UserSpec{LocalUser: &v1.LocalUserSpec{ServerAdminUsername: "admin"}}}, | ||
| newUser: &v1.User{Spec: v1.UserSpec{AADUser: &v1.AADUserSpec{ServerAdminUsername: "admin"}}}, | ||
| wantErr: "cannot change from local user to AAD user", | ||
| }, | ||
| { | ||
| name: "aad to local - not allowed", | ||
| oldUser: &v1.User{Spec: v1.UserSpec{AADUser: &v1.AADUserSpec{ServerAdminUsername: "admin"}}}, | ||
| newUser: &v1.User{Spec: v1.UserSpec{LocalUser: &v1.LocalUserSpec{ServerAdminUsername: "admin"}}}, | ||
| wantErr: "cannot change from AAD user to local user", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, err := webhook.validateUserTypeNotChanged(ctx, tt.oldUser, tt.newUser) | ||
| if tt.wantErr == "" { | ||
| g.Expect(err).To(BeNil()) | ||
| } else { | ||
| g.Expect(err).ToNot(BeNil()) | ||
| g.Expect(err.Error()).To(ContainSubstring(tt.wantErr)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_UserWebhook_ValidateUserAADAliasNotChanged(t *testing.T) { | ||
| t.Parallel() | ||
| g := NewGomegaWithT(t) | ||
| webhook := &User_Webhook{} | ||
| ctx := context.Background() | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| oldUser *v1.User | ||
| newUser *v1.User | ||
| wantErr string | ||
| }{ | ||
| { | ||
| name: "alias unchanged - allowed", | ||
| oldUser: &v1.User{Spec: v1.UserSpec{AADUser: &v1.AADUserSpec{Alias: "myalias"}}}, | ||
| newUser: &v1.User{Spec: v1.UserSpec{AADUser: &v1.AADUserSpec{Alias: "myalias"}}}, | ||
| wantErr: "", | ||
| }, | ||
| { | ||
| name: "alias changed - not allowed", | ||
| oldUser: &v1.User{Spec: v1.UserSpec{AADUser: &v1.AADUserSpec{Alias: "oldalias"}}}, | ||
| newUser: &v1.User{Spec: v1.UserSpec{AADUser: &v1.AADUserSpec{Alias: "newalias"}}}, | ||
| wantErr: "cannot change AAD user 'alias'", | ||
| }, | ||
| { | ||
| name: "local user - skipped", | ||
| oldUser: &v1.User{Spec: v1.UserSpec{LocalUser: &v1.LocalUserSpec{ServerAdminUsername: "admin"}}}, | ||
| newUser: &v1.User{Spec: v1.UserSpec{LocalUser: &v1.LocalUserSpec{ServerAdminUsername: "admin"}}}, | ||
| wantErr: "", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, err := webhook.validateUserAADAliasNotChanged(ctx, tt.oldUser, tt.newUser) | ||
| if tt.wantErr == "" { | ||
| g.Expect(err).To(BeNil()) | ||
| } else { | ||
| g.Expect(err).ToNot(BeNil()) | ||
| g.Expect(err.Error()).To(ContainSubstring(tt.wantErr)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_UserWebhook_ValidateCreate(t *testing.T) { | ||
| t.Parallel() | ||
| g := NewGomegaWithT(t) | ||
| webhook := &User_Webhook{} | ||
| ctx := context.Background() | ||
|
|
||
| user := &v1.User{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test-user", Namespace: "default"}, | ||
| Spec: v1.UserSpec{ | ||
| AzureName: "my-managed-identity", | ||
| AADUser: &v1.AADUserSpec{ServerAdminUsername: "admin"}, | ||
| }, | ||
| } | ||
|
|
||
| _, err := webhook.ValidateCreate(ctx, user) | ||
| g.Expect(err).To(BeNil()) | ||
| } |
This file contains hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
hah, great catch