You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How to create a policy with enabled fetchRoles using keycloak-admin-client?
#35480
I have the same problem as you , but I'm in the 26.0.5.
I'm programming a backend in go and the library that I use gocloak have the same fields that the one you put.
So I take a look to the gui how is doing it an figure it out that the problem it was that the library was calling to
/admin/realms/realmID/clients/clientID/authz/resource-server/policy
In the gui it was calling to
/admin/realms/%s/clients/%s/authz/resource-server/policy/role
Plus that change in the url the fetchRole they put it as a field of the policy instead of in the config.
So I end creating a function that call directly the endpoint here you have it just in case you want to use it
type Role struct {
ID string `json:"id"`
Required bool `json:"required"`
}
type Policy struct {
Name string `json:"name"`
Description string `json:"description"`
Roles []Role `json:"roles"`
FetchRoles bool `json:"fetchRoles"`
Logic string `json:"logic"`
}
func CreateRolePolicy(ctx context.Context,url, token, tenantID, clientID string, p Policy) (*string, error) {
baseURL := url+"/admin/realms/%s/clients/%s/authz/resource-server/policy/role"
endpoint := fmt.Sprintf(baseURL, tenantID, clientID)
jsonPayload, err := json.Marshal(p)
if err != nil {
return nil, fmt.Errorf("failed to marshal policy payload: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("failed to create policy. Status: %d, Body: %s", resp.StatusCode, string(body))
}
var result struct {
ID string `json:"id"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &result.ID, nil
}
Maybe its a mistake with ts typings & rest api docs > PolicyRepresentation because in the admin UI sends a fetchRoles attribute when creating role policies and if you pass fetchRoles using @ts-expect-error to bypass the type check, it works correctly
awaitkcAdminClient.clients.createPolicy({id: clientUuid,type: "role",realm: params.realm},{
...stuff,// @ts-expect-error -- ..........fetchRoles: true,});// works fine
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Keycloak/keycloak-admin-client version: 25.0.6
Hi! Is setting
fetchRolestotruepossible?this doesn't have it, and I've tried with
configlike this:which results in:
Thanks in advance!
All reactions
Replies: 2 comments
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I have the same problem as you , but I'm in the 26.0.5.
I'm programming a backend in go and the library that I use gocloak have the same fields that the one you put.
So I take a look to the gui how is doing it an figure it out that the problem it was that the library was calling to
/admin/realms/realmID/clients/clientID/authz/resource-server/policy
In the gui it was calling to
/admin/realms/%s/clients/%s/authz/resource-server/policy/role
Plus that change in the url the fetchRole they put it as a field of the policy instead of in the config.
So I end creating a function that call directly the endpoint here you have it just in case you want to use it
With this function everything works fine
All reactions
Uh oh!
There was an error while loading. Please reload this page.
Maybe its a mistake with ts typings & rest api docs > PolicyRepresentation because in the admin UI sends a fetchRoles attribute when creating role policies and if you pass fetchRoles using @ts-expect-error to bypass the type check, it works correctly
All reactions