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

Add support for cascading retries #35

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
104 changes: 54 additions & 50 deletions clients/gatekeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clients

import (
"bytes"
"context"
"encoding/json"
"log"
"net/http"
Expand All @@ -10,39 +11,41 @@ import (

"github.com/tidepool-org/go-common/clients/disc"
"github.com/tidepool-org/go-common/clients/status"
"github.com/tidepool-org/go-common/errors"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/semconv"
)

type (
//Inteface so that we can mock gatekeeperClient for tests
//Interface so that we can mock gatekeeperClient for tests
Gatekeeper interface {
//userID -- the Tidepool-assigned userID
//groupID -- the Tidepool-assigned groupID
//
// returns the Permissions
UserInGroup(userID, groupID string) (Permissions, error)
UserInGroup(ctx context.Context, userID, groupID string) (Permissions, error)

//groupID -- the Tidepool-assigned groupID
//
// returns the map of user id to Permissions
UsersInGroup(groupID string) (UsersPermissions, error)
UsersInGroup(ctx context.Context, groupID string) (UsersPermissions, error)

//userID -- the Tidepool-assigned userID
//groupID -- the Tidepool-assigned groupID
//permissions -- the permisson we want to give the user for the group
SetPermissions(userID, groupID string, permissions Permissions) (Permissions, error)
SetPermissions(ctx context.Context, userID, groupID string, permissions Permissions) (Permissions, error)
}

GatekeeperClient struct {
httpClient *http.Client // store a reference to the http client so we can reuse it
hostGetter disc.HostGetter // The getter that provides the host to talk to for the client
tokenProvider TokenProvider // An object that provides tokens for communicating with gatekeeper
httpClient *http.Client // store a reference to the http client so we can reuse it
tokenProvider TokenProvider // An object that provides tokens for communicating with gatekeeper
host *url.URL
}

gatekeeperClientBuilder struct {
httpClient *http.Client // store a reference to the http client so we can reuse it
hostGetter disc.HostGetter // The getter that provides the host to talk to for the client
tokenProvider TokenProvider // An object that provides tokens for communicating with gatekeeper
httpClient *http.Client // store a reference to the http client so we can reuse it
tokenProvider TokenProvider // An object that provides tokens for communicating with gatekeeper
host *url.URL
}

Permission map[string]interface{}
Expand All @@ -64,7 +67,12 @@ func (b *gatekeeperClientBuilder) WithHttpClient(httpClient *http.Client) *gatek
}

func (b *gatekeeperClientBuilder) WithHostGetter(hostGetter disc.HostGetter) *gatekeeperClientBuilder {
b.hostGetter = hostGetter
b.host = &hostGetter.HostGet()[0]
return b
}

func (b *gatekeeperClientBuilder) WithHost(host *url.URL) *gatekeeperClientBuilder {
b.host = host
return b
}

Expand All @@ -74,8 +82,8 @@ func (b *gatekeeperClientBuilder) WithTokenProvider(tokenProvider TokenProvider)
}

func (b *gatekeeperClientBuilder) Build() *GatekeeperClient {
if b.hostGetter == nil {
panic("gatekeeperClient requires a hostGetter to be set")
if b.host == nil {
panic("gatekeeperClient requires a host to be set")
}
if b.tokenProvider == nil {
panic("gatekeeperClient requires a tokenProvider to be set")
Expand All @@ -87,19 +95,21 @@ func (b *gatekeeperClientBuilder) Build() *GatekeeperClient {

return &GatekeeperClient{
httpClient: b.httpClient,
hostGetter: b.hostGetter,
host: b.host,
tokenProvider: b.tokenProvider,
}
}

func (client *GatekeeperClient) UserInGroup(userID, groupID string) (Permissions, error) {
host := client.getHost()
if host == nil {
return nil, errors.New("No known gatekeeper hosts")
}
func (client *GatekeeperClient) UserInGroup(ctx context.Context, userID, groupID string) (Permissions, error) {
host := client.host
host.Path = path.Join(host.Path, "access", groupID, userID)

req, _ := http.NewRequest("GET", host.String(), nil)
tr := global.Tracer("go-common tracer")

spanCtx, span := tr.Start(ctx, "UserInGroup", trace.WithAttributes(semconv.PeerServiceKey.String("gatekeeper")))
defer span.End()

req, _ := http.NewRequestWithContext(spanCtx, "GET", host.String(), nil)
req.Header.Add("x-tidepool-session-token", client.tokenProvider.TokenProvide())

res, err := client.httpClient.Do(req)
Expand All @@ -112,24 +122,26 @@ func (client *GatekeeperClient) UserInGroup(userID, groupID string) (Permissions
retVal := make(Permissions)
if err := json.NewDecoder(res.Body).Decode(&retVal); err != nil {
log.Println(err)
return nil, &status.StatusError{status.NewStatus(500, "UserInGroup Unable to parse response.")}
return nil, &status.StatusError{Status: status.NewStatus(500, "UserInGroup Unable to parse response.")}
}
return retVal, nil
} else if res.StatusCode == 404 {
return nil, nil
} else {
return nil, &status.StatusError{status.NewStatusf(res.StatusCode, "Unknown response code from service[%s]", req.URL)}
return nil, &status.StatusError{Status: status.NewStatusf(res.StatusCode, "Unknown response code from service[%s]", req.URL)}
}
}

func (client *GatekeeperClient) UsersInGroup(groupID string) (UsersPermissions, error) {
host := client.getHost()
if host == nil {
return nil, errors.New("No known gatekeeper hosts")
}
func (client *GatekeeperClient) UsersInGroup(ctx context.Context, groupID string) (UsersPermissions, error) {
host := client.host
host.Path = path.Join(host.Path, "access", groupID)

req, _ := http.NewRequest("GET", host.String(), nil)
tr := global.Tracer("go-common tracer")

spanCtx, span := tr.Start(ctx, "UserInGroup", trace.WithAttributes(semconv.PeerServiceKey.String("gatekeeper")))
defer span.End()

req, _ := http.NewRequestWithContext(spanCtx, "GET", host.String(), nil)
req.Header.Add("x-tidepool-session-token", client.tokenProvider.TokenProvide())

res, err := client.httpClient.Do(req)
Expand All @@ -142,28 +154,30 @@ func (client *GatekeeperClient) UsersInGroup(groupID string) (UsersPermissions,
retVal := make(UsersPermissions)
if err := json.NewDecoder(res.Body).Decode(&retVal); err != nil {
log.Println(err)
return nil, &status.StatusError{status.NewStatus(500, "UserInGroup Unable to parse response.")}
return nil, &status.StatusError{Status: status.NewStatus(500, "UserInGroup Unable to parse response.")}
}
return retVal, nil
} else if res.StatusCode == 404 {
return nil, nil
} else {
return nil, &status.StatusError{status.NewStatusf(res.StatusCode, "Unknown response code from service[%s]", req.URL)}
return nil, &status.StatusError{Status: status.NewStatusf(res.StatusCode, "Unknown response code from service[%s]", req.URL)}
}
}

func (client *GatekeeperClient) SetPermissions(userID, groupID string, permissions Permissions) (Permissions, error) {
host := client.getHost()
if host == nil {
return nil, errors.New("No known gatekeeper hosts")
}
func (client *GatekeeperClient) SetPermissions(ctx context.Context, userID, groupID string, permissions Permissions) (Permissions, error) {
host := client.host
host.Path = path.Join(host.Path, "access", groupID, userID)

if jsonPerms, err := json.Marshal(permissions); err != nil {
log.Println(err)
return nil, &status.StatusError{status.NewStatusf(http.StatusInternalServerError, "Error marshaling the permissons [%s]", err)}
return nil, &status.StatusError{Status: status.NewStatusf(http.StatusInternalServerError, "Error marshaling the permissons [%s]", err)}
} else {
req, _ := http.NewRequest("POST", host.String(), bytes.NewBuffer(jsonPerms))
tr := global.Tracer("go-common tracer")

spanCtx, span := tr.Start(ctx, "UserInGroup", trace.WithAttributes(semconv.PeerServiceKey.String("gatekeeper")))
defer span.End()

req, _ := http.NewRequestWithContext(spanCtx, "POST", host.String(), bytes.NewBuffer(jsonPerms))
req.Header.Set("content-type", "application/json")
req.Header.Add("x-tidepool-session-token", client.tokenProvider.TokenProvide())

Expand All @@ -177,23 +191,13 @@ func (client *GatekeeperClient) SetPermissions(userID, groupID string, permissio
retVal := make(Permissions)
if err := json.NewDecoder(res.Body).Decode(&retVal); err != nil {
log.Printf("SetPermissions: Unable to parse response: [%s]", err.Error())
return nil, &status.StatusError{status.NewStatus(500, "SetPermissions: Unable to parse response:")}
return nil, &status.StatusError{Status: status.NewStatus(500, "SetPermissions: Unable to parse response:")}
}
return retVal, nil
} else if res.StatusCode == 404 {
return nil, nil
} else {
return nil, &status.StatusError{status.NewStatusf(res.StatusCode, "Unknown response code from service[%s]", req.URL)}
return nil, &status.StatusError{Status: status.NewStatusf(res.StatusCode, "Unknown response code from service[%s]", req.URL)}
}
}
}

func (client *GatekeeperClient) getHost() *url.URL {
if hostArr := client.hostGetter.HostGet(); len(hostArr) > 0 {
cpy := new(url.URL)
*cpy = hostArr[0]
return cpy
} else {
return nil
}
}
63 changes: 38 additions & 25 deletions clients/highwater/highwater.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package highwater

import (
"bytes"
"context"
"encoding/gob"
"log"
"net/http"
Expand All @@ -10,6 +11,9 @@ import (
"strings"

"github.com/tidepool-org/go-common/clients/disc"
"go.opentelemetry.io/otel/api/global"
"go.opentelemetry.io/otel/api/trace"
"go.opentelemetry.io/otel/semconv"
)

// Client interface that we will implement and mock
Expand All @@ -20,13 +24,13 @@ type Client interface {
}

type HighwaterClient struct {
hostGetter disc.HostGetter
host *url.URL
config *HighwaterClientConfig
httpClient *http.Client
}

type HighwaterClientBuilder struct {
hostGetter disc.HostGetter
host *url.URL
config *HighwaterClientConfig
httpClient *http.Client
}
Expand All @@ -44,7 +48,12 @@ func NewHighwaterClientBuilder() *HighwaterClientBuilder {
}

func (b *HighwaterClientBuilder) WithHostGetter(val disc.HostGetter) *HighwaterClientBuilder {
b.hostGetter = val
b.host = &val.HostGet()[0]
return b
}

func (b *HighwaterClientBuilder) WithHost(host *url.URL) *HighwaterClientBuilder {
b.host = host
return b
}

Expand Down Expand Up @@ -73,8 +82,8 @@ func (b *HighwaterClientBuilder) WithConfig(val *HighwaterClientConfig) *Highwat
}

func (b *HighwaterClientBuilder) Build() *HighwaterClient {
if b.hostGetter == nil {
panic("HighwaterClient requires a hostGetter to be set")
if b.host == nil {
panic("HighwaterClient requires a host to be set")
}
if b.config.Name == "" {
panic("HighwaterClient requires a name to be set")
Expand All @@ -92,22 +101,12 @@ func (b *HighwaterClientBuilder) Build() *HighwaterClient {
}

return &HighwaterClient{
hostGetter: b.hostGetter,
host: b.host,
httpClient: b.httpClient,
config: b.config,
}
}

func (client *HighwaterClient) getHost() *url.URL {
if hostArr := client.hostGetter.HostGet(); len(hostArr) > 0 {
cpy := new(url.URL)
*cpy = hostArr[0]
return cpy
} else {
return nil
}
}

func (client *HighwaterClient) adjustEventName(name string) string {
src := client.config.MetricsSource
src = strings.Replace(src, "-", " ", -1)
Expand All @@ -127,17 +126,22 @@ func (client *HighwaterClient) adjustEventParams(params map[string]string) []byt
return buf.Bytes()
}

func (client *HighwaterClient) PostServer(eventName, token string, params map[string]string) {
func (client *HighwaterClient) PostServer(ctx context.Context, eventName, token string, params map[string]string) {

host := client.getHost()
host := client.host
if host == nil {
log.Println("No known highwater hosts.")
return
}

host.Path = path.Join(host.Path, "server", client.config.Name, client.adjustEventName(eventName))

req, _ := http.NewRequest("GET", host.String(), bytes.NewBuffer(client.adjustEventParams(params)))
tr := global.Tracer("go-common tracer")

spanCtx, span := tr.Start(ctx, "PostServer", trace.WithAttributes(semconv.PeerServiceKey.String("highwater")))
defer span.End()

req, _ := http.NewRequestWithContext(spanCtx, "GET", host.String(), bytes.NewBuffer(client.adjustEventParams(params)))
req.Header.Add("x-tidepool-session-token", token)

res, err := client.httpClient.Do(req)
Expand All @@ -151,16 +155,21 @@ func (client *HighwaterClient) PostServer(eventName, token string, params map[st
return
}

func (client *HighwaterClient) PostThisUser(eventName, token string, params map[string]string) {
host := client.getHost()
func (client *HighwaterClient) PostThisUser(ctx context.Context, eventName, token string, params map[string]string) {
host := client.host
if host == nil {
log.Println("No known highwater hosts.")
return
}

host.Path = path.Join(host.Path, "thisuser", client.adjustEventName(eventName))

req, _ := http.NewRequest("GET", host.String(), bytes.NewBuffer(client.adjustEventParams(params)))
tr := global.Tracer("go-common tracer")

spanCtx, span := tr.Start(ctx, "PostThisUser", trace.WithAttributes(semconv.PeerServiceKey.String("highwater")))
defer span.End()

req, _ := http.NewRequestWithContext(spanCtx, "GET", host.String(), bytes.NewBuffer(client.adjustEventParams(params)))
req.Header.Add("x-tidepool-session-token", token)

res, err := client.httpClient.Do(req)
Expand All @@ -174,16 +183,20 @@ func (client *HighwaterClient) PostThisUser(eventName, token string, params map[
return
}

func (client *HighwaterClient) PostWithUser(userId, eventName, token string, params map[string]string) {
host := client.getHost()
func (client *HighwaterClient) PostWithUser(ctx context.Context, userId, eventName, token string, params map[string]string) {
host := client.host
if host == nil {
log.Println("No known highwater hosts.")
return
}

host.Path = path.Join(host.Path, "user", userId, client.adjustEventName(eventName))
tr := global.Tracer("go-common tracer")

spanCtx, span := tr.Start(ctx, "PostWithUser", trace.WithAttributes(semconv.PeerServiceKey.String("highwater")))
defer span.End()

req, _ := http.NewRequest("GET", host.String(), bytes.NewBuffer(client.adjustEventParams(params)))
req, _ := http.NewRequestWithContext(spanCtx, "GET", host.String(), bytes.NewBuffer(client.adjustEventParams(params)))
req.Header.Add("x-tidepool-session-token", token)

if _, err := client.httpClient.Do(req); err != nil {
Expand Down
Loading