Uploaded image for project: 'Keycloak'
  1. Keycloak
  2. KEYCLOAK-7322

Remove group from group policy always failed with NPE

    Details

      Description

      I'm using the admin-client to remove a group from group policy but it always fail with NPE. Here is my code:

      try
      {
      RealmResource realm = getRealm(curRealmName);
      ClientResource client = getClient(realm, curClientName);

      //check if a GROUP policy is available in the given permission
      ScopePermissionResource permRes = client.authorization().permissions().scope().findById(permissionUuid);
      List<PolicyRepresentation> associatedPolicies = permRes.associatedPolicies();
      List<PolicyRepresentation> groupPolicies = associatedPolicies
      .stream()
      .filter(p -> p.getType().equals("group"))
      .collect(Collectors.toList());
      if(groupPolicies == null || groupPolicies.isEmpty()) //no GROUP policy exists for this permission, need to create one

      { throw new WebApplicationException("Group policy not found on permission " + permissionUuid); }

      else if(groupPolicies.size() == 1)//There is an existing GROUP policy for this permission, just need to add the given group to this GROUP policy

      { GroupRepresentation group = realm.groups().group(groupUuid).toRepresentation(); String policyUuid = groupPolicies.get(0).getId(); GroupPolicyResource polRes = client.authorization().policies().group().findById(policyUuid); GroupPolicyRepresentation policy = polRes.toRepresentation(); policy.removeGroup(group.getPath()); //TODO: not sure why it always fail here with NPE polRes.update(policy); }

      else

      { throw new WebApplicationException("multiple Group policies found in permission " + permissionUuid); }

      }
      catch(RuntimeException e)

      { throw new WebApplicationException("Cannot complete removing permission " + permissionUuid + " from group " + groupUuid, e); }

      Stack trace:
      Exception in thread "main" javax.ws.rs.WebApplicationException: Cannot complete removing permission 2bd9d898-e904-4201-9e49-87d630ab0185 from group 95ad03a3-d1dd-4ad3-bba6-ba34c2599fd3
      at com.castortech.util.keycloak.KeycloakAdminBroker.removePermissionFromGroup(KeycloakAdminBroker.java:1531)
      at com.castortech.util.keycloak.test.KeycloakClientTest.main(KeycloakClientTest.java:155)
      Caused by: java.lang.NullPointerException
      at org.keycloak.representations.idm.authorization.GroupPolicyRepresentation.removeGroup(GroupPolicyRepresentation.java:82)
      at com.castortech.util.keycloak.KeycloakAdminBroker.removePermissionFromGroup(KeycloakAdminBroker.java:1521)
      ... 1 more

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                pcraveiro Pedro Igor
                Reporter:
                ntle Nhut Thai Le
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: