Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update windows data dir installation #1510

Merged
merged 6 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/packagekit/assets/main.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
Level="1"
Display="hidden">
<ComponentGroupRef Id="AppFiles" />
<ComponentGroupRef Id="AppData" />
</Feature>

<Feature
Expand Down
2 changes: 1 addition & 1 deletion pkg/packagekit/package_wix.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func PackageWixMSI(ctx context.Context, w io.Writer, po *PackageOptions, include
wixArgs = append(wixArgs, wix.WithService(launcherService))
}

wixTool, err := wix.New(po.Root, mainWxsContent.Bytes(), wixArgs...)
wixTool, err := wix.New(po.Root, po.Identifier, mainWxsContent.Bytes(), wixArgs...)
if err != nil {
return fmt.Errorf("making wixTool: %w", err)
}
Expand Down
92 changes: 78 additions & 14 deletions pkg/packagekit/wix/wix.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,24 @@ import (
"strings"

"github.com/go-kit/kit/log/level"
"github.com/kolide/kit/fsutil"
"github.com/kolide/launcher/pkg/contexts/ctxlog"
)

type wixTool struct {
wixPath string // Where is wix installed
packageRoot string // What's the root of the packaging files?
buildDir string // The wix tools want to work in a build dir.
msArch string // What's the Microsoft architecture name?
services []*Service // array of services.
dockerImage string // If in docker, what image?
skipValidation bool // Skip light validation. Seems to be needed for running in 32bit wine environments.
skipCleanup bool // Skip cleaning temp dirs. Useful when debugging wix generation
cleanDirs []string // directories to rm on cleanup
ui bool // whether or not to include a ui
extraFiles []extraFile
wixPath string // Where is wix installed
packageRoot string // What's the root of the packaging files?
packageDataRoot string // What's the root for the data directory packaging files?
buildDir string // The wix tools want to work in a build dir.
msArch string // What's the Microsoft architecture name?
services []*Service // array of services.
dockerImage string // If in docker, what image?
skipValidation bool // Skip light validation. Seems to be needed for running in 32bit wine environments.
skipCleanup bool // Skip cleaning temp dirs. Useful when debugging wix generation
cleanDirs []string // directories to rm on cleanup
ui bool // whether or not to include a ui
extraFiles []extraFile
identifier string // the package identifier used for directory path creation (e.g. kolide-k2)

execCC func(context.Context, string, ...string) *exec.Cmd // Allows test overrides
}
Expand Down Expand Up @@ -102,12 +105,12 @@ func SkipCleanup() WixOpt {
// New takes a packageRoot of files, and a wxsContent of xml wix
// configuration, and will return a struct with methods for building
// packages with.
func New(packageRoot string, mainWxsContent []byte, wixOpts ...WixOpt) (*wixTool, error) {
func New(packageRoot string, identifier string, mainWxsContent []byte, wixOpts ...WixOpt) (*wixTool, error) {
wo := &wixTool{
wixPath: FindWixInstall(),
packageRoot: packageRoot,

execCC: exec.CommandContext, //nolint:forbidigo // Fine to use exec.CommandContext outside of launcher proper
identifier: identifier,
execCC: exec.CommandContext, //nolint:forbidigo // Fine to use exec.CommandContext outside of launcher proper
}

for _, opt := range wixOpts {
Expand Down Expand Up @@ -169,6 +172,10 @@ func (wo *wixTool) Cleanup() {
// Package will run through the wix steps to produce a resulting
// package. The path for the resultant package will be returned.
func (wo *wixTool) Package(ctx context.Context) (string, error) {
if err := wo.setupDataDir(ctx); err != nil {
return "", fmt.Errorf("adding data file stubs: %w", err)
}

if err := wo.heat(ctx); err != nil {
return "", fmt.Errorf("running heat: %w", err)
}
Expand Down Expand Up @@ -236,6 +243,57 @@ func (wo *wixTool) addServices(ctx context.Context) error {
return nil
}

// setupDataDir handles the windows data directory setup by pre-creating any files
// that we want to ensure are cleaned up on uninstall.
// this is handled before the other heat/candle/light calls because we must issue
// a separate heat call to harvest the data directory in ProgramData instead of Program Files
func (wo *wixTool) setupDataDir(ctx context.Context) error {
var err error
wo.packageDataRoot, err = os.MkdirTemp("", "package.packageDataRoot")
if err != nil {
return fmt.Errorf("unable to create temporary packaging data directory: %w", err)
}

wo.cleanDirs = append(wo.cleanDirs, wo.packageDataRoot)

fullIdentifier := fmt.Sprintf("Launcher-%s", wo.identifier)
dataFilesPath := filepath.Join(wo.packageDataRoot, fullIdentifier, "data")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a good idea to update the default value here https://github.com/kolide/launcher/blob/main/pkg/launcher/paths.go#L42 also?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do!


if err := os.MkdirAll(dataFilesPath, fsutil.DirMode); err != nil {
return fmt.Errorf("create base data dir error for wix harvest: %w", err)
}

// touch these known file names before harvest to ensure they're cleaned up on uninstall
dataFilenames := []string{"launcher.db", "metadata.json", "kv.sqlite"}

for _, fname := range dataFilenames {
newPath := filepath.Join(dataFilesPath, fname)
newFile, err := os.Create(newPath)
if err != nil {
return err
}

newFile.Close()
}

_, err = wo.execOut(ctx,
Fixed Show fixed Hide fixed
filepath.Join(wo.wixPath, "heat.exe"),
"dir", wo.packageDataRoot,
"-nologo",
"-gg", "-g1",
"-srd",
"-sfrag",
"-ke",
"-cg", "AppData",
"-template", "fragment",
"-dr", "DATADIR",
"-var", "var.SourceDataDir",
"-out", "AppData.wxs",
)

return err
}

// heat invokes wix's heat command. This examines a directory and
// "harvests" the files into an xml structure. See
// http://wixtoolset.org/documentation/manual/v3/overview/heat.html
Expand All @@ -259,6 +317,7 @@ func (wo *wixTool) heat(ctx context.Context) error {
"-var", "var.SourceDir",
"-out", "AppFiles.wxs",
)

return err
}

Expand All @@ -271,9 +330,11 @@ func (wo *wixTool) candle(ctx context.Context) error {
"-nologo",
"-arch", wo.msArch,
"-dSourceDir="+wo.packageRoot,
"-dSourceDataDir="+wo.packageDataRoot,
"-ext", "WixUtilExtension",
"Installer.wxs",
"AppFiles.wxs",
"AppData.wxs",
)
return err
}
Expand All @@ -286,8 +347,10 @@ func (wo *wixTool) light(ctx context.Context) error {
"-nologo",
"-dcl:high", // compression level
"-dSourceDir=" + wo.packageRoot,
"-dSourceDataDir=" + wo.packageDataRoot,
"-ext", "WixUtilExtension",
"AppFiles.wixobj",
"AppData.wixobj",
"Installer.wixobj",
"-out", "out.msi",
}
Expand Down Expand Up @@ -315,6 +378,7 @@ func (wo *wixTool) execOut(ctx context.Context, argv0 string, args ...string) (s
"run",
"--entrypoint", "",
"-v", fmt.Sprintf("%s:%s", wo.packageRoot, wo.packageRoot),
"-v", fmt.Sprintf("%s:%s", wo.packageDataRoot, wo.packageDataRoot),
"-v", fmt.Sprintf("%s:%s", wo.buildDir, wo.buildDir),
"-w", wo.buildDir,
wo.dockerImage,
Expand Down
1 change: 1 addition & 0 deletions pkg/packagekit/wix/wix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestWixPackage(t *testing.T) {
require.NoError(t, err)

wixTool, err := New(packageRoot,
"test-identifier",
mainWxsContent,
As32bit(), // wine is 32bit
SkipValidation(), // wine can't validate
Expand Down
21 changes: 20 additions & 1 deletion pkg/packaging/packaging.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (p *PackageOptions) Build(ctx context.Context, packageWriter io.Writer, tar

launcherMapFlags := map[string]string{
"hostname": p.Hostname,
"root_directory": p.canonicalizePath(p.rootDir),
"root_directory": p.canonicalizeRootDir(p.rootDir),
"osqueryd_path": p.canonicalizePath(filepath.Join(p.binDir, "osqueryd")),
"enroll_secret_path": p.canonicalizePath(filepath.Join(p.confDir, "secret")),
}
Expand Down Expand Up @@ -736,12 +736,19 @@ func (p *PackageOptions) setupDirectories() error {
// to take that into account for the guid generation.
p.binDir = filepath.Join("Launcher-"+p.Identifier, "bin")
p.confDir = filepath.Join("Launcher-"+p.Identifier, "conf")
// we handle the data directory setup differently for windows before final package generation,
// see wix/wix.go's setupDataDir method for additional context
p.rootDir = filepath.Join("Launcher-"+p.Identifier, "data")
default:
return fmt.Errorf("unknown platform %s", string(p.target.Platform))
}

for _, d := range []string{p.binDir, p.confDir, p.rootDir} {
// don't create the root (data) directory for windows before heat can run
if p.target.Platform == Windows && d == p.rootDir {
continue
}

if err := os.MkdirAll(filepath.Join(p.packageRoot, d), fsutil.DirMode); err != nil {
return fmt.Errorf("create dir (%s) for %s: %w", d, p.target.String(), err)
}
Expand Down Expand Up @@ -785,3 +792,15 @@ func (p *PackageOptions) canonicalizePath(path string) string {

return filepath.Join(`C:\Program Files\Kolide`, path)
}

// canonicalizeRootDir functions similarly to canonicalizePath above,
// only impacting MSI targets. This is broken out from canonicalizePath
// because for windows the full path to the root directory should be expanded
// into ProgramData
func (p *PackageOptions) canonicalizeRootDir(path string) string {
if p.target.Package != Msi {
return path
}

return filepath.Join(`C:\ProgramData\Kolide`, path)
}
Loading