util

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2025 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package util provides utility functions for Docker and other shared functionality.

Index

Constants

This section is empty.

Variables

View Source
var (
	// IsDockerSocketAccessibleFunc is a variable to allow mocking in tests.
	// It checks if the Docker socket is accessible.
	IsDockerSocketAccessibleFunc = isDockerSocketAccessibleDefault
)

Functions

func CloseDockerClient

func CloseDockerClient()

CloseDockerClient closes the shared Docker client. It should be called on application shutdown.

func GenerateUUID

func GenerateUUID() string

GenerateUUID creates a new version 4 UUID and returns it as a string.

func GetDockerCommand

func GetDockerCommand() (string, []string)

GetDockerCommand returns the command and base arguments for running Docker, respecting the USE_SUDO_FOR_DOCKER environment variable.

func IsDockerSocketAccessible

func IsDockerSocketAccessible() bool

IsDockerSocketAccessible checks if the Docker daemon is accessible through the socket.

func NewHTTPClientWithTLS

func NewHTTPClientWithTLS(tlsConfig *configv1.TLSConfig) (*http.Client, error)

NewHTTPClientWithTLS creates a new *http.Client configured with the specified TLS settings. It supports setting a custom CA certificate, a client certificate and key, the server name for SNI, and skipping verification. If the provided tlsConfig is nil, it returns http.DefaultClient.

tlsConfig contains the TLS settings to apply to the HTTP client's transport. It returns a configured *http.Client or an error if the TLS configuration is invalid or files cannot be read.

func ParseToolName

func ParseToolName(toolName string) (service, bareToolName string, err error)

ParseToolName deconstructs a fully qualified tool name into its service key and bare tool name components. It splits the name using the standard separator.

toolName is the fully qualified tool name. It returns the service key, the bare tool name, and an error if parsing fails (though the current implementation does not return an error).

func ReplaceURLPath

func ReplaceURLPath(urlPath string, params map[string]interface{}) string

ReplaceURLPath replaces placeholders in a URL path with values from a params map.

func ResolveSecret

func ResolveSecret(secret *configv1.SecretValue) (string, error)

ResolveSecret resolves a SecretValue into a string.

func ResolveSecretMap added in v0.0.2

func ResolveSecretMap(secretMap map[string]*configv1.SecretValue, plainMap map[string]string) (map[string]string, error)

ResolveSecretMap resolves a map of secrets and merges them with a plain map. Secrets in the secretMap take precedence over values in the plainMap.

func SafeDialContext

func SafeDialContext(ctx context.Context, network, addr string) (net.Conn, error)

SafeDialContext creates a connection to the given address, but strictly prevents connections to private or loopback IP addresses to mitigate SSRF vulnerabilities.

It resolves the host's IP addresses and checks each one. If any resolved IP is private or loopback, the connection is blocked.

ctx is the context for the dial operation. network is the network type (e.g., "tcp"). addr is the address to connect to (host:port). It returns the established connection or an error if the connection fails or is blocked.

func SanitizeID

func SanitizeID(ids []string, alwaysAppendHash bool, maxSanitizedPrefixLength, hashLength int) (string, error)

SanitizeID sanitizes a slice of strings to create a single valid identifier.

It processes each string in the input slice `ids` to make it conform to a set of rules, ensuring that the resulting identifier is safe and consistent for use in various contexts. The sanitization process involves:

  1. Removing any characters that are not alphanumeric, underscore, or hyphen.
  2. Truncating the string if it exceeds a specified maximum length.
  3. Optionally, appending a hash of the original string to ensure uniqueness, especially when truncation occurs or when illegal characters are present.

After sanitizing each string individually, it joins them with a "." separator to form the final identifier.

Parameters:

ids: A slice of strings to be sanitized and joined. Each element of the slice
     represents a part of the final identifier.
alwaysAppendHash: A boolean that, if true, forces a hash to be appended to each
                  sanitized string, regardless of whether it was modified.
maxSanitizedPrefixLength: The maximum allowed length for the sanitized prefix of each
                          string before a hash is appended.
hashLength: The desired length of the hexadecimal hash to be appended.

Returns:

A single string representing the sanitized and joined identifier.

func SanitizeOperationID

func SanitizeOperationID(input string) string

SanitizeOperationID cleans an input string to make it suitable for use as an operation ID. It replaces any sequence of disallowed characters with a short hexadecimal hash of that sequence, ensuring uniqueness while preserving as much of the original string as possible.

input is the string to be sanitized. It returns the sanitized string.

func SanitizeServiceName

func SanitizeServiceName(name string) (string, error)

SanitizeServiceName sanitizes the given service name. It ensures that the name is a valid identifier by removing disallowed characters and appending a hash if the name is too long or contains illegal characters. This function calls SanitizeID with alwaysAppendHash set to false.

func SanitizeToolName

func SanitizeToolName(name string) (string, error)

SanitizeToolName sanitizes the given tool name. It ensures that the name is a valid identifier by removing disallowed characters and appending a hash if the name is too long or contains illegal characters. This function calls SanitizeID with alwaysAppendHash set to false.

Types

This section is empty.

Directories

Path Synopsis
Package schemaconv provides utilities for converting between schema formats.
Package schemaconv provides utilities for converting between schema formats.
Package testutil provides testing utilities.
Package testutil provides testing utilities.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL