Skip to content

Commit 2a06a87

Browse files
committed
continue adding ldap properties
1 parent 2d0a6a8 commit 2a06a87

File tree

12 files changed

+382
-57
lines changed

12 files changed

+382
-57
lines changed
Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,22 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
19-
package com.redhat.lightblue.metadata.ldap.parser.property;
20-
21-
import java.util.HashSet;
22-
import java.util.Set;
23-
24-
/**
25-
* Container for special ldap properties parsed from the metadata.json file.
26-
*
27-
* @author dcrissman
28-
*
29-
* @see com.redhat.lightblue.metadata.ldap.parser.LdapPropertyParser
30-
*/
31-
public class LdapProperty {
32-
33-
private final Set<FieldToAttribute> fieldsToAttributes = new HashSet<FieldToAttribute>();
34-
35-
public Set<FieldToAttribute> getFieldsToAttributes(){
36-
return fieldsToAttributes;
37-
}
38-
39-
public void addFieldToAttribute(FieldToAttribute fieldToAttribute){
40-
fieldsToAttributes.add(fieldToAttribute);
41-
}
19+
package com.redhat.lightblue.common.ldap;
20+
21+
public interface LdapMetadataProperty {
22+
23+
/**
24+
* Returns the attributeName with the given fieldName.
25+
* @param fieldName - metadata field name
26+
* @return ldap attribute name or <code>null</code> if a match is not present.
27+
*/
28+
public String getAttributeNameForFieldName(String fieldName);
29+
30+
/**
31+
* Returns the fieldName with the given attributeName.
32+
* @param attributeName - ldap attribute name
33+
* @return metadata field name or <code>null</code> if a match is not present.
34+
*/
35+
public String getFieldNameForAttributeName(String attributeName);
4236

4337
}

lightblue-ldap-crud/src/main/java/com/redhat/lightblue/crud/ldap/EntryBuilder.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.fasterxml.jackson.databind.JsonNode;
2626
import com.redhat.lightblue.common.ldap.LdapConstant;
27+
import com.redhat.lightblue.common.ldap.LdapMetadataProperty;
2728
import com.redhat.lightblue.common.ldap.LightblueUtil;
2829
import com.redhat.lightblue.metadata.ArrayElement;
2930
import com.redhat.lightblue.metadata.ArrayField;
@@ -46,8 +47,11 @@
4647
*/
4748
public class EntryBuilder extends TranslatorFromJson<Entry>{
4849

49-
public EntryBuilder(EntityMetadata md){
50+
private final LdapMetadataProperty property;
51+
52+
public EntryBuilder(EntityMetadata md, LdapMetadataProperty property){
5053
super(md);
54+
this.property = property;
5155
}
5256

5357
public Entry build(String dn, JsonDoc document){
@@ -71,7 +75,7 @@ else if(type instanceof BinaryType){
7175

7276
@Override
7377
protected void translate(SimpleField field, Path path, JsonNode node, Entry target) {
74-
String fieldName = field.getName();
78+
String fieldName = findAttributeName(field.getName());
7579

7680
if(LdapConstant.FIELD_DN.equalsIgnoreCase(fieldName)){
7781
throw new IllegalArgumentException(
@@ -106,7 +110,7 @@ protected void translate(ObjectField field, Path path, JsonNode node, Entry targ
106110
protected void translateSimpleArray(ArrayField field, Path path, List<Object> items, Entry target) {
107111
ArrayElement arrayElement = field.getElement();
108112
Type arrayElementType = arrayElement.getType();
109-
String fieldName = field.getName();
113+
String fieldName = findAttributeName(field.getName());
110114

111115
if(arrayElementType instanceof BinaryType){
112116
List<byte[]> bytes = new ArrayList<byte[]>();
@@ -129,4 +133,17 @@ protected void translateObjectArray(ArrayField field, JsonNodeCursor cursor, Ent
129133
throw new UnsupportedOperationException("Object ArrayField type is not currently supported.");
130134
}
131135

136+
private String findAttributeName(String fieldName){
137+
if(property == null){
138+
return fieldName;
139+
}
140+
141+
String attributeName = property.getAttributeNameForFieldName(fieldName);
142+
if(attributeName == null){
143+
return fieldName;
144+
}
145+
146+
return attributeName;
147+
}
148+
132149
}

lightblue-ldap-crud/src/main/java/com/redhat/lightblue/crud/ldap/LdapCRUDController.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.redhat.lightblue.common.ldap.DBResolver;
3333
import com.redhat.lightblue.common.ldap.LdapConstant;
3434
import com.redhat.lightblue.common.ldap.LdapDataStore;
35+
import com.redhat.lightblue.common.ldap.LdapMetadataProperty;
3536
import com.redhat.lightblue.common.ldap.LightblueUtil;
3637
import com.redhat.lightblue.crud.CRUDController;
3738
import com.redhat.lightblue.crud.CRUDDeleteResponse;
@@ -104,9 +105,10 @@ public CRUDInsertionResponse insert(CRUDOperationContext ctx,
104105

105106
EntityMetadata md = ctx.getEntityMetadata(ctx.getEntityName());
106107
LdapDataStore store = getLdapDataStore(md);
108+
LdapMetadataProperty property = getLdapMetadataProperty(md);
107109

108110
FieldAccessRoleEvaluator roles = new FieldAccessRoleEvaluator(md, ctx.getCallerRoles());
109-
EntryBuilder entryBuilder = new EntryBuilder(md);
111+
EntryBuilder entryBuilder = new EntryBuilder(md, property);
110112

111113
//Create Entry instances for each document.
112114
List<com.unboundid.ldap.sdk.Entry> entries = new ArrayList<com.unboundid.ldap.sdk.Entry>();
@@ -312,6 +314,26 @@ private LdapDataStore getLdapDataStore(EntityMetadata md){
312314
return (LdapDataStore) store;
313315
}
314316

317+
/**
318+
* Shortcut method to get and return the {@link LdapMetadataProperty} on the passed
319+
* in {@link EntityMetadata}.
320+
* @param md - {@link EntityMetadata}.
321+
* @return {@link LdapMetadataProperty}
322+
* @throws IllegalArgumentException if an invalid object is found.
323+
*/
324+
private LdapMetadataProperty getLdapMetadataProperty(EntityMetadata md){
325+
Object o = md.getEntityInfo().getProperties().get(LdapConstant.BACKEND);
326+
327+
if(o == null){
328+
return null;
329+
}
330+
331+
if(!(o instanceof LdapMetadataProperty)){
332+
throw new IllegalArgumentException("Object of type " + o.getClass() + " is not supported.");
333+
}
334+
return (LdapMetadataProperty) o;
335+
}
336+
315337
/**
316338
* Creates and returns a unique DN.
317339
* @param store - {@link LdapDataStore} to use as the BaseDN and field that

lightblue-ldap-crud/src/test/java/com/redhat/lightblue/crud/ldap/EntryBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected static Entry buildEntry(String fieldName, String metadataType, String
6363
.replaceFirst("#value", crudValue);
6464

6565
EntityMetadata md = MetadataUtil.createEntityMetadata(LdapConstant.BACKEND, json(metadata), null, null);
66-
EntryBuilder builder = new EntryBuilder(md);
66+
EntryBuilder builder = new EntryBuilder(md, null);
6767

6868
return builder.build("uid=someuid,dc=example,dc=com",
6969
new JsonDoc(json(crud).get("data")));

lightblue-ldap-integration-test/src/test/java/com/redhat/lightblue/crud/ldap/ITCaseLdapCRUDControllerTest.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@
3737
import com.redhat.lightblue.crud.FindRequest;
3838
import com.redhat.lightblue.crud.InsertionRequest;
3939
import com.redhat.lightblue.ldap.test.LdapServerExternalResource;
40-
import com.redhat.lightblue.ldap.test.LdapServerExternalResource.InMemoryLdapServer;
4140
import com.redhat.lightblue.mongo.test.MongoServerExternalResource;
42-
import com.redhat.lightblue.mongo.test.MongoServerExternalResource.InMemoryMongoServer;
4341
import com.redhat.lightblue.test.FakeClientIdentification;
4442
import com.unboundid.ldap.sdk.Attribute;
4543

@@ -49,8 +47,6 @@
4947
*
5048
* @author dcrissman
5149
*/
52-
@InMemoryLdapServer
53-
@InMemoryMongoServer
5450
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
5551
public class ITCaseLdapCRUDControllerTest extends AbstractLdapCRUDController{
5652

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
Copyright 2015 Red Hat, Inc. and/or its affiliates.
3+
4+
This file is part of lightblue.
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
package com.redhat.lightblue.crud.ldap;
20+
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertNotNull;
23+
24+
import org.junit.BeforeClass;
25+
import org.junit.FixMethodOrder;
26+
import org.junit.Ignore;
27+
import org.junit.Test;
28+
import org.junit.runners.MethodSorters;
29+
import org.skyscreamer.jsonassert.JSONAssert;
30+
31+
import com.fasterxml.jackson.databind.JsonNode;
32+
import com.redhat.lightblue.Response;
33+
import com.redhat.lightblue.crud.FindRequest;
34+
import com.redhat.lightblue.crud.InsertionRequest;
35+
import com.redhat.lightblue.ldap.test.LdapServerExternalResource;
36+
import com.redhat.lightblue.mongo.test.MongoServerExternalResource;
37+
import com.unboundid.ldap.sdk.Attribute;
38+
39+
/**
40+
*
41+
* @author dcrissman
42+
*/
43+
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
44+
public class ITCaseLdapCRUDController_WithProperties_Test extends AbstractLdapCRUDController{
45+
46+
private static final String BASEDB_USERS = "ou=Users,dc=example,dc=com";
47+
48+
@BeforeClass
49+
public static void beforeClass() throws Exception {
50+
ldapServer.add("ou=Users,dc=example,dc=com", new Attribute[]{
51+
new Attribute("objectClass", "top"),
52+
new Attribute("objectClass", "organizationalUnit"),
53+
new Attribute("ou", "Users")});
54+
55+
System.setProperty("ldap.host", "localhost");
56+
System.setProperty("ldap.port", String.valueOf(LdapServerExternalResource.DEFAULT_PORT));
57+
System.setProperty("ldap.database", "test");
58+
System.setProperty("ldap.person.basedn", BASEDB_USERS);
59+
60+
System.setProperty("mongo.host", "localhost");
61+
System.setProperty("mongo.port", String.valueOf(MongoServerExternalResource.DEFAULT_PORT));
62+
System.setProperty("mongo.database", "lightblue");
63+
64+
initLightblueFactory("./datasources.json", "./metadata/person-metadata-with-properties.json");
65+
}
66+
67+
@Test
68+
public void testPersonInsertWithProperties() throws Exception{
69+
Response response = lightblueFactory.getMediator().insert(
70+
createRequest_FromResource(InsertionRequest.class, "./crud/insert/person-insert-many.json"));
71+
72+
assertNotNull(response);
73+
assertNoErrors(response);
74+
assertNoDataErrors(response);
75+
assertEquals(4, response.getModifiedCount());
76+
77+
JsonNode entityData = response.getEntityData();
78+
assertNotNull(entityData);
79+
JSONAssert.assertEquals(
80+
"[{\"dn\":\"uid=junior.doe," + BASEDB_USERS + "\"},{\"dn\":\"uid=john.doe," + BASEDB_USERS + "\"},{\"dn\":\"uid=jane.doe," + BASEDB_USERS + "\"},{\"dn\":\"uid=jack.buck," + BASEDB_USERS + "\"}]",
81+
entityData.toString(), false);
82+
}
83+
84+
@Test
85+
@Ignore
86+
public void testFindSingleWithProperties() throws Exception{
87+
Response response = lightblueFactory.getMediator().find(
88+
createRequest_FromResource(FindRequest.class, "./crud/find/person-find-single.json"));
89+
90+
assertNotNull(response);
91+
assertNoErrors(response);
92+
assertNoDataErrors(response);
93+
assertEquals(1, response.getMatchCount());
94+
95+
JsonNode entityData = response.getEntityData();
96+
assertNotNull(entityData);
97+
JSONAssert.assertEquals(
98+
"[{\"dn\":\"uid=john.doe," + BASEDB_USERS + "\",\"uid\":\"john.doe\",\"objectType\":\"person\",\"objectClass#\":4}]",
99+
entityData.toString(), true);
100+
}
101+
102+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"entityInfo": {
3+
"name": "person",
4+
"datastore": {
5+
"backend":"ldap",
6+
"database": "${ldap.database}",
7+
"basedn": "${ldap.person.basedn}",
8+
"uniqueattr": "uid"
9+
},
10+
"ldap": {
11+
"fieldsToAttributes": [
12+
{
13+
"field": "firstName",
14+
"attribute": "givenName"
15+
},
16+
{
17+
"field": "lastName",
18+
"attribute": "sn"
19+
}
20+
]
21+
}
22+
},
23+
"schema": {
24+
"name": "person",
25+
"version": {
26+
"value": "1.0.0",
27+
"changelog": "blahblah"
28+
},
29+
"status": {
30+
"value": "active"
31+
},
32+
"access" : {
33+
"insert": ["anyone"],
34+
"update": ["anyone"],
35+
"delete": ["anyone"],
36+
"find": ["anyone"]
37+
},
38+
"fields": {
39+
"uid": {"type": "string"},
40+
"givenName": {"type": "string"},
41+
"sn": {"type": "string"},
42+
"cn": {"type": "string"}
43+
}
44+
}
45+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <http://www.gnu.org/licenses/>.
1818
*/
19-
package com.redhat.lightblue.metadata.ldap.parser.property;
19+
package com.redhat.lightblue.metadata.ldap.model;
2020

2121
/**
2222
* Maps the metadata field name to the Ldap attribute name.

0 commit comments

Comments
 (0)