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

[v14] Fix azure join when filtering by resource group #42140

Merged
merged 2 commits into from
May 29, 2024
Merged
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
25 changes: 21 additions & 4 deletions lib/auth/join_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/pem"
"net/url"
"slices"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
Expand Down Expand Up @@ -284,15 +285,31 @@ func checkAzureAllowRules(vm *azure.VirtualMachine, token string, allowRules []*
if rule.Subscription != vm.Subscription {
continue
}
if len(rule.ResourceGroups) > 0 {
if !slices.Contains(rule.ResourceGroups, vm.ResourceGroup) {
continue
}
if !azureResourceGroupIsAllowed(rule.ResourceGroups, vm.ResourceGroup) {
continue
}
return nil
}
return trace.AccessDenied("instance %v did not match any allow rules in token %v", vm.Name, token)
}
func azureResourceGroupIsAllowed(allowedResourceGroups []string, vmResourceGroup string) bool {
if len(allowedResourceGroups) == 0 {
return true
}

// ResourceGroups are case insensitive.
// https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/frequently-asked-questions#are-resource-group-names-case-sensitive
// The API returns them using capital case, but docs don't mention a specific case.
// Converting everything to the same case will ensure a proper comparison.
resourceGroup := strings.ToUpper(vmResourceGroup)
for _, allowedResourceGroup := range allowedResourceGroups {
if strings.EqualFold(resourceGroup, allowedResourceGroup) {
return true
}
}

return false
}

func (a *Server) checkAzureRequest(ctx context.Context, challenge string, req *proto.RegisterUsingAzureMethodRequest, cfg *azureRegisterConfig) error {
requestStart := a.clock.Now()
Expand Down
40 changes: 31 additions & 9 deletions lib/auth/join_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestAuth_RegisterUsingAzureMethod(t *testing.T) {
tokenName: "test-token",
requestTokenName: "test-token",
subscription: subID,
resourceGroup: "rg",
resourceGroup: "RG",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Azure: &types.ProvisionTokenSpecV2Azure{
Expand All @@ -205,12 +205,34 @@ func TestAuth_RegisterUsingAzureMethod(t *testing.T) {
certs: []*x509.Certificate{tlsConfig.Certificate},
assertError: require.NoError,
},
{
name: "resource group is case insensitive",
tokenName: "test-token",
requestTokenName: "test-token",
subscription: subID,
resourceGroup: "my-RESOURCE-GROUP",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Azure: &types.ProvisionTokenSpecV2Azure{
Allow: []*types.ProvisionTokenSpecV2Azure_Rule{
{
Subscription: subID,
ResourceGroups: []string{"MY-resource-group"},
},
},
},
JoinMethod: types.JoinMethodAzure,
},
verify: mockVerifyToken(nil),
certs: []*x509.Certificate{tlsConfig.Certificate},
assertError: require.NoError,
},
{
name: "wrong token",
tokenName: "test-token",
requestTokenName: "wrong-token",
subscription: subID,
resourceGroup: "rg",
resourceGroup: "RG",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Azure: &types.ProvisionTokenSpecV2Azure{
Expand All @@ -231,7 +253,7 @@ func TestAuth_RegisterUsingAzureMethod(t *testing.T) {
tokenName: "test-token",
requestTokenName: "test-token",
subscription: subID,
resourceGroup: "rg",
resourceGroup: "RG",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Azure: &types.ProvisionTokenSpecV2Azure{
Expand All @@ -253,7 +275,7 @@ func TestAuth_RegisterUsingAzureMethod(t *testing.T) {
tokenName: "test-token",
requestTokenName: "test-token",
subscription: "some-junk",
resourceGroup: "rg",
resourceGroup: "RG",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Azure: &types.ProvisionTokenSpecV2Azure{
Expand All @@ -274,7 +296,7 @@ func TestAuth_RegisterUsingAzureMethod(t *testing.T) {
tokenName: "test-token",
requestTokenName: "test-token",
subscription: subID,
resourceGroup: "wrong-rg",
resourceGroup: "WRONG-RG",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Azure: &types.ProvisionTokenSpecV2Azure{
Expand All @@ -296,7 +318,7 @@ func TestAuth_RegisterUsingAzureMethod(t *testing.T) {
tokenName: "test-token",
requestTokenName: "test-token",
subscription: subID,
resourceGroup: "rg",
resourceGroup: "RG",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Azure: &types.ProvisionTokenSpecV2Azure{
Expand All @@ -320,7 +342,7 @@ func TestAuth_RegisterUsingAzureMethod(t *testing.T) {
tokenName: "test-token",
requestTokenName: "test-token",
subscription: subID,
resourceGroup: "rg",
resourceGroup: "RG",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Azure: &types.ProvisionTokenSpecV2Azure{
Expand All @@ -341,7 +363,7 @@ func TestAuth_RegisterUsingAzureMethod(t *testing.T) {
tokenName: "test-token",
requestTokenName: "test-token",
subscription: subID,
resourceGroup: "rg",
resourceGroup: "RG",
vmID: "vm-id",
tokenSpec: types.ProvisionTokenSpecV2{
Roles: []types.SystemRole{types.RoleNode},
Expand All @@ -356,7 +378,7 @@ func TestAuth_RegisterUsingAzureMethod(t *testing.T) {
},
vmResult: &azure.VirtualMachine{
Subscription: subID,
ResourceGroup: "rg",
ResourceGroup: "RG",
VMID: "different-id",
},
verify: mockVerifyToken(nil),
Expand Down
Loading