diff --git a/config_app/config_endpoints/api/superuser.py b/config_app/config_endpoints/api/superuser.py index 37c207383..b7528820b 100644 --- a/config_app/config_endpoints/api/superuser.py +++ b/config_app/config_endpoints/api/superuser.py @@ -36,8 +36,8 @@ class SuperUserCustomCertificate(ApiResource): # Validate the certificate. try: logger.debug('Loading custom certificate %s', certpath) - cert = config_provider.get_volume_file(cert_full_path) - load_certificate(cert) + with config_provider.get_volume_file(cert_full_path) as f: + load_certificate(f.read()) except CertInvalidException: logger.exception('Got certificate invalid error for cert %s', certpath) return '', 204 @@ -70,13 +70,14 @@ class SuperUserCustomCertificates(ApiResource): cert_views = [] for extra_cert_path in extra_certs_found: try: - cert = config_provider.get_volume_file(extra_cert_path) - certificate = load_certificate(cert) - cert_views.append({ - 'path': extra_cert_path, - 'names': list(certificate.names), - 'expired': certificate.expired, - }) + cert_full_path = config_provider.get_volume_path(EXTRA_CA_DIRECTORY, extra_cert_path) + with config_provider.get_volume_file(cert_full_path) as f: + certificate = load_certificate(f.read()) + cert_views.append({ + 'path': extra_cert_path, + 'names': list(certificate.names), + 'expired': certificate.expired, + }) except CertInvalidException as cie: cert_views.append({ 'path': extra_cert_path, diff --git a/config_app/config_endpoints/api/tar_config_loader.py b/config_app/config_endpoints/api/tar_config_loader.py index a63d6b5f5..63b57d214 100644 --- a/config_app/config_endpoints/api/tar_config_loader.py +++ b/config_app/config_endpoints/api/tar_config_loader.py @@ -1,5 +1,4 @@ import tarfile -import cStringIO from flask import request, make_response @@ -13,16 +12,12 @@ class TarConfigLoader(ApiResource): """ Resource for validating a block of configuration against an external service. """ @nickname('uploadTarballConfig') - def post(self): + def put(self): """ Loads tarball config into the config provider """ input_stream = request.stream + tar_stream = tarfile.open(mode="r|gz", fileobj=input_stream) - # since we're working with a tar file, shouldn't be larger than ~20KB, so just read the whole thing into mem - buf = input_stream.read() - config = tarfile.open(mode="r:gz", fileobj=cStringIO.StringIO(buf)) - - # TODO(sam): refactor config provider to accept a stream write to avoid loading into memory - config_provider.load_from_tarball(config) + config_provider.load_from_tar_stream(tar_stream) # now try to connect to the db provided in their config combined = dict(**app.config) diff --git a/config_app/config_util/config/inmemoryprovider.py b/config_app/config_util/config/inmemoryprovider.py index 0f018f385..638c5458a 100644 --- a/config_app/config_util/config/inmemoryprovider.py +++ b/config_app/config_util/config/inmemoryprovider.py @@ -39,7 +39,7 @@ class InMemoryProvider(BaseProvider): return any([ name.startswith(filename) for name in self.files ]) def get_volume_file(self, filename, mode='r'): - return self.files[filename] + return io.BytesIO(self.files[filename]) def write_volume_file(self, filename, contents): raise Exception('Not implemented yet') @@ -48,7 +48,12 @@ class InMemoryProvider(BaseProvider): raise Exception('Not implemented yet') def list_volume_directory(self, path): - return [ name for name in self.files if name.startswith(path) ] + def strip_directory(string): + if '/' in string: + return string[string.rfind('/') + 1:] + return string + + return [ strip_directory(name) for name in self.files if name.startswith(path) ] def save_volume_file(self, filename, flask_file): self.files[filename] = flask_file.read() @@ -66,9 +71,17 @@ class InMemoryProvider(BaseProvider): def load_from_tarball(self, tarfile): for tarinfo in tarfile.getmembers(): if tarinfo.isfile(): - if tarinfo.name == CONFIG_FILENAME: - self.config = yaml.load(tarfile.extractfile(tarinfo.name).read()) - else: - self.files[tarinfo.name] = tarfile.extractfile(tarinfo.name).read() - self.was_loaded = True + self.files[tarinfo.name] = tarfile.extractfile(tarinfo.name).read() + if self.files.has_key(CONFIG_FILENAME): + self.config = yaml.load(self.files.get(CONFIG_FILENAME)) + self.was_loaded = True + + def load_from_tar_stream(self, tarfile): + for tarinfo in tarfile: + if tarinfo.isfile(): + self.files[tarinfo.name] = tarfile.extractfile(tarinfo).read() + + if self.files.has_key(CONFIG_FILENAME): + self.config = yaml.load(self.files.get(CONFIG_FILENAME)) + self.was_loaded = True diff --git a/config_app/js/components/file-upload-box.js b/config_app/js/components/file-upload-box.js index 280517eeb..ae2f6ec3a 100644 --- a/config_app/js/components/file-upload-box.js +++ b/config_app/js/components/file-upload-box.js @@ -33,9 +33,9 @@ angular.module('quay-config').directive('fileUploadBox', function () { $scope.state = 'clear'; $scope.selectedFiles = []; - var conductUpload = function(file, url, fileId, mimeType, progressCb, doneCb) { + var conductUpload = function(file, apiEndpoint, fileId, mimeType, progressCb, doneCb) { var request = new XMLHttpRequest(); - request.open('POST', url, true); + request.open('PUT', '/api/v1/' + apiEndpoint, true); request.setRequestHeader('Content-Type', mimeType); request.onprogress = function(e) { $scope.$apply(function() { diff --git a/config_app/js/components/load-config/load-config.html b/config_app/js/components/load-config/load-config.html index fabcfa327..1a4d101e6 100644 --- a/config_app/js/components/load-config/load-config.html +++ b/config_app/js/components/load-config/load-config.html @@ -11,7 +11,7 @@