diff --git a/common-functions b/common-functions index 697fcd7..d215074 100755 --- a/common-functions +++ b/common-functions @@ -731,3 +731,15 @@ verify_service_name() { [[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] && dokku_log_fail "$PLUGIN_SERVICE service $SERVICE does not exist" return 0 } + +is_valid_service_name() { + declare desc="Validates a service name" + declare SERVICE="$1" + [[ -z "$SERVICE" ]] && dokku_log_fail "SERVICE must not be null" + + if [[ "$SERVICE" =~ ^[A-Za-z0-9_]+$ ]]; then + return 0 + fi + + return 1 +} diff --git a/functions b/functions index ac6209b..b46132d 100755 --- a/functions +++ b/functions @@ -21,6 +21,7 @@ service_connect() { service_create() { local SERVICE="$1" + is_valid_service_name "$SERVICE" || dokku_log_fail "Please specify a valid name for the service. Valid characters are: [A-Za-z0-9_]+" [[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service" [[ ! -d "$PLUGIN_DATA_ROOT/$SERVICE" ]] || dokku_log_fail "$PLUGIN_SERVICE service $SERVICE already exists" SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"; LINKS_FILE="$SERVICE_ROOT/LINKS" diff --git a/tests/service_create.bats b/tests/service_create.bats index 7ecb87b..777441d 100755 --- a/tests/service_create.bats +++ b/tests/service_create.bats @@ -10,3 +10,11 @@ load test_helper run dokku "$PLUGIN_COMMAND_PREFIX:create" assert_contains "${lines[*]}" "Please specify a name for the service" } + +@test "($PLUGIN_COMMAND_PREFIX:create) error when there is an invalid name specified" { + run dokku "$PLUGIN_COMMAND_PREFIX:create" d.erp + assert_failure + + run dokku "$PLUGIN_COMMAND_PREFIX:create" d-erp + assert_failure +}