@ -1,5 +1,5 @@
- -
- -
-- Copyright © 2016-2021 The Thingsboard Authors
-- Copyright © 2016-2022 The Thingsboard Authors
- -
- -
-- Licensed under the Apache License, Version 2.0 (the "License");
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- you may not use this file except in compliance with the License.
@ -14,6 +14,7 @@
-- limitations under the License.
-- limitations under the License.
- -
- -
CREATE OR REPLACE PROCEDURE update_profile_bootstrap ( )
CREATE OR REPLACE PROCEDURE update_profile_bootstrap ( )
LANGUAGE plpgsql AS
LANGUAGE plpgsql AS
$ $
$ $
@ -25,9 +26,11 @@ BEGIN
profile_data ,
profile_data ,
' {transportConfiguration} ' ,
' {transportConfiguration} ' ,
get_bootstrap (
get_bootstrap (
profile_data : : jsonb #> '{transportConfiguration}',
profile_data : : jsonb #> '{transportConfiguration}',
subquery . publickey_bs ,
subquery . publickey_bs ,
subquery . publickey_lw ) ,
subquery . publickey_lw ,
profile_data : : json #>> '{transportConfiguration, bootstrap, bootstrapServer, securityMode}',
profile_data : : json #>> '{transportConfiguration, bootstrap, lwm2mServer, securityMode}'),
true )
true )
FROM (
FROM (
SELECT id ,
SELECT id ,
@ -48,7 +51,8 @@ END;
$ $ ;
$ $ ;
CREATE OR REPLACE FUNCTION get_bootstrap ( transport_configuration_in jsonb , publickey_bs text ,
CREATE OR REPLACE FUNCTION get_bootstrap ( transport_configuration_in jsonb , publickey_bs text ,
publickey_lw text ) RETURNS jsonb AS
publickey_lw text , security_mode_bs text ,
security_mode_lw text ) RETURNS jsonb AS
$ $
$ $
DECLARE
DECLARE
@ -56,10 +60,19 @@ DECLARE
bootstrap_in jsonb ;
bootstrap_in jsonb ;
BEGIN
BEGIN
IF security_mode_lw IS NULL THEN
security_mode_lw : = ' NO_SEC ' ;
END IF ;
IF security_mode_bs IS NULL THEN
security_mode_bs : = ' NO_SEC ' ;
END IF ;
bootstrap_in : = transport_configuration_in : : jsonb #> '{bootstrap}';
bootstrap_in : = transport_configuration_in : : jsonb #> '{bootstrap}';
bootstrap_new : = json_build_array (
bootstrap_new : = json_build_array (
json_build_object ( ' shortServerId ' , bootstrap_in : : json #> '{bootstrapServer}' -> 'serverId',
json_build_object ( ' shortServerId ' , bootstrap_in : : json #> '{bootstrapServer}' -> 'serverId',
' securityMode ' , bootstrap_in : : json #> '{bootstrapServer}' ->> 'securityMode',
' securityMode ' , security_mode_bs ,
' binding ' , bootstrap_in : : json #> '{servers}' ->> 'binding',
' binding ' , bootstrap_in : : json #> '{servers}' ->> 'binding',
' lifetime ' , bootstrap_in : : json #> '{servers}' -> 'lifetime',
' lifetime ' , bootstrap_in : : json #> '{servers}' -> 'lifetime',
' notifIfDisabled ' , bootstrap_in : : json #> '{servers}' -> 'notifIfDisabled',
' notifIfDisabled ' , bootstrap_in : : json #> '{servers}' -> 'notifIfDisabled',
@ -73,7 +86,7 @@ BEGIN
bootstrap_in : : json #> '{bootstrapServer}' -> 'bootstrapServerAccountTimeout'
bootstrap_in : : json #> '{bootstrapServer}' -> 'bootstrapServerAccountTimeout'
) ,
) ,
json_build_object ( ' shortServerId ' , bootstrap_in : : json #> '{lwm2mServer}' -> 'serverId',
json_build_object ( ' shortServerId ' , bootstrap_in : : json #> '{lwm2mServer}' -> 'serverId',
' securityMode ' , bootstrap_in : : json #> '{lwm2mServer}' ->> 'securityMode' ,
' securityMode ' , security_mode_lw ,
' binding ' , bootstrap_in : : json #> '{servers}' ->> 'binding',
' binding ' , bootstrap_in : : json #> '{servers}' ->> 'binding',
' lifetime ' , bootstrap_in : : json #> '{servers}' -> 'lifetime',
' lifetime ' , bootstrap_in : : json #> '{servers}' -> 'lifetime',
' notifIfDisabled ' , bootstrap_in : : json #> '{servers}' -> 'notifIfDisabled',
' notifIfDisabled ' , bootstrap_in : : json #> '{servers}' -> 'notifIfDisabled',
@ -93,7 +106,7 @@ BEGIN
bootstrap_new ,
bootstrap_new ,
true ) | | ' {"bootstrapServerUpdateEnable": true} ' ;
true ) | | ' {"bootstrapServerUpdateEnable": true} ' ;
END ;
END ;
$ $ LANGUAGE plpgsql ;
$ $ LANGUAGE plpgsql ;
CREATE OR REPLACE PROCEDURE update_device_credentials_to_base64_and_bootstrap ( )
CREATE OR REPLACE PROCEDURE update_device_credentials_to_base64_and_bootstrap ( )
@ -102,9 +115,9 @@ $$
BEGIN
BEGIN
UPDATE device_credentials
UPDATE device_credentials
SET credentials_value = get_device_and_bootstrap ( credentials_value : : text )
SET credentials_value = get_device_and_bootstrap ( credentials_value : : text )
WHERE credentials_type = ' LWM2M_CREDENTIALS ' ;
WHERE credentials_type = ' LWM2M_CREDENTIALS ' ;
END ;
END ;
$ $ ;
$ $ ;
@ -112,7 +125,7 @@ CREATE OR REPLACE FUNCTION get_device_and_bootstrap(IN credentials_value text, O
LANGUAGE plpgsql AS
LANGUAGE plpgsql AS
$ $
$ $
DECLARE
DECLARE
client_secret_key text ;
client_secret_key text ;
client_public_key_or_id text ;
client_public_key_or_id text ;
client_key_value_object jsonb ;
client_key_value_object jsonb ;
client_bootstrap_server_value_object jsonb ;
client_bootstrap_server_value_object jsonb ;
@ -130,7 +143,7 @@ BEGIN
' key ' , client_public_key_or_id ) ;
' key ' , client_public_key_or_id ) ;
credentials_value_new : =
credentials_value_new : =
credentials_value_new : : jsonb | | json_build_object ( ' client ' , client_key_value_object ) : : jsonb ;
credentials_value_new : : jsonb | | json_build_object ( ' client ' , client_key_value_object ) : : jsonb ;
END IF ;
END IF ;
IF credentials_value : : jsonb #> '{client}' ->> 'securityConfigClientMode' = 'X509' AND
IF credentials_value : : jsonb #> '{client}' ->> 'securityConfigClientMode' = 'X509' AND
NULL IF ( ( credentials_value : : jsonb #> '{client}' ->> 'cert' ~ '^[0-9a-fA-F]+$')::text, 'false') = 'true' THEN
NULL IF ( ( credentials_value : : jsonb #> '{client}' ->> 'cert' ~ '^[0-9a-fA-F]+$')::text, 'false') = 'true' THEN
client_public_key_or_id : =
client_public_key_or_id : =
@ -141,8 +154,8 @@ END IF;
' cert ' , client_public_key_or_id ) ;
' cert ' , client_public_key_or_id ) ;
credentials_value_new : =
credentials_value_new : =
credentials_value_new : : jsonb | | json_build_object ( ' client ' , client_key_value_object ) : : jsonb ;
credentials_value_new : : jsonb | | json_build_object ( ' client ' , client_key_value_object ) : : jsonb ;
END IF ;
END IF ;
IF credentials_value : : jsonb #> '{bootstrap,lwm2mServer}' ->> 'securityMode' = 'RPK' OR
IF credentials_value : : jsonb #> '{bootstrap,lwm2mServer}' ->> 'securityMode' = 'RPK' OR
credentials_value : : jsonb #> '{bootstrap,lwm2mServer}' ->> 'securityMode' = 'X509' THEN
credentials_value : : jsonb #> '{bootstrap,lwm2mServer}' ->> 'securityMode' = 'X509' THEN
IF NULL IF ( ( credentials_value : : jsonb #> '{bootstrap,lwm2mServer}' ->> 'clientSecretKey' ~ '^[0-9a-fA-F]+$')::text,
IF NULL IF ( ( credentials_value : : jsonb #> '{bootstrap,lwm2mServer}' ->> 'clientSecretKey' ~ '^[0-9a-fA-F]+$')::text,
@ -165,9 +178,9 @@ END IF;
client_bootstrap_object : = credentials_value_new : : jsonb #> '{bootstrap}' || client_bootstrap_server_object::jsonb;
client_bootstrap_object : = credentials_value_new : : jsonb #> '{bootstrap}' || client_bootstrap_server_object::jsonb;
credentials_value_new : =
credentials_value_new : =
jsonb_set ( credentials_value_new : : jsonb , ' {bootstrap} ' , client_bootstrap_object : : jsonb , false ) : : jsonb ;
jsonb_set ( credentials_value_new : : jsonb , ' {bootstrap} ' , client_bootstrap_object : : jsonb , false ) : : jsonb ;
END IF ;
END IF ;
END IF ;
END IF ;
IF credentials_value : : jsonb #> '{bootstrap,bootstrapServer}' ->> 'securityMode' = 'RPK' OR
IF credentials_value : : jsonb #> '{bootstrap,bootstrapServer}' ->> 'securityMode' = 'RPK' OR
credentials_value : : jsonb #> '{bootstrap,bootstrapServer}' ->> 'securityMode' = 'X509' THEN
credentials_value : : jsonb #> '{bootstrap,bootstrapServer}' ->> 'securityMode' = 'X509' THEN
IF NULL IF (
IF NULL IF (
@ -193,8 +206,8 @@ END IF;
client_bootstrap_object : = credentials_value_new : : jsonb #> '{bootstrap}' || client_bootstrap_server_object::jsonb;
client_bootstrap_object : = credentials_value_new : : jsonb #> '{bootstrap}' || client_bootstrap_server_object::jsonb;
credentials_value_new : =
credentials_value_new : =
jsonb_set ( credentials_value_new : : jsonb , ' {bootstrap} ' , client_bootstrap_object : : jsonb , false ) : : jsonb ;
jsonb_set ( credentials_value_new : : jsonb , ' {bootstrap} ' , client_bootstrap_object : : jsonb , false ) : : jsonb ;
END IF ;
END IF ;
END IF ;
END IF ;
END ;
END ;
$ $ ;
$ $ ;