sapling/edenscm/hgext/remotefilelog/edenapi.py
Arun Kulshreshtha b549fab666 remotefilelog: properly check whether Eden API is enabled
Summary: Previously the debug commands for the Eden API did not check whether HTTP data fetching was enabled before trying to access the client singleton, which would result in a crash if HTTP was disabled. This diff adds explicit checks so that the commands will abort instead of crashing.

Reviewed By: quark-zju

Differential Revision: D14692893

fbshipit-source-id: d36e241d8460dadb555a15c92aca9334ba00f34c
2019-03-29 18:52:36 -07:00

51 lines
1.4 KiB
Python

# Copyright 2018 Facebook, Inc.
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 2 or any later version.
from __future__ import absolute_import
from edenscm.mercurial import error, httpconnection
from edenscm.mercurial.i18n import _
from edenscm.mercurial.rust.bindings import edenapi
from . import shallowutil
def enabled(ui):
"""Check whether HTTP data fetching is enabled."""
return ui.configbool("edenapi", "enabled")
def bailifdisabled(ui):
"""Abort if HTTP data fetching is disabled."""
if not enabled(ui):
raise error.Abort(_("HTTP data fetching is disabled"))
def getbaseurl(ui):
"""Get the base URL of the API server."""
url = ui.config("edenapi", "url")
if url is None:
raise error.Abort(_("No Eden API base URL configured"))
return url
def getcreds(ui, url):
"""Get the TLS mutual authentication credentials for the given URL."""
res = httpconnection.readauthforuri(ui, url, None)
if res is None:
return None
group, auth = res
if "cert" not in auth or "key" not in auth:
return None
return (auth["cert"], auth["key"])
def initclient(ui, repo):
"""Initialize a new Eden API client using the user's config."""
url = getbaseurl(ui)
creds = getcreds(ui, url)
cachepath = shallowutil.getcachepath(ui)
return edenapi.client(url, cachepath, repo.name, creds)