fixed issues related to multi tenant analytics flow and operation flow for reference device types

revert-dabc3590
ayyoob 8 years ago
parent 654bb84c6d
commit 6a1beb8ead

@ -20,7 +20,10 @@
<param-name>doAuthentication</param-name> <param-name>doAuthentication</param-name>
<param-value>true</param-value> <param-value>true</param-value>
</context-param> </context-param>
<context-param>
<param-name>isSharedWithAllTenants</param-name>
<param-value>true</param-value>
</context-param>
<!--publish to apim--> <!--publish to apim-->
<context-param> <context-param>
<param-name>managed-api-enabled</param-name> <param-name>managed-api-enabled</param-name>

@ -31,12 +31,23 @@ function onRequest(context) {
if (encodedClientKeys) { if (encodedClientKeys) {
var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"]; var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"];
var resp = tokenUtil.decode(encodedClientKeys).split(":"); var resp = tokenUtil.decode(encodedClientKeys).split(":");
if (user.domain == "carbon.super") {
var tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username , "default", {}); var tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username , "default", {});
if (tokenPair) { if (tokenPair) {
token = tokenPair.accessToken; token = tokenPair.accessToken;
} }
websocketEndpoint = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.temperature/1.0.0?" websocketEndpoint = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.temperature/1.0.0?"
+ "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token; + "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token;
} else {
var tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username + "@" + user.domain
, "default", {});
if (tokenPair) {
token = tokenPair.accessToken;
}
websocketEndpoint = websocketEndpoint + "/secured-websocket/t/" + user.domain + "/org.wso2.iot.devices.temperature/1.0.0?"
+ "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token;
}
} }
return {"device": device, "websocketEndpoint": websocketEndpoint}; return {"device": device, "websocketEndpoint": websocketEndpoint};
} }

@ -28,15 +28,32 @@ function onRequest(context) {
var jwtClient = jwtService.getJWTClient(); var jwtClient = jwtService.getJWTClient();
var encodedClientKeys = session.get(constants["ENCODED_TENANT_BASED_WEB_SOCKET_CLIENT_CREDENTIALS"]); var encodedClientKeys = session.get(constants["ENCODED_TENANT_BASED_WEB_SOCKET_CLIENT_CREDENTIALS"]);
var token = ""; var token = "";
var user = session.get(constants.USER_SESSION_KEY);
if (!user) {
log.error("User object was not found in the session");
throw constants.ERRORS.USER_NOT_FOUND;
}
if (encodedClientKeys) { if (encodedClientKeys) {
var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"]; var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"];
var resp = tokenUtil.decode(encodedClientKeys).split(":"); var resp = tokenUtil.decode(encodedClientKeys).split(":");
if (user.domain == "carbon.super") {
var tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username , "default", {}); var tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username , "default", {});
if (tokenPair) { if (tokenPair) {
token = tokenPair.accessToken; token = tokenPair.accessToken;
} }
websocketEndpoint = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.temperature/1.0.0?" websocketEndpoint = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.temperature/1.0.0?"
+ "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token; + "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token;
} else {
var tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username + "@" + user.domain
, "default", {});
if (tokenPair) {
token = tokenPair.accessToken;
}
websocketEndpoint = websocketEndpoint + "/secured-websocket/t/" + user.domain + "/org.wso2.iot.devices.temperature/1.0.0?"
+ "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token;
}
} }
return {"device": device, "websocketEndpoint": websocketEndpoint}; return {"device": device, "websocketEndpoint": websocketEndpoint};
} }

@ -20,6 +20,10 @@
<param-name>doAuthentication</param-name> <param-name>doAuthentication</param-name>
<param-value>true</param-value> <param-value>true</param-value>
</context-param> </context-param>
<context-param>
<param-name>isSharedWithAllTenants</param-name>
<param-value>true</param-value>
</context-param>
<!--publish to apim--> <!--publish to apim-->
<context-param> <context-param>

@ -28,15 +28,31 @@ function onRequest(context) {
var jwtClient = jwtService.getJWTClient(); var jwtClient = jwtService.getJWTClient();
var encodedClientKeys = session.get(constants["ENCODED_TENANT_BASED_WEB_SOCKET_CLIENT_CREDENTIALS"]); var encodedClientKeys = session.get(constants["ENCODED_TENANT_BASED_WEB_SOCKET_CLIENT_CREDENTIALS"]);
var token = ""; var token = "";
var user = session.get(constants.USER_SESSION_KEY);
if (!user) {
log.error("User object was not found in the session");
throw constants.ERRORS.USER_NOT_FOUND;
}
if (encodedClientKeys) { if (encodedClientKeys) {
var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"]; var tokenUtil = require("/app/modules/oauth/token-handler-utils.js")["utils"];
var resp = tokenUtil.decode(encodedClientKeys).split(":"); var resp = tokenUtil.decode(encodedClientKeys).split(":");
if (user.domain == "carbon.super") {
var tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username , "default", {}); var tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username , "default", {});
if (tokenPair) { if (tokenPair) {
token = tokenPair.accessToken; token = tokenPair.accessToken;
} }
websocketEndpoint = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.temperature/1.0.0?" websocketEndpoint = websocketEndpoint + "/secured-websocket/org.wso2.iot.devices.temperature/1.0.0?"
+ "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token; + "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token;
} else {
var tokenPair = jwtClient.getAccessToken(resp[0], resp[1], context.user.username + "@" + user.domain
, "default", {});
if (tokenPair) {
token = tokenPair.accessToken;
}
websocketEndpoint = websocketEndpoint + "/secured-websocket/t/" + user.domain + "/org.wso2.iot.devices.temperature/1.0.0?"
+ "deviceId=" + device.deviceIdentifier + "&deviceType=" + device.type + "&websocketToken=" + token;
}
} }
return {"device": device, "websocketEndpoint": websocketEndpoint}; return {"device": device, "websocketEndpoint": websocketEndpoint};
} }

@ -204,7 +204,12 @@ public class MQTTAdapterListener implements MqttCallback, Runnable {
log.warn("Broker is unreachable, Waiting....."); log.warn("Broker is unreachable, Waiting.....");
return false; return false;
} }
try {
mqttClient.subscribe(topic); mqttClient.subscribe(topic);
} catch (MqttException e) {
log.error("Failed to subscribe to topic: " + topic + ", Retrying.....");
return false;
}
return true; return true;
} }

@ -77,12 +77,14 @@ public class DeviceAccessBasedMQTTAuthorizer implements IAuthorizer {
private static final String CACHE_MANAGER_NAME = "mqttAuthorizationCacheManager"; private static final String CACHE_MANAGER_NAME = "mqttAuthorizationCacheManager";
private static final String CACHE_NAME = "mqttAuthorizationCache"; private static final String CACHE_NAME = "mqttAuthorizationCache";
private static DeviceAccessAuthorizationAdminService deviceAccessAuthorizationAdminService; private static DeviceAccessAuthorizationAdminService deviceAccessAuthorizationAdminService;
private static OAuthRequestInterceptor oAuthRequestInterceptor;
private static final String GATEWAY_ERROR_CODE = "<am:code>404</am:code>";
public DeviceAccessBasedMQTTAuthorizer() { public DeviceAccessBasedMQTTAuthorizer() {
oAuthRequestInterceptor = new OAuthRequestInterceptor();
this.MQTTAuthorizationConfiguration = AuthorizationConfigurationManager.getInstance(); this.MQTTAuthorizationConfiguration = AuthorizationConfigurationManager.getInstance();
deviceAccessAuthorizationAdminService = Feign.builder().client(getSSLClient()).logger(new Slf4jLogger()) deviceAccessAuthorizationAdminService = Feign.builder().client(getSSLClient()).logger(new Slf4jLogger())
.logLevel(Logger.Level.FULL).requestInterceptor(new OAuthRequestInterceptor()) .logLevel(Logger.Level.FULL).requestInterceptor(oAuthRequestInterceptor)
.contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder()) .contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder())
.target(DeviceAccessAuthorizationAdminService.class, .target(DeviceAccessAuthorizationAdminService.class,
MQTTAuthorizationConfiguration.getDeviceMgtServerUrl() + CDMF_SERVER_BASE_CONTEXT); MQTTAuthorizationConfiguration.getDeviceMgtServerUrl() + CDMF_SERVER_BASE_CONTEXT);
@ -121,7 +123,12 @@ public class DeviceAccessBasedMQTTAuthorizer implements IAuthorizer {
} }
return false; return false;
} catch (FeignException e) { } catch (FeignException e) {
oAuthRequestInterceptor.resetApiApplicationKey();
if (e.getMessage().contains(GATEWAY_ERROR_CODE)) {
log.error("Failed to connect to the device authorization service.");
} else {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
}
return false; return false;
} }
} }
@ -164,6 +171,12 @@ public class DeviceAccessBasedMQTTAuthorizer implements IAuthorizer {
} }
} }
} catch (FeignException e) { } catch (FeignException e) {
oAuthRequestInterceptor.resetApiApplicationKey();
if (e.getMessage().contains(GATEWAY_ERROR_CODE)) {
log.error("Failed to connect to the device authorization service.");
} else {
log.error(e.getMessage(), e);
}
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
} }
} finally { } finally {

@ -123,6 +123,10 @@ public class OAuthRequestInterceptor implements RequestInterceptor {
template.header("Authorization", headerValue); template.header("Authorization", headerValue);
} }
public void resetApiApplicationKey() {
apiApplicationKey = null;
}
private static Client getSSLClient() { private static Client getSSLClient() {
return new Client.Default(getTrustedSSLSocketFactory(), new HostnameVerifier() { return new Client.Default(getTrustedSSLSocketFactory(), new HostnameVerifier() {
@Override @Override

@ -31,7 +31,7 @@
</Features> </Features>
<ProvisioningConfig> <ProvisioningConfig>
<SharedWithAllTenants>false</SharedWithAllTenants> <SharedWithAllTenants>true</SharedWithAllTenants>
</ProvisioningConfig> </ProvisioningConfig>
<License> <License>

@ -31,7 +31,7 @@
</Features> </Features>
<ProvisioningConfig> <ProvisioningConfig>
<SharedWithAllTenants>false</SharedWithAllTenants> <SharedWithAllTenants>true</SharedWithAllTenants>
</ProvisioningConfig> </ProvisioningConfig>
<License> <License>

@ -31,7 +31,7 @@
</Features> </Features>
<ProvisioningConfig> <ProvisioningConfig>
<SharedWithAllTenants>false</SharedWithAllTenants> <SharedWithAllTenants>true</SharedWithAllTenants>
</ProvisioningConfig> </ProvisioningConfig>
<PushNotificationProvider type="MQTT"> <PushNotificationProvider type="MQTT">

@ -31,7 +31,7 @@
</Features> </Features>
<ProvisioningConfig> <ProvisioningConfig>
<SharedWithAllTenants>false</SharedWithAllTenants> <SharedWithAllTenants>true</SharedWithAllTenants>
</ProvisioningConfig> </ProvisioningConfig>
<PushNotificationProvider type="MQTT"> <PushNotificationProvider type="MQTT">

Loading…
Cancel
Save