Skip to content

Commit b8fc691

Browse files
committed
gateway-client/server stubs
1 parent 8186f9c commit b8fc691

File tree

14 files changed

+619
-12
lines changed

14 files changed

+619
-12
lines changed

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,20 @@ release: clean build.linux build/osx/$(BINARY)
7979
protoc.local-auth:
8080
protoc -I plugin/local-auth/proto/ plugin/local-auth/proto/auth.proto --go_out=plugins=grpc:plugin/local-auth/proto/
8181

82+
protoc.gateway-client:
83+
protoc -I plugin/gateway-client/proto/ plugin/gateway-client/proto/token-provider.proto --go_out=plugins=grpc:plugin/gateway-client/proto/
84+
85+
protoc.gateway-server:
86+
protoc -I plugin/gateway-server/proto/ plugin/gateway-server/proto/token-info.proto --go_out=plugins=grpc:plugin/gateway-server/proto/
87+
8288
plugin.auth-user:
8389
CGO_ENABLED=0 go build -o build/auth-user $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" cmd/plugin-auth-user/main.go
8490

8591
plugin.auth-ldap:
8692
CGO_ENABLED=0 go build -o build/auth-ldap $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" cmd/plugin-auth-ldap/main.go
8793

94+
plugin.googleid-provider:
95+
CGO_ENABLED=0 go build -o build/googleid-provider $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" cmd/plugin-googleid-provider/main.go
96+
8897
clean:
8998
@rm -rf build

cmd/kafka-proxy/server.go

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919
"time"
2020

2121
"errors"
22-
"github.com/grepplabs/kafka-proxy/plugin/local-auth/shared"
22+
gatewayclient "github.com/grepplabs/kafka-proxy/plugin/gateway-client/shared"
23+
localauth "github.com/grepplabs/kafka-proxy/plugin/local-auth/shared"
2324
"github.com/hashicorp/go-hclog"
2425
"github.com/hashicorp/go-plugin"
2526
)
@@ -143,7 +144,7 @@ func init() {
143144
func Run(_ *cobra.Command, _ []string) {
144145
logrus.Infof("Starting kafka-proxy version %s", config.Version)
145146

146-
var passwordAuthenticator shared.PasswordAuthenticator
147+
var passwordAuthenticator localauth.PasswordAuthenticator
147148
if c.Auth.Local.Enable {
148149
client := NewLocalAuthPluginClient()
149150
defer client.Kill()
@@ -157,11 +158,32 @@ func Run(_ *cobra.Command, _ []string) {
157158
logrus.Fatal(err)
158159
}
159160
var ok bool
160-
passwordAuthenticator, ok = raw.(shared.PasswordAuthenticator)
161+
passwordAuthenticator, ok = raw.(localauth.PasswordAuthenticator)
161162
if !ok {
162163
logrus.Fatal(errors.New("unsupported plugin type"))
163164
}
164165
}
166+
167+
var tokenProvider gatewayclient.TokenProvider
168+
if c.Auth.Gateway.Client.Enable {
169+
client := NewGatewayClientPluginClient()
170+
defer client.Kill()
171+
172+
rpcClient, err := client.Client()
173+
if err != nil {
174+
logrus.Fatal(err)
175+
}
176+
raw, err := rpcClient.Dispense("tokenProvider")
177+
if err != nil {
178+
logrus.Fatal(err)
179+
}
180+
var ok bool
181+
tokenProvider, ok = raw.(gatewayclient.TokenProvider)
182+
if !ok {
183+
logrus.Fatal(errors.New("unsupported plugin type"))
184+
}
185+
}
186+
165187
var g group.Group
166188
{
167189
// All active connections are stored in this variable.
@@ -175,7 +197,7 @@ func Run(_ *cobra.Command, _ []string) {
175197
if err != nil {
176198
logrus.Fatal(err)
177199
}
178-
proxyClient, err := proxy.NewClient(connset, c, listeners.GetNetAddressMapping, passwordAuthenticator)
200+
proxyClient, err := proxy.NewClient(connset, c, listeners.GetNetAddressMapping, passwordAuthenticator, tokenProvider)
179201
if err != nil {
180202
logrus.Fatal(err)
181203
}
@@ -274,24 +296,32 @@ func SetLogger() {
274296
logrus.SetLevel(level)
275297
}
276298

299+
func NewGatewayClientPluginClient() *plugin.Client {
300+
return NewPluginClient(gatewayclient.Handshake, gatewayclient.PluginMap, c.Auth.Gateway.Client.LogLevel, c.Auth.Gateway.Client.Command, c.Auth.Gateway.Client.Parameters)
301+
}
302+
277303
func NewLocalAuthPluginClient() *plugin.Client {
304+
return NewPluginClient(localauth.Handshake, localauth.PluginMap, c.Auth.Local.LogLevel, c.Auth.Local.Command, c.Auth.Local.Parameters)
305+
}
306+
307+
func NewPluginClient(handshakeConfig plugin.HandshakeConfig, plugins map[string]plugin.Plugin, logLevel string, command string, params []string) *plugin.Client {
278308
jsonFormat := false
279309
if c.Log.Format == "json" {
280310
jsonFormat = true
281311
}
282312
logger := hclog.New(&hclog.LoggerOptions{
283313
Output: os.Stdout,
284-
Level: hclog.LevelFromString(c.Auth.Local.LogLevel),
314+
Level: hclog.LevelFromString(logLevel),
285315
Name: "plugin",
286316
JSONFormat: jsonFormat,
287317
TimeFormat: time.RFC3339,
288318
})
289319

290320
return plugin.NewClient(&plugin.ClientConfig{
291-
HandshakeConfig: shared.Handshake,
292-
Plugins: shared.PluginMap,
321+
HandshakeConfig: handshakeConfig,
322+
Plugins: plugins,
293323
Logger: logger,
294-
Cmd: exec.Command(c.Auth.Local.Command, c.Auth.Local.Parameters...),
324+
Cmd: exec.Command(command, params...),
295325
AllowedProtocols: []plugin.Protocol{
296326
plugin.ProtocolNetRPC, plugin.ProtocolGRPC},
297327
})
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"github.com/grepplabs/kafka-proxy/plugin/gateway-client/shared"
7+
"github.com/hashicorp/go-plugin"
8+
"golang.org/x/oauth2/google"
9+
"google.golang.org/api/oauth2/v2"
10+
"os"
11+
"time"
12+
)
13+
14+
type TokenProvider struct {
15+
timeout int
16+
}
17+
18+
//TODO: caching, expiry
19+
//TODO: refresh in the half of time
20+
//TODO: send claims
21+
func (p TokenProvider) GetToken(claims []string) (int32, string, error) {
22+
23+
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(p.timeout)*time.Second)
24+
defer cancel()
25+
26+
tokenSource, err := google.DefaultTokenSource(ctx, oauth2.UserinfoEmailScope)
27+
if err != nil {
28+
return tokenResponse(1, "")
29+
}
30+
token, err := tokenSource.Token()
31+
if err != nil {
32+
return tokenResponse(2, "")
33+
}
34+
if token.Extra("id_token") == nil {
35+
return tokenResponse(3, "")
36+
}
37+
idToken := token.Extra("id_token").(string)
38+
return tokenResponse(0, idToken)
39+
40+
}
41+
42+
func tokenResponse(status int32, token string) (int32, string, error) {
43+
return status, token, nil
44+
}
45+
46+
func (f *TokenProvider) flagSet() *flag.FlagSet {
47+
fs := flag.NewFlagSet("google-id provider settings", flag.ContinueOnError)
48+
return fs
49+
}
50+
51+
func main() {
52+
tokenProvider := &TokenProvider{}
53+
fs := tokenProvider.flagSet()
54+
fs.Parse(os.Args[1:])
55+
fs.IntVar(&tokenProvider.timeout, "timeout", 5, "Request timeout")
56+
57+
plugin.Serve(&plugin.ServeConfig{
58+
HandshakeConfig: shared.Handshake,
59+
Plugins: map[string]plugin.Plugin{
60+
"tokenProvider": &shared.TokenProviderPlugin{Impl: tokenProvider},
61+
},
62+
// A non-nil value here enables gRPC serving for this plugin...
63+
GRPCServer: plugin.DefaultGRPCServer,
64+
})
65+
}

pkg/apis/gateway.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package apis
2+
3+
type TokenProvider interface {
4+
GetToken(claims []string) (int32, string, error)
5+
}

pkg/apis/localauth.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package apis
2+
3+
type PasswordAuthenticator interface {
4+
Authenticate(username, password string) (bool, int32, error)
5+
}

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

Lines changed: 167 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
syntax = "proto3";
2+
package proto;
3+
4+
message TokenRequest {
5+
repeated string claims = 1;
6+
}
7+
8+
message TokenResponse {
9+
int32 status = 1;
10+
string token = 2;
11+
// TODO: expiry ?
12+
}
13+
14+
service TokenProvider {
15+
rpc GetToken(TokenRequest) returns (TokenResponse);
16+
}

0 commit comments

Comments
 (0)