|
|
@ -19,8 +19,11 @@
|
|
|
|
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;
|
|
|
@ -28,11 +31,13 @@ import org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.Communica
|
|
|
|
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 org.wso2.carbon.device.mgt.iot.virtualfirealarm.agent.transport.TransportUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.net.ssl.HostnameVerifier;
|
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
|
|
|
import javax.net.ssl.SSLSession;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.DataOutputStream;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
@ -78,7 +83,9 @@ public class AgentUtilOperations {
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
if (path != null) {
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + path);
|
|
|
|
rootPath = path.getPath().replace("wso2-firealarm-virtual-agent.jar!/deviceConfig.properties", "")
|
|
|
|
rootPath = path.getPath().replace("wso2-firealarm-virtual-agent.jar!/deviceConfig.properties", "")
|
|
|
|
.replace("jar:", "").replace("file:", "");
|
|
|
|
.replace("jar:", "").replace("file:", "");
|
|
|
|
|
|
|
|
|
|
|
@ -157,7 +164,10 @@ public class AgentUtilOperations {
|
|
|
|
iotServerConfigs.getDataPushInterval());
|
|
|
|
iotServerConfigs.getDataPushInterval());
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "XMPP Server Name: " +
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "XMPP Server Name: " +
|
|
|
|
iotServerConfigs.getXmppServerName());
|
|
|
|
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: " + rootPath;
|
|
|
|
String errorMsg = "[" + propertiesFileName + "] file not found at: " + rootPath;
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
@ -216,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();
|
|
|
@ -234,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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -282,26 +311,19 @@ public class AgentUtilOperations {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void refreshOAuthToken() throws AgentCoreOperationException {
|
|
|
|
public static boolean refreshOAuthToken() throws AgentCoreOperationException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AgentManager agentManager = AgentManager.getInstance();
|
|
|
|
AgentManager agentManager = AgentManager.getInstance();
|
|
|
|
String tokenEndpoint = agentManager.getAgentConfigs().getApimGatewayEndpoint() + "/token";
|
|
|
|
String tokenEndpoint = agentManager.getAgentConfigs().getApimGatewayEndpoint();
|
|
|
|
|
|
|
|
tokenEndpoint = tokenEndpoint + APIManagerTokenUtils.TOKEN_ENDPOINT;
|
|
|
|
|
|
|
|
|
|
|
|
HttpURLConnection httpConnection = null;
|
|
|
|
HttpURLConnection httpConnection = null;
|
|
|
|
BufferedReader connectionBuffer = null;
|
|
|
|
BufferedReader connectionBuffer = null;
|
|
|
|
String requestPayload;
|
|
|
|
String requestPayload;
|
|
|
|
String dataFromBuffer;
|
|
|
|
String dataFromBuffer;
|
|
|
|
StringBuilder responseMessage = new StringBuilder();
|
|
|
|
StringBuilder responseMessage = new StringBuilder();
|
|
|
|
boolean refreshStatus = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
httpConnection = TransportUtils.getHttpConnection(tokenEndpoint);
|
|
|
|
|
|
|
|
httpConnection.setRequestMethod(AgentConstants.HTTP_POST);
|
|
|
|
|
|
|
|
httpConnection.setRequestProperty(AgentConstants.AUTHORIZATION_HEADER,
|
|
|
|
|
|
|
|
"Bearer " + agentManager.getAgentConfigs().getApiApplicationKey());
|
|
|
|
|
|
|
|
httpConnection.setRequestProperty(AgentConstants.CONTENT_TYPE_HEADER, AgentConstants.X_WWW_FORM_URLENCODED);
|
|
|
|
|
|
|
|
httpConnection.setDoOutput(true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String refreshToken = agentManager.getAgentConfigs().getRefreshToken();
|
|
|
|
String refreshToken = agentManager.getAgentConfigs().getRefreshToken();
|
|
|
|
String applicationScope = "device_type_" + AgentConstants.DEVICE_TYPE +
|
|
|
|
String applicationScope = "device_type_" + AgentConstants.DEVICE_TYPE +
|
|
|
|
" device_" + agentManager.getAgentConfigs().getDeviceId();
|
|
|
|
" device_" + agentManager.getAgentConfigs().getDeviceId();
|
|
|
@ -310,6 +332,13 @@ public class AgentUtilOperations {
|
|
|
|
APIManagerTokenUtils.REFRESH_TOKEN + "=" + refreshToken + "&" +
|
|
|
|
APIManagerTokenUtils.REFRESH_TOKEN + "=" + refreshToken + "&" +
|
|
|
|
APIManagerTokenUtils.SCOPE + "=" + applicationScope;
|
|
|
|
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());
|
|
|
|
DataOutputStream dataOutPutWriter = new DataOutputStream(httpConnection.getOutputStream());
|
|
|
|
dataOutPutWriter.writeBytes(requestPayload);
|
|
|
|
dataOutPutWriter.writeBytes(requestPayload);
|
|
|
|
dataOutPutWriter.flush();
|
|
|
|
dataOutPutWriter.flush();
|
|
|
@ -320,16 +349,18 @@ public class AgentUtilOperations {
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Response [" + httpConnection.getResponseCode() + ":" +
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Response [" + httpConnection.getResponseCode() + ":" +
|
|
|
|
httpConnection.getResponseMessage() + "] was received for token refresh attempt.");
|
|
|
|
httpConnection.getResponseMessage() + "] was received for token refresh attempt.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (httpConnection.getResponseCode() == HttpStatus.OK_200) {
|
|
|
|
connectionBuffer = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
|
|
|
|
connectionBuffer = new BufferedReader(new InputStreamReader(httpConnection.getInputStream()));
|
|
|
|
|
|
|
|
|
|
|
|
while ((dataFromBuffer = connectionBuffer.readLine()) != null) {
|
|
|
|
while ((dataFromBuffer = connectionBuffer.readLine()) != null) {
|
|
|
|
responseMessage.append(dataFromBuffer);
|
|
|
|
responseMessage.append(dataFromBuffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "Response [" + responseMessage +
|
|
|
|
log.info(AgentConstants.LOG_APPENDER +
|
|
|
|
"] was received for the token refresh call.");
|
|
|
|
"Response " + responseMessage + " was received for the token refresh call.");
|
|
|
|
|
|
|
|
updateExistingTokens(responseMessage.toString());
|
|
|
|
refreshStatus = updateExistingTokens(responseMessage.toString());
|
|
|
|
} else {
|
|
|
|
|
|
|
|
log.info(AgentConstants.LOG_APPENDER + "There was an issue with refreshing the Access Token.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} catch (TransportHandlerException e) {
|
|
|
|
} catch (TransportHandlerException e) {
|
|
|
|
throw new AgentCoreOperationException(e);
|
|
|
|
throw new AgentCoreOperationException(e);
|
|
|
@ -359,20 +390,18 @@ public class AgentUtilOperations {
|
|
|
|
httpConnection.disconnect();
|
|
|
|
httpConnection.disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return refreshStatus;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static void updateExistingTokens(String responseFromTokenEP) throws AgentCoreOperationException {
|
|
|
|
private static boolean updateExistingTokens(String responseFromTokenEP) {
|
|
|
|
|
|
|
|
JSONObject jsonTokenObject = new JSONObject(responseFromTokenEP);
|
|
|
|
JSONObject jsonTokenObject = new JSONObject(responseFromTokenEP);
|
|
|
|
String newAccessToken = jsonTokenObject.get(APIManagerTokenUtils.ACCESS_TOKEN).toString();
|
|
|
|
String newAccessToken = jsonTokenObject.get(APIManagerTokenUtils.ACCESS_TOKEN).toString();
|
|
|
|
String newRefreshToken = jsonTokenObject.get(APIManagerTokenUtils.REFRESH_TOKEN).toString();
|
|
|
|
String newRefreshToken = jsonTokenObject.get(APIManagerTokenUtils.REFRESH_TOKEN).toString();
|
|
|
|
|
|
|
|
|
|
|
|
if (newAccessToken == null || newRefreshToken == null) {
|
|
|
|
if (newAccessToken == null || newRefreshToken == null) {
|
|
|
|
log.error(
|
|
|
|
String msg =
|
|
|
|
AgentConstants.LOG_APPENDER + "Neither Access-Token nor Refresh-Token was found in the response [" +
|
|
|
|
"Neither Access-Token nor Refresh-Token was found in the response [" + responseFromTokenEP + "].";
|
|
|
|
responseFromTokenEP + "].");
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + msg);
|
|
|
|
return false;
|
|
|
|
throw new AgentCoreOperationException(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
AgentManager.getInstance().getAgentConfigs().setAuthToken(newAccessToken);
|
|
|
|
AgentManager.getInstance().getAgentConfigs().setAuthToken(newAccessToken);
|
|
|
@ -380,42 +409,21 @@ public class AgentUtilOperations {
|
|
|
|
String deviceConfigFilePath =
|
|
|
|
String deviceConfigFilePath =
|
|
|
|
AgentManager.getInstance().getRootPath() + AgentConstants.AGENT_PROPERTIES_FILE_NAME;
|
|
|
|
AgentManager.getInstance().getRootPath() + AgentConstants.AGENT_PROPERTIES_FILE_NAME;
|
|
|
|
|
|
|
|
|
|
|
|
Properties deviceProperties = new Properties();
|
|
|
|
|
|
|
|
FileOutputStream fileOutputStream = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
fileOutputStream = new FileOutputStream(deviceConfigFilePath);
|
|
|
|
|
|
|
|
deviceProperties.setProperty(AgentConstants.AUTH_TOKEN_PROPERTY, newAccessToken);
|
|
|
|
|
|
|
|
deviceProperties.setProperty(AgentConstants.REFRESH_TOKEN_PROPERTY, newRefreshToken);
|
|
|
|
|
|
|
|
deviceProperties.store(fileOutputStream, null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (FileNotFoundException ex) {
|
|
|
|
|
|
|
|
String errorMsg =
|
|
|
|
|
|
|
|
"[" + AgentConstants.AGENT_PROPERTIES_FILE_NAME + "] file not found at: " + deviceConfigFilePath;
|
|
|
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException ex) {
|
|
|
|
|
|
|
|
String errorMsg = "Error occurred whilst trying to write to [" + AgentConstants.AGENT_PROPERTIES_FILE_NAME +
|
|
|
|
|
|
|
|
"] at: " + deviceConfigFilePath;
|
|
|
|
|
|
|
|
log.error(AgentConstants.LOG_APPENDER + errorMsg);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
if (fileOutputStream != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
fileOutputStream.close();
|
|
|
|
PropertiesConfiguration propertyFileConfiguration = new PropertiesConfiguration(deviceConfigFilePath);
|
|
|
|
} catch (IOException e) {
|
|
|
|
propertyFileConfiguration.setProperty(AgentConstants.AUTH_TOKEN_PROPERTY, newAccessToken);
|
|
|
|
log.error(AgentConstants.LOG_APPENDER +
|
|
|
|
propertyFileConfiguration.setProperty(AgentConstants.REFRESH_TOKEN_PROPERTY, newRefreshToken);
|
|
|
|
"Error occurred whilst trying to close InputStream resource used to read the '" +
|
|
|
|
propertyFileConfiguration.save();
|
|
|
|
AgentConstants.AGENT_PROPERTIES_FILE_NAME + "' file");
|
|
|
|
} 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class APIManagerTokenUtils {
|
|
|
|
private class APIManagerTokenUtils {
|
|
|
|
|
|
|
|
public static final String TOKEN_ENDPOINT = "/oauth2/token";
|
|
|
|
public static final String GRANT_TYPE = "grant_type";
|
|
|
|
public static final String GRANT_TYPE = "grant_type";
|
|
|
|
public static final String ACCESS_TOKEN = "access_token";
|
|
|
|
public static final String ACCESS_TOKEN = "access_token";
|
|
|
|
public static final String REFRESH_TOKEN = "refresh_token";
|
|
|
|
public static final String REFRESH_TOKEN = "refresh_token";
|
|
|
|