Skip to content

Commit

Permalink
Merge pull request #6 from JameelB/INTLY-6430
Browse files Browse the repository at this point in the history
KEYCLOAK-13018: ensure state contains the correct user
  • Loading branch information
pb82 authored Mar 24, 2020
2 parents c6e3f01 + 7fe58dc commit da7bd65
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 90 deletions.
84 changes: 39 additions & 45 deletions pkg/common/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ type Client struct {
type T interface{}

// Generic create function for creating new Keycloak resources
func (c *Client) create(obj T, resourcePath, resourceName string) error {
func (c *Client) create(obj T, resourcePath, resourceName string) (string, error) {
jsonValue, err := json.Marshal(obj)
if err != nil {
logrus.Errorf("error %+v marshalling object", err)
return nil
return "", nil
}

req, err := http.NewRequest(
Expand All @@ -52,7 +52,7 @@ func (c *Client) create(obj T, resourcePath, resourceName string) error {
)
if err != nil {
logrus.Errorf("error creating POST %s request %+v", resourceName, err)
return errors.Wrapf(err, "error creating POST %s request", resourceName)
return "", errors.Wrapf(err, "error creating POST %s request", resourceName)
}

req.Header.Set("Content-Type", "application/json")
Expand All @@ -61,35 +61,37 @@ func (c *Client) create(obj T, resourcePath, resourceName string) error {

if err != nil {
logrus.Errorf("error on request %+v", err)
return errors.Wrapf(err, "error performing POST %s request", resourceName)
return "", errors.Wrapf(err, "error performing POST %s request", resourceName)
}
defer res.Body.Close()

if res.StatusCode != 201 && res.StatusCode != 204 {
return fmt.Errorf("failed to create %s: (%d) %s", resourceName, res.StatusCode, res.Status)
return "", fmt.Errorf("failed to create %s: (%d) %s", resourceName, res.StatusCode, res.Status)
}

if resourceName == "client" {
d, _ := ioutil.ReadAll(res.Body)
fmt.Println("user response ", string(d))
}

return nil
location := strings.Split(res.Header.Get("Location"), "/")
uid := location[len(location)-1]
return uid, nil
}

func (c *Client) CreateRealm(realm *v1alpha1.KeycloakRealm) error {
func (c *Client) CreateRealm(realm *v1alpha1.KeycloakRealm) (string, error) {
return c.create(realm.Spec.Realm, "realms", "realm")
}

func (c *Client) CreateClient(client *v1alpha1.KeycloakAPIClient, realmName string) error {
func (c *Client) CreateClient(client *v1alpha1.KeycloakAPIClient, realmName string) (string, error) {
return c.create(client, fmt.Sprintf("realms/%s/clients", realmName), "client")
}

func (c *Client) CreateUser(user *v1alpha1.KeycloakAPIUser, realmName string) error {
func (c *Client) CreateUser(user *v1alpha1.KeycloakAPIUser, realmName string) (string, error) {
return c.create(user, fmt.Sprintf("realms/%s/users", realmName), "user")
}

func (c *Client) CreateFederatedIdentity(fid v1alpha1.FederatedIdentity, userID string, realmName string) error {
func (c *Client) CreateFederatedIdentity(fid v1alpha1.FederatedIdentity, userID string, realmName string) (string, error) {
return c.create(fid, fmt.Sprintf("realms/%s/users/%s/federated-identity/%s", realmName, userID, fid.IdentityProvider), "federated-identity")
}

Expand All @@ -109,22 +111,22 @@ func (c *Client) GetUserFederatedIdentities(userID string, realmName string) ([]
return result.([]v1alpha1.FederatedIdentity), err
}

func (c *Client) CreateUserClientRole(role *v1alpha1.KeycloakUserRole, realmName, clientID, userID string) error {
func (c *Client) CreateUserClientRole(role *v1alpha1.KeycloakUserRole, realmName, clientID, userID string) (string, error) {
return c.create(
[]*v1alpha1.KeycloakUserRole{role},
fmt.Sprintf("realms/%s/users/%s/role-mappings/clients/%s", realmName, userID, clientID),
"user-client-role",
)
}
func (c *Client) CreateUserRealmRole(role *v1alpha1.KeycloakUserRole, realmName, userID string) error {
func (c *Client) CreateUserRealmRole(role *v1alpha1.KeycloakUserRole, realmName, userID string) (string, error) {
return c.create(
[]*v1alpha1.KeycloakUserRole{role},
fmt.Sprintf("realms/%s/users/%s/role-mappings/realm", realmName, userID),
"user-realm-role",
)
}

func (c *Client) CreateAuthenticatorConfig(authenticatorConfig *v1alpha1.AuthenticatorConfig, realmName, executionID string) error {
func (c *Client) CreateAuthenticatorConfig(authenticatorConfig *v1alpha1.AuthenticatorConfig, realmName, executionID string) (string, error) {
return c.create(authenticatorConfig, fmt.Sprintf("realms/%s/authentication/executions/%s/config", realmName, executionID), "AuthenticatorConfig")
}

Expand Down Expand Up @@ -179,15 +181,18 @@ func (c *Client) FindUserByEmail(email, realm string) (*v1alpha1.KeycloakAPIUser
}

func (c *Client) FindUserByUsername(name, realm string) (*v1alpha1.KeycloakAPIUser, error) {
result, err := c.get(fmt.Sprintf("realms/%s/users?first=0&max=1&search=%s", realm, name), "user", func(body []byte) (T, error) {
result, err := c.get(fmt.Sprintf("realms/%s/users?username=%s&max=-1", realm, name), "user", func(body []byte) (T, error) {
var users []*v1alpha1.KeycloakAPIUser
if err := json.Unmarshal(body, &users); err != nil {
return nil, err
}
if len(users) == 0 {
return nil, errors.New("not found")

for _, user := range users {
if user.UserName == name {
return user, nil
}
}
return users[0], nil
return nil, errors.New("not found")
})
if err != nil {
return nil, err
Expand All @@ -198,9 +203,8 @@ func (c *Client) FindUserByUsername(name, realm string) (*v1alpha1.KeycloakAPIUs
return result.(*v1alpha1.KeycloakAPIUser), nil
}

func (c *Client) CreateIdentityProvider(identityProvider *v1alpha1.KeycloakIdentityProvider, realmName string) error {
err := c.create(identityProvider, fmt.Sprintf("realms/%s/identity-provider/instances", realmName), "identity provider")
return err
func (c *Client) CreateIdentityProvider(identityProvider *v1alpha1.KeycloakIdentityProvider, realmName string) (string, error) {
return c.create(identityProvider, fmt.Sprintf("realms/%s/identity-provider/instances", realmName), "identity provider")
}

// Generic get function for returning a Keycloak resource
Expand Down Expand Up @@ -738,18 +742,7 @@ func (c *Client) CreateGroup(groupName string, realmName string) (string, error)
}

// Create the new group
err := c.create(group, fmt.Sprintf("realms/%s/groups", realmName), "group")
if err != nil {
return "", err
}

createdGroup, err := c.FindGroupByName(groupName, realmName)

if err != nil {
return "", err
}

return createdGroup.ID, nil
return c.create(group, fmt.Sprintf("realms/%s/groups", realmName), "group")
}

func (c *Client) MakeGroupDefault(groupID string, realmName string) error {
Expand Down Expand Up @@ -811,14 +804,15 @@ func (c *Client) SetGroupChild(groupID, realmName string, childGroup *Group) err
}

// Otherwise, set the child group
return c.create(
_, err = c.create(
childGroup,
fmt.Sprintf("realms/%s/groups/%s/children", realmName, groupID),
"group-child",
)
return err
}

func (c *Client) CreateGroupClientRole(role *v1alpha1.KeycloakUserRole, realmName, clientID, groupID string) error {
func (c *Client) CreateGroupClientRole(role *v1alpha1.KeycloakUserRole, realmName, clientID, groupID string) (string, error) {
return c.create(
[]*v1alpha1.KeycloakUserRole{role},
fmt.Sprintf("realms/%s/groups/%s/role-mappings/clients/%s", realmName, groupID, clientID),
Expand Down Expand Up @@ -890,7 +884,7 @@ func (c *Client) FindGroupClientRole(realmName, clientID, groupID string, predic
return nil, nil
}

func (c *Client) CreateGroupRealmRole(role *v1alpha1.KeycloakUserRole, realmName, groupID string) error {
func (c *Client) CreateGroupRealmRole(role *v1alpha1.KeycloakUserRole, realmName, groupID string) (string, error) {
return c.create(
[]*v1alpha1.KeycloakUserRole{role},
fmt.Sprintf("realms/%s/groups/%s/role-mappings/realm", realmName, groupID),
Expand Down Expand Up @@ -1013,22 +1007,22 @@ func defaultRequester() Requester {
type KeycloakInterface interface {
Ping() error

CreateRealm(realm *v1alpha1.KeycloakRealm) error
CreateRealm(realm *v1alpha1.KeycloakRealm) (string, error)
GetRealm(realmName string) (*v1alpha1.KeycloakRealm, error)
UpdateRealm(specRealm *v1alpha1.KeycloakRealm) error
DeleteRealm(realmName string) error
ListRealms() ([]*v1alpha1.KeycloakAPIRealm, error)

CreateClient(client *v1alpha1.KeycloakAPIClient, realmName string) error
CreateClient(client *v1alpha1.KeycloakAPIClient, realmName string) (string, error)
GetClient(clientID, realmName string) (*v1alpha1.KeycloakAPIClient, error)
GetClientSecret(clientID, realmName string) (string, error)
GetClientInstall(clientID, realmName string) ([]byte, error)
UpdateClient(specClient *v1alpha1.KeycloakAPIClient, realmName string) error
DeleteClient(clientID, realmName string) error
ListClients(realmName string) ([]*v1alpha1.KeycloakAPIClient, error)

CreateUser(user *v1alpha1.KeycloakAPIUser, realmName string) error
CreateFederatedIdentity(fid v1alpha1.FederatedIdentity, userID string, realmName string) error
CreateUser(user *v1alpha1.KeycloakAPIUser, realmName string) (string, error)
CreateFederatedIdentity(fid v1alpha1.FederatedIdentity, userID string, realmName string) (string, error)
RemoveFederatedIdentity(fid v1alpha1.FederatedIdentity, userID string, realmName string) error
GetUserFederatedIdentities(userName string, realmName string) ([]v1alpha1.FederatedIdentity, error)
UpdatePassword(user *v1alpha1.KeycloakAPIUser, realmName, newPass string) error
Expand All @@ -1048,28 +1042,28 @@ type KeycloakInterface interface {
ListDefaultGroups(realmName string) ([]*Group, error)
SetGroupChild(groupID, realmName string, childGroup *Group) error

CreateGroupClientRole(role *v1alpha1.KeycloakUserRole, realmName, clientID, groupID string) error
CreateGroupClientRole(role *v1alpha1.KeycloakUserRole, realmName, clientID, groupID string) (string, error)
ListGroupClientRoles(realmName, clientID, groupID string) ([]*v1alpha1.KeycloakUserRole, error)
FindGroupClientRole(realmName, clientID, groupID string, predicate func(*v1alpha1.KeycloakUserRole) bool) (*v1alpha1.KeycloakUserRole, error)
ListAvailableGroupClientRoles(realmName, clientID, groupID string) ([]*v1alpha1.KeycloakUserRole, error)
FindAvailableGroupClientRole(realmName, clientID, groupID string, predicate func(*v1alpha1.KeycloakUserRole) bool) (*v1alpha1.KeycloakUserRole, error)

CreateGroupRealmRole(role *v1alpha1.KeycloakUserRole, realmName, groupID string) error
CreateGroupRealmRole(role *v1alpha1.KeycloakUserRole, realmName, groupID string) (string, error)
ListGroupRealmRoles(realmName, groupID string) ([]*v1alpha1.KeycloakUserRole, error)
ListAvailableGroupRealmRoles(realmName, groupID string) ([]*v1alpha1.KeycloakUserRole, error)

CreateIdentityProvider(identityProvider *v1alpha1.KeycloakIdentityProvider, realmName string) error
CreateIdentityProvider(identityProvider *v1alpha1.KeycloakIdentityProvider, realmName string) (string, error)
GetIdentityProvider(alias, realmName string) (*v1alpha1.KeycloakIdentityProvider, error)
UpdateIdentityProvider(specIdentityProvider *v1alpha1.KeycloakIdentityProvider, realmName string) error
DeleteIdentityProvider(alias, realmName string) error
ListIdentityProviders(realmName string) ([]*v1alpha1.KeycloakIdentityProvider, error)

CreateUserClientRole(role *v1alpha1.KeycloakUserRole, realmName, clientID, userID string) error
CreateUserClientRole(role *v1alpha1.KeycloakUserRole, realmName, clientID, userID string) (string, error)
ListUserClientRoles(realmName, clientID, userID string) ([]*v1alpha1.KeycloakUserRole, error)
ListAvailableUserClientRoles(realmName, clientID, userID string) ([]*v1alpha1.KeycloakUserRole, error)
DeleteUserClientRole(role *v1alpha1.KeycloakUserRole, realmName, clientID, userID string) error

CreateUserRealmRole(role *v1alpha1.KeycloakUserRole, realmName, userID string) error
CreateUserRealmRole(role *v1alpha1.KeycloakUserRole, realmName, userID string) (string, error)
ListUserRealmRoles(realmName, userID string) ([]*v1alpha1.KeycloakUserRole, error)
ListAvailableUserRealmRoles(realmName, userID string) ([]*v1alpha1.KeycloakUserRole, error)
DeleteUserRealmRole(role *v1alpha1.KeycloakUserRole, realmName, userID string) error
Expand All @@ -1078,7 +1072,7 @@ type KeycloakInterface interface {
FindAuthenticationExecutionForFlow(flowAlias, realmName string, predicate func(*v1alpha1.AuthenticationExecutionInfo) bool) (*v1alpha1.AuthenticationExecutionInfo, error)
UpdateAuthenticationExecutionForFlow(flowAlias, realmName string, execution *v1alpha1.AuthenticationExecutionInfo) error

CreateAuthenticatorConfig(authenticatorConfig *v1alpha1.AuthenticatorConfig, realmName, executionID string) error
CreateAuthenticatorConfig(authenticatorConfig *v1alpha1.AuthenticatorConfig, realmName, executionID string) (string, error)
GetAuthenticatorConfig(configID, realmName string) (*v1alpha1.AuthenticatorConfig, error)
UpdateAuthenticatorConfig(authenticatorConfig *v1alpha1.AuthenticatorConfig, realmName string) error
DeleteAuthenticatorConfig(configID, realmName string) error
Expand Down
Loading

0 comments on commit da7bd65

Please sign in to comment.