Debian Codegraph @

ansible_collections/splunk/es/tests/unit/mock/vault_helper.py

raw
1 # Ansible is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5 #
6 # Ansible is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
13
14 from ansible.module_utils.common.text.converters import to_bytes
15 from ansible.parsing.vault import VaultSecret
16
17
18 class TextVaultSecret(VaultSecret):
19 """A secret piece of text. ie, a password. Tracks text encoding.
20
21 The text encoding of the text may not be the default text encoding so
22 we keep track of the encoding so we encode it to the same bytes."""
23
24 def __init__(self, text, encoding=None, errors=None, _bytes=None):
25 super(TextVaultSecret, self).__init__()
26 self.text = text
27 self.encoding = encoding or "utf-8"
28 self._bytes = _bytes
29 self.errors = errors or "strict"
30
31 @property
32 def bytes(self):
33 """The text encoded with encoding, unless we specifically set _bytes."""
34 return self._bytes or to_bytes(
35 self.text,
36 encoding=self.encoding,
37 errors=self.errors,
38 )