Search in sources :

Example 31 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class ReceiveCommits method validateConfigPush.

/**
 * Validates a push to refs/meta/config, and reject the command if it fails.
 */
private void validateConfigPush(ReceiveCommand cmd) throws PermissionBackendException {
    try (TraceTimer traceTimer = newTimer("validateConfigPush")) {
        logger.atFine().log("Processing %s command", cmd.getRefName());
        try {
            permissions.check(ProjectPermission.WRITE_CONFIG);
        } catch (AuthException e) {
            reject(cmd, String.format("must be either project owner or have %s permission", ProjectPermission.WRITE_CONFIG.describeForException()));
            return;
        }
        switch(cmd.getType()) {
            case CREATE:
            case UPDATE:
            case UPDATE_NONFASTFORWARD:
                try {
                    ProjectConfig cfg = projectConfigFactory.create(project.getNameKey());
                    cfg.load(project.getNameKey(), receivePack.getRevWalk(), cmd.getNewId());
                    if (!cfg.getValidationErrors().isEmpty()) {
                        addError("Invalid project configuration:");
                        for (ValidationError err : cfg.getValidationErrors()) {
                            addError("  " + err.getMessage());
                        }
                        reject(cmd, "invalid project configuration");
                        logger.atSevere().log("User %s tried to push invalid project configuration %s for %s", user.getLoggableName(), cmd.getNewId().name(), project.getName());
                        return;
                    }
                    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");
                            return;
                        }
                    } else {
                        if (!oldParent.equals(newParent)) {
                            if (allowProjectOwnersToChangeParent) {
                                try {
                                    permissionBackend.user(user).project(project.getNameKey()).check(ProjectPermission.WRITE_CONFIG);
                                } catch (AuthException e) {
                                    reject(cmd, "invalid project configuration: only project owners can set parent");
                                    return;
                                }
                            } else {
                                try {
                                    permissionBackend.user(user).check(GlobalPermission.ADMINISTRATE_SERVER);
                                } catch (AuthException e) {
                                    reject(cmd, "invalid project configuration: only Gerrit admin can set parent");
                                    return;
                                }
                            }
                        }
                        if (!projectCache.get(newParent).isPresent()) {
                            reject(cmd, "invalid project configuration: parent does not exist");
                            return;
                        }
                    }
                    validatePluginConfig(cmd, cfg);
                } catch (Exception e) {
                    reject(cmd, "invalid project configuration");
                    logger.atSevere().withCause(e).log("User %s tried to push invalid project configuration %s for %s", user.getLoggableName(), cmd.getNewId().name(), project.getName());
                    return;
                }
                break;
            case DELETE:
                break;
            default:
                reject(cmd, "prohibited by Gerrit: don't know how to handle config update of type " + cmd.getType());
        }
    }
}
Also used : BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Project(com.google.gerrit.entities.Project) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) AuthException(com.google.gerrit.extensions.restapi.AuthException) ValidationError(com.google.gerrit.server.git.ValidationError) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RequestCancelledException(com.google.gerrit.server.cancellation.RequestCancelledException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) UpdateException(com.google.gerrit.server.update.UpdateException) AuthException(com.google.gerrit.extensions.restapi.AuthException) PermissionDeniedException(com.google.gerrit.server.permissions.PermissionDeniedException) CmdLineException(org.kohsuke.args4j.CmdLineException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) InvalidDeadlineException(com.google.gerrit.server.InvalidDeadlineException) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RefOperationValidationException(com.google.gerrit.server.git.validators.RefOperationValidationException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) StorageException(com.google.gerrit.exceptions.StorageException) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException)

Example 32 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class SubmitStrategyOp method checkProjectConfig.

private void checkProjectConfig(RepoContext ctx, CodeReviewCommit commit) {
    String refName = getDest().branch();
    if (RefNames.REFS_CONFIG.equals(refName)) {
        logger.atFine().log("Loading new configuration from %s", RefNames.REFS_CONFIG);
        try {
            ProjectConfig cfg = args.projectConfigFactory.create(getProject());
            cfg.load(ctx.getRevWalk(), commit);
        } catch (Exception e) {
            throw new StorageException("Submit would store invalid" + " project configuration " + commit.name() + " for " + getProject(), e);
        }
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) StorageException(com.google.gerrit.exceptions.StorageException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) AuthException(com.google.gerrit.extensions.restapi.AuthException) StorageException(com.google.gerrit.exceptions.StorageException) IOException(java.io.IOException)

Example 33 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class AccessIT method grantRevertPermissionOnlyWorksOnce.

@Test
public void grantRevertPermissionOnlyWorksOnce() throws Exception {
    grantRevertPermission.execute(newProjectName);
    grantRevertPermission.execute(newProjectName);
    try (Repository repo = repoManager.openRepository(newProjectName)) {
        MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, newProjectName, repo);
        ProjectConfig projectConfig = projectConfigFactory.read(md);
        AccessSection all = projectConfig.getAccessSection(AccessSection.ALL);
        Permission permission = all.getPermission(Permission.REVERT);
        assertThat(permission.getRules()).hasSize(1);
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) GrantRevertPermission(com.google.gerrit.server.schema.GrantRevertPermission) Permission(com.google.gerrit.entities.Permission) AccessSection(com.google.gerrit.entities.AccessSection) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 34 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class SubmitRequirementsValidationIT method readProjectConfig.

private Config readProjectConfig(RevCommit commit) throws Exception {
    try (TreeWalk tw = TreeWalk.forPath(testRepo.getRevWalk().getObjectReader(), ProjectConfig.PROJECT_CONFIG, commit.getTree())) {
        if (tw == null) {
            throw new IllegalStateException(String.format("%s does not exist", ProjectConfig.PROJECT_CONFIG));
        }
    }
    RevObject blob = testRepo.get(commit.getTree(), ProjectConfig.PROJECT_CONFIG);
    byte[] data = testRepo.getRepository().open(blob).getCachedBytes(Integer.MAX_VALUE);
    String content = RawParseUtils.decode(data);
    Config projectConfig = new Config();
    projectConfig.fromText(content);
    return projectConfig;
}
Also used : RevObject(org.eclipse.jgit.revwalk.RevObject) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) Config(org.eclipse.jgit.lib.Config) TreeWalk(org.eclipse.jgit.treewalk.TreeWalk)

Example 35 with ProjectConfig

use of com.google.gerrit.server.project.ProjectConfig in project gerrit by GerritCodeReview.

the class RefControlTest method setUp.

@Before
public void setUp() throws Exception {
    Injector injector = Guice.createInjector(new InMemoryModule());
    injector.injectMembers(this);
    // Tests previously used ProjectConfig.Factory to create ProjectConfigs without going through
    // the ProjectCache, which was wrong. Manually call getInstance so we don't store it in a
    // field that is accessible to test methods.
    ProjectConfig.Factory projectConfigFactory = injector.getInstance(ProjectConfig.Factory.class);
    singleVersionListener.start();
    try {
        schemaCreator.create();
    } finally {
        singleVersionListener.stop();
    }
    // changing defaults in SchemaCreator or ProjectCreator.
    try (Repository allProjectsRepo = repoManager.createRepository(allProjectsName);
        TestRepository<Repository> tr = new TestRepository<>(allProjectsRepo)) {
        tr.delete(REFS_CONFIG);
        try (MetaDataUpdate md = metaDataUpdateFactory.create(allProjectsName)) {
            ProjectConfig allProjectsConfig = projectConfigFactory.create(allProjectsName);
            allProjectsConfig.load(md);
            LabelType cr = TestLabels.codeReview();
            allProjectsConfig.upsertLabelType(cr);
            allProjectsConfig.commit(md);
        }
    }
    repoManager.createRepository(parentKey).close();
    repoManager.createRepository(localKey).close();
    try (MetaDataUpdate md = metaDataUpdateFactory.create(localKey)) {
        ProjectConfig newLocal = projectConfigFactory.create(localKey);
        newLocal.load(md);
        newLocal.updateProject(p -> p.setParent(parentKey));
        newLocal.commit(md);
    }
    requestContext.setContext(() -> null);
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) TestRepository(org.eclipse.jgit.junit.TestRepository) Injector(com.google.inject.Injector) LabelType(com.google.gerrit.entities.LabelType) InMemoryModule(com.google.gerrit.testing.InMemoryModule) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) Before(org.junit.Before)

Aggregations

ProjectConfig (com.google.gerrit.server.project.ProjectConfig)39 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)26 AuthException (com.google.gerrit.extensions.restapi.AuthException)8 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)8 Project (com.google.gerrit.entities.Project)7 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)7 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)7 Test (org.junit.Test)7 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)6 AccessSection (com.google.gerrit.entities.AccessSection)6 BooleanProjectConfig (com.google.gerrit.entities.BooleanProjectConfig)6 LabelType (com.google.gerrit.entities.LabelType)6 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)6 Repository (org.eclipse.jgit.lib.Repository)6 GroupReference (com.google.gerrit.entities.GroupReference)5 Permission (com.google.gerrit.entities.Permission)5 IOException (java.io.IOException)5 ProjectState (com.google.gerrit.server.project.ProjectState)4 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)4 TestRepository (org.eclipse.jgit.junit.TestRepository)4