Move instance name validation to handlers

This commit is contained in:
2025-10-22 18:40:39 +02:00
parent 0f2c14d3ed
commit c794e4f98b
3 changed files with 65 additions and 103 deletions

View File

@@ -4,7 +4,6 @@ import (
"context"
"fmt"
"llamactl/pkg/instance"
"llamactl/pkg/validation"
"log"
)
@@ -58,12 +57,7 @@ func (im *instanceManager) CreateInstance(name string, options *instance.Options
return nil, fmt.Errorf("instance options cannot be nil")
}
name, err := validation.ValidateInstanceName(name)
if err != nil {
return nil, err
}
err = options.BackendOptions.ValidateInstanceOptions()
err := options.BackendOptions.ValidateInstanceOptions()
if err != nil {
return nil, err
}

View File

@@ -8,7 +8,6 @@ import (
"io"
"llamactl/pkg/config"
"llamactl/pkg/instance"
"llamactl/pkg/validation"
"net/http"
"net/url"
"sync"
@@ -139,16 +138,9 @@ func parseRemoteResponse(resp *http.Response, result any) error {
// --- Remote CRUD operations ---
// CreateInstance creates a new instance on a remote node.
// createInstance creates a new instance on a remote node.
func (rm *remoteManager) createInstance(ctx context.Context, node *config.NodeConfig, name string, opts *instance.Options) (*instance.Instance, error) {
// Validate instance name to prevent injection attacks
validatedName, err := validation.ValidateInstanceName(name)
if err != nil {
return nil, err
}
// URL-encode the validated instance name to safely include it in the URL path
escapedName := url.PathEscape(validatedName)
escapedName := url.PathEscape(name)
path := fmt.Sprintf("%s%s/", apiBasePath, escapedName)
@@ -165,16 +157,10 @@ func (rm *remoteManager) createInstance(ctx context.Context, node *config.NodeCo
return &inst, nil
}
// GetInstance retrieves an instance by name from a remote node.
// getInstance retrieves an instance by name from a remote node.
func (rm *remoteManager) getInstance(ctx context.Context, node *config.NodeConfig, name string) (*instance.Instance, error) {
// Validate instance name to prevent injection attacks
validatedName, err := validation.ValidateInstanceName(name)
if err != nil {
return nil, err
}
// URL-encode the validated instance name to safely include it in the URL path
escapedName := url.PathEscape(validatedName)
escapedName := url.PathEscape(name)
path := fmt.Sprintf("%s%s/", apiBasePath, escapedName)
resp, err := rm.makeRemoteRequest(ctx, node, "GET", path, nil)
@@ -190,16 +176,10 @@ func (rm *remoteManager) getInstance(ctx context.Context, node *config.NodeConfi
return &inst, nil
}
// UpdateInstance updates an existing instance on a remote node.
// updateInstance updates an existing instance on a remote node.
func (rm *remoteManager) updateInstance(ctx context.Context, node *config.NodeConfig, name string, opts *instance.Options) (*instance.Instance, error) {
// Validate instance name to prevent injection attacks
validatedName, err := validation.ValidateInstanceName(name)
if err != nil {
return nil, err
}
// URL-encode the validated instance name to safely include it in the URL path
escapedName := url.PathEscape(validatedName)
escapedName := url.PathEscape(name)
path := fmt.Sprintf("%s%s/", apiBasePath, escapedName)
@@ -216,16 +196,10 @@ func (rm *remoteManager) updateInstance(ctx context.Context, node *config.NodeCo
return &inst, nil
}
// DeleteInstance deletes an instance from a remote node.
// deleteInstance deletes an instance from a remote node.
func (rm *remoteManager) deleteInstance(ctx context.Context, node *config.NodeConfig, name string) error {
// Validate instance name to prevent injection attacks
validatedName, err := validation.ValidateInstanceName(name)
if err != nil {
return err
}
// URL-encode the validated instance name to safely include it in the URL path
escapedName := url.PathEscape(validatedName)
escapedName := url.PathEscape(name)
path := fmt.Sprintf("%s%s/", apiBasePath, escapedName)
resp, err := rm.makeRemoteRequest(ctx, node, "DELETE", path, nil)
@@ -236,16 +210,10 @@ func (rm *remoteManager) deleteInstance(ctx context.Context, node *config.NodeCo
return parseRemoteResponse(resp, nil)
}
// StartInstance starts an instance on a remote node.
// startInstance starts an instance on a remote node.
func (rm *remoteManager) startInstance(ctx context.Context, node *config.NodeConfig, name string) (*instance.Instance, error) {
// Validate instance name to prevent injection attacks
validatedName, err := validation.ValidateInstanceName(name)
if err != nil {
return nil, err
}
// URL-encode the validated instance name to safely include it in the URL path
escapedName := url.PathEscape(validatedName)
escapedName := url.PathEscape(name)
path := fmt.Sprintf("%s%s/start", apiBasePath, escapedName)
resp, err := rm.makeRemoteRequest(ctx, node, "POST", path, nil)
@@ -261,16 +229,10 @@ func (rm *remoteManager) startInstance(ctx context.Context, node *config.NodeCon
return &inst, nil
}
// StopInstance stops an instance on a remote node.
// stopInstance stops an instance on a remote node.
func (rm *remoteManager) stopInstance(ctx context.Context, node *config.NodeConfig, name string) (*instance.Instance, error) {
// Validate instance name to prevent injection attacks
validatedName, err := validation.ValidateInstanceName(name)
if err != nil {
return nil, err
}
// URL-encode the validated instance name to safely include it in the URL path
escapedName := url.PathEscape(validatedName)
escapedName := url.PathEscape(name)
path := fmt.Sprintf("%s%s/stop", apiBasePath, escapedName)
resp, err := rm.makeRemoteRequest(ctx, node, "POST", path, nil)
@@ -286,16 +248,9 @@ func (rm *remoteManager) stopInstance(ctx context.Context, node *config.NodeConf
return &inst, nil
}
// RestartInstance restarts an instance on a remote node.
// restartInstance restarts an instance on a remote node.
func (rm *remoteManager) restartInstance(ctx context.Context, node *config.NodeConfig, name string) (*instance.Instance, error) {
// Validate instance name to prevent injection attacks
validatedName, err := validation.ValidateInstanceName(name)
if err != nil {
return nil, err
}
// URL-encode the validated instance name to safely include it in the URL path
escapedName := url.PathEscape(validatedName)
escapedName := url.PathEscape(name)
path := fmt.Sprintf("%s%s/restart", apiBasePath, escapedName)
resp, err := rm.makeRemoteRequest(ctx, node, "POST", path, nil)
@@ -311,16 +266,10 @@ func (rm *remoteManager) restartInstance(ctx context.Context, node *config.NodeC
return &inst, nil
}
// GetInstanceLogs retrieves logs for an instance from a remote node.
// getInstanceLogs retrieves logs for an instance from a remote node.
func (rm *remoteManager) getInstanceLogs(ctx context.Context, node *config.NodeConfig, name string, numLines int) (string, error) {
// Validate instance name to prevent injection attacks
validatedName, err := validation.ValidateInstanceName(name)
if err != nil {
return "", err
}
// URL-encode the validated instance name to safely include it in the URL path
escapedName := url.PathEscape(validatedName)
escapedName := url.PathEscape(name)
path := fmt.Sprintf("%s%s/logs?lines=%d", apiBasePath, escapedName, numLines)
resp, err := rm.makeRemoteRequest(ctx, node, "GET", path, nil)