2021-03-03 01:19:24 +00:00
|
|
|
package packaging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-11-22 14:13:26 +00:00
|
|
|
"fmt"
|
2021-03-03 01:19:24 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
|
2022-05-03 19:46:02 +00:00
|
|
|
"github.com/Masterminds/semver"
|
2021-08-11 14:02:22 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/orbit/pkg/constant"
|
|
|
|
"github.com/fleetdm/fleet/v4/orbit/pkg/update"
|
2021-08-24 12:50:03 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/pkg/secure"
|
2021-03-03 01:19:24 +00:00
|
|
|
"github.com/goreleaser/nfpm/v2"
|
|
|
|
"github.com/goreleaser/nfpm/v2/files"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
)
|
|
|
|
|
2021-11-15 13:40:58 +00:00
|
|
|
func buildNFPM(opt Options, pkger nfpm.Packager) (string, error) {
|
2021-03-03 01:19:24 +00:00
|
|
|
// Initialize directories
|
2021-10-27 23:17:41 +00:00
|
|
|
tmpDir, err := initializeTempDir()
|
2021-03-03 01:19:24 +00:00
|
|
|
if err != nil {
|
2021-11-15 13:40:58 +00:00
|
|
|
return "", err
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpDir)
|
|
|
|
|
2022-05-02 18:18:59 +00:00
|
|
|
rootDir := filepath.Join(tmpDir, "root")
|
|
|
|
if err := secure.MkdirAll(rootDir, constant.DefaultDirMode); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("create root dir: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
2022-05-02 18:18:59 +00:00
|
|
|
orbitRoot := filepath.Join(rootDir, "opt", "orbit")
|
2021-08-11 14:02:22 +00:00
|
|
|
if err := secure.MkdirAll(orbitRoot, constant.DefaultDirMode); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("create orbit dir: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize autoupdate metadata
|
|
|
|
updateOpt := update.DefaultOptions
|
2022-03-15 19:04:12 +00:00
|
|
|
|
2021-03-03 01:19:24 +00:00
|
|
|
updateOpt.RootDirectory = orbitRoot
|
2022-03-15 19:04:12 +00:00
|
|
|
updateOpt.Targets = update.LinuxTargets
|
|
|
|
|
2022-05-04 14:14:12 +00:00
|
|
|
if opt.Desktop {
|
|
|
|
updateOpt.Targets["desktop"] = update.DesktopLinuxTarget
|
|
|
|
// Override default channel with the provided value.
|
|
|
|
updateOpt.Targets.SetTargetChannel("desktop", opt.DesktopChannel)
|
|
|
|
}
|
|
|
|
|
2022-03-15 19:04:12 +00:00
|
|
|
// Override default channels with the provided values.
|
2022-03-21 17:53:53 +00:00
|
|
|
updateOpt.Targets.SetTargetChannel("orbit", opt.OrbitChannel)
|
|
|
|
updateOpt.Targets.SetTargetChannel("osqueryd", opt.OsquerydChannel)
|
2022-03-15 19:04:12 +00:00
|
|
|
|
2021-03-10 22:42:02 +00:00
|
|
|
updateOpt.ServerURL = opt.UpdateURL
|
2021-03-23 00:38:32 +00:00
|
|
|
if opt.UpdateRoots != "" {
|
|
|
|
updateOpt.RootKeys = opt.UpdateRoots
|
|
|
|
}
|
2021-03-03 01:19:24 +00:00
|
|
|
|
2022-01-31 13:41:11 +00:00
|
|
|
updatesData, err := InitializeUpdates(updateOpt)
|
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("initialize updates: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
2022-01-31 13:41:11 +00:00
|
|
|
log.Debug().Stringer("data", updatesData).Msg("updates initialized")
|
|
|
|
if opt.Version == "" {
|
|
|
|
// We set the package version to orbit's latest version.
|
|
|
|
opt.Version = updatesData.OrbitVersion
|
|
|
|
}
|
2021-03-03 01:19:24 +00:00
|
|
|
|
2022-05-03 19:46:02 +00:00
|
|
|
varLibSymlink := false
|
|
|
|
if orbitSemVer, err := semver.NewVersion(updatesData.OrbitVersion); err == nil {
|
|
|
|
if orbitSemVer.LessThan(semver.MustParse("0.0.11")) {
|
|
|
|
varLibSymlink = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If err != nil we assume non-legacy Orbit.
|
|
|
|
|
2021-03-03 01:19:24 +00:00
|
|
|
// Write files
|
|
|
|
|
2022-05-02 18:18:59 +00:00
|
|
|
if err := writeSystemdUnit(opt, rootDir); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("write systemd unit: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 18:18:59 +00:00
|
|
|
if err := writeEnvFile(opt, rootDir); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("write env file: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 23:06:33 +00:00
|
|
|
if err := writeOsqueryFlagfile(opt, orbitRoot); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("write flagfile: %w", err)
|
2021-11-18 23:06:33 +00:00
|
|
|
}
|
|
|
|
|
2021-11-19 01:17:05 +00:00
|
|
|
if err := writeOsqueryCertPEM(opt, orbitRoot); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("write certs.pem: %w", err)
|
2021-11-19 01:17:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-03 01:19:24 +00:00
|
|
|
postInstallPath := filepath.Join(tmpDir, "postinstall.sh")
|
|
|
|
if err := writePostInstall(opt, postInstallPath); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("write postinstall script: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
2022-04-19 12:32:47 +00:00
|
|
|
preRemovePath := filepath.Join(tmpDir, "preremove.sh")
|
|
|
|
if err := writePreRemove(opt, preRemovePath); err != nil {
|
|
|
|
return "", fmt.Errorf("write preremove script: %w", err)
|
|
|
|
}
|
|
|
|
postRemovePath := filepath.Join(tmpDir, "postremove.sh")
|
|
|
|
if err := writePostRemove(opt, postRemovePath); err != nil {
|
|
|
|
return "", fmt.Errorf("write postremove script: %w", err)
|
|
|
|
}
|
2021-03-03 01:19:24 +00:00
|
|
|
|
2021-04-24 01:03:48 +00:00
|
|
|
if opt.FleetCertificate != "" {
|
|
|
|
if err := writeCertificate(opt, orbitRoot); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("write fleet certificate: %w", err)
|
2021-04-24 01:03:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-03 01:19:24 +00:00
|
|
|
// Pick up all file contents
|
|
|
|
|
|
|
|
contents := files.Contents{
|
|
|
|
&files.Content{
|
2022-05-02 18:18:59 +00:00
|
|
|
Source: filepath.Join(rootDir, "**"),
|
2021-03-03 01:19:24 +00:00
|
|
|
Destination: "/",
|
|
|
|
},
|
2022-05-02 18:18:59 +00:00
|
|
|
// Symlink current into /opt/orbit/bin/orbit/orbit
|
2021-03-03 01:19:24 +00:00
|
|
|
&files.Content{
|
2022-05-02 18:18:59 +00:00
|
|
|
Source: "/opt/orbit/bin/orbit/linux/" + opt.OrbitChannel + "/orbit",
|
|
|
|
Destination: "/opt/orbit/bin/orbit/orbit",
|
2021-03-03 01:19:24 +00:00
|
|
|
Type: "symlink",
|
|
|
|
FileInfo: &files.ContentFileInfo{
|
|
|
|
Mode: constant.DefaultExecutableMode | os.ModeSymlink,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// Symlink current into /usr/local/bin
|
|
|
|
&files.Content{
|
2022-05-02 18:18:59 +00:00
|
|
|
Source: "/opt/orbit/bin/orbit/orbit",
|
2021-03-03 01:19:24 +00:00
|
|
|
Destination: "/usr/local/bin/orbit",
|
|
|
|
Type: "symlink",
|
|
|
|
FileInfo: &files.ContentFileInfo{
|
|
|
|
Mode: constant.DefaultExecutableMode | os.ModeSymlink,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:32:01 +00:00
|
|
|
// Add empty folders to be created.
|
|
|
|
for _, emptyFolder := range []string{"/var/log/osquery", "/var/log/orbit"} {
|
|
|
|
contents = append(contents, (&files.Content{
|
|
|
|
Destination: emptyFolder,
|
|
|
|
Type: "dir",
|
|
|
|
}).WithFileInfoDefaults())
|
|
|
|
}
|
2021-03-03 01:19:24 +00:00
|
|
|
|
2022-05-03 19:46:02 +00:00
|
|
|
if varLibSymlink {
|
|
|
|
contents = append(contents,
|
|
|
|
// Symlink needed to support old versions of orbit.
|
|
|
|
&files.Content{
|
|
|
|
Source: "/opt/orbit",
|
|
|
|
Destination: "/var/lib/orbit",
|
|
|
|
Type: "symlink",
|
|
|
|
FileInfo: &files.ContentFileInfo{
|
|
|
|
Mode: os.ModeSymlink,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-02 18:18:59 +00:00
|
|
|
contents, err = files.ExpandContentGlobs(contents, false)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("glob contents: %w", err)
|
|
|
|
}
|
|
|
|
for _, c := range contents {
|
|
|
|
log.Debug().Interface("file", c).Msg("added file")
|
|
|
|
}
|
|
|
|
|
2022-04-19 12:32:01 +00:00
|
|
|
// Build package
|
2021-03-03 01:19:24 +00:00
|
|
|
info := &nfpm.Info{
|
2021-11-19 01:43:52 +00:00
|
|
|
Name: "fleet-osquery",
|
2021-03-03 01:19:24 +00:00
|
|
|
Version: opt.Version,
|
2021-11-19 01:43:52 +00:00
|
|
|
Description: "Fleet osquery -- runtime and autoupdater",
|
2021-03-03 01:19:24 +00:00
|
|
|
Arch: "amd64",
|
|
|
|
Maintainer: "Fleet Engineers <engineering@fleetdm.com>",
|
2021-11-19 01:43:52 +00:00
|
|
|
Homepage: "https://fleetdm.com",
|
2021-03-03 01:19:24 +00:00
|
|
|
Overridables: nfpm.Overridables{
|
|
|
|
Contents: contents,
|
|
|
|
Scripts: nfpm.Scripts{
|
|
|
|
PostInstall: postInstallPath,
|
2022-04-19 12:32:47 +00:00
|
|
|
PreRemove: preRemovePath,
|
|
|
|
PostRemove: postRemovePath,
|
2021-03-03 01:19:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
filename := pkger.ConventionalFileName(info)
|
2022-07-11 12:49:13 +00:00
|
|
|
if opt.NativeTooling {
|
|
|
|
filename = filepath.Join("build", filename)
|
|
|
|
}
|
2021-03-03 01:19:24 +00:00
|
|
|
|
2021-08-11 14:02:22 +00:00
|
|
|
out, err := secure.OpenFile(filename, os.O_CREATE|os.O_RDWR, constant.DefaultFileMode)
|
2021-03-03 01:19:24 +00:00
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("open output file: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
if err := pkger.Package(info, out); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("write package: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
2021-08-17 12:41:56 +00:00
|
|
|
if err := out.Sync(); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return "", fmt.Errorf("sync output file: %w", err)
|
2021-08-17 12:41:56 +00:00
|
|
|
}
|
2021-03-03 01:19:24 +00:00
|
|
|
log.Info().Str("path", filename).Msg("wrote package")
|
|
|
|
|
2021-11-15 13:40:58 +00:00
|
|
|
return filename, nil
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func writeSystemdUnit(opt Options, rootPath string) error {
|
|
|
|
systemdRoot := filepath.Join(rootPath, "usr", "lib", "systemd", "system")
|
2021-08-11 14:02:22 +00:00
|
|
|
if err := secure.MkdirAll(systemdRoot, constant.DefaultDirMode); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("create systemd dir: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(
|
|
|
|
filepath.Join(systemdRoot, "orbit.service"),
|
|
|
|
[]byte(`
|
|
|
|
[Unit]
|
|
|
|
Description=Orbit osquery
|
|
|
|
After=network.service syslog.service
|
|
|
|
StartLimitIntervalSec=0
|
|
|
|
|
|
|
|
[Service]
|
|
|
|
TimeoutStartSec=0
|
|
|
|
EnvironmentFile=/etc/default/orbit
|
2022-05-02 18:18:59 +00:00
|
|
|
ExecStart=/opt/orbit/bin/orbit/orbit
|
2021-03-03 01:19:24 +00:00
|
|
|
Restart=always
|
|
|
|
RestartSec=1
|
|
|
|
KillMode=control-group
|
|
|
|
KillSignal=SIGTERM
|
|
|
|
CPUQuota=20%
|
|
|
|
|
|
|
|
[Install]
|
|
|
|
WantedBy=multi-user.target
|
|
|
|
`),
|
2022-05-23 22:18:19 +00:00
|
|
|
constant.DefaultSystemdUnitMode,
|
2021-03-03 01:19:24 +00:00
|
|
|
); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("write file: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var envTemplate = template.Must(template.New("env").Parse(`
|
2021-03-10 22:42:02 +00:00
|
|
|
ORBIT_UPDATE_URL={{ .UpdateURL }}
|
2021-03-10 00:27:35 +00:00
|
|
|
ORBIT_ORBIT_CHANNEL={{ .OrbitChannel }}
|
|
|
|
ORBIT_OSQUERYD_CHANNEL={{ .OsquerydChannel }}
|
2022-04-11 20:42:36 +00:00
|
|
|
ORBIT_UPDATE_INTERVAL={{ .OrbitUpdateInterval }}
|
2022-05-04 14:14:12 +00:00
|
|
|
{{ if .Desktop }}
|
|
|
|
ORBIT_FLEET_DESKTOP=true
|
|
|
|
ORBIT_DESKTOP_CHANNEL={{ .DesktopChannel }}
|
|
|
|
{{ end }}
|
2021-03-10 00:27:35 +00:00
|
|
|
{{ if .Insecure }}ORBIT_INSECURE=true{{ end }}
|
2022-02-18 18:42:39 +00:00
|
|
|
{{ if .DisableUpdates }}ORBIT_DISABLE_UPDATES=true{{ end }}
|
2021-03-03 01:19:24 +00:00
|
|
|
{{ if .FleetURL }}ORBIT_FLEET_URL={{.FleetURL}}{{ end }}
|
2022-05-02 18:18:59 +00:00
|
|
|
{{ if .FleetCertificate }}ORBIT_FLEET_CERTIFICATE=/opt/orbit/fleet.pem{{ end }}
|
2021-03-03 01:19:24 +00:00
|
|
|
{{ if .EnrollSecret }}ORBIT_ENROLL_SECRET={{.EnrollSecret}}{{ end }}
|
2021-03-09 23:22:17 +00:00
|
|
|
{{ if .Debug }}ORBIT_DEBUG=true{{ end }}
|
2021-03-03 01:19:24 +00:00
|
|
|
`))
|
|
|
|
|
|
|
|
func writeEnvFile(opt Options, rootPath string) error {
|
|
|
|
envRoot := filepath.Join(rootPath, "etc", "default")
|
2021-08-11 14:02:22 +00:00
|
|
|
if err := secure.MkdirAll(envRoot, constant.DefaultDirMode); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("create env dir: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var contents bytes.Buffer
|
|
|
|
if err := envTemplate.Execute(&contents, opt); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("execute template: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(
|
|
|
|
filepath.Join(envRoot, "orbit"),
|
|
|
|
contents.Bytes(),
|
|
|
|
constant.DefaultFileMode,
|
|
|
|
); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("write file: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-23 22:18:19 +00:00
|
|
|
var postInstallTemplate = template.Must(template.New("postinstall").Parse(`#!/bin/sh
|
2021-03-03 01:19:24 +00:00
|
|
|
|
|
|
|
# Exit on error
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# If we have a systemd, daemon-reload away now
|
2022-01-31 23:56:05 +00:00
|
|
|
if command -v systemctl >/dev/null 2>&1; then
|
|
|
|
systemctl daemon-reload >/dev/null 2>&1
|
2021-03-03 01:19:24 +00:00
|
|
|
{{ if .StartService -}}
|
2022-01-01 23:18:30 +00:00
|
|
|
systemctl restart orbit.service 2>&1
|
2022-01-31 23:56:05 +00:00
|
|
|
systemctl enable orbit.service 2>&1
|
2021-03-03 01:19:24 +00:00
|
|
|
{{- end}}
|
|
|
|
fi
|
|
|
|
`))
|
|
|
|
|
|
|
|
func writePostInstall(opt Options, path string) error {
|
|
|
|
var contents bytes.Buffer
|
|
|
|
if err := postInstallTemplate.Execute(&contents, opt); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("execute template: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := ioutil.WriteFile(path, contents.Bytes(), constant.DefaultFileMode); err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("write file: %w", err)
|
2021-03-03 01:19:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-19 12:32:47 +00:00
|
|
|
|
|
|
|
func writePreRemove(opt Options, path string) error {
|
2022-05-04 14:14:12 +00:00
|
|
|
// We add `|| true` in case the service is not running
|
|
|
|
// or has been manually disabled already. Otherwise,
|
|
|
|
// uninstallation fails.
|
2022-06-21 19:26:14 +00:00
|
|
|
//
|
|
|
|
// "pkill fleet-desktop" is required because the application
|
|
|
|
// runs as user (separate from sudo command that launched it),
|
|
|
|
// so on some systems it's not killed properly.
|
2022-04-19 12:32:47 +00:00
|
|
|
if err := ioutil.WriteFile(path, []byte(`#!/bin/sh
|
|
|
|
|
2022-05-04 14:14:12 +00:00
|
|
|
systemctl stop orbit.service || true
|
|
|
|
systemctl disable orbit.service || true
|
2022-06-21 19:26:14 +00:00
|
|
|
pkill fleet-desktop || true
|
2022-04-19 12:32:47 +00:00
|
|
|
`), constant.DefaultFileMode); err != nil {
|
|
|
|
return fmt.Errorf("write file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writePostRemove(opt Options, path string) error {
|
|
|
|
if err := ioutil.WriteFile(path, []byte(`#!/bin/sh
|
|
|
|
|
2022-05-02 18:18:59 +00:00
|
|
|
rm -rf /var/lib/orbit /var/log/orbit /usr/local/bin/orbit /etc/default/orbit /usr/lib/systemd/system/orbit.service /opt/orbit
|
2022-04-19 12:32:47 +00:00
|
|
|
`), constant.DefaultFileMode); err != nil {
|
|
|
|
return fmt.Errorf("write file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|