Search in sources :

Example 61 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class AbstractChangeNotes method load.

public T load() throws OrmException {
    if (loaded) {
        return self();
    }
    boolean read = args.migration.readChanges();
    if (!read && primaryStorage == PrimaryStorage.NOTE_DB) {
        throw new OrmException("NoteDb is required to read change " + changeId);
    }
    boolean readOrWrite = read || args.migration.rawWriteChangesSetting();
    if (!readOrWrite && !autoRebuild) {
        loadDefaults();
        return self();
    }
    if (args.migration.failOnLoad()) {
        throw new OrmException("Reading from NoteDb is disabled");
    }
    try (Timer1.Context timer = args.metrics.readLatency.start(CHANGES);
        Repository repo = args.repoManager.openRepository(getProjectName());
        // auto-rebuilding before this object may get passed to a ChangeUpdate.
        LoadHandle handle = openHandle(repo)) {
        if (read) {
            revision = handle.id();
            onLoad(handle);
        } else {
            loadDefaults();
        }
        loaded = true;
    } catch (ConfigInvalidException | IOException e) {
        throw new OrmException(e);
    }
    return self();
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OrmException(com.google.gwtorm.server.OrmException) IOException(java.io.IOException) Timer1(com.google.gerrit.metrics.Timer1)

Example 62 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class ExternalId method parse.

/**
   * Parses an external ID from a byte array that contain the external ID as an Git config file
   * text.
   *
   * <p>The Git config must have exactly one externalId subsection with an accountId and optionally
   * email and password:
   *
   * <pre>
   * [externalId "username:jdoe"]
   *   accountId = 1003407
   *   email = jdoe@example.com
   *   password = bcrypt:4:LCbmSBDivK/hhGVQMfkDpA==:XcWn0pKYSVU/UJgOvhidkEtmqCp6oKB7
   * </pre>
   */
public static ExternalId parse(String noteId, byte[] raw) throws ConfigInvalidException {
    Config externalIdConfig = new Config();
    try {
        externalIdConfig.fromText(new String(raw, UTF_8));
    } catch (ConfigInvalidException e) {
        throw invalidConfig(noteId, e.getMessage());
    }
    Set<String> externalIdKeys = externalIdConfig.getSubsections(EXTERNAL_ID_SECTION);
    if (externalIdKeys.size() != 1) {
        throw invalidConfig(noteId, String.format("Expected exactly 1 '%s' section, found %d", EXTERNAL_ID_SECTION, externalIdKeys.size()));
    }
    String externalIdKeyStr = Iterables.getOnlyElement(externalIdKeys);
    Key externalIdKey = Key.parse(externalIdKeyStr);
    if (externalIdKey == null) {
        throw invalidConfig(noteId, String.format("External ID %s is invalid", externalIdKeyStr));
    }
    if (!externalIdKey.sha1().getName().equals(noteId)) {
        throw invalidConfig(noteId, String.format("SHA1 of external ID '%s' does not match note ID '%s'", externalIdKeyStr, noteId));
    }
    String email = externalIdConfig.getString(EXTERNAL_ID_SECTION, externalIdKeyStr, EMAIL_KEY);
    String password = externalIdConfig.getString(EXTERNAL_ID_SECTION, externalIdKeyStr, PASSWORD_KEY);
    int accountId = readAccountId(noteId, externalIdConfig, externalIdKeyStr);
    return new AutoValue_ExternalId(externalIdKey, new Account.Id(accountId), Strings.emptyToNull(email), Strings.emptyToNull(password));
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Config(org.eclipse.jgit.lib.Config)

Example 63 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class ExternalIdsConsistencyChecker method check.

private List<ConsistencyProblemInfo> check(Repository repo, ObjectId commit) throws IOException {
    List<ConsistencyProblemInfo> problems = new ArrayList<>();
    ListMultimap<String, ExternalId.Key> emails = MultimapBuilder.hashKeys().arrayListValues().build();
    try (RevWalk rw = new RevWalk(repo)) {
        NoteMap noteMap = ExternalIdReader.readNoteMap(rw, commit);
        for (Note note : noteMap) {
            byte[] raw = rw.getObjectReader().open(note.getData(), OBJ_BLOB).getCachedBytes(ExternalIdReader.MAX_NOTE_SZ);
            try {
                ExternalId extId = ExternalId.parse(note.getName(), raw);
                problems.addAll(validateExternalId(extId));
                if (extId.email() != null) {
                    emails.put(extId.email(), extId.key());
                }
            } catch (ConfigInvalidException e) {
                addError(String.format(e.getMessage()), problems);
            }
        }
    }
    emails.asMap().entrySet().stream().filter(e -> e.getValue().size() > 1).forEach(e -> addError(String.format("Email '%s' is not unique, it's used by the following external IDs: %s", e.getKey(), e.getValue().stream().map(k -> "'" + k.get() + "'").sorted().collect(joining(", "))), problems));
    return problems;
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) AccountCache(com.google.gerrit.server.account.AccountCache) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OutgoingEmailValidator(com.google.gerrit.server.mail.send.OutgoingEmailValidator) Note(org.eclipse.jgit.notes.Note) OBJ_BLOB(org.eclipse.jgit.lib.Constants.OBJ_BLOB) ListMultimap(com.google.common.collect.ListMultimap) DecoderException(org.apache.commons.codec.DecoderException) MultimapBuilder(com.google.common.collect.MultimapBuilder) Inject(com.google.inject.Inject) IOException(java.io.IOException) Collectors.joining(java.util.stream.Collectors.joining) ArrayList(java.util.ArrayList) ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) List(java.util.List) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) SCHEME_USERNAME(com.google.gerrit.server.account.externalids.ExternalId.SCHEME_USERNAME) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo) HashedPassword(com.google.gerrit.server.account.HashedPassword) Repository(org.eclipse.jgit.lib.Repository) NoteMap(org.eclipse.jgit.notes.NoteMap) Singleton(com.google.inject.Singleton) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ArrayList(java.util.ArrayList) NoteMap(org.eclipse.jgit.notes.NoteMap) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Note(org.eclipse.jgit.notes.Note) ConsistencyProblemInfo(com.google.gerrit.extensions.api.config.ConsistencyCheckInfo.ConsistencyProblemInfo)

Example 64 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class DeleteEmail method apply.

public Response<?> apply(IdentifiedUser user, String email) throws ResourceNotFoundException, ResourceConflictException, MethodNotAllowedException, OrmException, IOException, ConfigInvalidException {
    if (!realm.allowsEdit(AccountFieldName.REGISTER_NEW_EMAIL)) {
        throw new MethodNotAllowedException("realm does not allow deleting emails");
    }
    Set<ExternalId> extIds = externalIds.byAccount(user.getAccountId()).stream().filter(e -> email.equals(e.email())).collect(toSet());
    if (extIds.isEmpty()) {
        throw new ResourceNotFoundException(email);
    }
    try {
        for (ExternalId extId : extIds) {
            AuthRequest authRequest = new AuthRequest(extId.key());
            authRequest.setEmailAddress(email);
            accountManager.unlink(user.getAccountId(), authRequest);
        }
    } catch (AccountException e) {
        throw new ResourceConflictException(e.getMessage());
    }
    return Response.none();
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) GlobalPermission(com.google.gerrit.server.permissions.GlobalPermission) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) CurrentUser(com.google.gerrit.server.CurrentUser) OrmException(com.google.gwtorm.server.OrmException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Input(com.google.gerrit.server.account.DeleteEmail.Input) Inject(com.google.inject.Inject) Set(java.util.Set) AccountFieldName(com.google.gerrit.extensions.client.AccountFieldName) IOException(java.io.IOException) Response(com.google.gerrit.extensions.restapi.Response) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) ExternalIds(com.google.gerrit.server.account.externalids.ExternalIds) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) Provider(com.google.inject.Provider) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) AuthException(com.google.gerrit.extensions.restapi.AuthException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) Collectors.toSet(java.util.stream.Collectors.toSet) Singleton(com.google.inject.Singleton) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException)

Example 65 with ConfigInvalidException

use of org.eclipse.jgit.errors.ConfigInvalidException in project gerrit by GerritCodeReview.

the class ProjectLevelConfig method getWithInheritance.

public Config getWithInheritance() {
    Config cfgWithInheritance = new Config();
    try {
        cfgWithInheritance.fromText(get().toText());
    } catch (ConfigInvalidException e) {
    // cannot happen
    }
    ProjectState parent = Iterables.getFirst(project.parents(), null);
    if (parent != null) {
        Config parentCfg = parent.getConfig(fileName).getWithInheritance();
        for (String section : parentCfg.getSections()) {
            Set<String> allNames = get().getNames(section);
            for (String name : parentCfg.getNames(section)) {
                if (!allNames.contains(name)) {
                    cfgWithInheritance.setStringList(section, null, name, Arrays.asList(parentCfg.getStringList(section, null, name)));
                }
            }
            for (String subsection : parentCfg.getSubsections(section)) {
                allNames = get().getNames(section, subsection);
                for (String name : parentCfg.getNames(section, subsection)) {
                    if (!allNames.contains(name)) {
                        cfgWithInheritance.setStringList(section, subsection, name, Arrays.asList(parentCfg.getStringList(section, subsection, name)));
                    }
                }
            }
        }
    }
    return cfgWithInheritance;
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Config(org.eclipse.jgit.lib.Config) ProjectState(com.google.gerrit.server.project.ProjectState)

Aggregations

ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)73 IOException (java.io.IOException)43 OrmException (com.google.gwtorm.server.OrmException)30 Repository (org.eclipse.jgit.lib.Repository)24 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)18 Project (com.google.gerrit.reviewdb.client.Project)15 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)14 Account (com.google.gerrit.reviewdb.client.Account)13 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)12 RevWalk (org.eclipse.jgit.revwalk.RevWalk)12 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)10 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)10 ObjectId (org.eclipse.jgit.lib.ObjectId)10 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)9 Inject (com.google.inject.Inject)9 ArrayList (java.util.ArrayList)9 Provider (com.google.inject.Provider)8 Map (java.util.Map)8 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)7 HashMap (java.util.HashMap)7