Load SecureStore implementation from site lib dir

This approach shares core ideas of the servlet filter and is alternative
approach to secure store plugin.

To use this extension additional jar file needs to be put in
$site_path/lib directory and gerrit.secureStoreClass property needs to
be added to gerrit.config file. This property must contain fully
qualified class name that implements
com.google.gerrit.server.securestore.SecureStore interface.

During Gerrit start up the class configured with the
gerrit.secureStoreClass will be loaded and the SecureStore interface
will be bound to it.

Change-Id: I90972d28ecab0818d782c3d43acb8c70ba598d12
Signed-off-by: Dariusz Luksza <dariusz@luksza.org>
This commit is contained in:
Dariusz Luksza
2014-08-12 14:46:35 +02:00
parent bc59748c55
commit 9927a96b0d
5 changed files with 143 additions and 14 deletions

View File

@@ -25,8 +25,10 @@ import com.google.gerrit.pgm.init.InitPlugins;
import com.google.gerrit.pgm.init.api.ConsoleUI;
import com.google.gerrit.pgm.util.ErrorLogFile;
import com.google.gerrit.pgm.util.IoUtil;
import com.google.gerrit.pgm.util.SecureStoreProvider;
import com.google.gerrit.server.config.GerritServerConfigModule;
import com.google.gerrit.server.config.SitePath;
import com.google.gerrit.server.securestore.SecureStore;
import com.google.gerrit.server.util.HostPlatform;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
@@ -102,6 +104,7 @@ public class Init extends BaseInit {
protected void configure() {
bind(File.class).annotatedWith(SitePath.class).toInstance(getSitePath());
bind(Browser.class);
bind(SecureStore.class).toProvider(SecureStoreProvider.class);
}
});
modules.add(new GerritServerConfigModule());

View File

@@ -0,0 +1,86 @@
// Copyright (C) 2013 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.pgm.util;
import com.google.common.base.Strings;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.securestore.DefaultSecureStore;
import com.google.gerrit.server.securestore.SecureStore;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
public class SecureStoreProvider implements Provider<SecureStore> {
private static final Logger log = LoggerFactory
.getLogger(SecureStoreProvider.class);
private final File libdir;
private final Injector injector;
private final String secureStoreClassName;
private SecureStore instance;
@Inject
SecureStoreProvider(
Injector injector,
SitePaths sitePaths) {
FileBasedConfig cfg =
new FileBasedConfig(sitePaths.gerrit_config, FS.DETECTED);
try {
cfg.load();
} catch (IOException | ConfigInvalidException e) {
throw new RuntimeException("Cannot read gerrit.config file", e);
}
this.injector = injector;
this.libdir = sitePaths.lib_dir;
this.secureStoreClassName =
cfg.getString("gerrit", null, "secureStoreClass");
}
@Override
public SecureStore get() {
if (instance == null) {
instance = injector.getInstance(getSecureStoreImpl());
}
return instance;
}
@SuppressWarnings("unchecked")
private Class<? extends SecureStore> getSecureStoreImpl() {
if (Strings.isNullOrEmpty(secureStoreClassName)) {
return DefaultSecureStore.class;
}
SiteLibraryLoaderUtil.loadSiteLib(libdir);
try {
return (Class<? extends SecureStore>) Class.forName(secureStoreClassName);
} catch (ClassNotFoundException e) {
String msg =
String.format("Cannot load secure store class: %s",
secureStoreClassName);
log.error(msg, e);
throw new RuntimeException(msg, e);
}
}
}

View File

@@ -30,6 +30,7 @@ import com.google.gerrit.server.schema.DataSourceProvider;
import com.google.gerrit.server.schema.DataSourceType;
import com.google.gerrit.server.schema.DatabaseModule;
import com.google.gerrit.server.schema.SchemaModule;
import com.google.gerrit.server.securestore.SecureStore;
import com.google.gwtorm.server.OrmException;
import com.google.inject.AbstractModule;
import com.google.inject.Binding;
@@ -96,6 +97,7 @@ public abstract class SiteProgram extends AbstractProgram {
@Override
protected void configure() {
bind(File.class).annotatedWith(SitePath.class).toInstance(sitePath);
bind(SecureStore.class).toProvider(SecureStoreProvider.class);
}
};
modules.add(sitePathModule);

View File

@@ -0,0 +1,46 @@
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.config;
import com.google.gerrit.server.securestore.SecureStore;
import org.eclipse.jgit.lib.Config;
class GerritConfig extends Config {
private final SecureStore secureStore;
GerritConfig(Config baseConfig, SecureStore secureStore) {
super(baseConfig);
this.secureStore = secureStore;
}
@Override
public String getString(String section, String subsection, String name) {
String secure = secureStore.get(section, subsection, name);
if (secure != null) {
return secure;
}
return super.getString(section, subsection, name);
}
@Override
public String[] getStringList(String section, String subsection, String name) {
String[] secure = secureStore.getList(section, subsection, name);
if (secure != null) {
return secure;
}
return super.getStringList(section, subsection, name);
}
}

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.server.config;
import com.google.gerrit.server.securestore.SecureStore;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.ProvisionException;
@@ -33,10 +34,12 @@ class GerritServerConfigProvider implements Provider<Config> {
LoggerFactory.getLogger(GerritServerConfigProvider.class);
private final SitePaths site;
private final SecureStore secureStore;
@Inject
GerritServerConfigProvider(final SitePaths site) {
GerritServerConfigProvider(final SitePaths site, final SecureStore secureStore) {
this.site = site;
this.secureStore = secureStore;
}
@Override
@@ -46,7 +49,7 @@ class GerritServerConfigProvider implements Provider<Config> {
if (!cfg.getFile().exists()) {
log.info("No " + site.gerrit_config.getAbsolutePath()
+ "; assuming defaults");
return cfg;
return new GerritConfig(cfg, secureStore);
}
try {
@@ -57,17 +60,6 @@ class GerritServerConfigProvider implements Provider<Config> {
throw new ProvisionException(e.getMessage(), e);
}
if (site.secure_config.exists()) {
cfg = new FileBasedConfig(cfg, site.secure_config, FS.DETECTED);
try {
cfg.load();
} catch (IOException e) {
throw new ProvisionException(e.getMessage(), e);
} catch (ConfigInvalidException e) {
throw new ProvisionException(e.getMessage(), e);
}
}
return cfg;
return new GerritConfig(cfg, secureStore);
}
}