Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/cwltool/secrets.py @ 0:d30785e31577 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:18:57 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d30785e31577 |
---|---|
1 """Minimal in memory storage of secrets.""" | |
2 import uuid | |
3 from typing import Any, Dict, List, MutableMapping, MutableSequence | |
4 | |
5 from six import string_types | |
6 from typing_extensions import Text # pylint: disable=unused-import | |
7 # move to a regular typing import when Python 3.3-3.6 is no longer supported | |
8 | |
9 | |
10 class SecretStore(object): | |
11 """Minimal implementation of a secret storage.""" | |
12 | |
13 def __init__(self): # type: () -> None | |
14 """Initialize the secret store.""" | |
15 self.secrets = {} # type: Dict[Text, Text] | |
16 | |
17 def add(self, value): # type: (Text) -> Text | |
18 """ | |
19 Add the given value to the store. | |
20 | |
21 Returns a placeholder value to use until the real value is needed. | |
22 """ | |
23 if not isinstance(value, string_types): | |
24 raise Exception("Secret store only accepts strings") | |
25 | |
26 if value not in self.secrets: | |
27 placeholder = "(secret-%s)" % Text(uuid.uuid4()) | |
28 self.secrets[placeholder] = value | |
29 return placeholder | |
30 return value | |
31 | |
32 def store(self, secrets, job): | |
33 # type: (List[Text], MutableMapping[Text, Any]) -> None | |
34 """Sanitize the job object of any of the given secrets.""" | |
35 for j in job: | |
36 if j in secrets: | |
37 job[j] = self.add(job[j]) | |
38 | |
39 def has_secret(self, value): # type: (Any) -> bool | |
40 """Test if the provided document has any of our secrets.""" | |
41 if isinstance(value, string_types): | |
42 for k in self.secrets: | |
43 if k in value: | |
44 return True | |
45 elif isinstance(value, MutableMapping): | |
46 for this_value in value.values(): | |
47 if self.has_secret(this_value): | |
48 return True | |
49 elif isinstance(value, MutableSequence): | |
50 for this_value in value: | |
51 if self.has_secret(this_value): | |
52 return True | |
53 return False | |
54 | |
55 def retrieve(self, value): # type: (Any) -> Any | |
56 """Replace placeholders with their corresponding secrets.""" | |
57 if isinstance(value, string_types): | |
58 for key, this_value in self.secrets.items(): | |
59 value = value.replace(key, this_value) | |
60 elif isinstance(value, MutableMapping): | |
61 return {k: self.retrieve(v) for k, v in value.items()} | |
62 elif isinstance(value, MutableSequence): | |
63 return [self.retrieve(v) for k, v in enumerate(value)] | |
64 return value |