Skip to content

Commit

Permalink
attestation/snp: use context with timeout on THIM request
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Meyer <[email protected]>
  • Loading branch information
katexochen committed Dec 6, 2024
1 parent 74e3e1a commit 8b58e8e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/attestation/snp/issuer/issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (i *Issuer) OID() asn1.ObjectIdentifier {
}

// Issue the attestation document.
func (i *Issuer) Issue(_ context.Context, ownPublicKey []byte, nonce []byte) (res []byte, err error) {
func (i *Issuer) Issue(ctx context.Context, ownPublicKey []byte, nonce []byte) (res []byte, err error) {
i.logger.Info("Issue called")
defer func() {
if err != nil {
Expand All @@ -71,7 +71,7 @@ func (i *Issuer) Issue(_ context.Context, ownPublicKey []byte, nonce []byte) (re

// Get cert chain from THIM
var certChain *spb.CertificateChain
thimRaw, err := i.thimGetter.GetCertification()
thimRaw, err := i.thimGetter.GetCertification(ctx)
if err != nil {
i.logger.Info("Could not retrieve THIM certification", "error", err)
} else {
Expand Down
7 changes: 5 additions & 2 deletions internal/attestation/snp/issuer/thim.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package issuer

import (
"context"
"encoding/json"
"encoding/pem"
"fmt"
Expand Down Expand Up @@ -78,7 +79,7 @@ func NewTHIMGetter(httpClient httpClient) *THIMGetter {
}

// GetCertification returns the THIM certification.
func (t *THIMGetter) GetCertification() (THIMSNPCertification, error) {
func (t *THIMGetter) GetCertification(ctx context.Context) (THIMSNPCertification, error) {
// Return cached response if it is still valid.
if cached := t.getCached(); cached != nil {
var certification THIMSNPCertification
Expand All @@ -102,7 +103,9 @@ func (t *THIMGetter) GetCertification() (THIMSNPCertification, error) {
"Metadata": {"true"},
},
}
resp, err := t.httpClient.Do(req)
reqCtx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
resp, err := t.httpClient.Do(req.WithContext(reqCtx))
if err != nil {
return THIMSNPCertification{}, fmt.Errorf("getting THIM certification: %w", err)
}
Expand Down

0 comments on commit 8b58e8e

Please sign in to comment.