Add schema migration to populate gpg subkey to master key map

Change-Id: I342413df1730395745cda837c90e71a09003d24c
This commit is contained in:
David Ostrovsky
2018-12-13 09:33:02 +01:00
parent f955491af4
commit 4abd8a8b3a
7 changed files with 57 additions and 3 deletions

View File

@@ -3,6 +3,7 @@ java_library(
srcs = glob(["**/*.java"]),
visibility = ["//visibility:public"],
deps = [
"//java/com/google/gerrit/common:annotations",
"//java/com/google/gerrit/common:server",
"//java/com/google/gerrit/exceptions",
"//java/com/google/gerrit/extensions:api",

View File

@@ -272,6 +272,25 @@ public class PublicKeyStore implements AutoCloseable {
}
}
public void rebuildSubkeyMasterKeyMap()
throws MissingObjectException, IncorrectObjectTypeException, IOException, PGPException {
if (reader == null) {
load();
}
if (notes != null) {
try (ObjectInserter ins = repo.newObjectInserter()) {
for (Note note : notes) {
for (PGPPublicKeyRing keyRing :
new PGPPublicKeyRingCollection(readKeysFromNote(note, null))) {
long masterKeyId = keyRing.getPublicKey().getKeyID();
ObjectId masterKeyObjectId = keyObjectId(masterKeyId);
saveSubkeyMaping(ins, keyRing, masterKeyId, masterKeyObjectId);
}
}
}
}
}
/**
* Add a public key to the store.
*

View File

@@ -10,6 +10,7 @@ java_library(
"//java/com/google/gerrit/exceptions",
"//java/com/google/gerrit/extensions:api",
"//java/com/google/gerrit/git",
"//java/com/google/gerrit/gpg",
"//java/com/google/gerrit/lifecycle",
"//java/com/google/gerrit/metrics",
"//java/com/google/gerrit/reviewdb:server",

View File

@@ -15,6 +15,7 @@
package com.google.gerrit.server.schema;
import com.google.gerrit.server.config.AllProjectsName;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@@ -30,11 +31,14 @@ interface NoteDbSchemaVersion {
class Arguments {
final GitRepositoryManager repoManager;
final AllProjectsName allProjects;
final AllUsersName allUsers;
@Inject
Arguments(GitRepositoryManager repoManager, AllProjectsName allProjects) {
Arguments(
GitRepositoryManager repoManager, AllProjectsName allProjects, AllUsersName allUsers) {
this.repoManager = repoManager;
this.allProjects = allProjects;
this.allUsers = allUsers;
}
}

View File

@@ -28,7 +28,7 @@ import java.util.stream.Stream;
public class NoteDbSchemaVersions {
static final ImmutableSortedMap<Integer, Class<? extends NoteDbSchemaVersion>> ALL =
// List all supported NoteDb schema versions here.
Stream.of(Schema_180.class)
Stream.of(Schema_180.class, Schema_181.class)
.collect(toImmutableSortedMap(naturalOrder(), v -> guessVersion(v).get(), v -> v));
public static final int FIRST = ALL.firstKey();

View File

@@ -0,0 +1,29 @@
// Copyright (C) 2018 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.schema;
import com.google.gerrit.gpg.PublicKeyStore;
import org.eclipse.jgit.lib.Repository;
public class Schema_181 implements NoteDbSchemaVersion {
@Override
public void upgrade(Arguments args, UpdateUI ui) throws Exception {
ui.message("Rebuild GPGP note map to build subkey to master key map");
try (Repository repo = args.repoManager.openRepository(args.allUsers);
PublicKeyStore store = new PublicKeyStore(repo)) {
store.rebuildSubkeyMasterKeyMap();
}
}
}

View File

@@ -99,7 +99,7 @@ public class NoteDbSchemaUpdaterTest extends GerritBaseTests {
allUsersName = new AllUsersName("The-Users");
repoManager = new InMemoryRepositoryManager();
args = new NoteDbSchemaVersion.Arguments(repoManager, allProjectsName);
args = new NoteDbSchemaVersion.Arguments(repoManager, allProjectsName, allUsersName);
NoteDbSchemaVersionManager versionManager =
new NoteDbSchemaVersionManager(allProjectsName, repoManager);
updater =