Parse the config and return a dict with the parsed values. :rtype: ``dict``
(self)
| 100 | self.LOG = log |
| 101 | |
| 102 | def parse(self): |
| 103 | """ |
| 104 | Parse the config and return a dict with the parsed values. |
| 105 | |
| 106 | :rtype: ``dict`` |
| 107 | """ |
| 108 | result = defaultdict(dict) |
| 109 | |
| 110 | if not os.path.isfile(self.config_file_path): |
| 111 | # Config doesn't exist, return the default values |
| 112 | return CONFIG_DEFAULT_VALUES |
| 113 | |
| 114 | config_dir_path = os.path.dirname(self.config_file_path) |
| 115 | |
| 116 | if self.validate_config_permissions: |
| 117 | # Make sure the directory permissions == 0o770 |
| 118 | if bool(os.stat(config_dir_path).st_mode & 0o7): |
| 119 | self.LOG.warning( |
| 120 | "The StackStorm configuration directory permissions are " |
| 121 | "insecure (too permissive): others have access." |
| 122 | ) |
| 123 | |
| 124 | # Make sure the setgid bit is set on the directory |
| 125 | if not bool(os.stat(config_dir_path).st_mode & 0o2000): |
| 126 | self.LOG.info( |
| 127 | "The SGID bit is not set on the StackStorm configuration " |
| 128 | "directory." |
| 129 | ) |
| 130 | |
| 131 | # Make sure the file permissions == 0o660 |
| 132 | if bool(os.stat(self.config_file_path).st_mode & 0o7): |
| 133 | self.LOG.warning( |
| 134 | "The StackStorm configuration file permissions are " |
| 135 | "insecure: others have access." |
| 136 | ) |
| 137 | |
| 138 | config = ConfigParser() |
| 139 | with io.open(self.config_file_path, "r", encoding="utf8") as fp: |
| 140 | config.read_file(fp) |
| 141 | for section, keys in six.iteritems(CONFIG_FILE_OPTIONS): |
| 142 | for key, options in six.iteritems(keys): |
| 143 | key_type = options["type"] |
| 144 | key_default_value = options["default"] |
| 145 | |
| 146 | if config.has_option(section, key): |
| 147 | if key_type in ["str", "string"]: |
| 148 | get_func = config.get |
| 149 | elif key_type in ["int", "integer"]: |
| 150 | get_func = config.getint |
| 151 | elif key_type in ["float"]: |
| 152 | get_func = config.getfloat |
| 153 | elif key_type in ["bool", "boolean"]: |
| 154 | get_func = config.getboolean |
| 155 | else: |
| 156 | msg = 'Invalid type "%s" for option "%s"' % (key_type, key) |
| 157 | raise ValueError(msg) |
| 158 | |
| 159 | value = get_func(section, key, raw=True) |