Search in sources :

Example 1 with Section

use of com.google.gerrit.pgm.init.api.Section in project gerrit by GerritCodeReview.

the class SitePathInitializer method saveSecureStore.

private void saveSecureStore() throws IOException {
    if (secureStoreInitData != null) {
        Path dst = site.lib_dir.resolve(secureStoreInitData.jarFile.getFileName());
        Files.copy(secureStoreInitData.jarFile, dst);
        Section gerritSection = sectionFactory.get("gerrit", null);
        gerritSection.set("secureStoreClass", secureStoreInitData.className);
    }
}
Also used : Path(java.nio.file.Path) Section(com.google.gerrit.pgm.init.api.Section)

Example 2 with Section

use of com.google.gerrit.pgm.init.api.Section in project gerrit by GerritCodeReview.

the class UpgradeFrom2_0_x method run.

@Override
public void run() throws IOException, ConfigInvalidException {
    if (!isNeedUpgrade()) {
        return;
    }
    if (!ui.yesno(true, "Upgrade '%s'", site_path.toAbsolutePath())) {
        throw die("aborted by user");
    }
    for (String name : etcFiles) {
        Path src = site_path.resolve(name);
        Path dst = etc_dir.resolve(name);
        if (Files.exists(src)) {
            if (Files.exists(dst)) {
                throw die("File " + src + " would overwrite " + dst);
            }
            try {
                Files.move(src, dst);
            } catch (IOException e) {
                throw die("Cannot rename " + src + " to " + dst, e);
            }
        }
    }
    // We have to reload the configuration after the rename as
    // the initial load pulled up an non-existent (and thus
    // believed to be empty) file.
    //
    cfg.load();
    final Properties oldprop = readGerritServerProperties();
    if (oldprop != null) {
        final Section database = sections.get("database", null);
        String url = oldprop.getProperty("url");
        if (url != null && !convertUrl(database, url)) {
            database.set("type", "jdbc");
            database.set("driver", oldprop.getProperty("driver"));
            database.set("url", url);
        }
        String username = oldprop.getProperty("user");
        if (username == null || username.isEmpty()) {
            username = oldprop.getProperty("username");
        }
        if (username != null && !username.isEmpty()) {
            cfg.setString("database", null, "username", username);
        }
        String password = oldprop.getProperty("password");
        if (password != null && !password.isEmpty()) {
            sec.set("database", null, "password", password);
        }
    }
    String[] values;
    values = cfg.getStringList("ldap", null, "password");
    cfg.unset("ldap", null, "password");
    sec.setList("ldap", null, "password", Arrays.asList(values));
    values = cfg.getStringList("sendemail", null, "smtpPass");
    cfg.unset("sendemail", null, "smtpPass");
    sec.setList("sendemail", null, "smtpPass", Arrays.asList(values));
    savePublic(cfg);
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) Properties(java.util.Properties) Section(com.google.gerrit.pgm.init.api.Section)

Example 3 with Section

use of com.google.gerrit.pgm.init.api.Section in project gerrit by GerritCodeReview.

the class SetPasswd method run.

public void run(String section, String key, String password) throws Exception {
    Section passwordSection = sections.get(section, null);
    if (ui.isBatch()) {
        passwordSection.setSecure(key, password);
    } else {
        ui.header("Set password for [%s]", section);
        passwordSection.passwordForKey("Enter password", key);
    }
}
Also used : Section(com.google.gerrit.pgm.init.api.Section)

Example 4 with Section

use of com.google.gerrit.pgm.init.api.Section in project gerrit by GerritCodeReview.

the class UpgradeFrom2_0_xTest method upgrade.

@Test
public void upgrade() throws IOException, ConfigInvalidException {
    final Path p = newSitePath();
    final SitePaths site = new SitePaths(p);
    assertTrue(site.isNew);
    FileUtil.mkdirsOrDie(site.etc_dir, "Failed to create");
    for (String n : UpgradeFrom2_0_x.etcFiles) {
        Files.write(p.resolve(n), ("# " + n + "\n").getBytes(UTF_8));
    }
    FileBasedConfig old = new FileBasedConfig(p.resolve("gerrit.config").toFile(), FS.DETECTED);
    old.setString("ldap", null, "username", "ldap.user");
    old.setString("ldap", null, "password", "ldap.s3kr3t");
    old.setString("sendemail", null, "smtpUser", "email.user");
    old.setString("sendemail", null, "smtpPass", "email.s3kr3t");
    old.save();
    final InMemorySecureStore secureStore = new InMemorySecureStore();
    final InitFlags flags = new InitFlags(site, secureStore, Collections.<String>emptyList(), false);
    final ConsoleUI ui = createStrictMock(ConsoleUI.class);
    Section.Factory sections = new Section.Factory() {

        @Override
        public Section get(String name, String subsection) {
            return new Section(flags, site, secureStore, ui, name, subsection);
        }
    };
    expect(ui.yesno(eq(true), eq("Upgrade '%s'"), eq(p.toAbsolutePath().normalize()))).andReturn(true);
    replay(ui);
    UpgradeFrom2_0_x u = new UpgradeFrom2_0_x(site, flags, ui, sections);
    assertTrue(u.isNeedUpgrade());
    u.run();
    assertFalse(u.isNeedUpgrade());
    verify(ui);
    for (String n : UpgradeFrom2_0_x.etcFiles) {
        if ("gerrit.config".equals(n) || "secure.config".equals(n)) {
            continue;
        }
        try (InputStream in = Files.newInputStream(site.etc_dir.resolve(n))) {
            assertEquals("# " + n + "\n", new String(ByteStreams.toByteArray(in), UTF_8));
        }
    }
    FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
    cfg.load();
    assertEquals("email.user", cfg.getString("sendemail", null, "smtpUser"));
    assertNull(cfg.getString("sendemail", null, "smtpPass"));
    assertEquals("email.s3kr3t", secureStore.get("sendemail", null, "smtpPass"));
    assertEquals("ldap.user", cfg.getString("ldap", null, "username"));
    assertNull(cfg.getString("ldap", null, "password"));
    assertEquals("ldap.s3kr3t", secureStore.get("ldap", null, "password"));
    u.run();
}
Also used : Path(java.nio.file.Path) InitFlags(com.google.gerrit.pgm.init.api.InitFlags) InputStream(java.io.InputStream) SitePaths(com.google.gerrit.server.config.SitePaths) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) Section(com.google.gerrit.pgm.init.api.Section) ConsoleUI(com.google.gerrit.pgm.init.api.ConsoleUI) Test(org.junit.Test)

Aggregations

Section (com.google.gerrit.pgm.init.api.Section)4 Path (java.nio.file.Path)3 ConsoleUI (com.google.gerrit.pgm.init.api.ConsoleUI)1 InitFlags (com.google.gerrit.pgm.init.api.InitFlags)1 SitePaths (com.google.gerrit.server.config.SitePaths)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Properties (java.util.Properties)1 FileBasedConfig (org.eclipse.jgit.storage.file.FileBasedConfig)1 Test (org.junit.Test)1