Commit 2586f844 authored by Titouan Soulard's avatar Titouan Soulard

slapos: use Software Instance certificate/key pair for request

parent 9f79a6dd
...@@ -1629,7 +1629,16 @@ stderr_logfile_backups=1 ...@@ -1629,7 +1629,16 @@ stderr_logfile_backups=1
# even if something is terribly wrong while processing an instance, it # even if something is terribly wrong while processing an instance, it
# won't prevent processing other ones. # won't prevent processing other ones.
if not self.api_backward_compatibility: if not self.api_backward_compatibility:
computer_partition = self.slap.jio_api_connector.get(computer_partition["get_parameters"]) # Try to use partition certificate
# TODO: find a clean way to request the certificate
partition_certificate_file = os.path.join(self.certificate_repository_path, computer_partition["compute_partition_id"] + ".crt")
partition_key_file = os.path.join(self.certificate_repository_path, computer_partition["compute_partition_id"] + ".key")
if os.path.exists(partition_certificate_file) and os.path.exists(partition_key_file):
computer_partition = self.slap.jio_api_connector.get(computer_partition["get_parameters"],
cert_key=(partition_certificate_file, partition_key_file))
else:
computer_partition = self.slap.jio_api_connector.get(computer_partition["get_parameters"])
try: try:
# Process the partition itself # Process the partition itself
self.processComputerPartition(computer_partition) self.processComputerPartition(computer_partition)
......
...@@ -87,7 +87,7 @@ class ConnectionHelper: ...@@ -87,7 +87,7 @@ class ConnectionHelper:
cache=FileCache(os.path.expanduser("~/.slapos_cached_get"))) cache=FileCache(os.path.expanduser("~/.slapos_cached_get")))
def do_request(self, method, path, params=None, data=None, headers=None, def do_request(self, method, path, params=None, data=None, headers=None,
expect_json_error=False): expect_json_error=False, cert_key=None):
url = parse.urljoin(self.slapgrid_uri, path) url = parse.urljoin(self.slapgrid_uri, path)
if headers is None: if headers is None:
headers = {} headers = {}
...@@ -97,10 +97,8 @@ class ConnectionHelper: ...@@ -97,10 +97,8 @@ class ConnectionHelper:
# raise ValueError('method path should be relative: %s' % path) # raise ValueError('method path should be relative: %s' % path)
try: try:
if url.startswith('https'): if url.startswith('https') and cert_key is None:
cert = (self.cert_file, self.key_file) cert_key = (self.cert_file, self.key_file)
else:
cert = None
# XXX TODO: handle host cert verify # XXX TODO: handle host cert verify
...@@ -114,7 +112,7 @@ class ConnectionHelper: ...@@ -114,7 +112,7 @@ class ConnectionHelper:
req = method(url=url, req = method(url=url,
params=params, params=params,
cert=cert, cert=cert_key,
verify=False, verify=False,
data=data, data=data,
headers=headers, headers=headers,
......
...@@ -796,30 +796,34 @@ def json_loads_byteified(json_text): ...@@ -796,30 +796,34 @@ def json_loads_byteified(json_text):
) )
class JioAPIConnectionHelper(ConnectionHelper): class JioAPIConnectionHelper(ConnectionHelper):
def apiCall(self, path, data, cert_key=None):
def apiCall(self, path, data):
req = self.do_request(requests.post, req = self.do_request(requests.post,
path=path, path=path,
data=json.dumps(data), data=json.dumps(data),
headers={'Content-type': 'application/json'}, headers={'Content-type': 'application/json'},
expect_json_error=True) expect_json_error=True,
cert_key=cert_key)
return json_loads_byteified(req.text) return json_loads_byteified(req.text)
def get(self, data): def get(self, data, cert_key=None):
return self.apiCall(path="get/", return self.apiCall(path="get/",
data=data) data=data,
cert_key=cert_key)
def post(self, data): def post(self, data, cert_key=None):
return self.apiCall(path="post/", return self.apiCall(path="post/",
data=data) data=data,
cert_key=cert_key)
def put(self, data): def put(self, data, cert_key=None):
return self.apiCall(path="put/", return self.apiCall(path="put/",
data=data) data=data,
cert_key=cert_key)
def allDocs(self, data): def allDocs(self, data, cert_key=None):
return self.apiCall(path="allDocs/", return self.apiCall(path="allDocs/",
data=data) data=data,
cert_key=cert_key)
getHateoasUrl_cache = {} getHateoasUrl_cache = {}
getjIOAPI_cache = {} getjIOAPI_cache = {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment