mirror of
				https://github.com/asterisk/asterisk.git
				synced 2025-10-31 02:37:10 +00:00 
			
		
		
		
	ARI: Creating log channels
An http request can be sent to create a log channel in Asterisk. The command "curl -v -u user:pass -X POST 'http://localhost:088/ari/asterisk/logging/mylog? configuration=notice,warning'" can be run in the terminal to access the newly implemented functionality for ARI. * Ability to create log channels using ARI ASTERISK-25252 Change-Id: I9a20e5c75716dfbb6b62fd3474faf55be20bd782
This commit is contained in:
		| @@ -98,6 +98,14 @@ struct ast_callid; | ||||
| void ast_log_callid(int level, const char *file, int line, const char *function, struct ast_callid *callid, const char *fmt, ...) | ||||
| 	__attribute__((format(printf, 6, 7))); | ||||
|  | ||||
| /*! | ||||
|  * \brief Create a log channel | ||||
|  * | ||||
|  * \param log_channel Log channel to create | ||||
|  * \param components Logging config levels to add to the log channel | ||||
|  */ | ||||
| int ast_logger_create_channel(const char *log_channel, const char *components); | ||||
|  | ||||
| /*! | ||||
|  * \brief Delete the specified log channel | ||||
|  * | ||||
|   | ||||
| @@ -1054,6 +1054,44 @@ static char *handle_logger_show_channels(struct ast_cli_entry *e, int cmd, struc | ||||
| 	return CLI_SUCCESS; | ||||
| } | ||||
|  | ||||
| int ast_logger_create_channel(const char *log_channel, const char *components) | ||||
| { | ||||
| 	struct logchannel *chan; | ||||
| 	struct ast_str *filename = ast_str_create(64); | ||||
| 	int chan_exists = AST_LOGGER_SUCCESS; | ||||
|  | ||||
| 	if (ast_strlen_zero(components)) { | ||||
| 		return AST_LOGGER_DECLINE; | ||||
| 	} | ||||
|  | ||||
| 	if (!filename) { | ||||
| 		return AST_LOGGER_ALLOC_ERROR; | ||||
| 	} | ||||
|  | ||||
| 	ast_str_append(&filename, 0, "%s/%s", ast_config_AST_LOG_DIR, log_channel); | ||||
|  | ||||
| 	AST_RWLIST_WRLOCK(&logchannels); | ||||
|  | ||||
| 	AST_RWLIST_TRAVERSE(&logchannels, chan, list) { | ||||
| 		if (!strcmp(ast_str_buffer(filename), chan->filename)) { | ||||
| 			chan_exists = AST_LOGGER_FAILURE; | ||||
| 			break; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if (!chan_exists) { | ||||
| 		chan = make_logchannel(log_channel, components, 0, 1); | ||||
| 		if (chan) { | ||||
| 			AST_RWLIST_INSERT_HEAD(&logchannels, chan, list); | ||||
| 			global_logmask |= chan->logmask; | ||||
| 			chan_exists = AST_LOGGER_SUCCESS; | ||||
| 		} | ||||
| 	} | ||||
| 	AST_RWLIST_UNLOCK(&logchannels); | ||||
|  | ||||
| 	return chan_exists; | ||||
| } | ||||
|  | ||||
| static char *handle_logger_add_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) | ||||
| { | ||||
| 	struct logchannel *chan; | ||||
|   | ||||
| @@ -366,12 +366,22 @@ int ast_ari_validate_log_channel(struct ast_json *json) | ||||
| { | ||||
| 	int res = 1; | ||||
| 	struct ast_json_iter *iter; | ||||
| 	int has_channel = 0; | ||||
| 	int has_configuration = 0; | ||||
| 	int has_name = 0; | ||||
| 	int has_status = 0; | ||||
| 	int has_type = 0; | ||||
|  | ||||
| 	for (iter = ast_json_object_iter(json); iter; iter = ast_json_object_iter_next(json, iter)) { | ||||
| 		if (strcmp("channel", ast_json_object_iter_key(iter)) == 0) { | ||||
| 			int prop_is_valid; | ||||
| 			has_channel = 1; | ||||
| 			prop_is_valid = ast_ari_validate_string( | ||||
| 				ast_json_object_iter_value(iter)); | ||||
| 			if (!prop_is_valid) { | ||||
| 				ast_log(LOG_ERROR, "ARI LogChannel field channel failed validation\n"); | ||||
| 				res = 0; | ||||
| 			} | ||||
| 		} else | ||||
| 		if (strcmp("configuration", ast_json_object_iter_key(iter)) == 0) { | ||||
| 			int prop_is_valid; | ||||
| 			has_configuration = 1; | ||||
| @@ -382,16 +392,6 @@ int ast_ari_validate_log_channel(struct ast_json *json) | ||||
| 				res = 0; | ||||
| 			} | ||||
| 		} else | ||||
| 		if (strcmp("name", ast_json_object_iter_key(iter)) == 0) { | ||||
| 			int prop_is_valid; | ||||
| 			has_name = 1; | ||||
| 			prop_is_valid = ast_ari_validate_string( | ||||
| 				ast_json_object_iter_value(iter)); | ||||
| 			if (!prop_is_valid) { | ||||
| 				ast_log(LOG_ERROR, "ARI LogChannel field name failed validation\n"); | ||||
| 				res = 0; | ||||
| 			} | ||||
| 		} else | ||||
| 		if (strcmp("status", ast_json_object_iter_key(iter)) == 0) { | ||||
| 			int prop_is_valid; | ||||
| 			has_status = 1; | ||||
| @@ -420,13 +420,13 @@ int ast_ari_validate_log_channel(struct ast_json *json) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if (!has_configuration) { | ||||
| 		ast_log(LOG_ERROR, "ARI LogChannel missing required field configuration\n"); | ||||
| 	if (!has_channel) { | ||||
| 		ast_log(LOG_ERROR, "ARI LogChannel missing required field channel\n"); | ||||
| 		res = 0; | ||||
| 	} | ||||
|  | ||||
| 	if (!has_name) { | ||||
| 		ast_log(LOG_ERROR, "ARI LogChannel missing required field name\n"); | ||||
| 	if (!has_configuration) { | ||||
| 		ast_log(LOG_ERROR, "ARI LogChannel missing required field configuration\n"); | ||||
| 		res = 0; | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -1302,8 +1302,8 @@ ari_validator ast_ari_validate_application_fn(void); | ||||
|  * - attribute: string (required) | ||||
|  * - value: string (required) | ||||
|  * LogChannel | ||||
|  * - channel: string (required) | ||||
|  * - configuration: string (required) | ||||
|  * - name: string (required) | ||||
|  * - status: string (required) | ||||
|  * - type: string (required) | ||||
|  * Module | ||||
|   | ||||
| @@ -628,6 +628,33 @@ void ast_ari_asterisk_reload_module(struct ast_variable *headers, | ||||
| 	ast_ari_response_no_content(response); | ||||
| } | ||||
|  | ||||
| void ast_ari_asterisk_add_log(struct ast_variable *headers, | ||||
| 	struct ast_ari_asterisk_add_log_args *args, | ||||
| 	struct ast_ari_response *response) | ||||
| { | ||||
| 	int res; | ||||
|  | ||||
| 	ast_assert(response != NULL); | ||||
|  | ||||
| 	res = ast_logger_create_channel(args->log_channel_name, args->configuration); | ||||
|  | ||||
| 	if (res == AST_LOGGER_DECLINE) { | ||||
| 		ast_ari_response_error(response, 400, "Bad Request", | ||||
| 			"Configuration levels are required"); | ||||
| 		return; | ||||
| 	} else if (res == AST_LOGGER_FAILURE) { | ||||
| 		ast_ari_response_error(response, 409, "Conflict", | ||||
| 			"Log channel already exists"); | ||||
| 		return; | ||||
| 	} else if (res == AST_LOGGER_ALLOC_ERROR) { | ||||
| 		ast_ari_response_error(response, 500, "Internal Server Error", | ||||
| 			"Allocation failed"); | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	ast_ari_response_no_content(response); | ||||
| } | ||||
|  | ||||
| void ast_ari_asterisk_rotate_log(struct ast_variable *headers, | ||||
| 	struct ast_ari_asterisk_rotate_log_args *args, | ||||
| 	struct ast_ari_response *response) | ||||
|   | ||||
| @@ -194,6 +194,32 @@ struct ast_ari_asterisk_reload_module_args { | ||||
|  * \param[out] response HTTP response | ||||
|  */ | ||||
| void ast_ari_asterisk_reload_module(struct ast_variable *headers, struct ast_ari_asterisk_reload_module_args *args, struct ast_ari_response *response); | ||||
| /*! Argument struct for ast_ari_asterisk_add_log() */ | ||||
| struct ast_ari_asterisk_add_log_args { | ||||
| 	/*! The log channel to add */ | ||||
| 	const char *log_channel_name; | ||||
| 	/*! levels of the log channel */ | ||||
| 	const char *configuration; | ||||
| }; | ||||
| /*! | ||||
|  * \brief Body parsing function for /asterisk/logging/{logChannelName}. | ||||
|  * \param body The JSON body from which to parse parameters. | ||||
|  * \param[out] args The args structure to parse into. | ||||
|  * \retval zero on success | ||||
|  * \retval non-zero on failure | ||||
|  */ | ||||
| int ast_ari_asterisk_add_log_parse_body( | ||||
| 	struct ast_json *body, | ||||
| 	struct ast_ari_asterisk_add_log_args *args); | ||||
|  | ||||
| /*! | ||||
|  * \brief Adds a log channel. | ||||
|  * | ||||
|  * \param headers HTTP headers | ||||
|  * \param args Swagger parameters | ||||
|  * \param[out] response HTTP response | ||||
|  */ | ||||
| void ast_ari_asterisk_add_log(struct ast_variable *headers, struct ast_ari_asterisk_add_log_args *args, struct ast_ari_response *response); | ||||
| /*! Argument struct for ast_ari_asterisk_delete_log() */ | ||||
| struct ast_ari_asterisk_delete_log_args { | ||||
| 	/*! Log channels name */ | ||||
|   | ||||
| @@ -718,6 +718,104 @@ static void ast_ari_asterisk_reload_module_cb( | ||||
| 	} | ||||
| #endif /* AST_DEVMODE */ | ||||
|  | ||||
| fin: __attribute__((unused)) | ||||
| 	return; | ||||
| } | ||||
| int ast_ari_asterisk_add_log_parse_body( | ||||
| 	struct ast_json *body, | ||||
| 	struct ast_ari_asterisk_add_log_args *args) | ||||
| { | ||||
| 	struct ast_json *field; | ||||
| 	/* Parse query parameters out of it */ | ||||
| 	field = ast_json_object_get(body, "configuration"); | ||||
| 	if (field) { | ||||
| 		args->configuration = ast_json_string_get(field); | ||||
| 	} | ||||
| 	return 0; | ||||
| } | ||||
|  | ||||
| /*! | ||||
|  * \brief Parameter parsing callback for /asterisk/logging/{logChannelName}. | ||||
|  * \param get_params GET parameters in the HTTP request. | ||||
|  * \param path_vars Path variables extracted from the request. | ||||
|  * \param headers HTTP headers. | ||||
|  * \param[out] response Response to the HTTP request. | ||||
|  */ | ||||
| static void ast_ari_asterisk_add_log_cb( | ||||
| 	struct ast_tcptls_session_instance *ser, | ||||
| 	struct ast_variable *get_params, struct ast_variable *path_vars, | ||||
| 	struct ast_variable *headers, struct ast_ari_response *response) | ||||
| { | ||||
| 	struct ast_ari_asterisk_add_log_args args = {}; | ||||
| 	struct ast_variable *i; | ||||
| 	RAII_VAR(struct ast_json *, body, NULL, ast_json_unref); | ||||
| #if defined(AST_DEVMODE) | ||||
| 	int is_valid; | ||||
| 	int code; | ||||
| #endif /* AST_DEVMODE */ | ||||
|  | ||||
| 	for (i = get_params; i; i = i->next) { | ||||
| 		if (strcmp(i->name, "configuration") == 0) { | ||||
| 			args.configuration = (i->value); | ||||
| 		} else | ||||
| 		{} | ||||
| 	} | ||||
| 	for (i = path_vars; i; i = i->next) { | ||||
| 		if (strcmp(i->name, "logChannelName") == 0) { | ||||
| 			args.log_channel_name = (i->value); | ||||
| 		} else | ||||
| 		{} | ||||
| 	} | ||||
| 	/* Look for a JSON request entity */ | ||||
| 	body = ast_http_get_json(ser, headers); | ||||
| 	if (!body) { | ||||
| 		switch (errno) { | ||||
| 		case EFBIG: | ||||
| 			ast_ari_response_error(response, 413, "Request Entity Too Large", "Request body too large"); | ||||
| 			goto fin; | ||||
| 		case ENOMEM: | ||||
| 			ast_ari_response_error(response, 500, "Internal Server Error", "Error processing request"); | ||||
| 			goto fin; | ||||
| 		case EIO: | ||||
| 			ast_ari_response_error(response, 400, "Bad Request", "Error parsing request body"); | ||||
| 			goto fin; | ||||
| 		} | ||||
| 	} | ||||
| 	if (ast_ari_asterisk_add_log_parse_body(body, &args)) { | ||||
| 		ast_ari_response_alloc_failed(response); | ||||
| 		goto fin; | ||||
| 	} | ||||
| 	ast_ari_asterisk_add_log(headers, &args, response); | ||||
| #if defined(AST_DEVMODE) | ||||
| 	code = response->response_code; | ||||
|  | ||||
| 	switch (code) { | ||||
| 	case 0: /* Implementation is still a stub, or the code wasn't set */ | ||||
| 		is_valid = response->message == NULL; | ||||
| 		break; | ||||
| 	case 500: /* Internal Server Error */ | ||||
| 	case 501: /* Not Implemented */ | ||||
| 	case 400: /* Bad request body */ | ||||
| 	case 409: /* Log channel could not be created. */ | ||||
| 		is_valid = 1; | ||||
| 		break; | ||||
| 	default: | ||||
| 		if (200 <= code && code <= 299) { | ||||
| 			is_valid = ast_ari_validate_void( | ||||
| 				response->message); | ||||
| 		} else { | ||||
| 			ast_log(LOG_ERROR, "Invalid error response %d for /asterisk/logging/{logChannelName}\n", code); | ||||
| 			is_valid = 0; | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if (!is_valid) { | ||||
| 		ast_log(LOG_ERROR, "Response validation failed for /asterisk/logging/{logChannelName}\n"); | ||||
| 		ast_ari_response_error(response, 500, | ||||
| 			"Internal Server Error", "Response validation failed"); | ||||
| 	} | ||||
| #endif /* AST_DEVMODE */ | ||||
|  | ||||
| fin: __attribute__((unused)) | ||||
| 	return; | ||||
| } | ||||
| @@ -1120,6 +1218,7 @@ static struct stasis_rest_handlers asterisk_logging_logChannelName = { | ||||
| 	.path_segment = "logChannelName", | ||||
| 	.is_wildcard = 1, | ||||
| 	.callbacks = { | ||||
| 		[AST_HTTP_POST] = ast_ari_asterisk_add_log_cb, | ||||
| 		[AST_HTTP_DELETE] = ast_ari_asterisk_delete_log_cb, | ||||
| 	}, | ||||
| 	.num_children = 1, | ||||
|   | ||||
| @@ -300,6 +300,40 @@ | ||||
| 			"path": "/asterisk/logging/{logChannelName}", | ||||
| 			"description": "Asterisk log channel", | ||||
| 			"operations": [ | ||||
| 				{ | ||||
| 					"httpMethod": "POST", | ||||
| 					"summary": "Adds a log channel.", | ||||
| 					"nickname": "addLog", | ||||
| 					"responseClass": "void", | ||||
| 					"parameters": [ | ||||
| 						{ | ||||
| 							"name": "logChannelName", | ||||
| 							"description": "The log channel to add", | ||||
| 							"paramType": "path", | ||||
| 							"required": true, | ||||
| 							"allowMultiple": false, | ||||
| 							"dataType": "string" | ||||
| 						}, | ||||
| 						{ | ||||
| 							"name": "configuration", | ||||
| 							"description": "levels of the log channel", | ||||
| 							"paramType": "query", | ||||
| 							"required": true, | ||||
| 							"allowMultiple": false, | ||||
| 							"dataType": "string" | ||||
| 						} | ||||
| 					], | ||||
| 					"errorResponses": [ | ||||
| 						{ | ||||
| 							"code": 400, | ||||
| 							"reason": "Bad request body" | ||||
| 						}, | ||||
| 						{ | ||||
| 							"code": 409, | ||||
| 							"reason": "Log channel could not be created." | ||||
| 						} | ||||
| 					] | ||||
| 				}, | ||||
| 				{ | ||||
| 					"httpMethod": "DELETE", | ||||
| 					"summary": "Deletes a log channel.", | ||||
| @@ -609,7 +643,7 @@ | ||||
| 					"required": true | ||||
| 				}, | ||||
| 				"configuration": { | ||||
| 					"type": "List[string]", | ||||
| 					"type": "string", | ||||
| 					"description": "The various log levels", | ||||
| 					"required": true | ||||
| 				} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user