Skip to content

Commit cfb20f8

Browse files
committed
Merge pull request #728 from aanand/debug-auth
Add logging for auth loading/resolution
2 parents 8ba85ac + b5368ad commit cfb20f8

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

docker/auth/auth.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import base64
1616
import fileinput
1717
import json
18+
import logging
1819
import os
1920
import warnings
2021

@@ -28,6 +29,8 @@
2829
DOCKER_CONFIG_FILENAME = os.path.join('.docker', 'config.json')
2930
LEGACY_DOCKER_CONFIG_FILENAME = '.dockercfg'
3031

32+
log = logging.getLogger(__name__)
33+
3134

3235
def resolve_repository_name(repo_name, insecure=False):
3336
if insecure:
@@ -65,14 +68,18 @@ def resolve_authconfig(authconfig, registry=None):
6568
"""
6669
# Default to the public index server
6770
registry = convert_to_hostname(registry) if registry else INDEX_NAME
71+
log.debug("Looking for auth entry for {0}".format(repr(registry)))
6872

6973
if registry in authconfig:
74+
log.debug("Found {0}".format(repr(registry)))
7075
return authconfig[registry]
7176

7277
for key, config in six.iteritems(authconfig):
7378
if convert_to_hostname(key) == registry:
79+
log.debug("Found {0}".format(repr(key)))
7480
return config
7581

82+
log.debug("No entry found")
7683
return None
7784

7885

@@ -112,6 +119,10 @@ def parse_auth(entries):
112119
conf = {}
113120
for registry, entry in six.iteritems(entries):
114121
username, password = decode_auth(entry['auth'])
122+
log.debug(
123+
'Found entry (registry={0}, username={1})'
124+
.format(repr(registry), repr(username))
125+
)
115126
conf[registry] = {
116127
'username': username,
117128
'password': password,
@@ -133,38 +144,49 @@ def load_config(config_path=None):
133144
config_file = config_path or os.path.join(os.path.expanduser('~'),
134145
DOCKER_CONFIG_FILENAME)
135146

147+
log.debug("Trying {0}".format(config_file))
148+
136149
if os.path.exists(config_file):
137150
try:
138151
with open(config_file) as f:
139152
for section, data in six.iteritems(json.load(f)):
140153
if section != 'auths':
141154
continue
155+
log.debug("Found 'auths' section")
142156
return parse_auth(data)
143-
except (IOError, KeyError, ValueError):
157+
log.debug("Couldn't find 'auths' section")
158+
except (IOError, KeyError, ValueError) as e:
144159
# Likely missing new Docker config file or it's in an
145160
# unknown format, continue to attempt to read old location
146161
# and format.
162+
log.debug(e)
147163
pass
164+
else:
165+
log.debug("File doesn't exist")
148166

149167
config_file = config_path or os.path.join(os.path.expanduser('~'),
150168
LEGACY_DOCKER_CONFIG_FILENAME)
151169

152-
# if config path doesn't exist return empty config
170+
log.debug("Trying {0}".format(config_file))
171+
153172
if not os.path.exists(config_file):
173+
log.debug("File doesn't exist - returning empty config")
154174
return {}
155175

156-
# Try reading legacy location as JSON.
176+
log.debug("Attempting to parse as JSON")
157177
try:
158178
with open(config_file) as f:
159179
return parse_auth(json.load(f))
160-
except:
180+
except Exception as e:
181+
log.debug(e)
161182
pass
162183

163184
# If that fails, we assume the configuration file contains a single
164185
# authentication token for the public registry in the following format:
165186
#
166187
# auth = AUTH_TOKEN
167188
189+
log.debug("Attempting to parse legacy auth file format")
168190
try:
169191
data = []
170192
for line in fileinput.input(config_file):
@@ -182,8 +204,9 @@ def load_config(config_path=None):
182204
'serveraddress': INDEX_URL,
183205
}
184206
return conf
185-
except:
207+
except Exception as e:
208+
log.debug(e)
186209
pass
187210

188-
# If all fails, return an empty config
211+
log.debug("All parsing attempts failed - returning empty config")
189212
return {}

docker/client.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import logging
1516
import os
1617
import re
1718
import shlex
@@ -27,6 +28,8 @@
2728
from .utils import utils, check_resource
2829
from .constants import INSECURE_REGISTRY_DEPRECATION_WARNING
2930

31+
log = logging.getLogger(__name__)
32+
3033

3134
class Client(clientbase.ClientBase):
3235
@check_resource
@@ -130,14 +133,22 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
130133
headers['Content-Encoding'] = encoding
131134

132135
if utils.compare_version('1.9', self._version) >= 0:
136+
log.debug('Looking for auth config')
137+
133138
# If we don't have any auth data so far, try reloading the config
134139
# file one more time in case anything showed up in there.
135140
if not self._auth_configs:
141+
log.debug("No auth config in memory - loading from filesystem")
136142
self._auth_configs = auth.load_config()
137143

138144
# Send the full auth configuration (if any exists), since the build
139145
# could use any (or all) of the registries.
140146
if self._auth_configs:
147+
log.debug(
148+
'Sending auth config ({0})'.format(
149+
', '.join(repr(k) for k in self._auth_configs.keys())
150+
)
151+
)
141152
if headers is None:
142153
headers = {}
143154
if utils.compare_version('1.19', self._version) >= 0:
@@ -148,6 +159,8 @@ def build(self, path=None, tag=None, quiet=False, fileobj=None,
148159
headers['X-Registry-Config'] = auth.encode_header({
149160
'configs': self._auth_configs
150161
})
162+
else:
163+
log.debug('No auth config found')
151164

152165
response = self._post(
153166
u,
@@ -620,19 +633,26 @@ def pull(self, repository, tag=None, stream=False,
620633
# If we don't have any auth data so far, try reloading the config
621634
# file one more time in case anything showed up in there.
622635
if auth_config is None:
636+
log.debug('Looking for auth config')
623637
if not self._auth_configs:
638+
log.debug(
639+
"No auth config in memory - loading from filesystem")
624640
self._auth_configs = auth.load_config()
625641
authcfg = auth.resolve_authconfig(self._auth_configs, registry)
626642
# Do not fail here if no authentication exists for this
627643
# specific registry as we can have a readonly pull. Just
628644
# put the header if we can.
629645
if authcfg:
646+
log.debug('Found auth config')
630647
# auth_config needs to be a dict in the format used by
631648
# auth.py username , password, serveraddress, email
632649
headers['X-Registry-Auth'] = auth.encode_header(
633650
authcfg
634651
)
652+
else:
653+
log.debug('No auth config found')
635654
else:
655+
log.debug('Sending supplied auth config')
636656
headers['X-Registry-Auth'] = auth.encode_header(auth_config)
637657

638658
response = self._post(

0 commit comments

Comments
 (0)