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 ldap hardcoded group mapper and add new mappers to GetLdapUserFederationMappers #264

Merged
merged 2 commits into from
Apr 1, 2020
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
75 changes: 75 additions & 0 deletions keycloak/ldap_hardcoded_group_mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package keycloak

import "fmt"

type LdapHardcodedGroupMapper struct {
Id string
Name string
RealmId string
LdapUserFederationId string
Group string
}

func convertFromLdapHardcodedGroupMapperToComponent(ldapMapper *LdapHardcodedGroupMapper) *component {
return &component{
Id: ldapMapper.Id,
Name: ldapMapper.Name,
ProviderId: "hardcoded-ldap-group-mapper",
ProviderType: "org.keycloak.storage.ldap.mappers.LDAPStorageMapper",
ParentId: ldapMapper.LdapUserFederationId,

Config: map[string][]string{
"group": {
ldapMapper.Group,
},
},
}
}

func convertFromComponentToLdapHardcodedGroupMapper(component *component, realmId string) *LdapHardcodedGroupMapper {
return &LdapHardcodedGroupMapper{
Id: component.Id,
Name: component.Name,
RealmId: realmId,
LdapUserFederationId: component.ParentId,

Group: component.getConfig("group"),
}
}

func (keycloakClient *KeycloakClient) ValidateLdapHardcodedGroupMapper(ldapMapper *LdapHardcodedGroupMapper) error {
if len(ldapMapper.Group) == 0 {
return fmt.Errorf("validation error: hardcoded group name must not be empty")
}
return nil
}

func (keycloakClient *KeycloakClient) NewLdapHardcodedGroupMapper(ldapMapper *LdapHardcodedGroupMapper) error {
_, location, err := keycloakClient.post(fmt.Sprintf("/realms/%s/components", ldapMapper.RealmId), convertFromLdapHardcodedGroupMapperToComponent(ldapMapper))
if err != nil {
return err
}

ldapMapper.Id = getIdFromLocationHeader(location)

return nil
}

func (keycloakClient *KeycloakClient) GetLdapHardcodedGroupMapper(realmId, id string) (*LdapHardcodedGroupMapper, error) {
var component *component

err := keycloakClient.get(fmt.Sprintf("/realms/%s/components/%s", realmId, id), &component, nil)
if err != nil {
return nil, err
}

return convertFromComponentToLdapHardcodedGroupMapper(component, realmId), nil
}

func (keycloakClient *KeycloakClient) UpdateLdapHardcodedGroupMapper(ldapMapper *LdapHardcodedGroupMapper) error {
return keycloakClient.put(fmt.Sprintf("/realms/%s/components/%s", ldapMapper.RealmId, ldapMapper.Id), convertFromLdapHardcodedGroupMapperToComponent(ldapMapper))
}

func (keycloakClient *KeycloakClient) DeleteLdapHardcodedGroupMapper(realmId, id string) error {
return keycloakClient.delete(fmt.Sprintf("/realms/%s/components/%s", realmId, id), nil)
}
13 changes: 12 additions & 1 deletion keycloak/ldap_user_federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ func (keycloakClient *KeycloakClient) GetLdapUserFederationMappers(realmId, id s
if err != nil {
return nil, err
}

for _, component := range components {
switch component.ProviderId {
case "full-name-ldap-mapper":
Expand All @@ -347,6 +346,18 @@ func (keycloakClient *KeycloakClient) GetLdapUserFederationMappers(realmId, id s
return nil, err
}
ldapUserFederationMappers = append(ldapUserFederationMappers, mapper)
case "hardcoded-ldap-group-mapper":
mapper := convertFromComponentToLdapHardcodedGroupMapper(component, realmId)
ldapUserFederationMappers = append(ldapUserFederationMappers, mapper)
case "hardcoded-ldap-role-mapper":
mapper := convertFromComponentToLdapHardcodedRoleMapper(component, realmId)
ldapUserFederationMappers = append(ldapUserFederationMappers, mapper)
case "msad-lds-user-account-control-mapper":
mapper, err := convertFromComponentToLdapMsadLdsUserAccountControlMapper(component, realmId)
if err != nil {
return nil, err
}
ldapUserFederationMappers = append(ldapUserFederationMappers, mapper)
case "msad-user-account-control-mapper":
mapper, err := convertFromComponentToLdapMsadUserAccountControlMapper(component, realmId)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func KeycloakProvider() *schema.Provider {
"keycloak_ldap_user_attribute_mapper": resourceKeycloakLdapUserAttributeMapper(),
"keycloak_ldap_group_mapper": resourceKeycloakLdapGroupMapper(),
"keycloak_ldap_hardcoded_role_mapper": resourceKeycloakLdapHardcodedRoleMapper(),
"keycloak_ldap_hardcoded_group_mapper": resourceKeycloakLdapHardcodedGroupMapper(),
"keycloak_ldap_msad_user_account_control_mapper": resourceKeycloakLdapMsadUserAccountControlMapper(),
"keycloak_ldap_msad_lds_user_account_control_mapper": resourceKeycloakLdapMsadLdsUserAccountControlMapper(),
"keycloak_ldap_full_name_mapper": resourceKeycloakLdapFullNameMapper(),
Expand Down
127 changes: 127 additions & 0 deletions provider/resource_keycloak_ldap_hardcoded_group_mapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package provider

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/mrparkers/terraform-provider-keycloak/keycloak"
)

func resourceKeycloakLdapHardcodedGroupMapper() *schema.Resource {
return &schema.Resource{
Create: resourceKeycloakLdapHardcodedGroupMapperCreate,
Read: resourceKeycloakLdapHardcodedGroupMapperRead,
Update: resourceKeycloakLdapHardcodedGroupMapperUpdate,
Delete: resourceKeycloakLdapHardcodedGroupMapperDelete,
// This resource can be imported using {{realm}}/{{provider_id}}/{{mapper_id}}. The Provider and Mapper IDs are displayed in the GUI
Importer: &schema.ResourceImporter{
State: resourceKeycloakLdapGenericMapperImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Display name of the mapper when displayed in the console.",
},
"realm_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The realm in which the ldap user federation provider exists.",
},
"ldap_user_federation_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The ldap user federation provider to attach this mapper to.",
},
"group": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Group to grant to user.",
},
},
}
}

func getLdapHardcodedGroupMapperFromData(data *schema.ResourceData) *keycloak.LdapHardcodedGroupMapper {
return &keycloak.LdapHardcodedGroupMapper{
Id: data.Id(),
Name: data.Get("name").(string),
RealmId: data.Get("realm_id").(string),
LdapUserFederationId: data.Get("ldap_user_federation_id").(string),
Group: data.Get("group").(string),
}
}

func setLdapHardcodedGroupMapperData(data *schema.ResourceData, ldapMapper *keycloak.LdapHardcodedGroupMapper) {
data.SetId(ldapMapper.Id)
data.Set("name", ldapMapper.Name)
data.Set("realm_id", ldapMapper.RealmId)
data.Set("ldap_user_federation_id", ldapMapper.LdapUserFederationId)
data.Set("group", ldapMapper.Group)
}

func resourceKeycloakLdapHardcodedGroupMapperCreate(data *schema.ResourceData, meta interface{}) error {
keycloakClient := meta.(*keycloak.KeycloakClient)

ldapMapper := getLdapHardcodedGroupMapperFromData(data)

err := keycloakClient.ValidateLdapHardcodedGroupMapper(ldapMapper)
if err != nil {
return err
}

err = keycloakClient.NewLdapHardcodedGroupMapper(ldapMapper)
if err != nil {
return err
}

setLdapHardcodedGroupMapperData(data, ldapMapper)

return resourceKeycloakLdapHardcodedGroupMapperRead(data, meta)
}

func resourceKeycloakLdapHardcodedGroupMapperRead(data *schema.ResourceData, meta interface{}) error {
keycloakClient := meta.(*keycloak.KeycloakClient)

realmId := data.Get("realm_id").(string)
id := data.Id()

ldapMapper, err := keycloakClient.GetLdapHardcodedGroupMapper(realmId, id)
if err != nil {
return handleNotFoundError(err, data)
}

setLdapHardcodedGroupMapperData(data, ldapMapper)

return nil
}

func resourceKeycloakLdapHardcodedGroupMapperUpdate(data *schema.ResourceData, meta interface{}) error {
keycloakClient := meta.(*keycloak.KeycloakClient)

ldapMapper := getLdapHardcodedGroupMapperFromData(data)

err := keycloakClient.ValidateLdapHardcodedGroupMapper(ldapMapper)
if err != nil {
return err
}

err = keycloakClient.UpdateLdapHardcodedGroupMapper(ldapMapper)
if err != nil {
return err
}

setLdapHardcodedGroupMapperData(data, ldapMapper)

return nil
}

func resourceKeycloakLdapHardcodedGroupMapperDelete(data *schema.ResourceData, meta interface{}) error {
keycloakClient := meta.(*keycloak.KeycloakClient)

realmId := data.Get("realm_id").(string)
id := data.Id()

return keycloakClient.DeleteLdapHardcodedGroupMapper(realmId, id)
}
Loading