-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jans-auth-server): added new HealthCheck custom sсript (#8144)
* feat(jans-auth-server): added definition of new HealthCheck custom script #7991 Signed-off-by: YuriyZ <yzabrovarniy@gmail.com> * feat(jans-auth-server): added external health check service Signed-off-by: YuriyZ <yzabrovarniy@gmail.com> * feat(jans-auth-server): corrected different typos Signed-off-by: YuriyZ <yzabrovarniy@gmail.com> * feat(jans-auth-server): injected health check custom script into controller Signed-off-by: YuriyZ <yzabrovarniy@gmail.com> * feat(jans-auth-server): added sample health check custom script Signed-off-by: YuriyZ <yzabrovarniy@gmail.com> * doc(jans-auth-server): added documentation for health check custom script Signed-off-by: YuriyZ <yzabrovarniy@gmail.com> --------- Signed-off-by: YuriyZ <yzabrovarniy@gmail.com>
- Loading branch information
Showing
10 changed files
with
309 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
--- | ||
tags: | ||
- administration | ||
- developer | ||
- scripts | ||
--- | ||
|
||
# Create User Custom Script | ||
|
||
## Overview | ||
"HealthCheck" custom script is used when Health Check Endpoint is called. | ||
Custom script allows inject/modify response from Health Check Endpoint. | ||
|
||
## Interface | ||
The HealthCheck script implements the [HealthCheckType](https://github.com/JanssenProject/jans/blob/main/jans-core/script/src/main/java/io/jans/model/custom/script/type/health_check/HealthCheckType.java) interface. This extends methods from the base script type in addition to adding new methods: | ||
|
||
### Inherited Methods | ||
| Method header | Method description | | ||
|:-----|:------| | ||
| `def init(self, customScript, configurationAttributes)` | This method is only called once during the script initialization. It can be used for global script initialization, initiate objects etc | | ||
| `def destroy(self, configurationAttributes)` | This method is called once to destroy events. It can be used to free resource and objects created in the `init()` method | | ||
| `def getApiVersion(self, configurationAttributes, customScript)` | The getApiVersion method allows API changes in order to do transparent migration from an old script to a new API. Only include the customScript variable if the value for getApiVersion is greater than 10 | | ||
|
||
### New methods | ||
| Method header | Method description | | ||
|:-----|:------| | ||
|`def healthCheck(self, context)`| Returns response for health check endpoint | ||
|
||
|
||
### Objects | ||
| Object name | Object description | | ||
|:-----|:------| | ||
|`customScript`| The custom script object. [Reference](https://github.com/JanssenProject/jans/blob/main/jans-core/script/src/main/java/io/jans/model/custom/script/model/CustomScript.java) | | ||
|`context`| [Reference](https://github.com/JanssenProject/jans/blob/main/jans-auth-server/server/src/main/java/io/jans/as/server/service/external/context/ExternalScriptContext.java) | | ||
|
||
|
||
### Saample Scrip in Java | ||
|
||
```java | ||
package io.jans.as.server._scripts; | ||
|
||
import io.jans.as.server.service.external.context.ExternalScriptContext; | ||
import io.jans.model.SimpleCustomProperty; | ||
import io.jans.model.custom.script.model.CustomScript; | ||
import io.jans.model.custom.script.type.health.HealthCheckType; | ||
import io.jans.service.custom.script.CustomScriptManager; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
public class HealthCheck implements HealthCheckType { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(HealthCheck.class); | ||
private static final Logger scriptLogger = LoggerFactory.getLogger(CustomScriptManager.class); | ||
|
||
@Override | ||
public String healthCheck(Object context) { | ||
ExternalScriptContext scriptContext = (ExternalScriptContext) context; | ||
HttpServletRequest httpRequest = scriptContext.getExecutionContext().getHttpRequest(); | ||
|
||
String appStatus = "running"; | ||
String dbStatus = "online"; | ||
return String.format("{\"status\": \"%s\", \"db_status\":\"%s\"}", appStatus, dbStatus); | ||
} | ||
|
||
@Override | ||
public boolean init(Map<String, SimpleCustomProperty> configurationAttributes) { | ||
scriptLogger.info("Initialized HealthCheck Java custom script."); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean init(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) { | ||
scriptLogger.info("Initialized HealthCheck Java custom script."); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean destroy(Map<String, SimpleCustomProperty> configurationAttributes) { | ||
scriptLogger.info("Destroyed HealthCheck Java custom script."); | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getApiVersion() { | ||
return 11; | ||
} | ||
} | ||
|
||
``` | ||
|
||
## Sample Scripts | ||
- [Sample HealthCheck script](../../../script-catalog/health_check/HealthCheck.java) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.jans.as.server._scripts; | ||
|
||
import io.jans.as.server.service.external.context.ExternalScriptContext; | ||
import io.jans.model.SimpleCustomProperty; | ||
import io.jans.model.custom.script.model.CustomScript; | ||
import io.jans.model.custom.script.type.health.HealthCheckType; | ||
import io.jans.service.custom.script.CustomScriptManager; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
public class HealthCheck implements HealthCheckType { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(HealthCheck.class); | ||
private static final Logger scriptLogger = LoggerFactory.getLogger(CustomScriptManager.class); | ||
|
||
@Override | ||
public String healthCheck(Object context) { | ||
ExternalScriptContext scriptContext = (ExternalScriptContext) context; | ||
HttpServletRequest httpRequest = scriptContext.getExecutionContext().getHttpRequest(); | ||
|
||
String appStatus = "running"; | ||
String dbStatus = "online"; | ||
return String.format("{\"status\": \"%s\", \"db_status\":\"%s\"}", appStatus, dbStatus); | ||
} | ||
|
||
@Override | ||
public boolean init(Map<String, SimpleCustomProperty> configurationAttributes) { | ||
scriptLogger.info("Initialized HealthCheck Java custom script."); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean init(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) { | ||
scriptLogger.info("Initialized HealthCheck Java custom script."); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean destroy(Map<String, SimpleCustomProperty> configurationAttributes) { | ||
scriptLogger.info("Destroyed HealthCheck Java custom script."); | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getApiVersion() { | ||
return 11; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...r/server/src/main/java/io/jans/as/server/service/external/ExternalHealthCheckService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package io.jans.as.server.service.external; | ||
|
||
import com.google.common.collect.Sets; | ||
import io.jans.as.server.model.common.ExecutionContext; | ||
import io.jans.as.server.service.external.context.ExternalScriptContext; | ||
import io.jans.model.custom.script.CustomScriptType; | ||
import io.jans.model.custom.script.conf.CustomScriptConfiguration; | ||
import io.jans.model.custom.script.type.health.HealthCheckType; | ||
import io.jans.service.custom.script.ExternalScriptService; | ||
import jakarta.ejb.DependsOn; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Named; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Set; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
@ApplicationScoped | ||
@DependsOn("appInitializer") | ||
@Named | ||
public class ExternalHealthCheckService extends ExternalScriptService { | ||
|
||
public ExternalHealthCheckService() { | ||
super(CustomScriptType.HEALTH_CHECK); | ||
} | ||
|
||
public String externalHealthCheck(ExecutionContext context) { | ||
final Set<CustomScriptConfiguration> scripts = getScriptsToExecute(); | ||
if (scripts.isEmpty()) { | ||
return null; | ||
} | ||
|
||
log.trace("Found {} health-check scripts.", scripts.size()); | ||
|
||
for (CustomScriptConfiguration script : scripts) { | ||
final String result = externalHealthCheck(script, context); | ||
if (StringUtils.isNotBlank(result)) { | ||
log.debug("'healthCheck' returned result {}, script: {}", result, script.getName()); | ||
return result; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private String externalHealthCheck(CustomScriptConfiguration scriptConfiguration, ExecutionContext context) { | ||
try { | ||
log.trace("Executing external 'healthCheck' method, script name: {}, context: {}", scriptConfiguration.getName(), context); | ||
|
||
HealthCheckType script = (HealthCheckType) scriptConfiguration.getExternalType(); | ||
context.setScript(scriptConfiguration); | ||
|
||
final ExternalScriptContext scriptContext = new ExternalScriptContext(context); | ||
final String result = script.healthCheck(scriptContext); | ||
|
||
log.trace("Finished external 'healthCheck' method, script name: {}, context: {}, result: {}", scriptConfiguration.getName(), context, result); | ||
return result; | ||
} catch (Exception ex) { | ||
log.error(ex.getMessage(), ex); | ||
saveScriptError(scriptConfiguration.getCustomScript(), ex); | ||
return null; | ||
} | ||
} | ||
|
||
private Set<CustomScriptConfiguration> getScriptsToExecute() { | ||
Set<CustomScriptConfiguration> result = Sets.newHashSet(); | ||
if (this.customScriptConfigurations == null) { | ||
return result; | ||
} | ||
|
||
result.addAll(customScriptConfigurations); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
jans-core/script/src/main/java/io/jans/model/custom/script/type/health/DummyHealthCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package io.jans.model.custom.script.type.health; | ||
|
||
import io.jans.model.SimpleCustomProperty; | ||
import io.jans.model.custom.script.model.CustomScript; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
public class DummyHealthCheck implements HealthCheckType { | ||
|
||
@Override | ||
public String healthCheck(Object context) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public boolean init(Map<String, SimpleCustomProperty> configurationAttributes) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean init(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean destroy(Map<String, SimpleCustomProperty> configurationAttributes) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public int getApiVersion() { | ||
return 11; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
jans-core/script/src/main/java/io/jans/model/custom/script/type/health/HealthCheckType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.jans.model.custom.script.type.health; | ||
|
||
import io.jans.model.custom.script.type.BaseExternalType; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
public interface HealthCheckType extends BaseExternalType { | ||
|
||
/** | ||
* @param context script context | ||
* @return health check response | ||
*/ | ||
String healthCheck(Object context); | ||
} |