Skip to content

Commit c6f1906

Browse files
emqx-ci-robotSwilder-M
authored andcommitted
sync blog
1 parent b362230 commit c6f1906

File tree

4 files changed

+289
-0
lines changed

4 files changed

+289
-0
lines changed

README-ZH.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ MQTTX is a Fully Open-source MQTT 5.0 cross-platform Desktop Client, makes it ea
570570
## [Neuron - IIoT Connectivity Server](https://www.emqx.com/zh/blog/category/neuron)
571571
IoT edge industrial protocol gateway software, which supports one-stop access to dozens of industrial protocols and converts them into MQTT protocol to access the cloud industrial IoT platform. It just requires ultra-low resource consumption, and supports three major architectures of X86, ARM, and MIPS.
572572

573+
- [工业边缘网关软件 NeuronEX 中基于角色的访问控制功能](https://www.emqx.com/zh/blog/neuronex-role-based-access-control) ([Edit](https://github.com/emqx/blog/blob/main/zh/202408/neuronex-role-based-access-control.md))
573574
- [灵活数据流处理:NeuronEX 支持 JavaScript 自定义函数](https://www.emqx.com/zh/blog/neuronex-custom-function) ([Edit](https://github.com/emqx/blog/blob/main/zh/202407/neuronex-custom-function.md))
574575
- [NeuronEX 3.3.0 发布:工业数据采集、分析和管理多项升级](https://www.emqx.com/zh/blog/neuronex-v-3-3-0-release-notes) ([Edit](https://github.com/emqx/blog/blob/main/zh/202406/neuronex-v-3-3-0-release-notes.md))
575576
- [工业边缘网关软件 NeuronEX 中的反向代理和进程管理](https://www.emqx.com/zh/blog/proxy-and-process-management-in-neuronex) ([Edit](https://github.com/emqx/blog/blob/main/zh/202405/proxy-and-process-management-in-neuronex.md))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ The easiest way to start MQTT service. Connect your IoT devices to any cloud wit
387387
## [EMQX Enterprise](https://www.emqx.com/en/blog/category/enterprise)
388388
The world's most scalable and reliable MQTT messaging platform to connect, move and process your data in business-critical scenarios for the IoT era.
389389

390+
- [Enhancing Data Integration Stability: A Guide on EMQX Platform E2E Rule Testing](https://www.emqx.com/en/blog/emqx-platform-e2e-rule-testing-guide) ([Edit](https://github.com/emqx/blog/blob/main/en/202408/emqx-platform-e2e-rule-testing-guide.md))
390391
- [EMQX Enterprise 5.7 Released: Durable Sessions, Message Schema Validation, and Advanced Rule Engine Capabilities](https://www.emqx.com/en/blog/emqx-enterprise-5-7-release-notes) ([Edit](https://github.com/emqx/blog/blob/main/en/202405/emqx-enterprise-5-7-release-notes.md))
391392
- [EMQX Enterprise 5.6 Released: Introducing Amazon S3 Data Integration and JSON Schema SQL Validation Function](https://www.emqx.com/en/blog/emqx-enterprise-5-6-release-notes) ([Edit](https://github.com/emqx/blog/blob/main/en/202404/emqx-enterprise-5-6-release-notes.md))
392393
- [MQTT to MongoDB: A Beginner's Guide for IoT Data Integration](https://www.emqx.com/en/blog/mqtt-and-mongodb-crafting-seamless-synergy-for-iot-data-mangement) ([Edit](https://github.com/emqx/blog/blob/main/en/202403/mqtt-and-mongodb-crafting-seamless-synergy-for-iot-data-mangement.md))
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
Since version 5.7.0, EMQX has supported SQL debugging and introduced debugging for the rules within the entire data integration process. This new feature allows users to comprehensively verify and optimize rules during the development phase, ensuring that the rules operate stably and efficiently in a production environment.
2+
3+
> Download EMQX latest version here: <https://www.emqx.com/en/try?tab=self-managed>
4+
5+
This blog will guide you through debugging rules within EMQX's data integration, providing a step-by-step tutorial to help you make the most of this powerful feature.
6+
7+
## What is the EMQX Rule Engine?
8+
9+
The EMQX rule engine is an SQL-based data processing component that uses data integration to allow users to perform IoT data extraction, filtering, transformation, storage, and processing without writing code.
10+
11+
![EMQX Platform Architecture](https://assets.emqx.com/images/d50a4cb1cf20e5c3a5243abb1fc6d4ec.png)
12+
13+
**How the Rule Engine Works**:
14+
15+
- **Data Source**: Specified using the FROM clause in SQL.
16+
- **Data Processing**: Described using SQL statements and functions.
17+
- **Processing Result Destination**: Actions handle the output results, such as storing them in a database or republishing them to an MQTT topic.
18+
19+
**Key Actions**:
20+
21+
- Message republish to MQTT topics
22+
- Console output
23+
- Sending to external data systems like Kafka, MySQL, PostgreSQL, etc.
24+
25+
This guide focuses on debugging these rules within EMQX’s data integration to ensure they work as expected in a production environment.
26+
27+
## Why Do You Need Rule Testing?
28+
29+
Currently, creating a directly usable rule in EMQX can be challenging due to several limitations:
30+
31+
- **Limited to SQL Testing**: Only supports simulating data input for SQL testing to help users easily adjust SQL syntax to achieve their goals.
32+
- **Action Testing**: Actions can only be observed in the production environment of Sink resources, limiting the ability to test and verify them beforehand fully. Each adjustment requires switching to different places to check if it was successful.
33+
- **Simulated Data Limitations**: Simulated test data is often limited and may not accurately reflect the full range of MQTT attributes and event simulations. Real data can more effectively emulate various user scenarios.
34+
35+
Rule testing effectively addresses these issues by providing comprehensive feedback through the Dashboard and tracking logs across the entire data integration lifecycle. This helps users better identify and resolve problems. The benefits of rule testing include:
36+
37+
- **End-to-End Validation**: Allows the use of real data sources and actions, ensuring that every step of the data flow from input to output works as expected.
38+
- **Better Issue Detection**: During the development and rule editing phases, the UI provides a user-friendly interface that helps users effectively identify and resolve potential issues, reducing failures in the production environment.
39+
- **Increased Development Efficiency**: Testing features can significantly shorten development and testing cycles.
40+
- **System Stability Assurance**: Pre-testing and validating rules help prevent unforeseen issues in the production environment.
41+
42+
## A Step-by-Step Guide on Using Rule Testing
43+
44+
### Installing EMQX Enterprise
45+
46+
**Recommended Download:** [**EMQX Enterprise**](https://www.emqx.com/en/try?tab=self-managed) - This version provides rich data integration features, including Kafka, RabbitMQ, MySQL, PostgreSQL, InfluxDB, TimescaleDB, and other commonly used relational databases, time-series databases, and stream processing middleware, etc.
47+
48+
Alternatively, you can install it using the Docker command:
49+
50+
```bash
51+
docker run -d --name emqx-enterprise -p 1883:1883 -p 8083:8083 -p 8084:8084 -p 8883:8883 -p 18083:18083 emqx/emqx-enterprise:5.7.1
52+
```
53+
54+
Once the installation is complete, open the address `<http://<your-host-address>>:18083` in your browser, and enter the default username and password to log into the Dashboard.
55+
56+
After entering the Dashboard, click on **Data Integration****Rules****Create**, and you will be directed to the rule engine creation page. The upper section of this page is for configuring SQL, Data Source, and Sink. Below this is the section where we will use rule testing. Let two simple examples demonstrate how to create and enable rule testing.
57+
58+
![Create Rule](https://assets.emqx.com/images/24c91198743b30326c09bab0aa90dbdc.png)
59+
60+
### Creating Rules
61+
62+
After EMQX is installed, we demonstrate the use of the rule engine through a specific example. This scenario simulates vehicle data reporting; when the vehicle speed exceeds 120 km/h, related data (including speed and geographical location) is published to an HTTP service. The vehicle ID and MQTT connection information are stored in a PostgreSQL database.
63+
64+
- **Simulating MQTT Payload for Testing**: First, construct a test MQTT payload to simulate vehicle data reporting.
65+
66+
```json
67+
{
68+
"vehicle_id": "VH-958-XYZ",
69+
"speed": 72,
70+
"location": {
71+
"latitude": 40.7128,
72+
"longitude": -74.0060
73+
},
74+
"safety_features": {
75+
"airbag_deployed": false,
76+
"abs_status": "active"
77+
},
78+
"timestamp": "2024-07-11T15:45:00Z"
79+
}
80+
```
81+
82+
- **Creating SQL Rules**: Use the SQL editor to create a rule on the `devices/#` topic, with the condition set to trigger when the speed exceeds 120 km/h.
83+
84+
```sql
85+
SELECT
86+
username,
87+
clientid,
88+
payload.vehicle_id as vehicle_id,
89+
payload.speed as speed,
90+
payload.location.latitude as latitude,
91+
payload.location.longitude as longitude,
92+
payload.safety_features.airbag_deployed as airbag_deployed,
93+
payload.safety_features.abs_status as abs_status,
94+
timestamp
95+
FROM
96+
"devices/#"
97+
WHERE payload.speed > '120'
98+
```
99+
100+
- **Testing and Debugging SQL**: After writing the SQL, click the 'Start Test' button to debug the SQL syntax in the SQL Tab. You can enter simulated data to test, thus adjusting and optimizing your SQL statement. For a detailed guide on writing SQL, refer to [EMQX Documentation](https://docs.emqx.com/en/enterprise/latest/data-integration/rule-sql-syntax.html). Once testing is complete, we will set up HTTP and PostgreSQL environments to add two Actions for this rule.
101+
102+
### Setting up HTTP Service
103+
104+
Next, we use Node.js to create a simple HTTP service to receive and display data.
105+
106+
```js
107+
const express = require("express");
108+
const app = express();
109+
app.use(express.json());
110+
111+
app.post("/speed", (req, res) => {
112+
const { speed, latitude, longitude } = req.body;
113+
console.log(`Received data: Speed is ${speed} km/h at coordinates (${latitude}, ${longitude}).`);
114+
res.status(200. send("Data received successfully!");
115+
});
116+
117+
const PORT = 3000;
118+
app.listen(PORT, () => {
119+
console.log(`Server is running on port ${PORT}`);
120+
});
121+
```
122+
123+
On the rule creation page, choose to add an action, create an HTTP connector, and configure the HTTP request body to send the data extracted by SQL to the HTTP service.
124+
125+
![Edit The Action](https://assets.emqx.com/images/05ba47370b5df9da688c768c01994b1e.png)
126+
127+
### Configuring PostgreSQL Database
128+
129+
Next, we deploy a PostgreSQL database quickly using Docker and manage the database using Postico or another GUI client.
130+
131+
```yaml
132+
services:
133+
postgres:
134+
image: postgres
135+
environment:
136+
POSTGRES_DB: emqx
137+
POSTGRES_USER: admin
138+
POSTGRES_PASSWORD: public
139+
ports:
140+
- "5432:5432"
141+
volumes:
142+
- pgdata:/var/lib/postgresql/data
143+
```
144+
145+
Create a database table and add an action to store data in the database.
146+
147+
```sql
148+
CREATE TABLE car_infos (
149+
id SERIAL PRIMARY KEY,
150+
username VARCHAR(255),
151+
clientid VARCHAR(255),
152+
timestamp TIMESTAMP,
153+
vehicle_id VARCHAR(255),
154+
speed INT
155+
);
156+
```
157+
158+
Once created, also on the rule creation page, click to add an action, create a PostgreSQL Connector, and use this Connector to create an action. In the SQL template, input the following INSERT statement to ensure that when the rule triggers, the SQL-filtered data is saved to the database:
159+
160+
```sql
161+
INSERT INTO car_infos (
162+
username,
163+
clientid,
164+
timestamp,
165+
vehicle_id,
166+
speed
167+
) VALUES (
168+
${username},
169+
${clientid},
170+
TO_TIMESTAMP(${timestamp}::bigint / 1000),
171+
${vehicle_id},
172+
${speed}
173+
);
174+
```
175+
176+
![PostgreSQL Action](https://assets.emqx.com/images/bf9d908332183817319fd64fef307877.png)
177+
178+
### Starting the Test
179+
180+
> **Note:** Ensure the rules are saved before starting the test.
181+
182+
Begin by navigating the Rule Tab and clicking the "Start Test" button. Make sure to save the SQL rules using the save button at the bottom of the interface. Saving is essential as it enables end-to-end tracing of the test execution.
183+
184+
For users not using an MQTT client, simulated test data can be entered through the testing interface's right-side panel. This method allows for input simulation, though it may not completely mimic real-world scenarios due to potential configuration restrictions.
185+
186+
We recommend using [MQTTX](https://mqttx.app/) to simulate test data effectively. Connect to the current MQTT Broker and send real data to the topic `devices/1`.
187+
188+
![MQTTX Desktop](https://assets.emqx.com/images/f361608ad36be1adf0d361c63339b732.png)
189+
190+
If the rule is not triggered—for example, if the `speed` value is less than 120—you will observe an output indicating `SQL No Result`, signaling that the rurule's’sonditions were not met.
191+
192+
![Rule Execution and Output](https://assets.emqx.com/images/e657f3b05f1e0a42ac75ceb6c887aeb1.png)
193+
194+
Conversely, if the rule is successfully triggered, such as when the `speed` exceeds 120, each test instance will appear chronologically in the test interface's left panel, showing the event or topic message and the time the test started. Detailed results of the test can be viewed by selecting any listed instance.
195+
196+
The outcomes of each action, such as operations involving the HTTP server or the PostgreSQL database, will be clearly displayed. Successful actions are indicated by a green checkmark, while failed ones are marked with a red "X".
197+
198+
Each action detail expands to show a Request section, indicating what data the rule engine processed and forwarded. For example, the HTTP service's response is displayed following each request, providing insights into the action's outcome. Similarly, other actions will display a Result section to illustrate their effectiveness.
199+
200+
Next, to simulate error debugging, remove the Path configuration in the HTTP Action. We also modify the `vehicle_id` in a payload for the PostgreSQL database to exceed 255 characters, triggering a rule that assesses both setups.
201+
202+
![Test Target](https://assets.emqx.com/images/fba079ecb7f427b5b21ea6a765af3bf3.png)
203+
204+
#### HTTP Server
205+
206+
Upon triggering, if the HTTP Server action fails, you can view the details by examining the Request. The Body content should align with expectations. If the Action fails, the Reason field will provide specific failure details. Typically, a 404 status indicates a misconfiguration; an error message such as "Cannot post" will appear if the path was incorrectly set to `/`.
207+
208+
![Examining the Request](https://assets.emqx.com/images/fb1925b4df76f611b32741edd9800b42.png)
209+
210+
To correct this, return to the rule editing interface, update the Action to include a Path of `/speed`, and save. This adjustment doesn't require a rule re-save - simply resend the test data. If correctly configured, the HTTP server's response should reflect success, and corresponding data will be visible on the HTTP service console.
211+
212+
![HTTP Server's Response](https://assets.emqx.com/images/db2e72b3b6663d25e45236d588c99abe.png)
213+
214+
![HTTP Service Console](https://assets.emqx.com/images/559ee786603506af87755b33f22c0bc7.png)
215+
216+
#### PostgreSQL
217+
218+
Reviewing the PostgreSQL action, if the insertion attempt fails, the Reason field will reveal an error code such as `string_data_right_truncation` (22001), indicating that the data length exceeded the field's limits in the database.
219+
220+
![Error Code](https://assets.emqx.com/images/e7c544ee1abd0ec584c232da88c3804d.png)
221+
222+
![Error Code](https://assets.emqx.com/images/f33544a3ea1471c1f8b769add32238e8.png)
223+
224+
Adjust the `vehicle_id` to a shorter length, resend the data, and observe successful insertion, as confirmed in the Result section and by database checks using tools like Postico.
225+
226+
![Observe Successful Insertion](https://assets.emqx.com/images/fcc1775e8382ba08e95865ef97f6e4c3.png)
227+
228+
![Database Inspection](https://assets.emqx.com/images/8e9d845d5d53b3f1bc0b9062df93fa8c.png)
229+
230+
This structured approach tests rule accuracy and enhances debugging, allowing users to efficiently correct and optimize rule settings.
231+
232+
## Summary
233+
234+
This article demonstrated the basics of rule debugging and tracing in EMQX through a straightforward example, helping to clarify the differences from traditional SQL testing. End-to-end (E2E) rule testing is crucial for maintaining the robustness and reliability of the EMQX Platform, particularly in complex IoT environments. It enhances development efficiency and ensures system stability by detecting issues early.
235+
236+
While the current tools offer significant advantages, there is room for improvement, particularly in content display and user interaction. Future updates will refine these aspects to enhance user experience and functionality. Please stay tuned.
237+
238+
<section class="promotion">
239+
<div>
240+
Talk to an Expert
241+
</div>
242+
<a href="https://www.emqx.com/en/contact?product=solutions" class="button is-gradient">Contact Us →</a>
243+
</section>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
随着企业数字化转型的不断深入,确保数据和信息安全变得日益重要。传统的访问控制方法往往需要为每个用户单独配置权限,这种方法管理复杂且漏洞百出,难以适应快速变化的业务需求。而基于角色的访问控制(RBAC)功能通过定义不同角色及其权限来简化权限管理,不仅能有效地控制用户对敏感信息和系统的访问,而且减轻了企业的运维负担。
2+
3+
NeuronEX 作为一款工业边缘网关软件,提供工业多协议接入与边缘计算能力。能够实现工业设备协议采集、工业各系统数据集成、边端数据过滤分析、AI算法集成以及数据转发和平台对接等功能,为工业场景提供低延迟的数据接入管理及智能分析服务。
4+
5+
本文将重点介绍 NeuronEX 中基于角色的访问控制功能。该功能支持用户创建不同的角色,不同的角色相对应不同的操作权限,以此达到权限隔离的目的,提高用户数据的安全性、合规性和灵活性。
6+
7+
## 用户管理
8+
9+
当前系统内置了一个用户名为 admin 的超级管理员用户,首次登录系统后可更改此用户的密码。为了实现基于角色访问控制功能,NeuronEX 新增了用户管理的功能,通过前端右上角的 **用户管理** 按钮即可进入。
10+
11+
![用户管理](https://assets.emqx.com/images/7f2ddcc2a0a81d5f69292bc46dda7aaf.png)
12+
13+
点击 **创建用户** 按钮,在弹出的页面中,需要填写新创建用户的名字、密码、角色和描述信息,在角色下拉选项框中,可选择 Administrator 和 Viewer 两种角色。填写好用户信息后,点击 **提交** 按钮即可保存。
14+
15+
![创建用户](https://assets.emqx.com/images/84bf72b36be872b6ead07c89b5644335.png)
16+
17+
此外用户创建好后,在用户列表中还支持重新编辑用户信息、修改密码和删除用户。
18+
19+
## 访问控制
20+
21+
NeuronEX 的权限与角色密切关联,并且不支持用户自定义权限与角色之间的对应关系,其对应关系由 NeuronEX 决定 。目前 Administrator 角色拥有 NeuronEX 所有的控制权限,可以查看、创建、修改和删除所有配置,而 Viewer 角色对一些配置只有查看的权限。
22+
23+
当创建好用户后,凭借新用户的登录信息即可登录系统。由于用户在创建时已被赋予了 Administrator 或者 Viewer 角色,因此在登录时该用户的权限已经确定。对于 Viewer 角色的用户来说,前端上的某些编辑按钮会呈现出不可编辑的状态。
24+
25+
![访问控制——Administrator](https://assets.emqx.com/images/c83012b399251ba1fa8926288f350893.png)
26+
27+
Administrator 角色可以创建新的南向设备
28+
29+
![访问控制——Viewer](https://assets.emqx.com/images/988dab1aa973d336d632005faf173b6b.png)
30+
31+
Viewer 角色无法创建新的南向设备
32+
33+
此外,访问控制除了在前端生效外,针对 NeuronEX API 的请求依旧生效,用户需要首先调用 /api/login 接口来获取 Token, 然后用获得的 Token 来调用对应的 NeuronEX API。以 Administrator 角色获得的 Token 可以请求任意 API,而用 Viewer 角色获得的 Token 请求没有权限的 API 时会返回 403 状态码。
34+
35+
## 总结
36+
37+
本文介绍了 NeuronEX 中基于角色的访问控制功能。通过支持创建多个用户并赋予用户 Administrator 或 Viewer 角色,并将角色与控制权限做了对应关系,实现了基本的访问控制功能。本例中,当用户分别以角色Administrator 和 Viewer 请求时,会根据角色限制用户的访问。该功能从 3.0.0 版本开始支持,欢迎体验。
38+
39+
<section class="promotion">
40+
<div>
41+
咨询 EMQ 技术专家
42+
</div>
43+
<a href="https://www.emqx.com/zh/contact?product=solutions" class="button is-gradient">联系我们 →</a>
44+
</section>

0 commit comments

Comments
 (0)