Skip to content

Commit

Permalink
feat: add tooling
Browse files Browse the repository at this point in the history
  • Loading branch information
james-d-elliott committed Jun 9, 2024
1 parent 74e2212 commit e95ceb3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions metadata/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,41 @@ package metadata
import (
"crypto/x509"
"fmt"
"net/http"
"net/url"
"strings"
"time"

"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
Expand Down
4 changes: 4 additions & 0 deletions metadata/providers/memory/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)

0 comments on commit e95ceb3

Please sign in to comment.