2022-12-29 11:24:08 -05:00
/ *
Copyright ( c ) Edgeless Systems GmbH
SPDX - License - Identifier : AGPL - 3.0 - only
* /
2023-01-19 09:57:50 -05:00
/ *
2023-06-02 03:19:23 -04:00
Package fetcher implements a client for the Constellation Resource API .
2023-01-19 09:57:50 -05:00
The fetcher is used to get information from the versions API without having to
authenticate with AWS , where the API is currently hosted . This package should be
used in user - facing application code most of the time , like the CLI .
2023-06-02 03:19:23 -04:00
Each sub - API included in the Constellation Resource API should define it ' s resources by implementing types that implement apiObject .
The new package can then call this package ' s Fetch function to get the resource from the API .
To modify resources , pkg internal / api / client should be used in a similar fashion .
2023-01-19 09:57:50 -05:00
* /
2022-12-29 11:24:08 -05:00
package fetcher
import (
"context"
"encoding/json"
"fmt"
2024-06-11 10:11:53 -04:00
"io"
2022-12-29 11:24:08 -05:00
"net/http"
2023-09-25 05:53:02 -04:00
"net/url"
2023-08-01 10:48:13 -04:00
"github.com/edgelesssys/constellation/v2/internal/sigstore"
2022-12-29 11:24:08 -05:00
)
2023-05-25 12:43:44 -04:00
// NewHTTPClient returns a new http client.
func NewHTTPClient ( ) HTTPClient {
2023-11-23 08:42:13 -05:00
return & http . Client { Transport : & http . Transport {
DisableKeepAlives : true , // DisableKeepAlives fixes concurrency issue see https://stackoverflow.com/a/75816347
Proxy : http . ProxyFromEnvironment ,
} }
2022-12-29 11:24:08 -05:00
}
2023-06-02 03:19:23 -04:00
// Fetch fetches the given apiObject from the public Constellation CDN.
// Fetch does not require authentication.
2023-09-25 05:53:02 -04:00
func Fetch [ T apiObject ] ( ctx context . Context , c HTTPClient , cdnURL string , obj T ) ( T , error ) {
2024-06-11 10:11:53 -04:00
rawObj , err := fetch ( ctx , c , cdnURL , obj )
if err != nil {
return * new ( T ) , fmt . Errorf ( "fetching %T: %w" , obj , err )
}
return parseObject ( rawObj , obj )
}
// FetchAndVerify fetches the given apiObject, checks if it can fetch an accompanying signature and verifies if the signature matches the found object.
// The public key used to verify the signature is embedded in the verifier argument.
// FetchAndVerify uses a generic to return a new object of type T.
// Otherwise the caller would have to cast the interface type to a concrete object, which could fail.
func FetchAndVerify [ T apiObject ] ( ctx context . Context , c HTTPClient , cdnURL string , obj T , cosignVerifier sigstore . Verifier ) ( T , error ) {
rawObj , err := fetch ( ctx , c , cdnURL , obj )
if err != nil {
return * new ( T ) , fmt . Errorf ( "fetching %T: %w" , obj , err )
}
fetchedObj , err := parseObject ( rawObj , obj )
if err != nil {
return fetchedObj , fmt . Errorf ( "parsing %T: %w" , obj , err )
}
signature , err := Fetch ( ctx , c , cdnURL , signature { Signed : obj . JSONPath ( ) } )
if err != nil {
return fetchedObj , fmt . Errorf ( "fetching signature: %w" , err )
}
err = cosignVerifier . VerifySignature ( rawObj , signature . Signature )
if err != nil {
return fetchedObj , fmt . Errorf ( "verifying signature: %w" , err )
}
return fetchedObj , nil
}
func fetch [ T apiObject ] ( ctx context . Context , c HTTPClient , cdnURL string , obj T ) ( [ ] byte , error ) {
2022-12-29 11:24:08 -05:00
if err := obj . ValidateRequest ( ) ; err != nil {
2024-06-11 10:11:53 -04:00
return nil , fmt . Errorf ( "validating request for %T: %w" , obj , err )
2022-12-29 11:24:08 -05:00
}
2023-09-25 05:53:02 -04:00
urlObj , err := url . Parse ( cdnURL )
2022-12-29 11:24:08 -05:00
if err != nil {
2024-06-11 10:11:53 -04:00
return nil , fmt . Errorf ( "parsing CDN root URL: %w" , err )
2022-12-29 11:24:08 -05:00
}
2023-09-25 05:53:02 -04:00
urlObj . Path = obj . JSONPath ( )
url := urlObj . String ( )
2022-12-29 11:24:08 -05:00
req , err := http . NewRequestWithContext ( ctx , http . MethodGet , url , http . NoBody )
if err != nil {
2024-06-11 10:11:53 -04:00
return nil , fmt . Errorf ( "creating request for %T: %w" , obj , err )
2022-12-29 11:24:08 -05:00
}
resp , err := c . Do ( req )
if err != nil {
2024-06-11 10:11:53 -04:00
return nil , fmt . Errorf ( "sending request for %T: %w" , obj , err )
2022-12-29 11:24:08 -05:00
}
defer resp . Body . Close ( )
switch resp . StatusCode {
case http . StatusOK :
case http . StatusNotFound :
2024-06-11 10:11:53 -04:00
return nil , & NotFoundError { fmt . Errorf ( "requesting resource at %s returned status code 404" , url ) }
2022-12-29 11:24:08 -05:00
default :
2024-06-11 10:11:53 -04:00
return nil , fmt . Errorf ( "unexpected status code %d while requesting resource" , resp . StatusCode )
2022-12-29 11:24:08 -05:00
}
2024-06-11 10:11:53 -04:00
return io . ReadAll ( resp . Body )
}
func parseObject [ T apiObject ] ( rawObj [ ] byte , obj T ) ( T , error ) {
2022-12-29 11:24:08 -05:00
var newObj T
2024-06-11 10:11:53 -04:00
if err := json . Unmarshal ( rawObj , & newObj ) ; err != nil {
2022-12-29 11:24:08 -05:00
return * new ( T ) , fmt . Errorf ( "decoding %T: %w" , obj , err )
}
if newObj . Validate ( ) != nil {
return * new ( T ) , fmt . Errorf ( "received invalid %T: %w" , newObj , newObj . Validate ( ) )
}
return newObj , nil
}
// NotFoundError is an error that is returned when a resource is not found.
type NotFoundError struct {
err error
}
func ( e * NotFoundError ) Error ( ) string {
return fmt . Sprintf ( "the requested resource was not found: %s" , e . err . Error ( ) )
}
func ( e * NotFoundError ) Unwrap ( ) error {
return e . err
}
2023-05-25 12:43:44 -04:00
// HTTPClient is an interface for http clients.
type HTTPClient interface {
2022-12-29 11:24:08 -05:00
Do ( req * http . Request ) ( * http . Response , error )
}
2023-06-02 03:19:23 -04:00
type apiObject interface {
ValidateRequest ( ) error
Validate ( ) error
2023-09-25 05:53:02 -04:00
JSONPath ( ) string
2023-06-02 03:19:23 -04:00
}
2023-08-01 10:48:13 -04:00
2023-08-25 06:40:47 -04:00
// signature manages the signature of a object saved at location 'Signed'.
2023-08-01 10:48:13 -04:00
type signature struct {
// Signed is the object that is signed.
2024-06-12 10:30:03 -04:00
Signed string ` json:"signed" `
2023-08-01 10:48:13 -04:00
// Signature is the signature of `Signed`.
Signature [ ] byte ` json:"signature" `
}
// URL returns the URL for the request to the config api.
2023-09-25 05:53:02 -04:00
func ( s signature ) JSONPath ( ) string {
return s . Signed + ".sig"
2023-08-01 10:48:13 -04:00
}
2024-06-12 10:30:03 -04:00
// ValidateRequest is a no-op.
2023-08-01 10:48:13 -04:00
func ( s signature ) ValidateRequest ( ) error {
2023-08-25 06:40:47 -04:00
return nil
2023-08-01 10:48:13 -04:00
}
2023-08-25 06:40:47 -04:00
// Validate checks that the signature is base64 encoded.
2023-08-01 10:48:13 -04:00
func ( s signature ) Validate ( ) error {
2023-08-25 06:40:47 -04:00
return sigstore . IsBase64 ( s . Signature )
2023-08-01 10:48:13 -04:00
}