Force params to be strings unless they should remain a different type
(key, val)
| 182 | |
| 183 | |
| 184 | def enforce_types(key, val): |
| 185 | """ |
| 186 | Force params to be strings unless they should remain a different type |
| 187 | """ |
| 188 | non_string_params = { |
| 189 | "ssl_verify": bool, |
| 190 | "insecure_auth": bool, |
| 191 | "disable_saltenv_mapping": bool, |
| 192 | "saltenv_whitelist": "stringlist", |
| 193 | "saltenv_blacklist": "stringlist", |
| 194 | "refspecs": "stringlist", |
| 195 | "ref_types": "stringlist", |
| 196 | "update_interval": int, |
| 197 | } |
| 198 | |
| 199 | def _find_global(key): |
| 200 | for item in non_string_params: |
| 201 | try: |
| 202 | if key.endswith("_" + item): |
| 203 | ret = item |
| 204 | break |
| 205 | except TypeError: |
| 206 | if key.endswith("_" + str(item)): |
| 207 | ret = item |
| 208 | break |
| 209 | else: |
| 210 | ret = None |
| 211 | return ret |
| 212 | |
| 213 | if key not in non_string_params: |
| 214 | key = _find_global(key) |
| 215 | if key is None: |
| 216 | return str(val) |
| 217 | |
| 218 | expected = non_string_params[key] |
| 219 | if expected == "stringlist": |
| 220 | if not isinstance(val, ((str,), list)): |
| 221 | val = str(val) |
| 222 | if isinstance(val, str): |
| 223 | return [x.strip() for x in val.split(",")] |
| 224 | return [str(x) for x in val] |
| 225 | else: |
| 226 | try: |
| 227 | return expected(val) |
| 228 | except Exception: # pylint: disable=broad-except |
| 229 | log.error( |
| 230 | "Failed to enforce type for key=%s with val=%s, falling back " |
| 231 | "to a string", |
| 232 | key, |
| 233 | val, |
| 234 | ) |
| 235 | return str(val) |
| 236 | |
| 237 | |
| 238 | def failhard(role): |
no test coverage detected