forked from DMTF/Redfish-Interface-Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulator_ssl.py
404 lines (332 loc) · 13 KB
/
emulator_ssl.py
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#
# Copyright (c) 2016 Intel Corporation. All Rights Reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# Neither the name of the Distributed Management Task Force (DMTF) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
"""
Emulator Flask Server
"""
import os
import json
import argparse
import traceback
import xml.etree.ElementTree as ET
# Flask Imports
from flask import Flask, request, make_response, render_template, jsonify
from flask.ext.restful import reqparse, Api, Resource
from flask_httpauth import HTTPBasicAuth
# Emulator Imports
from api_emulator import __version__
from api_emulator.resource_manager import ResourceManager
from api_emulator.exceptions import CreatePooledNodeError, ConfigurationError, RemovePooledNodeError
from api_emulator.resource_dictionary import ResourceDictionary
from api_emulator.account_sevice import AccountService
#Restful authentication
auth = HTTPBasicAuth()
#Temporary account service
accountService = AccountService()
@auth.get_password
def get_password(username):
return accountService.getPassword(username)
@auth.error_handler
def unauthorized():
return make_response(jsonify({'error': 'Unauthorized access'}), 401)
# Trays to load into the resource manager
TRAYS = None
SPEC = None
MODE=None
CONFIG = 'emulator-config.json'
# Base URL of the RESTful interface
REST_BASE = '/redfish/v1/'
# Creating the ResourceManager
resource_manager = None
resource_dictionary = None
# Create Flask server
app = Flask(__name__)
# Create RESTful API
api = Api(app)
# Parse REST request for Action
parser = reqparse.RequestParser()
parser.add_argument('Action', type=str, required=True)
# Read emulator-config file.
# If running on Cloud, use dyanaically assigned port
with open(CONFIG, 'r') as f:
config = json.load(f)
MODE = config['MODE']
if(MODE=='Cloud'):
port = int(os.getenv("VCAP_APP_PORT"))
# Execution starts a main(), at end of file
def init_resource_manager():
"""
Initializes the resource manager
"""
global resource_manager
global REST_BASE
global TRAYS
global SPEC
resource_manager = ResourceManager(REST_BASE, SPEC,MODE,TRAYS)
resource_dictionary = ResourceDictionary()
def error_response(msg, status, jsonify=False):
data = {
'Status': status,
'Message': msg
}
if jsonify:
data = json.dumps(data, indent=4)
return data, status
INTERNAL_ERROR = error_response('Internal Server Error', 500)
class PathError(Exception):
pass
@api.representation('application/json')
def output_json(data, code, headers=None):
"""
Overriding how JSON is returned by the server so that it looks nice
"""
resp = make_response(json.dumps(data, indent=4), code)
resp.headers.extend(headers or {})
return resp
class RedfishAPI(Resource):
decorators = [auth.login_required,
accountService.checkPrivilege('Admin', auth.username, unauthorized)]
def __init__(self):
# Dictionary of actions and their method
self.actions = {
'CreateGenericComputerSystem': self.create_system,
'ApplySettings':self.update_system,
'Reset': self.create_system,
'Subscribe': self.subscribe_events }
if resource_manager.spec == 'Redfish':
self.system_path = 'Systems'
self.chassis_path = 'Chassis'
self.actions['ApplySettings']=self.update_system
self.actions['Reset']=self.create_system
self.actions['Subscribe']=self.subscribe_events
super(RedfishAPI, self).__init__()
def post(self, path):
if path.find(self.system_path) != -1 or path.find(self.chassis_path) != -1:
args = parser.parse_args()
try:
action = args['Action']
path = path.split('/')
if len(path) >= 2:
cs_puid = int(path[1])
else:
cs_puid = 0
resp = self.actions[action](action, cs_puid)
except KeyError as e:
traceback.print_exc()
resp = error_response('Unknown action: {0}'.format(e.message), 400)
except Exception:
traceback.print_exc()
resp = INTERNAL_ERROR
elif path == 'EventService':
args = parser.parse_args()
try:
action = args['Action']
resp = self.actions[action](action)
except KeyError as e:
traceback.print_exc()
resp = error_response('Unknown action: {0}'.format(e.message), 400)
except Exception:
traceback.print_exc()
resp = INTERNAL_ERROR
else:
resp = '', 404
return resp
"""
Either return ServiceRoot or let resource manager handel
"""
def get(self, path=None):
try:
if path is not None:
# path has a value
config = self.get_configuration(resource_manager, path)
else:
# path is None, fetch ServiceRoot
config = resource_manager.configuration
resp = config, 200
except PathError:
resp = error_response('Attribute Does Not Exist', 404)
except Exception:
traceback.print_exc()
resp = INTERNAL_ERROR
return resp
def delete(self, path):
"""
Delete pooled node -- ONLY ALLOWS THE DELETION OF A POOLED NODE
"""
try:
path = path.split('/')
assert len(path) == 2
assert path[0] == self.system_path
cs_puid = int(path[1])
resource_manager.remove_pooled_node(cs_puid)
resp = {'Message': 'Pooled node deleted successfully'}, 200
except (AssertionError, ValueError):
resp = error_response('Unknown DELETE request - this is only for pooled nodes', 404)
except RemovePooledNodeError as e:
resp = error_response(e.message, 400)
except Exception:
traceback.print_exc()
resp = INTERNAL_ERROR
return resp
@staticmethod
def create_system(action, idx=1):
global resource_manager
try:
assert request.json is not None, 'No JSON configuration given'
if(action =='Reset'):
cs_puid = idx
config = resource_manager.update_cs(cs_puid,request.json)
else:
config = resource_manager._create_redfish(request.json, action)
resp = config, 201
except CreatePooledNodeError as e:
resp = error_response(e.message, 406)
except AssertionError as e:
resp = error_response(e.message, 400)
except Exception:
traceback.print_exc()
resp = INTERNAL_ERROR
return resp
@staticmethod
def update_system(action, idx=1):
global resource_manager
try:
assert request.json is not None, 'No JSON configuration given'
config = resource_manager.update_system(request.json, idx)
resp = config, 201
except CreatePooledNodeError as e:
resp = error_response(e.message, 406)
except AssertionError as e:
resp = error_response(e.message, 400)
except Exception:
traceback.print_exc()
resp = INTERNAL_ERROR
return resp
@staticmethod
def subscribe_events(action, idx=1):
global resource_manager
try:
assert request.json is not None, 'No JSON configuration given'
config = resource_manager.add_event_subscription(request.json)
resp = config, 201
except CreatePooledNodeError as e:
resp = error_response(e.message, 406)
except AssertionError as e:
resp = error_response(e.message, 400)
except Exception:
traceback.print_exc()
resp = INTERNAL_ERROR
return resp
@staticmethod
def get_configuration(obj, path):
"""
Helper function to follow the given path starting with the given object
Arguments:
obj - Beginning object to start searching down. obj should have a get_resource()
path - Path of object to get
"""
try:
config = obj.get_resource(path)
except (IndexError, AttributeError, TypeError, AssertionError, KeyError) as e:
traceback.print_exc()
raise PathError("Resource not found: " + str(e.message))
return config
#
# If DELETE /redfish/v1/reset, then reset the resource manager
#
@app.route('/redfish/v1/reset/', methods=['DELETE'])
@auth.login_required
def reset():
try:
init_resource_manager()
data = {'Message': 'Emulator reset successfully'}
resp = json.dumps(data, indent=4), 200
except Exception:
traceback.print_exc()
resp = error_response('Internal Server Error', 500, True)
return resp
#
# If GET /, then return index.html (an intro page)
#
@app.route('/')
@auth.login_required
def index():
return render_template('index.html')
#
# If any other RESTful request, send to RedfishAPI object for processing. Note: <path:path> specifies any path
#
api.add_resource(RedfishAPI, '/redfish/v1/', '/redfish/v1/<path:path>/')
def startup():
"""
Startup method -- Reads in the configuration file
"""
global CONFIG
global TRAYS
global SPEC
global MODE
with open(CONFIG, 'r') as f:
config = json.load(f)
TRAYS = config['TRAYS']
SPEC = config['SPEC']
MODE = config['MODE']
assert SPEC == 'Redfish', \
'Unknown spec: {0}, must be Redfish'.format(SPEC)
init_resource_manager()
def main():
"""
Main Method
"""
global app
global MODE
with open(CONFIG, 'r') as f:
config = json.load(f)
MODE = config['MODE']
if(MODE=='Cloud'):
argparser = argparse.ArgumentParser(
version=__version__,
description='Redfish Manageability API Emulator - Version: ' + __version__,
epilog='Developed by Intel')
argparser.add_argument('-port', type=int, default=port,
help='Port to run the emulator on. Default define by Foundry')
argparser.add_argument('-debug', action='store_true', default=False,
help='Run the emulator in debug mode. Note that if you'
' run in debug mode, then the emulator will only'
'be ran locally.')
args = argparser.parse_args()
elif(MODE=='Local'):
argparser = argparse.ArgumentParser(
version=__version__,
description='Redfish Manageability API Emulator - Version: ' + __version__,
epilog='Developed by Intel')
argparser.add_argument('-port', type=int, default=5000,
help='Port to run the emulator on. Default is 5000')
argparser.add_argument('-debug', action='store_true', default=False,
help='Run the emulator in debug mode. Note that if you'
' run in debug mode, then the emulator will only'
'be ran locally.')
args = argparser.parse_args()
try:
startup()
except ConfigurationError as e:
print 'Error Loading Trays:', e.message
else:
context = ('server.crt', 'server.key')
kwargs = {'debug': args.debug, 'port': args.port, 'ssl_context' : context}
if not args.debug:
kwargs['host'] = '0.0.0.0'
print ' * Running in', SPEC, 'mode'
app.run(**kwargs)
if __name__ == '__main__':
main()
else:
startup()