From b7e32429115a5a34c7f6e3de9d7c1aa2945888c3 Mon Sep 17 00:00:00 2001 From: Suresh Srinivas Date: Sat, 29 Jul 2023 11:42:28 -0700 Subject: [PATCH] Make LoadingCache members final variables (#12648) * Make LoadingCache members final variables * Simplify singleton classes * Reuse Entity fields instead of declaring them again --- .../java/org/openmetadata/service/Entity.java | 5 - .../scheduled/DataInsightsReportJob.java | 36 +++-- .../subscription/ActivityFeedAlertCache.java | 29 ++-- .../events/subscription/AlertUtil.java | 2 +- .../subscription/AlertsRuleEvaluator.java | 4 +- .../subscription/SubscriptionPublisher.java | 6 +- .../subscription/email/EmailPublisher.java | 2 +- .../subscription/gchat/GChatPublisher.java | 2 +- .../generic/GenericPublisher.java | 2 +- .../msteams/MSTeamsPublisher.java | 2 +- .../slack/SlackEventPublisher.java | 2 +- .../service/jdbi3/CollectionDAO.java | 2 +- .../service/jdbi3/EntityRepository.java | 4 +- .../service/jdbi3/FeedRepository.java | 25 ++- .../service/jdbi3/PolicyRepository.java | 4 +- .../service/jdbi3/RoleRepository.java | 4 +- .../service/jdbi3/SystemRepository.java | 2 +- .../service/jdbi3/TeamRepository.java | 8 +- .../service/jdbi3/UserRepository.java | 10 +- .../service/resources/bots/BotResource.java | 2 +- .../EventSubscriptionResource.java | 2 - .../glossary/GlossaryTermResource.java | 7 - .../resources/policies/PolicyResource.java | 2 - .../resources/settings/SettingsCache.java | 33 ++-- .../resources/system/ConfigResource.java | 2 +- .../service/resources/tags/TagLabelCache.java | 100 +++++------- .../service/resources/teams/RoleResource.java | 2 - .../service/resources/teams/UserResource.java | 14 +- .../service/search/EntityBuilderConstant.java | 13 -- .../ElasticSearchClientImpl.java | 52 +++---- .../openSearch/OpenSearchClientImpl.java | 70 ++++----- .../service/security/DefaultAuthorizer.java | 2 +- .../service/security/JwtFilter.java | 4 +- .../service/security/NoopAuthorizer.java | 6 +- .../security/auth/BasicAuthenticator.java | 48 +++--- .../service/security/auth/BotTokenCache.java | 16 +- .../security/auth/LdapAuthenticator.java | 13 +- .../security/auth/LoginAttemptCache.java | 1 - .../service/security/auth/UserTokenCache.java | 15 +- .../security/policyevaluator/PolicyCache.java | 43 ++---- .../policyevaluator/PostResourceContext.java | 2 +- .../security/policyevaluator/RoleCache.java | 52 ++----- .../policyevaluator/RuleEvaluator.java | 8 +- .../policyevaluator/SubjectCache.java | 123 ++++++--------- .../policyevaluator/SubjectContext.java | 19 +-- .../ThreadResourceContext.java | 2 +- .../service/socket/WebSocketManager.java | 2 +- .../openmetadata/service/util/EmailUtil.java | 142 +++++++----------- .../openmetadata/service/util/EntityUtil.java | 1 - .../service/util/NotificationHandler.java | 16 +- .../service/util/SubscriptionUtil.java | 8 +- .../openmetadata/service/util/UserUtil.java | 10 +- .../service/util/ValidatorUtil.java | 13 +- .../service/resources/EntityResourceTest.java | 12 +- .../policyevaluator/RuleEvaluatorTest.java | 10 +- .../policyevaluator/SubjectContextTest.java | 22 ++- 56 files changed, 391 insertions(+), 649 deletions(-) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/Entity.java b/openmetadata-service/src/main/java/org/openmetadata/service/Entity.java index 9821e59df1e8..c3d66d96c6c0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/Entity.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/Entity.java @@ -261,11 +261,6 @@ public static boolean shouldDisplayEntityChangeOnFeed(@NonNull String entityType return !ACTIVITY_FEED_EXCLUDED_ENTITIES.contains(entityType); } - public static Fields getFields(String entityType, String fields) { - EntityRepository entityRepository = Entity.getEntityRepository(entityType); - return entityRepository.getFields(fields); - } - public static Fields getFields(String entityType, List fields) { EntityRepository entityRepository = Entity.getEntityRepository(entityType); return entityRepository.getFields(String.join(",", fields)); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/DataInsightsReportJob.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/DataInsightsReportJob.java index 2b76da0e1289..80ba58572e1c 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/DataInsightsReportJob.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/scheduled/DataInsightsReportJob.java @@ -108,7 +108,7 @@ private void sendReportsToTeams( if (!CommonUtil.nullOrEmpty(email)) { emails.add(email); } else { - team.getUsers().forEach(user -> emails.add(SubjectCache.getInstance().getUserById(user.getId()).getEmail())); + team.getUsers().forEach(user -> emails.add(SubjectCache.getUserById(user.getId()).getEmail())); } try { DataInsightTotalAssetTemplate totalAssetTemplate = @@ -119,15 +119,14 @@ private void sendReportsToTeams( createOwnershipTemplate(searchClient, team.getName(), scheduleTime, currentTime, numberOfDaysChange); DataInsightDescriptionAndOwnerTemplate tierTemplate = createTierTemplate(searchClient, team.getName(), scheduleTime, currentTime, numberOfDaysChange); - EmailUtil.getInstance() - .sendDataInsightEmailNotificationToUser( - emails, - totalAssetTemplate, - descriptionTemplate, - ownershipTemplate, - tierTemplate, - EmailUtil.getInstance().getDataInsightReportSubject(), - EmailUtil.DATA_INSIGHT_REPORT_TEMPLATE); + EmailUtil.sendDataInsightEmailNotificationToUser( + emails, + totalAssetTemplate, + descriptionTemplate, + ownershipTemplate, + tierTemplate, + EmailUtil.getDataInsightReportSubject(), + EmailUtil.DATA_INSIGHT_REPORT_TEMPLATE); } catch (Exception ex) { LOG.error("[DataInsightReport] Failed for Team: {}, Reason : {}", team.getName(), ex.getMessage()); } @@ -149,15 +148,14 @@ private void sendToAdmins(SearchClient searchClient, Long scheduleTime, Long cur createOwnershipTemplate(searchClient, null, scheduleTime, currentTime, numberOfDaysChange); DataInsightDescriptionAndOwnerTemplate tierTemplate = createTierTemplate(searchClient, null, scheduleTime, currentTime, numberOfDaysChange); - EmailUtil.getInstance() - .sendDataInsightEmailNotificationToUser( - emailList, - totalAssetTemplate, - descriptionTemplate, - ownershipTemplate, - tierTemplate, - EmailUtil.getInstance().getDataInsightReportSubject(), - EmailUtil.DATA_INSIGHT_REPORT_TEMPLATE); + EmailUtil.sendDataInsightEmailNotificationToUser( + emailList, + totalAssetTemplate, + descriptionTemplate, + ownershipTemplate, + tierTemplate, + EmailUtil.getDataInsightReportSubject(), + EmailUtil.DATA_INSIGHT_REPORT_TEMPLATE); } catch (Exception ex) { LOG.error("[DataInsightReport] Failed for Admin, Reason : {}", ex.getMessage(), ex); } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/ActivityFeedAlertCache.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/ActivityFeedAlertCache.java index b18f83cdf17b..1f1a34cfc11f 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/ActivityFeedAlertCache.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/ActivityFeedAlertCache.java @@ -10,36 +10,26 @@ import javax.annotation.CheckForNull; import lombok.extern.slf4j.Slf4j; import org.openmetadata.schema.entity.events.EventSubscription; +import org.openmetadata.schema.type.Include; +import org.openmetadata.service.Entity; import org.openmetadata.service.exception.EntityNotFoundException; -import org.openmetadata.service.jdbi3.EventSubscriptionRepository; @Slf4j public class ActivityFeedAlertCache { - private static final ActivityFeedAlertCache INSTANCE = new ActivityFeedAlertCache(); - private static volatile boolean initialized = false; - protected static final LoadingCache eventSubCache = + protected static final LoadingCache EVENT_SUB_CACHE = CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterWrite(3, TimeUnit.MINUTES) .build(new ActivityFeedAlertLoader()); - protected static EventSubscriptionRepository eventSubscriptionRepository; - private static String activityFeedAlertName; + private static final String ACTIVITY_FEED_ALERT = "ActivityFeedAlert"; - public static void initialize(String alertName, EventSubscriptionRepository repo) { - if (!initialized) { - eventSubscriptionRepository = repo; - initialized = true; - activityFeedAlertName = alertName; - } - } - - public static ActivityFeedAlertCache getInstance() { - return INSTANCE; + private ActivityFeedAlertCache() { + // Private constructor for static class } - public EventSubscription getActivityFeedAlert() throws EntityNotFoundException { + public static EventSubscription getActivityFeedAlert() throws EntityNotFoundException { try { - return eventSubCache.get(activityFeedAlertName); + return EVENT_SUB_CACHE.get(ACTIVITY_FEED_ALERT); } catch (ExecutionException | UncheckedExecutionException ex) { throw new EntityNotFoundException(ex.getMessage()); } @@ -48,8 +38,7 @@ public EventSubscription getActivityFeedAlert() throws EntityNotFoundException { static class ActivityFeedAlertLoader extends CacheLoader { @Override public EventSubscription load(@CheckForNull String alertName) throws IOException { - EventSubscription alert = - eventSubscriptionRepository.getByName(null, alertName, eventSubscriptionRepository.getFields("*")); + EventSubscription alert = Entity.getEntityByName(Entity.EVENT_SUBSCRIPTION, alertName, "*", Include.NON_DELETED); LOG.debug("Loaded Alert {}", alert); return alert; } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/AlertUtil.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/AlertUtil.java index e6ca2486f1e1..4055b528235f 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/AlertUtil.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/AlertUtil.java @@ -213,7 +213,7 @@ public static boolean shouldTriggerAlert(String entityType, FilteringRules confi public static boolean shouldProcessActivityFeedRequest(ChangeEvent event) { // Check Trigger Conditions - FilteringRules filteringRules = ActivityFeedAlertCache.getInstance().getActivityFeedAlert().getFilteringRules(); + FilteringRules filteringRules = ActivityFeedAlertCache.getActivityFeedAlert().getFilteringRules(); return AlertUtil.shouldTriggerAlert(event.getEntityType(), filteringRules) && AlertUtil.evaluateAlertConditions(event, filteringRules.getRules()); } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/AlertsRuleEvaluator.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/AlertsRuleEvaluator.java index 082bbb88cae0..c0ed84b455f7 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/AlertsRuleEvaluator.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/AlertsRuleEvaluator.java @@ -70,14 +70,14 @@ public boolean matchAnyOwnerName(String... ownerNameList) throws IOException { EntityReference ownerReference = entity.getOwner(); if (ownerReference != null) { if (USER.equals(ownerReference.getType())) { - User user = SubjectCache.getInstance().getSubjectContext(ownerReference.getId()).getUser(); + User user = SubjectCache.getSubjectContext(ownerReference.getId()).getUser(); for (String name : ownerNameList) { if (user.getName().equals(name)) { return true; } } } else if (TEAM.equals(ownerReference.getType())) { - Team team = SubjectCache.getInstance().getTeam(ownerReference.getId()); + Team team = SubjectCache.getTeam(ownerReference.getId()); for (String name : ownerNameList) { if (team.getName().equals(name)) { return true; diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/SubscriptionPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/SubscriptionPublisher.java index 3e70fcb7a5b2..3878a6acc453 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/SubscriptionPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/SubscriptionPublisher.java @@ -28,8 +28,6 @@ import org.openmetadata.schema.entity.events.SubscriptionStatus; import org.openmetadata.service.events.EventPubSub; import org.openmetadata.service.events.errors.EventPublisherException; -import org.openmetadata.service.jdbi3.CollectionDAO; -import org.openmetadata.service.jdbi3.EventSubscriptionRepository; import org.openmetadata.service.resources.events.EventResource; /** @@ -52,11 +50,9 @@ public class SubscriptionPublisher extends AbstractAlertPublisher { private final CountDownLatch shutdownLatch = new CountDownLatch(1); @Getter private BatchEventProcessor processor; - private final EventSubscriptionRepository eventSubscriptionRepository; - public SubscriptionPublisher(EventSubscription eventSub, CollectionDAO dao) { + public SubscriptionPublisher(EventSubscription eventSub) { super(eventSub); - this.eventSubscriptionRepository = new EventSubscriptionRepository(dao); } @SneakyThrows diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/email/EmailPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/email/EmailPublisher.java index 544d56628939..0cd9d340f8bd 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/email/EmailPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/email/EmailPublisher.java @@ -43,7 +43,7 @@ public class EmailPublisher extends SubscriptionPublisher { private final CollectionDAO daoCollection; public EmailPublisher(EventSubscription eventSub, CollectionDAO dao) { - super(eventSub, dao); + super(eventSub); if (eventSub.getSubscriptionType() == EMAIL) { this.emailAlertConfig = JsonUtils.convertValue(eventSub.getSubscriptionConfig(), EmailAlertConfig.class); this.daoCollection = dao; diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/gchat/GChatPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/gchat/GChatPublisher.java index 283eaffcbfe4..89c2227e51a4 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/gchat/GChatPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/gchat/GChatPublisher.java @@ -45,7 +45,7 @@ public class GChatPublisher extends SubscriptionPublisher { private final CollectionDAO daoCollection; public GChatPublisher(EventSubscription eventSub, CollectionDAO dao) { - super(eventSub, dao); + super(eventSub); if (eventSub.getSubscriptionType() == G_CHAT_WEBHOOK) { this.daoCollection = dao; this.webhook = JsonUtils.convertValue(eventSub.getSubscriptionConfig(), Webhook.class); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/generic/GenericPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/generic/GenericPublisher.java index fa310cca314e..803088aed0a3 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/generic/GenericPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/generic/GenericPublisher.java @@ -43,7 +43,7 @@ public class GenericPublisher extends SubscriptionPublisher { private final CollectionDAO daoCollection; public GenericPublisher(EventSubscription eventSub, CollectionDAO dao) { - super(eventSub, dao); + super(eventSub); if (eventSub.getSubscriptionType() == GENERIC_WEBHOOK) { this.daoCollection = dao; this.webhook = JsonUtils.convertValue(eventSub.getSubscriptionConfig(), Webhook.class); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/msteams/MSTeamsPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/msteams/MSTeamsPublisher.java index 25f10041ce68..4277740f0a72 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/msteams/MSTeamsPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/msteams/MSTeamsPublisher.java @@ -45,7 +45,7 @@ public class MSTeamsPublisher extends SubscriptionPublisher { private final CollectionDAO daoCollection; public MSTeamsPublisher(EventSubscription eventSub, CollectionDAO dao) { - super(eventSub, dao); + super(eventSub); if (eventSub.getSubscriptionType() == MS_TEAMS_WEBHOOK) { this.daoCollection = dao; this.webhook = JsonUtils.convertValue(eventSub.getSubscriptionConfig(), Webhook.class); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/slack/SlackEventPublisher.java b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/slack/SlackEventPublisher.java index 6fea0e6287c5..4fd1105df795 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/slack/SlackEventPublisher.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/events/subscription/slack/SlackEventPublisher.java @@ -43,7 +43,7 @@ public class SlackEventPublisher extends SubscriptionPublisher { private final CollectionDAO daoCollection; public SlackEventPublisher(EventSubscription eventSub, CollectionDAO dao) { - super(eventSub, dao); + super(eventSub); if (eventSub.getSubscriptionType() == SLACK_WEBHOOK) { this.daoCollection = dao; this.webhook = JsonUtils.convertValue(eventSub.getSubscriptionConfig(), Webhook.class); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/CollectionDAO.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/CollectionDAO.java index e63cebf221e7..662d28136b30 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/CollectionDAO.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/CollectionDAO.java @@ -2068,7 +2068,7 @@ void applyTag( default List getTags(String targetFQN) { List tags = getTagsInternal(FullyQualifiedName.buildHash(targetFQN)); - tags.forEach(tagLabel -> tagLabel.setDescription(TagLabelCache.getInstance().getDescription(tagLabel))); + tags.forEach(tagLabel -> tagLabel.setDescription(TagLabelCache.getDescription(tagLabel))); return tags; } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java index 0b7b4425dc13..53ae44f84c2d 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java @@ -1154,7 +1154,7 @@ void checkMutuallyExclusive(List tagLabels) { // When two tags have the same parent that is mutuallyExclusive, then throw an error String parentFqn = FullyQualifiedName.getParentFQN(tagLabel.getTagFQN()); TagLabel stored = map.put(parentFqn, tagLabel); - if (stored != null && TagLabelCache.getInstance().mutuallyExclusive(tagLabel)) { + if (stored != null && TagLabelCache.mutuallyExclusive(tagLabel)) { throw new IllegalArgumentException(CatalogExceptionMessage.mutuallyExclusiveLabels(tagLabel, stored)); } } @@ -1598,7 +1598,7 @@ public EntityUpdater(T original, T updated, Operation operation) { this.updatingUser = updated.getUpdatedBy().equalsIgnoreCase(ADMIN_USER_NAME) ? new User().withName(ADMIN_USER_NAME).withIsAdmin(true) - : SubjectCache.getInstance().getSubjectContext(updated.getUpdatedBy()).getUser(); + : SubjectCache.getSubjectContext(updated.getUpdatedBy()).getUser(); } /** Compare original and updated entities and perform updates. Update the entity version and track changes. */ diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/FeedRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/FeedRepository.java index a9c1a9005fed..be06dd2aebaa 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/FeedRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/FeedRepository.java @@ -126,10 +126,7 @@ public Thread create(Thread thread) throws IOException { // Validate about data entity is valid and get the owner for that entity EntityLink about = EntityLink.parse(thread.getAbout()); EntityRepository repository = Entity.getEntityRepository(about.getEntityType()); - String field = "owner"; - if (!repository.supportsOwner) { - field = "id"; - } + String field = repository.supportsOwner ? "owner" : ""; EntityInterface aboutEntity = Entity.getEntity(about, field, ALL); thread.withEntityId(aboutEntity.getId()); // Add entity id to thread return createThread(thread, about, aboutEntity.getOwner()); @@ -139,7 +136,7 @@ public Thread create(Thread thread) throws IOException { private Thread createThread(Thread thread, EntityLink about, EntityReference entityOwner) throws JsonProcessingException { // Validate user creating thread - User createdByUser = SubjectCache.getInstance().getUser(thread.getCreatedBy()); + User createdByUser = SubjectCache.getUser(thread.getCreatedBy()); if (thread.getType() == ThreadType.Task) { thread.withTask(thread.getTask().withId(getNextTaskId())); // Assign taskId for a task @@ -326,7 +323,7 @@ private void storeMentions(Thread thread, String message) { @Transaction public Thread addPostToThread(String id, Post post, String userName) throws IOException { // Validate the user posting the message - User fromUser = SubjectCache.getInstance().getUser(post.getFrom()); + User fromUser = SubjectCache.getUser(post.getFrom()); // Update the thread with the new post Thread thread = EntityUtil.validate(id, dao.feedDAO().findById(id), Thread.class); @@ -387,10 +384,6 @@ public DeleteResponse deleteThread(Thread thread, String deletedByUser) return new DeleteResponse<>(thread, RestUtil.ENTITY_DELETED); } - public EntityReference getOwnerReference(String username) { - return dao.userDAO().findEntityByName(EntityInterfaceUtil.quoteName(username)).getEntityReference(); - } - @Transaction public ThreadCount getThreadsCount(FeedFilter filter, String link) { List> result; @@ -475,7 +468,7 @@ public ResultList list(FeedFilter filter, String link, int limitPosts, S total = filteredThreads.getTotalCount(); } else { // Only data assets are added as about - User user = userId != null ? SubjectCache.getInstance().getUserById(userId) : null; + User user = userId != null ? SubjectCache.getUserById(userId) : null; List teamNameHash = getTeamNames(user); String userNameHash = getUserNameHash(user); List jsons = @@ -631,7 +624,7 @@ public final PatchResponse patchThread(UriInfo uriInfo, UUID id, String public void checkPermissionsForResolveTask(Thread thread, boolean closeTask, SecurityContext securityContext) throws IOException { String userName = securityContext.getUserPrincipal().getName(); - User user = SubjectCache.getInstance().getUser(userName); + User user = SubjectCache.getUser(userName); EntityLink about = EntityLink.parse(thread.getAbout()); EntityReference aboutRef = EntityUtil.validateEntityLink(about); if (Boolean.TRUE.equals(user.getIsAdmin())) { @@ -830,7 +823,7 @@ private Thread populateAssignees(Thread thread) { /** Return the tasks created by or assigned to the user. */ private FilteredThreads getTasksOfUser(FeedFilter filter, String userId, int limit) throws IOException { - String username = SubjectCache.getInstance().getUserById(userId).getName(); + String username = SubjectCache.getUserById(userId).getName(); List teamIds = getTeamIds(userId); List userTeamJsonPostgres = getUserTeamJsonPostgres(userId, teamIds); String userTeamJsonMysql = getUserTeamJsonMysql(userId, teamIds); @@ -845,7 +838,7 @@ private FilteredThreads getTasksOfUser(FeedFilter filter, String userId, int lim /** Return the tasks created by the user. */ private FilteredThreads getTasksAssignedBy(FeedFilter filter, String userId, int limit) throws IOException { - String username = SubjectCache.getInstance().getUserById(userId).getName(); + String username = SubjectCache.getUserById(userId).getName(); List jsons = dao.feedDAO().listTasksAssigned(username, limit, filter.getCondition()); List threads = JsonUtils.readObjects(jsons, Thread.class); int totalCount = dao.feedDAO().listCountTasksAssignedBy(username, filter.getCondition(false)); @@ -869,7 +862,7 @@ private FilteredThreads getThreadsByOwner(FeedFilter filter, String userId, int /** Returns the threads where the user or the team they belong to were mentioned by other users with @mention. */ private FilteredThreads getThreadsByMentions(FeedFilter filter, String userId, int limit) throws IOException { - User user = SubjectCache.getInstance().getUserById(userId); + User user = SubjectCache.getUserById(userId); String userNameHash = getUserNameHash(user); // Return the threads where the user or team was mentioned List teamNamesHash = getTeamNames(user); @@ -891,7 +884,7 @@ private FilteredThreads getThreadsByMentions(FeedFilter filter, String userId, i private List getTeamIds(String userId) { List teamIds = null; if (userId != null) { - User user = SubjectCache.getInstance().getUserById(userId); + User user = SubjectCache.getUserById(userId); teamIds = listOrEmpty(user.getTeams()).stream().map(ref -> ref.getId().toString()).collect(Collectors.toList()); } return nullOrEmpty(teamIds) ? List.of(StringUtils.EMPTY) : teamIds; diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/PolicyRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/PolicyRepository.java index e03ee0222f76..2a51850c5d5c 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/PolicyRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/PolicyRepository.java @@ -77,7 +77,7 @@ public void prepare(Policy policy) throws IOException { public void storeEntity(Policy policy, boolean update) throws IOException { store(policy, update); if (update) { - PolicyCache.getInstance().invalidatePolicy(policy.getId()); + PolicyCache.invalidatePolicy(policy.getId()); } } @@ -102,7 +102,7 @@ protected void preDelete(Policy entity) { @Override protected void cleanup(Policy policy) throws IOException { super.cleanup(policy); - PolicyCache.getInstance().invalidatePolicy(policy.getId()); + PolicyCache.invalidatePolicy(policy.getId()); } public void validateRules(Policy policy) { diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/RoleRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/RoleRepository.java index f661e8c4d18d..94090e4eac41 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/RoleRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/RoleRepository.java @@ -92,7 +92,7 @@ public void storeEntity(Role role, boolean update) throws IOException { role.withPolicies(null); store(role, update); if (update) { - RoleCache.getInstance().invalidateRole(role.getId()); + RoleCache.invalidateRole(role.getId()); } role.withPolicies(policies); } @@ -120,7 +120,7 @@ protected void preDelete(Role entity) { @Override protected void cleanup(Role role) throws IOException { super.cleanup(role); - RoleCache.getInstance().invalidateRole(role.getId()); + RoleCache.invalidateRole(role.getId()); } /** Handles entity updated from PUT and POST operation. */ diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/SystemRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/SystemRepository.java index 43d0278c5d29..fd2f8fc6d480 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/SystemRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/SystemRepository.java @@ -154,7 +154,7 @@ public void updateSetting(Settings setting) { } dao.insertSettings(setting.getConfigType().toString(), JsonUtils.pojoToJson(setting.getConfigValue())); // Invalidate Cache - SettingsCache.getInstance().invalidateSettings(setting.getConfigType().value()); + SettingsCache.invalidateSettings(setting.getConfigType().value()); } catch (Exception ex) { LOG.error("Failing in Updating Setting.", ex); throw new CustomExceptionMessage(Response.Status.INTERNAL_SERVER_ERROR, ex.getMessage()); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TeamRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TeamRepository.java index 3bba82aaa463..8d213e82d5bb 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TeamRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/TeamRepository.java @@ -144,7 +144,7 @@ public void storeEntity(Team team, boolean update) throws IOException { store(team, update); if (update) { - SubjectCache.getInstance().invalidateTeam(team.getId()); + SubjectCache.invalidateTeam(team.getId()); } // Restore the relationships @@ -216,7 +216,7 @@ protected void cleanup(Team team) throws IOException { } } super.cleanup(team); - SubjectCache.getInstance().invalidateTeam(team.getId()); + SubjectCache.invalidateTeam(team.getId()); } @Override @@ -233,7 +233,7 @@ public CsvImportResult importFromCsv(String name, String csv, boolean dryRun, St } private List getInheritedRoles(Team team) throws IOException { - return SubjectCache.getInstance().getRolesForTeams(getParentsForInheritedRoles(team)); + return SubjectCache.getRolesForTeams(getParentsForInheritedRoles(team)); } private TeamHierarchy getTeamHierarchy(Team team) { @@ -619,7 +619,7 @@ private void getParents(CSVPrinter printer, CSVRecord csvRecord, Team importedTe continue; // Parent is being created by CSV import } // Else the parent should already exist - if (!SubjectCache.getInstance().isInTeam(team.getName(), parentRef)) { + if (!SubjectCache.isInTeam(team.getName(), parentRef)) { importFailure( printer, invalidTeam(4, team.getName(), importedTeam.getName(), parentRef.getName()), csvRecord); processRecord = false; diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/UserRepository.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/UserRepository.java index dad2219c1c37..aaf260bb4a32 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/UserRepository.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/UserRepository.java @@ -121,7 +121,7 @@ private List getInheritedRoles(User user) throws IOException { return null; // No inherited roles for bots } getTeams(user); - return SubjectCache.getInstance() != null ? SubjectCache.getInstance().getRolesForTeams(getTeams(user)) : null; + return SubjectCache.getRolesForTeams(getTeams(user)); } @Override @@ -140,7 +140,7 @@ public void storeEntity(User user, boolean update) throws IOException { store(user, update); if (update) { - SubjectCache.getInstance().invalidateUser(user.getName()); + SubjectCache.invalidateUser(user.getName()); } // Restore the relationships @@ -175,13 +175,13 @@ public UserUpdater getUpdater(User original, User updated, Operation operation) @Override protected void postDelete(User entity) { - SubjectCache.getInstance().invalidateUser(entity.getName()); + SubjectCache.invalidateUser(entity.getName()); } @Override protected void cleanup(User user) throws IOException { super.cleanup(user); - SubjectCache.getInstance().invalidateUser(user.getName()); + SubjectCache.invalidateUser(user.getName()); } @Override @@ -436,7 +436,7 @@ private List getTeams(CSVPrinter printer, CSVRecord csvRecord, continue; // Team is same as the team to which CSV is being imported, then it is in the same hierarchy } // Else the parent should already exist - if (!SubjectCache.getInstance().isInTeam(team.getName(), teamRef)) { + if (!SubjectCache.isInTeam(team.getName(), teamRef)) { importFailure(printer, invalidTeam(6, team.getName(), user, teamRef.getName()), csvRecord); processRecord = false; } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotResource.java index 5c00e0fc509d..4a76852fd6cf 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/bots/BotResource.java @@ -103,7 +103,7 @@ public void initialize(OpenMetadataApplicationConfig config) throws IOException // Add role corresponding to the bot to the user // we need to set a mutable list here user.setRoles(getRoleForBot(bot.getName())); - user = UserUtil.addOrUpdateBotUser(user, config); + user = UserUtil.addOrUpdateBotUser(user); bot.withBotUser(user.getEntityReference()); repository.initializeEntity(bot); } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java index d842bdd8e50c..f1c555ed2cc7 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/events/subscription/EventSubscriptionResource.java @@ -63,7 +63,6 @@ import org.openmetadata.service.Entity; import org.openmetadata.service.OpenMetadataApplicationConfig; import org.openmetadata.service.events.scheduled.ReportsHandler; -import org.openmetadata.service.events.subscription.ActivityFeedAlertCache; import org.openmetadata.service.events.subscription.AlertUtil; import org.openmetadata.service.events.subscription.EventsSubscriptionRegistry; import org.openmetadata.service.exception.EntityNotFoundException; @@ -125,7 +124,6 @@ public void initialize(OpenMetadataApplicationConfig config) { try { repository.initSeedDataFromResources(); EventsSubscriptionRegistry.initialize(listOrEmpty(EventSubscriptionResource.getDescriptors())); - ActivityFeedAlertCache.initialize("ActivityFeedAlert", repository); searchClient = IndexUtil.getSearchClient(config.getElasticSearchConfiguration(), daoCollection); ReportsHandler.initialize(daoCollection, searchClient); initializeEventSubscriptions(); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermResource.java index 4ec492ce49a8..b096a89a26a7 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/glossary/GlossaryTermResource.java @@ -56,14 +56,12 @@ import org.openmetadata.schema.type.MetadataOperation; import org.openmetadata.schema.utils.EntityInterfaceUtil; import org.openmetadata.service.Entity; -import org.openmetadata.service.OpenMetadataApplicationConfig; import org.openmetadata.service.exception.CatalogExceptionMessage; import org.openmetadata.service.jdbi3.CollectionDAO; import org.openmetadata.service.jdbi3.GlossaryTermRepository; import org.openmetadata.service.jdbi3.ListFilter; import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; -import org.openmetadata.service.resources.tags.TagLabelCache; import org.openmetadata.service.security.Authorizer; import org.openmetadata.service.util.EntityUtil.Fields; import org.openmetadata.service.util.RestUtil; @@ -91,11 +89,6 @@ public GlossaryTerm addHref(UriInfo uriInfo, GlossaryTerm term) { return term; } - @Override - public void initialize(OpenMetadataApplicationConfig config) { - TagLabelCache.initialize(); - } - public GlossaryTermResource(CollectionDAO dao, Authorizer authorizer) { super(GlossaryTerm.class, new GlossaryTermRepository(dao), authorizer); } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyResource.java index 3a971a3e4226..f4028b241a6c 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/policies/PolicyResource.java @@ -67,7 +67,6 @@ import org.openmetadata.service.resources.EntityResource; import org.openmetadata.service.security.Authorizer; import org.openmetadata.service.security.policyevaluator.CompiledRule; -import org.openmetadata.service.security.policyevaluator.PolicyCache; import org.openmetadata.service.security.policyevaluator.RuleEvaluator; import org.openmetadata.service.util.JsonUtils; import org.openmetadata.service.util.ResultList; @@ -106,7 +105,6 @@ protected List getEntitySpecificOperations() { public void initialize(OpenMetadataApplicationConfig config) throws IOException { // Load any existing rules from database, before loading seed data. repository.initSeedDataFromResources(); - PolicyCache.initialize(); } @Override diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/settings/SettingsCache.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/settings/SettingsCache.java index 58b4f0c010f5..6f62b758f827 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/settings/SettingsCache.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/settings/SettingsCache.java @@ -34,16 +34,18 @@ @Slf4j public class SettingsCache { - private static final SettingsCache INSTANCE = new SettingsCache(); private static volatile boolean initialized = false; - protected static LoadingCache cache; + protected static final LoadingCache CACHE = + CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new SettingsLoader()); protected static SystemRepository systemRepository; + private SettingsCache() { + // Private constructor for singleton + } + // Expected to be called only once from the DefaultAuthorizer public static void initialize(CollectionDAO dao, OpenMetadataApplicationConfig config) { if (!initialized) { - cache = - CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new SettingsLoader()); systemRepository = new SystemRepository(dao.systemDAO()); initialized = true; createDefaultConfiguration(config); @@ -72,13 +74,9 @@ private static void createDefaultConfiguration(OpenMetadataApplicationConfig app } } - public static SettingsCache getInstance() { - return INSTANCE; - } - - public T getSetting(SettingsType settingName, Class clazz) { + public static T getSetting(SettingsType settingName, Class clazz) { try { - String json = JsonUtils.pojoToJson(cache.get(settingName.toString()).getConfigValue()); + String json = JsonUtils.pojoToJson(CACHE.get(settingName.toString()).getConfigValue()); return JsonUtils.readValue(json, clazz); } catch (Exception ex) { LOG.error("Failed to fetch Settings . Setting {}", settingName, ex); @@ -86,23 +84,14 @@ public T getSetting(SettingsType settingName, Class clazz) { } } - public Settings getSetting(SettingsType settingName) { - try { - return cache.get(settingName.toString()); - } catch (Exception ex) { - LOG.error("Failed to fetch Settings . Setting {}", settingName, ex); - throw new EntityNotFoundException("Setting not found"); - } - } - public static void cleanUp() { - cache.invalidateAll(); + CACHE.invalidateAll(); initialized = false; } - public void invalidateSettings(String settingsName) { + public static void invalidateSettings(String settingsName) { try { - cache.invalidate(settingsName); + CACHE.invalidate(settingsName); } catch (Exception ex) { LOG.error("Failed to invalidate cache for settings {}", settingsName, ex); } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/system/ConfigResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/system/ConfigResource.java index b58fe8d9cf14..3de52cf164e0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/system/ConfigResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/system/ConfigResource.java @@ -102,7 +102,7 @@ public AuthenticationConfiguration getAuthConfig() { schema = @Schema(implementation = AuthenticationConfiguration.class))) }) public LogoConfiguration getCustomLogoConfig() { - return SettingsCache.getInstance().getSetting(SettingsType.CUSTOM_LOGO_CONFIGURATION, LogoConfiguration.class); + return SettingsCache.getSetting(SettingsType.CUSTOM_LOGO_CONFIGURATION, LogoConfiguration.class); } @GET diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagLabelCache.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagLabelCache.java index f4ce8539f074..c43b2c09752b 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagLabelCache.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/tags/TagLabelCache.java @@ -13,6 +13,8 @@ package org.openmetadata.service.resources.tags; +import static org.openmetadata.schema.type.Include.NON_DELETED; + import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -31,11 +33,6 @@ import org.openmetadata.service.Entity; import org.openmetadata.service.exception.CatalogExceptionMessage; import org.openmetadata.service.exception.EntityNotFoundException; -import org.openmetadata.service.jdbi3.ClassificationRepository; -import org.openmetadata.service.jdbi3.GlossaryRepository; -import org.openmetadata.service.jdbi3.GlossaryTermRepository; -import org.openmetadata.service.jdbi3.TagRepository; -import org.openmetadata.service.util.EntityUtil.Fields; import org.openmetadata.service.util.FullyQualifiedName; /** @@ -43,96 +40,67 @@ */ @Slf4j public class TagLabelCache { - private static final TagLabelCache INSTANCE = new TagLabelCache(); - private static volatile boolean initialized = false; - - protected static TagRepository tagRepository; - protected static ClassificationRepository tagClassificationRepository; - protected static LoadingCache tagCache; // Tag fqn to Tag - protected static LoadingCache classificationCache; // Classification name to Classification + // Tag fqn to Tag + protected static final LoadingCache TAG_CACHE = + CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(2, TimeUnit.MINUTES).build(new TagLoader()); + // Classification name to Classification + protected static final LoadingCache CLASSIFICATION_CACHE = + CacheBuilder.newBuilder().maximumSize(25).expireAfterWrite(2, TimeUnit.MINUTES).build(new ClassificationLoader()); - protected static GlossaryTermRepository glossaryTermRepository; - protected static GlossaryRepository glossaryRepository; // Glossary term fqn to GlossaryTerm - protected static LoadingCache glossaryTermCache; - - protected static LoadingCache glossaryCache; // Glossary fqn to Glossary - - // Expected to be called only once from the TagResource during initialization - public static void initialize() { - if (!initialized) { - classificationCache = - CacheBuilder.newBuilder() - .maximumSize(25) - .expireAfterWrite(2, TimeUnit.MINUTES) - .build(new ClassificationLoader()); - tagCache = - CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(2, TimeUnit.MINUTES).build(new TagLoader()); - tagRepository = (TagRepository) Entity.getEntityRepository(Entity.TAG); - tagClassificationRepository = (ClassificationRepository) Entity.getEntityRepository(Entity.CLASSIFICATION); - - glossaryCache = - CacheBuilder.newBuilder().maximumSize(25).expireAfterWrite(2, TimeUnit.MINUTES).build(new GlossaryLoader()); - glossaryTermCache = - CacheBuilder.newBuilder() - .maximumSize(100) - .expireAfterWrite(2, TimeUnit.MINUTES) - .build(new GlossaryTermLoader()); - glossaryTermRepository = (GlossaryTermRepository) Entity.getEntityRepository(Entity.GLOSSARY_TERM); - glossaryRepository = (GlossaryRepository) Entity.getEntityRepository(Entity.GLOSSARY); - initialized = true; - } else { - LOG.info("Subject cache is already initialized"); - } - } + protected static final LoadingCache GLOSSARY_TERM_CACHE = + CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(2, TimeUnit.MINUTES).build(new GlossaryTermLoader()); + + // Glossary fqn to Glossary + protected static final LoadingCache GLOSSARY_CACHE = + CacheBuilder.newBuilder().maximumSize(25).expireAfterWrite(2, TimeUnit.MINUTES).build(new GlossaryLoader()); - public static TagLabelCache getInstance() { - return INSTANCE; + private TagLabelCache() { + // Private constructor for utility class } public static void cleanUp() { - classificationCache.cleanUp(); - tagCache.cleanUp(); - glossaryCache.cleanUp(); - glossaryTermCache.cleanUp(); - initialized = false; + CLASSIFICATION_CACHE.cleanUp(); + TAG_CACHE.cleanUp(); + GLOSSARY_CACHE.cleanUp(); + GLOSSARY_TERM_CACHE.cleanUp(); } - public Classification getClassification(String classificationName) { + public static Classification getClassification(String classificationName) { try { - return classificationCache.get(classificationName); + return CLASSIFICATION_CACHE.get(classificationName); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage( CatalogExceptionMessage.entityNotFound(Entity.CLASSIFICATION, classificationName)); } } - public Tag getTag(String tagFqn) { + public static Tag getTag(String tagFqn) { try { - return tagCache.get(tagFqn); + return TAG_CACHE.get(tagFqn); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage(CatalogExceptionMessage.entityNotFound(Entity.TAG, tagFqn)); } } - public Glossary getGlossary(String glossaryName) { + public static Glossary getGlossary(String glossaryName) { try { - return glossaryCache.get(glossaryName); + return GLOSSARY_CACHE.get(glossaryName); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage(CatalogExceptionMessage.entityNotFound(Entity.GLOSSARY, glossaryName)); } } - public GlossaryTerm getGlossaryTerm(String glossaryTermFqn) { + public static GlossaryTerm getGlossaryTerm(String glossaryTermFqn) { try { - return glossaryTermCache.get(glossaryTermFqn); + return GLOSSARY_TERM_CACHE.get(glossaryTermFqn); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage( CatalogExceptionMessage.entityNotFound(Entity.GLOSSARY_TERM, glossaryTermFqn)); } } - public String getDescription(TagLabel label) { + public static String getDescription(TagLabel label) { if (label.getSource() == TagSource.CLASSIFICATION) { return getTag(label.getTagFQN()).getDescription(); } else if (label.getSource() == TagSource.GLOSSARY) { @@ -143,7 +111,7 @@ public String getDescription(TagLabel label) { } /** Returns true if the parent of the tag label is mutually exclusive */ - public boolean mutuallyExclusive(TagLabel label) { + public static boolean mutuallyExclusive(TagLabel label) { String[] fqnParts = FullyQualifiedName.split(label.getTagFQN()); String parentFqn = FullyQualifiedName.getParentFQN(fqnParts); boolean rootParent = fqnParts.length == 2; @@ -163,7 +131,7 @@ public boolean mutuallyExclusive(TagLabel label) { static class TagLoader extends CacheLoader { @Override public Tag load(@CheckForNull String tagName) throws IOException { - Tag tag = tagRepository.getByName(null, tagName, Fields.EMPTY_FIELDS); + Tag tag = Entity.getEntityByName(Entity.TAG, tagName, "", NON_DELETED); LOG.info("Loaded tag {}:{}", tag.getName(), tag.getId()); return tag; } @@ -173,7 +141,7 @@ static class ClassificationLoader extends CacheLoader { @Override public Classification load(@CheckForNull String classificationName) throws IOException { Classification classification = - tagClassificationRepository.getByName(null, classificationName, Fields.EMPTY_FIELDS); + Entity.getEntityByName(Entity.CLASSIFICATION, classificationName, "", NON_DELETED); LOG.info("Loaded classification {}:{}", classification.getName(), classification.getId()); return classification; } @@ -182,7 +150,7 @@ public Classification load(@CheckForNull String classificationName) throws IOExc static class GlossaryTermLoader extends CacheLoader { @Override public GlossaryTerm load(@CheckForNull String glossaryTermName) throws IOException { - GlossaryTerm glossaryTerm = glossaryTermRepository.getByName(null, glossaryTermName, Fields.EMPTY_FIELDS); + GlossaryTerm glossaryTerm = Entity.getEntityByName(Entity.GLOSSARY_TERM, glossaryTermName, "", NON_DELETED); LOG.info("Loaded glossaryTerm {}:{}", glossaryTerm.getName(), glossaryTerm.getId()); return glossaryTerm; } @@ -191,7 +159,7 @@ public GlossaryTerm load(@CheckForNull String glossaryTermName) throws IOExcepti static class GlossaryLoader extends CacheLoader { @Override public Glossary load(@CheckForNull String glossaryName) throws IOException { - Glossary glossary = glossaryRepository.getByName(null, glossaryName, Fields.EMPTY_FIELDS); + Glossary glossary = Entity.getEntityByName(Entity.GLOSSARY, glossaryName, "", NON_DELETED); LOG.info("Loaded glossary {}:{}", glossary.getName(), glossary.getId()); return glossary; } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java index 0ad2bec2e3b1..1577ec8e881f 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/RoleResource.java @@ -63,7 +63,6 @@ import org.openmetadata.service.resources.Collection; import org.openmetadata.service.resources.EntityResource; import org.openmetadata.service.security.Authorizer; -import org.openmetadata.service.security.policyevaluator.RoleCache; import org.openmetadata.service.util.EntityUtil.Fields; import org.openmetadata.service.util.RestUtil; import org.openmetadata.service.util.ResultList; @@ -112,7 +111,6 @@ public void initialize(OpenMetadataApplicationConfig config) throws IOException } repository.initializeEntity(role); } - RoleCache.initialize(); } public static class RoleList extends ResultList { diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/UserResource.java b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/UserResource.java index 51ecc0d60623..b9d56fe7ef1d 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/UserResource.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/resources/teams/UserResource.java @@ -132,7 +132,6 @@ import org.openmetadata.service.security.jwt.JWTTokenGenerator; import org.openmetadata.service.security.policyevaluator.OperationContext; import org.openmetadata.service.security.policyevaluator.ResourceContext; -import org.openmetadata.service.security.policyevaluator.SubjectCache; import org.openmetadata.service.security.saml.JwtTokenCacheManager; import org.openmetadata.service.util.EmailUtil; import org.openmetadata.service.util.EntityUtil; @@ -194,8 +193,6 @@ public void initialize(OpenMetadataApplicationConfig config) { this.authenticationConfiguration = config.getAuthenticationConfiguration(); SmtpSettings smtpSettings = config.getSmtpSettings(); this.isEmailServiceEnabled = smtpSettings != null && smtpSettings.getEnableSmtpServer(); - // Keep this before initializeUsers, else getUpdater() will fail - SubjectCache.initialize(); this.repository.initializeUsers(config); } @@ -523,7 +520,7 @@ public Response createUser( authHandler.sendInviteMailToUser( uriInfo, user, - String.format("Welcome to %s", EmailUtil.getInstance().getEmailingEntity()), + String.format("Welcome to %s", EmailUtil.getEmailingEntity()), create.getCreatePasswordType(), create.getPassword()); } catch (Exception ex) { @@ -635,7 +632,7 @@ public Response revokeToken( RestUtil.PutResponse response = repository.createOrUpdate(uriInfo, user); addHref(uriInfo, response.getEntity()); // Invalidate Bot Token in Cache - BotTokenCache.getInstance().invalidateToken(user.getName()); + BotTokenCache.invalidateToken(user.getName()); return response.toResponse(); } @@ -910,10 +907,7 @@ public Response generateResetPasswordLink(@Context UriInfo uriInfo, @Valid Email try { // send a mail to the User with the Update authHandler.sendPasswordResetLink( - uriInfo, - registeredUser, - EmailUtil.getInstance().getPasswordResetSubject(), - EmailUtil.PASSWORD_RESET_TEMPLATE_FILE); + uriInfo, registeredUser, EmailUtil.getPasswordResetSubject(), EmailUtil.PASSWORD_RESET_TEMPLATE_FILE); } catch (Exception ex) { LOG.error("Error in sending mail for reset password" + ex.getMessage()); return Response.status(424).entity(new ErrorMessage(424, EMAIL_SENDING_ISSUE)).build(); @@ -1117,7 +1111,7 @@ public Response revokePersonalAccessToken( List ids = request.getTokenIds().stream().map(UUID::toString).collect(Collectors.toList()); tokenRepository.deleteAllToken(ids); } - UserTokenCache.getInstance().invalidateToken(user.getName()); + UserTokenCache.invalidateToken(user.getName()); List tokens = tokenRepository.findByUserIdAndType(user.getId().toString(), TokenType.PERSONAL_ACCESS_TOKEN.value()); return Response.status(Response.Status.OK).entity(new ResultList<>(tokens)).build(); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/search/EntityBuilderConstant.java b/openmetadata-service/src/main/java/org/openmetadata/service/search/EntityBuilderConstant.java index 108c26a2dbf4..ebc2bf7ee614 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/search/EntityBuilderConstant.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/search/EntityBuilderConstant.java @@ -1,22 +1,10 @@ package org.openmetadata.service.search; public class EntityBuilderConstant { - public static final String FIELD_OWNER = "owner"; - public static final String FIELD_NAME = "name"; - public static final String FIELD_DESCRIPTION = "description"; - public static final String FIELD_FOLLOWERS = "followers"; - public static final String FIELD_VOTES = "votes"; - public static final String FIELD_TAGS = "tags"; - public static final String FIELD_DELETED = "deleted"; - public static final String FIELD_PIPELINE_STATUS = "pipelineStatus"; - public static final String FIELD_DISPLAY_NAME = "displayName"; - public static final String FIELD_EXTENSION = "extension"; - public static final String FIELD_USAGE_SUMMARY = "usageSummary"; public static final String UNIFIED = "unified"; public static final String ES_MESSAGE_SCHEMA_FIELD = "messageSchema.schemaFields.name"; public static final String ES_TAG_FQN_FIELD = "tags.tagFQN"; public static final String NAME_KEYWORD = "name.keyword"; - public static final String DISPLAY_NAME = "displayName"; public static final String FIELD_NAME_NGRAM = "name.ngram"; public static final String DISPLAY_NAME_KEYWORD = "displayName.keyword"; public static final String FIELD_DISPLAY_NAME_NGRAM = "displayName.ngram"; @@ -25,7 +13,6 @@ public class EntityBuilderConstant { public static final String POST_TAG = ""; public static final Integer MAX_AGGREGATE_SIZE = 50; public static final Integer MAX_RESULT_HITS = 10000; - public static final String DESCRIPTION = "description"; public static final String QUERY = "query"; public static final String QUERY_NGRAM = "query.ngram"; diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/search/elasticSearch/ElasticSearchClientImpl.java b/openmetadata-service/src/main/java/org/openmetadata/service/search/elasticSearch/ElasticSearchClientImpl.java index 870226b00eff..0a7dfac5fc16 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/search/elasticSearch/ElasticSearchClientImpl.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/search/elasticSearch/ElasticSearchClientImpl.java @@ -403,20 +403,20 @@ public Response suggest(ElasticSearchRequest request) throws IOException { private static SearchSourceBuilder buildPipelineSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) - .field(EntityBuilderConstant.DESCRIPTION, 1.0f) + .field(FIELD_DESCRIPTION, 1.0f) .field("tasks.name", 2.0f) .field("tasks.description", 1.0f) .defaultOperator(Operator.AND) .fuzziness(Fuzziness.AUTO); HighlightBuilder.Field highlightPipelineName = new HighlightBuilder.Field(FIELD_DISPLAY_NAME); highlightPipelineName.highlighterType(EntityBuilderConstant.UNIFIED); - HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(EntityBuilderConstant.DESCRIPTION); + HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(FIELD_DESCRIPTION); highlightDescription.highlighterType(EntityBuilderConstant.UNIFIED); HighlightBuilder.Field highlightTasks = new HighlightBuilder.Field("tasks.name"); highlightTasks.highlighterType(EntityBuilderConstant.UNIFIED); @@ -436,20 +436,20 @@ private static SearchSourceBuilder buildPipelineSearchBuilder(String query, int private static SearchSourceBuilder buildMlModelSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) - .field(EntityBuilderConstant.DESCRIPTION, 1.0f) + .field(FIELD_DESCRIPTION, 1.0f) .field("mlFeatures.name", 2.0f) .field("mlFeatures.description", 1.0f) .defaultOperator(Operator.AND) .fuzziness(Fuzziness.AUTO); HighlightBuilder.Field highlightPipelineName = new HighlightBuilder.Field(FIELD_DISPLAY_NAME); highlightPipelineName.highlighterType(EntityBuilderConstant.UNIFIED); - HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(EntityBuilderConstant.DESCRIPTION); + HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(FIELD_DESCRIPTION); highlightDescription.highlighterType(EntityBuilderConstant.UNIFIED); HighlightBuilder.Field highlightTasks = new HighlightBuilder.Field("mlFeatures.name"); highlightTasks.highlighterType(EntityBuilderConstant.UNIFIED); @@ -467,14 +467,14 @@ private static SearchSourceBuilder buildMlModelSearchBuilder(String query, int f private static SearchSourceBuilder buildTopicSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_NAME_NGRAM) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) - .field(EntityBuilderConstant.FIELD_DESCRIPTION, 1.0f) + .field(Entity.FIELD_DESCRIPTION, 1.0f) .field(EntityBuilderConstant.ES_MESSAGE_SCHEMA_FIELD, 2.0f) .field("messageSchema.schemaFields.description", 1.0f) .field("messageSchema.schemaFields.children.name", 2.0f) @@ -503,14 +503,14 @@ private static SearchSourceBuilder buildTopicSearchBuilder(String query, int fro private static SearchSourceBuilder buildDashboardSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_NAME_NGRAM) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) - .field(EntityBuilderConstant.FIELD_DESCRIPTION, 1.0f) + .field(Entity.FIELD_DESCRIPTION, 1.0f) .field("charts.name", 2.0f) .field("charts.description", 1.0f) .defaultOperator(Operator.AND) @@ -541,13 +541,13 @@ private static SearchSourceBuilder buildDashboardSearchBuilder(String query, int private static SearchSourceBuilder buildTableSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryStringBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_NAME_NGRAM) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) - .field(EntityBuilderConstant.FIELD_DESCRIPTION, 1.0f) + .field(Entity.FIELD_DESCRIPTION, 1.0f) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field("columns.name.keyword", 10.0f) .field("columns.name", 2.0f) @@ -569,7 +569,7 @@ private static SearchSourceBuilder buildTableSearchBuilder(String query, int fro queryBuilder.boostMode(CombineFunction.SUM); HighlightBuilder.Field highlightTableName = new HighlightBuilder.Field(FIELD_DISPLAY_NAME); highlightTableName.highlighterType(EntityBuilderConstant.UNIFIED); - HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(EntityBuilderConstant.DESCRIPTION); + HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(FIELD_DESCRIPTION); highlightDescription.highlighterType(EntityBuilderConstant.UNIFIED); HighlightBuilder hb = new HighlightBuilder(); HighlightBuilder.Field highlightColumns = new HighlightBuilder.Field("columns.name"); @@ -597,7 +597,7 @@ private static SearchSourceBuilder buildTableSearchBuilder(String query, int fro private static SearchSourceBuilder buildUserOrTeamSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 3.0f) + .field(Entity.FIELD_DISPLAY_NAME, 3.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 5.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 2.0f) @@ -610,16 +610,16 @@ private static SearchSourceBuilder buildUserOrTeamSearchBuilder(String query, in private static SearchSourceBuilder buildGlossaryTermSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 10.0f) + .field(Entity.FIELD_DISPLAY_NAME, 10.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM, 1.0f) .field(FIELD_NAME, 10.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 10.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 10.0f) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 10.0f) + .field(Entity.FIELD_DISPLAY_NAME, 10.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field("synonyms", 5.0f) .field("synonyms.ngram") - .field(EntityBuilderConstant.DESCRIPTION, 3.0f) + .field(FIELD_DESCRIPTION, 3.0f) .field("glossary.name", 5.0f) .field("glossary.displayName", 5.0f) .field("glossary.displayName.ngram") @@ -658,10 +658,10 @@ private static SearchSourceBuilder buildTagSearchBuilder(String query, int from, QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) .field(FIELD_NAME, 10.0f) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 10.0f) + .field(Entity.FIELD_DISPLAY_NAME, 10.0f) .field(EntityBuilderConstant.FIELD_NAME_NGRAM, 1.0f) .field("classification.name", 1.0f) - .field(EntityBuilderConstant.DESCRIPTION, 3.0f) + .field(FIELD_DESCRIPTION, 3.0f) .defaultOperator(Operator.AND) .fuzziness(Fuzziness.AUTO); @@ -684,10 +684,10 @@ private static SearchSourceBuilder buildTagSearchBuilder(String query, int from, private static SearchSourceBuilder buildContainerSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) - .field(EntityBuilderConstant.FIELD_DESCRIPTION, 1.0f) + .field(Entity.FIELD_DESCRIPTION, 1.0f) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) @@ -702,7 +702,7 @@ private static SearchSourceBuilder buildContainerSearchBuilder(String query, int .fuzziness(Fuzziness.AUTO); HighlightBuilder.Field highlightContainerName = new HighlightBuilder.Field(FIELD_DISPLAY_NAME); highlightContainerName.highlighterType(EntityBuilderConstant.UNIFIED); - HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(EntityBuilderConstant.DESCRIPTION); + HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(FIELD_DESCRIPTION); highlightDescription.highlighterType(EntityBuilderConstant.UNIFIED); HighlightBuilder hb = new HighlightBuilder(); HighlightBuilder.Field highlightColumns = new HighlightBuilder.Field("dataModel.columns.name"); @@ -728,16 +728,16 @@ private static SearchSourceBuilder buildContainerSearchBuilder(String query, int private static SearchSourceBuilder buildQuerySearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.DISPLAY_NAME, 10.0f) + .field(FIELD_DISPLAY_NAME, 10.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(EntityBuilderConstant.QUERY, 10.0f) .field(EntityBuilderConstant.QUERY_NGRAM) - .field(EntityBuilderConstant.DESCRIPTION, 1.0f) + .field(FIELD_DESCRIPTION, 1.0f) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .defaultOperator(Operator.AND) .fuzziness(Fuzziness.AUTO); - HighlightBuilder.Field highlightGlossaryName = new HighlightBuilder.Field(EntityBuilderConstant.DISPLAY_NAME); + HighlightBuilder.Field highlightGlossaryName = new HighlightBuilder.Field(FIELD_DISPLAY_NAME); highlightGlossaryName.highlighterType(EntityBuilderConstant.UNIFIED); HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(FIELD_DESCRIPTION); highlightDescription.highlighterType(EntityBuilderConstant.UNIFIED); @@ -756,7 +756,7 @@ private static SearchSourceBuilder buildTestCaseSearch(String query, int from, i QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) .field(FIELD_NAME, 10.0f) - .field(EntityBuilderConstant.DESCRIPTION, 3.0f) + .field(FIELD_DESCRIPTION, 3.0f) .field("testSuite.fullyQualifiedName", 10.0f) .field("testSuite.name", 10.0f) .field("testSuite.description", 3.0f) diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/search/openSearch/OpenSearchClientImpl.java b/openmetadata-service/src/main/java/org/openmetadata/service/search/openSearch/OpenSearchClientImpl.java index d414c976cdce..a023669b6981 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/search/openSearch/OpenSearchClientImpl.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/search/openSearch/OpenSearchClientImpl.java @@ -154,11 +154,11 @@ public OpenSearchClientImpl(ElasticSearchConfiguration esConfig, CollectionDAO d this.dao = dao; } - private static NamedXContentRegistry xContentRegistry; + private static final NamedXContentRegistry X_CONTENT_REGISTRY; static { SearchModule searchModule = new SearchModule(Settings.EMPTY, false, List.of()); - xContentRegistry = new NamedXContentRegistry(searchModule.getNamedXContents()); + X_CONTENT_REGISTRY = new NamedXContentRegistry(searchModule.getNamedXContents()); } @Override @@ -286,7 +286,7 @@ public Response search(ElasticSearchRequest request) throws IOException { XContentParser filterParser = XContentType.JSON .xContent() - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, request.getQueryFilter()); + .createParser(X_CONTENT_REGISTRY, LoggingDeprecationHandler.INSTANCE, request.getQueryFilter()); QueryBuilder filter = SearchSourceBuilder.fromXContent(filterParser).query(); BoolQueryBuilder newQuery = QueryBuilders.boolQuery().must(searchSourceBuilder.query()).filter(filter); searchSourceBuilder.query(newQuery); @@ -300,7 +300,7 @@ public Response search(ElasticSearchRequest request) throws IOException { XContentParser filterParser = XContentType.JSON .xContent() - .createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, request.getPostFilter()); + .createParser(X_CONTENT_REGISTRY, LoggingDeprecationHandler.INSTANCE, request.getPostFilter()); QueryBuilder filter = SearchSourceBuilder.fromXContent(filterParser).query(); searchSourceBuilder.postFilter(filter); } catch (Exception ex) { @@ -344,8 +344,8 @@ public Response search(ElasticSearchRequest request) throws IOException { public Response aggregate(String index, String fieldName, String value, String query) throws IOException { SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); XContentParser filterParser = - XContentType.JSON.xContent().createParser(xContentRegistry, LoggingDeprecationHandler.INSTANCE, query); - QueryBuilder filter = searchSourceBuilder.fromXContent(filterParser).query(); + XContentType.JSON.xContent().createParser(X_CONTENT_REGISTRY, LoggingDeprecationHandler.INSTANCE, query); + QueryBuilder filter = SearchSourceBuilder.fromXContent(filterParser).query(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(filter); searchSourceBuilder @@ -403,20 +403,20 @@ public Response suggest(ElasticSearchRequest request) throws IOException { private static SearchSourceBuilder buildPipelineSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) - .field(EntityBuilderConstant.DESCRIPTION, 1.0f) + .field(FIELD_DESCRIPTION, 1.0f) .field("tasks.name", 2.0f) .field("tasks.description", 1.0f) .defaultOperator(Operator.AND) .fuzziness(Fuzziness.AUTO); HighlightBuilder.Field highlightPipelineName = new HighlightBuilder.Field(FIELD_DISPLAY_NAME); highlightPipelineName.highlighterType(EntityBuilderConstant.UNIFIED); - HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(EntityBuilderConstant.DESCRIPTION); + HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(FIELD_DESCRIPTION); highlightDescription.highlighterType(EntityBuilderConstant.UNIFIED); HighlightBuilder.Field highlightTasks = new HighlightBuilder.Field("tasks.name"); highlightTasks.highlighterType(EntityBuilderConstant.UNIFIED); @@ -436,20 +436,20 @@ private static SearchSourceBuilder buildPipelineSearchBuilder(String query, int private static SearchSourceBuilder buildMlModelSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) - .field(EntityBuilderConstant.DESCRIPTION, 1.0f) + .field(FIELD_DESCRIPTION, 1.0f) .field("mlFeatures.name", 2.0f) .field("mlFeatures.description", 1.0f) .defaultOperator(Operator.AND) .fuzziness(Fuzziness.AUTO); HighlightBuilder.Field highlightPipelineName = new HighlightBuilder.Field(FIELD_DISPLAY_NAME); highlightPipelineName.highlighterType(EntityBuilderConstant.UNIFIED); - HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(EntityBuilderConstant.DESCRIPTION); + HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(FIELD_DESCRIPTION); highlightDescription.highlighterType(EntityBuilderConstant.UNIFIED); HighlightBuilder.Field highlightTasks = new HighlightBuilder.Field("mlFeatures.name"); highlightTasks.highlighterType(EntityBuilderConstant.UNIFIED); @@ -467,14 +467,14 @@ private static SearchSourceBuilder buildMlModelSearchBuilder(String query, int f private static SearchSourceBuilder buildTopicSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_NAME_NGRAM) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) - .field(EntityBuilderConstant.FIELD_DESCRIPTION, 1.0f) + .field(Entity.FIELD_DESCRIPTION, 1.0f) .field(EntityBuilderConstant.ES_MESSAGE_SCHEMA_FIELD, 2.0f) .field("messageSchema.schemaFields.description", 1.0f) .field("messageSchema.schemaFields.children.name", 2.0f) @@ -503,14 +503,14 @@ private static SearchSourceBuilder buildTopicSearchBuilder(String query, int fro private static SearchSourceBuilder buildDashboardSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_NAME_NGRAM) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) - .field(EntityBuilderConstant.FIELD_DESCRIPTION, 1.0f) + .field(Entity.FIELD_DESCRIPTION, 1.0f) .field("charts.name", 2.0f) .field("charts.description", 1.0f) .defaultOperator(Operator.AND) @@ -541,13 +541,13 @@ private static SearchSourceBuilder buildDashboardSearchBuilder(String query, int private static SearchSourceBuilder buildTableSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryStringBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_NAME_NGRAM) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) - .field(EntityBuilderConstant.FIELD_DESCRIPTION, 1.0f) + .field(Entity.FIELD_DESCRIPTION, 1.0f) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field("columns.name.keyword", 10.0f) .field("columns.name", 2.0f) @@ -569,7 +569,7 @@ private static SearchSourceBuilder buildTableSearchBuilder(String query, int fro queryBuilder.boostMode(CombineFunction.SUM); HighlightBuilder.Field highlightTableName = new HighlightBuilder.Field(FIELD_DISPLAY_NAME); highlightTableName.highlighterType(EntityBuilderConstant.UNIFIED); - HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(EntityBuilderConstant.DESCRIPTION); + HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(FIELD_DESCRIPTION); highlightDescription.highlighterType(EntityBuilderConstant.UNIFIED); HighlightBuilder hb = new HighlightBuilder(); HighlightBuilder.Field highlightColumns = new HighlightBuilder.Field("columns.name"); @@ -598,7 +598,7 @@ private static SearchSourceBuilder buildTableSearchBuilder(String query, int fro private static SearchSourceBuilder buildUserOrTeamSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.DISPLAY_NAME, 3.0f) + .field(FIELD_DISPLAY_NAME, 3.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 5.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 2.0f) @@ -611,16 +611,16 @@ private static SearchSourceBuilder buildUserOrTeamSearchBuilder(String query, in private static SearchSourceBuilder buildGlossaryTermSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 10.0f) + .field(Entity.FIELD_DISPLAY_NAME, 10.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM, 1.0f) .field(FIELD_NAME, 10.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 10.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 10.0f) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 10.0f) + .field(Entity.FIELD_DISPLAY_NAME, 10.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field("synonyms", 5.0f) .field("synonyms.ngram") - .field(EntityBuilderConstant.DESCRIPTION, 3.0f) + .field(FIELD_DESCRIPTION, 3.0f) .field("glossary.name", 5.0f) .field("glossary.displayName", 5.0f) .field("glossary.displayName.ngram") @@ -659,9 +659,9 @@ private static SearchSourceBuilder buildTagSearchBuilder(String query, int from, QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) .field(FIELD_NAME, 10.0f) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 10.0f) + .field(Entity.FIELD_DISPLAY_NAME, 10.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM, 1.0f) - .field(EntityBuilderConstant.DESCRIPTION, 3.0f) + .field(FIELD_DESCRIPTION, 3.0f) .defaultOperator(Operator.AND) .fuzziness(Fuzziness.AUTO); @@ -684,10 +684,10 @@ private static SearchSourceBuilder buildTagSearchBuilder(String query, int from, private static SearchSourceBuilder buildContainerSearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.FIELD_DISPLAY_NAME, 15.0f) + .field(Entity.FIELD_DISPLAY_NAME, 15.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(FIELD_NAME, 15.0f) - .field(EntityBuilderConstant.FIELD_DESCRIPTION, 1.0f) + .field(Entity.FIELD_DESCRIPTION, 1.0f) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .field(EntityBuilderConstant.DISPLAY_NAME_KEYWORD, 25.0f) .field(EntityBuilderConstant.NAME_KEYWORD, 25.0f) @@ -702,7 +702,7 @@ private static SearchSourceBuilder buildContainerSearchBuilder(String query, int .fuzziness(Fuzziness.AUTO); HighlightBuilder.Field highlightContainerName = new HighlightBuilder.Field(FIELD_DISPLAY_NAME); highlightContainerName.highlighterType(EntityBuilderConstant.UNIFIED); - HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(EntityBuilderConstant.DESCRIPTION); + HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(FIELD_DESCRIPTION); highlightDescription.highlighterType(EntityBuilderConstant.UNIFIED); HighlightBuilder hb = new HighlightBuilder(); HighlightBuilder.Field highlightColumns = new HighlightBuilder.Field("dataModel.columns.name"); @@ -728,16 +728,16 @@ private static SearchSourceBuilder buildContainerSearchBuilder(String query, int private static SearchSourceBuilder buildQuerySearchBuilder(String query, int from, int size) { QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) - .field(EntityBuilderConstant.DISPLAY_NAME, 10.0f) + .field(FIELD_DISPLAY_NAME, 10.0f) .field(EntityBuilderConstant.FIELD_DISPLAY_NAME_NGRAM) .field(EntityBuilderConstant.QUERY, 10.0f) .field(EntityBuilderConstant.QUERY_NGRAM) - .field(EntityBuilderConstant.DESCRIPTION, 1.0f) + .field(FIELD_DESCRIPTION, 1.0f) .field(EntityBuilderConstant.FIELD_DESCRIPTION_NGRAM, 1.0f) .defaultOperator(Operator.AND) .fuzziness(Fuzziness.AUTO); - HighlightBuilder.Field highlightGlossaryName = new HighlightBuilder.Field(EntityBuilderConstant.DISPLAY_NAME); + HighlightBuilder.Field highlightGlossaryName = new HighlightBuilder.Field(FIELD_DISPLAY_NAME); highlightGlossaryName.highlighterType(EntityBuilderConstant.UNIFIED); HighlightBuilder.Field highlightDescription = new HighlightBuilder.Field(FIELD_DESCRIPTION); highlightDescription.highlighterType(EntityBuilderConstant.UNIFIED); @@ -756,7 +756,7 @@ private static SearchSourceBuilder buildTestCaseSearch(String query, int from, i QueryStringQueryBuilder queryBuilder = QueryBuilders.queryStringQuery(query) .field(FIELD_NAME, 10.0f) - .field(EntityBuilderConstant.DESCRIPTION, 3.0f) + .field(FIELD_DESCRIPTION, 3.0f) .field("testSuite.fullyQualifiedName", 10.0f) .field("testSuite.name", 10.0f) .field("testSuite.description", 3.0f) @@ -808,9 +808,9 @@ private static SearchSourceBuilder addAggregation(SearchSourceBuilder builder) { AggregationBuilders.terms("owner.displayName.keyword") .field("owner.displayName.keyword") .size(EntityBuilderConstant.MAX_AGGREGATE_SIZE)) - .aggregation(AggregationBuilders.terms("tags.tagFQN").field("tags.tagFQN")); - ; - + .aggregation( + AggregationBuilders.terms(EntityBuilderConstant.ES_TAG_FQN_FIELD) + .field(EntityBuilderConstant.ES_TAG_FQN_FIELD)); return builder; } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/DefaultAuthorizer.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/DefaultAuthorizer.java index f1bae44b31ee..c88c90d7bb90 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/DefaultAuthorizer.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/DefaultAuthorizer.java @@ -117,7 +117,7 @@ public static SubjectContext getSubjectContext(SecurityContext securityContext) } public static SubjectContext getSubjectContext(String userName) { - return SubjectCache.getInstance().getSubjectContext(EntityInterfaceUtil.quoteName(userName)); + return SubjectCache.getSubjectContext(EntityInterfaceUtil.quoteName(userName)); } private SubjectContext changeSubjectContext(String user, SubjectContext loggedInUser) { diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/JwtFilter.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/JwtFilter.java index ed00c26c62ee..e1bb3c288d01 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/JwtFilter.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/JwtFilter.java @@ -237,14 +237,14 @@ public static String extractToken(String tokenFromHeader) { } private void validateBotToken(String tokenFromHeader, String userName) { - if (tokenFromHeader.equals(BotTokenCache.getInstance().getToken(userName))) { + if (tokenFromHeader.equals(BotTokenCache.getToken(userName))) { return; } throw AuthenticationException.getInvalidTokenException(); } private void validatePersonalAccessToken(String tokenFromHeader, String userName) { - if (UserTokenCache.getInstance().getToken(userName).contains(tokenFromHeader)) { + if (UserTokenCache.getToken(userName).contains(tokenFromHeader)) { return; } throw AuthenticationException.getInvalidTokenException(); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/NoopAuthorizer.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/NoopAuthorizer.java index 7e2b247571ac..3f7e1e201346 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/NoopAuthorizer.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/NoopAuthorizer.java @@ -21,6 +21,7 @@ import org.jdbi.v3.core.Jdbi; import org.openmetadata.schema.entity.teams.User; import org.openmetadata.schema.type.EntityReference; +import org.openmetadata.schema.type.Include; import org.openmetadata.schema.type.Permission.Access; import org.openmetadata.schema.type.ResourcePermission; import org.openmetadata.service.Entity; @@ -30,15 +31,12 @@ import org.openmetadata.service.security.policyevaluator.OperationContext; import org.openmetadata.service.security.policyevaluator.PolicyEvaluator; import org.openmetadata.service.security.policyevaluator.ResourceContextInterface; -import org.openmetadata.service.security.policyevaluator.SubjectCache; -import org.openmetadata.service.util.EntityUtil.Fields; import org.openmetadata.service.util.RestUtil; @Slf4j public class NoopAuthorizer implements Authorizer { @Override public void init(OpenMetadataApplicationConfig openMetadataApplicationConfig, Jdbi jdbi) { - SubjectCache.initialize(); addAnonymousUser(); } @@ -68,7 +66,7 @@ public void authorize( private void addAnonymousUser() { String username = "anonymous"; try { - Entity.getEntityRepository(Entity.USER).getByName(null, username, Fields.EMPTY_FIELDS); + Entity.getEntityByName(Entity.USER, username, "", Include.NON_DELETED); } catch (EntityNotFoundException ex) { User user = new User() diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/BasicAuthenticator.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/BasicAuthenticator.java index 0cb37a18c555..92cf62545d05 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/BasicAuthenticator.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/BasicAuthenticator.java @@ -166,11 +166,9 @@ public void sendEmailVerification(UriInfo uriInfo, User user) throws IOException String emailVerificationLink = String.format( "%s/users/registrationConfirmation?user=%s&token=%s", - EmailUtil.getInstance().buildBaseUrl(uriInfo.getRequestUri()), - user.getFullyQualifiedName(), - mailVerificationToken); + EmailUtil.buildBaseUrl(uriInfo.getRequestUri()), user.getFullyQualifiedName(), mailVerificationToken); try { - EmailUtil.getInstance().sendEmailVerification(emailVerificationLink, user); + EmailUtil.sendEmailVerification(emailVerificationLink, user); } catch (TemplateException e) { LOG.error("Error in sending mail to the User : {}", e.getMessage(), e); throw new CustomExceptionMessage(424, EMAIL_SENDING_ISSUE); @@ -189,11 +187,9 @@ public void sendPasswordResetLink(UriInfo uriInfo, User user, String subject, St String passwordResetLink = String.format( "%s/users/password/reset?user=%s&token=%s", - EmailUtil.getInstance().buildBaseUrl(uriInfo.getRequestUri()), - user.getFullyQualifiedName(), - mailVerificationToken); + EmailUtil.buildBaseUrl(uriInfo.getRequestUri()), user.getFullyQualifiedName(), mailVerificationToken); try { - EmailUtil.getInstance().sendPasswordResetLink(passwordResetLink, user, subject, templateFilePath); + EmailUtil.sendPasswordResetLink(passwordResetLink, user, subject, templateFilePath); } catch (TemplateException e) { LOG.error("Error in sending mail to the User : {}", e.getMessage(), e); throw new CustomExceptionMessage(424, EMAIL_SENDING_ISSUE); @@ -235,7 +231,7 @@ public void resetUserPasswordWithToken(UriInfo uriInfo, PasswordResetRequest req // Update user about Password Change try { - EmailUtil.getInstance().sendAccountStatus(storedUser, "Update Password", "Change Successful"); + EmailUtil.sendAccountStatus(storedUser, "Update Password", "Change Successful"); } catch (TemplateException ex) { LOG.error("Error in sending Password Change Mail to User. Reason : " + ex.getMessage(), ex); throw new CustomExceptionMessage(424, EMAIL_SENDING_ISSUE); @@ -285,7 +281,7 @@ public void changeUserPwdWithOldPwd(UriInfo uriInfo, String userName, ChangePass sendInviteMailToUser( uriInfo, response.getEntity(), - String.format("%s: Password Update", EmailUtil.getInstance().getEmailingEntity()), + String.format("%s: Password Update", EmailUtil.getEmailingEntity()), ADMIN_CREATE, request.getNewPassword()); } @@ -298,19 +294,18 @@ public void sendInviteMailToUser( switch (requestType) { case ADMIN_CREATE: Map templatePopulator = new HashMap<>(); - templatePopulator.put(EmailUtil.ENTITY, EmailUtil.getInstance().getEmailingEntity()); - templatePopulator.put(EmailUtil.SUPPORT_URL, EmailUtil.getInstance().getSupportUrl()); + templatePopulator.put(EmailUtil.ENTITY, EmailUtil.getEmailingEntity()); + templatePopulator.put(EmailUtil.SUPPORT_URL, EmailUtil.getSupportUrl()); templatePopulator.put(EmailUtil.USERNAME, user.getName()); templatePopulator.put(EmailUtil.PASSWORD, pwd); - templatePopulator.put(EmailUtil.APPLICATION_LOGIN_LINK, EmailUtil.getInstance().getOMUrl()); + templatePopulator.put(EmailUtil.APPLICATION_LOGIN_LINK, EmailUtil.getOMUrl()); try { - EmailUtil.getInstance() - .sendMail( - subject, - templatePopulator, - user.getEmail(), - EmailUtil.EMAIL_TEMPLATE_BASEPATH, - EmailUtil.INVITE_RANDOM_PWD); + EmailUtil.sendMail( + subject, + templatePopulator, + user.getEmail(), + EmailUtil.EMAIL_TEMPLATE_BASEPATH, + EmailUtil.INVITE_RANDOM_PWD); } catch (TemplateException ex) { LOG.error("Failed in sending Mail to user [{}]. Reason : {}", user.getEmail(), ex.getMessage(), ex); } @@ -440,13 +435,12 @@ public void recordFailedLoginAttempt(String providedIdentity, User storedUser) t loginAttemptCache.recordFailedLogin(providedIdentity); int failedLoginAttempt = loginAttemptCache.getUserFailedLoginCount(providedIdentity); if (failedLoginAttempt == loginConfiguration.getMaxLoginFailAttempts()) { - EmailUtil.getInstance() - .sendAccountStatus( - storedUser, - "Multiple Failed Login Attempts.", - String.format( - "Someone is trying to access your account. Login is Blocked for %s minutes. Please change your password.", - loginConfiguration.getAccessBlockTime())); + EmailUtil.sendAccountStatus( + storedUser, + "Multiple Failed Login Attempts.", + String.format( + "Someone is trying to access your account. Login is Blocked for %s minutes. Please change your password.", + loginConfiguration.getAccessBlockTime())); } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/BotTokenCache.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/BotTokenCache.java index 56ec43f3d0c4..8a0d0c2c6d98 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/BotTokenCache.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/BotTokenCache.java @@ -24,11 +24,14 @@ @Slf4j public class BotTokenCache { public static final String EMPTY_STRING = ""; - private static BotTokenCache instance; private static final LoadingCache BOTS_TOKEN_CACHE = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(2, TimeUnit.MINUTES).build(new BotTokenLoader()); - public String getToken(String botName) { + private BotTokenCache() { + // Private constructor for utility class + } + + public static String getToken(String botName) { try { if (BOTS_TOKEN_CACHE.get(botName).equals(EMPTY_STRING)) { BOTS_TOKEN_CACHE.invalidate(botName); @@ -39,7 +42,7 @@ public String getToken(String botName) { } } - public void invalidateToken(String botName) { + public static void invalidateToken(String botName) { try { BOTS_TOKEN_CACHE.invalidate(botName); } catch (Exception ex) { @@ -47,13 +50,6 @@ public void invalidateToken(String botName) { } } - public static BotTokenCache getInstance() { - if (instance == null) { - instance = new BotTokenCache(); - } - return instance; - } - static class BotTokenLoader extends CacheLoader { @Override public String load(@CheckForNull String botName) throws IOException { diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/LdapAuthenticator.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/LdapAuthenticator.java index f6b4dce670b4..30ce504b11f0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/LdapAuthenticator.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/LdapAuthenticator.java @@ -143,13 +143,12 @@ public void recordFailedLoginAttempt(String providedIdentity, User storedUser) t loginAttemptCache.recordFailedLogin(providedIdentity); int failedLoginAttempt = loginAttemptCache.getUserFailedLoginCount(providedIdentity); if (failedLoginAttempt == loginConfiguration.getMaxLoginFailAttempts()) { - EmailUtil.getInstance() - .sendAccountStatus( - storedUser, - "Multiple Failed Login Attempts.", - String.format( - "Someone is tried accessing your account. Login is Blocked for %s seconds.", - loginConfiguration.getAccessBlockTime())); + EmailUtil.sendAccountStatus( + storedUser, + "Multiple Failed Login Attempts.", + String.format( + "Someone is tried accessing your account. Login is Blocked for %s seconds.", + loginConfiguration.getAccessBlockTime())); } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/LoginAttemptCache.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/LoginAttemptCache.java index 892c1aecc5c6..f5e3b88e42b5 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/LoginAttemptCache.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/LoginAttemptCache.java @@ -14,7 +14,6 @@ public class LoginAttemptCache { private final LoadingCache attemptsCache; public LoginAttemptCache(OpenMetadataApplicationConfig config) { - super(); LoginConfiguration loginConfiguration = config.getApplicationConfiguration().getLoginConfig(); long accessBlockTime = 600; if (loginConfiguration != null) { diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/UserTokenCache.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/UserTokenCache.java index 16869edd3a99..579ebc5d0a9e 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/UserTokenCache.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/auth/UserTokenCache.java @@ -27,8 +27,7 @@ @Slf4j public class UserTokenCache { - private static final UserTokenCache instance = new UserTokenCache(); - private static final LoadingCache> cache = + private static final LoadingCache> CACHE = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(2, TimeUnit.MINUTES).build(new UserTokenLoader()); private static volatile boolean initialized = false; private static TokenRepository tokenRepository; @@ -47,27 +46,23 @@ public static void initialize(CollectionDAO dao) { } } - public Set getToken(String userName) { + public static Set getToken(String userName) { try { - return cache.get(userName); + return CACHE.get(userName); } catch (ExecutionException | UncheckedExecutionException ex) { LOG.error("Token not found", ex); return null; } } - public void invalidateToken(String userName) { + public static void invalidateToken(String userName) { try { - cache.invalidate(userName); + CACHE.invalidate(userName); } catch (Exception ex) { LOG.error("Failed to invalidate User token cache for User {}", userName, ex); } } - public static UserTokenCache getInstance() { - return instance; - } - static class UserTokenLoader extends CacheLoader> { @Override public HashSet load(@CheckForNull String userName) throws IOException { diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/PolicyCache.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/PolicyCache.java index a69a0a8e63a3..aab480b4febb 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/PolicyCache.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/PolicyCache.java @@ -27,53 +27,33 @@ import lombok.extern.slf4j.Slf4j; import org.openmetadata.schema.entity.policies.Policy; import org.openmetadata.schema.entity.policies.accessControl.Rule; +import org.openmetadata.schema.type.Include; import org.openmetadata.service.Entity; import org.openmetadata.service.exception.EntityNotFoundException; -import org.openmetadata.service.jdbi3.PolicyRepository; -import org.openmetadata.service.util.EntityUtil.Fields; /** Subject context used for Access Control Policies */ @Slf4j public class PolicyCache { - private static final PolicyCache INSTANCE = new PolicyCache(); - private static volatile boolean initialized = false; + protected static final LoadingCache> CACHE = + CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new PolicyLoader()); - protected static LoadingCache> policyCache; - private static PolicyRepository policyRepository; - private static Fields fields; - - public static PolicyCache getInstance() { - return INSTANCE; - } - - /** To be called during application startup by Default Authorizer */ - public static void initialize() { - if (!initialized) { - policyCache = - CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new PolicyLoader()); - policyRepository = (PolicyRepository) Entity.getEntityRepository(Entity.POLICY); - fields = policyRepository.getFields("rules"); - initialized = true; - } - } - - public List getPolicyRules(UUID policyId) { + public static List getPolicyRules(UUID policyId) { try { - return policyCache.get(policyId); + return CACHE.get(policyId); } catch (ExecutionException | UncheckedExecutionException ex) { throw new EntityNotFoundException(ex.getMessage()); } } - public void invalidatePolicy(UUID policyId) { + public static void invalidatePolicy(UUID policyId) { try { - policyCache.invalidate(policyId); + CACHE.invalidate(policyId); } catch (Exception ex) { LOG.error("Failed to invalidate cache for policy {}", policyId, ex); } } - protected List getRules(Policy policy) { + protected static List getRules(Policy policy) { List rules = new ArrayList<>(); for (Rule r : policy.getRules()) { rules.add(new CompiledRule(r)); @@ -82,16 +62,15 @@ protected List getRules(Policy policy) { } public static void cleanUp() { - policyCache.cleanUp(); - initialized = false; + CACHE.cleanUp(); } static class PolicyLoader extends CacheLoader> { @Override public List load(@CheckForNull UUID policyId) throws IOException { - Policy policy = policyRepository.get(null, policyId, fields); + Policy policy = Entity.getEntity(Entity.POLICY, policyId, "rules", Include.NON_DELETED); LOG.info("Loaded policy {}:{}", policy.getName(), policy.getId()); - return PolicyCache.getInstance().getRules(policy); + return PolicyCache.getRules(policy); } } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/PostResourceContext.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/PostResourceContext.java index 0c9dd18f9ac0..7b90a58b2761 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/PostResourceContext.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/PostResourceContext.java @@ -22,7 +22,7 @@ public String getResource() { @Override public EntityReference getOwner() { - return SubjectCache.getInstance().getUser(postedBy).getEntityReference(); + return SubjectCache.getUser(postedBy).getEntityReference(); } @Override diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/RoleCache.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/RoleCache.java index 37b0d4e769ae..ceff3f1d1ca0 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/RoleCache.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/RoleCache.java @@ -26,60 +26,41 @@ import javax.annotation.CheckForNull; import lombok.extern.slf4j.Slf4j; import org.openmetadata.schema.entity.teams.Role; +import org.openmetadata.schema.type.Include; import org.openmetadata.service.Entity; import org.openmetadata.service.exception.EntityNotFoundException; -import org.openmetadata.service.jdbi3.RoleRepository; -import org.openmetadata.service.util.EntityUtil.Fields; /** Subject context used for Access Control Policies */ @Slf4j public class RoleCache { - private static final RoleCache INSTANCE = new RoleCache(); - private static volatile boolean initialized = false; - protected static LoadingCache roleCache; - protected static LoadingCache roleCacheWithId; - private static RoleRepository roleRepository; - private static Fields fields; + protected static final LoadingCache CACHE = + CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(3, TimeUnit.MINUTES).build(new RoleLoader()); + protected static final LoadingCache CACHE_WITH_ID = + CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(3, TimeUnit.MINUTES).build(new RoleLoaderWithId()); - public static RoleCache getInstance() { - return INSTANCE; + private RoleCache() { + // Private constructor for singleton } - /** To be called only once during the application start from DefaultAuthorizer */ - public static void initialize() { - if (!initialized) { - roleCache = - CacheBuilder.newBuilder().maximumSize(100).expireAfterWrite(3, TimeUnit.MINUTES).build(new RoleLoader()); - roleCacheWithId = - CacheBuilder.newBuilder() - .maximumSize(100) - .expireAfterWrite(3, TimeUnit.MINUTES) - .build(new RoleLoaderWithId()); - roleRepository = (RoleRepository) Entity.getEntityRepository(Entity.ROLE); - fields = roleRepository.getFields("policies"); - initialized = true; - } - } - - public Role getRole(String roleName) { + public static Role getRole(String roleName) { try { - return roleCache.get(roleName); + return CACHE.get(roleName); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage(entityNotFound(Entity.ROLE, roleName)); } } - public Role getRoleById(UUID roleId) { + public static Role getRoleById(UUID roleId) { try { - return roleCacheWithId.get(roleId); + return CACHE_WITH_ID.get(roleId); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage(entityNotFound(Entity.ROLE, roleId)); } } - public void invalidateRole(UUID roleId) { + public static void invalidateRole(UUID roleId) { try { - roleCacheWithId.invalidate(roleId); + CACHE_WITH_ID.invalidate(roleId); } catch (Exception ex) { LOG.error("Failed to invalidate cache for role {}", roleId, ex); } @@ -88,7 +69,7 @@ public void invalidateRole(UUID roleId) { static class RoleLoader extends CacheLoader { @Override public Role load(@CheckForNull String roleName) throws IOException { - Role role = roleRepository.getByName(null, roleName, fields); + Role role = Entity.getEntityByName(Entity.ROLE, roleName, "policies", Include.NON_DELETED); LOG.info("Loaded role {}:{}", role.getName(), role.getId()); return role; } @@ -97,14 +78,13 @@ public Role load(@CheckForNull String roleName) throws IOException { static class RoleLoaderWithId extends CacheLoader { @Override public Role load(@CheckForNull UUID roleId) throws IOException { - Role role = roleRepository.get(null, roleId, fields); + Role role = Entity.getEntity(Entity.ROLE, roleId, "policies", Include.NON_DELETED); LOG.info("Loaded role {}:{}", role.getName(), role.getId()); return role; } } public static void cleanUp() { - roleCacheWithId.cleanUp(); - initialized = false; + CACHE_WITH_ID.cleanUp(); } } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/RuleEvaluator.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/RuleEvaluator.java index 82e0eb1ca346..bd177ed59f78 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/RuleEvaluator.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/RuleEvaluator.java @@ -75,7 +75,7 @@ public boolean isOwner() throws IOException { public boolean matchAllTags(String... tagFQNs) throws IOException { if (expressionValidation) { for (String tagFqn : tagFQNs) { - TagLabelCache.getInstance().getTag(tagFqn); + TagLabelCache.getTag(tagFqn); } return false; } @@ -102,7 +102,7 @@ public boolean matchAllTags(String... tagFQNs) throws IOException { public boolean matchAnyTag(String... tagFQNs) throws IOException { if (expressionValidation) { for (String tagFqn : tagFQNs) { - TagLabelCache.getInstance().getTag(tagFqn); + TagLabelCache.getTag(tagFqn); } return false; } @@ -151,7 +151,7 @@ public boolean matchTeam() throws IOException { public boolean inAnyTeam(String... teams) { if (expressionValidation) { for (String team : teams) { - SubjectCache.getInstance().getTeamByName(team); + SubjectCache.getTeamByName(team); } return false; } @@ -179,7 +179,7 @@ public boolean inAnyTeam(String... teams) { public boolean hasAnyRole(String... roles) { if (expressionValidation) { for (String role : roles) { - RoleCache.getInstance().getRole(role); + RoleCache.getRole(role); } return false; } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/SubjectCache.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/SubjectCache.java index c1b063c62044..2cb47f78a09d 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/SubjectCache.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/SubjectCache.java @@ -14,6 +14,7 @@ package org.openmetadata.service.security.policyevaluator; import static org.openmetadata.common.utils.CommonUtil.listOrEmpty; +import static org.openmetadata.schema.type.Include.NON_DELETED; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; @@ -38,111 +39,79 @@ import org.openmetadata.service.Entity; import org.openmetadata.service.exception.CatalogExceptionMessage; import org.openmetadata.service.exception.EntityNotFoundException; -import org.openmetadata.service.jdbi3.TeamRepository; -import org.openmetadata.service.jdbi3.UserRepository; -import org.openmetadata.service.util.EntityUtil.Fields; /** Subject context used for Access Control Policies */ @Slf4j public class SubjectCache { - private static SubjectCache instance; - private static volatile boolean initialized = false; - protected static LoadingCache userCache; - protected static LoadingCache userCacheWihId; - protected static LoadingCache teamCache; - protected static LoadingCache teamCacheWithId; - protected static UserRepository userRepository; - protected static Fields userFields; - protected static TeamRepository teamRepository; - protected static Fields teamFields; - - // Expected to be called only once from the DefaultAuthorizer - public static void initialize() { - if (!initialized) { - userCache = - CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new UserLoader()); - userCacheWihId = - CacheBuilder.newBuilder() - .maximumSize(1000) - .expireAfterWrite(3, TimeUnit.MINUTES) - .build(new UserLoaderWithId()); - teamCache = - CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new TeamLoader()); - teamCacheWithId = - CacheBuilder.newBuilder() - .maximumSize(1000) - .expireAfterWrite(3, TimeUnit.MINUTES) - .build(new TeamLoaderWithId()); - userRepository = (UserRepository) Entity.getEntityRepository(Entity.USER); - userFields = userRepository.getFields("roles, teams, isAdmin, profile"); - teamRepository = (TeamRepository) Entity.getEntityRepository(Entity.TEAM); - teamFields = teamRepository.getFields("defaultRoles, policies, parents, profile"); - instance = new SubjectCache(); - initialized = true; - LOG.info("Subject cache is initialized"); - } else { - LOG.info("Subject cache is already initialized"); - } - } - - public static SubjectCache getInstance() { - return instance; - } - - public SubjectContext getSubjectContext(String userName) throws EntityNotFoundException { + protected static final LoadingCache USER_CACHE = + CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new UserLoader()); + protected static final LoadingCache USER_CACHE_WITH_ID = + CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new UserLoaderWithId()); + protected static final LoadingCache TEAM_CACHE = + CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new TeamLoader()); + protected static final LoadingCache TEAM_CACHE_WITH_ID = + CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(3, TimeUnit.MINUTES).build(new TeamLoaderWithId()); + private static final String USER_FIELDS = "roles,teams,isAdmin,profile"; + private static final String TEAM_FIELDS = "defaultRoles, policies, parents, profile"; + + private SubjectCache() { + // Private constructor for singleton + } + + public static SubjectContext getSubjectContext(String userName) throws EntityNotFoundException { try { - return userCache.get(userName); + return USER_CACHE.get(userName); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage(CatalogExceptionMessage.entityNotFound(Entity.USER, userName)); } } - public SubjectContext getSubjectContext(UUID userId) throws EntityNotFoundException { + public static SubjectContext getSubjectContext(UUID userId) throws EntityNotFoundException { try { - return userCacheWihId.get(userId); + return USER_CACHE_WITH_ID.get(userId); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage(CatalogExceptionMessage.entityNotFound(Entity.USER, userId)); } } - public User getUser(String userName) throws EntityNotFoundException { + public static User getUser(String userName) throws EntityNotFoundException { try { - return userCache.get(userName).getUser(); + return USER_CACHE.get(userName).getUser(); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage(CatalogExceptionMessage.entityNotFound(Entity.USER, userName)); } } - public User getUserById(String userId) throws EntityNotFoundException { + public static User getUserById(String userId) throws EntityNotFoundException { return getUserById(UUID.fromString(userId)); } - public User getUserById(UUID userId) throws EntityNotFoundException { + public static User getUserById(UUID userId) throws EntityNotFoundException { try { - return userCacheWihId.get(userId).getUser(); + return USER_CACHE_WITH_ID.get(userId).getUser(); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage(CatalogExceptionMessage.entityNotFound(Entity.USER, userId)); } } - public Team getTeam(UUID teamId) throws EntityNotFoundException { + public static Team getTeam(UUID teamId) throws EntityNotFoundException { try { - return teamCacheWithId.get(teamId); + return TEAM_CACHE_WITH_ID.get(teamId); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage(CatalogExceptionMessage.entityNotFound(Entity.TEAM, teamId)); } } - public Team getTeamByName(String teamName) throws EntityNotFoundException { + public static Team getTeamByName(String teamName) throws EntityNotFoundException { try { - return teamCache.get(teamName); + return TEAM_CACHE.get(teamName); } catch (ExecutionException | UncheckedExecutionException ex) { throw EntityNotFoundException.byMessage(CatalogExceptionMessage.entityNotFound(Entity.TEAM, teamName)); } } /** Return true if given list of teams is part of the hierarchy of parentTeam */ - public boolean isInTeam(String parentTeam, EntityReference team) { + public static boolean isInTeam(String parentTeam, EntityReference team) { Deque stack = new ArrayDeque<>(); stack.push(team); // Start with team and see if the parent matches while (!stack.isEmpty()) { @@ -156,7 +125,7 @@ public boolean isInTeam(String parentTeam, EntityReference team) { } /** Return true if the given user has any roles the list of roles */ - public boolean hasRole(User user, String role) { + public static boolean hasRole(User user, String role) { Deque stack = new ArrayDeque<>(); // If user has one of the roles directly assigned then return true if (hasRole(user.getRoles(), role)) { @@ -179,28 +148,27 @@ private static boolean hasRole(List userRoles, String expectedR public static void cleanUp() { LOG.info("Subject cache is cleaned up"); - userCache.invalidateAll(); - teamCacheWithId.invalidateAll(); - initialized = false; + USER_CACHE.invalidateAll(); + TEAM_CACHE_WITH_ID.invalidateAll(); } - public void invalidateUser(String userName) { + public static void invalidateUser(String userName) { try { - userCache.invalidate(userName); + USER_CACHE.invalidate(userName); } catch (Exception ex) { LOG.error("Failed to invalidate cache for user {}", userName, ex); } } - public void invalidateTeam(UUID teamId) { + public static void invalidateTeam(UUID teamId) { try { - teamCacheWithId.invalidate(teamId); + TEAM_CACHE_WITH_ID.invalidate(teamId); } catch (Exception ex) { LOG.error("Failed to invalidate cache for team {}", teamId, ex); } } - public List getRolesForTeams(List teams) { + public static List getRolesForTeams(List teams) { List roles = new ArrayList<>(); for (EntityReference teamRef : listOrEmpty(teams)) { Team team = getTeam(teamRef.getId()); @@ -215,10 +183,9 @@ public List getRolesForTeams(List teams) { static class UserLoader extends CacheLoader { @Override public SubjectContext load(@CheckForNull String userName) throws IOException { - // XXX try { - System.out.println("Loading user by name " + userName); - User user = userRepository.getByName(null, EntityInterfaceUtil.quoteName(userName), userFields); + User user = + Entity.getEntityByName(Entity.USER, EntityInterfaceUtil.quoteName(userName), USER_FIELDS, NON_DELETED); LOG.info("Loaded user {}:{}", user.getName(), user.getId()); return new SubjectContext(user); } catch (Exception ex) { @@ -231,7 +198,7 @@ public SubjectContext load(@CheckForNull String userName) throws IOException { static class UserLoaderWithId extends CacheLoader { @Override public SubjectContext load(@CheckForNull UUID uid) throws IOException { - User user = userRepository.get(null, uid, userFields); + User user = Entity.getEntity(Entity.USER, uid, USER_FIELDS, NON_DELETED); LOG.info("Loaded user {}:{}", user.getName(), user.getId()); return new SubjectContext(user); } @@ -239,9 +206,9 @@ public SubjectContext load(@CheckForNull UUID uid) throws IOException { static class TeamLoader extends CacheLoader { @Override - public Team load(@CheckForNull String userName) throws IOException { - Team team = teamRepository.getByName(null, userName, teamFields); - LOG.info("Loaded user {}:{}", team.getName(), team.getId()); + public Team load(@CheckForNull String teamName) throws IOException { + Team team = Entity.getEntityByName(Entity.TEAM, teamName, TEAM_FIELDS, NON_DELETED); + LOG.info("Loaded team {}:{}", team.getName(), team.getId()); return team; } } @@ -249,7 +216,7 @@ public Team load(@CheckForNull String userName) throws IOException { static class TeamLoaderWithId extends CacheLoader { @Override public Team load(@NonNull UUID teamId) throws IOException { - Team team = teamRepository.get(null, teamId, teamFields); + Team team = Entity.getEntity(Entity.TEAM, teamId, TEAM_FIELDS, NON_DELETED); LOG.info("Loaded team {}:{}", team.getName(), team.getId()); return team; } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/SubjectContext.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/SubjectContext.java index cb48396f3909..334bcdaf8657 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/SubjectContext.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/SubjectContext.java @@ -74,10 +74,10 @@ public boolean isUserUnderTeam(String parentTeam) { /** Returns true if the given resource owner is under the team hierarchy of parentTeam */ public boolean isTeamAsset(String parentTeam, EntityReference owner) { if (owner.getType().equals(Entity.USER)) { - SubjectContext subjectContext = SubjectCache.getInstance().getSubjectContext(owner.getName()); + SubjectContext subjectContext = SubjectCache.getSubjectContext(owner.getName()); return subjectContext.isUserUnderTeam(parentTeam); } else if (owner.getType().equals(Entity.TEAM)) { - Team team = SubjectCache.getInstance().getTeam(owner.getId()); + Team team = SubjectCache.getTeam(owner.getId()); return isInTeam(parentTeam, team.getEntityReference()); } return false; @@ -85,7 +85,7 @@ public boolean isTeamAsset(String parentTeam, EntityReference owner) { /** Return true if the team is part of the hierarchy of parentTeam */ private boolean isInTeam(String parentTeam, EntityReference team) { - return SubjectCache.getInstance().isInTeam(parentTeam, team); + return SubjectCache.isInTeam(parentTeam, team); } // Iterate over all the policies of the team hierarchy the user belongs to @@ -99,7 +99,7 @@ public List getTeams() { /** Returns true if the user has any of the roles (either direct or inherited roles) */ public boolean hasAnyRole(String roles) { - return SubjectCache.getInstance().hasRole(getUser(), roles); + return SubjectCache.hasRole(getUser(), roles); } @Getter @@ -163,7 +163,7 @@ public PolicyContext next() { } EntityReference policy = policies.get(policyIndex++); return new PolicyContext( - entityType, entityName, roleName, policy.getName(), PolicyCache.getInstance().getPolicyRules(policy.getId())); + entityType, entityName, roleName, policy.getName(), PolicyCache.getPolicyRules(policy.getId())); } } @@ -184,10 +184,7 @@ static class RolePolicyIterator implements Iterator { for (EntityReference role : listOrEmpty(roles)) { policyIterators.add( new PolicyIterator( - entityType, - entityName, - role.getName(), - RoleCache.getInstance().getRoleById(role.getId()).getPolicies())); + entityType, entityName, role.getName(), RoleCache.getRoleById(role.getId()).getPolicies())); } } @@ -240,7 +237,7 @@ static class UserPolicyIterator implements Iterator { // Finally, iterate over policies of teams that own the resource if (resourceOwner != null && resourceOwner.getType().equals(Entity.TEAM)) { - Team team = SubjectCache.getInstance().getTeam(resourceOwner.getId()); + Team team = SubjectCache.getTeam(resourceOwner.getId()); iterators.add(new TeamPolicyIterator(team.getId(), teamsVisited, true)); } } @@ -276,7 +273,7 @@ static class TeamPolicyIterator implements Iterator { /** Policy iterator for a team */ TeamPolicyIterator(UUID teamId, List teamsVisited, boolean skipRoles) { - Team team = SubjectCache.getInstance().getTeam(teamId); + Team team = SubjectCache.getTeam(teamId); // If a team is already visited (because user can belong to multiple teams // and a team can belong to multiple teams) then don't visit the roles/policies of that team diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/ThreadResourceContext.java b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/ThreadResourceContext.java index d4899c9bcda4..1fd255ccabff 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/ThreadResourceContext.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/security/policyevaluator/ThreadResourceContext.java @@ -22,7 +22,7 @@ public String getResource() { @Override public EntityReference getOwner() { - return SubjectCache.getInstance().getUser(createdBy).getEntityReference(); + return SubjectCache.getUser(createdBy).getEntityReference(); } @Override diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/socket/WebSocketManager.java b/openmetadata-service/src/main/java/org/openmetadata/service/socket/WebSocketManager.java index 748285679e93..4facb14dd796 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/socket/WebSocketManager.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/socket/WebSocketManager.java @@ -102,7 +102,7 @@ public void sendToOne(UUID receiver, String event, String message) { public void sendToOne(String username, String event, String message) { try { - UUID receiver = SubjectCache.getInstance().getSubjectContext(username).getUser().getId(); + UUID receiver = SubjectCache.getSubjectContext(username).getUser().getId(); if (activityFeedEndpoints.containsKey(receiver)) { activityFeedEndpoints.get(receiver).forEach((key, value) -> value.send(event, message)); } diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/EmailUtil.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/EmailUtil.java index 674167fcfeef..5b00d8d76b8f 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/EmailUtil.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/EmailUtil.java @@ -35,7 +35,6 @@ import org.openmetadata.schema.entity.feed.Thread; import org.openmetadata.schema.entity.teams.User; import org.openmetadata.schema.settings.SettingsType; -import org.openmetadata.schema.tests.type.TestCaseResult; import org.openmetadata.service.events.scheduled.template.DataInsightDescriptionAndOwnerTemplate; import org.openmetadata.service.events.scheduled.template.DataInsightTotalAssetTemplate; import org.openmetadata.service.events.subscription.email.EmailMessage; @@ -74,28 +73,23 @@ public class EmailUtil { private static final String CHANGE_EVENT_UPDATE = "Change Event Update from %s"; private static final String TASK_SUBJECT = "%s : Task Assignment Notification"; - private static final String TEST_SUBJECT = "%s : Test Result Notification"; public static final String INVITE_RANDOM_PWD = "invite-randompwd.ftl"; public static final String CHANGE_EVENT_TEMPLATE = "changeEvent.ftl"; public static final String INVITE_CREATE_PWD = "invite-createPassword.ftl"; public static final String TASK_NOTIFICATION_TEMPLATE = "taskAssignment.ftl"; - public static final String TEST_NOTIFICATION_TEMPLATE = "testResultStatus.ftl"; private static final String REPORT_SUBJECT = "%s: Data Insights Weekly - %s"; public static final String DATA_INSIGHT_REPORT_TEMPLATE = "dataInsightReport.ftl"; - private static EmailUtil instance; - private static SmtpSettings STORED_SMTP_SETTINGS; + private static SmtpSettings storedSmtpSettings; private static Mailer mailer; - private static Configuration templateConfiguration; + private static final Configuration templateConfiguration = new Configuration(VERSION_2_3_28); - private static final String EMAIL_IGNORE_MSG = "Email was not sent to %s as SMTP setting is not enabled"; + private static final String EMAIL_IGNORE_MSG = "Email was not sent to {} as SMTP setting is not enabled"; private EmailUtil() { try { - STORED_SMTP_SETTINGS = getSmtpSettings(); - mailer = createMailer(STORED_SMTP_SETTINGS); - templateConfiguration = new Configuration(VERSION_2_3_28); + getSmtpSettings(); LOG.info("Email Util cache is initialized"); } catch (Exception ex) { LOG.warn("[MAILER] Smtp Configurations are missing : Reason {} ", ex.getMessage(), ex); @@ -127,14 +121,7 @@ private static Mailer createMailer(SmtpSettings smtpServerSettings) { return null; } - public static EmailUtil getInstance() { - if (instance == null) { - instance = new EmailUtil(); - } - return instance; - } - - public void sendAccountStatus(User user, String action, String status) throws IOException, TemplateException { + public static void sendAccountStatus(User user, String action, String status) throws IOException, TemplateException { if (Boolean.TRUE.equals(getSmtpSettings().getEnableSmtpServer())) { Map templatePopulator = new HashMap<>(); templatePopulator.put(ENTITY, getEmailingEntity()); @@ -149,11 +136,12 @@ public void sendAccountStatus(User user, String action, String status) throws IO EMAIL_TEMPLATE_BASEPATH, ACCOUNT_STATUS_TEMPLATE_FILE); } else { - LOG.warn(String.format(EMAIL_IGNORE_MSG, user.getEmail())); + LOG.warn(EMAIL_IGNORE_MSG, user.getEmail()); } } - public void sendEmailVerification(String emailVerificationLink, User user) throws IOException, TemplateException { + public static void sendEmailVerification(String emailVerificationLink, User user) + throws IOException, TemplateException { if (Boolean.TRUE.equals(getSmtpSettings().getEnableSmtpServer())) { Map templatePopulator = new HashMap<>(); templatePopulator.put(ENTITY, getEmailingEntity()); @@ -168,11 +156,11 @@ public void sendEmailVerification(String emailVerificationLink, User user) throw EMAIL_TEMPLATE_BASEPATH, EMAIL_VERIFICATION_TEMPLATE_PATH); } else { - LOG.warn(String.format(EMAIL_IGNORE_MSG, user.getEmail())); + LOG.warn(EMAIL_IGNORE_MSG, user.getEmail()); } } - public void sendPasswordResetLink(String passwordResetLink, User user, String subject, String templateFilePath) + public static void sendPasswordResetLink(String passwordResetLink, User user, String subject, String templateFilePath) throws IOException, TemplateException { if (Boolean.TRUE.equals(getSmtpSettings().getEnableSmtpServer())) { Map templatePopulator = new HashMap<>(); @@ -184,11 +172,11 @@ public void sendPasswordResetLink(String passwordResetLink, User user, String su sendMail(subject, templatePopulator, user.getEmail(), EMAIL_TEMPLATE_BASEPATH, templateFilePath); } else { - LOG.warn(String.format(EMAIL_IGNORE_MSG, user.getEmail())); + LOG.warn(EMAIL_IGNORE_MSG, user.getEmail()); } } - public void sendTaskAssignmentNotificationToUser( + public static void sendTaskAssignmentNotificationToUser( String assigneeName, String email, String taskLink, Thread thread, String subject, String templateFilePath) throws IOException, TemplateException { if (Boolean.TRUE.equals(getSmtpSettings().getEnableSmtpServer())) { @@ -204,32 +192,11 @@ public void sendTaskAssignmentNotificationToUser( sendMail(subject, templatePopulator, email, EMAIL_TEMPLATE_BASEPATH, templateFilePath); } else { - LOG.warn(String.format(EMAIL_IGNORE_MSG, email)); - } - } - - public void sendTestResultEmailNotificationToUser( - String email, - String testResultLink, - String testCaseName, - TestCaseResult result, - String subject, - String templateFilePath) - throws IOException, TemplateException { - if (Boolean.TRUE.equals(getSmtpSettings().getEnableSmtpServer())) { - Map templatePopulator = new HashMap<>(); - templatePopulator.put("receiverName", email.split("@")[0]); - templatePopulator.put("testResultName", testCaseName); - templatePopulator.put("testResultDescription", result.getResult()); - templatePopulator.put("testResultStatus", result.getTestCaseStatus().toString()); - templatePopulator.put("testResultTimestamp", result.getTimestamp().toString()); - templatePopulator.put("testResultLink", testResultLink); - - sendMail(subject, templatePopulator, email, EMAIL_TEMPLATE_BASEPATH, templateFilePath); + LOG.warn(EMAIL_IGNORE_MSG, email); } } - public void sendMail( + public static void sendMail( String subject, Map model, String to, String baseTemplatePackage, String templatePath) throws IOException, TemplateException { if (Boolean.TRUE.equals(getSmtpSettings().getEnableSmtpServer())) { @@ -238,7 +205,7 @@ public void sendMail( emailBuilder.to(to); emailBuilder.from(getSmtpSettings().getSenderMail()); - templateConfiguration.setClassForTemplateLoading(getClass(), baseTemplatePackage); + templateConfiguration.setClassForTemplateLoading(EmailUtil.class, baseTemplatePackage); Template template = templateConfiguration.getTemplate(templatePath); // write the freemarker output to a StringWriter @@ -252,7 +219,7 @@ public void sendMail( } } - public void sendMailToMultiple( + public static void sendMailToMultiple( String subject, Map model, Set to, String baseTemplatePackage, String templatePath) throws IOException, TemplateException { if (Boolean.TRUE.equals(getSmtpSettings().getEnableSmtpServer())) { @@ -261,7 +228,7 @@ public void sendMailToMultiple( emailBuilder.toMultiple(to); emailBuilder.from(getSmtpSettings().getSenderMail()); - templateConfiguration.setClassForTemplateLoading(getClass(), baseTemplatePackage); + templateConfiguration.setClassForTemplateLoading(EmailUtil.class, baseTemplatePackage); Template template = templateConfiguration.getTemplate(templatePath); // write the freemarker output to a StringWriter @@ -273,13 +240,13 @@ public void sendMailToMultiple( } } - public void sendMail(Email email) { + public static void sendMail(Email email) { if (mailer != null && getSmtpSettings().getEnableSmtpServer()) { mailer.sendMail(email, true); } } - public String buildBaseUrl(URI uri) { + public static String buildBaseUrl(URI uri) { try { if (CommonUtil.nullOrEmpty(getSmtpSettings().getOpenMetadataUrl())) { return String.format("%s://%s", uri.getScheme(), uri.getHost()); @@ -295,24 +262,23 @@ public String buildBaseUrl(URI uri) { public static void sendInviteMailToAdmin(User user, String pwd) { if (Boolean.TRUE.equals(getSmtpSettings().getEnableSmtpServer())) { Map templatePopulator = new HashMap<>(); - templatePopulator.put(EmailUtil.ENTITY, EmailUtil.getInstance().getEmailingEntity()); - templatePopulator.put(EmailUtil.SUPPORT_URL, EmailUtil.getInstance().getSupportUrl()); + templatePopulator.put(EmailUtil.ENTITY, EmailUtil.getEmailingEntity()); + templatePopulator.put(EmailUtil.SUPPORT_URL, EmailUtil.getSupportUrl()); templatePopulator.put(EmailUtil.USERNAME, user.getName()); templatePopulator.put(EmailUtil.PASSWORD, pwd); - templatePopulator.put(EmailUtil.APPLICATION_LOGIN_LINK, EmailUtil.getInstance().getOMUrl()); + templatePopulator.put(EmailUtil.APPLICATION_LOGIN_LINK, EmailUtil.getOMUrl()); try { - EmailUtil.getInstance() - .sendMail( - EmailUtil.getInstance().getEmailInviteSubject(), - templatePopulator, - user.getEmail(), - EmailUtil.EMAIL_TEMPLATE_BASEPATH, - EmailUtil.INVITE_RANDOM_PWD); + EmailUtil.sendMail( + EmailUtil.getEmailInviteSubject(), + templatePopulator, + user.getEmail(), + EmailUtil.EMAIL_TEMPLATE_BASEPATH, + EmailUtil.INVITE_RANDOM_PWD); } catch (Exception ex) { LOG.error("Failed in sending Mail to user [{}]. Reason : {}", user.getEmail(), ex.getMessage()); } } else { - LOG.warn(String.format(EMAIL_IGNORE_MSG, user.getEmail())); + LOG.warn(EMAIL_IGNORE_MSG, user.getEmail()); } } @@ -329,22 +295,21 @@ public static void sendChangeEventMail(String receiverMail, EmailMessage emailMe } templatePopulator.put("changeMessage", buff.toString()); try { - EmailUtil.getInstance() - .sendMail( - EmailUtil.getInstance().getChangeEventTemplate(), - templatePopulator, - receiverMail, - EmailUtil.EMAIL_TEMPLATE_BASEPATH, - EmailUtil.CHANGE_EVENT_TEMPLATE); + EmailUtil.sendMail( + EmailUtil.getChangeEventTemplate(), + templatePopulator, + receiverMail, + EmailUtil.EMAIL_TEMPLATE_BASEPATH, + EmailUtil.CHANGE_EVENT_TEMPLATE); } catch (Exception ex) { LOG.error("Failed in sending Mail to user [{}]. Reason : {}", receiverMail, ex.getMessage()); } } else { - LOG.warn(String.format(EMAIL_IGNORE_MSG, receiverMail)); + LOG.warn(EMAIL_IGNORE_MSG, receiverMail); } } - public void sendDataInsightEmailNotificationToUser( + public static void sendDataInsightEmailNotificationToUser( Set emails, DataInsightTotalAssetTemplate totalAssetObj, DataInsightDescriptionAndOwnerTemplate descriptionObj, @@ -365,60 +330,55 @@ public void sendDataInsightEmailNotificationToUser( } } - public void testConnection() { + public static void testConnection() { mailer.testConnection(); } - private String getEmailVerificationSubject() { + private static String getEmailVerificationSubject() { return String.format(EMAIL_VERIFICATION_SUBJECT, getSmtpSettings().getEmailingEntity()); } - public String getPasswordResetSubject() { + public static String getPasswordResetSubject() { return String.format(PASSWORD_RESET_SUBJECT, getSmtpSettings().getEmailingEntity()); } - private String getAccountStatusChangeSubject() { + private static String getAccountStatusChangeSubject() { return String.format(ACCOUNT_STATUS_SUBJECT, getSmtpSettings().getEmailingEntity()); } - public String getEmailInviteSubject() { + public static String getEmailInviteSubject() { return String.format(INVITE_SUBJECT, getSmtpSettings().getEmailingEntity()); } - public String getChangeEventTemplate() { + public static String getChangeEventTemplate() { return String.format(CHANGE_EVENT_UPDATE, getSmtpSettings().getEmailingEntity()); } - public String getTaskAssignmentSubject() { + public static String getTaskAssignmentSubject() { return String.format(TASK_SUBJECT, getSmtpSettings().getEmailingEntity()); } - public String getTestResultSubject() { - return String.format(TEST_SUBJECT, getSmtpSettings().getEmailingEntity()); - } - - public String getDataInsightReportSubject() { + public static String getDataInsightReportSubject() { return String.format( REPORT_SUBJECT, getSmtpSettings().getEmailingEntity(), new SimpleDateFormat("dd-MM-yy").format(new Date())); } - public String getEmailingEntity() { + public static String getEmailingEntity() { return getSmtpSettings().getEmailingEntity(); } - public String getSupportUrl() { + public static String getSupportUrl() { return getSmtpSettings().getSupportUrl(); } - public String getOMUrl() { + public static String getOMUrl() { return getSmtpSettings().getOpenMetadataUrl(); } private static SmtpSettings getSmtpSettings() { - SmtpSettings emailConfig = - SettingsCache.getInstance().getSetting(SettingsType.EMAIL_CONFIGURATION, SmtpSettings.class); - if (!emailConfig.equals(STORED_SMTP_SETTINGS)) { - STORED_SMTP_SETTINGS = emailConfig; + SmtpSettings emailConfig = SettingsCache.getSetting(SettingsType.EMAIL_CONFIGURATION, SmtpSettings.class); + if (!emailConfig.equals(storedSmtpSettings)) { + storedSmtpSettings = emailConfig; mailer = createMailer(emailConfig); } return emailConfig; diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/EntityUtil.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/EntityUtil.java index 47d7af9c08f7..9f95cbaa3a48 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/EntityUtil.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/EntityUtil.java @@ -165,7 +165,6 @@ public static List getEntityReferences(List refs = new ArrayList<>(); for (EntityRelationshipRecord ref : list) { - System.out.println("XXX getting entityRef for " + ref.getType() + " " + ref.getId()); refs.add(Entity.getEntityReferenceById(ref.getType(), ref.getId(), ALL)); } refs.sort(compareEntityReference); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/NotificationHandler.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/NotificationHandler.java index a06ddfbb89cd..0a30fe5b5a73 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/NotificationHandler.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/NotificationHandler.java @@ -159,15 +159,13 @@ private void handleEmailNotifications(HashSet userList, Thread thread) { id -> { try { User user = repository.get(null, id, repository.getFields("name,email,href")); - EmailUtil.getInstance() - .sendTaskAssignmentNotificationToUser( - user.getName(), - user.getEmail(), - String.format( - "%s/users/%s/tasks", EmailUtil.getInstance().buildBaseUrl(urlInstance), user.getName()), - thread, - EmailUtil.getInstance().getTaskAssignmentSubject(), - EmailUtil.TASK_NOTIFICATION_TEMPLATE); + EmailUtil.sendTaskAssignmentNotificationToUser( + user.getName(), + user.getEmail(), + String.format("%s/users/%s/tasks", EmailUtil.buildBaseUrl(urlInstance), user.getName()), + thread, + EmailUtil.getTaskAssignmentSubject(), + EmailUtil.TASK_NOTIFICATION_TEMPLATE); } catch (IOException ex) { LOG.error("Task Email Notification Failed :", ex); } catch (TemplateException ex) { diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java index 3c9890ef50bd..e752d5f9b913 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/SubscriptionUtil.java @@ -113,19 +113,19 @@ public static Set getOwnerOrFollowers( if (type == CreateEventSubscription.SubscriptionType.EMAIL || type == CreateEventSubscription.SubscriptionType.DATA_INSIGHT) { if (USER.equals(owner.getType())) { - User user = SubjectCache.getInstance().getSubjectContext(owner.getId()).getUser(); + User user = SubjectCache.getSubjectContext(owner.getId()).getUser(); data.add(user.getEmail()); } else { - Team team = SubjectCache.getInstance().getTeam(owner.getId()); + Team team = SubjectCache.getTeam(owner.getId()); data.add(team.getEmail()); } } else { Profile profile = null; if (USER.equals(owner.getType())) { - User user = SubjectCache.getInstance().getSubjectContext(owner.getId()).getUser(); + User user = SubjectCache.getSubjectContext(owner.getId()).getUser(); profile = user.getProfile(); } else if (TEAM.equals(owner.getType())) { - Team team = SubjectCache.getInstance().getTeam(owner.getId()); + Team team = SubjectCache.getTeam(owner.getId()); profile = team.getProfile(); } data.addAll(getWebhookUrlsFromProfile(profile, owner.getId(), owner.getType(), type)); diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/UserUtil.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/UserUtil.java index 8a53a88a88f1..042f4be042a7 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/UserUtil.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/UserUtil.java @@ -26,7 +26,6 @@ import java.util.Set; import java.util.UUID; import lombok.extern.slf4j.Slf4j; -import org.openmetadata.schema.api.configuration.pipelineServiceClient.PipelineServiceClientConfiguration; import org.openmetadata.schema.auth.BasicAuthMechanism; import org.openmetadata.schema.auth.JWTAuthMechanism; import org.openmetadata.schema.auth.JWTTokenExpiry; @@ -37,7 +36,6 @@ import org.openmetadata.schema.type.EntityReference; import org.openmetadata.schema.utils.EntityInterfaceUtil; import org.openmetadata.service.Entity; -import org.openmetadata.service.OpenMetadataApplicationConfig; import org.openmetadata.service.exception.EntityNotFoundException; import org.openmetadata.service.jdbi3.EntityRepository; import org.openmetadata.service.jdbi3.UserRepository; @@ -73,7 +71,7 @@ private static void createOrUpdateUser(AuthProvider authProvider, String usernam // Fetch Original User, is available User originalUser = userRepository.getByName(null, EntityInterfaceUtil.quoteName(username), new Fields(fieldList)); - if (!originalUser.getIsBot() && !originalUser.getIsAdmin()) { + if (Boolean.FALSE.equals(originalUser.getIsBot()) && Boolean.FALSE.equals(originalUser.getIsAdmin())) { updatedUser = originalUser; // Update Auth Mechanism if not present, and send mail to the user @@ -115,7 +113,7 @@ private static void createOrUpdateUser(AuthProvider authProvider, String usernam private static String getPassword() { try { - EmailUtil.getInstance().testConnection(); + EmailUtil.testConnection(); return PasswordUtil.generateRandomPassword(); } catch (Exception ex) { LOG.info("Password set to Default."); @@ -176,10 +174,8 @@ public static User user(String name, String domain, String updatedBy) { * * */ - public static User addOrUpdateBotUser(User user, OpenMetadataApplicationConfig openMetadataApplicationConfig) { + public static User addOrUpdateBotUser(User user) { User originalUser = retrieveWithAuthMechanism(user); - PipelineServiceClientConfiguration pipelineServiceClientConfiguration = - openMetadataApplicationConfig.getPipelineServiceClientConfiguration(); AuthenticationMechanism authMechanism = originalUser != null ? originalUser.getAuthenticationMechanism() : null; // the user did not have an auth mechanism and auth config is present if (authMechanism == null) { diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/util/ValidatorUtil.java b/openmetadata-service/src/main/java/org/openmetadata/service/util/ValidatorUtil.java index fbb75bbb582b..ffb79a101177 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/util/ValidatorUtil.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/util/ValidatorUtil.java @@ -5,10 +5,11 @@ import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validation; +import javax.validation.Validator; import javax.validation.ValidatorFactory; public class ValidatorUtil { - public static final javax.validation.Validator VALIDATOR; + public static final Validator VALIDATOR; static { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); @@ -21,11 +22,9 @@ private ValidatorUtil() { public static String validate(T entity) { Set> violations = VALIDATOR.validate(entity); - String ret = - violations.isEmpty() - ? null - : Arrays.toString( - violations.stream().map(v -> String.format("%s %s", v.getPropertyPath(), v.getMessage())).toArray()); - return ret; + return violations.isEmpty() + ? null + : Arrays.toString( + violations.stream().map(v -> String.format("%s %s", v.getPropertyPath(), v.getMessage())).toArray()); } } diff --git a/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java b/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java index 0123f4a6665e..472ddfcf16e9 100644 --- a/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java +++ b/openmetadata-service/src/test/java/org/openmetadata/service/resources/EntityResourceTest.java @@ -567,7 +567,6 @@ void get_entityListWithPagination_200(TestInfo test) throws IOException { Random rand = new Random(); int maxEntities = rand.nextInt(16) + 5; - System.out.println("XXX creating entities " + maxEntities); List createdUUIDs = new ArrayList<>(); for (int i = 0; i < maxEntities; i++) { createdUUIDs.add(createEntity(createRequest(test, i + 1), ADMIN_AUTH_HEADERS).getId()); @@ -580,8 +579,6 @@ void get_entityListWithPagination_200(TestInfo test) throws IOException { // Test listing entities that include deleted, non-deleted, and all the entities Random random = new Random(); for (Include include : List.of(Include.NON_DELETED, Include.ALL, Include.DELETED)) { - System.out.println("XXX supportSoftDelete " + supportsSoftDelete); - System.out.println("XXX Include " + include); if (!supportsSoftDelete && include.equals(Include.DELETED)) { continue; } @@ -592,7 +589,6 @@ void get_entityListWithPagination_200(TestInfo test) throws IOException { ResultList allEntities = listEntities(queryParams, 1000000, null, null, ADMIN_AUTH_HEADERS); int totalRecords = allEntities.getData().size(); printEntities(allEntities); - System.out.println("XXX totalRecords " + totalRecords); // List entity with "limit" set from 1 to maxEntities size with random jumps (to reduce the test time) // Each time compare the returned list with allTables list to make sure right results are returned @@ -605,8 +601,8 @@ void get_entityListWithPagination_200(TestInfo test) throws IOException { ResultList backwardPage; boolean foundDeleted = false; do { // For each limit (or page size) - forward scroll till the end - LOG.info( - "XXX Limit {} forward pageCount {} indexInAllTables {} totalRecords {} afterCursor {}", + LOG.debug( + "Limit {} forward pageCount {} indexInAllTables {} totalRecords {} afterCursor {}", limit, pageCount, indexInAllTables, @@ -2494,8 +2490,8 @@ public ResultList listEntities( } private void printEntities(ResultList list) { - list.getData().forEach(e -> LOG.info("XXX {} {}", entityClass, e.getFullyQualifiedName())); - LOG.info("XXX before {} after {} ", list.getPaging().getBefore(), list.getPaging().getAfter()); + list.getData().forEach(e -> LOG.debug("{} {}", entityClass, e.getFullyQualifiedName())); + LOG.debug("before {} after {} ", list.getPaging().getBefore(), list.getPaging().getAfter()); } public void assertEntityDeleted(T entity, boolean hardDelete) { diff --git a/openmetadata-service/src/test/java/org/openmetadata/service/security/policyevaluator/RuleEvaluatorTest.java b/openmetadata-service/src/test/java/org/openmetadata/service/security/policyevaluator/RuleEvaluatorTest.java index ac579f65783f..36f076335f8f 100644 --- a/openmetadata-service/src/test/java/org/openmetadata/service/security/policyevaluator/RuleEvaluatorTest.java +++ b/openmetadata-service/src/test/java/org/openmetadata/service/security/policyevaluator/RuleEvaluatorTest.java @@ -44,8 +44,6 @@ public static void setup() { Entity.registerEntity(User.class, Entity.USER, mock(UserRepository.class), null); Entity.registerEntity(Team.class, Entity.TEAM, mock(TeamRepository.class), null); Entity.registerEntity(Role.class, Entity.ROLE, mock(RoleRepository.class), null); - SubjectCache.initialize(); - RoleCache.initialize(); TableRepository tableRepository = mock(TableRepository.class); Mockito.when(tableRepository.getAllTags(any())) @@ -255,10 +253,10 @@ private Team createTeam(String teamName, String parentName) { Team team = new Team().withName(teamName).withId(teamId); if (parentName != null) { UUID parentId = UUID.nameUUIDFromBytes(parentName.getBytes(StandardCharsets.UTF_8)); - Team parentTeam = SubjectCache.getInstance().getTeam(parentId); + Team parentTeam = SubjectCache.getTeam(parentId); team.setParents(listOf(parentTeam.getEntityReference())); } - SubjectCache.teamCacheWithId.put(team.getId(), team); + SubjectCache.TEAM_CACHE_WITH_ID.put(team.getId(), team); return team; } @@ -268,7 +266,7 @@ private Team createTeamWithRole(String teamName, String parentName) { team.setDefaultRoles(listOf(role.getEntityReference())); team.setInheritedRoles(new ArrayList<>()); for (EntityReference parent : listOrEmpty(team.getParents())) { - Team parentTeam = SubjectCache.getInstance().getTeam(parent.getId()); + Team parentTeam = SubjectCache.getTeam(parent.getId()); team.getInheritedRoles().addAll(listOrEmpty(parentTeam.getDefaultRoles())); team.getInheritedRoles().addAll(listOrEmpty(parentTeam.getInheritedRoles())); } @@ -278,7 +276,7 @@ private Team createTeamWithRole(String teamName, String parentName) { private Role createRole(String roleName) { UUID roleId = UUID.nameUUIDFromBytes(roleName.getBytes(StandardCharsets.UTF_8)); Role role = new Role().withName(roleName).withId(roleId); - RoleCache.roleCacheWithId.put(role.getId(), role); + RoleCache.CACHE_WITH_ID.put(role.getId(), role); return role; } diff --git a/openmetadata-service/src/test/java/org/openmetadata/service/security/policyevaluator/SubjectContextTest.java b/openmetadata-service/src/test/java/org/openmetadata/service/security/policyevaluator/SubjectContextTest.java index f645c2578e7c..d5b8ec6af24e 100644 --- a/openmetadata-service/src/test/java/org/openmetadata/service/security/policyevaluator/SubjectContextTest.java +++ b/openmetadata-service/src/test/java/org/openmetadata/service/security/policyevaluator/SubjectContextTest.java @@ -70,9 +70,6 @@ public static void setup() { Entity.registerEntity(Team.class, Entity.TEAM, mock(TeamRepository.class), null); Entity.registerEntity(Policy.class, Entity.POLICY, mock(PolicyRepository.class), null); Entity.registerEntity(Role.class, Entity.ROLE, mock(RoleRepository.class), null); - PolicyCache.initialize(); - RoleCache.initialize(); - SubjectCache.initialize(); // Create team hierarchy: // team1 @@ -111,7 +108,7 @@ public static void setup() { userRoles = getRoles("user"); List userRolesRef = toEntityReferences(userRoles); user = new User().withName("user").withRoles(userRolesRef).withTeams(List.of(team111.getEntityReference())); - SubjectCache.userCache.put("user", new SubjectContext(user)); + SubjectCache.USER_CACHE.put("user", new SubjectContext(user)); } @AfterAll @@ -124,7 +121,7 @@ public static void tearDown() { @Test void testPolicyIterator() { // Check iteration order of the policies without resourceOwner - SubjectContext subjectContext = SubjectCache.getInstance().getSubjectContext(user.getName()); + SubjectContext subjectContext = SubjectCache.getSubjectContext(user.getName()); Iterator policyContextIterator = subjectContext.getPolicies(null); List expectedUserPolicyOrder = new ArrayList<>(); expectedUserPolicyOrder.addAll(getPolicyListFromRoles(userRoles)); // First polices associated with user roles @@ -137,7 +134,7 @@ void testPolicyIterator() { assertPolicyIterator(expectedUserPolicyOrder, policyContextIterator); // Check iteration order of policies with team13 as the resource owner - subjectContext = SubjectCache.getInstance().getSubjectContext(user.getName()); + subjectContext = SubjectCache.getSubjectContext(user.getName()); policyContextIterator = subjectContext.getPolicies(team13.getEntityReference()); List expectedUserAndTeam13PolicyOrder = new ArrayList<>(); expectedUserAndTeam13PolicyOrder.addAll(expectedUserPolicyOrder); @@ -145,7 +142,7 @@ void testPolicyIterator() { assertPolicyIterator(expectedUserAndTeam13PolicyOrder, policyContextIterator); // Check iteration order of policies with team131 as the resource owner - subjectContext = SubjectCache.getInstance().getSubjectContext(user.getName()); + subjectContext = SubjectCache.getSubjectContext(user.getName()); policyContextIterator = subjectContext.getPolicies(team131.getEntityReference()); // Roles & policies are inherited from resource owner team131 List expectedUserAndTeam131PolicyOrder = new ArrayList<>(); @@ -157,7 +154,7 @@ void testPolicyIterator() { @Test void testUserInHierarchy() { - SubjectContext subjectContext = SubjectCache.getInstance().getSubjectContext(user.getName()); + SubjectContext subjectContext = SubjectCache.getSubjectContext(user.getName()); // // Now test given user is part of team hierarchy // @@ -171,7 +168,7 @@ void testUserInHierarchy() { @Test void testResourceIsTeamAsset() { - SubjectContext subjectContext = SubjectCache.getInstance().getSubjectContext(user.getName()); + SubjectContext subjectContext = SubjectCache.getSubjectContext(user.getName()); // // Given entity owner "user", ensure isTeamAsset is true for teams 111, 11, 12, 1 and not for 13 @@ -193,7 +190,6 @@ void testResourceIsTeamAsset() { assertFalse(subjectContext.isTeamAsset("team13", teamOwner)); } - @Test private static List getRoles(String prefix) { // Create roles with 3 policies each and each policy with 3 rules List roles = new ArrayList<>(3); @@ -201,7 +197,7 @@ private static List getRoles(String prefix) { String name = prefix + "_role_" + i; List policies = toEntityReferences(getPolicies(name)); Role role = new Role().withName(name).withId(UUID.randomUUID()).withPolicies(policies); - RoleCache.roleCacheWithId.put(role.getId(), role); + RoleCache.CACHE_WITH_ID.put(role.getId(), role); roles.add(role); } return roles; @@ -213,7 +209,7 @@ private static List getPolicies(String prefix) { String name = prefix + "_policy_" + i; Policy policy = new Policy().withName(name).withId(UUID.randomUUID()).withRules(getRules(name)); policies.add(policy); - PolicyCache.policyCache.put(policy.getId(), PolicyCache.getInstance().getRules(policy)); + PolicyCache.CACHE.put(policy.getId(), PolicyCache.getRules(policy)); } return policies; } @@ -268,7 +264,7 @@ private static Team createTeam(String name, List roles, List polic .withDefaultRoles(toEntityReferences(roles)) .withPolicies(toEntityReferences(policies)) .withParents(parentList); - SubjectCache.teamCacheWithId.put(team.getId(), team); + SubjectCache.TEAM_CACHE_WITH_ID.put(team.getId(), team); return team; }