Rename added functions for docker client

This commit is contained in:
Jonny Booker
2025-06-10 12:57:50 +01:00
parent a52260b49d
commit e335bb8a1f
2 changed files with 24 additions and 24 deletions

View File

@@ -69,8 +69,8 @@ func CheckSocket(socketPath string) bool {
return true return true
} }
// IsWithinNewtNetwork checks if a provided target is within the newt network // IsWithinHostNetwork checks if a provided target is within the host container network
func IsWithinNewtNetwork(socketPath string, containerNameAsHostname bool, targetAddress string, targetPort int) (bool, error) { func IsWithinHostNetwork(socketPath string, containerNameAsHostname bool, targetAddress string, targetPort int) (bool, error) {
// Always enforce network validation // Always enforce network validation
containers, err := ListContainers(socketPath, true, containerNameAsHostname) containers, err := ListContainers(socketPath, true, containerNameAsHostname)
if err != nil { if err != nil {
@@ -103,7 +103,7 @@ func IsWithinNewtNetwork(socketPath string, containerNameAsHostname bool, target
} }
combinedTargetAddress := targetAddress + ":" + strconv.Itoa(targetPort) combinedTargetAddress := targetAddress + ":" + strconv.Itoa(targetPort)
return false, fmt.Errorf("target address not within newt network: %s", combinedTargetAddress) return false, fmt.Errorf("target address not within host container network: %s", combinedTargetAddress)
} }
// ListContainers lists all Docker containers with their network information // ListContainers lists all Docker containers with their network information
@@ -127,10 +127,10 @@ func ListContainers(socketPath string, enforceNetworkValidation bool, containerN
} }
defer cli.Close() defer cli.Close()
// Get the newt container // Get the host container
newtContainer, err := getNewtContainer(ctx, cli) hostContainer, err := getHostContainer(ctx, cli)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to list containers: %v", err) return nil, fmt.Errorf("failed to get host container: %v", err)
} }
// List containers // List containers
@@ -172,16 +172,16 @@ func ListContainers(socketPath string, enforceNetworkValidation bool, containerN
logger.Debug("Failed to inspect container %s for network info: %v", c.ID[:12], err) logger.Debug("Failed to inspect container %s for network info: %v", c.ID[:12], err)
// Continue without network info if inspection fails // Continue without network info if inspection fails
} else { } else {
// Only containers within the newt network will be returned // Only containers within the host container network will be returned
isInNewtNetwork := false isInHostContainerNetwork := false
// Extract network information from inspection // Extract network information from inspection
if containerInfo.NetworkSettings != nil && containerInfo.NetworkSettings.Networks != nil { if containerInfo.NetworkSettings != nil && containerInfo.NetworkSettings.Networks != nil {
for networkName, endpoint := range containerInfo.NetworkSettings.Networks { for networkName, endpoint := range containerInfo.NetworkSettings.Networks {
// Determine if the current container is in the newt network // Determine if the current container is in the host container network
for _, newtNetwork := range newtContainer.NetworkSettings.Networks { for _, hostContainerNetwork := range hostContainer.NetworkSettings.Networks {
if !isInNewtNetwork { if !isInHostContainerNetwork {
isInNewtNetwork = endpoint.NetworkID == newtNetwork.NetworkID isInHostContainerNetwork = endpoint.NetworkID == hostContainerNetwork.NetworkID
} }
} }
@@ -207,9 +207,9 @@ func ListContainers(socketPath string, enforceNetworkValidation bool, containerN
} }
} }
// Don't continue returning this container if not in the newt network(s) // Don't continue returning this container if not in the host container network(s)
if enforceNetworkValidation && !isInNewtNetwork { if enforceNetworkValidation && !isInHostContainerNetwork {
logger.Debug("container not found within the newt network, skipping: %s", name) logger.Debug("container not found within the host container network, skipping: %s", name)
continue continue
} }
} }
@@ -231,18 +231,18 @@ func ListContainers(socketPath string, enforceNetworkValidation bool, containerN
return dockerContainers, nil return dockerContainers, nil
} }
func getNewtContainer(dockerContext context.Context, dockerClient *client.Client) (*container.InspectResponse, error) { func getHostContainer(dockerContext context.Context, dockerClient *client.Client) (*container.InspectResponse, error) {
// Get newt hostname from the os // Get hostname from the os
newtContainerName, err := os.Hostname() containerHostname, err := os.Hostname()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to find newt hostname: %v", err) return nil, fmt.Errorf("failed to find hostname: %v", err)
} }
// Get newt container from the docker socket // Get host container from the docker socket
newtContainer, err := dockerClient.ContainerInspect(dockerContext, newtContainerName) hostContainer, err := dockerClient.ContainerInspect(dockerContext, containerHostname)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to find newt container: %v", err) return nil, fmt.Errorf("failed to inspect host container: %v", err)
} }
return &newtContainer, nil return &hostContainer, nil
} }

View File

@@ -833,7 +833,7 @@ func updateTargets(pm *proxy.ProxyManager, action string, tunnelIP string, proto
if dockerEnforceNetworkValidationBool { if dockerEnforceNetworkValidationBool {
logger.Info("Enforcing docker network validation") logger.Info("Enforcing docker network validation")
isWithinNewtNetwork, err := docker.IsWithinNewtNetwork(dockerSocket, dockerContainerAsHostnameBool, targetAddress, targetPort) isWithinNewtNetwork, err := docker.IsWithinHostNetwork(dockerSocket, dockerContainerAsHostnameBool, targetAddress, targetPort)
if !isWithinNewtNetwork { if !isWithinNewtNetwork {
logger.Error("Not adding target: %v", err) logger.Error("Not adding target: %v", err)
} else { } else {