Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP health server (closes #115) #227

Open
wants to merge 44 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
d378b30
Add HTTP health server (closes #115)
keeganwitt Dec 7, 2024
4a0e3b6
PR feedback
keeganwitt Dec 12, 2024
6ac9f3f
More PR feedback
keeganwitt Dec 12, 2024
6b32bae
Fix DaemonModeFlagName const
keeganwitt Dec 12, 2024
b4b21f6
Make health check path configurable and implement default health chec…
keeganwitt Dec 13, 2024
8a61552
Merge remote-tracking branch 'upstream/main' into 115
keeganwitt Dec 13, 2024
07985d7
Keep ParseConfigFile in main
keeganwitt Dec 14, 2024
28885ec
Fix tests
keeganwitt Dec 14, 2024
1e7ccd1
Merge remote-tracking branch 'upstream/main' into 115
keeganwitt Dec 14, 2024
59af728
Upgrade golangci-lint to fix error
keeganwitt Dec 14, 2024
68a27ba
Update cmd/spiffe-helper/config/config.go
keeganwitt Dec 16, 2024
6084685
Update README.md
keeganwitt Dec 16, 2024
944d880
Fix remaining config renames
keeganwitt Dec 17, 2024
15928aa
Merge branch 'main' into 115
keeganwitt Dec 17, 2024
d8ff964
Add back flag duplication
keeganwitt Dec 17, 2024
1640761
Don't use a pointer for HealthCheckConfig
keeganwitt Dec 17, 2024
97019c6
Remove global variable
keeganwitt Dec 17, 2024
4f28004
Avoid passing entire HCL config into health check, simplify status JSON
keeganwitt Jan 5, 2025
4ee902e
Update cmd/spiffe-helper/config/config.go
keeganwitt Jan 15, 2025
86adfb2
Update pkg/health/health.go
keeganwitt Jan 15, 2025
47d461c
Update cmd/spiffe-helper/main.go
keeganwitt Jan 15, 2025
71b5463
Add missing X509WriteSuccess check
keeganwitt Jan 15, 2025
5801257
Add JSON marshalling tags
keeganwitt Jan 15, 2025
2375ab6
Lint fixes
keeganwitt Jan 15, 2025
a7a3ef2
More PR feedback changes
keeganwitt Jan 15, 2025
f9c6688
Lint fixes
keeganwitt Jan 15, 2025
a4bca3f
Update pkg/sidecar/sidecar.go
keeganwitt Jan 15, 2025
7a8cc0d
Update pkg/sidecar/sidecar.go
keeganwitt Jan 15, 2025
dd5f6be
Update pkg/sidecar/sidecar.go
keeganwitt Jan 15, 2025
273db1a
Update pkg/sidecar/sidecar.go
keeganwitt Jan 15, 2025
28c7f4c
Update pkg/sidecar/sidecar_test.go
keeganwitt Jan 15, 2025
653c99e
Update pkg/sidecar/sidecar.go
keeganwitt Jan 15, 2025
4e2696a
Update pkg/sidecar/sidecar.go
keeganwitt Jan 15, 2025
d36cf15
Update pkg/sidecar/sidecar.go
keeganwitt Jan 15, 2025
cf1f22a
Update pkg/health/health.go
keeganwitt Jan 15, 2025
a7c0b89
Lint fixes
keeganwitt Jan 16, 2025
bcbde1e
Start sidecar after health server
keeganwitt Jan 16, 2025
98d5fbd
Create separate health endpoints for liveness and readiness
keeganwitt Jan 16, 2025
c563e1c
Move StartHealthServer() into startSidecar()
keeganwitt Jan 17, 2025
241ca62
Lint fix
keeganwitt Jan 17, 2025
9cad97f
Update readme
keeganwitt Jan 17, 2025
61be108
Log when health server is starting
keeganwitt Jan 17, 2025
b1b054b
Change Error response to also be JSON
keeganwitt Jan 17, 2025
dff19b4
Refactoring
keeganwitt Jan 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,28 @@ The flag `-exitWhenReady` is also supported.
## Configuration
The configuration file is an [HCL](https://github.com/hashicorp/hcl) formatted file that defines the following configurations:

| Configuration | Description | Example Value |
|-------------------------------|----------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `agent_address` | Socket address of SPIRE Agent. | `"/tmp/agent.sock"` |
| `cmd` | The path to the process to launch. | `"ghostunnel"` |
| `cmd_args` | The arguments of the process to launch. | `"server --listen localhost:8002 --target localhost:8001--keystore certs/svid_key.pem --cacert certs/svid_bundle.pem --allow-uri-san spiffe://example.org/Database"` |
| `cert_dir` | Directory name to store the fetched certificates. This directory must be created previously. | `"certs"` |
| `daemon_mode` | Toggle running as a daemon, keeping X.509 and JWT up to date; or just fetch X.509 and JWT and exit 0 | `true` |
| `add_intermediates_to_bundle` | Add intermediate certificates into Bundle file instead of SVID file. | `true` |
| `renew_signal` | The signal that the process to be launched expects to reload the certificates. It is not supported on Windows. | `"SIGUSR1"` |
| `svid_file_name` | File name to be used to store the X.509 SVID public certificate in PEM format. | `"svid.pem"` |
| `svid_key_file_name` | File name to be used to store the X.509 SVID private key and public certificate in PEM format. | `"svid_key.pem"` |
| `svid_bundle_file_name` | File name to be used to store the X.509 SVID Bundle in PEM format. | `"svid_bundle.pem"` |
| `jwt_svids` | An array with the audience, optional extra audiences array, and file name to store the JWT SVIDs. File is Base64-encoded string). | `[{jwt_audience="your-audience", jwt_extra_audiences=["your-extra-audience-1", "your-extra-audience-2"], jwt_svid_file_name="jwt_svid.token"}]` |
| `jwt_bundle_file_name` | File name to be used to store JWT Bundle in JSON format. | `"jwt_bundle.json"` |
| `include_federated_domains` | Include trust domains from federated servers in the CA bundle. | `true` |
| `cert_file_mode` | The octal file mode to use when saving the X.509 public certificate file. | `0644` |
| `key_file_mode` | The octal file mode to use when saving the X.509 private key file. | `0600` |
| `jwt_bundle_file_mode` | The octal file mode to use when saving a JWT Bundle file. | `0600` |
| `jwt_svid_file_mode` | The octal file mode to use when saving a JWT SVID file. | `0600` |
| Configuration | Description | Example Value |
|-------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `agent_address` | Socket address of SPIRE Agent. | `"/tmp/agent.sock"` |
| `cmd` | The path to the process to launch. | `"ghostunnel"` |
| `cmd_args` | The arguments of the process to launch. | `"server --listen localhost:8002 --target localhost:8001--keystore certs/svid_key.pem --cacert certs/svid_bundle.pem --allow-uri-san spiffe://example.org/Database"` |
| `cert_dir` | Directory name to store the fetched certificates. This directory must be created previously. | `"certs"` |
| `daemon_mode` | Toggle running as a daemon, keeping X.509 and JWT up to date; or just fetch X.509 and JWT and exit 0 | `true` |
| `add_intermediates_to_bundle` | Add intermediate certificates into Bundle file instead of SVID file. | `true` |
| `renew_signal` | The signal that the process to be launched expects to reload the certificates. It is not supported on Windows. | `"SIGUSR1"` |
| `svid_file_name` | File name to be used to store the X.509 SVID public certificate in PEM format. | `"svid.pem"` |
| `svid_key_file_name` | File name to be used to store the X.509 SVID private key and public certificate in PEM format. | `"svid_key.pem"` |
| `svid_bundle_file_name` | File name to be used to store the X.509 SVID Bundle in PEM format. | `"svid_bundle.pem"` |
| `jwt_svids` | An array with the audience, optional extra audiences array, and file name to store the JWT SVIDs. File is Base64-encoded string). | `[{jwt_audience="your-audience", jwt_extra_audiences=["your-extra-audience-1", "your-extra-audience-2"], jwt_svid_file_name="jwt_svid.token"}]` |
| `jwt_bundle_file_name` | File name to be used to store JWT Bundle in JSON format. | `"jwt_bundle.json"` |
| `include_federated_domains` | Include trust domains from federated servers in the CA bundle. | `true` |
| `cert_file_mode` | The octal file mode to use when saving the X.509 public certificate file. | `0644` |
| `key_file_mode` | The octal file mode to use when saving the X.509 private key file. | `0600` |
| `jwt_bundle_file_mode` | The octal file mode to use when saving a JWT Bundle file. | `0600` |
| `jwt_svid_file_mode` | The octal file mode to use when saving a JWT SVID file. | `0600` |
| `health_checks.enable_health_check` | Whether to start an HTTP server at the configured endpoint for the daemon health. Doesn't apply for non-daemon mode. | `false` |
| `health_checks.health_check_port` | The port to run the HTTP health server. | `8081` |
| `health_checks.health_check_path` | The URL path for the health check | `/healthz` |
keeganwitt marked this conversation as resolved.
Show resolved Hide resolved

### Configuration example
```
Expand Down
66 changes: 51 additions & 15 deletions cmd/spiffe-helper/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,35 @@ import (
"github.com/spiffe/spiffe-helper/pkg/sidecar"
)

const (
DaemonModeFlagName = "daemon-mode"
)

keeganwitt marked this conversation as resolved.
Show resolved Hide resolved
const (
defaultAgentAddress = "/tmp/spire-agent/public/api.sock"
defaultCertFileMode = 0644
defaultKeyFileMode = 0600
defaultJWTBundleFileMode = 0600
defaultJWTSVIDFileMode = 0600
defaultHealthCheckPort = 8081
defaultHealthCheckPath = "/healthz"
keeganwitt marked this conversation as resolved.
Show resolved Hide resolved
)

type Config struct {
AddIntermediatesToBundle bool `hcl:"add_intermediates_to_bundle"`
AgentAddress string `hcl:"agent_address"`
Cmd string `hcl:"cmd"`
CmdArgs string `hcl:"cmd_args"`
PIDFileName string `hcl:"pid_file_name"`
CertDir string `hcl:"cert_dir"`
CertFileMode int `hcl:"cert_file_mode"`
KeyFileMode int `hcl:"key_file_mode"`
JWTBundleFileMode int `hcl:"jwt_bundle_file_mode"`
JWTSVIDFileMode int `hcl:"jwt_svid_file_mode"`
IncludeFederatedDomains bool `hcl:"include_federated_domains"`
RenewSignal string `hcl:"renew_signal"`
DaemonMode *bool `hcl:"daemon_mode"`
AddIntermediatesToBundle bool `hcl:"add_intermediates_to_bundle"`
AgentAddress string `hcl:"agent_address"`
Cmd string `hcl:"cmd"`
CmdArgs string `hcl:"cmd_args"`
PIDFileName string `hcl:"pid_file_name"`
CertDir string `hcl:"cert_dir"`
CertFileMode int `hcl:"cert_file_mode"`
KeyFileMode int `hcl:"key_file_mode"`
JWTBundleFileMode int `hcl:"jwt_bundle_file_mode"`
JWTSVIDFileMode int `hcl:"jwt_svid_file_mode"`
IncludeFederatedDomains bool `hcl:"include_federated_domains"`
RenewSignal string `hcl:"renew_signal"`
DaemonMode *bool `hcl:"daemon_mode"`
HealthCheck *HealthCheckConfig `hcl:"health_checks"`
keeganwitt marked this conversation as resolved.
Show resolved Hide resolved

// x509 configuration
SVIDFileName string `hcl:"svid_file_name"`
Expand All @@ -50,6 +57,12 @@ type Config struct {
UnusedKeyPositions map[string][]token.Pos `hcl:",unusedKeyPositions"`
}

type HealthCheckConfig struct {
EnableHealthCheck *bool `hcl:"enable_health_check"`
HealthCheckPort int `hcl:"health_check_port"`
HealthCheckPath string `hcl:"health_check_path"`
}

type JWTConfig struct {
JWTAudience string `hcl:"jwt_audience"`
JWTExtraAudiences []string `hcl:"jwt_extra_audiences"`
Expand All @@ -76,8 +89,8 @@ func ParseConfig(file string) (*Config, error) {
}

// ParseConfigFlagOverrides handles command line arguments that override config file settings
func (c *Config) ParseConfigFlagOverrides(daemonModeFlag bool, daemonModeFlagName string) {
if isFlagPassed(daemonModeFlagName) {
func (c *Config) ParseConfigFlagOverrides(daemonModeFlag bool) {
keeganwitt marked this conversation as resolved.
Show resolved Hide resolved
if isFlagPassed(DaemonModeFlagName) {
// If daemon mode is set by CLI this takes precedence
c.DaemonMode = &daemonModeFlag
} else if c.DaemonMode == nil {
Expand Down Expand Up @@ -158,9 +171,32 @@ func (c *Config) ValidateConfig(log logrus.FieldLogger) error {
c.JWTSVIDFileMode = defaultJWTSVIDFileMode
}

if c.HealthCheck == nil || c.HealthCheck.EnableHealthCheck == nil {
defaultEnableHealthCheck := false
c.HealthCheck.EnableHealthCheck = &defaultEnableHealthCheck
}
if c.HealthCheck.HealthCheckPort < 0 {
return errors.New("health check port must be positive")
} else if c.HealthCheck.HealthCheckPort == 0 {
c.HealthCheck.HealthCheckPort = defaultHealthCheckPort
}
if c.HealthCheck.HealthCheckPath == "" {
c.HealthCheck.HealthCheckPath = defaultHealthCheckPath
}

return nil
}

func ParseConfigFile(log logrus.FieldLogger, configFile string, daemonModeFlag bool) (*Config, error) {
keeganwitt marked this conversation as resolved.
Show resolved Hide resolved
log.Infof("Using configuration file: %q", configFile)
keeganwitt marked this conversation as resolved.
Show resolved Hide resolved
hclConfig, err := ParseConfig(configFile)
if err != nil {
return nil, fmt.Errorf("failed to parse %q: %w", configFile, err)
}
hclConfig.ParseConfigFlagOverrides(daemonModeFlag)
return hclConfig, nil
}

// checkForUnknownConfig looks for any unknown configuration keys and returns an error if one is found
func (c *Config) checkForUnknownConfig() error {
if len(c.UnusedKeyPositions) != 0 {
Expand Down
10 changes: 3 additions & 7 deletions cmd/spiffe-helper/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (
"github.com/stretchr/testify/require"
)

const (
daemonModeFlagName = "daemon-mode"
)

func TestParseConfig(t *testing.T) {
c, err := ParseConfig("testdata/helper.conf")

Expand Down Expand Up @@ -308,13 +304,13 @@ func TestDaemonModeFlag(t *testing.T) {
SVIDBundleFileName: "bundle.pem",
}

daemonModeFlag := flag.Bool(daemonModeFlagName, true, "Toggle running as a daemon to rotate X.509/JWT or just fetch and exit")
daemonModeFlag := flag.Bool(DaemonModeFlagName, true, "Toggle running as a daemon to rotate X.509/JWT or just fetch and exit")
flag.Parse()

err := flag.Set(daemonModeFlagName, "false")
err := flag.Set(DaemonModeFlagName, "false")
require.NoError(t, err)

config.ParseConfigFlagOverrides(*daemonModeFlag, daemonModeFlagName)
config.ParseConfigFlagOverrides(*daemonModeFlag)
require.NotNil(t, config.DaemonMode)
assert.Equal(t, false, *config.DaemonMode)
}
Loading
Loading