• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# -*- coding: utf-8 -*-
2# Copyright (c) 2023 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14import os
15import sys
16
17sys.path.append(os.path.dirname(os.path.realpath(__file__)) + os.sep)
18from utils import *
19
20log_tag = 'resolve_token_info'
21
22
23def check_and_get(file: list):
24    nativeAcls = {}
25    try:
26        set_log_content(LogLevel(2).name, log_tag,
27                        '-------------------------- invalidPermList check begin --------------------------')
28        check_pass = True
29        processName = 'xxxxxxxx'
30        for it in file:
31            if it.find('processName') != -1:
32                processName = it.replace(',', '').split(':')[1].split('"')[1]
33            elif it.find('invalidPermList') != -1:
34                check_pass = False
35                msg = 'invalidPermList information is detected in processName = {}'.format(processName)
36                set_log_content(LogLevel(2).name, log_tag, msg)
37            elif check_pass and it.find('nativeAcls') != -1:
38                bb = it.split(':')
39                if bb[1].split('"')[1].__len__() == 0:
40                    continue
41                permissionNameList = bb[1].split('"')[1].split(',')
42                nativeAcls[processName] = permissionNameList
43        if check_pass == False:
44            raise AclCheckException('-------------------------- The invalidPermList check failed --------------------------')
45        else:
46            set_log_content(LogLevel(2).name, log_tag,
47                            '-------------------------- The invalidPermList check successful --------------------------')
48    except Exception as e:
49        set_log_content(LogLevel(1).name, log_tag, e.msg)
50        raise
51    return nativeAcls
52
53
54def clear_token_info_txt(path):
55    try:
56        os.remove(path)
57    except Exception as e:
58        set_log_content(LogLevel(1).name, log_tag, e.msg)
59
60
61def read_txt(path):
62    set_log_content(LogLevel(2).name, log_tag, 'read {}'.format(path))
63    if not os.path.exists(path):
64        set_log_content(LogLevel(2).name, log_tag, '{} file not exits'.format(path))
65        raise AclCheckException('{} file not exits!'.format(path))
66    try:
67        with open(path, 'r') as f:
68            file = f.readlines()
69            return file
70    except Exception as e:
71        set_log_content(LogLevel(1).name, log_tag, e.msg)
72        raise AclCheckException('{} failed to read the file.'.format(path))
73