Skip to content

Commit

Permalink
handle multiple consecutive slashes in the path string. (#2046)
Browse files Browse the repository at this point in the history
  • Loading branch information
quzard authored Jan 17, 2025
1 parent 7ae6a1d commit 3527418
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 16 deletions.
11 changes: 7 additions & 4 deletions pkg/helper/docker_center.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package helper
import (
"context"
"hash/fnv"
"path"
"path/filepath"
"regexp"
"runtime"
"strings"
Expand Down Expand Up @@ -331,7 +331,7 @@ func isPathSeparator(c byte) bool {
}

func (did *DockerInfoDetail) FindBestMatchedPath(pth string) (sourcePath, containerPath string) {
pth = path.Clean(pth)
pth = filepath.Clean(pth)
pthSize := len(pth)

// logger.Debugf(context.Background(), "FindBestMatchedPath for container %s, target path: %s, containerInfo: %+v", did.IDPrefix(), pth, did.ContainerInfo)
Expand All @@ -341,7 +341,7 @@ func (did *DockerInfoDetail) FindBestMatchedPath(pth string) (sourcePath, contai
for _, mount := range did.ContainerInfo.Mounts {
// logger.Debugf("container(%s-%s) mount: source-%s destination-%s", did.IDPrefix(), did.ContainerInfo.Name, mount.Source, mount.Destination)

dst := path.Clean(mount.Destination)
dst := filepath.Clean(mount.Destination)
dstSize := len(dst)

if strings.HasPrefix(pth, dst) &&
Expand Down Expand Up @@ -619,7 +619,10 @@ func (dc *DockerCenter) CreateInfoDetail(info types.ContainerJSON, envConfigPref
if len(ip) > 0 {
containerNameTag["_container_ip_"] = ip
}

for i := range info.Mounts {
info.Mounts[i].Source = filepath.Clean(info.Mounts[i].Source)
info.Mounts[i].Destination = filepath.Clean(info.Mounts[i].Destination)
}
did := &DockerInfoDetail{
StdoutPath: info.LogPath,
ContainerInfo: info,
Expand Down
4 changes: 2 additions & 2 deletions pkg/helper/docker_center_file_discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ func staticContainerInfoToStandard(staticInfo *staticContainerInfo, stat fs.File

for _, mount := range staticInfo.Mounts {
dockerContainer.Mounts = append(dockerContainer.Mounts, types.MountPoint{
Source: mount.Source,
Destination: mount.Destination,
Source: filepath.Clean(mount.Source),
Destination: filepath.Clean(mount.Destination),
Driver: mount.Driver,
})
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/helper/docker_cri_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/url"
"os"
"path"
"path/filepath"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -290,8 +291,8 @@ func (cw *CRIRuntimeWrapper) createContainerInfo(containerID string) (detail *Do
hostnamePath = mount.Source
}
dockerContainer.Mounts = append(dockerContainer.Mounts, types.MountPoint{
Source: mount.Source,
Destination: mount.Destination,
Source: filepath.Clean(mount.Source),
Destination: filepath.Clean(mount.Destination),
Driver: mount.Type,
})
}
Expand Down
13 changes: 7 additions & 6 deletions plugins/input/docker/logmeta/metric_container_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"encoding/json"
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"regexp"
"sort"
Expand Down Expand Up @@ -117,6 +117,7 @@ func formatPath(path string) string {
if len(path) == 0 {
return path
}
path = filepath.Clean(path)
if path[len(path)-1] == '/' {
return path[0 : len(path)-1]
}
Expand Down Expand Up @@ -211,8 +212,8 @@ func (idf *InputDockerFile) Description() string {
func (idf *InputDockerFile) addMappingToLogtail(info *helper.DockerInfoDetail, containerInfo ContainerInfoCache, allCmd *DockerFileUpdateCmdAll) {
var cmd DockerFileUpdateCmd
cmd.ID = info.ContainerInfo.ID
cmd.UpperDir = path.Clean(containerInfo.UpperDir)
cmd.LogPath = path.Clean(containerInfo.LogPath)
cmd.UpperDir = filepath.Clean(containerInfo.UpperDir)
cmd.LogPath = filepath.Clean(containerInfo.LogPath)
// tags
tags := info.GetExternalTags(idf.ExternalEnvTag, idf.ExternalK8sLabelTag)
cmd.Tags = make([]string, 0, len(tags)*2)
Expand All @@ -229,8 +230,8 @@ func (idf *InputDockerFile) addMappingToLogtail(info *helper.DockerInfoDetail, c
cmd.Mounts = make([]Mount, 0, len(containerInfo.Mounts))
for _, mount := range containerInfo.Mounts {
cmd.Mounts = append(cmd.Mounts, Mount{
Source: path.Clean(mount.Source),
Destination: path.Clean(mount.Destination),
Source: filepath.Clean(mount.Source),
Destination: filepath.Clean(mount.Destination),
})
}
cmdBuf, _ := json.Marshal(&cmd)
Expand Down Expand Up @@ -280,7 +281,7 @@ func (idf *InputDockerFile) updateAll(allCmd *DockerFileUpdateCmdAll) {
}

func (idf *InputDockerFile) updateMapping(info *helper.DockerInfoDetail, allCmd *DockerFileUpdateCmdAll) {
logPath := path.Clean(info.StdoutPath)
logPath := filepath.Clean(info.StdoutPath)
id := info.ContainerInfo.ID
mounts := info.ContainerInfo.Mounts
upperDir := info.DefaultRootPath
Expand Down
53 changes: 53 additions & 0 deletions plugins/input/docker/logmeta/metric_container_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,56 @@ func TestServiceDockerStdout_Init(t *testing.T) {

assert.NoError(t, err)
}

func TestFormatPath(t *testing.T) {
tests := []struct {
name string
input string
want string
}{
{
name: "empty path",
input: "",
want: "",
},
{
name: "normal path without trailing slash",
input: "/path/to/somewhere",
want: "/path/to/somewhere",
},
{
name: "path with trailing forward slash",
input: "/path/to/somewhere/",
want: "/path/to/somewhere",
},
{
name: "path with trailing backslash",
input: "/path/to/somewhere\\",
want: "/path/to/somewhere",
},
{
name: "path with dots",
input: "/path/./to/../somewhere",
want: "/path/somewhere",
},
{
name: "path with multiple slashes",
input: "/path//to///somewhere",
want: "/path/to/somewhere",
},
{
name: "path with multiple slashes",
input: "/////path//////to///somewhere",
want: "/path/to/somewhere",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := formatPath(tt.input)
if got != tt.want {
t.Errorf("formatPath() = %v, want %v", got, tt.want)
}
})
}
}
5 changes: 3 additions & 2 deletions plugins/input/journal/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"errors"
"fmt"
"path"
"path/filepath"
"strconv"
"strings"

Expand Down Expand Up @@ -172,12 +173,12 @@ func unitNameMangle(name, suffix string) (string, error) {

if isDevicePath(name) {
// chop off path and put .device on the end
return path.Base(path.Clean(name)) + "device", nil
return path.Base(filepath.Clean(name)) + "device", nil
}

if pathIsAbsolute(name) {
// chop path and put .mount on the end
return path.Base(path.Clean(name)) + ".mount", nil
return path.Base(filepath.Clean(name)) + ".mount", nil
}

name = doEscapeMangle(name)
Expand Down

0 comments on commit 3527418

Please sign in to comment.