更新安全管控策略
========

#### 场景介绍

Enterprise Data Guard Kit为应用提供下发管控策略的能力，相关策略会被分发到HarmonyOS系统中执行。

#### 接口说明

详细接口说明可参考[接口文档](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard)。

| 接口名 | 描述 |
| :-- | :-- |
| [updatePolicy](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#updatepolicy)(policy: string, callback: AsyncCallback<void>): void | 使用Callback方式更新安全管控策略。 |
| [updatePolicy](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#updatepolicy-1)(policy: string): Promise<void> | 使用Promise方式更新安全管控策略。 |
| [getPolicy](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#getpolicy)(): Promise<string | null> | 使用Promise方式获取当前设备生效的管控策略内容。 |

#### 开发步骤

1.  导入模块。
    
    import { fileGuard } from '@kit.EnterpriseDataGuardKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
2.  初始化[FileGuard](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#fileguard)对象guard，调用接口updatePolicy，更新安全管控策略。
    
    *   通过回调函数方式，更新安全管控策略。
        
        function updatePolicyCallback() {
          let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
          let policy: string = '{' +
            '"net\_intercept\_toggle":1,' +
            '"default\_policy":1,' +
            '"net\_reject\_cache\_time":30,' +
            '"boundary":\["10.0.0.0-10.255.255.255","172.16.0.0-172.31.255.255"\],' +
            '"netsegment\_trustlist":\["10.0.0.0-10.255.255.255"\],' +
            '"netsegment\_blocklist":\["172.16.0.0-172.31.255.255","192.168.0.0-192.168.255.255"\],' +
            '"netsegment\_update\_type": 0,' +
            '"usb\_intercept\_toggle":1,' +
            '"smb\_client\_intercept\_toggle":1,' +
            '"smb\_server\_intercept\_toggle":1,' +
            '"new\_file\_audit\_toggle":1,' +
            '"kia\_variant\_toggle":1,' +
            '"audit\_filter\_toggle":1,' +
            '"print\_intercept\_toggle":0,' +
            '"bluetooth\_intercept\_toggle":\["bt\_socket","bt\_ble","bt\_opp"\],' +
            '"bluetooth\_intercept\_time":30,' +
            '"nearlink\_intercept\_toggle":\["nearlink\_ssap","nearlink\_dataTransfer"\],' +
            '"nearlink\_intercept\_time":30,' +
            '"trust\_app\_list":\["ohos.app.hap.myapplication\_BPch04bPYBrkJX8RAsmiGDbHFaG+BYvhkg4TK4fHQzJOL4VnoBCZU3boBBXGVEB+M/j0X2nnd7KVeyWuEORVxI2g="\],' +
            '"kia\_file\_access\_toggle":0,' +
            '"Tag1":{' +
            '   "tag":"sensitive",' +
            '   "usb\_intercept\_toggle":1,' +
            '   "net\_intercept\_toggle":1,' +
            '   "boundary":\["10.0.0.0-10.255.255.255"\],' +
            '   "netsegment\_trustlist":\["10.0.0.0-10.255.255.255"\],' +
            '   "netsegment\_blocklist":\["192.168.0.0-192.168.255.255"\]' +
            '  }' +
            '}';
          guard.updatePolicy(policy, (err: BusinessError) => {
            if (err) {
              console.error(\`Failed to update policy. Code: ${err.code}, message: ${err.message}.\`);
            } else {
              console.info(\`Succeeded in updating policy.\`);
            }
          });
        }
        
    *   通过Promise方式，更新安全管控策略。
        
        function updatePolicyPromise() {
          let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
          let policy: string = '{' +
            '"net\_intercept\_toggle":1,' +
            '"default\_policy":1,' +
            '"net\_reject\_cache\_time":30,' +
            '"boundary":\["10.0.0.0-10.255.255.255","172.16.0.0-172.31.255.255"\],' +
            '"netsegment\_trustlist":\["10.0.0.0-10.255.255.255"\],' +
            '"netsegment\_blocklist":\["172.16.0.0-172.31.255.255","192.168.0.0-192.168.255.255"\],' +
            '"netsegment\_update\_type": 0,' +
            '"usb\_intercept\_toggle":1,' +
            '"smb\_client\_intercept\_toggle":1,' +
            '"smb\_server\_intercept\_toggle":1,' +
            '"new\_file\_audit\_toggle":1,' +
            '"kia\_variant\_toggle":1,' +
            '"audit\_filter\_toggle":1,' +
            '"bluetooth\_intercept\_toggle":\["bt\_socket","bt\_ble","bt\_opp"\],' +
            '"bluetooth\_intercept\_time":30,' +
            '"nearlink\_intercept\_toggle":\["nearlink\_ssap","nearlink\_dataTransfer"\],' +
            '"nearlink\_intercept\_time":30,' +
            '"trust\_app\_list":\["ohos.app.hap.myapplication\_BPch04bPYBrkJX8RAsmiGDbHFaG+BYvhkg4TK4fHQzJOL4VnoBCZU3boBBXGVEB+M/j0X2nnd7KVeyWuEORVxI2g="\],' +
            '"Tag1":{' +
            '   "tag":"sensitive",' +
            '   "usb\_intercept\_toggle":1,' +
            '   "net\_intercept\_toggle":1,' +
            '   "boundary":\["10.0.0.0-10.255.255.255"\],' +
            '   "netsegment\_trustlist":\["10.0.0.0-10.255.255.255"\],' +
            '   "netsegment\_blocklist":\["192.168.0.0-192.168.255.255"\]' +
            '  }' +
            '}';
          guard.updatePolicy(policy).then(() => {
            console.info(\`Succeeded in updating policy.\`);
          }).catch((err: BusinessError) => {
            console.error(\`Failed to update policy. Code: ${err.code}, message: ${err.message}.\`);
          });
        }
        
3.  初始化[FileGuard](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#fileguard)对象guard，调用接口[getPolicy](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/dataguard-fileguard#getpolicy)，获取当前设备生效的管控策略内容。
    
    function getPolicyPromise() {
      let guard: fileGuard.FileGuard = new fileGuard.FileGuard();
      guard.getPolicy().then((policy: string | null) => {
        if (policy === null) {
          console.info(\`The obtained policy is null.\`);
          return;
        }
        console.info(\`Succeeded in getting policy. policy length: ${policy.length}\`);
        // 返回字符串较长，分次打印
        let len: number = 2000; // 每次打印2000字符
        let totalLen: number = policy.length;
        for (let i: number = 0; i < totalLen; i += len) {
          let end: number = Math.min(i + len, totalLen);
          let item: string = policy.substring(i, end);
          console.info(\`policy: ${item}\`);
        }
      }).catch((err: BusinessError) => {
        console.error(\`Failed to get policy. Code: ${err.code}, message: ${err.message}.\`);
      });
    }