Skip to content

Commit

Permalink
Merge pull request #8943 from mcalmer/adapt-channel-change
Browse files Browse the repository at this point in the history
Adapt channel change
  • Loading branch information
mcalmer authored Nov 6, 2024
2 parents da11f89 + f7e0ca4 commit 9374284
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 483 deletions.
40 changes: 25 additions & 15 deletions java/code/src/com/redhat/rhn/domain/channel/AccessTokenFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.criterion.Restrictions;
import org.jose4j.lang.JoseException;

import java.time.Duration;
Expand Down Expand Up @@ -58,21 +57,32 @@ public class AccessTokenFactory extends HibernateFactory {
* @return optional of AccessToken
*/
public static Optional<AccessToken> lookupById(long id) {
return Optional.ofNullable(
(AccessToken)HibernateFactory.getSession()
.createCriteria(AccessToken.class)
.add(Restrictions.eq("id", id))
.uniqueResult()
);
return getSession()
.createQuery("FROM AccessToken WHERE id = :id", AccessToken.class)
.setParameter("id", id)
.uniqueResultOptional();
}

/**
* Queries all AccessTokens
* @return list of AccessTokens
*/
public static List<AccessToken> all() {
return (List<AccessToken>) HibernateFactory.getSession()
.createCriteria(AccessToken.class)
return getSession()
.createQuery("FROM AccessToken", AccessToken.class)
.list();
}


/**
* Queries all AccessTokens for a specific minion
* @param minion the minion
* @return list of AccessTokens
*/
public static List<AccessToken> listByMinion(MinionServer minion) {
return getSession()
.createQuery("FROM AccessToken WHERE minion = :minion", AccessToken.class)
.setParameter("minion", minion)
.list();
}

Expand All @@ -82,12 +92,10 @@ public static List<AccessToken> all() {
* @return optional of AccessToken
*/
public static Optional<AccessToken> lookupByToken(String token) {
return Optional.ofNullable(
(AccessToken)HibernateFactory.getSession()
.createCriteria(AccessToken.class)
.add(Restrictions.eq("token", token))
.uniqueResult()
);
return getSession()
.createQuery("FROM AccessToken WHERE token = :token", AccessToken.class)
.setParameter("token", token)
.uniqueResultOptional();
}

/**
Expand Down Expand Up @@ -210,6 +218,8 @@ public static boolean refreshTokens(MinionServer minion, Collection<AccessToken>
minion.getAccessTokens().add(toActivate);
});

LOG.debug("Token refresh finished. Got Unneeded {} Got Updated {} Got New {}",
!unneededTokens.isEmpty(), !update.isEmpty(), !newTokens.isEmpty());
return !unneededTokens.isEmpty() || !update.isEmpty() || !newTokens.isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
Expand Down Expand Up @@ -588,6 +593,7 @@ public void createOrUpdateSaltFS() {
Path copyTo = fullDir.resolve(copyFrom.getFileName());
if (!Files.exists(copyTo) || Files.isSymbolicLink(copyTo)) {
Files.copy(copyFrom, copyTo, StandardCopyOption.REPLACE_EXISTING);
modifyOwner(copyTo, true);
}
}
}
Expand All @@ -596,6 +602,28 @@ public void createOrUpdateSaltFS() {
}
}

/**
* Change the owner to user tomcat when the current owner is root
* @param pathIn the path to change
* @param setOwner set to true if the owner should be changed, otherwise false
* @throws IOException
*/
protected void modifyOwner(Path pathIn, boolean setOwner) throws IOException {
if (!setOwner) {
return;
}
UserPrincipal tomcatUser = null;
UserPrincipal rootUser = null;
FileSystem fileSystem = FileSystems.getDefault();
UserPrincipalLookupService service = fileSystem.getUserPrincipalLookupService();
tomcatUser = service.lookupPrincipalByName("tomcat");
rootUser = service.lookupPrincipalByName("root");

if (Files.getOwner(pathIn, LinkOption.NOFOLLOW_LINKS).equals(rootUser)) {
Files.setOwner(pathIn, tomcatUser);
}
}

/**
* Remove the Salt Filesystem
*/
Expand Down
20 changes: 20 additions & 0 deletions java/code/src/com/redhat/rhn/domain/server/MinionServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package com.redhat.rhn.domain.server;

import com.redhat.rhn.domain.channel.AccessToken;
import com.redhat.rhn.domain.channel.AccessTokenFactory;
import com.redhat.rhn.domain.channel.Channel;
import com.redhat.rhn.domain.config.ConfigChannel;
import com.redhat.rhn.domain.user.User;
import com.redhat.rhn.manager.configuration.SaltConfigSubscriptionService;
Expand All @@ -24,11 +26,13 @@
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.time.Instant;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
* MinionServer
Expand Down Expand Up @@ -221,6 +225,22 @@ public boolean equals(Object other) {
.isEquals();
}


/**
* @return Return true when all assigned software channels have valid access tokens.
*/
public boolean hasValidTokensForAllChannels() {

Set<Channel> tokenChannels = AccessTokenFactory.listByMinion(this)
.stream()
.filter(AccessToken::getValid)
.filter(t -> t.getExpiration().toInstant().isAfter(Instant.now()))
.flatMap(t -> t.getChannels().stream())
.collect(Collectors.toSet());

return tokenChannels.containsAll(getChannels()) && getChannels().containsAll(tokenChannels);
}

/**
* Get channel access tokens assigned to this minion.
* @return set of access tokens
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
*/
public class MinionServerFactory extends HibernateFactory {

private static Logger log = LogManager.getLogger(MinionServerFactory.class);
private static final Logger LOG = LogManager.getLogger(MinionServerFactory.class);

/**
* Lookup all Servers that belong to an org
Expand All @@ -75,13 +75,13 @@ public static List<MinionServer> lookupByOrg(Long orgId) {
*/
public static Stream<MinionServer> lookupVisibleToUser(User user) {
return user.getServers().stream().flatMap(
s -> s.asMinionServer().map(Stream::of).orElseGet(Stream::empty)
s -> s.asMinionServer().stream()
);
}

@Override
protected Logger getLogger() {
return log;
return LOG;
}

/**
Expand Down Expand Up @@ -153,7 +153,7 @@ public static Optional<MinionServer> lookupById(Long id) {
*/
public static Stream<MinionServer> lookupByIds(List<Long> ids) {
return ServerFactory.lookupByIds(ids).stream().flatMap(server ->
server.asMinionServer().map(Stream::of).orElseGet(Stream::empty)
server.asMinionServer().stream()
);
}

Expand Down Expand Up @@ -324,7 +324,7 @@ public static List<MinionSummary> findByosServers(Action action) {
List<MinionSummary> allMinions = MinionServerFactory.findQueuedMinionSummaries(action.getId());
return allMinions.stream().filter(
minionSummary -> MinionServerFactory.findByMinionId(minionSummary.getMinionId())
.map(server -> server.isDeniedOnPayg())
.map(Server::isDeniedOnPayg)
.orElse(false)).toList();
}
}
Loading

0 comments on commit 9374284

Please sign in to comment.