2013-09-20 15:55:44 +00:00
|
|
|
import json
|
|
|
|
import urllib
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
from flask import request, make_response, jsonify, abort
|
2013-09-20 15:55:44 +00:00
|
|
|
from functools import wraps
|
|
|
|
|
2013-09-25 16:45:12 +00:00
|
|
|
from data import model
|
2013-10-03 20:19:01 +00:00
|
|
|
from app import app, mixpanel
|
2013-09-25 16:45:12 +00:00
|
|
|
from auth.auth import (process_auth, get_authenticated_user,
|
|
|
|
get_validated_token)
|
2013-09-25 20:46:28 +00:00
|
|
|
from util.names import parse_namespace_repository, parse_repository_name
|
2013-09-27 23:29:01 +00:00
|
|
|
from util.email import send_confirmation_email
|
2013-09-25 16:45:12 +00:00
|
|
|
from auth.permissions import (ModifyRepositoryPermission,
|
|
|
|
ReadRepositoryPermission, UserPermission)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2013-09-26 00:00:22 +00:00
|
|
|
def generate_headers(f):
|
|
|
|
@wraps(f)
|
|
|
|
def wrapper(namespace, repository, *args, **kwargs):
|
|
|
|
response = f(namespace, repository, *args, **kwargs)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-30 18:39:10 +00:00
|
|
|
response.headers['X-Docker-Endpoints'] = app.config['REGISTRY_SERVER']
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-26 00:00:22 +00:00
|
|
|
has_token_request = request.headers.get('X-Docker-Token', '')
|
2013-09-20 22:38:17 +00:00
|
|
|
|
2013-10-01 05:18:05 +00:00
|
|
|
if has_token_request:
|
2013-09-26 00:00:22 +00:00
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
token = model.create_access_token(get_authenticated_user(), repo)
|
|
|
|
token_str = 'signature=%s,repository="%s/%s"' % (token.code, namespace,
|
2013-10-01 05:18:05 +00:00
|
|
|
repository)
|
2013-09-26 00:00:22 +00:00
|
|
|
response.headers['WWW-Authenticate'] = token_str
|
|
|
|
response.headers['X-Docker-Token'] = token_str
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-26 00:00:22 +00:00
|
|
|
return response
|
|
|
|
return wrapper
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/v1/users', methods=['POST'])
|
|
|
|
@app.route('/v1/users/', methods=['POST'])
|
|
|
|
def create_user():
|
|
|
|
user_data = request.get_json()
|
2013-10-01 16:25:06 +00:00
|
|
|
username = user_data['username']
|
|
|
|
password = user_data['password']
|
|
|
|
|
|
|
|
existing_user = model.get_user(username)
|
|
|
|
if existing_user:
|
|
|
|
verified = model.verify_user(username, password)
|
|
|
|
if verified:
|
|
|
|
return make_response('Verified', 201)
|
|
|
|
else:
|
|
|
|
abort(401)
|
|
|
|
else:
|
|
|
|
# New user case
|
|
|
|
new_user = model.create_user(username, password, user_data['email'])
|
|
|
|
code = model.create_confirm_email_code(new_user)
|
|
|
|
send_confirmation_email(new_user.username, new_user.email, code.code)
|
|
|
|
return make_response('Created', 201)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/v1/users', methods=['GET'])
|
|
|
|
@app.route('/v1/users/', methods=['GET'])
|
2013-09-20 22:38:17 +00:00
|
|
|
@process_auth
|
2013-09-20 15:55:44 +00:00
|
|
|
def get_user():
|
|
|
|
return jsonify({
|
2013-09-20 22:38:17 +00:00
|
|
|
'username': get_authenticated_user().username,
|
|
|
|
'email': get_authenticated_user().email,
|
2013-09-20 15:55:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/v1/users/<username>/', methods=['PUT'])
|
2013-09-20 22:38:17 +00:00
|
|
|
@process_auth
|
2013-09-20 15:55:44 +00:00
|
|
|
def update_user(username):
|
2013-09-20 22:38:17 +00:00
|
|
|
permission = UserPermission(username)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
if permission.can():
|
|
|
|
update_request = request.get_json()
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
if 'password' in update_request:
|
|
|
|
logger.debug('Updating user password.')
|
|
|
|
model.change_password(get_authenticated_user(),
|
|
|
|
update_request['password'])
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
if 'email' in update_request:
|
|
|
|
logger.debug('Updating user email')
|
|
|
|
model.update_email(get_authenticated_user(), update_request['email'])
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
return jsonify({
|
|
|
|
'username': get_authenticated_user().username,
|
|
|
|
'email': get_authenticated_user().email,
|
|
|
|
})
|
|
|
|
|
|
|
|
abort(403)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/v1/repositories/<path:repository>', methods=['PUT'])
|
2013-09-20 22:38:17 +00:00
|
|
|
@process_auth
|
2013-09-20 15:55:44 +00:00
|
|
|
@parse_repository_name
|
2013-09-26 00:00:22 +00:00
|
|
|
@generate_headers
|
2013-09-20 15:55:44 +00:00
|
|
|
def create_repository(namespace, repository):
|
|
|
|
image_descriptions = json.loads(request.data)
|
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
|
|
|
|
if repo:
|
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
|
|
|
if not permission.can():
|
2013-09-26 19:58:11 +00:00
|
|
|
abort(403)
|
2013-09-20 22:47:47 +00:00
|
|
|
|
2013-09-26 19:58:11 +00:00
|
|
|
else:
|
2013-09-20 22:47:47 +00:00
|
|
|
if get_authenticated_user().username != namespace:
|
|
|
|
abort(403)
|
2013-09-20 22:38:17 +00:00
|
|
|
|
|
|
|
logger.debug('Creaing repository with owner: %s' %
|
|
|
|
get_authenticated_user().username)
|
|
|
|
repo = model.create_repository(namespace, repository,
|
|
|
|
get_authenticated_user())
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
new_repo_images = {desc['id']: desc for desc in image_descriptions}
|
|
|
|
added_images = dict(new_repo_images)
|
|
|
|
for existing in model.get_repository_images(namespace, repository):
|
2013-10-01 18:14:39 +00:00
|
|
|
if existing.docker_image_id in new_repo_images:
|
|
|
|
added_images.pop(existing.docker_image_id)
|
2013-09-20 15:55:44 +00:00
|
|
|
else:
|
|
|
|
existing.repositoryimage.delete()
|
|
|
|
|
|
|
|
for image_description in added_images.values():
|
2013-09-26 19:58:11 +00:00
|
|
|
image = model.create_image(image_description['id'], repo)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
response = make_response('Created', 201)
|
2013-10-03 20:19:01 +00:00
|
|
|
|
|
|
|
mixpanel.track(get_authenticated_user().username, 'push_repo')
|
|
|
|
|
2013-09-20 15:55:44 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/v1/repositories/<path:repository>/images', methods=['PUT'])
|
2013-09-20 22:38:17 +00:00
|
|
|
@process_auth
|
2013-09-20 15:55:44 +00:00
|
|
|
@parse_repository_name
|
2013-09-26 00:00:22 +00:00
|
|
|
@generate_headers
|
2013-09-20 15:55:44 +00:00
|
|
|
def update_images(namespace, repository):
|
2013-09-20 22:38:17 +00:00
|
|
|
permission = ModifyRepositoryPermission(namespace, repository)
|
|
|
|
|
|
|
|
if permission.can():
|
2013-09-26 19:58:11 +00:00
|
|
|
repository = model.get_repository(namespace, repository)
|
2013-09-20 22:38:17 +00:00
|
|
|
image_with_checksums = json.loads(request.data)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
for image in image_with_checksums:
|
2013-09-26 19:58:11 +00:00
|
|
|
logger.debug('Setting checksum for image id: %s to %s' %
|
2013-10-08 15:29:42 +00:00
|
|
|
(image['id'], image['checksum']))
|
2013-09-26 19:58:11 +00:00
|
|
|
model.set_image_checksum(image['id'], repository, image['checksum'])
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
return make_response('Updated', 204)
|
|
|
|
|
|
|
|
abort(403)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/v1/repositories/<path:repository>/images', methods=['GET'])
|
2013-09-20 22:38:17 +00:00
|
|
|
@process_auth
|
2013-09-20 15:55:44 +00:00
|
|
|
@parse_repository_name
|
2013-09-26 00:00:22 +00:00
|
|
|
@generate_headers
|
2013-09-20 15:55:44 +00:00
|
|
|
def get_repository_images(namespace, repository):
|
2013-09-20 22:38:17 +00:00
|
|
|
permission = ReadRepositoryPermission(namespace, repository)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
# TODO invalidate token?
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-28 04:05:32 +00:00
|
|
|
if permission.can() or model.repository_is_public(namespace, repository):
|
2013-09-20 22:38:17 +00:00
|
|
|
all_images = []
|
|
|
|
for image in model.get_repository_images(namespace, repository):
|
|
|
|
new_image_view = {
|
2013-10-01 18:14:39 +00:00
|
|
|
'id': image.docker_image_id,
|
2013-09-20 22:38:17 +00:00
|
|
|
'checksum': image.checksum,
|
|
|
|
}
|
|
|
|
all_images.append(new_image_view)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
resp = make_response(json.dumps(all_images), 200)
|
|
|
|
resp.mimetype = 'application/json'
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-10-03 23:18:03 +00:00
|
|
|
pull_username = 'anonymous'
|
|
|
|
if get_authenticated_user():
|
|
|
|
pull_username = get_authenticated_user().username
|
|
|
|
|
|
|
|
mixpanel.track(pull_username, 'pull_repo')
|
2013-10-03 20:19:01 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
return resp
|
2013-09-20 15:55:44 +00:00
|
|
|
|
2013-09-20 22:38:17 +00:00
|
|
|
abort(403)
|
2013-09-20 15:55:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/v1/repositories/<path:repository>/images', methods=['DELETE'])
|
2013-09-20 22:38:17 +00:00
|
|
|
@process_auth
|
2013-09-20 15:55:44 +00:00
|
|
|
@parse_repository_name
|
2013-09-26 00:00:22 +00:00
|
|
|
@generate_headers
|
2013-09-20 15:55:44 +00:00
|
|
|
def delete_repository_images(namespace, repository):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/v1/repositories/<path:repository>/auth', methods=['PUT'])
|
|
|
|
@parse_repository_name
|
|
|
|
def put_repository_auth(namespace, repository):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/v1/search', methods=['GET'])
|
|
|
|
def get_search():
|
2013-09-20 22:38:17 +00:00
|
|
|
pass
|
2013-09-25 16:45:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route('/_ping')
|
|
|
|
@app.route('/v1/_ping')
|
|
|
|
def ping():
|
|
|
|
response = make_response('true', 200)
|
|
|
|
response.headers['X-Docker-Registry-Version'] = '0.6.0'
|
|
|
|
return response
|