Search in sources :

Example 1 with ConsistencyProblemInfo

use of com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo in project gerrit by GerritCodeReview.

the class ExternalIdsConsistencyChecker method check.

private List<ConsistencyProblemInfo> check(ExternalIdNotes extIdNotes) throws IOException {
    List<ConsistencyProblemInfo> problems = new ArrayList<>();
    ListMultimap<String, ExternalId> emails = MultimapBuilder.hashKeys().arrayListValues().build();
    try (RevWalk rw = new RevWalk(extIdNotes.getRepository())) {
        NoteMap noteMap = extIdNotes.getNoteMap();
        for (Note note : noteMap) {
            byte[] raw = ExternalIdNotes.readNoteData(rw, note.getData());
            try {
                ExternalId extId = externalIdFactory.parse(note.getName(), raw, note.getData());
                problems.addAll(validateExternalId(extId));
                if (extId.email() != null) {
                    String email = extId.email();
                    if (emails.get(email).stream().noneMatch(e -> e.accountId().get() == extId.accountId().get())) {
                        emails.put(email, extId);
                    }
                }
            } catch (ConfigInvalidException e) {
                addError(String.format(e.getMessage()), problems);
            }
        }
    }
    emails.asMap().entrySet().stream().filter(e -> e.getValue().size() > 1).forEach(e -> addError(String.format("Email '%s' is not unique, it's used by the following external IDs: %s", e.getKey(), e.getValue().stream().map(k -> "'" + k.key().get() + "'").sorted().collect(joining(", "))), problems));
    return problems;
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) AccountCache(com.google.gerrit.server.account.AccountCache) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OutgoingEmailValidator(com.google.gerrit.server.mail.send.OutgoingEmailValidator) Note(org.eclipse.jgit.notes.Note) ListMultimap(com.google.common.collect.ListMultimap) MultimapBuilder(com.google.common.collect.MultimapBuilder) Inject(com.google.inject.Inject) IOException(java.io.IOException) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) List(java.util.List) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) SCHEME_USERNAME(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) HashedPassword(com.google.gerrit.server.account.HashedPassword) Repository(org.eclipse.jgit.lib.Repository) NoteMap(org.eclipse.jgit.notes.NoteMap) Singleton(com.google.inject.Singleton) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Note(org.eclipse.jgit.notes.Note) ArrayList(java.util.ArrayList) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk)

Example 2 with ConsistencyProblemInfo

use of com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo in project gerrit by GerritCodeReview.

the class ExternalIdIT method insertNonParsableExternalIds.

private Set<ConsistencyProblemInfo> insertNonParsableExternalIds() throws IOException {
    MutableInteger i = new MutableInteger();
    String scheme = "corrupt";
    Set<ConsistencyProblemInfo> expectedProblems = new HashSet<>();
    try (Repository repo = repoManager.openRepository(allUsers);
        RevWalk rw = new RevWalk(repo)) {
        String externalId = nextId(scheme, i);
        String noteId = insertExternalIdWithoutAccountId(repo, rw, admin.newIdent(), admin.id(), externalId);
        expectedProblems.add(consistencyError("Invalid external ID config for note '" + noteId + "': Value for 'externalId." + externalId + ".accountId' is missing, expected account ID"));
        externalId = nextId(scheme, i);
        noteId = insertExternalIdWithKeyThatDoesntMatchNoteId(repo, rw, admin.newIdent(), admin.id(), externalId);
        expectedProblems.add(consistencyError("Invalid external ID config for note '" + noteId + "': SHA1 of external ID '" + externalId + "' does not match note ID '" + noteId + "'"));
        noteId = insertExternalIdWithInvalidConfig(repo, rw, admin.newIdent(), nextId(scheme, i));
        expectedProblems.add(consistencyError("Invalid external ID config for note '" + noteId + "': Invalid line in config file"));
        noteId = insertExternalIdWithEmptyNote(repo, rw, admin.newIdent(), nextId(scheme, i));
        expectedProblems.add(consistencyError("Invalid external ID config for note '" + noteId + "': Expected exactly 1 'externalId' section, found 0"));
    }
    return expectedProblems;
}
Also used : InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) MutableInteger(org.eclipse.jgit.util.MutableInteger) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) RevWalk(org.eclipse.jgit.revwalk.RevWalk) HashSet(java.util.HashSet)

Example 3 with ConsistencyProblemInfo

use of com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo in project gerrit by GerritCodeReview.

the class GroupsNoteDbConsistencyChecker method checkGlobalConsistency.

/**
 * Check invariants of the group refs with the group name refs.
 */
private List<ConsistencyProblemInfo> checkGlobalConsistency(Map<AccountGroup.UUID, InternalGroup> uuidToGroupMap, BiMap<AccountGroup.UUID, String> uuidNameBiMap) {
    List<ConsistencyProblemInfo> problems = new ArrayList<>();
    // Check consistency between the data coming from different refs.
    for (AccountGroup.UUID uuid : uuidToGroupMap.keySet()) {
        if (!uuidNameBiMap.containsKey(uuid)) {
            problems.add(error("group %s has no entry in name map", uuid));
            continue;
        }
        String noteName = uuidNameBiMap.get(uuid);
        String groupRefName = uuidToGroupMap.get(uuid).getName();
        if (!Objects.equals(noteName, groupRefName)) {
            problems.add(error("inconsistent name for group %s (name map %s vs. group ref %s)", uuid, noteName, groupRefName));
        }
    }
    for (AccountGroup.UUID uuid : uuidNameBiMap.keySet()) {
        if (!uuidToGroupMap.containsKey(uuid)) {
            problems.add(error("name map has entry (%s, %s), entry missing as group ref", uuid, uuidNameBiMap.get(uuid)));
        }
    }
    if (problems.isEmpty()) {
        // Check ids.
        Map<AccountGroup.Id, InternalGroup> groupById = new HashMap<>();
        for (InternalGroup g : uuidToGroupMap.values()) {
            InternalGroup before = groupById.get(g.getId());
            if (before != null) {
                problems.add(error("shared group id %s for %s (%s) and %s (%s)", g.getId(), before.getName(), before.getGroupUUID(), g.getName(), g.getGroupUUID()));
            }
            groupById.put(g.getId(), g);
        }
    }
    return problems;
}
Also used : AccountGroup(com.google.gerrit.entities.AccountGroup) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) InternalGroup(com.google.gerrit.entities.InternalGroup)

Example 4 with ConsistencyProblemInfo

use of com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo in project gerrit by GerritCodeReview.

the class ExternalIdsConsistencyChecker method check.

private List<ConsistencyProblemInfo> check(Repository repo, ObjectId commit) throws IOException {
    List<ConsistencyProblemInfo> problems = new ArrayList<>();
    ListMultimap<String, ExternalId.Key> emails = MultimapBuilder.hashKeys().arrayListValues().build();
    try (RevWalk rw = new RevWalk(repo)) {
        NoteMap noteMap = ExternalIdReader.readNoteMap(rw, commit);
        for (Note note : noteMap) {
            byte[] raw = rw.getObjectReader().open(note.getData(), OBJ_BLOB).getCachedBytes(ExternalIdReader.MAX_NOTE_SZ);
            try {
                ExternalId extId = ExternalId.parse(note.getName(), raw);
                problems.addAll(validateExternalId(extId));
                if (extId.email() != null) {
                    emails.put(extId.email(), extId.key());
                }
            } catch (ConfigInvalidException e) {
                addError(String.format(e.getMessage()), problems);
            }
        }
    }
    emails.asMap().entrySet().stream().filter(e -> e.getValue().size() > 1).forEach(e -> addError(String.format("Email '%s' is not unique, it's used by the following external IDs: %s", e.getKey(), e.getValue().stream().map(k -> "'" + k.get() + "'").sorted().collect(joining(", "))), problems));
    return problems;
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) AccountCache(com.google.gerrit.server.account.AccountCache) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OutgoingEmailValidator(com.google.gerrit.server.mail.send.OutgoingEmailValidator) Note(org.eclipse.jgit.notes.Note) OBJ_BLOB(org.eclipse.jgit.lib.Constants.OBJ_BLOB) ListMultimap(com.google.common.collect.ListMultimap) DecoderException(org.apache.commons.codec.DecoderException) MultimapBuilder(com.google.common.collect.MultimapBuilder) Inject(com.google.inject.Inject) IOException(java.io.IOException) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) List(java.util.List) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) SCHEME_USERNAME(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) HashedPassword(com.google.gerrit.server.account.HashedPassword) Repository(org.eclipse.jgit.lib.Repository) NoteMap(org.eclipse.jgit.notes.NoteMap) Singleton(com.google.inject.Singleton) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ArrayList(java.util.ArrayList) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Note(org.eclipse.jgit.notes.Note) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo)

Example 5 with ConsistencyProblemInfo

use of com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo in project gerrit by GerritCodeReview.

the class GroupsConsistencyChecker method checkCycle.

/**
 * checkCycle walks through root's subgroups recursively, and checks for cycles.
 */
private List<ConsistencyProblemInfo> checkCycle(InternalGroup root, Map<AccountGroup.UUID, InternalGroup> byUUID) {
    List<ConsistencyProblemInfo> problems = new ArrayList<>();
    Set<InternalGroup> todo = new LinkedHashSet<>();
    Set<InternalGroup> seen = new HashSet<>();
    todo.add(root);
    while (!todo.isEmpty()) {
        InternalGroup t = todo.iterator().next();
        todo.remove(t);
        if (seen.contains(t)) {
            continue;
        }
        seen.add(t);
        // We don't check for owner cycles, since those are normal in self-administered groups.
        for (AccountGroup.UUID subUuid : t.getSubgroups()) {
            InternalGroup g = byUUID.get(subUuid);
            if (g == null) {
                continue;
            }
            if (Objects.equals(g, root)) {
                problems.add(warning("group %s (%s) contains a cycle: %s (%s) points to it as subgroup.", root.getName(), root.getGroupUUID(), t.getName(), t.getGroupUUID()));
            }
            todo.add(g);
        }
    }
    return problems;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) AccountGroup(com.google.gerrit.entities.AccountGroup) ArrayList(java.util.ArrayList) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) InternalGroup(com.google.gerrit.entities.InternalGroup) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

ConsistencyProblemInfo (com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo)9 HashSet (java.util.HashSet)5 ArrayList (java.util.ArrayList)4 ObjectId (org.eclipse.jgit.lib.ObjectId)3 Repository (org.eclipse.jgit.lib.Repository)3 RevWalk (org.eclipse.jgit.revwalk.RevWalk)3 ListMultimap (com.google.common.collect.ListMultimap)2 MultimapBuilder (com.google.common.collect.MultimapBuilder)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 AccountGroup (com.google.gerrit.entities.AccountGroup)2 InternalGroup (com.google.gerrit.entities.InternalGroup)2 ConsistencyCheckInfo (com.google.gerrit.extensions.api.config.ConsistencyCheckInfo)2 ConsistencyCheckInput (com.google.gerrit.extensions.api.config.ConsistencyCheckInput)2 AccountCache (com.google.gerrit.server.account.AccountCache)2 HashedPassword (com.google.gerrit.server.account.HashedPassword)2 SCHEME_USERNAME (com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME)2 AllUsersName (com.google.gerrit.server.config.AllUsersName)2 GitRepositoryManager (com.google.gerrit.server.git.GitRepositoryManager)2 OutgoingEmailValidator (com.google.gerrit.server.mail.send.OutgoingEmailValidator)2 Inject (com.google.inject.Inject)2