Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues/1972: Fix failing tests in NpmArtifactControllerTestIT #1990

Draft
wants to merge 3 commits into
base: issues/1649-steve
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ default <S2> Traversal<S, Vertex> saveV(Object uuid,
__.<Vertex>unfold()
.sideEffect(entity::applyUnfold)
.sideEffect(EntityTraversalUtils::fetched))
.map(unfoldTraversal);
.sideEffect(t -> EntityTraversalUtils.debug("saved", t))
.map(unfoldTraversal)
.sideEffect(t -> EntityTraversalUtils.debug("unfolded", t));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void deleteById(String id)
start(this::g).findById(id, label)
.flatMap(adapter().cascade())
.dedup()
.debug("Delete")
.info("Delete")
.drop()
.iterate();
session.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ public UserDetails loadUserByUsername(String username)
private Optional<User> loadUserDetails(String username)
{
Optional<User> optionalUser = Optional.ofNullable(strongboxUserManager.findByUsername(username)).filter(this::isInternalOrValidExternalUser);
if (optionalUser.isPresent()) {
if (optionalUser.isPresent())
{
User user = optionalUser.get();
logger.debug("Found valid user details: username-[{}], sourceId=[{}]", user.getUsername(), user.getSourceId());

return optionalUser;
}

Expand All @@ -120,6 +124,7 @@ private Optional<User> loadUserDetails(String username)

protected Optional<User> loadExternalUserDetails(String username)
{
logger.info("Load external user details: username=[{}]", username);
for (Entry<String, UserDetailsService> userDetailsServiceEntry : userProviderMap.entrySet())
{
String sourceId = userDetailsServiceEntry.getKey();
Expand All @@ -134,7 +139,8 @@ protected Optional<User> loadExternalUserDetails(String username)
{
continue;
}


logger.debug("Fetched external user details: username=[{}], sourceId=[{}]", externalUser.getUsername(), sourceId);
try
{
return Optional.of(strongboxUserManager.cacheExternalUserDetails(sourceId, externalUser));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import org.apache.commons.lang3.StringUtils;
import org.janusgraph.core.SchemaViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
Expand All @@ -26,28 +28,68 @@
@Transactional
public class DatabaseExternalUsersCacheManager extends DatabaseUserService implements StrongboxExternalUsersCacheManager
{

private static final Logger logger = LoggerFactory.getLogger(DatabaseExternalUsersCacheManager.class);


@Override
public UserEntity findByUsername(String username)
{
UserEntity result = super.findByUsername(username);
if (result != null)
{
logger.debug("User found in DB cache: username=[{}], sourceId=[{}], id=[{}], uuid=[{}]",
result.getUsername(),
result.getSourceId(),
result.getNativeId(),
result.getUuid());
}
else
{
logger.debug("User not found in DB cache: username=[{}]", username);
}

return result;
}

@Override
@CacheEvict(cacheNames = CacheName.User.AUTHENTICATIONS, key = "#p1.username")
public User cacheExternalUserDetails(String sourceId,
UserDetails springUser)
{
User user = springUser instanceof StrongboxUserDetails ? ((StrongboxUserDetails) springUser).getUser()
: new UserData(springUser);
User user;
if (springUser instanceof StrongboxUserDetails)
{
user = ((StrongboxUserDetails) springUser).getUser();
}
else
{
user = new UserData(springUser);
}
String username = user.getUsername();
logger.info("Cache external user: username=[{}], id=[{}], uuid=[{}], sourceId=[{}], UserDetails=[{}]",
username,
user.getNativeId(),
user.getUuid(),
sourceId,
springUser.getClass().getSimpleName());

Optional<UserEntity> optionalUser = Optional.ofNullable(findByUsername(user.getUsername()));
Optional<UserEntity> oldUser = Optional.ofNullable(findByUsername(user.getUsername()));
try
{
// If found user was from another source then remove before save
if (optionalUser.map(User::getSourceId).map(sourceId::equals).filter(Boolean.FALSE::equals).isPresent())
Optional<String> oldSource = oldUser.map(User::getSourceId);
if (oldSource.map(sourceId::equals).filter(Boolean.FALSE::equals).isPresent())
{
deleteByUsername(optionalUser.map(u -> user.getUuid()).get());
optionalUser = Optional.empty();
logger.debug("Invalidate user from another source: username=[{}], oldSource=[{}], newSource=[{}]",
username,
oldSource.get(),
sourceId);
deleteByUsername(oldUser.map(u -> user.getUuid()).get());
oldUser = Optional.empty();
}

UserEntity userEntry = optionalUser.orElseGet(() -> new UserEntity(username));
UserEntity userEntry = oldUser.orElseGet(() -> new UserEntity(username));

if (!StringUtils.isBlank(user.getPassword()))
{
Expand All @@ -59,7 +101,15 @@ public User cacheExternalUserDetails(String sourceId,
userEntry.setLastUpdated(LocalDateTimeInstance.now());
userEntry.setSourceId(sourceId);

return userRepository.save(userEntry);
UserEntity result = userRepository.save(userEntry);
logger.debug("Cached user: username=[{}], id=[{}], uuid=[{}], sourceId=[{}], lastUpdated=[{}]",
result.getUsername(),
result.getNativeId(),
result.getUuid(),
result.getSourceId(),
result.getLastUpdated());

return result;
}
catch (SchemaViolationException e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.carlspring.strongbox.users.userdetails.StrongboxExternalUsersCacheManager;
import org.carlspring.strongbox.users.userdetails.StrongboxUserDetails;
import org.carlspring.strongbox.util.LocalDateTimeInstance;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ public UnfoldEntityTraversal<Vertex, Vertex> unfold(User entity)

userRoleTraversal = userRoleTraversal.addE(Edges.USER_HAS_SECURITY_ROLES)
.from(__.<Vertex, Vertex>select(storedUserId).unfold())
.inV();

userRoleTraversal = userRoleTraversal.inE(Edges.USER_HAS_SECURITY_ROLES).outV();

.outV();
}

unfoldTraversal = unfoldTraversal.map(unfoldUser(entity))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
public class DatabaseUserService implements UserService
{

private static final Logger logger = LoggerFactory.getLogger(DatabaseUserService.class);

@Inject
private SecurityTokenProvider tokenProvider;

Expand Down Expand Up @@ -151,6 +153,7 @@ public User save(User user)

public void expireUser(String username, boolean clearSourceId)
{
logger.debug("Expire user: username=[{}]", username);
UserEntity externalUserEntry = findByUsername(username);
externalUserEntry.setLastUpdated(LocalDateTime.ofInstant(Instant.ofEpochMilli(0), ZoneId.systemDefault()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ public void testResolveArtifactViaGroupWithProxy(@Remote(url = REMOTE_URL)

@ExtendWith(RepositoryManagementTestExecutionListener.class)
@Test
@Disabled("See https://github.com/strongbox/strongbox/issues/1972")
public void testViewArtifactViaProxy(@Remote(url = REMOTE_URL)
@NpmRepository(repositoryId = REPOSITORY_PROXY)
Repository proxyRepository)
Expand Down Expand Up @@ -135,7 +134,6 @@ public void testViewArtifactViaProxy(@Remote(url = REMOTE_URL)

@ExtendWith(RepositoryManagementTestExecutionListener.class)
@Test
@Disabled("See https://github.com/strongbox/strongbox/issues/1972")
public void testSearchArtifactViaProxy(@Remote(url = REMOTE_URL)
@NpmRepository(repositoryId = REPOSITORY_PROXY)
Repository proxyRepository)
Expand Down