Skip to content
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
8 changes: 8 additions & 0 deletions components/org.wso2.micro.integrator.initializer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
<groupId>org.wso2.integration.transaction.counter</groupId>
<artifactId>transaction-count-handler</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.orbit.com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
</dependency>
</dependencies>

<build>
Expand All @@ -138,6 +142,10 @@
org.wso2.micro.integrator.initializer.*;version="${project.version}"
</Export-Package>
<Import-Package>
com.nimbusds.jose;version="${nimbus-jose.orbit.imp.pkg.version}",
com.nimbusds.jose.jwk;version="${nimbus-jose.orbit.imp.pkg.version}",
com.nimbusds.jose.crypto;version="${nimbus-jose.orbit.imp.pkg.version}",
com.nimbusds.jwt;version="${nimbus-jose.orbit.imp.pkg.version}",
org.wso2.micro.integrator.core.*;version="${project.version}",
*;resolution:=optional
</Import-Package>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ private Constants() {
public static final String DASHBOARD_CONFIG_MANAGEMENT_HOSTNAME = "dashboard_config.management_hostname";
public static final String DASHBOARD_CONFIG_MANAGEMENT_PORT = "dashboard_config.management_port";

// New ICP Configuration
public static final String ICP_CONFIG_URL = "icp_config.icp_url";
public static final String ICP_CONFIG_ENVIRONMENT = "icp_config.environment";
public static final String ICP_CONFIG_PROJECT = "icp_config.project";
public static final String ICP_CONFIG_COMPONENT = "icp_config.integration";
public static final String ICP_CONFIG_ENABLED = "icp_config.enabled";
public static final String ICP_CONFIG_HEARTBEAT_INTERVAL = "icp_config.heartbeat_interval";

// JWT Configuration
public static final String ICP_JWT_ISSUER = "icp_config.jwt_issuer";
public static final String ICP_JWT_AUDIENCE = "icp_config.jwt_audience";
public static final String ICP_JWT_SCOPE = "icp_config.jwt_scope";
public static final String ICP_JWT_EXPIRY_SECONDS = "icp_config.jwt_expiry_seconds";
public static final String ICP_JWT_HMAC_SECRET = "icp_config.jwt_hmac_secret";

// Default ICP Configuration
public static final String DEFAULT_ENVIRONMENT = "production";
public static final String DEFAULT_PROJECT = "default";
public static final String DEFAULT_COMPONENT = "default";
public static final String DEFAULT_ICP_URL = "https://localhost:9445";

public static final String DEFAULT_JWT_ISSUER = "icp-runtime-jwt-issuer";
public static final String DEFAULT_JWT_AUDIENCE = "icp-server";
public static final String DEFAULT_JWT_SCOPE = "runtime_agent";
public static final long DEFAULT_JWT_EXPIRY_SECONDS = 3600;
public static final String DEFAULT_JWT_HMAC_SECRET = "default-secret-key-at-least-32-characters-long-for-hs256";
public static final String RUNTIME_TYPE_MI = "MI";
public static final String RUNTIME_STATUS_RUNNING = "RUNNING";

public static final String DEFAULT_GROUP_ID = "default";
public static final long DEFAULT_HEARTBEAT_INTERVAL = 5;

Expand All @@ -23,4 +52,8 @@ private Constants() {
public static final String COLON = ":";
public static final String HTTPS_PREFIX = "https://";
public static final String MANAGEMENT = "management";

// ICP Endpoints
public static final String ICP_HEARTBEAT_ENDPOINT = "/icp/heartbeat";
public static final String ICP_DELTA_HEARTBEAT_ENDPOINT = "/icp/deltaHeartbeat";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.wso2.micro.integrator.initializer.dashboard;

import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.crypto.MACSigner;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;

import java.nio.charset.StandardCharsets;
import java.util.Date;

public class HMACJWTTokenGenerator {

private final String hmacSecret;

public HMACJWTTokenGenerator(String hmacSecret) {
if (hmacSecret == null || hmacSecret.getBytes(StandardCharsets.UTF_8).length < 32) {
throw new IllegalArgumentException("HMAC secret must be at least 256 bits (32 bytes)");
}
this.hmacSecret = hmacSecret;
}

/**
* Generate JWT Token with HMAC SHA256
*/
public String generateToken(String issuer, String audience, String scope, long expiryTimeSeconds)
throws JOSEException {

// Calculate expiry
long expiryMillis = System.currentTimeMillis() + (expiryTimeSeconds * 1000);

// Build claims
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
.issuer(issuer)
.audience(audience)
.expirationTime(new Date(expiryMillis))
.issueTime(new Date())
.claim("scope", scope)
.build();

// Create HMAC signer
JWSSigner signer = new MACSigner(hmacSecret.getBytes(StandardCharsets.UTF_8));

// Create and sign JWT
SignedJWT signedJWT = new SignedJWT(
new JWSHeader.Builder(JWSAlgorithm.HS256).build(),
claimsSet
);

signedJWT.sign(signer);
return signedJWT.serialize();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
*/
package org.wso2.micro.integrator.initializer.dashboard;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
Expand Down Expand Up @@ -71,7 +71,13 @@ private HeartBeatComponent(){
private static final Map<String, Object> configs = ConfigParser.getParsedConfigs();

public static void invokeHeartbeatExecutorService() {

// Check if new ICP is configured
if (ICPHeartBeatComponent.isICPConfigured()) {
log.info("New ICP configuration detected. Starting ICP heartbeat service.");
ICPHeartBeatComponent.invokeICPHeartbeatExecutorService();
return;
}
// Fall back to old dashboard heartbeat
String heartbeatApiUrl = configs.get(DASHBOARD_CONFIG_URL) + "/heartbeat";
Comment on lines 73 to 81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 1

Suggested change
public static void invokeHeartbeatExecutorService() {
// Check if new ICP is configured
if (ICPHeartBeatComponent.isICPConfigured()) {
log.info("New ICP configuration detected. Starting ICP heartbeat service.");
ICPHeartBeatComponent.invokeICPHeartbeatExecutorService();
return;
}
// Fall back to old dashboard heartbeat
String heartbeatApiUrl = configs.get(DASHBOARD_CONFIG_URL) + "/heartbeat";
// Check if new ICP is configured
if (ICPHeartBeatComponent.isICPConfigured()) {
log.info("New ICP configuration detected. Starting ICP heartbeat service.");
ICPHeartBeatComponent.invokeICPHeartbeatExecutorService();
return;
}
// Fall back to old dashboard heartbeat
if (log.isDebugEnabled()) {
log.debug("Starting dashboard heartbeat service with URL: " + heartbeatApiUrl);
}

String groupId = getGroupId();
String nodeId = getNodeId();
Expand Down Expand Up @@ -174,14 +180,16 @@ private static String generateRandomId() {
}

public static boolean isDashboardConfigured() {
return configs.get(DASHBOARD_CONFIG_URL) != null;
// Check for either old dashboard config or new ICP config
return configs.get(DASHBOARD_CONFIG_URL) != null || ICPHeartBeatComponent.isICPConfigured();
}

public static JsonObject getJsonResponse(CloseableHttpResponse response) {
String stringResponse = getStringResponse(response);
JsonObject responseObject = null;
try {
responseObject = new JsonParser().parse(stringResponse).getAsJsonObject();
Gson gson = new Gson();
responseObject = gson.fromJson(stringResponse, JsonObject.class);
Comment on lines 188 to +192
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 2

Suggested change
String stringResponse = getStringResponse(response);
JsonObject responseObject = null;
try {
responseObject = new JsonParser().parse(stringResponse).getAsJsonObject();
Gson gson = new Gson();
responseObject = gson.fromJson(stringResponse, JsonObject.class);
JsonObject responseObject = null;
try {
Gson gson = new Gson();
responseObject = gson.fromJson(stringResponse, JsonObject.class);
if (log.isDebugEnabled()) {
log.debug("Successfully parsed heartbeat response");
}

} catch (JsonParseException e) {
log.debug("Error occurred while parsing the heartbeat response.", e);
}
Expand Down
Loading
Loading