|
20 | 20 | """ |
21 | 21 |
|
22 | 22 | import getpass |
| 23 | +import keyring |
23 | 24 | import logging |
24 | 25 | import os |
25 | 26 | import sys |
|
33 | 34 |
|
34 | 35 |
|
35 | 36 | VERSION = '0.1' |
| 37 | +KEYRING_SERVICE = 'openstack' |
36 | 38 |
|
37 | 39 |
|
38 | 40 | def env(*vars, **kwargs): |
@@ -149,12 +151,14 @@ def authenticate_user(self): |
149 | 151 | "You must provide a username via" |
150 | 152 | " either --os-username or env[OS_USERNAME]") |
151 | 153 |
|
| 154 | + self.get_password_from_keyring() |
152 | 155 | if not self.options.os_password: |
153 | 156 | # No password, if we've got a tty, try prompting for it |
154 | 157 | if hasattr(sys.stdin, 'isatty') and sys.stdin.isatty(): |
155 | 158 | # Check for Ctl-D |
156 | 159 | try: |
157 | 160 | self.options.os_password = getpass.getpass() |
| 161 | + self.set_password_in_keyring() |
158 | 162 | except EOFError: |
159 | 163 | pass |
160 | 164 | # No password because we did't have a tty or the |
@@ -188,6 +192,24 @@ def authenticate_user(self): |
188 | 192 | ) |
189 | 193 | return |
190 | 194 |
|
| 195 | + def get_password_from_keyring(self): |
| 196 | + """Get password from keyring, if it's set""" |
| 197 | + service = KEYRING_SERVICE |
| 198 | + if not self.options.os_password: |
| 199 | + password = keyring.get_password(service, self.options.os_username) |
| 200 | + os.options.os_password = password |
| 201 | + |
| 202 | + def set_password_in_keyring(self): |
| 203 | + """Set password in keyring for this user""" |
| 204 | + service = KEYRING_SERVICE |
| 205 | + if self.options.os_password: |
| 206 | + password = keyring.get_password(service, self.options.os_username) |
| 207 | + # either password is not set in keyring, or it is different |
| 208 | + if password != self.options.os_password: |
| 209 | + keyring.set_password(service, |
| 210 | + self.options.os_username, |
| 211 | + self.options.os_password) |
| 212 | + |
191 | 213 | def initialize_app(self, argv): |
192 | 214 | """Global app init bits: |
193 | 215 |
|
|
0 commit comments