summaryrefslogtreecommitdiffstats
path: root/java/com/google/gerrit/server/account/externalids/ExternalIdCaseSensitivityMigrator.java
blob: a6ee366c70d5ab5fd14644836a31d864cd406c2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Copyright (C) 2021 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.account.externalids;

import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_GERRIT;
import static com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME;

import com.google.common.flogger.FluentLogger;
import com.google.gerrit.exceptions.DuplicateKeyException;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.meta.MetaDataUpdate;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.FactoryModuleBuilder;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.lib.Repository;

public class ExternalIdCaseSensitivityMigrator {

  public static class ExternalIdCaseSensitivityMigratorModule extends AbstractModule {
    @Override
    public void configure() {
      install(new FactoryModuleBuilder().build(ExternalIdCaseSensitivityMigrator.Factory.class));
    }
  }

  private static final FluentLogger logger = FluentLogger.forEnclosingClass();

  public interface Factory {
    ExternalIdCaseSensitivityMigrator create(
        @Assisted("isUserNameCaseInsensitive") Boolean isUserNameCaseInsensitive,
        @Assisted("dryRun") Boolean dryRun);
  }

  private GitRepositoryManager repoManager;
  private AllUsersName allUsersName;
  private Provider<MetaDataUpdate.Server> metaDataUpdateServerFactory;
  private ExternalIdNotes.FactoryNoReindex externalIdNotesFactory;

  private ExternalIdFactory externalIdFactory;
  private Boolean isUserNameCaseInsensitive;
  private Boolean dryRun;

  @Inject
  public ExternalIdCaseSensitivityMigrator(
      GitRepositoryManager repoManager,
      AllUsersName allUsersName,
      Provider<MetaDataUpdate.Server> metaDataUpdateServerFactory,
      ExternalIdNotes.FactoryNoReindex externalIdNotesFactory,
      ExternalIdFactory externalIdFactory,
      @Assisted("isUserNameCaseInsensitive") Boolean isUserNameCaseInsensitive,
      @Assisted("dryRun") Boolean dryRun) {
    this.repoManager = repoManager;
    this.allUsersName = allUsersName;
    this.metaDataUpdateServerFactory = metaDataUpdateServerFactory;
    this.externalIdNotesFactory = externalIdNotesFactory;
    this.externalIdFactory = externalIdFactory;

    this.isUserNameCaseInsensitive = isUserNameCaseInsensitive;
    this.dryRun = dryRun;
  }

  private void recomputeExternalIdNoteId(ExternalIdNotes extIdNotes, ExternalId extId)
      throws DuplicateKeyException, IOException {
    if (extId.isScheme(SCHEME_GERRIT) || extId.isScheme(SCHEME_USERNAME)) {
      ExternalIdKeyFactory keyFactory =
          new ExternalIdKeyFactory(
              new ExternalIdKeyFactory.Config() {
                @Override
                public boolean isUserNameCaseInsensitive() {
                  return isUserNameCaseInsensitive;
                }
              });
      ExternalId.Key updatedKey = keyFactory.create(extId.key().scheme(), extId.key().id());
      ExternalId.Key oldKey =
          keyFactory.create(extId.key().scheme(), extId.key().id(), !isUserNameCaseInsensitive);
      if (!oldKey.sha1().getName().equals(updatedKey.sha1().getName())
          && !extId.key().sha1().getName().equals(updatedKey.sha1().getName())) {
        logger.atInfo().log("Converting note name of external ID: %s", oldKey);
        ExternalId updatedExtId =
            externalIdFactory.create(
                updatedKey, extId.accountId(), extId.email(), extId.password(), extId.blobId());
        ExternalId oldExtId =
            externalIdFactory.create(
                oldKey, extId.accountId(), extId.email(), extId.password(), extId.blobId());
        extIdNotes.replace(
            Collections.singleton(oldExtId),
            Collections.singleton(updatedExtId),
            (externalId) -> externalId.key().sha1());
      }
    }
  }

  public void migrate(Collection<ExternalId> todo, Runnable monitor)
      throws RepositoryNotFoundException, IOException, ConfigInvalidException {
    try (Repository repo = repoManager.openRepository(allUsersName)) {
      ExternalIdNotes extIdNotes = externalIdNotesFactory.load(repo);
      for (ExternalId extId : todo) {
        recomputeExternalIdNoteId(extIdNotes, extId);
        monitor.run();
      }
      if (!dryRun) {
        try (MetaDataUpdate metaDataUpdate =
            metaDataUpdateServerFactory.get().create(allUsersName)) {
          metaDataUpdate.setMessage(
              String.format(
                  "Migration to case %ssensitive usernames",
                  isUserNameCaseInsensitive ? "" : "in"));
          extIdNotes.commit(metaDataUpdate);
        } catch (Exception e) {
          logger.atSevere().withCause(e).log("%s", e.getMessage());
        }
      }
    } catch (DuplicateExternalIdKeyException e) {
      logger.atSevere().withCause(e).log("%s", e.getMessage());
      throw e;
    }
  }
}