Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try with retry #39

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/google/go-cmp v0.6.0
github.com/google/uuid v1.3.0
github.com/gookit/config/v2 v2.2.1
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/hellofresh/health-go/v5 v5.0.0
github.com/lestrrat-go/jwx v1.2.25
github.com/mitchellh/mapstructure v1.5.0
Expand All @@ -35,7 +36,7 @@ require (
github.com/consensys/gnark-crypto v0.12.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/fatih/color v1.14.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-jose/go-jose/v3 v3.0.1-0.20221117193127-916db76e8214 // indirect
github.com/goccy/go-yaml v1.10.0 // indirect
Expand All @@ -44,6 +45,7 @@ require (
github.com/google/tink/go v1.7.0 // indirect
github.com/gookit/color v1.5.2 // indirect
github.com/gookit/goutil v0.6.6 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hyperledger/fabric-amcl v0.0.0-20230602173724-9e02669dceb2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/kilic/bls12-381 v0.1.1-0.20210503002446-7b7597926c69 // indirect
Expand Down Expand Up @@ -99,7 +101,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
Expand All @@ -113,7 +115,7 @@ require (
golang.org/x/arch v0.5.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
18 changes: 12 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/fiware/dsba-pdp v0.0.0-20230215083849-cf2b4c3daacf h1:2bEmlh0Au6+n66cYm+5ECFzfwUqlHPAIWLJ28rOjkoc=
github.com/fiware/dsba-pdp v0.0.0-20230215083849-cf2b4c3daacf/go.mod h1:HpYuQF4RWRFxAFkqItyaCszNbOarnCOFuqKxWMGvFEI=
github.com/foolin/goview v0.3.0 h1:q5wKwXKEFb20dMRfYd59uj5qGCo7q4L9eVHHUjmMWrg=
Expand Down Expand Up @@ -241,6 +241,12 @@ github.com/gookit/goutil v0.6.6 h1:XdvnPocHpKDXA+eykfc/F846Y1V2Vyo3+cV8rfliG90=
github.com/gookit/goutil v0.6.6/go.mod h1:D++7kbQd/6vECyYTxB5tq6AKDIG9ZYwZNhubWJvN9dw=
github.com/gookit/ini/v2 v2.2.1 h1:6fCrz8icnUHhYqGZwu7RtHLh+v+ErrgrAt9+aIcoJCc=
github.com/gookit/ini/v2 v2.2.1/go.mod h1:as7p0WsJg4sufGECEqNdY/+I026qRnyzPa4UYHuuOR4=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-retryablehttp v0.7.7 h1:C8hUCYzor8PIfXHa4UrZkU4VvK8o9ISHxT2Q8+VepXU=
github.com/hashicorp/go-retryablehttp v0.7.7/go.mod h1:pkQpWZeYWskR+D1tR2O5OcBFOxfA7DoAO6xtkuQnHTk=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hellofresh/health-go/v5 v5.0.0 h1:jxjllHekqEU4VYIajKJtFoOxDp1YaaygNWwAoZwWFh0=
Expand Down Expand Up @@ -318,8 +324,8 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down Expand Up @@ -641,8 +647,8 @@ golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
Expand Down
67 changes: 33 additions & 34 deletions tir/tirClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,30 +123,50 @@ func (tc TirHttpClient) GetTrustedIssuer(tirEndpoints []string, did string) (exi
for _, tirEndpoint := range tirEndpoints {
trustedIssuer, hit := tc.tilCache.Get(tirEndpoint + did)
if !hit {
resp, err := tc.requestIssuer(tirEndpoint, did)
exists, trustedIssuer, err = tc.getIssuerWithRetry(tirEndpoint, did)
if err != nil {
logging.Log().Warnf("Was not able to get the issuer %s from %s because of err: %v.", did, tirEndpoint, err)
continue
}
if resp.StatusCode != 200 {
logging.Log().Debugf("Issuer %s is not known at %s.", did, tirEndpoint)
if !exists {
continue
}
trustedIssuer, err = parseTirResponse(*resp)
if err != nil {
logging.Log().Warnf("Was not able to parse the response from til %s for %s. Err: %v", tirEndpoint, did, err)
logging.Log().Debugf("Response was %v ", resp)
continue
}
logging.Log().Debugf("Got issuer %s.", logging.PrettyPrintObject(trustedIssuer))
tc.tilCache.Set(tirEndpoint+did, trustedIssuer, cache.DefaultExpiration)
logging.Log().Debugf("Got issuer %s.", logging.PrettyPrintObject(trustedIssuer))
}
return true, trustedIssuer.(TrustedIssuer), err

}
return false, trustedIssuer, err
}

func (tc TirHttpClient) getIssuerWithRetry(tirEndpoint string, did string) (exists bool, trustedIssuer TrustedIssuer, err error) {

currentTry := 0
for currentTry < 3 {
resp, err := tc.requestIssuer(tirEndpoint, did)
if err != nil {
logging.Log().Warnf("Was not able to get the issuer %s from %s because of err: %v.", did, tirEndpoint, err)
return false, trustedIssuer, err
}
if resp.StatusCode != 200 {
logging.Log().Debugf("Issuer %s is not known at %s.", did, tirEndpoint)
return false, trustedIssuer, err
}
trustedIssuer, err = parseTirResponse(*resp)
if err != nil && err.Error() == "EOF" {
logging.Log().Warnf("Was not able to parse the response from til %s for %s. Err: %v", tirEndpoint, did, err)
logging.Log().Debugf("Response was %v ", resp)
currentTry++
time.Sleep(time.Millisecond * 1000)
continue
} else if err != nil {
return false, trustedIssuer, err
}
exists = true
break
}
return exists, trustedIssuer, err
}

func parseTirResponse(resp http.Response) (trustedIssuer TrustedIssuer, err error) {

if resp.Body == nil {
Expand Down Expand Up @@ -181,20 +201,8 @@ func (tc TirHttpClient) issuerExists(tirEndpoint string, did string) (trusted bo
}

func (tc TirHttpClient) requestIssuer(tirEndpoint string, did string) (response *http.Response, err error) {
response, err = tc.requestIssuerWithVersion(tirEndpoint, getIssuerV4Url(did))
if err != nil {
logging.Log().Debugf("Got error %v", err)
return tc.requestIssuerWithVersion(tirEndpoint, getIssuerV3Url(did))
}

if response.StatusCode != 200 {
logging.Log().Debugf("Got status %v", response.StatusCode)
return tc.requestIssuerWithVersion(tirEndpoint, getIssuerV3Url(did))
}
return response, err
}
didPath := ISSUERS_V4_PATH + "/" + did

func (tc TirHttpClient) requestIssuerWithVersion(tirEndpoint string, didPath string) (response *http.Response, err error) {
logging.Log().Debugf("Get issuer %s/%s.", tirEndpoint, didPath)
cacheKey := common.BuildUrlString(tirEndpoint, didPath)
responseInterface, hit := common.GlobalCache.IssuerCache.Get(cacheKey)
Expand All @@ -216,12 +224,3 @@ func (tc TirHttpClient) requestIssuerWithVersion(tirEndpoint string, didPath str
logging.Log().Debugf("Added cache entry for %s", cacheKey)
return resp, err
}

func getIssuerV4Url(did string) string {
return ISSUERS_V4_PATH + "/" + did
}

func getIssuerV3Url(did string) string {
return ISSUERS_V3_PATH + "/" + did

}
4 changes: 2 additions & 2 deletions tir/tirClient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func TestGetTrustedIssuer(t *testing.T) {
{testName: "The issuer should be returned, even if something unparsable is returned at one endpoint.", testIssuer: "did:web:test.org", testEndpoints: []string{"https://my-other-tir.org", "https://my-tir.org"},
mockResponses: map[string]*http.Response{"https://my-other-tir.org/v4/issuers/did:web:test.org": getUnparsableResponse(), "https://my-tir.org/v4/issuers/did:web:test.org": getIssuerResponse("did:web:test.org")}, expectExists: true, expectedIssuer: "did:web:test.org"},
{testName: "The issuer not should be returned, if an error is thrown at the endpoint.", testIssuer: "did:web:test.org", testEndpoints: []string{"https://my-erronous-tir.org"},
mockErrors: map[string]error{"https://https://my-erronous-tir.org/v4/issuers/did:web:test.org": errors.New("something_bad")}, expectExists: false},
mockErrors: map[string]error{"https://my-erronous-tir.org/v4/issuers/did:web:test.org": errors.New("something_bad")}, expectExists: false},
{testName: "The issuer not should be returned, if the response cannot be parsed.", testIssuer: "did:web:test.org", testEndpoints: []string{"https://my-erronous-tir.org"},
mockResponses: map[string]*http.Response{"https://https://my-erronous-tir.org/v4/issuers/did:web:test.org": getUnparsableResponse()}, expectExists: false},
mockResponses: map[string]*http.Response{"https://my-erronous-tir.org/v4/issuers/did:web:test.org": getUnparsableResponse()}, expectExists: false},
}

for _, tc := range tests {
Expand Down
Loading