Skip to content
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
21 changes: 15 additions & 6 deletions lib/auth/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import (
apievents "github.com/gravitational/teleport/api/types/events"
"github.com/gravitational/teleport/api/types/installers"
"github.com/gravitational/teleport/api/types/wrappers"
apiutils "github.com/gravitational/teleport/api/utils"
"github.com/gravitational/teleport/lib/auth/assist/assistv1"
"github.com/gravitational/teleport/lib/auth/discoveryconfig/discoveryconfigv1"
integrationService "github.com/gravitational/teleport/lib/auth/integration/integrationv1"
Expand Down Expand Up @@ -2090,6 +2091,12 @@ func maybeDowngradeRoleLabelExpressions(ctx context.Context, role *types.RoleV6,
if !clientVersion.LessThan(minSupportedLabelExpressionVersion) {
return role, nil
}
// Make a shallow copy of the role so that we don't mutate the original.
// This is necessary because the role is shared
// between multiple clients sessions when notifying about changes in watchers.
// If we mutate the original role, it will be mutated for all clients
// which can cause panics since it causes a race condition.
role = apiutils.CloneProtoMsg(role)
hasLabelExpression := false
for _, kind := range types.LabelMatcherKinds {
allowLabelMatchers, err := role.GetLabelMatchers(types.Allow, kind)
Expand Down Expand Up @@ -2163,11 +2170,13 @@ func downgradeRoleToV6(r *types.RoleV6) (*types.RoleV6, bool, error) {
case types.V3, types.V4, types.V5, types.V6:
return r, false, nil
case types.V7:
var (
downgraded types.RoleV6
restricted bool
)
downgraded = *r
var restricted bool
// Make a shallow copy of the role so that we don't mutate the original.
// This is necessary because the role is shared
// between multiple clients sessions when notifying about changes in watchers.
// If we mutate the original role, it will be mutated for all clients
// which can cause panics since it causes a race condition.
downgraded := apiutils.CloneProtoMsg(r)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like quite an important change. We "probably" should add a test or at least a comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in a496294

downgraded.Version = types.V6

if len(downgraded.GetKubeResources(types.Deny)) > 0 {
Expand Down Expand Up @@ -2225,7 +2234,7 @@ func downgradeRoleToV6(r *types.RoleV6) (*types.RoleV6, bool, error) {
restricted = true
}

return &downgraded, restricted, nil
return downgraded, restricted, nil
default:
return nil, false, trace.BadParameter("unrecognized role version %T", r.Version)
}
Expand Down
22 changes: 18 additions & 4 deletions lib/auth/grpcserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ import (
"encoding/pem"
"fmt"
"io"
"maps"
"net"
"net/http"
"net/http/httptest"
"sort"
"testing"
"time"

"github.com/coreos/go-semver/semver"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
Expand Down Expand Up @@ -1808,10 +1810,9 @@ func TestIsMFARequired(t *testing.T) {

// If auth pref or role require session MFA, and MFA is not already
// verified according to private key policy, expect MFA required.
wantRequired :=
(role.GetOptions().RequireMFAType.IsSessionMFARequired() || authPref.GetRequireMFAType().IsSessionMFARequired()) &&
!role.GetPrivateKeyPolicy().MFAVerified() &&
!authPref.GetPrivateKeyPolicy().MFAVerified()
wantRequired := (role.GetOptions().RequireMFAType.IsSessionMFARequired() || authPref.GetRequireMFAType().IsSessionMFARequired()) &&
!role.GetPrivateKeyPolicy().MFAVerified() &&
!authPref.GetPrivateKeyPolicy().MFAVerified()
var wantMFARequired proto.MFARequired
if wantRequired {
wantMFARequired = proto.MFARequired_MFA_REQUIRED_YES
Expand Down Expand Up @@ -3930,8 +3931,12 @@ func TestRoleVersions(t *testing.T) {

wildcardLabels := types.Labels{types.Wildcard: {types.Wildcard}}

originalLabels := map[string]string{"env": "staging"}
newRole := func(version string, spec types.RoleSpecV6) types.Role {
role, err := types.NewRoleWithVersion("test_rule", version, spec)
meta := role.GetMetadata()
meta.Labels = maps.Clone(originalLabels)
role.SetMetadata(meta)
require.NoError(t, err)
return role
}
Expand Down Expand Up @@ -4150,6 +4155,15 @@ func TestRoleVersions(t *testing.T) {
require.NoError(t, err)
}
}

// Call maybeDowngrade directly to make sure the original
// role isn't modified
sv, err := semver.NewVersion(clientVersion)
if err == nil {
_, err := maybeDowngradeRoleToV6(ctx, role.(*types.RoleV6), sv)
require.NoError(t, err)
require.Equal(t, originalLabels, role.GetMetadata().Labels)
}
})
}
})
Expand Down