diff --git a/metadata/decode.go b/metadata/decode.go index 293ea9f..b83eecc 100644 --- a/metadata/decode.go +++ b/metadata/decode.go @@ -248,6 +248,7 @@ func mdsParseX509Certificate(value string) (certificate *x509.Certificate, err e return certificate, nil } +// DecoderOption is a representation of a function that can set options within a decoder. type DecoderOption func(decoder *Decoder) (err error) // WithIgnoreEntryParsingErrors is a DecoderOption which ignores errors when parsing individual entries. The values for @@ -260,6 +261,7 @@ func WithIgnoreEntryParsingErrors() DecoderOption { } } +// WithRootCertificate overrides the root certificate used to validate the authenticity of the metadata payload. func WithRootCertificate(value string) DecoderOption { return func(decoder *Decoder) (err error) { decoder.root = value diff --git a/metadata/metadata.go b/metadata/metadata.go index 2907e8a..64fb9eb 100644 --- a/metadata/metadata.go +++ b/metadata/metadata.go @@ -3,6 +3,7 @@ package metadata import ( "crypto/x509" "fmt" + "net/http" "net/url" "strings" "time" @@ -10,6 +11,33 @@ import ( "github.com/google/uuid" ) +// Fetch creates a new HTTP client and gets the production metadata, decodes it, and parses it. This is an +// instrumentation simplification that makes it easier to either just grab the latest metadata or for implementers to +// see the rough process of retrieving it to implement any of their own logic. +func Fetch() (metadata *Metadata, err error) { + var ( + decoder *Decoder + payload *MetadataBLOBPayloadJSON + res *http.Response + ) + + if decoder, err = NewDecoder(); err != nil { + return nil, err + } + + client := &http.Client{} + + if res, err = client.Get(ProductionMDSURL); err != nil { + return nil, err + } + + if payload, err = decoder.Decode(res.Body); err != nil { + return nil, err + } + + return decoder.Parse(payload) +} + type Metadata struct { Parsed MetadataBLOBPayload Unparsed []MetadataBLOBPayloadEntryError diff --git a/metadata/providers/memory/provider.go b/metadata/providers/memory/provider.go index 775e600..e60a080 100644 --- a/metadata/providers/memory/provider.go +++ b/metadata/providers/memory/provider.go @@ -72,3 +72,7 @@ func (p *Provider) ValidateStatusReports(ctx context.Context, reports []metadata return metadata.ValidateStatusReports(reports, p.desired, p.undesired) } + +var ( + _ metadata.Provider = (*Provider)(nil) +)