Search in sources :

Example 41 with GroupReference

use of com.google.gerrit.entities.GroupReference in project gerrit by GerritCodeReview.

the class AccountGroupUUIDHandler method parseArguments.

@Override
public final int parseArguments(Parameters params) throws CmdLineException {
    final String n = params.getParameter(0);
    AccountGroup.UUID uuid = AccountGroup.uuid(n);
    if (groupBackend.handles(uuid)) {
        GroupDescription.Basic d = groupBackend.get(uuid);
        if (d != null) {
            setter.addValue(uuid);
            return 1;
        }
    }
    // Might be a numeric AccountGroup.Id. -> Internal group.
    if (n.matches("^[1-9][0-9]*$")) {
        try {
            AccountGroup.Id groupId = AccountGroup.Id.parse(n);
            Optional<InternalGroup> groupInternal = groupCache.get(groupId);
            if (groupInternal.isPresent()) {
                uuid = new InternalGroupDescription(groupInternal.get()).getGroupUUID();
                setter.addValue(uuid);
                return 1;
            }
        } catch (IllegalArgumentException e) {
        // Ignored
        }
    }
    GroupReference group = GroupBackends.findExactSuggestion(groupBackend, n);
    if (group == null) {
        throw new CmdLineException(owner, localizable("Group \"%s\" does not exist"), n);
    }
    setter.addValue(group.getUUID());
    return 1;
}
Also used : GroupDescription(com.google.gerrit.entities.GroupDescription) InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) InternalGroupDescription(com.google.gerrit.server.group.InternalGroupDescription) AccountGroup(com.google.gerrit.entities.AccountGroup) GroupReference(com.google.gerrit.entities.GroupReference) CmdLineException(org.kohsuke.args4j.CmdLineException) InternalGroup(com.google.gerrit.entities.InternalGroup)

Example 42 with GroupReference

use of com.google.gerrit.entities.GroupReference in project gerrit by GerritCodeReview.

the class PluginConfig method withInheritance.

PluginConfig withInheritance(ProjectState.Factory projectStateFactory) {
    checkState(projectConfig().isPresent(), "no project config provided");
    ProjectState state = projectStateFactory.create(projectConfig().get());
    ProjectState parent = Iterables.getFirst(state.parents(), null);
    if (parent == null) {
        return this;
    }
    Map<AccountGroup.UUID, GroupReference> groupReferences = new HashMap<>();
    groupReferences.putAll(groupReferences());
    PluginConfig parentPluginConfig = parent.getPluginConfig(pluginName()).withInheritance(projectStateFactory);
    Set<String> allNames = cfg().getNames(PLUGIN, pluginName());
    Config newCfg = copyConfig(cfg());
    for (String name : parentPluginConfig.cfg().getNames(PLUGIN, pluginName())) {
        if (!allNames.contains(name)) {
            List<String> values = Arrays.asList(parentPluginConfig.cfg().getStringList(PLUGIN, pluginName(), name));
            for (String value : values) {
                Optional<GroupReference> groupRef = parentPluginConfig.projectConfig().get().getGroupByName(GroupReference.extractGroupName(value));
                if (groupRef.isPresent()) {
                    groupReferences.putIfAbsent(groupRef.get().getUUID(), groupRef.get());
                }
            }
            newCfg.setStringList(PLUGIN, pluginName(), name, values);
        }
    }
    return new AutoValue_PluginConfig(pluginName(), newCfg, projectConfig(), ImmutableMap.copyOf(groupReferences));
}
Also used : HashMap(java.util.HashMap) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Config(org.eclipse.jgit.lib.Config) CachedProjectConfig(com.google.gerrit.entities.CachedProjectConfig) ProjectState(com.google.gerrit.server.project.ProjectState) GroupReference(com.google.gerrit.entities.GroupReference)

Example 43 with GroupReference

use of com.google.gerrit.entities.GroupReference in project gerrit by GerritCodeReview.

the class ProjectWatch method add.

private void add(Watchers matching, Project.NameKey projectName, NotifyConfig nc) throws QueryParseException {
    logger.atFine().log("Checking watchers for notify config %s from project %s", nc, projectName);
    for (GroupReference groupRef : nc.getGroups()) {
        CurrentUser user = new GroupBackedUser(ImmutableSet.of(groupRef.getUUID()));
        if (filterMatch(user, nc.getFilter())) {
            deliverToMembers(matching.list(nc.getHeader()), groupRef.getUUID());
            logger.atFine().log("Added watchers for group %s", groupRef);
        } else {
            logger.atFine().log("The filter did not match for group %s; skip notification", groupRef);
        }
    }
    if (!nc.getAddresses().isEmpty()) {
        if (filterMatch(null, nc.getFilter())) {
            matching.list(nc.getHeader()).emails.addAll(nc.getAddresses());
            logger.atFine().log("Added watchers for these addresses: %s", nc.getAddresses());
        } else {
            logger.atFine().log("The filter did not match; skip notification for these addresses: %s", nc.getAddresses());
        }
    }
}
Also used : CurrentUser(com.google.gerrit.server.CurrentUser) GroupReference(com.google.gerrit.entities.GroupReference) GroupBackedUser(com.google.gerrit.server.query.change.GroupBackedUser)

Example 44 with GroupReference

use of com.google.gerrit.entities.GroupReference in project gerrit by GerritCodeReview.

the class InitAdminUser method postRun.

@Override
public void postRun() throws Exception {
    if (!accounts.hasAnyAccount()) {
        welcome();
    }
    AuthType authType = flags.cfg.getEnum(AuthType.values(), "auth", null, "type", null);
    if (authType != AuthType.DEVELOPMENT_BECOME_ANY_ACCOUNT) {
        return;
    }
    if (!accounts.hasAnyAccount()) {
        ui.header("Gerrit Administrator");
        if (ui.yesno(true, "Create administrator user")) {
            Account.Id id = Account.id(sequencesOnInit.nextAccountId());
            String username = ui.readString("admin", "username");
            String name = ui.readString("Administrator", "name");
            String httpPassword = ui.readString("secret", "HTTP password");
            AccountSshKey sshKey = readSshKey(id);
            String email = readEmail(sshKey);
            List<ExternalId> extIds = new ArrayList<>(2);
            extIds.add(externalIdFactory.createUsername(username, id, httpPassword));
            if (email != null) {
                extIds.add(externalIdFactory.createEmail(id, email));
            }
            externalIds.insert("Add external IDs for initial admin user", extIds);
            Account persistedAccount = accounts.insert(Account.builder(id, TimeUtil.now()).setFullName(name).setPreferredEmail(email));
            // Only two groups should exist at this point in time and hence iterating over all of them
            // is cheap.
            Optional<GroupReference> adminGroupReference = groupsOnInit.getAllGroupReferences().filter(group -> group.getName().equals("Administrators")).findAny();
            if (!adminGroupReference.isPresent()) {
                throw new NoSuchGroupException("Administrators");
            }
            GroupReference adminGroup = adminGroupReference.get();
            groupsOnInit.addGroupMember(adminGroup.getUUID(), persistedAccount);
            if (sshKey != null) {
                VersionedAuthorizedKeysOnInit authorizedKeys = authorizedKeysFactory.create(id).load();
                authorizedKeys.addKey(sshKey.sshPublicKey());
                authorizedKeys.save("Add SSH key for initial admin user\n");
            }
            AccountState as = AccountState.forAccount(persistedAccount, extIds);
            for (AccountIndex accountIndex : accountIndexCollection.getWriteIndexes()) {
                accountIndex.replace(as);
            }
            InternalGroup adminInternalGroup = groupsOnInit.getExistingGroup(adminGroup);
            for (GroupIndex groupIndex : groupIndexCollection.getWriteIndexes()) {
                groupIndex.replace(adminInternalGroup);
            }
        }
    }
}
Also used : InternalGroup(com.google.gerrit.entities.InternalGroup) AuthType(com.google.gerrit.extensions.client.AuthType) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) Inject(com.google.inject.Inject) GroupIndexCollection(com.google.gerrit.server.index.group.GroupIndexCollection) InitStep(com.google.gerrit.pgm.init.api.InitStep) EmailValidator(org.apache.commons.validator.routines.EmailValidator) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) AccountIndex(com.google.gerrit.server.index.account.AccountIndex) ExternalIdFactory(com.google.gerrit.server.account.externalids.ExternalIdFactory) ConsoleUI(com.google.gerrit.pgm.init.api.ConsoleUI) Path(java.nio.file.Path) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Account(com.google.gerrit.entities.Account) AccountIndexCollection(com.google.gerrit.server.index.account.AccountIndexCollection) IOException(java.io.IOException) GroupReference(com.google.gerrit.entities.GroupReference) List(java.util.List) AccountSshKey(com.google.gerrit.server.account.AccountSshKey) Paths(java.nio.file.Paths) Optional(java.util.Optional) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) GroupIndex(com.google.gerrit.server.index.group.GroupIndex) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) AccountState(com.google.gerrit.server.account.AccountState) SequencesOnInit(com.google.gerrit.pgm.init.api.SequencesOnInit) InitFlags(com.google.gerrit.pgm.init.api.InitFlags) Account(com.google.gerrit.entities.Account) AccountIndex(com.google.gerrit.server.index.account.AccountIndex) AccountSshKey(com.google.gerrit.server.account.AccountSshKey) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ArrayList(java.util.ArrayList) AccountState(com.google.gerrit.server.account.AccountState) NoSuchGroupException(com.google.gerrit.exceptions.NoSuchGroupException) InternalGroup(com.google.gerrit.entities.InternalGroup) GroupIndex(com.google.gerrit.server.index.group.GroupIndex) AuthType(com.google.gerrit.extensions.client.AuthType) GroupReference(com.google.gerrit.entities.GroupReference)

Example 45 with GroupReference

use of com.google.gerrit.entities.GroupReference in project gerrit by GerritCodeReview.

the class GroupsNoteDbConsistencyChecker method readGroupNames.

private void readGroupNames(Repository repo, List<Ref> refs, Result result, BiMap<AccountGroup.UUID, String> uuidNameBiMap) throws IOException {
    Optional<Ref> maybeRef = refs.stream().filter(r -> r.getName().equals(RefNames.REFS_GROUPNAMES)).findFirst();
    if (!maybeRef.isPresent()) {
        result.problems.add(error("ref %s does not exist", RefNames.REFS_GROUPNAMES));
        return;
    }
    Ref ref = maybeRef.get();
    try (RevWalk rw = new RevWalk(repo)) {
        RevCommit c = rw.parseCommit(ref.getObjectId());
        NoteMap nm = NoteMap.read(rw.getObjectReader(), c);
        for (Note note : nm) {
            ObjectLoader ld = rw.getObjectReader().open(note.getData());
            byte[] data = ld.getCachedBytes();
            GroupReference gRef;
            try {
                gRef = GroupNameNotes.getFromNoteData(data);
            } catch (ConfigInvalidException e) {
                result.problems.add(error("notename entry %s: %s does not parse: %s", note, new String(data, StandardCharsets.UTF_8), e.getMessage()));
                continue;
            }
            ObjectId nameKey = GroupNameNotes.getNoteKey(AccountGroup.nameKey(gRef.getName()));
            if (!Objects.equals(nameKey, note)) {
                result.problems.add(error("notename entry %s does not match name %s", note, gRef.getName()));
            }
            // We trust SHA1 to have no collisions, so no need to check uniqueness of name.
            uuidNameBiMap.put(gRef.getUUID(), gRef.getName());
        }
    }
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) InternalGroup(com.google.gerrit.entities.InternalGroup) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Inject(com.google.inject.Inject) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ImmutableList(com.google.common.collect.ImmutableList) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) Map(java.util.Map) FormatMethod(com.google.errorprone.annotations.FormatMethod) RefNames(com.google.gerrit.entities.RefNames) ConsistencyProblemInfo.error(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo.error) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) NoteMap(org.eclipse.jgit.notes.NoteMap) AccountGroup(com.google.gerrit.entities.AccountGroup) BiMap(com.google.common.collect.BiMap) Note(org.eclipse.jgit.notes.Note) ConsistencyCheckInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo) IOException(java.io.IOException) StandardCharsets(java.nio.charset.StandardCharsets) ObjectId(org.eclipse.jgit.lib.ObjectId) GroupReference(com.google.gerrit.entities.GroupReference) Objects(java.util.Objects) HashBiMap(com.google.common.collect.HashBiMap) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) Ref(org.eclipse.jgit.lib.Ref) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ConsistencyProblemInfo.warning(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo.warning) FluentLogger(com.google.common.flogger.FluentLogger) Repository(org.eclipse.jgit.lib.Repository) Singleton(com.google.inject.Singleton) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ObjectId(org.eclipse.jgit.lib.ObjectId) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Ref(org.eclipse.jgit.lib.Ref) Note(org.eclipse.jgit.notes.Note) ObjectLoader(org.eclipse.jgit.lib.ObjectLoader) GroupReference(com.google.gerrit.entities.GroupReference) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

GroupReference (com.google.gerrit.entities.GroupReference)59 Test (org.junit.Test)24 AccountGroup (com.google.gerrit.entities.AccountGroup)18 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)8 GroupDescription (com.google.gerrit.entities.GroupDescription)8 InternalGroup (com.google.gerrit.entities.InternalGroup)7 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)7 IOException (java.io.IOException)7 Repository (org.eclipse.jgit.lib.Repository)7 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)6 Config (org.eclipse.jgit.lib.Config)6 CachedProjectConfig (com.google.gerrit.entities.CachedProjectConfig)5 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)5 NotifyConfig (com.google.gerrit.entities.NotifyConfig)4 ArrayList (java.util.ArrayList)4 Account (com.google.gerrit.entities.Account)3 BooleanProjectConfig (com.google.gerrit.entities.BooleanProjectConfig)3 Permission (com.google.gerrit.entities.Permission)3 PermissionRule (com.google.gerrit.entities.PermissionRule)3 ProjectAccessInfo (com.google.gerrit.extensions.api.access.ProjectAccessInfo)3