Improving LDAP group handling and REST service

This commit is contained in:
Martin Stockhammer 2020-07-07 08:46:22 +02:00
parent 023d88d7b0
commit e9ea418911
11 changed files with 200 additions and 18 deletions

View File

@ -20,6 +20,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
@ -30,8 +31,8 @@
public class LdapGroup
{
String dn = "";
String name;
String description;
String name = "";
String description = "";
List<String> memberList;
public LdapGroup( )
@ -92,6 +93,9 @@ public void setMemberList( Collection<String> memberList) {
}
public List<String> getMemberList() {
if (memberList==null) {
return Collections.EMPTY_LIST;
}
return memberList;
}

View File

@ -24,6 +24,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
/**
* will map ldap group to redback role

View File

@ -53,6 +53,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author Olivier Lamy
@ -308,6 +309,21 @@ public void getAllGroups()
"internal-repo-manager" );
}
@Test
public void getAllGroupObjects()
throws Exception
{
List<LdapGroup> allGroups = ldapRoleMapper.getAllGroupObjects( getDirContext() );
log.info( "allGroups: {}", allGroups );
assertThat( allGroups ).isNotNull( ).isNotEmpty( );
assertThat( allGroups.stream().map(group -> group.getName()).collect( Collectors.toList()) ).contains( "archiva/group-with-slash", "archiva-admin",
"internal-repo-manager" );
assertThat( allGroups.stream().map(group -> group.getDn()).collect( Collectors.toList()) ).contains( "cn=archiva/group-with-slash,"+groupSuffix, "cn=archiva-admin,"+groupSuffix,
"cn=internal-repo-manager,"+groupSuffix );
}
@Test
public void getGroupsMember()
throws Exception

View File

@ -21,6 +21,7 @@
import javax.xml.bind.annotation.XmlRootElement;
import java.io.Serializable;
import java.util.Collection;
import java.util.List;
/**
* @author Olivier Lamy
@ -32,14 +33,14 @@ public class GroupMapping
{
private String group;
private Collection<String> roleNames;
private List<String> roleNames;
public GroupMapping()
{
// no op
}
public GroupMapping( String group, Collection<String> roleNames )
public GroupMapping( String group, List<String> roleNames )
{
this.group = group;
this.roleNames = roleNames;
@ -60,7 +61,7 @@ public Collection<String> getRoleNames()
return roleNames;
}
public void setRoleNames( Collection<String> roleNames )
public void setRoleNames( List<String> roleNames )
{
this.roleNames = roleNames;
}

View File

@ -0,0 +1,69 @@
package org.apache.archiva.redback.rest.api.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
@XmlRootElement(name="pagedResult")
public class PagedResult<T>
{
PaginationInfo pagination;
List<T> data;
public PagedResult() {
}
public PagedResult( long totalCount, long offset, long limit, List<T> data ) {
this.data = data;
this.pagination = new PaginationInfo( totalCount, offset, limit );
}
public static final <T> PagedResult<T> ofAllElements(long offset, long limit, List<T> elements) {
return new PagedResult( elements.size( ), offset, limit, elements.subList( (int)offset, (int)offset + (int)limit ) );
}
public static final <T> PagedResult<T> ofSegment(long totalSize, long offset, long limit, List<T> elements) {
return new PagedResult( totalSize, offset, limit, elements);
}
public List<T> getData( )
{
return data;
}
public void setData( List<T> data )
{
this.data = data;
}
public PaginationInfo getPagination( )
{
return pagination;
}
public void setPagination( PaginationInfo pagination )
{
this.pagination = pagination;
}
}

View File

@ -0,0 +1,75 @@
package org.apache.archiva.redback.rest.api.model;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
@XmlRootElement(name="pagination")
public class PaginationInfo
{
long totalCount;
long offset;
long limit;
public PaginationInfo() {
}
public PaginationInfo( long totalCount, long offset, long limit )
{
this.totalCount = totalCount;
this.offset = offset;
this.limit = limit;
}
@XmlElement(name="total_count")
public long getTotalCount( )
{
return totalCount;
}
public void setTotalCount( long totalCount )
{
this.totalCount = totalCount;
}
public long getOffset( )
{
return offset;
}
public void setOffset( long offset )
{
this.offset = offset;
}
public long getLimit( )
{
return limit;
}
public void setLimit( long limit )
{
this.limit = limit;
}
}

View File

@ -18,6 +18,7 @@
* under the License.
*/
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.archiva.redback.authorization.RedbackAuthorization;
import org.apache.archiva.redback.integration.security.role.RedbackRoleConstants;
@ -46,14 +47,17 @@
@Deprecated
public interface LdapGroupMappingService
{
@Path("ldapGroups")
@Operation( deprecated = true )
@Path( "ldapGroups" )
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization(permissions = RedbackRoleConstants.CONFIGURATION_EDIT_OPERATION)
StringList getLdapGroups()
@Produces( {MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML} )
@RedbackAuthorization( permissions = RedbackRoleConstants.CONFIGURATION_EDIT_OPERATION )
StringList getLdapGroups( )
throws RedbackServiceException;
@Operation( deprecated = true )
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@RedbackAuthorization(permissions = RedbackRoleConstants.CONFIGURATION_EDIT_OPERATION)
@ -61,6 +65,7 @@ List<LdapGroupMapping> getLdapGroupMappings()
throws RedbackServiceException;
@Operation( deprecated = true )
@PUT
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@ -68,6 +73,7 @@ List<LdapGroupMapping> getLdapGroupMappings()
ActionStatus addLdapGroupMapping( LdapGroupMapping ldapGroupMapping )
throws RedbackServiceException;
@Operation( deprecated = true )
@DELETE
@Path("{group}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@ -76,6 +82,7 @@ ActionStatus addLdapGroupMapping( LdapGroupMapping ldapGroupMapping )
ActionStatus removeLdapGroupMapping( @PathParam("group") String group )
throws RedbackServiceException;
@Operation( deprecated = true )
@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

View File

@ -28,6 +28,7 @@
import org.apache.archiva.redback.rest.api.model.Group;
import org.apache.archiva.redback.rest.api.model.GroupMapping;
import org.apache.archiva.redback.rest.api.model.GroupMappingUpdateRequest;
import org.apache.archiva.redback.rest.api.model.PagedResult;
import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
import javax.ws.rs.Consumes;
@ -61,8 +62,8 @@ public interface GroupService
@ApiResponse( description = "List of group objects. The number of returned results depend on the pagination parameters offset and limit." )
}
)
List<Group> getGroups( @QueryParam( "offset" ) @DefaultValue( "0" ) Long offset,
@QueryParam( "limit" ) @DefaultValue( value = Long.MAX_VALUE+"" ) Long limit)
PagedResult<Group> getGroups( @QueryParam( "offset" ) @DefaultValue( "0" ) Long offset,
@QueryParam( "limit" ) @DefaultValue( value = Long.MAX_VALUE+"" ) Long limit)
throws RedbackServiceException;

View File

@ -29,7 +29,7 @@
import org.apache.archiva.redback.rest.api.model.Group;
import org.apache.archiva.redback.rest.api.model.GroupMapping;
import org.apache.archiva.redback.rest.api.model.GroupMappingUpdateRequest;
import org.apache.archiva.redback.rest.api.model.StringList;
import org.apache.archiva.redback.rest.api.model.PagedResult;
import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
import org.apache.archiva.redback.rest.api.services.v2.GroupService;
import org.slf4j.Logger;
@ -82,7 +82,7 @@ private static final Group getGroupFromLdap( LdapGroup ldapGroup ) {
}
@Override
public List<Group> getGroups( Long offset, Long limit ) throws RedbackServiceException
public PagedResult<Group> getGroups( Long offset, Long limit ) throws RedbackServiceException
{
LdapConnection ldapConnection = null;
@ -92,7 +92,8 @@ public List<Group> getGroups( Long offset, Long limit ) throws RedbackServiceExc
{
ldapConnection = ldapConnectionFactory.getConnection();
context = ldapConnection.getDirContext();
return ldapRoleMapper.getAllGroupObjects( context ).stream( ).skip( offset ).limit( limit ).map( DefaultGroupService::getGroupFromLdap ).collect( Collectors.toList( ) );
List<LdapGroup> groups = ldapRoleMapper.getAllGroupObjects( context );
return PagedResult.ofSegment( groups.size( ), offset, limit, groups.stream( ).skip( offset ).limit( limit ).map( DefaultGroupService::getGroupFromLdap ).collect( Collectors.toList( ) ) );
}
catch ( LdapException | MappingException e )
{
@ -116,7 +117,7 @@ public List<GroupMapping> getGroupMappings()
List<GroupMapping> ldapGroupMappings = new ArrayList<>( map.size( ) );
for ( Map.Entry<String, Collection<String>> entry : map.entrySet() )
{
GroupMapping ldapGroupMapping = new GroupMapping( entry.getKey(), entry.getValue() );
GroupMapping ldapGroupMapping = new GroupMapping( entry.getKey( ), new ArrayList<>( entry.getValue( ) ) );
ldapGroupMappings.add( ldapGroupMapping );
}

View File

@ -47,8 +47,15 @@
<property name="mapper" ref="redbackJacksonXMLMapper"/>
</bean>
<bean id="redbackJacksonJsonMapper" class="com.fasterxml.jackson.databind.ObjectMapper" />
<bean id="redbackJacksonXMLMapper" class="com.fasterxml.jackson.dataformat.xml.XmlMapper" />
<bean id="redbackJacksonJsonMapper" class="com.fasterxml.jackson.databind.ObjectMapper" >
<property name="annotationIntrospector" ref="jacksonAnnotationIntrospector"/>
</bean>
<bean id="redbackJacksonXMLMapper" class="com.fasterxml.jackson.dataformat.xml.XmlMapper" >
<property name="annotationIntrospector" ref="jacksonAnnotationIntrospector"/>
</bean>
<bean id="jacksonAnnotationIntrospector" class="com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector" />
<jaxrs:server id="redbackServices" address="/redbackServices">

View File

@ -193,7 +193,7 @@ public void getAllGroups()
{
GroupService service = getGroupService( authorizationHeader );
List<String> allGroups = service.getGroups( Long.valueOf( 0 ), Long.valueOf( Long.MAX_VALUE ) ).stream( ).map( group -> group.getName( ) ).collect( Collectors.toList( ) );
List<String> allGroups = service.getGroups( Long.valueOf( 0 ), Long.valueOf( Long.MAX_VALUE ) ).getData().stream( ).map( group -> group.getName( ) ).collect( Collectors.toList( ) );
assertThat( allGroups ).isNotNull().isNotEmpty().hasSize( 3 ).containsAll( groups );
}