aboutsummaryrefslogtreecommitdiff
blob: bcbdaa146137d1aebd8ebfafe85e069825073962 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) 2017, Alice Ferrazzi <alice.ferrazzi@gmail.com>
# Distributed under the terms of the GNU General Public License v2 or later


import uuid

import os
import werkzeug
from flask import jsonify, make_response
from flask_restful import Resource, reqparse, fields, marshal

from elivepatch_server.resources.livepatch import PaTch

pack_fields = {
    'KernelVersion': fields.String,
    'LivepatchStatus': fields.String,
    'UserID': fields.String

}

packs = {
    'id': 1,
    'KernelVersion': None,
    'LivepatchStatus': None,
    'UserID': None
}


def set_kernel_dir(kernel_ID):
    kernel_absolute_path = 'linux-' + str(kernel_ID) + '-gentoo'
    kernel_path = os.path.join('/usr','src',kernel_absolute_path)
    lpatch.set_kernel_dir(kernel_path)

lpatch = PaTch()
kernel_dir = lpatch.get_kernel_dir()
set_kernel_dir(kernel_dir)


class BuildLivePatch(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('KernelVersion', type=str, required=False,
                                   help='No task title provided',
                                   location='json')
        self.reqparse.add_argument('LivepatchStatus', type=str, required=False,
                                   help='No task title provided',
                                   location='json')
        self.reqparse.add_argument('UserID', type=str, required=False,
                                   help='No task title provided',
                                   location='json')
        super(BuildLivePatch, self).__init__()
        pass

    def get(self):
        # lpatch.build_livepatch(kernel_dir, kernel_dir + '/vmlinux')
        return {'packs': [marshal(pack, pack_fields) for pack in packs]}

    def post(self):
        args = self.reqparse.parse_args()
        if not args['UserID']:
            args['UserID'] = id_generate()
        else:
            print('UserID: ' + str(args['UserID']))
        if args['KernelVersion']:
            set_kernel_dir(args['KernelVersion'])
            kernel_dir = lpatch.get_kernel_dir()
            kernel_config = lpatch.get_config()
            kernel_patch = lpatch.get_patch()
            if kernel_config and kernel_patch:
                lpatch.set_lp_status('working')
                print("build livepatch: " + str(args))
                # check vmlinux presence if not rebuild the kernel
                kernel_vmlinux = os.path.join(kernel_dir, 'vmlinux')
                if not os.path.isdir(kernel_dir):
                    lpatch.get_kernel(kernel_dir, kernel_vmlinux)
                if not os.path.isfile(kernel_vmlinux):
                    lpatch.build_kernel(kernel_dir)
                lpatch.build_livepatch(kernel_dir, kernel_vmlinux)
        pack = {
            'id': packs['id'] + 1,
            'KernelVersion': args['KernelVersion'],
            'LivepatchStatus': lpatch.livepatch_status,
            'UserID' : args['UserID']
        }
        return {'build_livepatch': marshal(pack, pack_fields)}, 201


class GetLivePatch(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('KernelVersion', type=str, required=False,
                                   help='No task title provided',
                                   location='json')
        self.reqparse.add_argument('UserID', type=str, required=False,
                                   help='No task title provided',
                                   location='json')
        super(GetLivePatch, self).__init__()
        pass

    def get(self):
        args = self.reqparse.parse_args()
        print("get livepatch: " + str(args))
        # check if is a new user
        if not args['UserID']:
            args['UserID'] = id_generate()
        else:
            print('UserID: ' + str(args['UserID']))
        # Getting livepatch build status
        status = lpatch.update_lp_status("kpatch-1.ko")
        if status == 'done':
            with open('kpatch-1.ko', 'rb') as fp:
                response = make_response(fp.read())
                response.headers['content-type'] = 'application/octet-stream'
                return response
        return {'packs': [marshal(pack, pack_fields) for pack in packs]}

    def post(self):
        return make_response(jsonify({'message': 'These are not the \
        patches you are looking for'}), 403)


class GetConfig(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('KernelVersion', type=str, required=False,
                                   help='No task title provided',
                                   location='headers')
        self.reqparse.add_argument('UserID', type=str, required=False,
                                   help='No task title provided',
                                   location='headers')
        super(GetConfig, self).__init__()
        pass

    def get(self):
        return make_response(jsonify({'message': 'These are not the \
        patches you are looking for'}), 403)

    def post(self):
        args = self.reqparse.parse_args()
        print("json get config: " + str(args))
        if not args['UserID']:
            args['UserID'] = str(id_generate())
        else:
            print('UserID: ' + str(args['UserID']))
        parse = reqparse.RequestParser()
        parse.add_argument('file', type=werkzeug.datastructures.FileStorage,
                           location='files')
        file_args = parse.parse_args()
        print("file get config: " + str(file_args))
        configFile = file_args['file']
        configFile_name = file_args['file'].filename

        # debug filename
        #print('configfile_name: '+ str(configFile_name))
        #print('configfile: '+ str(configFile))

        configFile_name = os.path.join('/tmp','elivepatch-' + args['UserID'], configFile_name)
        if not os.path.exists('/tmp/elivepatch-' + args['UserID']):
            os.makedirs('/tmp/elivepatch-' + args['UserID'])
        configFile.save(configFile_name)
        lpatch.set_config(configFile_name)
        pack = {
            'id': packs['id'] + 1,
            'KernelVersion': None,
            'UserID' : args['UserID']
        }
        return {'get_config': marshal(pack, pack_fields)}, 201


class GetPatch(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('KernelVersion', type=str, required=False,
                                   help='No task title provided',
                                   location='json')
        self.reqparse.add_argument('UserID', type=str, required=False,
                                   help='No task title provided',
                                   location='headers')
        super(GetPatch, self).__init__()
        pass

    def get(self):
        return make_response(jsonify({'message': 'These are not the \
        patches you are looking for'}), 403)

    def post(self):
        args = self.reqparse.parse_args()
        print("get patch: " + str(args))
        if not args['UserID']:
            args['UserID'] = str(id_generate())
        else:
            print('UserID: ' + str(args['UserID']))

        # parse file request information's
        parse = reqparse.RequestParser()
        parse.add_argument('file', type=werkzeug.datastructures.FileStorage,
                           location='files')
        file_args = parse.parse_args()
        patchFile = file_args['file']
        patchFile_name = file_args['file'].filename
        #print(audioFile_name)
        #print(audioFile)
        patchFile_name = os.path.join('/tmp', 'elivepatch-' + args['UserID'], patchFile_name)
        if not os.path.exists('/tmp/elivepatch-'+args['UserID']):
            os.makedirs('/tmp/elivepatch-'+args['UserID'])
        patchFile.save(patchFile_name)
        lpatch.set_patch(patchFile_name)
        pack = {
            'id': packs['id'] + 1,
            'KernelVersion': None,
            'UserID' : args['UserID']
        }
        return {'get_patch': marshal(pack, pack_fields)}, 201


class GetID(Resource):

    def __init__(self):
        self.reqparse = reqparse.RequestParser()
        self.reqparse.add_argument('UserID', type=str, required=False,
                                   help='No task title provided',
                                   location='json')
        super(GetID, self).__init__()
        pass

    def get(self):
        return make_response(jsonify({'message': 'These are not the \
        patches you are looking for'}), 403)

    def post(self):
        args = self.reqparse.parse_args()
        print("get ID: " + str(args))


def id_generate():
    UserID = uuid.uuid4()
    return UserID