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

Issue 8387 #8568

Merged
merged 4 commits into from
May 22, 2024
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ venv.bak/
# mkdocs documentation
/site

/bin

# mypy
.mypy_cache/

Expand Down
10 changes: 5 additions & 5 deletions jans-config-api/plugins/docs/lock-plugin-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ components:
type: string
externalLoggerConfiguration:
type: string
metricChannel:
type: string
metricReporterInterval:
type: integer
format: int32
Expand All @@ -130,17 +132,15 @@ components:
format: int32
opaConfiguration:
$ref: '#/components/schemas/OpaConfiguration'
messageConsumerType:
type: string
policyConsumerType:
pdpType:
type: string
policiesJsonUrisAccessToken:
policiesJsonUrisAuthorizationToken:
type: string
policiesJsonUris:
type: array
items:
type: string
policiesZipUrisAccessToken:
policiesZipUrisAuthorizationToken:
type: string
policiesZipUris:
type: array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class AppConfiguration implements Configuration {
@DocProperty(description = "List of token channel names", defaultValue = "jans_token")
private List<String> tokenChannels;


@DocProperty(description = "Choose whether to disable JDK loggers", defaultValue = "true")
private Boolean disableJdkLogger = true;

Expand All @@ -40,6 +41,9 @@ public class AppConfiguration implements Configuration {
@DocProperty(description = "The path to the external log4j2 logging configuration")
private String externalLoggerConfiguration;

@DocProperty(description = "Channel for metric reports", defaultValue = "jans_pdp_metric")
private String metricChannel;

@DocProperty(description = "The interval for metric reporter in seconds")
private int metricReporterInterval;

Expand All @@ -55,10 +59,8 @@ public class AppConfiguration implements Configuration {

private OpaConfiguration opaConfiguration;

@DocProperty(description = "PDP for data")
private String messageConsumerType;
@DocProperty(description = "PDP for policies")
private String policyConsumerType;
@DocProperty(description = "PDP type")
private String pdpType;

@DocProperty(description = "Authorization token to access Json Uris")
private String policiesJsonUrisAuthorizationToken;
Expand Down Expand Up @@ -124,6 +126,14 @@ public void setExternalLoggerConfiguration(String externalLoggerConfiguration) {
this.externalLoggerConfiguration = externalLoggerConfiguration;
}

public String getMetricChannel() {
return metricChannel;
}

public void setMetricChannel(String metricChannel) {
this.metricChannel = metricChannel;
}

public int getMetricReporterInterval() {
return metricReporterInterval;
}
Expand Down Expand Up @@ -164,20 +174,12 @@ public void setOpaConfiguration(OpaConfiguration opaConfiguration) {
this.opaConfiguration = opaConfiguration;
}

public String getMessageConsumerType() {
return messageConsumerType;
}

public void setMessageConsumerType(String messageConsumerType) {
this.messageConsumerType = messageConsumerType;
}

public String getPolicyConsumerType() {
return policyConsumerType;
public String getPdpType() {
return pdpType;
}

public void setPolicyConsumerType(String policyConsumerType) {
this.policyConsumerType = policyConsumerType;
public void setPdpType(String pdpType) {
this.pdpType = pdpType;
}

public String getPoliciesJsonUrisAuthorizationToken() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ private void closePersistenceEntryManager(PersistenceEntryManager oldPersistence
oldPersistenceEntryManager, oldPersistenceEntryManager.getOperationService());
}
}


public void destroy(@Observes @BeforeDestroyed(ApplicationScoped.class) ServletContext init) {
log.info("Stopping services and closing DB connections at server shutdown...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.slf4j.Logger;

import io.jans.lock.model.config.AppConfiguration;
import io.jans.lock.service.consumer.message.generic.NullMessageConsumer;
import io.jans.service.cdi.async.Asynchronous;
import io.jans.service.cdi.event.ApplicationInitialized;
import io.jans.service.cdi.event.ConfigurationUpdate;
Expand Down Expand Up @@ -79,7 +80,7 @@ private void recreateMessageConsumer() {
@Produces
@ApplicationScoped
public MessageConsumer produceMessageConsumer() {
String messageConsumerType = appConfiguration.getMessageConsumerType();
String messageConsumerType = appConfiguration.getPdpType();
MessageConsumer messageConsumer = getMessageConsumer(messageConsumerType);

return messageConsumer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.jans.lock.service.consumer.message;
package io.jans.lock.service.consumer.message.generic;

import org.slf4j.Logger;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
* Copyright (c) 2023, Janssen Project
*/

package io.jans.service.policy.consumer;
package io.jans.lock.service.consumer.policy;

/**
* Base message consumer
*
* @author Yuriy Movchan Date: 12/20/2023
*/
public abstract class PolicyConsumer implements MessagePolicyInterface {
public abstract class PolicyConsumer implements PolicyConsumerInterface {

public abstract String getPolicyConsumerType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import org.slf4j.Logger;

import io.jans.lock.model.config.AppConfiguration;
import io.jans.lock.service.consumer.policy.generic.NullPolicyConsumer;
import io.jans.service.cdi.async.Asynchronous;
import io.jans.service.cdi.event.ApplicationInitialized;
import io.jans.service.cdi.event.ConfigurationUpdate;
import io.jans.service.cdi.qualifier.Implementation;
import io.jans.service.policy.consumer.PolicyConsumer;
import io.jans.util.StringHelper;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
Expand Down Expand Up @@ -68,7 +68,7 @@ private void recreatePolicyConsumer() {
@Produces
@ApplicationScoped
public PolicyConsumer producePolicyConsumer() {
String policyConsumerType = appConfiguration.getPolicyConsumerType();
String policyConsumerType = appConfiguration.getPdpType();
PolicyConsumer policyConsumer = buildPolicyConsumer(policyConsumerType);

return policyConsumer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright (c) 2023, Janssen Project
*/

package io.jans.service.policy.consumer;
package io.jans.lock.service.consumer.policy;

import java.util.List;

Expand All @@ -13,7 +13,7 @@
*
* @author Yuriy Movchan Date: 12/20/2023
*/
public interface MessagePolicyInterface {
public interface PolicyConsumerInterface {

public boolean putPolicies(String sourceUri, List<String> policies);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.jans.lock.service.consumer.policy;
package io.jans.lock.service.consumer.policy.generic;

import java.util.List;

import org.slf4j.Logger;

import io.jans.lock.service.consumer.policy.PolicyConsumer;
import io.jans.service.cdi.qualifier.Implementation;
import io.jans.service.policy.consumer.PolicyConsumer;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Instance;
import jakarta.inject.Inject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

import io.jans.lock.model.config.AppConfiguration;
import io.jans.lock.model.config.OpaConfiguration;
import io.jans.lock.service.consumer.policy.PolicyConsumer;
import io.jans.lock.service.external.ExternalLockService;
import io.jans.lock.service.external.context.ExternalLockContext;
import io.jans.service.EncryptionService;
import io.jans.service.cdi.qualifier.Implementation;
import io.jans.service.net.BaseHttpService;
import io.jans.service.policy.consumer.PolicyConsumer;
import io.jans.util.StringHelper;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
import com.fasterxml.jackson.databind.ObjectMapper;

import io.jans.lock.model.config.AppConfiguration;
import io.jans.lock.service.consumer.policy.PolicyConsumer;
import io.jans.lock.service.policy.event.PolicyDownloadEvent;
import io.jans.service.EncryptionService;
import io.jans.service.cdi.async.Asynchronous;
import io.jans.service.cdi.event.Scheduled;
import io.jans.service.net.BaseHttpService;
import io.jans.service.policy.consumer.PolicyConsumer;
import io.jans.service.timer.event.TimerEvent;
import io.jans.service.timer.schedule.TimerSchedule;
import io.jans.util.StringHelper;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Janssen Project software is available under the Apache License (2004). See http://www.apache.org/licenses/ for full text.
*
* Copyright (c) 2023, Janssen Project
*/

package io.jans.lock.service.provider.metric;

import org.slf4j.Logger;

import io.jans.lock.model.config.AppConfiguration;
import io.jans.lock.service.consumer.policy.PolicyConsumer;
import io.jans.service.cdi.async.Asynchronous;
import io.jans.service.cdi.event.ApplicationInitialized;
import io.jans.service.cdi.event.ConfigurationUpdate;
import io.jans.service.cdi.qualifier.Implementation;
import io.jans.util.StringHelper;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.Instance;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;

/**
* Metric producer factory
*
* @author Yuriy Movchan Date: 05/03/2024
*/
@ApplicationScoped
public class MetricProducerFactory {

@Inject
private Logger log;

@Inject
private AppConfiguration appConfiguration;

@Inject
@Implementation
private Instance<PolicyConsumer> policyConsumerProviderInstances;

private boolean appStarted = false;

public void init(@Observes @ApplicationInitialized(ApplicationScoped.class) Object init) {
this.appStarted = true;
}

@Asynchronous
public void configurationUpdateEvent(@Observes @ConfigurationUpdate AppConfiguration appConfiguration) {
if (!appStarted) {
return;
}

recreatePolicyConsumer();
}

private void recreatePolicyConsumer() {
// Force to create new bean
for (PolicyConsumer policyConsumer : policyConsumerProviderInstances) {
policyConsumerProviderInstances.destroy(policyConsumer);
policyConsumer.destroy();
log.info("Destroyed policyConsumer instance '{}'", System.identityHashCode(policyConsumer));
}

producePolicyConsumer();
}

@Produces
@ApplicationScoped
public PolicyConsumer producePolicyConsumer() {
String policyConsumerType = appConfiguration.getPdpType();
PolicyConsumer policyConsumer = buildPolicyConsumer(policyConsumerType);

return policyConsumer;
}

private PolicyConsumer buildPolicyConsumer(String policyConsumerType) {
for (PolicyConsumer policyConsumer : policyConsumerProviderInstances) {
String serviceMessageConsumerType = policyConsumer.getPolicyConsumerType();
if (StringHelper.equalsIgnoreCase(serviceMessageConsumerType, policyConsumerType)) {
return policyConsumer;
}
}

log.error("Failed to find policy consumer with type '{}'. Using null policy consumer", policyConsumerType);
return policyConsumerProviderInstances.select(NullMessageProvider.class).get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Janssen Project software is available under the Apache License (2004). See http://www.apache.org/licenses/ for full text.
*
* Copyright (c) 2023, Janssen Project
*/

package io.jans.lock.service.provider.metric;

/**
* Base message consumer
*
* @author Yuriy Movchan Date: 12/20/2023
*/
public abstract class MetricProvider implements MetricProviderInterface {

public abstract String getPolicyConsumerType();

public abstract void destroy();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Janssen Project software is available under the Apache License (2004). See http://www.apache.org/licenses/ for full text.
*
* Copyright (c) 2023, Janssen Project
*/

package io.jans.lock.service.provider.metric;

import java.util.List;

/**
* Interface for each policy consumer
*
* @author Yuriy Movchan Date: 12/20/2023
*/
public interface MetricProviderInterface {

public boolean putPolicies(String sourceUri, List<String> policies);

public boolean removePolicies(String sourceUri);

}
Loading