-
Notifications
You must be signed in to change notification settings - Fork 467
/
Copy pathlinux_shared.go
373 lines (321 loc) · 10.7 KB
/
linux_shared.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package packaging
import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"text/template"
"github.com/Masterminds/semver"
"github.com/fleetdm/fleet/v4/orbit/pkg/constant"
"github.com/fleetdm/fleet/v4/orbit/pkg/update"
"github.com/fleetdm/fleet/v4/pkg/secure"
"github.com/goreleaser/nfpm/v2"
"github.com/goreleaser/nfpm/v2/files"
"github.com/rs/zerolog/log"
)
func buildNFPM(opt Options, pkger nfpm.Packager) (string, error) {
// Initialize directories
tmpDir, err := initializeTempDir()
if err != nil {
return "", err
}
defer os.RemoveAll(tmpDir)
rootDir := filepath.Join(tmpDir, "root")
if err := secure.MkdirAll(rootDir, constant.DefaultDirMode); err != nil {
return "", fmt.Errorf("create root dir: %w", err)
}
orbitRoot := filepath.Join(rootDir, "opt", "orbit")
if err := secure.MkdirAll(orbitRoot, constant.DefaultDirMode); err != nil {
return "", fmt.Errorf("create orbit dir: %w", err)
}
// Initialize autoupdate metadata
updateOpt := update.DefaultOptions
updateOpt.RootDirectory = orbitRoot
updateOpt.Targets = update.LinuxTargets
updateOpt.ServerCertificatePath = opt.UpdateTLSServerCertificate
if opt.UpdateTLSClientCertificate != "" {
updateClientCrt, err := tls.LoadX509KeyPair(opt.UpdateTLSClientCertificate, opt.UpdateTLSClientKey)
if err != nil {
return "", fmt.Errorf("error loading update client certificate and key: %w", err)
}
updateOpt.ClientCertificate = &updateClientCrt
}
if opt.Desktop {
updateOpt.Targets["desktop"] = update.DesktopLinuxTarget
// Override default channel with the provided value.
updateOpt.Targets.SetTargetChannel("desktop", opt.DesktopChannel)
}
// Override default channels with the provided values.
updateOpt.Targets.SetTargetChannel("orbit", opt.OrbitChannel)
updateOpt.Targets.SetTargetChannel("osqueryd", opt.OsquerydChannel)
updateOpt.ServerURL = opt.UpdateURL
if opt.UpdateRoots != "" {
updateOpt.RootKeys = opt.UpdateRoots
}
updatesData, err := InitializeUpdates(updateOpt)
if err != nil {
return "", fmt.Errorf("initialize updates: %w", err)
}
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
}
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.
// Write files
if err := writeSystemdUnit(opt, rootDir); err != nil {
return "", fmt.Errorf("write systemd unit: %w", err)
}
if err := writeEnvFile(opt, rootDir); err != nil {
return "", fmt.Errorf("write env file: %w", err)
}
if err := writeOsqueryFlagfile(opt, orbitRoot); err != nil {
return "", fmt.Errorf("write flagfile: %w", err)
}
if err := writeOsqueryCertPEM(opt, orbitRoot); err != nil {
return "", fmt.Errorf("write certs.pem: %w", err)
}
postInstallPath := filepath.Join(tmpDir, "postinstall.sh")
if err := writePostInstall(opt, postInstallPath); err != nil {
return "", fmt.Errorf("write postinstall script: %w", err)
}
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)
}
if opt.FleetCertificate != "" {
if err := writeFleetServerCertificate(opt, orbitRoot); err != nil {
return "", fmt.Errorf("write fleet server certificate: %w", err)
}
}
if opt.FleetTLSClientCertificate != "" {
if err := writeFleetClientCertificate(opt, orbitRoot); err != nil {
return "", fmt.Errorf("write fleet client certificate: %w", err)
}
}
if opt.UpdateTLSServerCertificate != "" {
if err := writeUpdateServerCertificate(opt, orbitRoot); err != nil {
return "", fmt.Errorf("write update server certificate: %w", err)
}
}
if opt.UpdateTLSClientCertificate != "" {
if err := writeUpdateClientCertificate(opt, orbitRoot); err != nil {
return "", fmt.Errorf("write update client certificate: %w", err)
}
}
// Pick up all file contents
contents := files.Contents{
&files.Content{
Source: filepath.Join(rootDir, "**"),
Destination: "/",
},
// Symlink current into /opt/orbit/bin/orbit/orbit
&files.Content{
Source: "/opt/orbit/bin/orbit/linux/" + opt.OrbitChannel + "/orbit",
Destination: "/opt/orbit/bin/orbit/orbit",
Type: "symlink",
FileInfo: &files.ContentFileInfo{
Mode: constant.DefaultExecutableMode | os.ModeSymlink,
},
},
// Symlink current into /usr/local/bin
&files.Content{
Source: "/opt/orbit/bin/orbit/orbit",
Destination: "/usr/local/bin/orbit",
Type: "symlink",
FileInfo: &files.ContentFileInfo{
Mode: constant.DefaultExecutableMode | os.ModeSymlink,
},
},
}
// 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())
}
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,
},
})
}
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")
}
// Build package
info := &nfpm.Info{
Name: "fleet-osquery",
Version: opt.Version,
Description: "Fleet osquery -- runtime and autoupdater",
Arch: "amd64",
Maintainer: "Fleet Device Management",
Vendor: "Fleet Device Management",
License: "https://github.com/fleetdm/fleet/blob/main/LICENSE",
Homepage: "https://fleetdm.com",
Overridables: nfpm.Overridables{
Contents: contents,
Scripts: nfpm.Scripts{
PostInstall: postInstallPath,
PreRemove: preRemovePath,
PostRemove: postRemovePath,
},
},
}
filename := pkger.ConventionalFileName(info)
if opt.NativeTooling {
filename = filepath.Join("build", filename)
}
out, err := secure.OpenFile(filename, os.O_CREATE|os.O_RDWR, constant.DefaultFileMode)
if err != nil {
return "", fmt.Errorf("open output file: %w", err)
}
defer out.Close()
if err := pkger.Package(info, out); err != nil {
return "", fmt.Errorf("write package: %w", err)
}
if err := out.Sync(); err != nil {
return "", fmt.Errorf("sync output file: %w", err)
}
log.Info().Str("path", filename).Msg("wrote package")
return filename, nil
}
func writeSystemdUnit(opt Options, rootPath string) error {
systemdRoot := filepath.Join(rootPath, "usr", "lib", "systemd", "system")
if err := secure.MkdirAll(systemdRoot, constant.DefaultDirMode); err != nil {
return fmt.Errorf("create systemd dir: %w", err)
}
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
ExecStart=/opt/orbit/bin/orbit/orbit
Restart=always
RestartSec=1
KillMode=control-group
KillSignal=SIGTERM
CPUQuota=20%
[Install]
WantedBy=multi-user.target
`),
constant.DefaultSystemdUnitMode,
); err != nil {
return fmt.Errorf("write file: %w", err)
}
return nil
}
var envTemplate = template.Must(template.New("env").Parse(`
ORBIT_UPDATE_URL={{ .UpdateURL }}
ORBIT_ORBIT_CHANNEL={{ .OrbitChannel }}
ORBIT_OSQUERYD_CHANNEL={{ .OsquerydChannel }}
ORBIT_UPDATE_INTERVAL={{ .OrbitUpdateInterval }}
{{ if .Desktop }}
ORBIT_FLEET_DESKTOP=true
ORBIT_DESKTOP_CHANNEL={{ .DesktopChannel }}
{{ if .FleetDesktopAlternativeBrowserHost }}
ORBIT_FLEET_DESKTOP_ALTERNATIVE_BROWSER_HOST={{ .FleetDesktopAlternativeBrowserHost }}
{{ end }}
{{ end }}
{{ if .Insecure }}ORBIT_INSECURE=true{{ end }}
{{ if .DisableUpdates }}ORBIT_DISABLE_UPDATES=true{{ end }}
{{ if .FleetURL }}ORBIT_FLEET_URL={{.FleetURL}}{{ end }}
{{ if .FleetCertificate }}ORBIT_FLEET_CERTIFICATE=/opt/orbit/fleet.pem{{ end }}
{{ if .UpdateTLSServerCertificate }}ORBIT_UPDATE_TLS_CERTIFICATE=/opt/orbit/update.pem{{ end }}
{{ if .EnrollSecret }}ORBIT_ENROLL_SECRET={{.EnrollSecret}}{{ end }}
{{ if .Debug }}ORBIT_DEBUG=true{{ end }}
{{ if .EnableScripts }}ORBIT_ENABLE_SCRIPTS=true{{ end }}
`))
func writeEnvFile(opt Options, rootPath string) error {
envRoot := filepath.Join(rootPath, "etc", "default")
if err := secure.MkdirAll(envRoot, constant.DefaultDirMode); err != nil {
return fmt.Errorf("create env dir: %w", err)
}
var contents bytes.Buffer
if err := envTemplate.Execute(&contents, opt); err != nil {
return fmt.Errorf("execute template: %w", err)
}
if err := ioutil.WriteFile(
filepath.Join(envRoot, "orbit"),
contents.Bytes(),
constant.DefaultFileMode,
); err != nil {
return fmt.Errorf("write file: %w", err)
}
return nil
}
var postInstallTemplate = template.Must(template.New("postinstall").Parse(`#!/bin/sh
# Exit on error
set -e
# If we have a systemd, daemon-reload away now
if command -v systemctl >/dev/null 2>&1; then
systemctl daemon-reload >/dev/null 2>&1
{{ if .StartService -}}
systemctl restart orbit.service 2>&1
systemctl enable orbit.service 2>&1
{{- end}}
fi
`))
func writePostInstall(opt Options, path string) error {
var contents bytes.Buffer
if err := postInstallTemplate.Execute(&contents, opt); err != nil {
return fmt.Errorf("execute template: %w", err)
}
if err := ioutil.WriteFile(path, contents.Bytes(), constant.DefaultFileMode); err != nil {
return fmt.Errorf("write file: %w", err)
}
return nil
}
func writePreRemove(opt Options, path string) error {
// We add `|| true` in case the service is not running
// or has been manually disabled already. Otherwise,
// uninstallation fails.
//
// "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.
if err := ioutil.WriteFile(path, []byte(`#!/bin/sh
systemctl stop orbit.service || true
systemctl disable orbit.service || true
pkill fleet-desktop || true
`), 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
rm -rf /var/lib/orbit /var/log/orbit /usr/local/bin/orbit /etc/default/orbit /usr/lib/systemd/system/orbit.service /opt/orbit
`), constant.DefaultFileMode); err != nil {
return fmt.Errorf("write file: %w", err)
}
return nil
}