Search in sources :

Example 1 with PluginConfig

use of com.google.gerrit.server.config.PluginConfig in project gerrit by GerritCodeReview.

the class ReceiveCommits method parseCommands.

private void parseCommands(Collection<ReceiveCommand> commands) throws PermissionBackendException {
    List<String> optionList = rp.getPushOptions();
    if (optionList != null) {
        for (String option : optionList) {
            int e = option.indexOf('=');
            if (e > 0) {
                pushOptions.put(option.substring(0, e), option.substring(e + 1));
            } else {
                pushOptions.put(option, "");
            }
        }
    }
    logDebug("Parsing {} commands", commands.size());
    for (ReceiveCommand cmd : commands) {
        if (cmd.getResult() != NOT_ATTEMPTED) {
            // Already rejected by the core receive process.
            logDebug("Already processed by core: {} {}", cmd.getResult(), cmd);
            continue;
        }
        if (!Repository.isValidRefName(cmd.getRefName()) || cmd.getRefName().contains("//")) {
            reject(cmd, "not valid ref");
            continue;
        }
        if (MagicBranch.isMagicBranch(cmd.getRefName())) {
            parseMagicBranch(cmd);
            continue;
        }
        if (projectControl.getProjectState().isAllUsers() && RefNames.REFS_USERS_SELF.equals(cmd.getRefName())) {
            String newName = RefNames.refsUsers(user.getAccountId());
            logDebug("Swapping out command for {} to {}", RefNames.REFS_USERS_SELF, newName);
            final ReceiveCommand orgCmd = cmd;
            cmd = new ReceiveCommand(cmd.getOldId(), cmd.getNewId(), newName, cmd.getType()) {

                @Override
                public void setResult(Result s, String m) {
                    super.setResult(s, m);
                    orgCmd.setResult(s, m);
                }
            };
        }
        Matcher m = NEW_PATCHSET.matcher(cmd.getRefName());
        if (m.matches()) {
            // The referenced change must exist and must still be open.
            //
            Change.Id changeId = Change.Id.parse(m.group(1));
            parseReplaceCommand(cmd, changeId);
            continue;
        }
        switch(cmd.getType()) {
            case CREATE:
                parseCreate(cmd);
                break;
            case UPDATE:
                parseUpdate(cmd);
                break;
            case DELETE:
                parseDelete(cmd);
                break;
            case UPDATE_NONFASTFORWARD:
                parseRewind(cmd);
                break;
            default:
                reject(cmd, "prohibited by Gerrit: unknown command type " + cmd.getType());
                continue;
        }
        if (cmd.getResult() != NOT_ATTEMPTED) {
            continue;
        }
        if (isConfig(cmd)) {
            logDebug("Processing {} command", cmd.getRefName());
            if (!projectControl.isOwner()) {
                reject(cmd, "not project owner");
                continue;
            }
            switch(cmd.getType()) {
                case CREATE:
                case UPDATE:
                case UPDATE_NONFASTFORWARD:
                    try {
                        ProjectConfig cfg = new ProjectConfig(project.getNameKey());
                        cfg.load(rp.getRevWalk(), cmd.getNewId());
                        if (!cfg.getValidationErrors().isEmpty()) {
                            addError("Invalid project configuration:");
                            for (ValidationError err : cfg.getValidationErrors()) {
                                addError("  " + err.getMessage());
                            }
                            reject(cmd, "invalid project configuration");
                            logError("User " + user.getUserName() + " tried to push invalid project configuration " + cmd.getNewId().name() + " for " + project.getName());
                            continue;
                        }
                        Project.NameKey newParent = cfg.getProject().getParent(allProjectsName);
                        Project.NameKey oldParent = project.getParent(allProjectsName);
                        if (oldParent == null) {
                            // update of the 'All-Projects' project
                            if (newParent != null) {
                                reject(cmd, "invalid project configuration: root project cannot have parent");
                                continue;
                            }
                        } else {
                            if (!oldParent.equals(newParent)) {
                                try {
                                    permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
                                } catch (AuthException e) {
                                    reject(cmd, "invalid project configuration: only Gerrit admin can set parent");
                                    continue;
                                }
                            }
                            if (projectCache.get(newParent) == null) {
                                reject(cmd, "invalid project configuration: parent does not exist");
                                continue;
                            }
                        }
                        for (Entry<ProjectConfigEntry> e : pluginConfigEntries) {
                            PluginConfig pluginCfg = cfg.getPluginConfig(e.getPluginName());
                            ProjectConfigEntry configEntry = e.getProvider().get();
                            String value = pluginCfg.getString(e.getExportName());
                            String oldValue = projectControl.getProjectState().getConfig().getPluginConfig(e.getPluginName()).getString(e.getExportName());
                            if (configEntry.getType() == ProjectConfigEntryType.ARRAY) {
                                oldValue = Arrays.stream(projectControl.getProjectState().getConfig().getPluginConfig(e.getPluginName()).getStringList(e.getExportName())).collect(joining("\n"));
                            }
                            if ((value == null ? oldValue != null : !value.equals(oldValue)) && !configEntry.isEditable(projectControl.getProjectState())) {
                                reject(cmd, String.format("invalid project configuration: Not allowed to set parameter" + " '%s' of plugin '%s' on project '%s'.", e.getExportName(), e.getPluginName(), project.getName()));
                                continue;
                            }
                            if (ProjectConfigEntryType.LIST.equals(configEntry.getType()) && value != null && !configEntry.getPermittedValues().contains(value)) {
                                reject(cmd, String.format("invalid project configuration: The value '%s' is " + "not permitted for parameter '%s' of plugin '%s'.", value, e.getExportName(), e.getPluginName()));
                            }
                        }
                    } catch (Exception e) {
                        reject(cmd, "invalid project configuration");
                        logError("User " + user.getUserName() + " tried to push invalid project configuration " + cmd.getNewId().name() + " for " + project.getName(), e);
                        continue;
                    }
                    break;
                case DELETE:
                    break;
                default:
                    reject(cmd, "prohibited by Gerrit: don't know how to handle config update of type " + cmd.getType());
                    continue;
            }
        }
    }
}
Also used : ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) Matcher(java.util.regex.Matcher) AuthException(com.google.gerrit.extensions.restapi.AuthException) Change(com.google.gerrit.reviewdb.client.Change) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RefOperationValidationException(com.google.gerrit.server.git.validators.RefOperationValidationException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) OrmException(com.google.gwtorm.server.OrmException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) UpdateException(com.google.gerrit.server.update.UpdateException) AuthException(com.google.gerrit.extensions.restapi.AuthException) CmdLineException(org.kohsuke.args4j.CmdLineException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) CommitValidationException(com.google.gerrit.server.git.validators.CommitValidationException) ServiceMayNotContinueException(org.eclipse.jgit.transport.ServiceMayNotContinueException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) Result(org.eclipse.jgit.transport.ReceiveCommand.Result) PluginConfig(com.google.gerrit.server.config.PluginConfig) Project(com.google.gerrit.reviewdb.client.Project) ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry)

Example 2 with PluginConfig

use of com.google.gerrit.server.config.PluginConfig in project gerrit by GerritCodeReview.

the class ProjectConfig method loadPluginSections.

private void loadPluginSections(Config rc) {
    pluginConfigs = new HashMap<>();
    for (String plugin : rc.getSubsections(PLUGIN)) {
        Config pluginConfig = new Config();
        pluginConfigs.put(plugin, pluginConfig);
        for (String name : rc.getNames(PLUGIN, plugin)) {
            String value = rc.getString(PLUGIN, plugin, name);
            if (value.startsWith("Group[")) {
                GroupReference refFromString = GroupReference.fromString(value);
                GroupReference ref = groupList.byUUID(refFromString.getUUID());
                if (ref == null) {
                    ref = refFromString;
                    error(new ValidationError(PROJECT_CONFIG, "group \"" + ref.getName() + "\" not in " + GroupList.FILE_NAME));
                }
                rc.setString(PLUGIN, plugin, name, ref.toString());
            }
            pluginConfig.setStringList(PLUGIN, plugin, name, Arrays.asList(rc.getStringList(PLUGIN, plugin, name)));
        }
    }
}
Also used : Config(org.eclipse.jgit.lib.Config) PluginConfig(com.google.gerrit.server.config.PluginConfig) GroupReference(com.google.gerrit.common.data.GroupReference)

Example 3 with PluginConfig

use of com.google.gerrit.server.config.PluginConfig in project gerrit by GerritCodeReview.

the class ProjectConfig method savePluginSections.

private void savePluginSections(Config rc, Set<AccountGroup.UUID> keepGroups) {
    List<String> existing = Lists.newArrayList(rc.getSubsections(PLUGIN));
    for (String name : existing) {
        rc.unsetSection(PLUGIN, name);
    }
    for (Entry<String, Config> e : pluginConfigs.entrySet()) {
        String plugin = e.getKey();
        Config pluginConfig = e.getValue();
        for (String name : pluginConfig.getNames(PLUGIN, plugin)) {
            String value = pluginConfig.getString(PLUGIN, plugin, name);
            if (value.startsWith("Group[")) {
                GroupReference ref = resolve(GroupReference.fromString(value));
                if (ref.getUUID() != null) {
                    keepGroups.add(ref.getUUID());
                    pluginConfig.setString(PLUGIN, plugin, name, ref.toString());
                }
            }
            rc.setStringList(PLUGIN, plugin, name, Arrays.asList(pluginConfig.getStringList(PLUGIN, plugin, name)));
        }
    }
}
Also used : Config(org.eclipse.jgit.lib.Config) PluginConfig(com.google.gerrit.server.config.PluginConfig) GroupReference(com.google.gerrit.common.data.GroupReference)

Example 4 with PluginConfig

use of com.google.gerrit.server.config.PluginConfig in project gerrit by GerritCodeReview.

the class ConfigInfoImpl method getPluginConfig.

private Map<String, Map<String, ConfigParameterInfo>> getPluginConfig(ProjectState project, DynamicMap<ProjectConfigEntry> pluginConfigEntries, PluginConfigFactory cfgFactory, AllProjectsName allProjects) {
    TreeMap<String, Map<String, ConfigParameterInfo>> pluginConfig = new TreeMap<>();
    for (Entry<ProjectConfigEntry> e : pluginConfigEntries) {
        ProjectConfigEntry configEntry = e.getProvider().get();
        PluginConfig cfg = cfgFactory.getFromProjectConfig(project, e.getPluginName());
        String configuredValue = cfg.getString(e.getExportName());
        ConfigParameterInfo p = new ConfigParameterInfo();
        p.displayName = configEntry.getDisplayName();
        p.description = configEntry.getDescription();
        p.warning = configEntry.getWarning(project);
        p.type = configEntry.getType();
        p.permittedValues = configEntry.getPermittedValues();
        p.editable = configEntry.isEditable(project) ? true : null;
        if (configEntry.isInheritable() && !allProjects.equals(project.getProject().getNameKey())) {
            PluginConfig cfgWithInheritance = cfgFactory.getFromProjectConfigWithInheritance(project, e.getPluginName());
            p.inheritable = true;
            p.value = configEntry.onRead(project, cfgWithInheritance.getString(e.getExportName(), configEntry.getDefaultValue()));
            p.configuredValue = configuredValue;
            p.inheritedValue = getInheritedValue(project, cfgFactory, e);
        } else {
            if (configEntry.getType() == ProjectConfigEntryType.ARRAY) {
                p.values = configEntry.onRead(project, Arrays.asList(cfg.getStringList(e.getExportName())));
            } else {
                p.value = configEntry.onRead(project, configuredValue != null ? configuredValue : configEntry.getDefaultValue());
            }
        }
        Map<String, ConfigParameterInfo> pc = pluginConfig.get(e.getPluginName());
        if (pc == null) {
            pc = new TreeMap<>();
            pluginConfig.put(e.getPluginName(), pc);
        }
        pc.put(e.getExportName(), p);
    }
    return !pluginConfig.isEmpty() ? pluginConfig : null;
}
Also used : PluginConfig(com.google.gerrit.server.config.PluginConfig) ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry) TreeMap(java.util.TreeMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap) Map(java.util.Map)

Example 5 with PluginConfig

use of com.google.gerrit.server.config.PluginConfig in project gerrit by GerritCodeReview.

the class ReceiveCommits method validatePluginConfig.

/**
 * validates a push to refs/meta/config for plugin configuration, and rejects the push if it
 * fails.
 */
private void validatePluginConfig(ReceiveCommand cmd, ProjectConfig cfg) {
    for (Extension<ProjectConfigEntry> e : pluginConfigEntries) {
        PluginConfig pluginCfg = cfg.getPluginConfig(e.getPluginName());
        ProjectConfigEntry configEntry = e.getProvider().get();
        String value = pluginCfg.getString(e.getExportName());
        String oldValue = projectState.getPluginConfig(e.getPluginName()).getString(e.getExportName());
        if (configEntry.getType() == ProjectConfigEntryType.ARRAY) {
            oldValue = Arrays.stream(projectState.getPluginConfig(e.getPluginName()).getStringList(e.getExportName())).collect(joining("\n"));
        }
        if ((value == null ? oldValue != null : !value.equals(oldValue)) && !configEntry.isEditable(projectState)) {
            reject(cmd, String.format("invalid project configuration: Not allowed to set parameter" + " '%s' of plugin '%s' on project '%s'.", e.getExportName(), e.getPluginName(), project.getName()));
            continue;
        }
        if (ProjectConfigEntryType.LIST.equals(configEntry.getType()) && value != null && !configEntry.getPermittedValues().contains(value)) {
            reject(cmd, String.format("invalid project configuration: The value '%s' is " + "not permitted for parameter '%s' of plugin '%s'.", value, e.getExportName(), e.getPluginName()));
        }
    }
}
Also used : PluginConfig(com.google.gerrit.server.config.PluginConfig) ProjectConfigEntry(com.google.gerrit.server.config.ProjectConfigEntry)

Aggregations

PluginConfig (com.google.gerrit.server.config.PluginConfig)19 ProjectConfigEntry (com.google.gerrit.server.config.ProjectConfigEntry)7 Config (org.eclipse.jgit.lib.Config)6 Test (org.junit.Test)5 GroupReference (com.google.gerrit.entities.GroupReference)4 Map (java.util.Map)4 BooleanProjectConfig (com.google.gerrit.entities.BooleanProjectConfig)3 CachedProjectConfig (com.google.gerrit.entities.CachedProjectConfig)3 NotifyConfig (com.google.gerrit.entities.NotifyConfig)3 DynamicMap (com.google.gerrit.extensions.registration.DynamicMap)3 LinkedHashMap (java.util.LinkedHashMap)3 StoredConfig (org.eclipse.jgit.lib.StoredConfig)3 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)2 GroupReference (com.google.gerrit.common.data.GroupReference)2 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)2 TreeMap (java.util.TreeMap)2 RevCommit (org.eclipse.jgit.revwalk.RevCommit)2 ConfigParameterInfo (com.google.gerrit.extensions.api.projects.ConfigInfo.ConfigParameterInfo)1 ConfigValue (com.google.gerrit.extensions.api.projects.ConfigValue)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1