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

feat: add option to resolve groups for users in Keycloak #1925

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions service/entityresolution/keycloak/keycloak_entity_resolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type KeycloakConfig struct {
LegacyKeycloak bool `mapstructure:"legacykeycloak" json:"legacykeycloak" default:"false"`
SubGroups bool `mapstructure:"subgroups" json:"subgroups" default:"false"`
InferID InferredIdentityConfig `mapstructure:"inferid,omitempty" json:"inferid,omitempty"`
GetGroups bool `mapstructure:"getgroups" json:"getgroups" default:"false"`
}

func RegisterKeycloakERS(config serviceregistry.ServiceConfig, logger *logger.Logger) (*KeycloakEntityResolutionService, serviceregistry.HandlerServer) {
Expand Down Expand Up @@ -202,6 +203,21 @@ func EntityResolution(ctx context.Context,
logger.Debug("user found", slog.String("user", *user.ID), slog.String("entity", ident.String()))
logger.Debug("user", slog.Any("details", user))
logger.Debug("user", slog.Any("attributes", user.Attributes))

if kcConfig.GetGroups {
groups, err := connector.client.GetUserGroups(ctx, connector.token.AccessToken, kcConfig.Realm, *user.ID, gocloak.GetGroupsParams{})
if err != nil {
return entityresolution.ResolveEntitiesResponse{},
connect.NewError(connect.CodeInternal, ErrGetRetrievalFailed)
}

groupStrs := []string{}
for _, group := range groups {
groupStrs = append(groupStrs, *group.Name)
}
user.Groups = &groupStrs
}

keycloakEntities = append(keycloakEntities, user)
default:
logger.Error("no user found for", slog.Any("entity", ident))
Expand Down