Skip to content

Commit f71e258

Browse files
committed
Built-in component registry
1 parent 0c7f5ea commit f71e258

File tree

14 files changed

+359
-147
lines changed

14 files changed

+359
-147
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ notifications:
2424

2525
script:
2626
- go build .
27-
- go build -o google-id-info cmd/plugin-googleid-info/main.go
2827
- export TAG=`if [[ $TRAVIS_PULL_REQUEST == "false" ]] && [[ $TRAVIS_BRANCH == "master" ]]; then echo "latest"; else echo "${TRAVIS_PULL_REQUEST_BRANCH:-${TRAVIS_BRANCH}}"; fi`
2928
- docker build -t $REPO:$TAG -f Dockerfile .
3029

Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ FROM alpine:3.7
33
RUN apk add --no-cache ca-certificates
44

55
ADD kafka-proxy /kafka-proxy
6-
ADD google-id-info /google-id-info
76

87
ENTRYPOINT ["/kafka-proxy"]
98
CMD ["--help"]

Makefile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,12 @@ build: build/$(BINARY)
3838

3939
build/$(BINARY): $(SOURCES)
4040
CGO_ENABLED=0 go build -o build/$(BINARY) $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" .
41-
CGO_ENABLED=0 go build -o build/google-id-info $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" cmd/plugin-googleid-info/main.go
4241

4342
build/linux/$(BINARY): $(SOURCES)
4443
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o build/linux/$(BINARY) $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" .
45-
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o build/linux/google-id-info $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" cmd/plugin-googleid-info/main.go
4644

4745
build/osx/$(BINARY): $(SOURCES)
4846
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o build/osx/$(BINARY) $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" .
49-
GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o build/linux/google-id-info $(BUILD_FLAGS) -ldflags "$(LDFLAGS)" cmd/plugin-googleid-info/main.go
5047

5148
build.docker-build: $(BUILD_DOCKER_BUILD)
5249

@@ -58,7 +55,6 @@ build.docker-build.linux:
5855
echo "containerId: $$buildContainer" ;\
5956
mkdir -p build ;\
6057
docker cp $$buildContainer:/go/src/github.com/grepplabs/kafka-proxy/build/linux/${BINARY} build/${BINARY} ;\
61-
docker cp $$buildContainer:/go/src/github.com/grepplabs/kafka-proxy/build/linux/google-id-info build/google-id-info ;\
6258
docker rm $$buildContainer ;\
6359
docker rmi $$buildContainerName ;\
6460

@@ -70,7 +66,6 @@ build.docker-build.osx:
7066
echo "containerId: $$buildContainer" ;\
7167
mkdir -p build ;\
7268
docker cp $$buildContainer:/go/src/github.com/grepplabs/kafka-proxy/build/osx/${BINARY} build/${BINARY} ;\
73-
docker cp $$buildContainer:/go/src/github.com/grepplabs/kafka-proxy/build/linux/google-id-info build/google-id-info ;\
7469
docker rm $$buildContainer ;\
7570
docker rmi $$buildContainerName ;\
7671

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ spec:
280280
* [X] Pluggable authentication between client kafka-proxy and broker kafka-proxy a.k.a kafka-gateway
281281
1. additional handshake - protocol: magic, method, data
282282
2. google-id method
283-
* [ ] Registry for built in plugins (avoid grpc communication)
283+
* [X] Registry for built-in plugins
284284
* [ ] Client cert check
285285
* [ ] TLS server parameters like CipherSuites etc. - see ory/graceful/blob/master/http_defaults.go
286286
* [ ] Performance tests and tuning

cmd/kafka-proxy/server.go

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ import (
2525
localauth "github.com/grepplabs/kafka-proxy/plugin/local-auth/shared"
2626
"github.com/hashicorp/go-hclog"
2727
"github.com/hashicorp/go-plugin"
28+
29+
"github.com/grepplabs/kafka-proxy/pkg/registry"
30+
// built-in plugins
31+
_ "github.com/grepplabs/kafka-proxy/pkg/libs/googleid-info"
32+
_ "github.com/grepplabs/kafka-proxy/pkg/libs/googleid-provider"
2833
)
2934

3035
var (
@@ -148,61 +153,89 @@ func Run(_ *cobra.Command, _ []string) {
148153

149154
var passwordAuthenticator apis.PasswordAuthenticator
150155
if c.Auth.Local.Enable {
151-
client := NewLocalAuthPluginClient()
152-
defer client.Kill()
156+
var err error
157+
factory, ok := registry.GetComponent(new(apis.PasswordAuthenticatorFactory), c.Auth.Local.Command).(apis.PasswordAuthenticatorFactory)
158+
if ok {
159+
logrus.Infof("Using built-in PasswordAuthenticator")
160+
passwordAuthenticator, err = factory.New(c.Auth.Local.Parameters)
161+
if err != nil {
162+
logrus.Fatal(err)
163+
}
164+
} else {
165+
client := NewPluginClient(localauth.Handshake, localauth.PluginMap, c.Auth.Local.LogLevel, c.Auth.Local.Command, c.Auth.Local.Parameters)
166+
defer client.Kill()
153167

154-
rpcClient, err := client.Client()
155-
if err != nil {
156-
logrus.Fatal(err)
157-
}
158-
raw, err := rpcClient.Dispense("passwordAuthenticator")
159-
if err != nil {
160-
logrus.Fatal(err)
161-
}
162-
var ok bool
163-
passwordAuthenticator, ok = raw.(apis.PasswordAuthenticator)
164-
if !ok {
165-
logrus.Fatal(errors.New("unsupported PasswordAuthenticator plugin type"))
168+
rpcClient, err := client.Client()
169+
if err != nil {
170+
logrus.Fatal(err)
171+
}
172+
raw, err := rpcClient.Dispense("passwordAuthenticator")
173+
if err != nil {
174+
logrus.Fatal(err)
175+
}
176+
passwordAuthenticator, ok = raw.(apis.PasswordAuthenticator)
177+
if !ok {
178+
logrus.Fatal(errors.New("unsupported PasswordAuthenticator plugin type"))
179+
}
166180
}
167181
}
168182

169183
var tokenProvider apis.TokenProvider
170184
if c.Auth.Gateway.Client.Enable {
171-
client := NewGatewayClientPluginClient()
172-
defer client.Kill()
185+
var err error
186+
factory, ok := registry.GetComponent(new(apis.TokenProviderFactory), c.Auth.Gateway.Client.Command).(apis.TokenProviderFactory)
187+
if ok {
188+
logrus.Infof("Using built-in TokenProvider")
189+
tokenProvider, err = factory.New(c.Auth.Gateway.Client.Parameters)
190+
if err != nil {
191+
logrus.Fatal(err)
192+
}
193+
} else {
194+
client := NewPluginClient(gatewayclient.Handshake, gatewayclient.PluginMap, c.Auth.Gateway.Client.LogLevel, c.Auth.Gateway.Client.Command, c.Auth.Gateway.Client.Parameters)
195+
defer client.Kill()
173196

174-
rpcClient, err := client.Client()
175-
if err != nil {
176-
logrus.Fatal(err)
177-
}
178-
raw, err := rpcClient.Dispense("tokenProvider")
179-
if err != nil {
180-
logrus.Fatal(err)
181-
}
182-
var ok bool
183-
tokenProvider, ok = raw.(apis.TokenProvider)
184-
if !ok {
185-
logrus.Fatal(errors.New("unsupported TokenProvider plugin type"))
197+
rpcClient, err := client.Client()
198+
if err != nil {
199+
logrus.Fatal(err)
200+
}
201+
raw, err := rpcClient.Dispense("tokenProvider")
202+
if err != nil {
203+
logrus.Fatal(err)
204+
}
205+
tokenProvider, ok = raw.(apis.TokenProvider)
206+
if !ok {
207+
logrus.Fatal(errors.New("unsupported TokenProvider plugin type"))
208+
}
186209
}
187210
}
188211

189212
var tokenInfo apis.TokenInfo
190213
if c.Auth.Gateway.Server.Enable {
191-
client := NewGatewayServerPluginClient()
192-
defer client.Kill()
214+
var err error
215+
factory, ok := registry.GetComponent(new(apis.TokenInfoFactory), c.Auth.Gateway.Server.Command).(apis.TokenInfoFactory)
216+
if ok {
217+
logrus.Infof("Using built-in TokenInfo")
218+
219+
tokenInfo, err = factory.New(c.Auth.Gateway.Server.Parameters)
220+
if err != nil {
221+
logrus.Fatal(err)
222+
}
223+
} else {
224+
client := NewPluginClient(gatewayserver.Handshake, gatewayserver.PluginMap, c.Auth.Gateway.Server.LogLevel, c.Auth.Gateway.Server.Command, c.Auth.Gateway.Server.Parameters)
225+
defer client.Kill()
193226

194-
rpcClient, err := client.Client()
195-
if err != nil {
196-
logrus.Fatal(err)
197-
}
198-
raw, err := rpcClient.Dispense("tokenInfo")
199-
if err != nil {
200-
logrus.Fatal(err)
201-
}
202-
var ok bool
203-
tokenInfo, ok = raw.(apis.TokenInfo)
204-
if !ok {
205-
logrus.Fatal(errors.New("unsupported TokenInfo plugin type"))
227+
rpcClient, err := client.Client()
228+
if err != nil {
229+
logrus.Fatal(err)
230+
}
231+
raw, err := rpcClient.Dispense("tokenInfo")
232+
if err != nil {
233+
logrus.Fatal(err)
234+
}
235+
tokenInfo, ok = raw.(apis.TokenInfo)
236+
if !ok {
237+
logrus.Fatal(errors.New("unsupported TokenInfo plugin type"))
238+
}
206239
}
207240
}
208241

@@ -318,16 +351,6 @@ func SetLogger() {
318351
logrus.SetLevel(level)
319352
}
320353

321-
func NewGatewayClientPluginClient() *plugin.Client {
322-
return NewPluginClient(gatewayclient.Handshake, gatewayclient.PluginMap, c.Auth.Gateway.Client.LogLevel, c.Auth.Gateway.Client.Command, c.Auth.Gateway.Client.Parameters)
323-
}
324-
func NewGatewayServerPluginClient() *plugin.Client {
325-
return NewPluginClient(gatewayserver.Handshake, gatewayserver.PluginMap, c.Auth.Gateway.Server.LogLevel, c.Auth.Gateway.Server.Command, c.Auth.Gateway.Server.Parameters)
326-
}
327-
func NewLocalAuthPluginClient() *plugin.Client {
328-
return NewPluginClient(localauth.Handshake, localauth.PluginMap, c.Auth.Local.LogLevel, c.Auth.Local.Command, c.Auth.Local.Parameters)
329-
}
330-
331354
func NewPluginClient(handshakeConfig plugin.HandshakeConfig, plugins map[string]plugin.Plugin, logLevel string, command string, params []string) *plugin.Client {
332355
jsonFormat := false
333356
if c.Log.Format == "json" {

cmd/plugin-googleid-info/main.go

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,17 @@
11
package main
22

33
import (
4-
"flag"
5-
"fmt"
64
"github.com/grepplabs/kafka-proxy/pkg/libs/googleid-info"
75
"github.com/grepplabs/kafka-proxy/plugin/gateway-server/shared"
86
"github.com/hashicorp/go-plugin"
97
"github.com/sirupsen/logrus"
108
"os"
119
)
1210

13-
func (f *pluginMeta) flagSet() *flag.FlagSet {
14-
fs := flag.NewFlagSet("google-id info settings", flag.ContinueOnError)
15-
return fs
16-
}
17-
18-
type pluginMeta struct {
19-
timeout int
20-
certsRefreshInterval int
21-
audience arrayFlags
22-
emailsRegex arrayFlags
23-
}
24-
25-
type arrayFlags []string
26-
27-
func (i *arrayFlags) String() string {
28-
return fmt.Sprintf("%v", *i)
29-
}
30-
31-
func (i *arrayFlags) Set(value string) error {
32-
*i = append(*i, value)
33-
return nil
34-
}
35-
36-
func (i *arrayFlags) asMap() map[string]struct{} {
37-
result := make(map[string]struct{})
38-
for _, elem := range *i {
39-
result[elem] = struct{}{}
40-
}
41-
return result
42-
}
43-
4411
func main() {
45-
pluginMeta := &pluginMeta{}
46-
fs := pluginMeta.flagSet()
47-
fs.IntVar(&pluginMeta.timeout, "timeout", 10, "Request timeout in seconds")
48-
fs.IntVar(&pluginMeta.certsRefreshInterval, "certs-refresh-interval", 60*60, "Certificates refresh interval in seconds")
49-
fs.Var(&pluginMeta.audience, "audience", "The audience of a token")
50-
fs.Var(&pluginMeta.emailsRegex, "email-regex", "Regex of the email claim")
51-
52-
fs.Parse(os.Args[1:])
53-
54-
opts := googleidinfo.TokenInfoOptions{
55-
Timeout: pluginMeta.timeout,
56-
CertsRefreshInterval: pluginMeta.certsRefreshInterval,
57-
Audience: pluginMeta.audience,
58-
EmailsRegex: pluginMeta.emailsRegex,
59-
}
60-
61-
tokenInfo, err := googleidinfo.NewTokenInfo(opts)
12+
tokenInfo, err := new(googleidinfo.Factory).New(os.Args[1:])
6213
if err != nil {
63-
logrus.Errorf("cannot initialize googleid-info provider: %v", err)
14+
logrus.Errorf("cannot initialize google-id-info provider: %v", err)
6415
os.Exit(1)
6516
}
6617

cmd/plugin-googleid-provider/main.go

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,17 @@
11
package main
22

33
import (
4-
"flag"
54
"github.com/grepplabs/kafka-proxy/pkg/libs/googleid-provider"
65
"github.com/grepplabs/kafka-proxy/plugin/gateway-client/shared"
76
"github.com/hashicorp/go-plugin"
87
"github.com/sirupsen/logrus"
98
"os"
109
)
1110

12-
func (f *pluginMeta) flagSet() *flag.FlagSet {
13-
fs := flag.NewFlagSet("google-id provider settings", flag.ContinueOnError)
14-
return fs
15-
}
16-
17-
type pluginMeta struct {
18-
timeout int
19-
adc bool
20-
21-
credentialsWatch bool
22-
credentialsFile string
23-
targetAudience string
24-
}
25-
2611
func main() {
27-
pluginMeta := &pluginMeta{}
28-
fs := pluginMeta.flagSet()
29-
fs.IntVar(&pluginMeta.timeout, "timeout", 10, "Request timeout in seconds")
30-
fs.BoolVar(&pluginMeta.adc, "adc", false, "Use Google Application Default Credentials instead of ServiceAccount JSON")
31-
fs.StringVar(&pluginMeta.credentialsFile, "credentials-file", "", "Location of the JSON file with the application credentials")
32-
fs.BoolVar(&pluginMeta.credentialsWatch, "credentials-watch", true, "Watch credential for reload")
33-
fs.StringVar(&pluginMeta.targetAudience, "target-audience", "", "URI of audience claim")
34-
35-
fs.Parse(os.Args[1:])
36-
37-
options := googleidprovider.TokenProviderOptions{
38-
Timeout: pluginMeta.timeout,
39-
Adc: pluginMeta.adc,
40-
CredentialsWatch: pluginMeta.credentialsWatch,
41-
CredentialsFile: pluginMeta.credentialsFile,
42-
TargetAudience: pluginMeta.targetAudience,
43-
}
44-
45-
tokenProvider, err := googleidprovider.NewTokenProvider(options)
12+
tokenProvider, err := new(googleidprovider.Factory).New(os.Args[1:])
4613
if err != nil {
47-
logrus.Errorf("cannot initialize googleid-token provider: %v", err)
14+
logrus.Errorf("cannot initialize google-id-token provider: %v", err)
4815
os.Exit(1)
4916
}
5017

pkg/apis/gateway.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ type TokenProvider interface {
1818
GetToken(ctx context.Context, request TokenRequest) (TokenResponse, error)
1919
}
2020

21+
type TokenProviderFactory interface {
22+
New(params []string) (TokenProvider, error)
23+
}
24+
2125
type VerifyRequest struct {
2226
Token string
2327
Params []string
@@ -32,3 +36,7 @@ type TokenInfo interface {
3236
// VerifyToken verifies the providing token and performs authorization. The returned error is only used by the underlying rpc protocol
3337
VerifyToken(ctx context.Context, request VerifyRequest) (VerifyResponse, error)
3438
}
39+
40+
type TokenInfoFactory interface {
41+
New(params []string) (TokenInfo, error)
42+
}

pkg/apis/localauth.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,7 @@ package apis
33
type PasswordAuthenticator interface {
44
Authenticate(username, password string) (bool, int32, error)
55
}
6+
7+
type PasswordAuthenticatorFactory interface {
8+
New(params []string) (PasswordAuthenticator, error)
9+
}

0 commit comments

Comments
 (0)