Merge "Enable SecureStore configuration during init"

This commit is contained in:
Saša Živkov
2015-02-19 09:05:07 +00:00
committed by Gerrit Code Review
21 changed files with 296 additions and 164 deletions

View File

@@ -16,6 +16,8 @@ package com.google.gerrit.server.config;
import static com.google.inject.Scopes.SINGLETON;
import com.google.gerrit.server.securestore.SecureStore;
import com.google.gerrit.server.securestore.SecureStoreProvider;
import com.google.inject.AbstractModule;
import org.eclipse.jgit.lib.Config;
@@ -28,5 +30,6 @@ public class GerritServerConfigModule extends AbstractModule {
bind(TrackingFooters.class).toProvider(TrackingFootersProvider.class).in(SINGLETON) ;
bind(Config.class).annotatedWith(GerritServerConfig.class).toProvider(
GerritServerConfigProvider.class).in(SINGLETON);
bind(SecureStore.class).toProvider(SecureStoreProvider.class).in(SINGLETON);
}
}

View File

@@ -37,7 +37,7 @@ class GerritServerConfigProvider implements Provider<Config> {
private final SecureStore secureStore;
@Inject
GerritServerConfigProvider(final SitePaths site, final SecureStore secureStore) {
GerritServerConfigProvider(SitePaths site, SecureStore secureStore) {
this.site = site;
this.secureStore = secureStore;
}

View File

@@ -24,7 +24,6 @@ import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.config.TrackingFooters;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.securestore.SecureStore;
import com.google.gerrit.server.securestore.SecureStoreProvider;
import com.google.gwtorm.server.SchemaFactory;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
@@ -117,12 +116,19 @@ class CopyConfigModule extends AbstractModule {
return serverIdentProvider.get();
}
@Inject
private SecureStore secureStore;
@Provides
SecureStore getSecureStore() {
return secureStore;
}
@Inject
CopyConfigModule() {
}
@Override
protected void configure() {
bind(SecureStore.class).toProvider(SecureStoreProvider.class);
}
}

View File

@@ -0,0 +1,12 @@
package com.google.gerrit.server.securestore;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.Retention;
@Retention(RUNTIME)
@BindingAnnotation
public @interface SecureStoreClassName {
}

View File

@@ -1,78 +0,0 @@
// 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.securestore;
import com.google.common.base.MoreObjects;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Objects;
public class SecureStoreData {
public final File pluginFile;
public final String storeName;
public final String className;
public SecureStoreData(String pluginName, String className, File jarFile,
String storeName) {
this.className = className;
this.pluginFile = jarFile;
this.storeName = String.format("%s/%s", pluginName, storeName);
}
public String getStoreName() {
return storeName;
}
public Class<? extends SecureStore> load() {
return load(pluginFile);
}
@SuppressWarnings("unchecked")
public Class<? extends SecureStore> load(File pluginFile) {
try {
URL[] pluginJarUrls = new URL[] {pluginFile.toURI().toURL()};
ClassLoader currentCL = Thread.currentThread().getContextClassLoader();
final URLClassLoader newClassLoader =
new URLClassLoader(pluginJarUrls, currentCL);
Thread.currentThread().setContextClassLoader(newClassLoader);
return (Class<? extends SecureStore>) newClassLoader.loadClass(className);
} catch (Exception e) {
throw new SecureStoreException(String.format(
"Cannot load secure store implementation for %s", storeName), e);
}
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("storeName", storeName)
.add("className", className).add("file", pluginFile).toString();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof SecureStoreData) {
SecureStoreData o = (SecureStoreData) obj;
return storeName.equals(o.storeName);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(storeName);
}
}

View File

@@ -1,27 +0,0 @@
// 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.server.securestore;
public class SecureStoreException extends RuntimeException {
private static final long serialVersionUID = 5581700510568485065L;
SecureStoreException(String msg) {
super(msg);
}
SecureStoreException(String msg, Exception e) {
super(msg, e);
}
}

View File

@@ -15,69 +15,55 @@
package com.google.gerrit.server.securestore;
import com.google.common.base.Strings;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.SiteLibraryLoaderUtil;
import com.google.gerrit.server.config.SitePaths;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Provider;
import com.google.inject.Singleton;
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;
@Singleton
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;
private final String className;
@Inject
SecureStoreProvider(
protected 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);
}
SitePaths sitePaths,
@Nullable @SecureStoreClassName String className) {
this.injector = injector;
this.libdir = sitePaths.lib_dir;
this.secureStoreClassName =
cfg.getString("gerrit", null, "secureStoreClass");
this.className = className;
}
@Override
public SecureStore get() {
if (instance == null) {
instance = injector.getInstance(getSecureStoreImpl());
}
return instance;
public synchronized SecureStore get() {
return injector.getInstance(getSecureStoreImpl());
}
@SuppressWarnings("unchecked")
private Class<? extends SecureStore> getSecureStoreImpl() {
if (Strings.isNullOrEmpty(secureStoreClassName)) {
if (Strings.isNullOrEmpty(className)) {
return DefaultSecureStore.class;
}
SiteLibraryLoaderUtil.loadSiteLib(libdir);
try {
return (Class<? extends SecureStore>) Class.forName(secureStoreClassName);
return (Class<? extends SecureStore>) Class.forName(className);
} catch (ClassNotFoundException e) {
String msg =
String.format("Cannot load secure store class: %s",
secureStoreClassName);
String.format("Cannot load secure store class: %s", className);
log.error(msg, e);
throw new RuntimeException(msg, e);
}