-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathservice_windows.go
237 lines (223 loc) · 8.28 KB
/
service_windows.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
// Teleport
// Copyright (C) 2025 Gravitational, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package vnet
import (
"cmp"
"context"
"log/slog"
"os"
"path/filepath"
"syscall"
"time"
"github.com/alecthomas/kingpin/v2"
"github.com/gravitational/trace"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/mgr"
)
const (
ServiceCommand = "vnet-service"
serviceName = "TeleportVNet"
serviceDescription = "This service manages networking and OS configuration for Teleport VNet."
serviceAccessFlags = windows.SERVICE_START | windows.SERVICE_STOP | windows.SERVICE_QUERY_STATUS
terminateTimeout = 30 * time.Second
)
// runService is called from the normal user process to run the VNet Windows in
// the background and wait for it to exit. It will terminate the service and
// return immediately if [ctx] is canceled.
func runService(ctx context.Context, cfg *windowsAdminProcessConfig) error {
service, err := startService(ctx, cfg)
if err != nil {
return trace.Wrap(err)
}
defer service.Close()
log.InfoContext(ctx, "Started Windows service", "service", service.Name)
ticker := time.Tick(time.Second)
loop:
for {
select {
case <-ctx.Done():
log.InfoContext(ctx, "Context canceled, stopping Windows service")
if _, err := service.Control(svc.Stop); err != nil {
return trace.Wrap(err, "sending stop request to Windows service %s", service.Name)
}
break loop
case <-ticker:
status, err := service.Query()
if err != nil {
return trace.Wrap(err, "querying Windows service %s", service.Name)
}
if status.State != svc.Running && status.State != svc.StartPending {
return trace.Errorf("service stopped running prematurely, status: %+v", status)
}
}
}
// Wait for the service to actually stop. Add some buffer to
// terminateTimeout which the service also uses to terminate itself to
// hopefully allow it to exit on its own.
deadline := time.After(terminateTimeout + 5*time.Second)
for {
select {
case <-deadline:
return trace.Errorf("Windows service %s failed to stop with %v", service.Name, terminateTimeout)
case <-ticker:
status, err := service.Query()
if err != nil {
return trace.Wrap(err, "querying Windows service %s", service.Name)
}
if status.State == svc.Stopped {
return nil
}
}
}
}
// startService starts the Windows VNet admin service in the background.
func startService(ctx context.Context, cfg *windowsAdminProcessConfig) (*mgr.Service, error) {
// Avoid [mgr.Connect] because it requests elevated permissions.
scManager, err := windows.OpenSCManager(nil /*machine*/, nil /*database*/, windows.SC_MANAGER_CONNECT)
if err != nil {
return nil, trace.Wrap(err, "opening Windows service manager")
}
defer windows.CloseServiceHandle(scManager)
serviceNamePtr, err := syscall.UTF16PtrFromString(serviceName)
if err != nil {
return nil, trace.Wrap(err, "converting service name to UTF16")
}
serviceHandle, err := windows.OpenService(scManager, serviceNamePtr, serviceAccessFlags)
if err != nil {
return nil, trace.Wrap(err, "opening Windows service %v", serviceName)
}
service := &mgr.Service{
Name: serviceName,
Handle: serviceHandle,
}
if err := service.Start(ServiceCommand,
"--addr", cfg.clientApplicationServiceAddr,
"--cred-path", cfg.serviceCredentialPath,
"--user-sid", cfg.userSID,
); err != nil {
return nil, trace.Wrap(err, "starting Windows service %s", serviceName)
}
return service, nil
}
// ServiceMain runs the Windows VNet admin service.
func ServiceMain() error {
if err := setupServiceLogger(); err != nil {
return trace.Wrap(err, "setting up logger for service")
}
if err := svc.Run(serviceName, &windowsService{}); err != nil {
return trace.Wrap(err, "running Windows service")
}
return nil
}
// windowsService implements [svc.Handler].
type windowsService struct{}
// Execute implements [svc.Handler.Execute], the GoDoc is copied below.
//
// Execute will be called by the package code at the start of the service, and
// the service will exit once Execute completes. Inside Execute you must read
// service change requests from [requests] and act accordingly. You must keep
// service control manager up to date about state of your service by writing
// into [status] as required. args contains service name followed by argument
// strings passed to the service.
// You can provide service exit code in exitCode return parameter, with 0 being
// "no error". You can also indicate if exit code, if any, is service specific
// or not by using svcSpecificEC parameter.
func (s *windowsService) Execute(args []string, requests <-chan svc.ChangeRequest, status chan<- svc.Status) (svcSpecificEC bool, exitCode uint32) {
const cmdsAccepted = svc.AcceptStop // Interrogate is always accepted and there is no const for it.
status <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
errCh := make(chan error)
go func() { errCh <- s.run(ctx, args) }()
var terminateTimedOut <-chan time.Time
loop:
for {
select {
case request := <-requests:
switch request.Cmd {
case svc.Interrogate:
state := svc.Running
if ctx.Err() != nil {
state = svc.StopPending
}
status <- svc.Status{State: state, Accepts: cmdsAccepted}
case svc.Stop:
slog.InfoContext(ctx, "Received stop command, shutting down service")
// Cancel the context passed to s.run to terminate the
// networking stack.
cancel()
terminateTimedOut = cmp.Or(terminateTimedOut, time.After(terminateTimeout))
status <- svc.Status{State: svc.StopPending}
}
case <-terminateTimedOut:
slog.ErrorContext(ctx, "Networking stack failed to terminate within timeout, exiting process",
slog.Duration("timeout", terminateTimeout))
exitCode = 1
break loop
case err := <-errCh:
slog.ErrorContext(ctx, "Windows VNet service terminated", "error", err)
if err != nil {
exitCode = 1
}
break loop
}
}
status <- svc.Status{State: svc.Stopped, Win32ExitCode: exitCode}
return false, exitCode
}
func (s *windowsService) run(ctx context.Context, args []string) error {
var cfg windowsAdminProcessConfig
app := kingpin.New(serviceName, "Teleport VNet Windows Service")
serviceCmd := app.Command("vnet-service", "Start the VNet service.")
serviceCmd.Flag("addr", "client application service address").Required().StringVar(&cfg.clientApplicationServiceAddr)
serviceCmd.Flag("cred-path", "path to TLS credentials for connecting to client application").Required().StringVar(&cfg.serviceCredentialPath)
serviceCmd.Flag("user-sid", "SID of the user running the client application").Required().StringVar(&cfg.userSID)
cmd, err := app.Parse(args[1:])
if err != nil {
return trace.Wrap(err, "parsing runtime arguments to Windows service")
}
if cmd != serviceCmd.FullCommand() {
return trace.BadParameter("Windows service runtime arguments did not match \"vnet-service\", args: %v", args[1:])
}
if err := runWindowsAdminProcess(ctx, &cfg); err != nil {
return trace.Wrap(err, "running admin process")
}
return nil
}
func setupServiceLogger() error {
logFile, err := serviceLogFile()
if err != nil {
return trace.Wrap(err, "creating log file for service")
}
slog.SetDefault(slog.New(slog.NewTextHandler(logFile, &slog.HandlerOptions{
Level: slog.LevelDebug,
})))
return nil
}
func serviceLogFile() (*os.File, error) {
// TODO(nklaassen): find a better path for Windows service logs.
exePath, err := os.Executable()
if err != nil {
return nil, trace.Wrap(err, "getting current executable path")
}
dir := filepath.Dir(exePath)
logFile, err := os.Create(filepath.Join(dir, "logs.txt"))
if err != nil {
return nil, trace.Wrap(err, "creating log file")
}
return logFile, nil
}