2022-06-01 23:05:05 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-12-23 16:21:49 +00:00
|
|
|
"io"
|
2022-06-01 23:05:05 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
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
|
|
|
"os"
|
2022-06-01 23:05:05 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/fleetdm/fleet/v4/pkg/fleethttp"
|
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/fleet"
|
2022-06-01 23:05:05 +00:00
|
|
|
)
|
|
|
|
|
2022-10-28 17:27:21 +00:00
|
|
|
var errInvalidScheme = errors.New("address must start with https:// for remote connections")
|
|
|
|
|
2022-06-01 23:05:05 +00:00
|
|
|
// httpClient interface allows the HTTP methods to be mocked.
|
|
|
|
type httpClient interface {
|
|
|
|
Do(req *http.Request) (*http.Response, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseClient struct {
|
|
|
|
baseURL *url.URL
|
|
|
|
http httpClient
|
|
|
|
urlPrefix string
|
|
|
|
insecureSkipVerify bool
|
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
|
|
|
// serverCapabilities is a map of capabilities that the server supports.
|
|
|
|
// This map is updated on each response we receive from the server.
|
|
|
|
serverCapabilities fleet.CapabilityMap
|
|
|
|
// clientCapabilities is a map of capabilities that the client supports.
|
|
|
|
// This list is given when the client is instantiated and shouldn't be
|
|
|
|
// modified afterwards.
|
|
|
|
clientCapabilities fleet.CapabilityMap
|
2022-06-01 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *baseClient) parseResponse(verb, path string, response *http.Response, responseDest interface{}) error {
|
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return notFoundErr{}
|
|
|
|
case http.StatusUnauthorized:
|
|
|
|
return ErrUnauthenticated
|
|
|
|
case http.StatusPaymentRequired:
|
|
|
|
return ErrMissingLicense
|
|
|
|
default:
|
2023-02-15 18:01:44 +00:00
|
|
|
if response.StatusCode >= 200 && response.StatusCode < 300 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2023-04-03 18:25:49 +00:00
|
|
|
e := &statusCodeErr{
|
|
|
|
code: response.StatusCode,
|
|
|
|
body: extractServerErrorText(response.Body),
|
|
|
|
}
|
|
|
|
return fmt.Errorf("%s %s received status %w", verb, path, e)
|
2022-06-01 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
2022-09-26 14:44:09 +00:00
|
|
|
bc.setServerCapabilities(response)
|
|
|
|
|
|
|
|
if responseDest != nil {
|
2022-12-23 16:21:49 +00:00
|
|
|
b, err := io.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading response body: %w", err)
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(b, &responseDest); err != nil {
|
|
|
|
return fmt.Errorf("decode %s %s response: %w, body: %s", verb, path, err, b)
|
2022-09-26 14:44:09 +00:00
|
|
|
}
|
|
|
|
if e, ok := responseDest.(errorer); ok {
|
|
|
|
if e.error() != nil {
|
|
|
|
return fmt.Errorf("%s %s error: %w", verb, path, e.error())
|
|
|
|
}
|
2022-06-01 23:05:05 +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
|
|
|
bc.setServerCapabilities(response)
|
|
|
|
|
2022-06-01 23:05:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bc *baseClient) url(path, rawQuery string) *url.URL {
|
|
|
|
u := *bc.baseURL
|
|
|
|
u.Path = bc.urlPrefix + path
|
|
|
|
u.RawQuery = rawQuery
|
|
|
|
return &u
|
|
|
|
}
|
|
|
|
|
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
|
|
|
// setServerCapabilities updates the server capabilities based on the response
|
|
|
|
// from the server.
|
|
|
|
func (bc *baseClient) setServerCapabilities(response *http.Response) {
|
|
|
|
capabilities := response.Header.Get(fleet.CapabilitiesHeader)
|
|
|
|
bc.serverCapabilities.PopulateFromString(capabilities)
|
|
|
|
}
|
|
|
|
|
2022-09-26 14:44:09 +00:00
|
|
|
func (bc *baseClient) GetServerCapabilities() fleet.CapabilityMap {
|
|
|
|
return bc.serverCapabilities
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// setClientCapabilities header is used to set a header with the client
|
|
|
|
// capabilities in the given request.
|
|
|
|
//
|
|
|
|
// This method is defined in baseClient because other clients generally have
|
|
|
|
// custom implementations of a method to perform the requests to the server.
|
|
|
|
func (bc *baseClient) setClientCapabilitiesHeader(req *http.Request) {
|
|
|
|
if len(bc.clientCapabilities) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Header == nil {
|
|
|
|
req.Header = http.Header{}
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set(fleet.CapabilitiesHeader, bc.clientCapabilities.String())
|
|
|
|
}
|
|
|
|
|
2023-04-27 11:44:39 +00:00
|
|
|
func newBaseClient(
|
|
|
|
addr string,
|
|
|
|
insecureSkipVerify bool,
|
|
|
|
rootCA, urlPrefix string,
|
|
|
|
fleetClientCert *tls.Certificate,
|
|
|
|
capabilities fleet.CapabilityMap,
|
|
|
|
) (*baseClient, error) {
|
2022-06-01 23:05:05 +00:00
|
|
|
baseURL, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("parsing URL: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-28 17:27:21 +00:00
|
|
|
allowHTTP := insecureSkipVerify || strings.Contains(baseURL.Host, "localhost") || strings.Contains(baseURL.Host, "127.0.0.1")
|
|
|
|
if baseURL.Scheme != "https" && !allowHTTP {
|
|
|
|
return nil, errInvalidScheme
|
2022-06-01 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rootCAPool := x509.NewCertPool()
|
2022-06-21 19:25:36 +00:00
|
|
|
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
// Osquery itself requires >= TLS 1.2.
|
|
|
|
// https://github.com/osquery/osquery/blob/9713ad9e28f1cfe6c16a823fb88bd531e39e192d/osquery/remote/transports/tls.cpp#L97-L98
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
|
2023-04-27 11:44:39 +00:00
|
|
|
if fleetClientCert != nil {
|
|
|
|
tlsConfig.Certificates = []tls.Certificate{*fleetClientCert}
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:25:36 +00:00
|
|
|
switch {
|
|
|
|
case rootCA != "":
|
2022-06-01 23:05:05 +00:00
|
|
|
// read in the root cert file specified in the context
|
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
|
|
|
certs, err := os.ReadFile(rootCA)
|
2022-06-01 23:05:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading root CA: %w", err)
|
|
|
|
}
|
|
|
|
// add certs to pool
|
|
|
|
if ok := rootCAPool.AppendCertsFromPEM(certs); !ok {
|
|
|
|
return nil, errors.New("failed to add certificates to root CA pool")
|
|
|
|
}
|
2022-06-21 19:25:36 +00:00
|
|
|
tlsConfig.RootCAs = rootCAPool
|
|
|
|
case insecureSkipVerify:
|
|
|
|
// Ignoring "G402: TLS InsecureSkipVerify set true", needed for development/testing.
|
|
|
|
tlsConfig.InsecureSkipVerify = true //nolint:gosec
|
|
|
|
default:
|
2022-06-01 23:05:05 +00:00
|
|
|
// Use only the system certs (doesn't work on Windows)
|
|
|
|
rootCAPool, err = x509.SystemCertPool()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("loading system cert pool: %w", err)
|
|
|
|
}
|
2022-06-21 19:25:36 +00:00
|
|
|
tlsConfig.RootCAs = rootCAPool
|
2022-06-01 23:05:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 19:25:36 +00:00
|
|
|
httpClient := fleethttp.NewClient(fleethttp.WithTLSClientConfig(tlsConfig))
|
2022-06-01 23:05:05 +00:00
|
|
|
client := &baseClient{
|
|
|
|
baseURL: baseURL,
|
|
|
|
http: httpClient,
|
|
|
|
insecureSkipVerify: insecureSkipVerify,
|
|
|
|
urlPrefix: urlPrefix,
|
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
|
|
|
clientCapabilities: capabilities,
|
2022-09-26 14:44:09 +00:00
|
|
|
serverCapabilities: fleet.CapabilityMap{},
|
2022-06-01 23:05:05 +00:00
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|