2021-07-16 18:28:13 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-11-01 14:13:12 +00:00
|
|
|
"crypto/x509"
|
2021-07-16 18:28:13 +00:00
|
|
|
"encoding/json"
|
2025-09-04 15:58:47 +00:00
|
|
|
"fmt"
|
2021-08-03 19:56:54 +00:00
|
|
|
"io"
|
2021-07-16 18:28:13 +00:00
|
|
|
"net/http"
|
2023-07-19 16:30:24 +00:00
|
|
|
"net/url"
|
2021-07-16 18:28:13 +00:00
|
|
|
"reflect"
|
|
|
|
|
|
add headers denoting capabilities between fleet server / desktop / orbit (#7833)
This adds a new mechanism to allow us to handle compatibility issues between Orbit, Fleet Server and Fleet Desktop.
The general idea is to _always_ send a custom header of the form:
```
fleet-capabilities-header = "X-Fleet-Capabilities:" capabilities
capabilities = capability * (,)
capability = string
```
Both from the server to the clients (Orbit, Fleet Desktop) and vice-versa. For an example, see: https://github.com/fleetdm/fleet/commit/8c0bbdd291f54e03e19766bcdfead0fb8067f60c
Also, the following applies:
- Backwards compat: if the header is not present, assume that orbit/fleet doesn't have the capability
- The current capabilities endpoint will be removed
### Motivation
This solution is trying to solve the following problems:
- We have three independent processes communicating with each other (Fleet Desktop, Orbit and Fleet Server). Each process can be updated independently, and therefore we need a way for each process to know what features are supported by its peers.
- We originally implemented a dedicated API endpoint in the server that returned a list of the capabilities (or "features") enabled, we found this, and any other server-only solution (like API versioning) to be insufficient because:
- There are cases in which the server also needs to know which features are supported by its clients
- Clients needed to poll for changes to detect if the capabilities supported by the server change, by sending the capabilities on each request we have a much cleaner way to handling different responses.
- We are also introducing an unauthenticated endpoint to get the server features, this gives us flexibility if we need to implement different authentication mechanisms, and was one of the pitfalls of the first implementation.
Related to https://github.com/fleetdm/fleet/issues/7929
2022-09-26 10:53:53 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/capabilities"
|
2021-07-16 18:28:13 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2025-02-03 17:23:26 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/service/middleware/auth"
|
2025-02-18 17:09:43 +00:00
|
|
|
eu "github.com/fleetdm/fleet/v4/server/service/middleware/endpoint_utils"
|
2022-03-07 18:10:55 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
2021-08-03 19:56:54 +00:00
|
|
|
kithttp "github.com/go-kit/kit/transport/http"
|
2024-06-17 13:27:31 +00:00
|
|
|
"github.com/go-kit/log"
|
2021-08-25 13:08:14 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-07-16 18:28:13 +00:00
|
|
|
)
|
|
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func makeDecoder(iface interface{}) kithttp.DecodeRequestFunc {
|
|
|
|
|
return eu.MakeDecoder(iface, jsonDecode, parseCustomTags, isBodyDecoder, decodeBody)
|
2022-03-08 16:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
// A value that implements bodyDecoder takes control of decoding the request body.
|
2022-10-24 12:49:44 +00:00
|
|
|
type bodyDecoder interface {
|
2023-11-01 14:13:12 +00:00
|
|
|
DecodeBody(ctx context.Context, r io.Reader, u url.Values, c []*x509.Certificate) error
|
2022-10-24 12:49:44 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func decodeBody(ctx context.Context, r *http.Request, v reflect.Value, body io.Reader) error {
|
|
|
|
|
bd := v.Interface().(bodyDecoder)
|
|
|
|
|
var certs []*x509.Certificate
|
|
|
|
|
if (r.TLS != nil) && (r.TLS.PeerCertificates != nil) {
|
|
|
|
|
certs = r.TLS.PeerCertificates
|
2022-03-08 16:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
if err := bd.DecodeBody(ctx, body, r.URL.Query(), certs); err != nil {
|
|
|
|
|
return err
|
2021-08-03 19:56:54 +00:00
|
|
|
}
|
2025-02-18 17:09:43 +00:00
|
|
|
return nil
|
|
|
|
|
}
|
2021-08-03 19:56:54 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func parseCustomTags(urlTagValue string, r *http.Request, field reflect.Value) (bool, error) {
|
|
|
|
|
switch urlTagValue {
|
|
|
|
|
case "list_options":
|
|
|
|
|
opts, err := listOptionsFromRequest(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
2022-10-24 12:49:44 +00:00
|
|
|
}
|
2025-02-18 17:09:43 +00:00
|
|
|
field.Set(reflect.ValueOf(opts))
|
|
|
|
|
return true, nil
|
2022-10-24 12:49:44 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
case "user_options":
|
|
|
|
|
opts, err := userListOptionsFromRequest(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
2021-08-03 13:33:27 +00:00
|
|
|
}
|
2025-02-18 17:09:43 +00:00
|
|
|
field.Set(reflect.ValueOf(opts))
|
|
|
|
|
return true, nil
|
2021-08-03 13:33:27 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
case "host_options":
|
|
|
|
|
opts, err := hostListOptionsFromRequest(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
field.Set(reflect.ValueOf(opts))
|
|
|
|
|
return true, nil
|
2022-01-10 19:43:39 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
case "carve_options":
|
|
|
|
|
opts, err := carveListOptionsFromRequest(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false, err
|
|
|
|
|
}
|
|
|
|
|
field.Set(reflect.ValueOf(opts))
|
|
|
|
|
return true, nil
|
|
|
|
|
}
|
|
|
|
|
return false, nil
|
|
|
|
|
}
|
2021-12-14 21:34:11 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func jsonDecode(body io.Reader, req any) error {
|
|
|
|
|
return json.NewDecoder(body).Decode(req)
|
|
|
|
|
}
|
2021-12-14 21:34:11 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func isBodyDecoder(v reflect.Value) bool {
|
|
|
|
|
_, ok := v.Interface().(bodyDecoder)
|
|
|
|
|
return ok
|
|
|
|
|
}
|
2021-08-03 13:33:27 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
// Compile-time check to ensure that endpointer implements Endpointer.
|
|
|
|
|
var _ eu.Endpointer[eu.HandlerFunc] = &endpointer{}
|
2021-09-14 13:58:48 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
type endpointer struct {
|
|
|
|
|
svc fleet.Service
|
|
|
|
|
}
|
2021-08-03 13:33:27 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func (e *endpointer) CallHandlerFunc(f eu.HandlerFunc, ctx context.Context, request interface{},
|
2025-09-04 15:58:47 +00:00
|
|
|
svc interface{},
|
|
|
|
|
) (fleet.Errorer, error) {
|
2025-02-18 17:09:43 +00:00
|
|
|
return f(ctx, request, svc.(fleet.Service))
|
|
|
|
|
}
|
2023-11-01 14:13:12 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func (e *endpointer) Service() interface{} {
|
|
|
|
|
return e.svc
|
|
|
|
|
}
|
2022-10-24 12:49:44 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func newUserAuthenticatedEndpointer(svc fleet.Service, opts []kithttp.ServerOption, r *mux.Router,
|
2025-09-04 15:58:47 +00:00
|
|
|
versions ...string,
|
|
|
|
|
) *eu.CommonEndpointer[eu.HandlerFunc] {
|
2025-02-18 17:09:43 +00:00
|
|
|
return &eu.CommonEndpointer[eu.HandlerFunc]{
|
|
|
|
|
EP: &endpointer{
|
|
|
|
|
svc: svc,
|
|
|
|
|
},
|
|
|
|
|
MakeDecoderFn: makeDecoder,
|
|
|
|
|
EncodeFn: encodeResponse,
|
|
|
|
|
Opts: opts,
|
|
|
|
|
AuthFunc: auth.AuthenticatedUser,
|
|
|
|
|
FleetService: svc,
|
|
|
|
|
Router: r,
|
|
|
|
|
Versions: versions,
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-09 18:23:08 +00:00
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func newNoAuthEndpointer(svc fleet.Service, opts []kithttp.ServerOption, r *mux.Router,
|
2025-09-04 15:58:47 +00:00
|
|
|
versions ...string,
|
|
|
|
|
) *eu.CommonEndpointer[eu.HandlerFunc] {
|
2025-02-18 17:09:43 +00:00
|
|
|
return &eu.CommonEndpointer[eu.HandlerFunc]{
|
|
|
|
|
EP: &endpointer{
|
|
|
|
|
svc: svc,
|
|
|
|
|
},
|
|
|
|
|
MakeDecoderFn: makeDecoder,
|
|
|
|
|
EncodeFn: encodeResponse,
|
|
|
|
|
Opts: opts,
|
|
|
|
|
AuthFunc: auth.UnauthenticatedRequest,
|
|
|
|
|
FleetService: svc,
|
|
|
|
|
Router: r,
|
|
|
|
|
Versions: versions,
|
2021-08-03 19:56:54 +00:00
|
|
|
}
|
2021-08-03 13:33:27 +00:00
|
|
|
}
|
|
|
|
|
|
2022-10-18 12:43:16 +00:00
|
|
|
func badRequest(msg string) error {
|
|
|
|
|
return &fleet.BadRequestError{Message: msg}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-04 15:58:47 +00:00
|
|
|
func badRequestf(format string, a ...any) error {
|
|
|
|
|
return &fleet.BadRequestError{
|
|
|
|
|
Message: fmt.Sprintf(format, a...),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func newDeviceAuthenticatedEndpointer(svc fleet.Service, logger log.Logger, opts []kithttp.ServerOption, r *mux.Router,
|
2025-09-04 15:58:47 +00:00
|
|
|
versions ...string,
|
|
|
|
|
) *eu.CommonEndpointer[eu.HandlerFunc] {
|
2022-03-09 21:13:56 +00:00
|
|
|
authFunc := func(svc fleet.Service, next endpoint.Endpoint) endpoint.Endpoint {
|
|
|
|
|
return authenticatedDevice(svc, logger, next)
|
|
|
|
|
}
|
add headers denoting capabilities between fleet server / desktop / orbit (#7833)
This adds a new mechanism to allow us to handle compatibility issues between Orbit, Fleet Server and Fleet Desktop.
The general idea is to _always_ send a custom header of the form:
```
fleet-capabilities-header = "X-Fleet-Capabilities:" capabilities
capabilities = capability * (,)
capability = string
```
Both from the server to the clients (Orbit, Fleet Desktop) and vice-versa. For an example, see: https://github.com/fleetdm/fleet/commit/8c0bbdd291f54e03e19766bcdfead0fb8067f60c
Also, the following applies:
- Backwards compat: if the header is not present, assume that orbit/fleet doesn't have the capability
- The current capabilities endpoint will be removed
### Motivation
This solution is trying to solve the following problems:
- We have three independent processes communicating with each other (Fleet Desktop, Orbit and Fleet Server). Each process can be updated independently, and therefore we need a way for each process to know what features are supported by its peers.
- We originally implemented a dedicated API endpoint in the server that returned a list of the capabilities (or "features") enabled, we found this, and any other server-only solution (like API versioning) to be insufficient because:
- There are cases in which the server also needs to know which features are supported by its clients
- Clients needed to poll for changes to detect if the capabilities supported by the server change, by sending the capabilities on each request we have a much cleaner way to handling different responses.
- We are also introducing an unauthenticated endpoint to get the server features, this gives us flexibility if we need to implement different authentication mechanisms, and was one of the pitfalls of the first implementation.
Related to https://github.com/fleetdm/fleet/issues/7929
2022-09-26 10:53:53 +00:00
|
|
|
|
|
|
|
|
// Inject the fleet.CapabilitiesHeader header to the response for device endpoints
|
2023-08-24 16:04:27 +00:00
|
|
|
opts = append(opts, capabilitiesResponseFunc(fleet.GetServerDeviceCapabilities()))
|
add headers denoting capabilities between fleet server / desktop / orbit (#7833)
This adds a new mechanism to allow us to handle compatibility issues between Orbit, Fleet Server and Fleet Desktop.
The general idea is to _always_ send a custom header of the form:
```
fleet-capabilities-header = "X-Fleet-Capabilities:" capabilities
capabilities = capability * (,)
capability = string
```
Both from the server to the clients (Orbit, Fleet Desktop) and vice-versa. For an example, see: https://github.com/fleetdm/fleet/commit/8c0bbdd291f54e03e19766bcdfead0fb8067f60c
Also, the following applies:
- Backwards compat: if the header is not present, assume that orbit/fleet doesn't have the capability
- The current capabilities endpoint will be removed
### Motivation
This solution is trying to solve the following problems:
- We have three independent processes communicating with each other (Fleet Desktop, Orbit and Fleet Server). Each process can be updated independently, and therefore we need a way for each process to know what features are supported by its peers.
- We originally implemented a dedicated API endpoint in the server that returned a list of the capabilities (or "features") enabled, we found this, and any other server-only solution (like API versioning) to be insufficient because:
- There are cases in which the server also needs to know which features are supported by its clients
- Clients needed to poll for changes to detect if the capabilities supported by the server change, by sending the capabilities on each request we have a much cleaner way to handling different responses.
- We are also introducing an unauthenticated endpoint to get the server features, this gives us flexibility if we need to implement different authentication mechanisms, and was one of the pitfalls of the first implementation.
Related to https://github.com/fleetdm/fleet/issues/7929
2022-09-26 10:53:53 +00:00
|
|
|
// Add the capabilities reported by the device to the request context
|
|
|
|
|
opts = append(opts, capabilitiesContextFunc())
|
|
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
return &eu.CommonEndpointer[eu.HandlerFunc]{
|
|
|
|
|
EP: &endpointer{
|
|
|
|
|
svc: svc,
|
|
|
|
|
},
|
|
|
|
|
MakeDecoderFn: makeDecoder,
|
|
|
|
|
EncodeFn: encodeResponse,
|
|
|
|
|
Opts: opts,
|
|
|
|
|
AuthFunc: authFunc,
|
|
|
|
|
FleetService: svc,
|
|
|
|
|
Router: r,
|
|
|
|
|
Versions: versions,
|
2022-03-09 21:13:56 +00:00
|
|
|
}
|
2022-03-07 18:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func newHostAuthenticatedEndpointer(svc fleet.Service, logger log.Logger, opts []kithttp.ServerOption, r *mux.Router,
|
2025-09-04 15:58:47 +00:00
|
|
|
versions ...string,
|
|
|
|
|
) *eu.CommonEndpointer[eu.HandlerFunc] {
|
2022-03-07 18:10:55 +00:00
|
|
|
authFunc := func(svc fleet.Service, next endpoint.Endpoint) endpoint.Endpoint {
|
|
|
|
|
return authenticatedHost(svc, logger, next)
|
|
|
|
|
}
|
2025-02-18 17:09:43 +00:00
|
|
|
return &eu.CommonEndpointer[eu.HandlerFunc]{
|
|
|
|
|
EP: &endpointer{
|
|
|
|
|
svc: svc,
|
|
|
|
|
},
|
|
|
|
|
MakeDecoderFn: makeDecoder,
|
|
|
|
|
EncodeFn: encodeResponse,
|
|
|
|
|
Opts: opts,
|
|
|
|
|
AuthFunc: authFunc,
|
|
|
|
|
FleetService: svc,
|
|
|
|
|
Router: r,
|
|
|
|
|
Versions: versions,
|
2022-03-07 18:10:55 +00:00
|
|
|
}
|
2021-07-16 18:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
func newOrbitAuthenticatedEndpointer(svc fleet.Service, logger log.Logger, opts []kithttp.ServerOption, r *mux.Router,
|
2025-09-04 15:58:47 +00:00
|
|
|
versions ...string,
|
|
|
|
|
) *eu.CommonEndpointer[eu.HandlerFunc] {
|
2022-09-23 19:00:23 +00:00
|
|
|
authFunc := func(svc fleet.Service, next endpoint.Endpoint) endpoint.Endpoint {
|
|
|
|
|
return authenticatedOrbitHost(svc, logger, next)
|
|
|
|
|
}
|
add headers denoting capabilities between fleet server / desktop / orbit (#7833)
This adds a new mechanism to allow us to handle compatibility issues between Orbit, Fleet Server and Fleet Desktop.
The general idea is to _always_ send a custom header of the form:
```
fleet-capabilities-header = "X-Fleet-Capabilities:" capabilities
capabilities = capability * (,)
capability = string
```
Both from the server to the clients (Orbit, Fleet Desktop) and vice-versa. For an example, see: https://github.com/fleetdm/fleet/commit/8c0bbdd291f54e03e19766bcdfead0fb8067f60c
Also, the following applies:
- Backwards compat: if the header is not present, assume that orbit/fleet doesn't have the capability
- The current capabilities endpoint will be removed
### Motivation
This solution is trying to solve the following problems:
- We have three independent processes communicating with each other (Fleet Desktop, Orbit and Fleet Server). Each process can be updated independently, and therefore we need a way for each process to know what features are supported by its peers.
- We originally implemented a dedicated API endpoint in the server that returned a list of the capabilities (or "features") enabled, we found this, and any other server-only solution (like API versioning) to be insufficient because:
- There are cases in which the server also needs to know which features are supported by its clients
- Clients needed to poll for changes to detect if the capabilities supported by the server change, by sending the capabilities on each request we have a much cleaner way to handling different responses.
- We are also introducing an unauthenticated endpoint to get the server features, this gives us flexibility if we need to implement different authentication mechanisms, and was one of the pitfalls of the first implementation.
Related to https://github.com/fleetdm/fleet/issues/7929
2022-09-26 10:53:53 +00:00
|
|
|
|
|
|
|
|
// Inject the fleet.Capabilities header to the response for Orbit hosts
|
2023-08-24 16:04:27 +00:00
|
|
|
opts = append(opts, capabilitiesResponseFunc(fleet.GetServerOrbitCapabilities()))
|
add headers denoting capabilities between fleet server / desktop / orbit (#7833)
This adds a new mechanism to allow us to handle compatibility issues between Orbit, Fleet Server and Fleet Desktop.
The general idea is to _always_ send a custom header of the form:
```
fleet-capabilities-header = "X-Fleet-Capabilities:" capabilities
capabilities = capability * (,)
capability = string
```
Both from the server to the clients (Orbit, Fleet Desktop) and vice-versa. For an example, see: https://github.com/fleetdm/fleet/commit/8c0bbdd291f54e03e19766bcdfead0fb8067f60c
Also, the following applies:
- Backwards compat: if the header is not present, assume that orbit/fleet doesn't have the capability
- The current capabilities endpoint will be removed
### Motivation
This solution is trying to solve the following problems:
- We have three independent processes communicating with each other (Fleet Desktop, Orbit and Fleet Server). Each process can be updated independently, and therefore we need a way for each process to know what features are supported by its peers.
- We originally implemented a dedicated API endpoint in the server that returned a list of the capabilities (or "features") enabled, we found this, and any other server-only solution (like API versioning) to be insufficient because:
- There are cases in which the server also needs to know which features are supported by its clients
- Clients needed to poll for changes to detect if the capabilities supported by the server change, by sending the capabilities on each request we have a much cleaner way to handling different responses.
- We are also introducing an unauthenticated endpoint to get the server features, this gives us flexibility if we need to implement different authentication mechanisms, and was one of the pitfalls of the first implementation.
Related to https://github.com/fleetdm/fleet/issues/7929
2022-09-26 10:53:53 +00:00
|
|
|
// Add the capabilities reported by Orbit to the request context
|
|
|
|
|
opts = append(opts, capabilitiesContextFunc())
|
|
|
|
|
|
2025-02-18 17:09:43 +00:00
|
|
|
return &eu.CommonEndpointer[eu.HandlerFunc]{
|
|
|
|
|
EP: &endpointer{
|
|
|
|
|
svc: svc,
|
|
|
|
|
},
|
|
|
|
|
MakeDecoderFn: makeDecoder,
|
|
|
|
|
EncodeFn: encodeResponse,
|
|
|
|
|
Opts: opts,
|
|
|
|
|
AuthFunc: authFunc,
|
|
|
|
|
FleetService: svc,
|
|
|
|
|
Router: r,
|
|
|
|
|
Versions: versions,
|
2022-03-08 16:27:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
add headers denoting capabilities between fleet server / desktop / orbit (#7833)
This adds a new mechanism to allow us to handle compatibility issues between Orbit, Fleet Server and Fleet Desktop.
The general idea is to _always_ send a custom header of the form:
```
fleet-capabilities-header = "X-Fleet-Capabilities:" capabilities
capabilities = capability * (,)
capability = string
```
Both from the server to the clients (Orbit, Fleet Desktop) and vice-versa. For an example, see: https://github.com/fleetdm/fleet/commit/8c0bbdd291f54e03e19766bcdfead0fb8067f60c
Also, the following applies:
- Backwards compat: if the header is not present, assume that orbit/fleet doesn't have the capability
- The current capabilities endpoint will be removed
### Motivation
This solution is trying to solve the following problems:
- We have three independent processes communicating with each other (Fleet Desktop, Orbit and Fleet Server). Each process can be updated independently, and therefore we need a way for each process to know what features are supported by its peers.
- We originally implemented a dedicated API endpoint in the server that returned a list of the capabilities (or "features") enabled, we found this, and any other server-only solution (like API versioning) to be insufficient because:
- There are cases in which the server also needs to know which features are supported by its clients
- Clients needed to poll for changes to detect if the capabilities supported by the server change, by sending the capabilities on each request we have a much cleaner way to handling different responses.
- We are also introducing an unauthenticated endpoint to get the server features, this gives us flexibility if we need to implement different authentication mechanisms, and was one of the pitfalls of the first implementation.
Related to https://github.com/fleetdm/fleet/issues/7929
2022-09-26 10:53:53 +00:00
|
|
|
func capabilitiesResponseFunc(capabilities fleet.CapabilityMap) kithttp.ServerOption {
|
|
|
|
|
return kithttp.ServerAfter(func(ctx context.Context, w http.ResponseWriter) context.Context {
|
|
|
|
|
writeCapabilitiesHeader(w, capabilities)
|
|
|
|
|
return ctx
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func capabilitiesContextFunc() kithttp.ServerOption {
|
2024-10-18 17:38:26 +00:00
|
|
|
return kithttp.ServerBefore(capabilities.NewContext)
|
add headers denoting capabilities between fleet server / desktop / orbit (#7833)
This adds a new mechanism to allow us to handle compatibility issues between Orbit, Fleet Server and Fleet Desktop.
The general idea is to _always_ send a custom header of the form:
```
fleet-capabilities-header = "X-Fleet-Capabilities:" capabilities
capabilities = capability * (,)
capability = string
```
Both from the server to the clients (Orbit, Fleet Desktop) and vice-versa. For an example, see: https://github.com/fleetdm/fleet/commit/8c0bbdd291f54e03e19766bcdfead0fb8067f60c
Also, the following applies:
- Backwards compat: if the header is not present, assume that orbit/fleet doesn't have the capability
- The current capabilities endpoint will be removed
### Motivation
This solution is trying to solve the following problems:
- We have three independent processes communicating with each other (Fleet Desktop, Orbit and Fleet Server). Each process can be updated independently, and therefore we need a way for each process to know what features are supported by its peers.
- We originally implemented a dedicated API endpoint in the server that returned a list of the capabilities (or "features") enabled, we found this, and any other server-only solution (like API versioning) to be insufficient because:
- There are cases in which the server also needs to know which features are supported by its clients
- Clients needed to poll for changes to detect if the capabilities supported by the server change, by sending the capabilities on each request we have a much cleaner way to handling different responses.
- We are also introducing an unauthenticated endpoint to get the server features, this gives us flexibility if we need to implement different authentication mechanisms, and was one of the pitfalls of the first implementation.
Related to https://github.com/fleetdm/fleet/issues/7929
2022-09-26 10:53:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func writeCapabilitiesHeader(w http.ResponseWriter, capabilities fleet.CapabilityMap) {
|
|
|
|
|
if len(capabilities) == 0 {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set(fleet.CapabilitiesHeader, capabilities.String())
|
|
|
|
|
}
|