comparison env/lib/python3.9/site-packages/cwltool/secrets.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Minimal in memory storage of secrets."""
2 import uuid
3 from typing import Dict, List, MutableMapping, MutableSequence, Optional, cast
4
5 from .utils import CWLObjectType, CWLOutputType
6
7
8 class SecretStore:
9 """Minimal implementation of a secret storage."""
10
11 def __init__(self) -> None:
12 """Initialize the secret store."""
13 self.secrets = {} # type: Dict[str, str]
14
15 def add(self, value: Optional[CWLOutputType]) -> Optional[CWLOutputType]:
16 """
17 Add the given value to the store.
18
19 Returns a placeholder value to use until the real value is needed.
20 """
21 if not isinstance(value, str):
22 raise Exception("Secret store only accepts strings")
23
24 if value not in self.secrets:
25 placeholder = "(secret-%s)" % str(uuid.uuid4())
26 self.secrets[placeholder] = value
27 return placeholder
28 return value
29
30 def store(self, secrets: List[str], job: CWLObjectType) -> None:
31 """Sanitize the job object of any of the given secrets."""
32 for j in job:
33 if j in secrets:
34 job[j] = self.add(job[j])
35
36 def has_secret(self, value: CWLOutputType) -> bool:
37 """Test if the provided document has any of our secrets."""
38 if isinstance(value, str):
39 for k in self.secrets:
40 if k in value:
41 return True
42 elif isinstance(value, MutableMapping):
43 for this_value in value.values():
44 if self.has_secret(cast(CWLOutputType, this_value)):
45 return True
46 elif isinstance(value, MutableSequence):
47 for this_value in value:
48 if self.has_secret(cast(CWLOutputType, this_value)):
49 return True
50 return False
51
52 def retrieve(self, value: CWLOutputType) -> CWLOutputType:
53 """Replace placeholders with their corresponding secrets."""
54 if isinstance(value, str):
55 for key, this_value in self.secrets.items():
56 value = value.replace(key, this_value)
57 return value
58 elif isinstance(value, MutableMapping):
59 return {k: self.retrieve(cast(CWLOutputType, v)) for k, v in value.items()}
60 elif isinstance(value, MutableSequence):
61 return [self.retrieve(cast(CWLOutputType, v)) for v in value]
62 return value