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

RHCLOUD-34913 - Refactor inventory-client-java to use common-client-java code #4

Merged
merged 9 commits into from
Sep 3, 2024
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
</scm>

<dependencies>
<dependency>
<groupId>org.project-kessel</groupId>
<artifactId>common-client-java</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

package org.project_kessel.inventory.client;

import org.project_kessel.clients.authn.AuthenticationConfig;
import org.project_kessel.clients.authn.oidc.client.OIDCClientCredentialsAuthenticationConfig;
import org.project_kessel.clients.authn.oidc.client.OIDCClientCredentialsAuthenticationConfig.OIDCClientCredentialsConfig;

public class AuthnConfigConverter {

public static AuthenticationConfig convert(Config.AuthenticationConfig authnConfig) {
if(authnConfig == null) {
return null;
}
AuthenticationConfig convertedAuthnConfig;
if(authnConfig.clientCredentialsConfig().isPresent()) {
Config.OIDCClientCredentialsConfig oidcClientCredentialsConfig = authnConfig.clientCredentialsConfig().get();

convertedAuthnConfig = new OIDCClientCredentialsAuthenticationConfig();
var convertedOidcClientCredentialsConfig = new OIDCClientCredentialsConfig();
convertedOidcClientCredentialsConfig.setIssuer(oidcClientCredentialsConfig.issuer());
convertedOidcClientCredentialsConfig.setClientId(oidcClientCredentialsConfig.clientId());
convertedOidcClientCredentialsConfig.setClientSecret(oidcClientCredentialsConfig.clientSecret());
convertedOidcClientCredentialsConfig.setScope(oidcClientCredentialsConfig.scope());
convertedOidcClientCredentialsConfig.setOidcClientCredentialsMinterImplementation(oidcClientCredentialsConfig.oidcClientCredentialsMinterImplementation());

((OIDCClientCredentialsAuthenticationConfig)convertedAuthnConfig).setCredentialsConfig(convertedOidcClientCredentialsConfig);
} else {
convertedAuthnConfig = new AuthenticationConfig();
}

convertedAuthnConfig.setMode(authnConfig.mode());

return convertedAuthnConfig;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import org.project_kessel.clients.authn.AuthenticationConfig.AuthMode;

/**
* A managed bean for providing relations api clients for injection in apps.
* A managed bean for providing clients for injection in apps.
* It has the current limitation that only one underlying grpc connection can be configured.
* However, it is still possible to create more via InventoryGrpcClientsManager directly.
* However, it is still possible to create more via KesselClientsManager implementation directly.
* This class does nothing unless the client is being managed by a CDI container (e.g. Quarkus)
*/
@ApplicationScoped
public class CDIManagedClients {
public class CDIManagedInventoryClients {
@Produces
InventoryGrpcClientsManager getManager(Config config) {
var isSecureClients = config.isSecureClients();
var targetUrl = config.targetUrl();
var authnEnabled = config.authenticationConfig().map(t -> !t.mode().equals(Config.AuthMode.DISABLED)).orElse(false);
var authnEnabled = config.authenticationConfig().map(t -> !t.mode().equals(AuthMode.DISABLED)).orElse(false);
if (isSecureClients) {
if(authnEnabled) {
return InventoryGrpcClientsManager.forSecureClients(targetUrl, config.authenticationConfig().get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithName;
import org.project_kessel.clients.authn.AuthenticationConfig.AuthMode;

import java.util.Optional;

Expand All @@ -14,10 +15,6 @@
*/
@ConfigMapping(prefix = "inventory-api")
public interface Config {
enum AuthMode {
DISABLED,
OIDC_CLIENT_CREDENTIALS
}

@WithDefault("false")
boolean isSecureClients();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,119 +1,38 @@
package org.project_kessel.inventory.client;

import io.grpc.*;
import org.project_kessel.inventory.client.authn.CallCredentialsFactory;
import io.grpc.Channel;
import org.project_kessel.clients.ChannelManager;
import org.project_kessel.clients.KesselClientsManager;

import java.util.HashMap;

public class InventoryGrpcClientsManager {
private static final HashMap<String, InventoryGrpcClientsManager> insecureManagers = new HashMap<>();
private static final HashMap<String, InventoryGrpcClientsManager> secureManagers = new HashMap<>();

private final ManagedChannel channel;

public static synchronized InventoryGrpcClientsManager forInsecureClients(String targetUrl) {
if (!insecureManagers.containsKey(targetUrl)) {
var manager = new InventoryGrpcClientsManager(targetUrl, InsecureChannelCredentials.create());
insecureManagers.put(targetUrl, manager);
}
return insecureManagers.get(targetUrl);
public class InventoryGrpcClientsManager extends KesselClientsManager {
private InventoryGrpcClientsManager(Channel channel) {
super(channel);
}

public static synchronized InventoryGrpcClientsManager forInsecureClients(String targetUrl, Config.AuthenticationConfig authnConfig) throws RuntimeException {
if (!insecureManagers.containsKey(targetUrl)) {
try {
var manager = new InventoryGrpcClientsManager(targetUrl,
InsecureChannelCredentials.create(),
CallCredentialsFactory.create(authnConfig));
insecureManagers.put(targetUrl, manager);
} catch (CallCredentialsFactory.CallCredentialsCreationException e) {
throw new RuntimeException(e);
}
}
return insecureManagers.get(targetUrl);
}
private static final String CHANNEL_MANAGER_KEY = InventoryGrpcClientsManager.class.getName();

public static synchronized InventoryGrpcClientsManager forSecureClients(String targetUrl) {
if (!secureManagers.containsKey(targetUrl)) {
var tlsChannelCredentials = TlsChannelCredentials.create();
var manager = new InventoryGrpcClientsManager(targetUrl, tlsChannelCredentials);
secureManagers.put(targetUrl, manager);
}
return secureManagers.get(targetUrl);
public static InventoryGrpcClientsManager forInsecureClients(String targetUrl) {
return new InventoryGrpcClientsManager(ChannelManager.getInstance(CHANNEL_MANAGER_KEY).forInsecureClients(targetUrl));
}

public static synchronized InventoryGrpcClientsManager forSecureClients(String targetUrl, Config.AuthenticationConfig authnConfig) {
if (!secureManagers.containsKey(targetUrl)) {
var tlsChannelCredentials = TlsChannelCredentials.create();
try {
var manager = new InventoryGrpcClientsManager(targetUrl,
tlsChannelCredentials,
CallCredentialsFactory.create(authnConfig));
secureManagers.put(targetUrl, manager);
} catch (CallCredentialsFactory.CallCredentialsCreationException e) {
throw new RuntimeException(e);
}
}
return secureManagers.get(targetUrl);
public static InventoryGrpcClientsManager forInsecureClients(String targetUrl, Config.AuthenticationConfig authnConfig) throws RuntimeException {
return new InventoryGrpcClientsManager(ChannelManager.getInstance(CHANNEL_MANAGER_KEY).forInsecureClients(targetUrl, AuthnConfigConverter.convert(authnConfig)));
}


public static synchronized void shutdownAll() {
for (var manager : insecureManagers.values()) {
manager.closeClientChannel();
}
insecureManagers.clear();
for (var manager : secureManagers.values()) {
manager.closeClientChannel();
}
secureManagers.clear();
public static InventoryGrpcClientsManager forSecureClients(String targetUrl) {
return new InventoryGrpcClientsManager(ChannelManager.getInstance(CHANNEL_MANAGER_KEY).forSecureClients(targetUrl));
}

public static synchronized void shutdownManager(InventoryGrpcClientsManager managerToShutdown) {
var iter = insecureManagers.entrySet().iterator();
while (iter.hasNext()) {
var entry = iter.next();
if(entry.getValue().channel == managerToShutdown.channel) {
entry.getValue().closeClientChannel();
iter.remove();
return;
}
}
iter = secureManagers.entrySet().iterator();
while (iter.hasNext()) {
var entry = iter.next();
if(entry.getValue().channel == managerToShutdown.channel) {
entry.getValue().closeClientChannel();
iter.remove();
return;
}
}
}
/**
*
* Bearer token and other things can be added to ChannelCredentials. New static factory methods can be added.
* Config management also required.
* @param targetUrl
* @param credentials
*/
private InventoryGrpcClientsManager(String targetUrl, ChannelCredentials credentials) {
this.channel = Grpc.newChannelBuilder(targetUrl, credentials).build();
public static InventoryGrpcClientsManager forSecureClients(String targetUrl, Config.AuthenticationConfig authnConfig) {
return new InventoryGrpcClientsManager(ChannelManager.getInstance(CHANNEL_MANAGER_KEY).forSecureClients(targetUrl, AuthnConfigConverter.convert(authnConfig)));
}

/**
* Create a manager for a grpc channel with server credentials and credentials for per-rpc client authentication.
* @param targetUrl
* @param serverCredentials authenticates the server for TLS or are InsecureChannelCredentials
* @param authnCredentials authenticates the client on each rpc
*/
private InventoryGrpcClientsManager(String targetUrl, ChannelCredentials serverCredentials, CallCredentials authnCredentials) {
this.channel = Grpc.newChannelBuilder(targetUrl,
CompositeChannelCredentials.create(serverCredentials, authnCredentials)).build();
public static void shutdownAll() {
ChannelManager.getInstance(CHANNEL_MANAGER_KEY).shutdownAll();
}


private void closeClientChannel() {
channel.shutdown();
public static void shutdownManager(InventoryGrpcClientsManager managerToShutdown) {
ChannelManager.getInstance(CHANNEL_MANAGER_KEY).shutdownChannel(managerToShutdown.channel);
}

public RhelHostClient getHostClient() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import io.grpc.Channel;
import org.project_kessel.api.inventory.v1.*;
import org.project_kessel.clients.KesselClient;


import java.util.logging.Logger;

public class InventoryHealthClient {
public class InventoryHealthClient extends KesselClient<KesselInventoryHealthServiceGrpc.KesselInventoryHealthServiceStub, KesselInventoryHealthServiceGrpc.KesselInventoryHealthServiceBlockingStub>{
private static final Logger logger = Logger.getLogger(InventoryHealthClient.class.getName());

private final KesselInventoryHealthServiceGrpc.KesselInventoryHealthServiceBlockingStub blockingStub;

InventoryHealthClient(Channel channel){
blockingStub = KesselInventoryHealthServiceGrpc.newBlockingStub(channel);
super(KesselInventoryHealthServiceGrpc.newStub(channel), KesselInventoryHealthServiceGrpc.newBlockingStub(channel));
}

public GetReadyzResponse getReadyz(GetReadyzRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.operators.multi.processors.UnicastProcessor;
import org.project_kessel.api.inventory.v1beta1.*;
import org.project_kessel.clients.KesselClient;


import java.util.logging.Logger;

public class K8sClustersClient {
public class K8sClustersClient extends KesselClient<KesselK8sClusterServiceGrpc.KesselK8sClusterServiceStub, KesselK8sClusterServiceGrpc.KesselK8sClusterServiceBlockingStub>{
private static final Logger logger = Logger.getLogger(K8sClustersClient.class.getName());
private final KesselK8sClusterServiceGrpc.KesselK8sClusterServiceStub asyncStub;
private final KesselK8sClusterServiceGrpc.KesselK8sClusterServiceBlockingStub blockingStub;

K8sClustersClient(Channel channel){
asyncStub = KesselK8sClusterServiceGrpc.newStub(channel);
blockingStub = KesselK8sClusterServiceGrpc.newBlockingStub(channel);
super(KesselK8sClusterServiceGrpc.newStub(channel), KesselK8sClusterServiceGrpc.newBlockingStub(channel));

}

public void UpdateK8sCluster(UpdateK8sClusterRequest request, StreamObserver<UpdateK8sClusterResponse> responseObserver){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.operators.multi.processors.UnicastProcessor;
import org.project_kessel.api.inventory.v1beta1.*;
import org.project_kessel.clients.KesselClient;


import java.util.logging.Logger;

public class NotificationsIntegrationClient {
public class NotificationsIntegrationClient extends KesselClient<KesselNotificationsIntegrationServiceGrpc.KesselNotificationsIntegrationServiceStub, KesselNotificationsIntegrationServiceGrpc.KesselNotificationsIntegrationServiceBlockingStub>{

private static final Logger logger = Logger.getLogger(RhelHostClient.class.getName());

private final KesselNotificationsIntegrationServiceGrpc.KesselNotificationsIntegrationServiceStub asyncStub;
private final KesselNotificationsIntegrationServiceGrpc.KesselNotificationsIntegrationServiceBlockingStub blockingStub;

NotificationsIntegrationClient(Channel channel) {
asyncStub = KesselNotificationsIntegrationServiceGrpc.newStub(channel);
blockingStub = KesselNotificationsIntegrationServiceGrpc.newBlockingStub(channel);
super(KesselNotificationsIntegrationServiceGrpc.newStub(channel), KesselNotificationsIntegrationServiceGrpc.newBlockingStub(channel));
}

public CreateNotificationsIntegrationResponse CreateNotificationsIntegration(CreateNotificationsIntegrationRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.operators.multi.processors.UnicastProcessor;
import org.project_kessel.api.inventory.v1beta1.*;
import org.project_kessel.clients.KesselClient;

import java.util.logging.Logger;

public class PoliciesClient {
public class PoliciesClient extends KesselClient<KesselPolicyServiceGrpc.KesselPolicyServiceStub, KesselPolicyServiceGrpc.KesselPolicyServiceBlockingStub>{
private static final Logger logger = Logger.getLogger(PoliciesClient.class.getName());

private final KesselPolicyServiceGrpc.KesselPolicyServiceStub asyncStub;
private final KesselPolicyServiceGrpc.KesselPolicyServiceBlockingStub blockingStub;

PoliciesClient(Channel channel){
asyncStub = KesselPolicyServiceGrpc.newStub(channel);
blockingStub = KesselPolicyServiceGrpc.newBlockingStub(channel);
super(KesselPolicyServiceGrpc.newStub(channel), KesselPolicyServiceGrpc.newBlockingStub(channel));
}

public CreatePolicyResponse CreatePolicy(CreatePolicyRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
import io.smallrye.mutiny.Uni;
import io.smallrye.mutiny.operators.multi.processors.UnicastProcessor;
import org.project_kessel.api.inventory.v1beta1.*;
import org.project_kessel.clients.KesselClient;


import java.util.logging.Logger;

public class PolicyRelationshipClient {
public class PolicyRelationshipClient extends KesselClient<KesselPolicyRelationshipServiceGrpc.KesselPolicyRelationshipServiceStub, KesselPolicyRelationshipServiceGrpc.KesselPolicyRelationshipServiceBlockingStub>{
private static final Logger logger = Logger.getLogger(PoliciesClient.class.getName());

private final KesselPolicyRelationshipServiceGrpc.KesselPolicyRelationshipServiceStub asyncStub;
private final KesselPolicyRelationshipServiceGrpc.KesselPolicyRelationshipServiceBlockingStub blockingStub;

PolicyRelationshipClient(Channel channel){
asyncStub = KesselPolicyRelationshipServiceGrpc.newStub(channel);
blockingStub = KesselPolicyRelationshipServiceGrpc.newBlockingStub(channel);
super(KesselPolicyRelationshipServiceGrpc.newStub(channel), KesselPolicyRelationshipServiceGrpc.newBlockingStub(channel));
}

public CreatePolicyRelationshipResponse CreatePolicyRelationship(CreatePolicyRelationshipRequest request){
Expand Down

This file was deleted.

Loading