|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2025-present Datadog, Inc. |
| 5 | + |
| 6 | +//go:build windows |
| 7 | + |
| 8 | +package workloadselectionimpl |
| 9 | + |
| 10 | +import ( |
| 11 | + "bytes" |
| 12 | + "fmt" |
| 13 | + "os" |
| 14 | + "os/exec" |
| 15 | + "path/filepath" |
| 16 | + |
| 17 | + "golang.org/x/sys/windows" |
| 18 | +) |
| 19 | + |
| 20 | +// getCompilePolicyBinaryPath returns the full path to the compile policy binary on Windows |
| 21 | +func getCompilePolicyBinaryPath() string { |
| 22 | + return filepath.Join(getInstallPath(), "bin", "dd-compile-policy.exe") |
| 23 | +} |
| 24 | + |
| 25 | +// isCompilePolicyBinaryAvailable checks if the compile policy binary is available |
| 26 | +// on Windows systems |
| 27 | +func (c *workloadselectionComponent) isCompilePolicyBinaryAvailable() bool { |
| 28 | + compilePath := getCompilePolicyBinaryPath() |
| 29 | + info, err := os.Stat(compilePath) |
| 30 | + if err != nil { |
| 31 | + if !os.IsNotExist(err) { |
| 32 | + c.log.Warnf("failed to stat APM workload selection compile policy binary: %v", err) |
| 33 | + } |
| 34 | + return false |
| 35 | + } |
| 36 | + // On Windows, check that it's a regular file (not a directory) |
| 37 | + return info.Mode().IsRegular() |
| 38 | +} |
| 39 | + |
| 40 | +// setFileReadableByEveryone sets the DACL on a file to allow: |
| 41 | +// - Current owner (ddagentuser): Full Control |
| 42 | +// - SYSTEM: Full Control |
| 43 | +// - Administrators: Full Control |
| 44 | +// - Everyone: Read and Execute |
| 45 | +// The owner is NOT changed - it remains as ddagentuser |
| 46 | +func setFileReadableByEveryone(path string) error { |
| 47 | + // Create an SDDL with only the DACL part (no Owner/Group) |
| 48 | + // D:AI - DACL, Auto-Inherit. |
| 49 | + // "Inherit permissions from the parent folder (System/Admins usually)." |
| 50 | + // (A;;GRGX;;;WD) - Allow Generic Read + Generic Execute to Everyone (World Domain). |
| 51 | + sddl := "D:AI(A;;GRGX;;;WD)" |
| 52 | + |
| 53 | + sd, err := windows.SecurityDescriptorFromString(sddl) |
| 54 | + if err != nil { |
| 55 | + return fmt.Errorf("failed to create security descriptor: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + dacl, _, err := sd.DACL() |
| 59 | + if err != nil { |
| 60 | + return fmt.Errorf("failed to get DACL: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + // Only set the DACL, don't touch owner or group |
| 64 | + return windows.SetNamedSecurityInfo( |
| 65 | + path, |
| 66 | + windows.SE_FILE_OBJECT, |
| 67 | + windows.DACL_SECURITY_INFORMATION|windows.PROTECTED_DACL_SECURITY_INFORMATION, |
| 68 | + nil, // owner - leave unchanged |
| 69 | + nil, // group - leave unchanged |
| 70 | + dacl, // DACL - set this |
| 71 | + nil, // SACL - leave unchanged |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +// compileAndWriteConfig compiles the policy binary into a binary file readable by the injector |
| 76 | +// On Windows, sets ACLs to allow Everyone read+execute access while keeping ddagentuser as owner |
| 77 | +func (c *workloadselectionComponent) compileAndWriteConfig(rawConfig []byte) error { |
| 78 | + dir := filepath.Dir(configPath) |
| 79 | + if err := os.MkdirAll(dir, 0755); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + cmd := exec.Command(getCompilePolicyBinaryPath(), "--input-string", string(rawConfig), "--output-file", configPath) |
| 83 | + var stdoutBuf, stderrBuf bytes.Buffer |
| 84 | + cmd.Stdout = &stdoutBuf |
| 85 | + cmd.Stderr = &stderrBuf |
| 86 | + if err := cmd.Run(); err != nil { |
| 87 | + return fmt.Errorf("error executing dd-policy-compile (%w); out: '%s'; err: '%s'", err, stdoutBuf.String(), stderrBuf.String()) |
| 88 | + } |
| 89 | + // Set permissions on the file to allow Everyone read+execute access |
| 90 | + // We keep the current owner (ddagentuser) and only modify the DACL |
| 91 | + if err := setFileReadableByEveryone(configPath); err != nil { |
| 92 | + return fmt.Errorf("failed to set permissions on file %s: %w", configPath, err) |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
0 commit comments