io.camunda.operate.webapp.elasticsearch.ElasticsearchSessionRepository Maven / Gradle / Ivy
The newest version!
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
* one or more contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright ownership.
* Licensed under the Camunda License 1.0. You may not use this file
* except in compliance with the Camunda License 1.0.
*/
package io.camunda.operate.webapp.elasticsearch;
import static io.camunda.operate.schema.indices.OperateWebSessionIndex.ATTRIBUTES;
import static io.camunda.operate.schema.indices.OperateWebSessionIndex.CREATION_TIME;
import static io.camunda.operate.schema.indices.OperateWebSessionIndex.ID;
import static io.camunda.operate.schema.indices.OperateWebSessionIndex.LAST_ACCESSED_TIME;
import static io.camunda.operate.schema.indices.OperateWebSessionIndex.MAX_INACTIVE_INTERVAL_IN_SECONDS;
import io.camunda.operate.conditions.ElasticsearchCondition;
import io.camunda.operate.schema.indices.OperateWebSessionIndex;
import io.camunda.operate.store.elasticsearch.RetryElasticsearchClient;
import io.camunda.operate.webapp.security.OperateSession;
import io.camunda.operate.webapp.security.SessionRepository;
import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.elasticsearch.action.search.SearchRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Conditional;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.stereotype.Component;
@Conditional(ElasticsearchCondition.class)
@Component
public class ElasticsearchSessionRepository implements SessionRepository {
private static final Logger LOGGER =
LoggerFactory.getLogger(ElasticsearchSessionRepository.class);
private final RetryElasticsearchClient retryElasticsearchClient;
private final GenericConversionService conversionService;
private final OperateWebSessionIndex operateWebSessionIndex;
private final HttpServletRequest request;
public ElasticsearchSessionRepository(
final RetryElasticsearchClient retryElasticsearchClient,
final GenericConversionService conversionService,
final OperateWebSessionIndex operateWebSessionIndex,
final HttpServletRequest request) {
this.retryElasticsearchClient = retryElasticsearchClient;
this.conversionService = conversionService;
this.operateWebSessionIndex = operateWebSessionIndex;
this.request = request;
}
@PostConstruct
private void setUp() {
setupConverter();
}
private void setupConverter() {
conversionService.addConverter(Object.class, byte[].class, new SerializingConverter());
conversionService.addConverter(byte[].class, Object.class, new DeserializingConverter());
}
@Override
public List getExpiredSessionIds() {
final SearchRequest searchRequest =
new SearchRequest(operateWebSessionIndex.getFullQualifiedName());
final List result = new ArrayList<>();
retryElasticsearchClient.doWithEachSearchResult(
searchRequest,
sh -> {
final Map document = sh.getSourceAsMap();
final Optional maybeSession = documentToSession(document);
if (maybeSession.isPresent()) {
final OperateSession session = maybeSession.get();
LOGGER.debug("Check if session {} is expired: {}", session, session.isExpired());
if (session.isExpired()) {
result.add(session.getId());
}
} else {
// need to delete entry in Elasticsearch in case of failing restore session
result.add(getSessionIdFrom(document));
}
});
return result;
}
@Override
public void save(final OperateSession session) {
retryElasticsearchClient.createOrUpdateDocument(
operateWebSessionIndex.getFullQualifiedName(), session.getId(), sessionToDocument(session));
}
@Override
public Optional findById(final String id) {
try {
final Optional