aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--elivepatch_client/client/checkers.py84
-rw-r--r--elivepatch_client/client/cli.py8
-rw-r--r--elivepatch_client/client/restful.py25
3 files changed, 84 insertions, 33 deletions
diff --git a/elivepatch_client/client/checkers.py b/elivepatch_client/client/checkers.py
index d5a7b74..b01a6a8 100644
--- a/elivepatch_client/client/checkers.py
+++ b/elivepatch_client/client/checkers.py
@@ -4,14 +4,15 @@
# (c) 2017, Alice Ferrazzi <alice.ferrazzi@gmail.com>
# Distributed under the terms of the GNU General Public License v2 or later
-import os
-from git import Repo
-from elivepatch_client.client import restful
import gzip
+import shelve
+
import os
import os.path
import re
-import shelve
+from git import Repo
+
+from elivepatch_client.client import restful
class Kernel(object):
@@ -27,6 +28,8 @@ class Kernel(object):
def get_version(self):
tmp = os.uname()[2]
version = tmp
+ if '-' in version:
+ version = version.split('-')[0]
return version
def set_config(self, config_path):
@@ -36,9 +39,11 @@ class Kernel(object):
self.patch = patch_path
def send_config(self):
- d = shelve.open('userid')
+ # debug print
print('conifg path: '+ str(self.config) + 'server url: ' + str(self. url))
print (os.path.basename(self.config))
+
+ # check the configuration file
path, file = (os.path.split(self.config))
if re.findall("[.]gz\Z", self.config):
print('gz extension')
@@ -46,27 +51,70 @@ class Kernel(object):
path, file = f_action.ungz()
# if the file is .gz the configuration path is the tmp folder uncompressed config file
self.config = os.path.join(path,file)
- # we are sending only uncompressed configuration files
+
+ # check userID
+ data_store = shelve.open('userid')
+
+ # get old userid if present
+ try:
+ old_userid = data_store['UserID']
+ except:
+ old_userid = None
+ print('no UserID')
+
+ # send only uncompressed config
replay = self.rest_manager.send_file(self.config, file, '/elivepatch/api/v1.0/config')
+
+ # get userid returned from the server
userid = replay['get_config']['UserID']
- old_userid = d['UserID']
- if userid:
- print(userid)
- d['UserID'] = userid
- d.close()
+ self.rest_manager.set_user_id(userid)
+ # check if the userid is new
+ if userid:
+ try:
+ if userid != old_userid:
+ print('new userid: ' + str(userid))
+ data_store['UserID'] = userid
+ data_store.close()
+ except:
+ pass
def send_patch(self):
- d = shelve.open('userid')
print("self.patch: "+ self.patch + ' url: '+ self.url)
path, file = (os.path.split(self.patch))
print('file :'+ file)
- replay = self.rest_manager.send_file(self.patch, file, '/elivepatch/api/v1.0/patch')
- new_userid = replay['get_patch']['UserID']
- if new_userid:
- print(new_userid)
- d['UserID'] = new_userid
- d.close()
+
+ data_store = shelve.open('userid')
+
+ # get old userid if present
+ try:
+ old_userid = data_store['UserID']
+ except:
+ old_userid = None
+ print('no UserID')
+
+ # send only uncompressed config
+ replay = self.rest_manager.send_file(self.config, file, '/elivepatch/api/v1.0/patch')
+
+ print(replay)
+ # get userid returned from the server
+ userid = replay['get_patch']['UserID']
+
+ # get old userid if present
+ try:
+ old_userid = data_store['UserID']
+ except:
+ print('no UserID')
+
+ if userid:
+ try:
+ if userid != old_userid:
+ self.rest_manager.set_user_id(userid)
+ print(userid)
+ data_store['UserID'] = userid
+ data_store.close()
+ except:
+ pass
def build_livepatch(self):
self.rest_manager.build_livepatch()
diff --git a/elivepatch_client/client/cli.py b/elivepatch_client/client/cli.py
index 90fbb8e..556b122 100644
--- a/elivepatch_client/client/cli.py
+++ b/elivepatch_client/client/cli.py
@@ -4,13 +4,12 @@
# (c) 2017, Alice Ferrazzi <alice.ferrazzi@gmail.com>
# Distributed under the terms of the GNU General Public License v2 or later
-import os, sys
-from elivepatch_client.client.checkers import CVE
+import sys
+
from elivepatch_client.client.checkers import Kernel
from elivepatch_client.client.restful import ManaGer
from elivepatch_client.client.version import VERSION
-
if sys.hexversion >= 0x30200f0:
ALL_KEYWORD = b'ALL'
else:
@@ -18,7 +17,8 @@ else:
class Main(object):
- """Performs the actions the user selected.
+ """
+ Performs the actions selected by the user
"""
def __init__(self, argparser):
diff --git a/elivepatch_client/client/restful.py b/elivepatch_client/client/restful.py
index 9329411..0c3384f 100644
--- a/elivepatch_client/client/restful.py
+++ b/elivepatch_client/client/restful.py
@@ -9,9 +9,17 @@ import requests
class ManaGer(object):
+
def __init__(self, server_url, kernel_version):
self.server_url = server_url
self.kernel_version = kernel_version
+ self.user_id = None
+
+ def set_user_id(self, user_id):
+ self.user_id = user_id
+
+ def get_user_id(self):
+ return self.user_id
def version(self):
url = self.server_url + '/elivepatch/api/v1.0/agent'
@@ -25,26 +33,22 @@ class ManaGer(object):
# The server is dividing user by UserID
# UserID is generated with python UUID
# TODO: add the UserID in the json location instead of headers
- headers = {'UserID': 'test-00001'}
+ headers = {
+ 'KernelVersion' : 'aaaa',
+ 'UserID': self.user_id
+ }
files = {'file': (name_file, open(send_file, 'rb'), 'multipart/form-data', {'Expires': '0'})}
- payload = {
- "KernelVersion" : ("aaaa", 'application/json', {'Expires': '0'}),
- "LivepatchStatus" : ("not known", 'application/json', {'Expires': '0'}),
- "UserID" : ("test-00001",'application/json', {'Expires': '0'})
- }
r = requests.post(url, files=files, headers=headers)
print('send file: ' + str(r.json()))
r_dict = r.json()
return r_dict
-
def build_livepatch(self):
url = self.server_url+'/elivepatch/api/v1.0/build_livepatch'
payload = {
'KernelVersion': self.kernel_version,
- 'LivepatchStatus' : 'no idea',
- 'UserID' : 'test-0000'
+ 'UserID' : self.user_id
}
r = requests.post(url, json=payload)
# print(r.text)
@@ -55,8 +59,7 @@ class ManaGer(object):
url = self.server_url+'/elivepatch/api/v1.0/get_livepatch'
payload = {
'KernelVersion': self.kernel_version,
- 'LivepatchStatus' : 'no idea',
- 'UserID' : 'test-0000'
+ 'UserID' : 'test-0001'
}
r = requests.get(url, json=payload)
if r.status_code == requests.codes.ok: # livepatch returned ok