|
|
@ -19,19 +19,30 @@
|
|
|
|
package org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.core;
|
|
|
|
package org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.core;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
|
|
|
|
|
import org.apache.commons.configuration.ConfigurationException;
|
|
|
|
|
|
|
|
import org.apache.commons.configuration.PropertiesConfiguration;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
|
|
import org.eclipse.jetty.http.HttpStatus;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.enrollment.EnrollmentManager;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.enrollment.EnrollmentManager;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.exception.AgentCoreOperationException;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.exception.AgentCoreOperationException;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.CommunicationUtils;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.CommunicationUtils;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandlerException;
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportHandlerException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportUtils;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import javax.net.ssl.HostnameVerifier;
|
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
|
|
|
import javax.net.ssl.SSLSession;
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
|
|
|
import java.net.ProtocolException;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
@ -67,94 +78,104 @@ public class AgentUtilOperations {
|
|
|
|
Properties properties = new Properties();
|
|
|
|
Properties properties = new Properties();
|
|
|
|
InputStream propertiesInputStream = null;
|
|
|
|
InputStream propertiesInputStream = null;
|
|
|
|
String propertiesFileName = AgentConstants.AGENT_PROPERTIES_FILE_NAME;
|
|
|
|
String propertiesFileName = AgentConstants.AGENT_PROPERTIES_FILE_NAME;
|
|
|
|
|
|
|
|
String rootPath = "";
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
ClassLoader loader = AgentUtilOperations.class.getClassLoader();
|
|
|
|
ClassLoader loader = AgentUtilOperations.class.getClassLoader();
|
|
|
|
URL path = loader.getResource(propertiesFileName);
|
|
|
|
URL path = loader.getResource(propertiesFileName);
|
|
|
|
System.out.println(path);
|
|
|
|
|
|
|
|
String root = path.getPath().replace("wso2-firealarm-virtual-agent.jar!/deviceConfig.properties", "")
|
|
|
|
|
|
|
|
.replace("jar:", "").replace("file:", "");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root = URLDecoder.decode(root, StandardCharsets.UTF_8.toString());
|
|
|
|
|
|
|
|
agentManager.setRootPath(root);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String deviceConfigFilePath = root + AgentConstants.AGENT_PROPERTIES_FILE_NAME;
|
|
|
|
|
|
|
|
propertiesInputStream = new FileInputStream(deviceConfigFilePath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//load a properties file from class path, inside static method
|
|
|
|
|
|
|
|
properties.load(propertiesInputStream);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iotServerConfigs.setTenantDomain(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.TENANT_DOMAIN));
|
|
|
|
|
|
|
|
iotServerConfigs.setDeviceOwner(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.DEVICE_OWNER_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setDeviceId(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.DEVICE_ID_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setDeviceName(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.DEVICE_NAME_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setControllerContext(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.DEVICE_CONTROLLER_CONTEXT_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setScepContext(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.DEVICE_SCEP_CONTEXT_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setHTTPS_ServerEndpoint(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.SERVER_HTTPS_EP_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setHTTP_ServerEndpoint(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.SERVER_HTTP_EP_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setApimGatewayEndpoint(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.APIM_GATEWAY_EP_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setMqttBrokerEndpoint(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.MQTT_BROKER_EP_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setXmppServerEndpoint(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.XMPP_SERVER_EP_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setXmppServerName(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.XMPP_SERVER_NAME_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setAuthMethod(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.AUTH_METHOD_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setAuthToken(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.AUTH_TOKEN_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setRefreshToken(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.REFRESH_TOKEN_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setDataPushInterval(Integer.parseInt(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.PUSH_INTERVAL_PROPERTY)));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Tenant Domain: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getTenantDomain());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Owner: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getDeviceOwner());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device ID: " + iotServerConfigs.getDeviceId());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Name: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getDeviceName());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Controller Context: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getControllerContext());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "IoT Server HTTPS EndPoint: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getHTTPS_ServerEndpoint());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "IoT Server HTTP EndPoint: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getHTTP_ServerEndpoint());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "API-Manager Gateway EndPoint: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getApimGatewayEndpoint());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "MQTT Broker EndPoint: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getMqttBrokerEndpoint());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "XMPP Server EndPoint: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getXmppServerEndpoint());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Authentication Method: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getAuthMethod());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Authentication Token: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getAuthToken());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Refresh Token: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getRefreshToken());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Data Push Interval: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getDataPushInterval());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "XMPP Server Name: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getXmppServerName());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (path != null) {
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + path);
|
|
|
|
|
|
|
|
rootPath = path.getPath().replace("wso2-firealarm-virtual-agent.jar!/deviceConfig.properties", "")
|
|
|
|
|
|
|
|
.replace("jar:", "").replace("file:", "");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rootPath = URLDecoder.decode(rootPath, StandardCharsets.UTF_8.toString());
|
|
|
|
|
|
|
|
agentManager.setRootPath(rootPath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String deviceConfigFilePath = rootPath + AgentConstants.AGENT_PROPERTIES_FILE_NAME;
|
|
|
|
|
|
|
|
propertiesInputStream = new FileInputStream(deviceConfigFilePath);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//load a properties file from class path, inside static method
|
|
|
|
|
|
|
|
properties.load(propertiesInputStream);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iotServerConfigs.setTenantDomain(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.TENANT_DOMAIN));
|
|
|
|
|
|
|
|
iotServerConfigs.setDeviceOwner(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.DEVICE_OWNER_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setDeviceId(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.DEVICE_ID_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setDeviceName(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.DEVICE_NAME_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setControllerContext(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.DEVICE_CONTROLLER_CONTEXT_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setScepContext(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.DEVICE_SCEP_CONTEXT_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setHTTPS_ServerEndpoint(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.SERVER_HTTPS_EP_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setHTTP_ServerEndpoint(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.SERVER_HTTP_EP_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setApimGatewayEndpoint(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.APIM_GATEWAY_EP_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setMqttBrokerEndpoint(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.MQTT_BROKER_EP_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setXmppServerEndpoint(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.XMPP_SERVER_EP_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setXmppServerName(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.XMPP_SERVER_NAME_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setApiApplicationKey(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.API_APPLICATION_KEY));
|
|
|
|
|
|
|
|
iotServerConfigs.setAuthMethod(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.AUTH_METHOD_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setAuthToken(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.AUTH_TOKEN_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setRefreshToken(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.REFRESH_TOKEN_PROPERTY));
|
|
|
|
|
|
|
|
iotServerConfigs.setDataPushInterval(Integer.parseInt(properties.getProperty(
|
|
|
|
|
|
|
|
AgentConstants.PUSH_INTERVAL_PROPERTY)));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Tenant Domain: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getTenantDomain());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Owner: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getDeviceOwner());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device ID: " + iotServerConfigs.getDeviceId());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Name: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getDeviceName());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Device Controller Context: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getControllerContext());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "IoT Server HTTPS EndPoint: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getHTTPS_ServerEndpoint());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "IoT Server HTTP EndPoint: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getHTTP_ServerEndpoint());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "API-Manager Gateway EndPoint: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getApimGatewayEndpoint());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "MQTT Broker EndPoint: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getMqttBrokerEndpoint());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "XMPP Server EndPoint: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getXmppServerEndpoint());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Authentication Method: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getAuthMethod());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Base64Encoded API Application Key: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getApiApplicationKey());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Authentication Token: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getAuthToken());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Refresh Token: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getRefreshToken());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Data Push Interval: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getDataPushInterval());
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "XMPP Server Name: " +
|
|
|
|
|
|
|
|
iotServerConfigs.getXmppServerName());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
throw new AgentCoreOperationException(
|
|
|
|
|
|
|
|
"Failed to load path of resource [" + propertiesFileName + "] from this classpath.");
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (FileNotFoundException ex) {
|
|
|
|
} catch (FileNotFoundException ex) {
|
|
|
|
String errorMsg = "[" + propertiesFileName + "] file not found at: " + AgentConstants.PROPERTIES_FILE_PATH;
|
|
|
|
String errorMsg = "[" + propertiesFileName + "] file not found at: " + rootPath;
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
throw new AgentCoreOperationException(errorMsg);
|
|
|
|
throw new AgentCoreOperationException(errorMsg);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
} catch (IOException ex) {
|
|
|
|
String errorMsg = "Error occurred whilst trying to fetch [" + propertiesFileName + "] from: " +
|
|
|
|
String errorMsg = "Error occurred whilst trying to fetch [" + propertiesFileName + "] from: " +
|
|
|
|
AgentConstants.PROPERTIES_FILE_PATH;
|
|
|
|
AgentConstants.PROPERTIES_FILE_PATH;
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
throw new AgentCoreOperationException(errorMsg);
|
|
|
|
throw new AgentCoreOperationException(errorMsg);
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
@ -174,10 +195,6 @@ public class AgentUtilOperations {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* This method constructs the URLs for each of the API Endpoints called by the device agent
|
|
|
|
* This method constructs the URLs for each of the API Endpoints called by the device agent
|
|
|
|
* Ex: Register API, Push-Data API
|
|
|
|
* Ex: Register API, Push-Data API
|
|
|
|
*
|
|
|
|
|
|
|
|
* @throws AgentCoreOperationException if any error occurs at socket level whilst trying to
|
|
|
|
|
|
|
|
* retrieve the deviceIP of the network-interface read
|
|
|
|
|
|
|
|
* from the configs file
|
|
|
|
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public static void initializeServerEndPoints() {
|
|
|
|
public static void initializeServerEndPoints() {
|
|
|
|
AgentManager agentManager = AgentManager.getInstance();
|
|
|
|
AgentManager agentManager = AgentManager.getInstance();
|
|
|
@ -209,6 +226,26 @@ public class AgentUtilOperations {
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Push-Data API EndPoint: " + pushDataEndPointURL);
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Push-Data API EndPoint: " + pushDataEndPointURL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void setHTTPSConfigurations() {
|
|
|
|
|
|
|
|
String apimEndpoint = AgentManager.getInstance().getAgentConfigs().getApimGatewayEndpoint();
|
|
|
|
|
|
|
|
System.setProperty("javax.net.ssl.trustStore", AgentConstants.DEVICE_KEYSTORE);
|
|
|
|
|
|
|
|
System.setProperty("javax.net.ssl.trustStorePassword", AgentConstants.DEVICE_KEYSTORE_PASSWORD);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
final String apimHost = TransportUtils.getHostAndPort(apimEndpoint).get(AgentConstants.HOST_PROPERTY);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
|
|
|
|
|
|
|
|
public boolean verify(String hostname, SSLSession session) {
|
|
|
|
|
|
|
|
return hostname.equals(apimHost);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (TransportHandlerException e) {
|
|
|
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER +
|
|
|
|
|
|
|
|
"Failed to set HTTPS HostNameVerifier to the APIMServer-Host using the APIM-Endpoint " +
|
|
|
|
|
|
|
|
"string [" + apimEndpoint + "].");
|
|
|
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static String prepareSecurePayLoad(String message) throws AgentCoreOperationException {
|
|
|
|
public static String prepareSecurePayLoad(String message) throws AgentCoreOperationException {
|
|
|
|
PrivateKey devicePrivateKey = EnrollmentManager.getInstance().getPrivateKey();
|
|
|
|
PrivateKey devicePrivateKey = EnrollmentManager.getInstance().getPrivateKey();
|
|
|
@ -227,7 +264,6 @@ public class AgentUtilOperations {
|
|
|
|
jsonPayload.put(JSON_SIGNATURE_KEY, signedPayload);
|
|
|
|
jsonPayload.put(JSON_SIGNATURE_KEY, signedPayload);
|
|
|
|
//below statements are temporary fix.
|
|
|
|
//below statements are temporary fix.
|
|
|
|
jsonPayload.put(JSON_SERIAL_KEY, EnrollmentManager.getInstance().getSCEPCertificate().getSerialNumber());
|
|
|
|
jsonPayload.put(JSON_SERIAL_KEY, EnrollmentManager.getInstance().getSCEPCertificate().getSerialNumber());
|
|
|
|
|
|
|
|
|
|
|
|
return jsonPayload.toString();
|
|
|
|
return jsonPayload.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -265,6 +301,138 @@ public class AgentUtilOperations {
|
|
|
|
return actualMessage;
|
|
|
|
return actualMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String getAuthenticationMethod() {
|
|
|
|
|
|
|
|
String authMethod = AgentManager.getInstance().getAgentConfigs().getAuthMethod();
|
|
|
|
|
|
|
|
switch (authMethod) {
|
|
|
|
|
|
|
|
case AgentConstants.TOKEN_AUTHENTICATION_METHOD:
|
|
|
|
|
|
|
|
return AgentConstants.TOKEN_AUTHENTICATION_METHOD;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void refreshOAuthToken() throws AgentCoreOperationException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AgentManager agentManager = AgentManager.getInstance();
|
|
|
|
|
|
|
|
String tokenEndpoint = agentManager.getAgentConfigs().getApimGatewayEndpoint();
|
|
|
|
|
|
|
|
tokenEndpoint = tokenEndpoint + APIManagerTokenUtils.TOKEN_ENDPOINT;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpURLConnection httpConnection = null;
|
|
|
|
|
|
|
|
BufferedReader connectionBuffer = null;
|
|
|
|
|
|
|
|
String requestPayload;
|
|
|
|
|
|
|
|
String dataFromBuffer;
|
|
|
|
|
|
|
|
StringBuilder responseMessage = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
String refreshToken = agentManager.getAgentConfigs().getRefreshToken();
|
|
|
|
|
|
|
|
String applicationScope = "device_type_" + AgentConstants.DEVICE_TYPE +
|
|
|
|
|
|
|
|
" device_" + agentManager.getAgentConfigs().getDeviceId();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requestPayload = APIManagerTokenUtils.GRANT_TYPE + "=" + APIManagerTokenUtils.REFRESH_TOKEN + "&" +
|
|
|
|
|
|
|
|
APIManagerTokenUtils.REFRESH_TOKEN + "=" + refreshToken + "&" +
|
|
|
|
|
|
|
|
APIManagerTokenUtils.SCOPE + "=" + applicationScope;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
httpConnection = TransportUtils.getHttpConnection(tokenEndpoint);
|
|
|
|
|
|
|
|
httpConnection.setRequestMethod(AgentConstants.HTTP_POST);
|
|
|
|
|
|
|
|
httpConnection.setRequestProperty(AgentConstants.AUTHORIZATION_HEADER,
|
|
|
|
|
|
|
|
"Basic " + agentManager.getAgentConfigs().getApiApplicationKey());
|
|
|
|
|
|
|
|
httpConnection.setRequestProperty(AgentConstants.CONTENT_TYPE_HEADER, AgentConstants.X_WWW_FORM_URLENCODED);
|
|
|
|
|
|
|
|
httpConnection.setDoOutput(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
DataOutputStream dataOutPutWriter = new DataOutputStream(httpConnection.getOutputStream());
|
|
|
|
|
|
|
|
dataOutPutWriter.writeBytes(requestPayload);
|
|
|
|
|
|
|
|
dataOutPutWriter.flush();
|
|
|
|
|
|
|
|
dataOutPutWriter.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Request to refresh OAuth token was sent to [" +
|
|
|
|
|
|
|
|
httpConnection.getURL() + "] with payload [" + requestPayload + "].");
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Response [" + httpConnection.getResponseCode() + ":" +
|
|
|
|
|
|
|
|
httpConnection.getResponseMessage() + "] was received for token refresh attempt.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (httpConnection.getResponseCode() == HttpStatus.OK_200) {
|
|
|
|
|
|
|
|
connectionBuffer = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
|
|
|
|
|
|
|
|
while ((dataFromBuffer = connectionBuffer.readLine()) != null) {
|
|
|
|
|
|
|
|
responseMessage.append(dataFromBuffer);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER +
|
|
|
|
|
|
|
|
"Response " + responseMessage + " was received for the token refresh call.");
|
|
|
|
|
|
|
|
updateExistingTokens(responseMessage.toString());
|
|
|
|
|
|
|
|
} else if (httpConnection.getResponseCode() == HttpStatus.BAD_REQUEST_400) {
|
|
|
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER +
|
|
|
|
|
|
|
|
"Token refresh call returned with a [400 Bad Request].\nThe refresh-token has " +
|
|
|
|
|
|
|
|
"probably expired.\nPlease contact System-Admin to get a valid refresh-token.");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.warn(AgentConstants.LOG_APPENDER + "There was an issue with refreshing the Access Token.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (TransportHandlerException e) {
|
|
|
|
|
|
|
|
throw new AgentCoreOperationException(e);
|
|
|
|
|
|
|
|
} catch (ProtocolException e) {
|
|
|
|
|
|
|
|
String errorMsg = "Protocol specific error occurred when trying to set method to " +
|
|
|
|
|
|
|
|
AgentConstants.HTTP_POST + " for endpoint at: " + tokenEndpoint;
|
|
|
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
|
|
|
|
throw new AgentCoreOperationException(errorMsg, e);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
String errorMsg = "An IO error occurred whilst trying to get the response code from: " + tokenEndpoint +
|
|
|
|
|
|
|
|
" for a HTTP " + AgentConstants.HTTP_POST + " call.";
|
|
|
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
|
|
|
|
throw new AgentCoreOperationException(errorMsg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
if (connectionBuffer != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
connectionBuffer.close();
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER +
|
|
|
|
|
|
|
|
"Error encounter whilst attempting to close buffer to connection at: " +
|
|
|
|
|
|
|
|
tokenEndpoint);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (httpConnection != null) {
|
|
|
|
|
|
|
|
httpConnection.disconnect();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void updateExistingTokens(String responseFromTokenEP) throws AgentCoreOperationException {
|
|
|
|
|
|
|
|
JSONObject jsonTokenObject = new JSONObject(responseFromTokenEP);
|
|
|
|
|
|
|
|
String newAccessToken = jsonTokenObject.get(APIManagerTokenUtils.ACCESS_TOKEN).toString();
|
|
|
|
|
|
|
|
String newRefreshToken = jsonTokenObject.get(APIManagerTokenUtils.REFRESH_TOKEN).toString();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (newAccessToken == null || newRefreshToken == null) {
|
|
|
|
|
|
|
|
String msg =
|
|
|
|
|
|
|
|
"Neither Access-Token nor Refresh-Token was found in the response [" + responseFromTokenEP + "].";
|
|
|
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + msg);
|
|
|
|
|
|
|
|
throw new AgentCoreOperationException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AgentManager.getInstance().getAgentConfigs().setAuthToken(newAccessToken);
|
|
|
|
|
|
|
|
AgentManager.getInstance().getAgentConfigs().setRefreshToken(newRefreshToken);
|
|
|
|
|
|
|
|
String deviceConfigFilePath =
|
|
|
|
|
|
|
|
AgentManager.getInstance().getRootPath() + AgentConstants.AGENT_PROPERTIES_FILE_NAME;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
PropertiesConfiguration propertyFileConfiguration = new PropertiesConfiguration(deviceConfigFilePath);
|
|
|
|
|
|
|
|
propertyFileConfiguration.setProperty(AgentConstants.AUTH_TOKEN_PROPERTY, newAccessToken);
|
|
|
|
|
|
|
|
propertyFileConfiguration.setProperty(AgentConstants.REFRESH_TOKEN_PROPERTY, newRefreshToken);
|
|
|
|
|
|
|
|
propertyFileConfiguration.save();
|
|
|
|
|
|
|
|
} catch (ConfigurationException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred whilst trying to update the [" + AgentConstants.AGENT_PROPERTIES_FILE_NAME +
|
|
|
|
|
|
|
|
"] at: " + deviceConfigFilePath + " will the new tokens.";
|
|
|
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + msg);
|
|
|
|
|
|
|
|
throw new AgentCoreOperationException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class APIManagerTokenUtils {
|
|
|
|
|
|
|
|
public static final String TOKEN_ENDPOINT = "/oauth2/token";
|
|
|
|
|
|
|
|
public static final String GRANT_TYPE = "grant_type";
|
|
|
|
|
|
|
|
public static final String ACCESS_TOKEN = "access_token";
|
|
|
|
|
|
|
|
public static final String REFRESH_TOKEN = "refresh_token";
|
|
|
|
|
|
|
|
public static final String SCOPE = "scope";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|