-
Notifications
You must be signed in to change notification settings - Fork 9
fleetnode: harden enrollment transport + stage install layout (stack 1/3) #322
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,54 @@ | ||
| # fleetnode | ||
|
|
||
| `fleetnode` is the on-prem agent that enrolls with a fleet server, holds a session, and (in later stack PRs) opens a `ControlStream` to execute server-issued discovery commands against the operator's local network. | ||
|
|
||
| ## Subcommands | ||
|
|
||
| | Command | Purpose | | ||
| |---------|---------| | ||
| | `fleetnode enroll` | Register with a fleet server using a one-time enrollment code. Persists keys and `api_key`. See [enroll.go](enroll.go). | | ||
| | `fleetnode status` | Print local state (server URL, fleet_node_id, fingerprint, session expiry). See [status.go](status.go). | | ||
| | `fleetnode refresh` | Renew the session token using the stored `api_key`. See [refresh.go](refresh.go). | | ||
|
ankitgoswami marked this conversation as resolved.
|
||
| | `fleetnode run` | Long-running daemon: maintain session, post heartbeats, and (in the discovery PR) consume the control stream. See [run.go](run.go). | | ||
|
|
||
| ## State directory and lock | ||
|
|
||
| State lives in `state.yaml` under one of, in order: | ||
|
|
||
| 1. `--state-dir <path>` (override; primarily for tests) | ||
| 2. `$XDG_STATE_HOME/fleetnode` | ||
| 3. `~/.local/state/fleetnode` | ||
|
|
||
| The file holds `server_url`, `fleet_node_id`, `identity_fingerprint`, both keypairs, the `api_key`, and the current `session_token`. It is created `0600` under a `0700` directory. See [state.go](../../internal/fleetnodebootstrap/state.go). | ||
|
|
||
| A `state.lock` file in the same directory serializes commands. Only one process may hold it at a time (`LOCK_EX | LOCK_NB`). The PID of the holder is written under the lock; if another invocation hits contention, the error includes that PID. To clear a stale lock, identify the owner with `ps -p <pid>`; remove the lock file only if the process is gone. | ||
|
|
||
| ## Build | ||
|
|
||
| ```bash | ||
| just build-fleetnode # produces server/.fleetnode/{fleetnode, nmap, plugins/} | ||
| go build -o fleetnode ./server/cmd/fleetnode # fast iteration | ||
| ``` | ||
|
|
||
| The staged layout reserves a `plugins/` subdirectory and a `nmap` symlink for the discovery PR. The agent does not yet exec anything from those locations. | ||
|
|
||
| ## Enrollment flow | ||
|
|
||
| 1. Operator mints an enrollment code in the UI and shares it. | ||
| 2. `fleetnode enroll --server-url=...` prompts for the code, registers, prints a fingerprint. | ||
| 3. Operator verifies the fingerprint in the UI and clicks confirm; the UI displays the `api_key`. | ||
| 4. Agent prompts for the `api_key`, completes the handshake, persists session. | ||
|
|
||
| If anything is interrupted between Register and Complete, `fleetnode refresh` resumes from the persisted state. | ||
|
|
||
| ## Security model | ||
|
|
||
| - **Transport.** `ValidateServerURL` requires `https://` for non-loopback servers; `--allow-insecure-transport` permits `http://` for testing only. The HTTP/2 transport is scheme-aware: `https://` goes through a TLS-validating `http2.Transport`, `http://` goes through h2c. See [client.go](../../internal/fleetnodebootstrap/client.go). | ||
| - **State file.** `state.yaml` is `0600` under a `0700` directory; the writer fsyncs the temp file, renames, then fsyncs the directory. Symlinks at the state dir leaf are refused. | ||
| - **Lock contention.** PID is written under the lock so contention reports are actionable. | ||
|
|
||
| ## Development | ||
|
|
||
| ```bash | ||
| go test ./server/internal/fleetnodebootstrap/... -race -count=1 | ||
| ``` | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,29 +1,66 @@ | ||
| package fleetnodebootstrap | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
| "net/http" | ||
| "net/url" | ||
| "time" | ||
|
|
||
| "golang.org/x/net/http2" | ||
|
|
||
| "github.com/block/proto-fleet/server/generated/grpc/fleetnodegateway/v1/fleetnodegatewayv1connect" | ||
| ) | ||
|
|
||
| const httpClientTimeout = 30 * time.Second | ||
|
|
||
| // Refusing every 30x stops a downgrade redirect from replaying the POST body | ||
| // (enrollment token, api_key, signature) to a plaintext target; Connect-RPC | ||
| // itself never expects redirects. | ||
| // Refusing 3xx stops a downgrade redirect from replaying the signed POST | ||
| // body (enrollment token, api_key, signature) to an attacker-chosen target. | ||
| var errRedirectNotAllowed = errors.New("redirects are not allowed for connect-rpc calls") | ||
|
|
||
| func newGatewayHTTPClient() *http.Client { | ||
| // A shared AllowHTTP+DialTLSContext shim would silently downgrade https to | ||
| // plaintext, defeating ValidateServerURL's https-required policy. The https | ||
| // branch uses net/http's Transport so ALPN can negotiate H2 when available | ||
| // and fall back to HTTP/1.1 — packaged nginx terminates TLS and proxies | ||
| // upstream over HTTP/1.1, so a bare http2.Transport refuses to talk to it. | ||
| func newGatewayHTTPClient(serverURL string) (*http.Client, error) { | ||
| u, err := url.Parse(serverURL) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("parse server-url: %w", err) | ||
| } | ||
| var tr http.RoundTripper | ||
| switch u.Scheme { | ||
| case "http": | ||
| tr = &http2.Transport{ | ||
|
ankitgoswami marked this conversation as resolved.
|
||
| AllowHTTP: true, | ||
| DialTLSContext: func(ctx context.Context, network, addr string, _ *tls.Config) (net.Conn, error) { | ||
| var d net.Dialer | ||
| return d.DialContext(ctx, network, addr) | ||
| }, | ||
| } | ||
| case "https": | ||
| tr = &http.Transport{ | ||
| Proxy: http.ProxyFromEnvironment, | ||
| TLSHandshakeTimeout: 10 * time.Second, | ||
| ExpectContinueTimeout: 1 * time.Second, | ||
| ForceAttemptHTTP2: true, | ||
| } | ||
| default: | ||
| return nil, fmt.Errorf("server-url scheme must be http or https; got %q", u.Scheme) | ||
| } | ||
| return &http.Client{ | ||
| Timeout: httpClientTimeout, | ||
| CheckRedirect: func(_ *http.Request, _ []*http.Request) error { | ||
| return errRedirectNotAllowed | ||
| }, | ||
| } | ||
| Transport: tr, | ||
| }, nil | ||
|
ankitgoswami marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func NewGatewayClient(serverURL string) fleetnodegatewayv1connect.FleetNodeGatewayServiceClient { | ||
| return fleetnodegatewayv1connect.NewFleetNodeGatewayServiceClient(newGatewayHTTPClient(), serverURL) | ||
| func NewGatewayClient(serverURL string) (fleetnodegatewayv1connect.FleetNodeGatewayServiceClient, error) { | ||
| httpClient, err := newGatewayHTTPClient(serverURL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return fleetnodegatewayv1connect.NewFleetNodeGatewayServiceClient(httpClient, serverURL), nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.