Repository navigation
Description
Description of Issue/Question
I am writing a state to pull docker images from AWS ECR registry. The way the credentials passed out by AWS are only valid for 12 hours, so I wrote a custom python pillar to run the proper awscli commands to fetch the credentials and build the pillar data for the registry. Assuming the pillar data is refreshed and with valid credentials for the run the minion still fails with an execption.
The problem is fixed by manually going onto the minion and running docker login, so I would guess it sees the configured auth credentials from a previous docker login and uses them even if they are invalid.
Setup
Here are the relevant salt files for the issue (w/ private info redacted)
This build the docker-registries pillar data
#!py
import subprocess
import re
def run():
'''
builds the data structure used by docker states
'''
docker = {
'docker-pkg': {
'lookup': {
'version': '1.9.1-0~'+__grains__['oscodename'],
'config': ['DOCKER_OPTS=" --host=unix:///var/run/docker.sock --storage-driver=aufs"']
}
},
'docker-registries': {
'https://xxxxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com': {
'email': 'none',
'password': fetchAWSECRCreds(),
'username': 'AWS'
}
},
'install_docker_py': True
}
return docker
#fetches aws credentials for ecr
#requires awscli to be installed and configured with valid credentials
def fetchAWSECRCreds():
r = subprocess.Popen(["aws","ecr","get-login"],stdout=subprocess.PIPE)
p = re.compile(ur'-p (.*) -e')
test_str = r.stdout.read()
return re.search(p,test_str).group(1)
Steps to Reproduce Issue
Easiest method to reproduce (using AWS ECR):
- Setup pillar data to login with valid credentials and do dockerng.pull (works fine)
- Invalidate credentials and try pull again (fails as expected)
- Update the credentials making sure to refresh pillar data
- Try to pull again and it should fail with the following message
minion log
2016-04-25 15:37:25,231 [requests.packages.urllib3.connectionpool][DEBUG ][19156] "GET /v1.21/images/json?only_ids=0&all=1 HTTP/1.1" 200 None
2016-04-25 15:37:25,318 [docker.auth.auth ][DEBUG ][19156] Looking for auth entry for '364535168108.dkr.ecr.us-east-1.amazonaws.com'
2016-04-25 15:37:25,319 [docker.auth.auth ][DEBUG ][19156] Found u'https://364535168108.dkr.ecr.us-east-1.amazonaws.com'
2016-04-25 15:37:25,319 [docker.api.image ][DEBUG ][19156] Looking for auth config
2016-04-25 15:37:25,319 [docker.auth.auth ][DEBUG ][19156] Looking for auth entry for '364535168108.dkr.ecr.us-east-1.amazonaws.com'
2016-04-25 15:37:25,320 [docker.auth.auth ][DEBUG ][19156] Found u'https://364535168108.dkr.ecr.us-east-1.amazonaws.com'
2016-04-25 15:37:25,320 [docker.api.image ][DEBUG ][19156] Found auth config
2016-04-25 15:37:25,449 [requests.packages.urllib3.connectionpool][DEBUG ][19156] "POST /v1.21/images/create?tag=search_alpha&fromImage=364535168108.dkr.ecr.us-east-1.amazonaws.com%2Fjs-sessions HTTP/1.1" 200 None
2016-04-25 15:37:25,467 [salt.minion ][WARNING ][19156] The minion function caused an exception
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/salt/minion.py", line 1071, in _thread_return
return_data = func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/salt/modules/dockerng.py", line 3865, in pull
_error_detail(errors, item)
File "/usr/local/lib/python2.7/dist-packages/salt/modules/dockerng.py", line 1058, in _error_detail
item['errorDetail']['message'],
IndexError: tuple index out of range
Versions Report
Salt Version:
Salt: 2015.8.7
Dependency Versions:
Jinja2: 2.8
M2Crypto: 0.21.1
Mako: 0.9.1
PyYAML: 3.11
PyZMQ: 15.2.0
Python: 2.7.6 (default, Jun 22 2015, 17:58:13)
RAET: Not Installed
Tornado: 4.3
ZMQ: 4.1.2
cffi: 1.5.0
cherrypy: 3.2.2
dateutil: 2.4.2
gitdb: 0.5.4
gitpython: 0.3.2 RC1
ioflo: Not Installed
libgit2: 0.23.4
libnacl: Not Installed
msgpack-pure: Not Installed
msgpack-python: 0.4.7
mysql-python: 1.2.3
pycparser: 2.14
pycrypto: 2.6.1
pygit2: 0.23.1
python-gnupg: Not Installed
smmap: 0.8.2
timelib: Not Installed
System Versions:
dist: Ubuntu 14.04 trusty
machine: x86_64
release: 3.13.0-71-generic
system: Ubuntu 14.04 trusty
Activity
tyhunt99 commentedon Apr 25, 2016
Upon further investigation it appears the exception is unique to AWS ECR repos because they return a special response back when credentials are invalid.
It also appears the same method can be used for any repository. I tested against a dockerhub repo and it failed under same conditions although the output it returned was not useful. None is always returned with no apparent error. The minion log for this case where I change my docker hub credentials and then update the pillar with new valid credentials follows:
Not sure if it is relevant but using the docker cli tool with invalid credentials simply returns no image found even when the image exists.
tyhunt99 commentedon Apr 25, 2016
Even further investigation seems to indicate if there is not a .docker/config file present then it will use the credentials stored in the pillar data, otherwise it will use whatever is in the config file without a way to force an override.
jfindlay commentedon Apr 26, 2016
@tyhunt99, thanks for the excellent investigative work here.
rallytime commentedon Apr 26, 2016
@ticosax This might be one you're interested in.
ticosax commentedon Apr 26, 2016
@rallytime Yes, I saw that issue, but it is not my priority at the moment, as I'm not affected by that bug.
So if someone is in need of an immediate fix, better you grab it yourself.
I'll let a comment here once I'll start working on it.
rallytime commentedon Apr 26, 2016
No problem. I knew you'd been working on some of these. Thanks for the update!
tyhunt99 commentedon Aug 23, 2016
I have a super simple fix which solves the problem but I am not sure if it valid for all cases. If you simply pass
reauth=Trueto the docker-py login function it will always use the credentials passed in.This seems valid to me because you are using the credentials passed in via pillar data which need to always be valid anyways for it to work.
But I can also see a possible use case of maybe having one thing ensure the .docker/config file exists and then rely on that for further calls.
If it is the former it is super easy and quick to implement a fix. The latter will require a little more though and design
add option to force a reauth for a docker registry (#36893)
add option to force a reauth for a docker registry