Skip to content

Commit 13ee3bb

Browse files
committed
Fix file access for upgrade.
1 parent 519f48c commit 13ee3bb

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

testing/upgradetest/upgrader.go

+34-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"encoding/json"
1010
"errors"
1111
"fmt"
12+
"github.com/hectane/go-acl"
1213
"os"
1314
"path/filepath"
15+
"runtime"
1416
"strings"
1517
"time"
1618

@@ -568,7 +570,19 @@ func getSourceURI(ctx context.Context, f *atesting.Fixture, unprivileged bool) (
568570
}
569571
if unprivileged {
570572
// move the file to temp directory
571-
dir, err := os.MkdirTemp("", "agent-upgrade-*")
573+
baseTmp := ""
574+
if runtime.GOOS == "windows" {
575+
// `elastic-agent-user` needs to have access to the file, default
576+
// will place this in C:\Users\windows\AppData\Local\Temp\ which
577+
// `elastic-agent-user` doesn't have access.
578+
579+
// create C:\Temp with world read/write to use for temp directory
580+
baseTmp, err = windowsBaseTemp()
581+
if err != nil {
582+
return "", fmt.Errorf("failed to create windows base temp path: %w", err)
583+
}
584+
}
585+
dir, err := os.MkdirTemp(baseTmp, "agent-upgrade-*")
572586
if err != nil {
573587
return "", fmt.Errorf("failed to create temp directory: %w", err)
574588
}
@@ -590,3 +604,22 @@ func getSourceURI(ctx context.Context, f *atesting.Fixture, unprivileged bool) (
590604
}
591605
return "file://" + filepath.Dir(srcPkg), nil
592606
}
607+
608+
func windowsBaseTemp() (string, error) {
609+
baseTmp := "C:\\Temp"
610+
_, err := os.Stat(baseTmp)
611+
if err != nil {
612+
if !errors.Is(err, os.ErrNotExist) {
613+
return "", fmt.Errorf("failed to stat %s: %w", baseTmp, err)
614+
}
615+
err = os.Mkdir(baseTmp, 0777)
616+
if err != nil {
617+
return "", fmt.Errorf("failed to mkdir %s: %w", baseTmp, err)
618+
}
619+
}
620+
err = acl.Chmod(baseTmp, 0777)
621+
if err != nil {
622+
return "", fmt.Errorf("failed to chmod %s: %w", baseTmp, err)
623+
}
624+
return baseTmp, nil
625+
}

0 commit comments

Comments
 (0)