GUACAMOLE-1174: Remove option use-exec, add snprintf result validation, fix code formatting.

This commit is contained in:
Yaroslav Nikonorov 2020-09-23 14:50:01 +03:00
parent 7683a17d69
commit 164f792b86
5 changed files with 29 additions and 44 deletions

View File

@ -217,7 +217,6 @@ void* guac_kubernetes_client_thread(void* data) {
settings->kubernetes_namespace,
settings->kubernetes_pod,
settings->kubernetes_container,
settings->use_exec,
settings->exec_command)) {
guac_client_abort(client, GUAC_PROTOCOL_STATUS_SERVER_ERROR,
"Unable to generate path for Kubernetes API endpoint: "

View File

@ -31,7 +31,6 @@ const char* GUAC_KUBERNETES_CLIENT_ARGS[] = {
"namespace",
"pod",
"container",
"use-exec",
"exec-command",
"use-ssl",
"client-cert",
@ -89,12 +88,7 @@ enum KUBERNETES_ARGS_IDX {
IDX_CONTAINER,
/**
* Whether exec call should be used. If omitted, attach call will be used.
*/
IDX_USE_EXEC,
/**
* The command used by exec call.
* The command used by exec call. If omitted, attach call will be used.
*/
IDX_EXEC_COMMAND,
@ -287,15 +281,10 @@ guac_kubernetes_settings* guac_kubernetes_parse_args(guac_user* user,
guac_user_parse_args_string(user, GUAC_KUBERNETES_CLIENT_ARGS, argv,
IDX_CONTAINER, NULL);
/* Parse whether exec call should be used */
settings->use_exec =
guac_user_parse_args_boolean(user, GUAC_KUBERNETES_CLIENT_ARGS, argv,
IDX_USE_EXEC, false);
/* Read exec command (optional) */
settings->exec_command =
guac_user_parse_args_string(user, GUAC_KUBERNETES_CLIENT_ARGS, argv,
IDX_EXEC_COMMAND, GUAC_KUBERNETES_DEFAULT_EXEC_COMMAND);
IDX_EXEC_COMMAND, NULL);
/* Parse whether SSL should be used */
settings->use_ssl =

View File

@ -47,12 +47,6 @@
*/
#define GUAC_KUBERNETES_DEFAULT_NAMESPACE "default"
/**
* The command that should be used by default for exec call if no
* specific command is provided.
*/
#define GUAC_KUBERNETES_DEFAULT_EXEC_COMMAND "/bin/sh"
/**
* The filename to use for the typescript, if not specified.
*/
@ -104,12 +98,8 @@ typedef struct guac_kubernetes_settings {
char* kubernetes_container;
/**
* Whether exec call should be used, default attach.
*/
bool use_exec;
/**
* Exec command, default /bin/sh.
* The command to generate api endpoint for call exec.
* If omitted call attach will be used.
*/
char* exec_command;

View File

@ -91,7 +91,7 @@ int guac_kubernetes_escape_url_component(char* output, int length,
int guac_kubernetes_endpoint_uri(char* buffer, int length,
const char* kubernetes_namespace, const char* kubernetes_pod,
const char* kubernetes_container, int use_exec, const char* exec_command) {
const char* kubernetes_container, const char* exec_command) {
int written;
@ -111,26 +111,32 @@ int guac_kubernetes_endpoint_uri(char* buffer, int length,
return 1;
/* Generate endpoint path depending on the call type */
char* call="attach";
if (use_exec)
char* call = "attach";
if (exec_command != NULL)
call = "exec";
char endpoint_path[GUAC_KUBERNETES_MAX_ENDPOINT_LENGTH];
snprintf(endpoint_path, GUAC_KUBERNETES_MAX_ENDPOINT_LENGTH*3,
"/api/v1/namespaces/%s/pods/%s/%s", escaped_namespace, escaped_pod, call);
written = snprintf(endpoint_path, sizeof(endpoint_path),
"/api/v1/namespaces/%s/pods/%s/%s", escaped_namespace, escaped_pod, call);
if (written < 0 || written >= sizeof(endpoint_path))
return 1;
/* Generate endpoint params */
char endpoint_params[GUAC_KUBERNETES_MAX_ENDPOINT_LENGTH]="";
int param_length=0;
if(use_exec){
if (exec_command != NULL) {
/* Escape exec command */
if (guac_kubernetes_escape_url_component(escaped_exec_command,
sizeof(escaped_exec_command), exec_command))
return 1;
param_length += snprintf(endpoint_params, GUAC_KUBERNETES_MAX_ENDPOINT_LENGTH,
"command=%s&", escaped_exec_command);
written = snprintf(endpoint_params, sizeof(endpoint_params),
"command=%s&", escaped_exec_command);
if (written < 0 || written >= sizeof(endpoint_params))
return 1;
}
if (kubernetes_container != NULL) {
@ -139,13 +145,17 @@ int guac_kubernetes_endpoint_uri(char* buffer, int length,
sizeof(escaped_container), kubernetes_container))
return 1;
snprintf(endpoint_params+param_length, GUAC_KUBERNETES_MAX_ENDPOINT_LENGTH-param_length,
written = snprintf(endpoint_params, sizeof(endpoint_params),
"container=%s&", escaped_container);
if (written < 0 || written >= sizeof(endpoint_params))
return 1;
}
/* Combine path and params to uri */
written = snprintf(buffer, length, "%s?%sstdin=true&stdout=true&tty=true",
endpoint_path, endpoint_params);
endpoint_path, endpoint_params);
/* Endpoint URL was successfully generated if it was written to the given
* buffer without truncation */
return !(written < length - 1);

View File

@ -72,13 +72,10 @@ int guac_kubernetes_escape_url_component(char* output, int length,
* @param kubernetes_container
* The name of the container to attach to, or NULL to arbitrarily attach
* to the first container in the pod.
*
* @param use_exec
* Whether use call exec.
* Execute a command in a container and attach to it instead of main container process.
*
*
* @param exec_command
* The command used in conjunction with exec call.
* The command used to run a new process and attach to it,
* instead of the main container process.
*
* @return
* Zero if the endpoint path was successfully written to the provided
@ -86,7 +83,7 @@ int guac_kubernetes_escape_url_component(char* output, int length,
*/
int guac_kubernetes_endpoint_uri(char* buffer, int length,
const char* kubernetes_namespace, const char* kubernetes_pod,
const char* kubernetes_container, int use_exec, const char* exec_command);
const char* kubernetes_container, const char* exec_command);
#endif