Skip to content

Commit eae1670

Browse files
committed
Add context parameter to GetToken
1 parent cdfd209 commit eae1670

File tree

6 files changed

+23
-13
lines changed

6 files changed

+23
-13
lines changed

cmd/plugin-googleid-provider/main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ type TokenProvider struct {
1717
}
1818

1919
//TODO: caching, expiry
20-
//TODO: refresh in the half of time
21-
//TODO: send claims
22-
func (p TokenProvider) GetToken(request apis.TokenRequest) (apis.TokenResponse, error) {
23-
24-
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(p.timeout)*time.Second)
20+
//TODO: go routine - on startup, refresh in the half of time (back off - > use the backoff lib)
21+
//TODO: read / write mutex
22+
//TODO: const status codes + prometheus metrics
23+
func (p TokenProvider) GetToken(parent context.Context, request apis.TokenRequest) (apis.TokenResponse, error) {
24+
ctx, cancel := context.WithTimeout(parent, time.Duration(p.timeout)*time.Second)
2525
defer cancel()
2626

2727
tokenSource, err := google.DefaultTokenSource(ctx, oauth2.UserinfoEmailScope)
@@ -52,7 +52,7 @@ func (f *TokenProvider) flagSet() *flag.FlagSet {
5252
func main() {
5353
tokenProvider := &TokenProvider{}
5454
fs := tokenProvider.flagSet()
55-
fs.IntVar(&tokenProvider.timeout, "timeout", 5, "Request timeout")
55+
fs.IntVar(&tokenProvider.timeout, "timeout", 5, "Request timeout in seconds")
5656

5757
fs.Parse(os.Args[1:])
5858

pkg/apis/gateway.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package apis
22

3+
import (
4+
"context"
5+
)
6+
37
type TokenRequest struct {
48
Params []string
59
}
@@ -10,5 +14,5 @@ type TokenResponse struct {
1014
}
1115

1216
type TokenProvider interface {
13-
GetToken(request TokenRequest) (TokenResponse, error)
17+
GetToken(ctx context.Context, request TokenRequest) (TokenResponse, error)
1418
}

plugin/gateway-client/proto/token-provider.proto

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ message TokenResponse {
99
bool success = 1;
1010
int32 status = 2;
1111
string token = 3;
12-
// TODO: expiry ?
1312
}
1413

1514
service TokenProvider {

plugin/gateway-client/shared/grpc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ type GRPCClient struct {
1313
client proto.TokenProviderClient
1414
}
1515

16-
func (m *GRPCClient) GetToken(request apis.TokenRequest) (apis.TokenResponse, error) {
17-
resp, err := m.client.GetToken(context.Background(), &proto.TokenRequest{Params: request.Params})
16+
func (m *GRPCClient) GetToken(ctx context.Context, request apis.TokenRequest) (apis.TokenResponse, error) {
17+
resp, err := m.client.GetToken(ctx, &proto.TokenRequest{Params: request.Params})
1818
return apis.TokenResponse{Success: resp.Success, Status: resp.Status, Token: resp.Token}, err
1919
}
2020

@@ -27,6 +27,6 @@ type GRPCServer struct {
2727
func (m *GRPCServer) GetToken(
2828
ctx context.Context,
2929
req *proto.TokenRequest) (*proto.TokenResponse, error) {
30-
resp, err := m.Impl.GetToken(apis.TokenRequest{Params: req.Params})
30+
resp, err := m.Impl.GetToken(ctx, apis.TokenRequest{Params: req.Params})
3131
return &proto.TokenResponse{Success: resp.Success, Status: resp.Status, Token: resp.Token}, err
3232
}

plugin/gateway-client/shared/rpc.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package shared
33
import (
44
"github.com/grepplabs/kafka-proxy/pkg/apis"
55
"net/rpc"
6+
"context"
67
)
78

89
type RPCClient struct{ client *rpc.Client }
@@ -21,7 +22,7 @@ type RPCServer struct {
2122

2223
func (m *RPCServer) GetToken(args map[string]interface{}, resp *map[string]interface{}) error {
2324

24-
r, err := m.Impl.GetToken(apis.TokenRequest{Params: args["params"].([]string)})
25+
r, err := m.Impl.GetToken(context.Background(), apis.TokenRequest{Params: args["params"].([]string)})
2526
*resp = map[string]interface{}{
2627
"success": r.Success,
2728
"status": r.Status,

proxy/auth.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io"
1010
"strings"
1111
"time"
12+
"context"
1213
)
1314

1415
type AuthClient struct {
@@ -23,7 +24,12 @@ type AuthClient struct {
2324
//TODO: reset deadlines after method - ok
2425
func (b *AuthClient) sendAndReceiveGatewayAuth(conn DeadlineReaderWriter) error {
2526
//TODO: retrieve from plugin (with timeout)
26-
resp, err := b.tokenProvider.GetToken(apis.TokenRequest{})
27+
/*
28+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(p.timeout)*time.Second)
29+
defer cancel()
30+
*/
31+
32+
resp, err := b.tokenProvider.GetToken(context.Background(), apis.TokenRequest{})
2733
if err != nil {
2834
return err
2935
}

0 commit comments

Comments
 (0)