Skip to content

Commit 6d09aa9

Browse files
authored
Append a random suffix to written out plugins so multiple don't clash (#43)
1 parent 64b82fc commit 6d09aa9

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

pluginutil/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.17
44

55
require (
66
github.com/hashicorp/go-plugin v1.4.3
7+
github.com/hashicorp/go-secure-stdlib/base62 v0.1.2
78
github.com/stretchr/testify v1.7.0
89
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
910
)
@@ -14,6 +15,7 @@ require (
1415
github.com/golang/protobuf v1.5.2 // indirect
1516
github.com/google/go-cmp v0.5.7 // indirect
1617
github.com/hashicorp/go-hclog v1.1.0 // indirect
18+
github.com/hashicorp/go-uuid v1.0.2 // indirect
1719
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect
1820
github.com/mattn/go-colorable v0.1.6 // indirect
1921
github.com/mattn/go-isatty v0.0.12 // indirect

pluginutil/go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ github.com/hashicorp/go-hclog v1.1.0 h1:QsGcniKx5/LuX2eYoeL+Np3UKYPNaN7YKpTh29h8
5656
github.com/hashicorp/go-hclog v1.1.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
5757
github.com/hashicorp/go-plugin v1.4.3 h1:DXmvivbWD5qdiBts9TpBC7BYL1Aia5sxbRgQB+v6UZM=
5858
github.com/hashicorp/go-plugin v1.4.3/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ=
59+
github.com/hashicorp/go-secure-stdlib/base62 v0.1.2 h1:ET4pqyjiGmY09R5y+rSd70J2w45CtbWDNvGqWp/R3Ng=
60+
github.com/hashicorp/go-secure-stdlib/base62 v0.1.2/go.mod h1:EdWO6czbmthiwZ3/PUsDV+UD1D5IRU4ActiaWGwt0Yw=
61+
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
62+
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
5963
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
6064
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
6165
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=

pluginutil/pluginutil.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strings"
1616

1717
gp "github.com/hashicorp/go-plugin"
18+
"github.com/hashicorp/go-secure-stdlib/base62"
1819
"golang.org/x/crypto/sha3"
1920
)
2021

@@ -236,6 +237,7 @@ func CreatePlugin(plugin *PluginInfo, opt ...Option) (interface{}, func() error,
236237
return nil, nil, fmt.Errorf("error reading gzip compressed data from reader: %w", err)
237238
}
238239
buf = uncompBuf.Bytes()
240+
name = strings.TrimSuffix(name, ".gz")
239241
}
240242

241243
cleanup := func() error {
@@ -255,8 +257,13 @@ func CreatePlugin(plugin *PluginInfo, opt ...Option) (interface{}, func() error,
255257
dir = tmpDir
256258
}
257259
pluginPath := filepath.Join(dir, name)
260+
randSuffix, err := base62.Random(5)
261+
if err != nil {
262+
return nil, nil, fmt.Errorf("error generating random suffix for plugin execution: %w", err)
263+
}
264+
pluginPath = fmt.Sprintf("%s-%s", pluginPath, randSuffix)
258265
if runtime.GOOS == "windows" {
259-
pluginPath += ".exe"
266+
pluginPath = fmt.Sprintf("%s.exe", pluginPath)
260267
}
261268
if err := ioutil.WriteFile(pluginPath, buf, fs.FileMode(0o700)); err != nil {
262269
return nil, cleanup, fmt.Errorf("error writing out plugin for execution: %w", err)

0 commit comments

Comments
 (0)