|
3 | 3 | import base64
|
4 | 4 | import json
|
5 | 5 | import os
|
| 6 | +import subprocess |
6 | 7 |
|
7 | 8 | from requests.auth import HTTPBasicAuth
|
8 | 9 |
|
9 | 10 | import dockercloud
|
10 | 11 | from .http import send_request
|
11 | 12 |
|
| 13 | +HUB_INDEX = "https://index.docker.io/v1/" |
12 | 14 |
|
13 | 15 | def authenticate(username, password):
|
14 | 16 | verify_credential(username, password)
|
@@ -43,11 +45,29 @@ def load_from_file(f="~/.docker/config.json"):
|
43 | 45 | try:
|
44 | 46 | with open(os.path.expanduser(f)) as config_file:
|
45 | 47 | data = json.load(config_file)
|
46 |
| - |
47 |
| - return data.get("auths", {}).get("https://index.docker.io/v1/", {}).get("auth", None) |
48 |
| - except Exception: |
| 48 | + except: |
49 | 49 | return None
|
50 | 50 |
|
| 51 | + creds_store = data.get("credsStore", None) |
| 52 | + if creds_store: |
| 53 | + try: |
| 54 | + cmd = "docker-credential-" + creds_store |
| 55 | + p = subprocess.Popen([cmd, 'get'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 56 | + out = p.communicate(input=HUB_INDEX)[0] |
| 57 | + except: |
| 58 | + raise dockercloud.AuthError('error getting credentials - err: exec: "%s": executable file not found in $PATH, out: ``' % cmd) |
| 59 | + |
| 60 | + try: |
| 61 | + credential = json.loads(out) |
| 62 | + username = credential.get("Username") |
| 63 | + password = credential.get("Secret") |
| 64 | + return base64.b64encode("%s:%s" % (username, password)) |
| 65 | + except: |
| 66 | + return None |
| 67 | + |
| 68 | + else: |
| 69 | + return data.get("auths", {}).get(HUB_INDEX, {}).get("auth", None) |
| 70 | + |
51 | 71 |
|
52 | 72 | def get_auth_header():
|
53 | 73 | try:
|
|
0 commit comments