Skip to content

Commit 8210258

Browse files
committed
Checkout alignak-backend-import correct branch - fix broken tests
Improve tests and return messages when host/service does not exist and creation is not allowed
1 parent dba79e6 commit 8210258

16 files changed

+1755
-270
lines changed
+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
[module.web-services]
2+
name=web-services
3+
type=web-services
4+
python_name=alignak_module_ws
5+
6+
# Module log level (DEBUG, INFO, WARNING, ERROR)
7+
;log_level=INFO
8+
9+
# ---------------
10+
# Alignak Backend endpoint URL
11+
# ---------------
12+
# If you intend to use an Alignak backend with this module, set its URL.
13+
# As an example: alignak_backend=http://127.0.0.1:5000
14+
# Default is not to use a backend connection
15+
;alignak_backend=
16+
17+
# Backend authentication:
18+
# ---
19+
# [Method 1 - most secure] Using a token:
20+
# Get a user token from the backend:
21+
# $ curl -H "Content-Type: application/json" -X POST -d '{"username":"admin","password":"admin"}' http://127.0.0.1:5000/login
22+
# Copy the returned token here and uncomment this variable:
23+
;token=
24+
25+
# [Method 2] Use login (username/password)
26+
# Set your backend username and password here and uncomment those variables
27+
;username=
28+
;password=
29+
30+
# On login, force a new token generation
31+
;allowgeneratetoken=false
32+
33+
# Alignak backend polling period
34+
# Periodically check that the Alignak backend connection is available
35+
# Use 0 to disable the Alignak backend background polling
36+
;alignak_backend_polling_period=60
37+
38+
39+
# ---------------
40+
# Alignak Backend objects
41+
# ---------------
42+
# Set this variable to 1 to allow the host creation by the WS
43+
# Default is to allow host creation
44+
;allow_host_creation=1
45+
# Raise an error status for an unknown host if host creation is not active
46+
;ignore_unknown_host=0
47+
# Set this variable to 1 to allow the service creation by the WS
48+
# Default is to not allow service creation (services are created through hosts templates)
49+
;allow_service_creation=0
50+
# Do not raise an error status for an unknown service if service creation is not active
51+
;ignore_unknown_service=1
52+
53+
# Realm case in templates
54+
# Use upper, lower or capitalize to update the provided realm
55+
# Default is to let the realm unchanged
56+
# Provided realm name will be updated before searching the realm identifier in the backend
57+
# This is not done if the provided realm is 'All'
58+
;realm_case=
59+
60+
# ---------------
61+
# Alignak arbiter configuration
62+
# ---------------
63+
# Alignak main arbiter interface
64+
# Set alignak_host as empty to disable the Alignak arbiter polling
65+
# The default is to poll a local Alignak arbiter to check it is alive
66+
;alignak_host=127.0.0.1
67+
;alignak_port=7770
68+
69+
# Alignak polling period
70+
# Periodically (every 5 seconds) check that the Alignak arbiter is alive
71+
;alignak_polling_period=5
72+
73+
# Alignak daemons status refresh period
74+
# Periodically get the Alignak daemons status
75+
;alignak_daemons_polling_period=10
76+
77+
78+
# ---------------
79+
# Set timestamp for the external commands if not set
80+
# If no timestamp is set in the external commands, Alignak will set its own timestamp
81+
;set_timestamp=1
82+
# ---------------
83+
84+
# ---------------
85+
# Give result
86+
# Return some information about the commands executed in the _result property
87+
# Default is no result to avoid verbose information
88+
# 0: no result
89+
# 1: send result
90+
;give_result=0
91+
# ---------------
92+
93+
# ---------------
94+
# Give feedback when updating the livestate
95+
# Return some information about the host/service updated
96+
# Default is only host feedback because services feedback is verbose ;)
97+
# 0: no feedback
98+
# 1: feedback only for host
99+
# 2: feedback for host and services
100+
;give_feedback=1
101+
# List of the host/service properties to include in the feedback
102+
# Note that name is always present
103+
#feedback_host
104+
# Some interesting properties for an host and its services:
105+
;feedback_host=alias,notes,location,active_checks_enabled,max_check_attempts,check_interval,retry_interval,passive_checks_enabled,check_freshness,freshness_state,freshness_threshold,_overall_state_id
106+
#feedback_service=alias,notes,active_checks_enabled,max_check_attempts,check_interval,retry_interval,passive_checks_enabled,check_freshness,freshness_state,freshness_threshold,_overall_state_id
107+
# Default is no feedback
108+
;feedback_host=
109+
;feedback_service=
110+
# ---------------
111+
112+
# ---------------
113+
# Direct update of the host/service livestate in the Alignak backend
114+
# This should not be necessary anymore when the backend broker module will get updated
115+
# You can set this option if no backend broker module is installed on your Alignak for the
116+
# WS module to update the backend livestate
117+
;alignak_backend_livestate_update=0
118+
119+
# ---------------
120+
# Send log check result to the backend for old received metrics (defaults to yes)
121+
# deprecated - timeshift is the delay to consider that a received livestate is in the past
122+
# if the received livestate is older than alignak_backend_timeshift seconds, no external
123+
# command will be sent to the scheduler and the check result will not be sent to the backend
124+
# if alignak_backend_get_lcr is 1 then the older log check result will be fetched from the
125+
# backend to update the posted log check result
126+
;alignak_backend_old_lcr=1
127+
;alignak_backend_get_lcr=0
128+
;alignak_backend_timeshift=0
129+
130+
# HTTP authorization
131+
# Setting this variable to 0 will disable the HTTP authorization check;
132+
# it is not the recommended configuration :)
133+
# As a default, authorization is enabled on all the WS endpoints
134+
;authorization=1
135+
136+
# SSL configuration
137+
;use_ssl=0
138+
;ca_cert=/usr/local/etc/alignak/certs/ca.pem
139+
;server_cert=/usr/local/etc/alignak/certs/server.crt
140+
;server_key=/usr/local/etc/alignak/certs/server.key
141+
;server_dh=/usr/local/etc/alignak/certs/server.pem
142+
;hard_ssl_name_check=0
143+
144+
# Export module metrics to a statsd server.
145+
# By default at localhost:8125 (UDP) with the alignak prefix
146+
# Use the same configuration as the one defined in alignak (if any...)
147+
# Stats will be available in statsd_prefix.module_alias
148+
;statsd_host=localhost
149+
;statsd_port=8125
150+
;statsd_prefix=alignak.modules
151+
# Default is not enabled
152+
;statsd_enabled=0

alignak_module_ws/utils/ws_server.py

+41-14
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def protect(*args, **kwargs):
4747

4848
authenticated = False
4949

50-
# A condition is just a callable that returns true or false
50+
# A condition is just a callable that returns true or false
5151
conditions = cherrypy.request.config.get('auth.require', None)
5252
if conditions is not None:
5353
app = cherrypy.request.app.root.app
@@ -72,6 +72,7 @@ def protect(*args, **kwargs):
7272
# Now check if the request includes HTTP Authorization?
7373
authorization = cherrypy.request.headers.get('Authorization')
7474
if authorization:
75+
print("Got authorization header: %s", authorization)
7576
logger.debug("Got authorization header: %s", authorization)
7677
ah = httpauth.parseAuthorization(authorization)
7778

@@ -265,7 +266,7 @@ def alignak_map(self):
265266
@cherrypy.tools.json_in()
266267
@cherrypy.tools.json_out()
267268
@require()
268-
def host(self, name=None):
269+
def host(self, name=None): # pylint: disable=too-many-return-statements
269270
""" Declare an host and its data
270271
:return:
271272
"""
@@ -276,6 +277,9 @@ def host(self, name=None):
276277
# Get an host
277278
# ---
278279
if cherrypy.request.method == "GET":
280+
if not self.app.backend_url:
281+
return {'_status': 'ERR', '_error': 'Not available without backend access.'}
282+
279283
logger.debug("Get /host: %s", cherrypy.request.params)
280284
if cherrypy.request.params.get('name', None) is not None:
281285
name = cherrypy.request.params.get('name', None)
@@ -298,17 +302,26 @@ def host(self, name=None):
298302

299303
_ts = time.time()
300304
logger.debug("Update /host: %s", cherrypy.request.json)
301-
data = {
302-
'active_checks_enabled': cherrypy.request.json.get('active_checks_enabled', None),
303-
'passive_checks_enabled': cherrypy.request.json.get('passive_checks_enabled', None),
304-
'check_freshness': cherrypy.request.json.get('check_freshness', None),
305-
'template': cherrypy.request.json.get('template', None),
306-
'livestate': cherrypy.request.json.get('livestate', None),
307-
'variables': cherrypy.request.json.get('variables', None),
308-
'services': cherrypy.request.json.get('services', None)
309-
}
310-
311-
response = self.app.update_host(name, data)
305+
if not self.app.backend_url:
306+
# Without a backend access, only update the livestate for host and its services
307+
data = {
308+
'livestate': cherrypy.request.json.get('livestate', None),
309+
'services': cherrypy.request.json.get('services', None)
310+
}
311+
312+
response = self.app.update_host(name, data)
313+
else:
314+
data = {
315+
'active_checks_enabled': cherrypy.request.json.get('active_checks_enabled', None),
316+
'passive_checks_enabled': cherrypy.request.json.get('passive_checks_enabled', None),
317+
'check_freshness': cherrypy.request.json.get('check_freshness', None),
318+
'template': cherrypy.request.json.get('template', None),
319+
'livestate': cherrypy.request.json.get('livestate', None),
320+
'variables': cherrypy.request.json.get('variables', None),
321+
'services': cherrypy.request.json.get('services', None)
322+
}
323+
324+
response = self.app.update_host(name, data)
312325

313326
# Specific case where WS client credentials are not authorized
314327
if response and '_issues' in response:
@@ -335,6 +348,12 @@ def hostgroup(self, name=None, embedded=False):
335348
if cherrypy.request.method not in ["GET"]:
336349
return {'_status': 'ERR', '_error': 'You must only GET on this endpoint.'}
337350

351+
if not self.app.backend_url:
352+
return {'_status': 'ERR', '_error': 'Not available without backend access.'}
353+
354+
if not self.app.authorization:
355+
return {'_status': 'ERR', '_error': 'Not available without authorized access.'}
356+
338357
# Get an hostgroup
339358
# ---
340359
logger.debug("Get /hostgroup: %s", cherrypy.request.params)
@@ -432,7 +451,12 @@ def command(self):
432451
command_line = '%s;%s' % (command_line, parameters)
433452

434453
# Add a command to get managed
435-
self.app.to_q.put(ExternalCommand(command_line))
454+
# todo: directly in from_q is better, no?
455+
# self.app.to_q.put(ExternalCommand(command_line))
456+
# -----
457+
logger.debug("Got an external command: %s", command_line)
458+
self.app.from_q.put(ExternalCommand(command_line))
459+
self.app.received_commands += 1
436460

437461
return {'_status': 'OK', '_command': command_line}
438462
command.method = 'post'
@@ -446,6 +470,9 @@ def alignak_logs(self, start=0, count=25, search=''):
446470
:return: True if is alive, False otherwise
447471
:rtype: dict
448472
"""
473+
if not self.app.authorization:
474+
return {'_status': 'ERR', '_error': 'Not available without authorized access.'}
475+
449476
logger.debug("Get /alignak_log: %s", cherrypy.request.params)
450477
start = int(cherrypy.request.params.get('start', '0'))
451478
count = int(cherrypy.request.params.get('count', '25'))

0 commit comments

Comments
 (0)