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 aggregate attributes flag to keycloak_saml_user_attribute_protocol_mapper (#942) #943

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/resources/saml_user_attribute_protocol_mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ resource "keycloak_saml_user_attribute_protocol_mapper" "saml_user_attribute_map
- `client_id` - (Optional) The client this protocol mapper should be attached to. Conflicts with `client_scope_id`. One of `client_id` or `client_scope_id` must be specified.
- `client_scope_id` - (Optional) The client scope this protocol mapper should be attached to. Conflicts with `client_id`. One of `client_id` or `client_scope_id` must be specified.
- `friendly_name` - (Optional) An optional human-friendly name for this attribute.
- `aggregate_attributes`- (Optional) Indicates whether this attribute is a single value or an array of values. Defaults to `false`.

## Import

Expand Down
39 changes: 24 additions & 15 deletions keycloak/saml_user_attribute_protocol_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keycloak
import (
"context"
"fmt"
"strconv"
)

type SamlUserAttributeProtocolMapper struct {
Expand All @@ -12,10 +13,11 @@ type SamlUserAttributeProtocolMapper struct {
ClientId string
ClientScopeId string

UserAttribute string
FriendlyName string
SamlAttributeName string
SamlAttributeNameFormat string
UserAttribute string
FriendlyName string
SamlAttributeName string
SamlAttributeNameFormat string
AggregateAttributeValues bool
}

func (mapper *SamlUserAttributeProtocolMapper) convertToGenericProtocolMapper() *protocolMapper {
Expand All @@ -25,27 +27,34 @@ func (mapper *SamlUserAttributeProtocolMapper) convertToGenericProtocolMapper()
Protocol: "saml",
ProtocolMapper: "saml-user-attribute-mapper",
Config: map[string]string{
attributeNameField: mapper.SamlAttributeName,
attributeNameFormatField: mapper.SamlAttributeNameFormat,
friendlyNameField: mapper.FriendlyName,
userAttributeField: mapper.UserAttribute,
attributeNameField: mapper.SamlAttributeName,
attributeNameFormatField: mapper.SamlAttributeNameFormat,
friendlyNameField: mapper.FriendlyName,
userAttributeField: mapper.UserAttribute,
aggregateAttributeValuesField: strconv.FormatBool(mapper.AggregateAttributeValues),
},
}
}

func (protocolMapper *protocolMapper) convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId string) *SamlUserAttributeProtocolMapper {
func (protocolMapper *protocolMapper) convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId string) (*SamlUserAttributeProtocolMapper, error) {
aggregateAttributeValues, err := parseBoolAndTreatEmptyStringAsFalse(protocolMapper.Config[addToAccessTokenField])
if err != nil {
return nil, err
}

return &SamlUserAttributeProtocolMapper{
Id: protocolMapper.Id,
Name: protocolMapper.Name,
RealmId: realmId,
ClientId: clientId,
ClientScopeId: clientScopeId,

UserAttribute: protocolMapper.Config[userAttributeField],
FriendlyName: protocolMapper.Config[friendlyNameField],
SamlAttributeName: protocolMapper.Config[attributeNameField],
SamlAttributeNameFormat: protocolMapper.Config[attributeNameFormatField],
}
UserAttribute: protocolMapper.Config[userAttributeField],
FriendlyName: protocolMapper.Config[friendlyNameField],
SamlAttributeName: protocolMapper.Config[attributeNameField],
SamlAttributeNameFormat: protocolMapper.Config[attributeNameFormatField],
AggregateAttributeValues: aggregateAttributeValues,
}, nil
}

func (keycloakClient *KeycloakClient) GetSamlUserAttributeProtocolMapper(ctx context.Context, realmId, clientId, clientScopeId, mapperId string) (*SamlUserAttributeProtocolMapper, error) {
Expand All @@ -56,7 +65,7 @@ func (keycloakClient *KeycloakClient) GetSamlUserAttributeProtocolMapper(ctx con
return nil, err
}

return protocolMapper.convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId), nil
return protocolMapper.convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId)
}

func (keycloakClient *KeycloakClient) DeleteSamlUserAttributeProtocolMapper(ctx context.Context, realmId, clientId, clientScopeId, mapperId string) error {
Expand Down
16 changes: 12 additions & 4 deletions provider/resource_keycloak_saml_user_attribute_protocol_mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func resourceKeycloakSamlUserAttributeProtocolMapper() *schema.Resource {
Required: true,
ValidateFunc: validation.StringInSlice(keycloakSamlUserAttributeProtocolMapperNameFormats, false),
},
"aggregate_attributes": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Indicates if attribute values should be aggregated within the group attributes",
},
},
}
}
Expand All @@ -74,10 +80,11 @@ func mapFromDataToSamlUserAttributeProtocolMapper(data *schema.ResourceData) *ke
ClientId: data.Get("client_id").(string),
ClientScopeId: data.Get("client_scope_id").(string),

UserAttribute: data.Get("user_attribute").(string),
FriendlyName: data.Get("friendly_name").(string),
SamlAttributeName: data.Get("saml_attribute_name").(string),
SamlAttributeNameFormat: data.Get("saml_attribute_name_format").(string),
UserAttribute: data.Get("user_attribute").(string),
FriendlyName: data.Get("friendly_name").(string),
SamlAttributeName: data.Get("saml_attribute_name").(string),
SamlAttributeNameFormat: data.Get("saml_attribute_name_format").(string),
AggregateAttributeValues: data.Get("aggregate_attributes").(bool),
}
}

Expand All @@ -96,6 +103,7 @@ func mapFromSamlUserAttributeMapperToData(mapper *keycloak.SamlUserAttributeProt
data.Set("friendly_name", mapper.FriendlyName)
data.Set("saml_attribute_name", mapper.SamlAttributeName)
data.Set("saml_attribute_name_format", mapper.SamlAttributeNameFormat)
data.Set("aggregate_attributes", mapper.AggregateAttributeValues)
}

func resourceKeycloakSamlUserAttributeProtocolMapperCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
Loading