Search in sources :

Example 1 with PermissionRule

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

the class CreateGroupPermissionSyncer method syncIfNeeded.

/**
 * Checks if {@code GlobalCapability.CREATE_GROUP} and {@code CREATE} permission on {@code
 * refs/groups/*} have diverged and syncs them by applying the {@code CREATE} permission to {@code
 * refs/groups/*}.
 */
public void syncIfNeeded() throws IOException, ConfigInvalidException {
    ProjectState allProjectsState = projectCache.getAllProjects();
    ProjectState allUsersState = projectCache.getAllUsers();
    Set<PermissionRule> createGroupsGlobal = new HashSet<>(allProjectsState.getCapabilityCollection().createGroup);
    Set<PermissionRule> createGroupsRef = new HashSet<>();
    Optional<AccessSection> allUsersCreateGroupAccessSection = allUsersState.getConfig().getAccessSection(RefNames.REFS_GROUPS + "*");
    if (allUsersCreateGroupAccessSection.isPresent()) {
        Permission create = allUsersCreateGroupAccessSection.get().getPermission(Permission.CREATE);
        if (create != null && create.getRules() != null) {
            createGroupsRef.addAll(create.getRules());
        }
    }
    if (Sets.symmetricDifference(createGroupsGlobal, createGroupsRef).isEmpty()) {
        // Nothing to sync
        return;
    }
    try (MetaDataUpdate md = metaDataUpdateFactory.get().create(allUsers)) {
        ProjectConfig config = projectConfigFactory.read(md);
        config.upsertAccessSection(RefNames.REFS_GROUPS + "*", refsGroupsAccessSectionBuilder -> {
            if (createGroupsGlobal.isEmpty()) {
                refsGroupsAccessSectionBuilder.modifyPermissions(permissions -> {
                    permissions.removeIf(p -> Permission.CREATE.equals(p.getName()));
                });
            } else {
                // The create permission is managed by Gerrit at this point only so there is no
                // concern of overwriting user-defined permissions here.
                Permission.Builder createGroupPermission = Permission.builder(Permission.CREATE);
                refsGroupsAccessSectionBuilder.remove(createGroupPermission);
                refsGroupsAccessSectionBuilder.addPermission(createGroupPermission);
                createGroupsGlobal.stream().map(p -> p.toBuilder()).forEach(createGroupPermission::add);
            }
        });
        config.commit(md);
        projectCache.evictAndReindex(config.getProject());
    }
}
Also used : ProjectConfig(com.google.gerrit.server.project.ProjectConfig) AllUsersName(com.google.gerrit.server.config.AllUsersName) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ProjectCache(com.google.gerrit.server.project.ProjectCache) AccessSection(com.google.gerrit.entities.AccessSection) Inject(com.google.inject.Inject) Permission(com.google.gerrit.entities.Permission) ProjectState(com.google.gerrit.server.project.ProjectState) Set(java.util.Set) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) ChangeMergedListener(com.google.gerrit.extensions.events.ChangeMergedListener) HashSet(java.util.HashSet) Provider(com.google.inject.Provider) AllProjectsName(com.google.gerrit.server.config.AllProjectsName) PermissionRule(com.google.gerrit.entities.PermissionRule) RefNames(com.google.gerrit.entities.RefNames) Optional(java.util.Optional) FluentLogger(com.google.common.flogger.FluentLogger) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) Singleton(com.google.inject.Singleton) PermissionRule(com.google.gerrit.entities.PermissionRule) Permission(com.google.gerrit.entities.Permission) ProjectState(com.google.gerrit.server.project.ProjectState) AccessSection(com.google.gerrit.entities.AccessSection) HashSet(java.util.HashSet) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 2 with PermissionRule

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

the class AccountLimits method getRules.

private List<PermissionRule> getRules(String permissionName) {
    List<PermissionRule> rules = capabilities.getPermission(permissionName);
    GroupMembership groups = user.getEffectiveGroups();
    List<PermissionRule> mine = new ArrayList<>(rules.size());
    for (PermissionRule rule : rules) {
        if (match(groups, rule)) {
            mine.add(rule);
        }
    }
    return mine;
}
Also used : PermissionRule(com.google.gerrit.entities.PermissionRule) ArrayList(java.util.ArrayList)

Example 3 with PermissionRule

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

the class PermissionCollection method calculateAllowRules.

/**
 * calculates permissions for ALLOW processing.
 */
private List<PermissionRule> calculateAllowRules(String permName) {
    Set<SeenRule> seen = new HashSet<>();
    List<PermissionRule> r = new ArrayList<>();
    for (AccessSection s : accessSectionsUpward) {
        Permission p = s.getPermission(permName);
        if (p == null) {
            continue;
        }
        for (PermissionRule pr : p.getRules()) {
            SeenRule sr = SeenRule.create(s, pr);
            if (seen.contains(sr)) {
                // negating access.
                continue;
            }
            seen.add(sr);
            if (pr.getAction() == BLOCK) {
                // Block rules are handled elsewhere.
                continue;
            }
            if (pr.getAction() == PermissionRule.Action.DENY) {
                // DENY rules work by not adding ALLOW rules. Nothing else to do.
                continue;
            }
            r.add(pr);
        }
        if (p.getExclusiveGroup()) {
            // We found an exclusive permission, so no need to further go up the hierarchy.
            break;
        }
    }
    return r;
}
Also used : PermissionRule(com.google.gerrit.entities.PermissionRule) ArrayList(java.util.ArrayList) Permission(com.google.gerrit.entities.Permission) AccessSection(com.google.gerrit.entities.AccessSection) HashSet(java.util.HashSet)

Example 4 with PermissionRule

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

the class PermissionCollection method calculateBlockRules.

// Calculates the inputs for determining BLOCK status, grouped by project.
private List<List<Permission>> calculateBlockRules(String permName) {
    List<List<Permission>> result = new ArrayList<>();
    for (List<AccessSection> secs : this.accessSectionsPerProjectDownward) {
        List<Permission> perms = new ArrayList<>();
        boolean blockFound = false;
        for (AccessSection sec : secs) {
            Permission p = sec.getPermission(permName);
            if (p == null) {
                continue;
            }
            for (PermissionRule pr : p.getRules()) {
                if (blockFound || pr.getAction() == Action.BLOCK) {
                    blockFound = true;
                    break;
                }
            }
            perms.add(p);
        }
        if (blockFound) {
            result.add(perms);
        }
    }
    return result;
}
Also used : PermissionRule(com.google.gerrit.entities.PermissionRule) ArrayList(java.util.ArrayList) Permission(com.google.gerrit.entities.Permission) ArrayList(java.util.ArrayList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) AccessSection(com.google.gerrit.entities.AccessSection)

Example 5 with PermissionRule

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

the class ContributorAgreementsChecker method check.

/**
 * Checks if the user has signed a contributor agreement for the project.
 *
 * @throws AuthException if the user has not signed a contributor agreement for the project
 * @throws IOException if project states could not be loaded
 */
public void check(Project.NameKey project, CurrentUser user) throws IOException, AuthException {
    metrics.claCheckCount.increment();
    ProjectState projectState = projectCache.get(project).orElseThrow(() -> new IOException("Can't load " + project));
    if (!projectState.is(BooleanProjectConfig.USE_CONTRIBUTOR_AGREEMENTS)) {
        return;
    }
    if (!user.isIdentifiedUser()) {
        throw new AuthException("Must be logged in to verify Contributor Agreement");
    }
    IdentifiedUser iUser = user.asIdentifiedUser();
    Collection<ContributorAgreement> contributorAgreements = projectCache.getAllProjects().getConfig().getContributorAgreements().values();
    List<UUID> okGroupIds = new ArrayList<>();
    for (ContributorAgreement ca : contributorAgreements) {
        List<AccountGroup.UUID> groupIds;
        groupIds = okGroupIds;
        // matchProjects defaults to match all projects when missing.
        List<String> matchProjectsRegexes = ca.getMatchProjectsRegexes();
        if (!matchProjectsRegexes.isEmpty() && !projectMatchesAnyPattern(project.get(), matchProjectsRegexes)) {
            // Doesn't match, isn't checked.
            continue;
        }
        // excludeProjects defaults to exclude no projects when missing.
        List<String> excludeProjectsRegexes = ca.getExcludeProjectsRegexes();
        if (!excludeProjectsRegexes.isEmpty() && projectMatchesAnyPattern(project.get(), excludeProjectsRegexes)) {
            // Matches, isn't checked.
            continue;
        }
        for (PermissionRule rule : ca.getAccepted()) {
            if ((rule.getAction() == Action.ALLOW) && (rule.getGroup() != null) && (rule.getGroup().getUUID() != null)) {
                groupIds.add(AccountGroup.uuid(rule.getGroup().getUUID().get()));
            }
        }
    }
    if (!okGroupIds.isEmpty() && !iUser.getEffectiveGroups().containsAnyOf(okGroupIds)) {
        final StringBuilder msg = new StringBuilder();
        msg.append("No Contributor Agreement on file for user ").append(iUser.getNameEmail()).append(" (id=").append(iUser.getAccountId()).append(")");
        msg.append(urlFormatter.get().getSettingsUrl("Agreements").orElse(""));
        throw new AuthException(msg.toString());
    }
}
Also used : PermissionRule(com.google.gerrit.entities.PermissionRule) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) IOException(java.io.IOException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ContributorAgreement(com.google.gerrit.entities.ContributorAgreement) UUID(com.google.gerrit.entities.AccountGroup.UUID)

Aggregations

PermissionRule (com.google.gerrit.entities.PermissionRule)18 Permission (com.google.gerrit.entities.Permission)7 ArrayList (java.util.ArrayList)6 AccessSection (com.google.gerrit.entities.AccessSection)5 ContributorAgreement (com.google.gerrit.entities.ContributorAgreement)4 GroupReference (com.google.gerrit.entities.GroupReference)3 AuthException (com.google.gerrit.extensions.restapi.AuthException)3 HashSet (java.util.HashSet)3 ImmutableList (com.google.common.collect.ImmutableList)2 AccountGroup (com.google.gerrit.entities.AccountGroup)2 PermissionRange (com.google.gerrit.entities.PermissionRange)2 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)2 MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)2 GlobalPermission (com.google.gerrit.server.permissions.GlobalPermission)2 ProjectPermission (com.google.gerrit.server.permissions.ProjectPermission)2 RefPermission (com.google.gerrit.server.permissions.RefPermission)2 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)2 ProjectState (com.google.gerrit.server.project.ProjectState)2 IOException (java.io.IOException)2 List (java.util.List)2