Skip to content

Commit

Permalink
implement group metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgubler committed Jan 8, 2025
1 parent 718b4f2 commit b076dd1
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 85 deletions.
1 change: 1 addition & 0 deletions app/entities/OpenIdUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private OpenIdUser(String idToken, IdToken.Payload idTokenPayload) {
}
groupsMetadata = Collections.unmodifiableList(groupsMetadata);
} catch (Exception e) {
e.printStackTrace();
// too bad
}
}
Expand Down
46 changes: 0 additions & 46 deletions app/entities/ScopedGroup.java

This file was deleted.

7 changes: 6 additions & 1 deletion app/entities/mongodb/MongoDbGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.bson.types.ObjectId;

@Entity(value = "groups", useDiscriminator = false)
public class MongoDbGroup implements MongoDbEntity, Group {
public class MongoDbGroup implements MongoDbEntity, Group, Comparable<MongoDbGroup> {
@Id
private ObjectId _id;

Expand Down Expand Up @@ -57,4 +57,9 @@ public void setEmail(String email) {
public String getPath() {
return path;
}

@Override
public int compareTo(MongoDbGroup mongoDbGroup) {
return getPath().compareTo(mongoDbGroup.getPath());
}
}
4 changes: 3 additions & 1 deletion app/services/MongoDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.morphia.mapping.MapperOptions;
import dev.morphia.Datastore;
import dev.morphia.Morphia;
import entities.mongodb.MongoDbGroup;
import entities.mongodb.MongoDbUser;
import org.bouncycastle.util.encoders.Hex;
import play.inject.ApplicationLifecycle;
Expand Down Expand Up @@ -33,7 +34,7 @@ public MongoDb(ApplicationLifecycle appLifecycle) {
String password = Config.get(Config.Option.MONGODB_PASSWORD);
String hostname = Config.get(Config.Option.MONGODB_HOSTNAME);
String database = Config.get(Config.Option.MONGODB_DATABASE);
// Don't use TLS by default for local development environments and for MongoDBs in OpenShift containers
// Use TLS by default except for local development
Boolean tls = !(Config.getBoolean(Config.Option.MONGODB_DISABLE_TLS) || "localhost".equals(hostname));
String mongoUrl;
if (username != null && password != null) {
Expand All @@ -49,6 +50,7 @@ public MongoDb(ApplicationLifecycle appLifecycle) {
MapperOptions mapperOptions = MapperOptions.builder().storeEmpties(false).storeNulls(false).build();
ds = Morphia.createDatastore(mongoClient, database, mapperOptions);
ds.getMapper().map(MongoDbUser.class);
ds.getMapper().map(MongoDbGroup.class);
ds.ensureIndexes();
ds.ensureCaps();

Expand Down
32 changes: 10 additions & 22 deletions app/services/ldap/OpenIdPartition.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import store.GroupsStore;
import store.UsersStore;
import store.ServicesStore;
import util.Config;
import util.CustomLogger;

import javax.inject.Inject;
Expand All @@ -30,8 +29,6 @@ public class OpenIdPartition extends AbstractPartition {

private final CustomLogger logger = new CustomLogger(this.getClass());

private final String groupsScope;

private DnFactory dnFactory;

private final Dn peopleDn;
Expand All @@ -56,7 +53,6 @@ public static OpenIdPartition createPartition(SchemaManager schemaManager, DnFac

public OpenIdPartition(SchemaManager schemaManager, DnFactory dnFactory, Dn suffixDn) {
try {
this.groupsScope = normalizeScope(Config.Option.LDAP_GROUPS_SCOPE.get());
this.dnFactory = dnFactory;
this.suffixDn = suffixDn;
this.peopleDn = suffixDn.add("ou=People");
Expand All @@ -74,17 +70,6 @@ public OpenIdPartition(SchemaManager schemaManager, DnFactory dnFactory, Dn suff
}
}

private static String normalizeScope(String scope) {
scope = scope.trim();
if (!scope.startsWith("/")) {
scope = "/" + scope;
}
if (scope.endsWith("/")) {
scope = scope.substring(0, scope.length() - 1);
}
return scope;
}

@Override
protected void doDestroy(PartitionTxn partitionTxn) throws LdapException {

Expand Down Expand Up @@ -152,8 +137,6 @@ public EntryFilteringCursor search(SearchOperationContext searchContext) throws
if (service != null && groupsDn.equals(searchContext.getDn().getParent())) { // need to go one level up to remove the ou=SERVICE
Evaluator evaluator = evaluatorBuilder.build(null, searchContext.getFilter());
List<Entry> entries = groupsStore.getAll()
.map(g -> ScopedGroup.scoped(g, groupsScope))
.filter(Objects::nonNull)
.map(g -> groupsEntryFromGroup(g, service))
.filter(e -> evaluate(evaluator, e))
.collect(Collectors.toList());
Expand Down Expand Up @@ -183,7 +166,7 @@ private Entry lookupInternal(LookupOperationContext lookupContext) {
return peopleEntryFromUser(user, service);
}

ScopedGroup group = ScopedGroup.scoped(getGroupByDn(lookupContext.getDn()), groupsScope);
Group group = getGroupByDn(lookupContext.getDn());
if (group != null) {
return groupsEntryFromGroup(group, service);
}
Expand Down Expand Up @@ -253,7 +236,6 @@ private Entry peopleEntryFromUser(User user, Service service) {
entry.put("objectClass", "inetOrgPerson", "inetUser", "mailRecipient", "organizationalPerson", "person", "top", "groupMember");
String[] groups = user.getGroupPaths().stream()
.map(groupsStore::getByPath)
.map(g -> ScopedGroup.scoped(g, groupsScope))
.filter(Objects::nonNull)
.map(g -> groupDn(g, service))
.map(Dn::toString)
Expand All @@ -270,19 +252,25 @@ private Dn userDn(User user, Service service) {
}
}

private Dn groupDn(ScopedGroup group, Service service) {
private Dn groupDn(Group group, Service service) {
try {
return dnFactory.create("cn=" + group.getCn(), "ou=" + service.getId(), "ou=Groups", getSuffixDn().toString());
} catch (LdapInvalidDnException e) {
throw new RuntimeException(e);
}
}

private Entry groupsEntryFromGroup(ScopedGroup group, Service service) {
private Entry groupsEntryFromGroup(Group group, Service service) {
Entry entry = new DefaultEntry(schemaManager, groupDn(group, service));
entry.put("cn", group.getCn());
entry.put("objectClass", "groupofuniquenames", "top");
entry.put("uniqueMember", usersStore.getByGroupPath(group.getRealGroup().getPath()).map(u -> userDn(u, service)).map(Dn::toString).toArray(String[]::new));
entry.put("uniqueMember", usersStore.getByGroupPath(group.getPath()).map(u -> userDn(u, service)).map(Dn::toString).toArray(String[]::new));
if (group.getDescription() != null) {
entry.put("description", group.getDescription());
}
if (group.getEmail() != null) {
entry.put("mail", group.getEmail());
}
return entry;
}

Expand Down
2 changes: 0 additions & 2 deletions app/store/UsersStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ public interface UsersStore {

Stream<? extends User> getByGroupPath(String groupPath);

Stream<? extends User> getAll();

UserSession createSession(User user, String sessionId, String openIdIdentityToken, String openIdAccessToken, String openIdRefreshToken, Long openIdTokenExpiry);

byte[] getEncryptionKey();
Expand Down
11 changes: 0 additions & 11 deletions app/store/memory/MemoryUsersStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
import com.google.api.client.auth.oauth2.CredentialRefreshListener;
import com.google.api.client.auth.oauth2.TokenErrorResponse;
import com.google.api.client.auth.oauth2.TokenResponse;
import dev.morphia.UpdateOptions;
import dev.morphia.query.updates.UpdateOperators;
import entities.OpenIdUser;
import entities.Service;
import entities.User;
import entities.UserSession;
import entities.memory.MemoryServicePasswords;
import entities.memory.MemoryUser;
import entities.memory.MemoryUserSession;
import entities.mongodb.MongoDbServicePasswords;
import entities.mongodb.MongoDbUser;
import org.apache.directory.api.ldap.model.constants.LdapSecurityConstants;
import org.apache.directory.api.ldap.model.password.PasswordUtil;
import play.mvc.Http;
Expand Down Expand Up @@ -137,13 +133,6 @@ public Stream<MemoryUser> getByGroupPath(String groupPath) {
return users.stream().filter(u -> u.getGroupPaths().contains(groupPath));
}

@Override
public List<User> getAll() {
List<User> usersList = new ArrayList<>(users);
Collections.sort(usersList, Comparator.comparing(User::getUid));
return usersList;
}

@Override
public UserSession createSession(User user, String id, String openIdIdentityToken, String openIdAccessToken, String openIdRefreshToken, Long openIdTokenExpiry) {
MemoryUser memoryUser = (MemoryUser)user;
Expand Down
4 changes: 2 additions & 2 deletions app/store/mongodb/MongoDbGroupsStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void updateMetadata(Collection<GroupsMetadata> metadata) {
continue;
}
if (!Objects.equals(g.getDescription(), m.getDescription()) || !Objects.equals(g.getEmail(), m.getEmail())) {
g.setDescription(g.getDescription());
g.setEmail(g.getEmail());
g.setDescription(m.getDescription());
g.setEmail(m.getEmail());
query(g).update(new UpdateOptions(), UpdateOperators.set("description", g.getDescription()), UpdateOperators.set("email", g.getEmail()));
}
}
Expand Down

0 comments on commit b076dd1

Please sign in to comment.