Test Failed
Pull Request — master (#4197)
by W
03:53
created

process_mistral_config()   B

Complexity

Conditions 7

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
dl 0
loc 22
rs 7.952
c 0
b 0
f 0
1
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
2
# contributor license agreements.  See the NOTICE file distributed with
3
# this work for additional information regarding copyright ownership.
4
# The ASF licenses this file to You under the Apache License, Version 2.0
5
# (the "License"); you may not use this file except in compliance with
6
# the License.  You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
16
import os
17
18
from six.moves.configparser import ConfigParser
19
20
__all__ = [
21
    'process_st2_config',
22
    'process_mistral_config',
23
    'process_content_pack_dir'
24
]
25
26
# Options which should be removed from the st2 config
27
ST2_CONF_OPTIONS_TO_REMOVE = {
28
    'database': ['username', 'password'],
29
    'messaging': ['url']
30
}
31
32
33
# Options which should be removed from the st2 config
34
MISTRAL_CONF_OPTIONS_TO_REMOVE = {
35
    'database': ['connection']
36
}
37
38
REMOVED_VALUE_NAME = '**removed**'
39
40
41
def process_st2_config(config_path):
42
    """
43
    Remove sensitive data (credentials) from the StackStorm config.
44
45
    :param config_path: Full absolute path to the st2 config inside /tmp.
46
    :type config_path: ``str``
47
    """
48
    assert config_path.startswith('/tmp')
49
50
    if not os.path.isfile(config_path):
51
        return
52
53
    config = ConfigParser()
54
    config.read(config_path)
55
56
    for section, options in ST2_CONF_OPTIONS_TO_REMOVE.items():
57
        for option in options:
58
            if config.has_option(section, option):
59
                config.set(section, option, REMOVED_VALUE_NAME)
60
61
    with open(config_path, 'w') as fp:
62
        config.write(fp)
63
64
65
def process_mistral_config(config_path):
66
    """
67
    Remove sensitive data (credentials) from the Mistral config.
68
69
    :param config_path: Full absolute path to the mistral config inside /tmp.
70
    :type config_path: ``str``
71
    """
72
    assert config_path.startswith('/tmp')
73
74
    if not os.path.isfile(config_path):
75
        return
76
77
    config = ConfigParser()
78
    config.read(config_path)
79
80
    for section, options in MISTRAL_CONF_OPTIONS_TO_REMOVE.items():
81
        for option in options:
82
            if config.has_option(section, option):
83
                config.set(section, option, REMOVED_VALUE_NAME)
84
85
    with open(config_path, 'w') as fp:
86
        config.write(fp)
87
88
89
def process_content_pack_dir(pack_dir):
90
    """
91
    Remove config.yaml from the pack directory.
92
93
    :param pack_dir: Full absolute path to the pack directory inside /tmp.
94
    :type pack_dir: ``str``
95
    """
96
    assert pack_dir.startswith('/tmp')
97
98
    config_file_path = os.path.join(pack_dir, 'config.yaml')
99
    if os.path.isfile(config_file_path):
100
        os.remove(config_file_path)
101