Remove UserRepository constructor which could cause NPE (#8047)

This commit is contained in:
Nahuel 2022-10-10 10:50:01 +02:00 committed by GitHub
parent b6ff10f781
commit 13b76dfd88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 57 deletions

View File

@ -1,46 +0,0 @@
/*
* Copyright 2021 Collate
* 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 org.openmetadata.service;
import com.codahale.metrics.health.HealthCheck;
import java.io.IOException;
import lombok.extern.slf4j.Slf4j;
import org.jdbi.v3.core.Jdbi;
import org.openmetadata.service.jdbi3.CollectionDAO;
import org.openmetadata.service.jdbi3.ListFilter;
import org.openmetadata.service.jdbi3.UserRepository;
import org.openmetadata.service.util.EntityUtil.Fields;
@Slf4j
public class CatalogHealthCheck extends HealthCheck {
private final UserRepository userRepository;
public CatalogHealthCheck(Jdbi jdbi) {
super();
CollectionDAO repo = jdbi.onDemand(CollectionDAO.class);
this.userRepository = new UserRepository(repo);
}
@Override
protected Result check() {
try {
ListFilter filter = new ListFilter();
userRepository.listAfter(null, Fields.EMPTY_FIELDS, filter, 1, null);
return Result.healthy();
} catch (IOException e) {
LOG.error("Health check error {}", e.getMessage());
return Result.unhealthy(e.getMessage());
}
}
}

View File

@ -74,10 +74,6 @@ public class UserRepository extends EntityRepository<User> {
return new Fields(allowedFields, fields); return new Fields(allowedFields, fields);
} }
public UserRepository(CollectionDAO dao) {
this(dao, null);
}
@Override @Override
public EntityReference getOriginalOwner(User entity) { public EntityReference getOriginalOwner(User entity) {
// For User entity, the entity and the owner are the same // For User entity, the entity and the owner are the same

View File

@ -60,6 +60,7 @@ import org.openmetadata.service.jdbi3.EntityRepository;
import org.openmetadata.service.jdbi3.ListFilter; import org.openmetadata.service.jdbi3.ListFilter;
import org.openmetadata.service.jdbi3.UserRepository; import org.openmetadata.service.jdbi3.UserRepository;
import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.Collection;
import org.openmetadata.service.secrets.SecretsManager;
import org.openmetadata.service.security.Authorizer; import org.openmetadata.service.security.Authorizer;
import org.openmetadata.service.util.ElasticSearchClientUtils; import org.openmetadata.service.util.ElasticSearchClientUtils;
import org.openmetadata.service.util.EntityUtil; import org.openmetadata.service.util.EntityUtil;
@ -83,14 +84,15 @@ public class BuildSearchIndexResource {
private final ExecutorService threadScheduler; private final ExecutorService threadScheduler;
private final UserRepository userRepository; private final UserRepository userRepository;
@Collection(constructorType = Collection.ConstructorType.DAO_AUTH_CONFIG) @Collection(constructorType = Collection.ConstructorType.DAO_AUTH_SM_CONFIG)
public BuildSearchIndexResource(CollectionDAO dao, Authorizer authorizer, OpenMetadataApplicationConfig config) { public BuildSearchIndexResource(
CollectionDAO dao, Authorizer authorizer, SecretsManager secretsManager, OpenMetadataApplicationConfig config) {
if (config.getElasticSearchConfiguration() != null) { if (config.getElasticSearchConfiguration() != null) {
this.client = ElasticSearchClientUtils.createElasticSearchClient(config.getElasticSearchConfiguration()); this.client = ElasticSearchClientUtils.createElasticSearchClient(config.getElasticSearchConfiguration());
this.elasticSearchIndexDefinition = new ElasticSearchIndexDefinition(client, dao); this.elasticSearchIndexDefinition = new ElasticSearchIndexDefinition(client, dao);
} }
this.dao = dao; this.dao = dao;
this.userRepository = new UserRepository(dao); this.userRepository = new UserRepository(dao, secretsManager);
this.authorizer = authorizer; this.authorizer = authorizer;
this.threadScheduler = this.threadScheduler =
new ThreadPoolExecutor( new ThreadPoolExecutor(

View File

@ -56,6 +56,8 @@ import org.openmetadata.service.fernet.Fernet;
import org.openmetadata.service.jdbi3.CollectionDAO; import org.openmetadata.service.jdbi3.CollectionDAO;
import org.openmetadata.service.jdbi3.UserRepository; import org.openmetadata.service.jdbi3.UserRepository;
import org.openmetadata.service.jdbi3.locator.ConnectionAwareAnnotationSqlLocator; import org.openmetadata.service.jdbi3.locator.ConnectionAwareAnnotationSqlLocator;
import org.openmetadata.service.secrets.SecretsManager;
import org.openmetadata.service.secrets.SecretsManagerFactory;
import org.openmetadata.service.security.jwt.JWTTokenGenerator; import org.openmetadata.service.security.jwt.JWTTokenGenerator;
public final class TablesInitializer { public final class TablesInitializer {
@ -354,7 +356,7 @@ public final class TablesInitializer {
user.setAuthenticationMechanism(authenticationMechanism); user.setAuthenticationMechanism(authenticationMechanism);
} }
try { try {
addOrUpdateUser(user, jdbi); addOrUpdateUser(user, jdbi, config);
if (jwtAuthMechanism != null) { if (jwtAuthMechanism != null) {
printToConsoleMandatory(JsonUtils.pojoToJson(user)); printToConsoleMandatory(JsonUtils.pojoToJson(user));
} }
@ -392,7 +394,9 @@ public final class TablesInitializer {
} }
try { try {
CollectionDAO daoObject = jdbi.onDemand(CollectionDAO.class); CollectionDAO daoObject = jdbi.onDemand(CollectionDAO.class);
UserRepository userRepository = new UserRepository(daoObject); SecretsManager secretsManager =
SecretsManagerFactory.createSecretsManager(config.getSecretsManagerConfiguration(), config.getClusterName());
UserRepository userRepository = new UserRepository(daoObject, secretsManager);
RestUtil.PutResponse<User> addedUser = userRepository.createOrUpdate(null, user); RestUtil.PutResponse<User> addedUser = userRepository.createOrUpdate(null, user);
printToConsoleInDebug("Updated user entry: " + addedUser.getEntity()); printToConsoleInDebug("Updated user entry: " + addedUser.getEntity());
if (jwtAuthMechanism != null) { if (jwtAuthMechanism != null) {
@ -403,9 +407,11 @@ public final class TablesInitializer {
} }
} }
private static void addOrUpdateUser(User user, Jdbi jdbi) throws Exception { private static void addOrUpdateUser(User user, Jdbi jdbi, OpenMetadataApplicationConfig config) throws Exception {
CollectionDAO daoObject = jdbi.onDemand(CollectionDAO.class); CollectionDAO daoObject = jdbi.onDemand(CollectionDAO.class);
UserRepository userRepository = new UserRepository(daoObject); SecretsManager secretsManager =
SecretsManagerFactory.createSecretsManager(config.getSecretsManagerConfiguration(), config.getClusterName());
UserRepository userRepository = new UserRepository(daoObject, secretsManager);
User addedUser = userRepository.create(null, user); User addedUser = userRepository.create(null, user);
printToConsoleInDebug("Added user entry: " + addedUser.getName()); printToConsoleInDebug("Added user entry: " + addedUser.getName());
} }