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

[pubsub/jetstream]: allow tls client authentication #1924

Merged
merged 4 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions pubsub/jetstream/jetstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func (js *jetstreamPubSub) Init(metadata pubsub.Metadata) error {
}, func(nonce []byte) ([]byte, error) {
return sigHandler(js.meta.seedKey, nonce)
}))
} else if js.meta.tls_client_cert != "" && js.meta.tls_client_key != "" {
js.l.Debug("Configure nats for tls client authentication")
opts = append(opts, nats.ClientCert(js.meta.tls_client_cert, js.meta.tls_client_key))
}

js.nc, err = nats.Connect(js.meta.natsURL, opts...)
Expand Down
15 changes: 15 additions & 0 deletions pubsub/jetstream/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ import (

type metadata struct {
natsURL string

jwt string
seedKey string

tls_client_cert string
tls_client_key string

name string
durableName string
queueGroupName string
Expand Down Expand Up @@ -55,6 +59,17 @@ func parseMetadata(psm pubsub.Metadata) (metadata, error) {
return metadata{}, fmt.Errorf("missing jwt")
}

m.tls_client_cert = psm.Properties["tls_client_cert"]
m.tls_client_key = psm.Properties["tls_client_key"]

if m.tls_client_cert != "" && m.tls_client_key == "" {
return metadata{}, fmt.Errorf("missing tls client key")
}

if m.tls_client_cert == "" && m.tls_client_key != "" {
return metadata{}, fmt.Errorf("missing tls client cert")
}

if m.name = psm.Properties["name"]; m.name == "" {
m.name = "dapr.io - pubsub.jetstream"
}
Expand Down
36 changes: 36 additions & 0 deletions pubsub/jetstream/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,42 @@ func TestParseMetadata(t *testing.T) {
want: metadata{},
expectErr: true,
},
{
desc: "Invalid metadata with missing tls client key",
input: pubsub.Metadata{
Properties: map[string]string{
"natsURL": "nats://localhost:4222",
"name": "myName",
"durableName": "myDurable",
"queueGroupName": "myQueue",
"startSequence": "1",
"startTime": "1629328511",
"deliverAll": "true",
"flowControl": "true",
"tls_client_cert": "/path/to/tls.pem",
},
},
want: metadata{},
expectErr: true,
},
{
desc: "Invalid metadata with missing tls client client",
input: pubsub.Metadata{
Properties: map[string]string{
"natsURL": "nats://localhost:4222",
"name": "myName",
"durableName": "myDurable",
"queueGroupName": "myQueue",
"startSequence": "1",
"startTime": "1629328511",
"deliverAll": "true",
"flowControl": "true",
"tls_client_key": "/path/to/tls.key",
},
},
want: metadata{},
expectErr: true,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
Expand Down