@@ -67,6 +67,9 @@ class _VaultClient(LoggingMixin): # pylint: disable=too-many-instance-attribute
67
67
:param token: Authentication token to include in requests sent to Vault
68
68
(for ``token`` and ``github`` auth_type).
69
69
:type token: str
70
+ :param token_path: path to file containing authentication token to include in requests sent to Vault
71
+ (for ``token`` and ``github`` auth_type).
72
+ :type token_path: str
70
73
:param username: Username for Authentication (for ``ldap`` and ``userpass`` auth_types).
71
74
:type username: str
72
75
:param password: Password for Authentication (for ``ldap`` and ``userpass`` auth_types).
@@ -110,6 +113,7 @@ def __init__( # pylint: disable=too-many-arguments
110
113
mount_point : str = "secret" ,
111
114
kv_engine_version : Optional [int ] = None ,
112
115
token : Optional [str ] = None ,
116
+ token_path : Optional [str ] = None ,
113
117
username : Optional [str ] = None ,
114
118
password : Optional [str ] = None ,
115
119
key_id : Optional [str ] = None ,
@@ -134,10 +138,10 @@ def __init__( # pylint: disable=too-many-arguments
134
138
if auth_type not in VALID_AUTH_TYPES :
135
139
raise VaultError (f"The auth_type is not supported: { auth_type } . "
136
140
f"It should be one of { VALID_AUTH_TYPES } " )
137
- if auth_type == "token" and not token :
138
- raise VaultError ("The 'token' authentication type requires 'token'" )
139
- if auth_type == "github" and not token :
140
- raise VaultError ("The 'github' authentication type requires 'token'" )
141
+ if auth_type == "token" and not token and not token_path :
142
+ raise VaultError ("The 'token' authentication type requires 'token' or 'token_path' " )
143
+ if auth_type == "github" and not token and not token_path :
144
+ raise VaultError ("The 'github' authentication type requires 'token' or 'token_path' " )
141
145
if auth_type == "approle" and not role_id :
142
146
raise VaultError ("The 'approle' authentication type requires 'role_id'" )
143
147
if auth_type == "kubernetes" :
@@ -161,6 +165,7 @@ def __init__( # pylint: disable=too-many-arguments
161
165
self .auth_type = auth_type
162
166
self .kwargs = kwargs
163
167
self .token = token
168
+ self .token_path = token_path
164
169
self .auth_mount_point = auth_mount_point
165
170
self .mount_point = mount_point
166
171
self .username = username
@@ -206,7 +211,7 @@ def client(self) -> hvac.Client:
206
211
elif self .auth_type == "radius" :
207
212
self ._auth_radius (_client )
208
213
elif self .auth_type == "token" :
209
- _client . token = self .token
214
+ self ._set_token ( _client )
210
215
elif self .auth_type == "userpass" :
211
216
self ._auth_userpass (_client )
212
217
else :
@@ -307,6 +312,13 @@ def _auth_approle(self, _client: hvac.Client) -> None:
307
312
else :
308
313
_client .auth_approle (role_id = self .role_id , secret_id = self .secret_id )
309
314
315
+ def _set_token (self , _client : hvac .Client ) -> None :
316
+ if self .token_path :
317
+ with open (self .token_path ) as f :
318
+ _client .token = f .read ()
319
+ else :
320
+ _client .token = self .token
321
+
310
322
def get_secret (self , secret_path : str , secret_version : Optional [int ] = None ) -> Optional [dict ]:
311
323
"""
312
324
Get secret value from the KV engine.
0 commit comments