-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support running SPIRE as a Windows service (#3625)
* Support running SPIRE as a Windows service Signed-off-by: Agustín Martínez Fayó <amartinezfayo@gmail.com>
- Loading branch information
1 parent
4e8aee5
commit a7df8a5
Showing
13 changed files
with
578 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package entrypoint | ||
|
||
import ( | ||
"context" | ||
"os" | ||
) | ||
|
||
type EntryPoint struct { | ||
runCmdFn func(ctx context.Context, args []string) int | ||
} | ||
|
||
func NewEntryPoint(runFn func(ctx context.Context, args []string) int) *EntryPoint { | ||
return &EntryPoint{ | ||
runCmdFn: runFn, | ||
} | ||
} | ||
|
||
func (e *EntryPoint) Main() int { | ||
return e.runCmdFn(context.Background(), os.Args[1:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package entrypoint | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestEntryPoint(t *testing.T) { | ||
assert.Equal(t, | ||
NewEntryPoint(func(ctx context.Context, args []string) int { return 0 }).Main(), | ||
0) | ||
|
||
assert.Equal(t, | ||
NewEntryPoint(func(ctx context.Context, args []string) int { return 1 }).Main(), | ||
1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//go:build windows | ||
// +build windows | ||
|
||
package entrypoint | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"golang.org/x/sys/windows/svc" | ||
) | ||
|
||
type systemCaller interface { | ||
IsWindowsService() (bool, error) | ||
Run(name string, handler svc.Handler) error | ||
} | ||
|
||
type systemCall struct { | ||
} | ||
|
||
func (s *systemCall) IsWindowsService() (bool, error) { | ||
return svc.IsWindowsService() | ||
} | ||
|
||
func (s *systemCall) Run(name string, handler svc.Handler) error { | ||
return svc.Run(name, handler) | ||
} | ||
|
||
type EntryPoint struct { | ||
handler svc.Handler | ||
runCmdFn func(ctx context.Context, args []string) int | ||
sc systemCaller | ||
} | ||
|
||
func NewEntryPoint(runCmdFn func(ctx context.Context, args []string) int) *EntryPoint { | ||
return &EntryPoint{ | ||
runCmdFn: runCmdFn, | ||
handler: &service{ | ||
executeServiceFn: func(ctx context.Context, stop context.CancelFunc, args []string) int { | ||
defer stop() | ||
retCode := runCmdFn(ctx, args[1:]) | ||
return retCode | ||
}, | ||
}, | ||
sc: &systemCall{}, | ||
} | ||
} | ||
|
||
func (e *EntryPoint) Main() int { | ||
// Determining if SPIRE is running as a Windows service is done | ||
// with a best-effort approach. If there is an error, just fallback | ||
// to the behavior of not running as a Windows service. | ||
isWindowsService, err := e.sc.IsWindowsService() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "Could not determine if running as a Windows service: %v", err) | ||
} | ||
if isWindowsService { | ||
errChan := make(chan error) | ||
go func() { | ||
// Since the service runs in its own process, the service name is ignored. | ||
// https://learn.microsoft.com/en-us/windows/win32/api/winsvc/nf-winsvc-startservicectrldispatcherw | ||
errChan <- e.sc.Run("", e.handler) | ||
}() | ||
err = <-errChan | ||
if err != nil { | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
return e.runCmdFn(context.Background(), os.Args[1:]) | ||
} |
Oops, something went wrong.