1
1
"""Minimal in memory storage of secrets."""
2
2
import uuid
3
- from typing import Any , Dict , List , MutableMapping , MutableSequence
3
+ from typing import Dict , List , MutableMapping , MutableSequence , Optional , cast
4
+
5
+ from .utils import CWLObjectType , CWLOutputType
4
6
5
7
6
8
class SecretStore (object ):
7
9
"""Minimal implementation of a secret storage."""
8
10
9
- def __init__ (self ): # type: () -> None
11
+ def __init__ (self ) -> None :
10
12
"""Initialize the secret store."""
11
13
self .secrets = {} # type: Dict[str, str]
12
14
13
- def add (self , value ): # type: (str ) -> str
15
+ def add (self , value : Optional [ CWLOutputType ] ) -> Optional [ CWLOutputType ]:
14
16
"""
15
17
Add the given value to the store.
16
18
@@ -25,36 +27,36 @@ def add(self, value): # type: (str) -> str
25
27
return placeholder
26
28
return value
27
29
28
- def store (self , secrets , job ):
29
- # type: (List[str], MutableMapping[str, Any]) -> None
30
+ def store (self , secrets : List [str ], job : CWLObjectType ) -> None :
30
31
"""Sanitize the job object of any of the given secrets."""
31
32
for j in job :
32
33
if j in secrets :
33
34
job [j ] = self .add (job [j ])
34
35
35
- def has_secret (self , value ): # type: (Any ) -> bool
36
+ def has_secret (self , value : CWLOutputType ) -> bool :
36
37
"""Test if the provided document has any of our secrets."""
37
38
if isinstance (value , str ):
38
39
for k in self .secrets :
39
40
if k in value :
40
41
return True
41
42
elif isinstance (value , MutableMapping ):
42
43
for this_value in value .values ():
43
- if self .has_secret (this_value ):
44
+ if self .has_secret (cast ( CWLOutputType , this_value ) ):
44
45
return True
45
46
elif isinstance (value , MutableSequence ):
46
47
for this_value in value :
47
- if self .has_secret (this_value ):
48
+ if self .has_secret (cast ( CWLOutputType , this_value ) ):
48
49
return True
49
50
return False
50
51
51
- def retrieve (self , value ): # type: (Any ) -> Any
52
+ def retrieve (self , value : CWLOutputType ) -> CWLOutputType :
52
53
"""Replace placeholders with their corresponding secrets."""
53
54
if isinstance (value , str ):
54
55
for key , this_value in self .secrets .items ():
55
56
value = value .replace (key , this_value )
57
+ return value
56
58
elif isinstance (value , MutableMapping ):
57
- return {k : self .retrieve (v ) for k , v in value .items ()}
59
+ return {k : self .retrieve (cast ( CWLOutputType , v ) ) for k , v in value .items ()}
58
60
elif isinstance (value , MutableSequence ):
59
- return [self .retrieve (v ) for k , v in enumerate ( value ) ]
61
+ return [self .retrieve (cast ( CWLOutputType , v )) for v in value ]
60
62
return value
0 commit comments