Search in sources :

Example 31 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gerrit by GerritCodeReview.

the class GerritServerConfigModule method getSecureStoreFromGerritConfig.

private static String getSecureStoreFromGerritConfig(Path sitePath) {
    try {
        SitePaths site = new SitePaths(sitePath);
        FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
        if (!cfg.getFile().exists()) {
            return DefaultSecureStore.class.getName();
        }
        cfg.load();
        String className = cfg.getString("gerrit", null, "secureStoreClass");
        return nullToDefault(className);
    } catch (IOException | ConfigInvalidException e) {
        throw new ProvisionException(e.getMessage(), e);
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IOException(java.io.IOException) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig)

Example 32 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gerrit by GerritCodeReview.

the class FileBasedGlobalPluginConfigProvider method get.

@Override
public Config get(String pluginName) {
    Path pluginConfigFile = site.etc_dir.resolve(pluginName + ".config");
    FileBasedConfig cfg = new FileBasedConfig(pluginConfigFile.toFile(), FS.DETECTED);
    if (!cfg.getFile().exists()) {
        logger.atInfo().log("No %s; assuming defaults", pluginConfigFile.toAbsolutePath());
        return cfg;
    }
    try {
        cfg.load();
    } catch (ConfigInvalidException e) {
        // This is an error in user input, don't spam logs with a stack trace.
        logger.atWarning().log("Failed to load %s: %s", pluginConfigFile.toAbsolutePath(), e.getMessage());
    } catch (IOException e) {
        logger.atWarning().withCause(e).log("Failed to load %s", pluginConfigFile.toAbsolutePath());
    }
    return cfg;
}
Also used : Path(java.nio.file.Path) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) IOException(java.io.IOException) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig)

Example 33 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gerrit by GerritCodeReview.

the class AbstractReindexTests method updateConfig.

private void updateConfig(Consumer<Config> configConsumer) throws Exception {
    FileBasedConfig cfg = new FileBasedConfig(sitePaths.gerrit_config.toFile(), FS.detect());
    cfg.load();
    configConsumer.accept(cfg);
    cfg.save();
}
Also used : FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig)

Example 34 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gitblit by gitblit.

the class ProjectManager method start.

@Override
public ProjectManager start() {
    // load and cache the project metadata
    projectConfigs = new FileBasedConfig(runtimeManager.getFileOrFolder(Keys.web.projectsFile, "${baseFolder}/projects.conf"), FS.detect());
    getProjectConfigs();
    return this;
}
Also used : FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig)

Example 35 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gitblit by gitblit.

the class ConfigUserService method read.

/**
 * Reads the realm file and rebuilds the in-memory lookup tables.
 */
protected synchronized void read() {
    if (realmFile.exists() && (forceReload || (realmFile.lastModified() != lastModified))) {
        forceReload = false;
        lastModified = realmFile.lastModified();
        users.clear();
        cookies.clear();
        teams.clear();
        try {
            StoredConfig config = new FileBasedConfig(realmFile, FS.detect());
            config.load();
            Set<String> usernames = config.getSubsections(USER);
            for (String username : usernames) {
                UserModel user = new UserModel(username.toLowerCase());
                user.password = config.getString(USER, username, PASSWORD);
                user.displayName = config.getString(USER, username, DISPLAYNAME);
                user.emailAddress = config.getString(USER, username, EMAILADDRESS);
                user.accountType = AccountType.fromString(config.getString(USER, username, ACCOUNTTYPE));
                user.disabled = config.getBoolean(USER, username, DISABLED, false);
                user.organizationalUnit = config.getString(USER, username, ORGANIZATIONALUNIT);
                user.organization = config.getString(USER, username, ORGANIZATION);
                user.locality = config.getString(USER, username, LOCALITY);
                user.stateProvince = config.getString(USER, username, STATEPROVINCE);
                user.countryCode = config.getString(USER, username, COUNTRYCODE);
                user.cookie = config.getString(USER, username, COOKIE);
                if (StringUtils.isEmpty(user.cookie) && !StringUtils.isEmpty(user.password)) {
                    user.cookie = user.createCookie();
                }
                // preferences
                user.getPreferences().setLocale(config.getString(USER, username, LOCALE));
                user.getPreferences().setEmailMeOnMyTicketChanges(config.getBoolean(USER, username, EMAILONMYTICKETCHANGES, true));
                user.getPreferences().setTransport(Transport.fromString(config.getString(USER, username, TRANSPORT)));
                // user roles
                Set<String> roles = new HashSet<String>(Arrays.asList(config.getStringList(USER, username, ROLE)));
                user.canAdmin = roles.contains(Role.ADMIN.getRole());
                user.canFork = roles.contains(Role.FORK.getRole());
                user.canCreate = roles.contains(Role.CREATE.getRole());
                user.excludeFromFederation = roles.contains(Role.NOT_FEDERATED.getRole());
                // repository memberships
                if (!user.canAdmin) {
                    // non-admin, read permissions
                    Set<String> repositories = new HashSet<String>(Arrays.asList(config.getStringList(USER, username, REPOSITORY)));
                    for (String repository : repositories) {
                        user.addRepositoryPermission(repository);
                    }
                }
                // starred repositories
                Set<String> starred = new HashSet<String>(Arrays.asList(config.getStringList(USER, username, STARRED)));
                for (String repository : starred) {
                    UserRepositoryPreferences prefs = user.getPreferences().getRepositoryPreferences(repository);
                    prefs.starred = true;
                }
                // update cache
                users.put(user.username, user);
                if (!StringUtils.isEmpty(user.cookie)) {
                    cookies.put(user.cookie, user);
                }
            }
            // load the teams
            Set<String> teamnames = config.getSubsections(TEAM);
            for (String teamname : teamnames) {
                TeamModel team = new TeamModel(teamname);
                Set<String> roles = new HashSet<String>(Arrays.asList(config.getStringList(TEAM, teamname, ROLE)));
                team.canAdmin = roles.contains(Role.ADMIN.getRole());
                team.canFork = roles.contains(Role.FORK.getRole());
                team.canCreate = roles.contains(Role.CREATE.getRole());
                team.accountType = AccountType.fromString(config.getString(TEAM, teamname, ACCOUNTTYPE));
                if (!team.canAdmin) {
                    // non-admin team, read permissions
                    team.addRepositoryPermissions(Arrays.asList(config.getStringList(TEAM, teamname, REPOSITORY)));
                }
                team.addUsers(Arrays.asList(config.getStringList(TEAM, teamname, USER)));
                team.addMailingLists(Arrays.asList(config.getStringList(TEAM, teamname, MAILINGLIST)));
                team.preReceiveScripts.addAll(Arrays.asList(config.getStringList(TEAM, teamname, PRERECEIVE)));
                team.postReceiveScripts.addAll(Arrays.asList(config.getStringList(TEAM, teamname, POSTRECEIVE)));
                teams.put(team.name.toLowerCase(), team);
                // set the teams on the users
                for (String user : team.users) {
                    UserModel model = users.get(user);
                    if (model != null) {
                        model.teams.add(team);
                    }
                }
            }
        } catch (Exception e) {
            logger.error(MessageFormat.format("Failed to read {0}", realmFile), e);
        }
    }
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) UserModel(com.gitblit.models.UserModel) TeamModel(com.gitblit.models.TeamModel) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) UserRepositoryPreferences(com.gitblit.models.UserRepositoryPreferences) IOException(java.io.IOException) HashSet(java.util.HashSet)

Aggregations

FileBasedConfig (org.eclipse.jgit.storage.file.FileBasedConfig)47 File (java.io.File)19 IOException (java.io.IOException)15 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)12 Config (org.eclipse.jgit.lib.Config)10 FS (org.eclipse.jgit.util.FS)7 Test (org.junit.Test)7 StoredConfig (org.eclipse.jgit.lib.StoredConfig)6 Path (java.nio.file.Path)5 Repository (org.eclipse.jgit.lib.Repository)5 SitePaths (com.google.gerrit.server.config.SitePaths)4 HashMap (java.util.HashMap)4 UserModel (com.gitblit.models.UserModel)3 FileBasedAllProjectsConfigProvider (com.google.gerrit.server.config.FileBasedAllProjectsConfigProvider)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 SystemReader (org.eclipse.jgit.util.SystemReader)3 Before (org.junit.Before)3