Skip to content

Commit 53ef2d5

Browse files
author
tomjo
committed
feat: TLS authentication
1 parent 2e56e27 commit 53ef2d5

7 files changed

Lines changed: 128 additions & 5 deletions

File tree

api/v1alpha1/common.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ type PulsarAuthentication struct {
4545

4646
// +optional
4747
OAuth2 *PulsarAuthenticationOAuth2 `json:"oauth2,omitempty"`
48+
49+
// +optional
50+
TLS *PulsarAuthenticationTLS `json:"tls,omitempty"`
4851
}
4952

5053
// PulsarResourceLifeCyclePolicy indicates whether it will keep or delete the resource
@@ -68,6 +71,12 @@ type PulsarAuthenticationOAuth2 struct {
6871
Scope string `json:"scope,omitempty"`
6972
}
7073

74+
// PulsarAuthenticationTLS indicates the parameters which are need by pulsar TLS Authentication
75+
type PulsarAuthenticationTLS struct {
76+
ClientCertificatePath string `json:"clientCertificatePath"`
77+
ClientCertificateKeyPath string `json:"clientCertificateKeyPath"`
78+
}
79+
7180
// IsPulsarResourceReady returns true if resource satisfies with these condition
7281
// 1. The instance is not deleted
7382
// 2. Status ObservedGeneration is equal with meta.ObservedGeneration

api/v1alpha1/pulsarconnection_types.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ type PulsarConnectionSpec struct {
5858
// set when enabling the Geo Replication
5959
// +optional
6060
ClusterName string `json:"clusterName,omitempty"`
61+
62+
// TLSEnableHostnameVerification indicates whether to verify the hostname of the broker.
63+
// Only used when using secure urls.
64+
// +optional
65+
TLSEnableHostnameVerification bool `json:"tlsEnableHostnameVerification,omitempty"`
66+
67+
// TLSAllowInsecureConnection indicates whether to allow insecure connection to the broker.
68+
// +optional
69+
TLSAllowInsecureConnection bool `json:"tlsAllowInsecureConnection,omitempty"`
70+
71+
// TLSTrustCertsFilePath Path for the TLS certificate used to validate the broker endpoint when using TLS.
72+
// +optional
73+
TLSTrustCertsFilePath string `json:"tlsTrustCertsFilePath,omitempty"`
6174
}
6275

6376
// PulsarConnectionStatus defines the observed state of PulsarConnection

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/resource.streamnative.io_pulsarconnections.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ spec:
130130
- issuerEndpoint
131131
- key
132132
type: object
133+
tls:
134+
description: PulsarAuthenticationTLS indicates the parameters
135+
which are need by pulsar TLS Authentication
136+
properties:
137+
clientCertificateKeyPath:
138+
type: string
139+
clientCertificatePath:
140+
type: string
141+
required:
142+
- clientCertificateKeyPath
143+
- clientCertificatePath
144+
type: object
133145
token:
134146
description: ValueOrSecretRef is a string or a secret reference
135147
of the authentication
@@ -167,6 +179,18 @@ spec:
167179
description: ClusterName indicates the local cluster name of the pulsar
168180
cluster. It should set when enabling the Geo Replication
169181
type: string
182+
tlsAllowInsecureConnection:
183+
description: TLSAllowInsecureConnection indicates whether to allow
184+
insecure connection to the broker.
185+
type: boolean
186+
tlsEnableHostnameVerification:
187+
description: TLSEnableHostnameVerification indicates whether to verify
188+
the hostname of the broker. Only used when using secure urls.
189+
type: boolean
190+
tlsTrustCertsFilePath:
191+
description: TLSTrustCertsFilePath Path for the TLS certificate used
192+
to validate the broker endpoint when using TLS.
193+
type: string
170194
type: object
171195
status:
172196
description: PulsarConnectionStatus defines the observed state of PulsarConnection

docs/pulsar_connection.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,24 @@ Other `PulsarConnection` configuration examples:
117117
# Use the keyFile contents as the oauth2 key value
118118
value: {"type":"sn_service_account","client_id":"zvex72oGvFQMBQGZ2ozMxOus2s4tQASJ","client_secret":"60J6fo81j-h69_vVvYvqFOHs2NfOyy6pqGqwIhTgnxpQ7O3UH8PdCbVtdm_SJjIf","client_email":"contoso@sndev.auth.streamnative.cloud","issuer_url":"https://auth.streamnative.cloud"}
119119
120+
* TLS authentication
121+
122+
```yaml
123+
apiVersion: resource.streamnative.io/v1alpha1
124+
kind: PulsarConnection
125+
metadata:
126+
name: test-tls-auth-pulsar-connection
127+
namespace: test
128+
spec:
129+
adminServiceURL: http://test-pulsar-sn-platform-broker.test.svc.cluster.local:8080
130+
brokerServiceURL: pulsar://test-pulsar-sn-platform-broker.test.svc.cluster.local:6650
131+
clusterName: pulsar-cluster
132+
authentication:
133+
tls:
134+
clientCertificateKeyPath: /certs/tls.key
135+
clientCertificatePath: /certs/tls.crt
136+
```
137+
120138
This table lists specifications available for the `PulsarConnection` resource.
121139

122140
| Option | Description | Required or not |
@@ -127,7 +145,9 @@ This table lists specifications available for the `PulsarConnection` resource.
127145
| `brokerServiceSecureURL` | The broker service URL for secure connection to the Pulsar cluster, such as `pulsar+ssl://cluster-broker.test.svc.cluster.local:6651`. This option is required for configuring Geo-replication when TLS is enabled. This option is available for version `0.3.0` or above. | No |
128146
| `adminServiceSecureURL` | The admin service URL for secure connection to the Pulsar cluster, such as `https://cluster-broker.test.svc.cluster.local:443`. This option is available for version `0.3.0` or above. | No |
129147
| `clusterName` | The Pulsar cluster name. You can use the `pulsar-admin clusters list` command to get the Pulsar cluster name. This option is required for configuring Geo-replication. Provided from `0.3.0` | No |
130-
148+
| `tlsAllowInsecureConnection` | A flag that indicates whether to allow insecure connection to the broker. Provided from `0.5.0` | No |
149+
| `tlsEnableHostnameVerification` | A flag that indicates wether hostname verification is enabled. Provided from `0.5.0` | No |
150+
| `tlsTrustCertsFilePath` | The path to the certificate used during hostname verfification. Provided from `0.5.0` | No |
131151

132152
1. Apply the YAML file to create the Pulsar Connection.
133153

pkg/admin/interface.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package admin
1616

1717
import (
18+
"fmt"
1819
"io/ioutil"
1920
"os"
2021
"strings"
@@ -179,6 +180,10 @@ type PulsarAdminConfig struct {
179180
Audience string
180181
Key string
181182
Scope string
183+
184+
// TLS Authentication related configuration
185+
ClientCertificatePath string
186+
ClientCertificateKeyPath string
182187
}
183188

184189
// NewPulsarAdmin initialize a pulsar admin client with configuration
@@ -189,8 +194,10 @@ func NewPulsarAdmin(conf PulsarAdminConfig) (PulsarAdmin, error) {
189194
var adminClient admin.Client
190195

191196
config := &config.Config{
192-
WebServiceURL: conf.WebServiceURL,
193-
TLSAllowInsecureConnection: true,
197+
WebServiceURL: conf.WebServiceURL,
198+
TLSAllowInsecureConnection: conf.TLSAllowInsecureConnection,
199+
TLSEnableHostnameVerification: conf.TLSEnableHostnameVerification,
200+
TLSTrustCertsFilePath: conf.TLSTrustCertsFilePath,
194201
// V2 admin endpoint contains operations for tenant, namespace and topic.
195202
PulsarAPIVersion: config.V2,
196203
}
@@ -228,9 +235,23 @@ func NewPulsarAdmin(conf PulsarAdminConfig) (PulsarAdmin, error) {
228235
if err != nil {
229236
return nil, err
230237
}
231-
} else {
238+
adminClient = admin.NewWithAuthProvider(config, oauthProvider)
239+
} else if conf.Token != "" {
232240
config.Token = conf.Token
233241

242+
adminClient, err = admin.New(config)
243+
if err != nil {
244+
return nil, err
245+
}
246+
} else if conf.ClientCertificatePath != "" {
247+
config.AuthPlugin = auth.TLSPluginName
248+
config.AuthParams = fmt.Sprintf("{\"tlsCertFile\": %q, \"tlsKeyFile\": %q}", conf.ClientCertificatePath, conf.ClientCertificateKeyPath)
249+
250+
adminClient, err = admin.New(config)
251+
if err != nil {
252+
return nil, err
253+
}
254+
} else {
234255
adminClient, err = admin.New(config)
235256
if err != nil {
236257
return nil, err

pkg/connection/reconciler.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,20 @@ func (r *PulsarConnectionReconciler) MakePulsarAdminConfig(ctx context.Context)
197197
return nil, fmt.Errorf("adminServiceURL or adminServiceSecureURL must not be empty")
198198
}
199199

200+
tlsEnableHostnameVerification := r.connection.Spec.TLSEnableHostnameVerification
201+
tlsAllowInsecureConnection := r.connection.Spec.TLSAllowInsecureConnection
202+
tlsTrustCertsFilePath := r.connection.Spec.TLSTrustCertsFilePath
203+
204+
if r.connection.Spec.AdminServiceSecureURL == "" {
205+
tlsEnableHostnameVerification = false
206+
tlsAllowInsecureConnection = true
207+
tlsTrustCertsFilePath = ""
208+
}
200209
cfg := admin.PulsarAdminConfig{
201-
WebServiceURL: r.connection.Spec.AdminServiceURL,
210+
WebServiceURL: r.connection.Spec.AdminServiceURL,
211+
TLSAllowInsecureConnection: tlsAllowInsecureConnection,
212+
TLSEnableHostnameVerification: tlsEnableHostnameVerification,
213+
TLSTrustCertsFilePath: tlsTrustCertsFilePath,
202214
}
203215
hasAuth := false
204216
if authn := r.connection.Spec.Authentication; authn != nil {
@@ -225,6 +237,10 @@ func (r *PulsarConnectionReconciler) MakePulsarAdminConfig(ctx context.Context)
225237
cfg.Key = *value
226238
}
227239
}
240+
if tls := authn.TLS; tls != nil {
241+
cfg.ClientCertificatePath = tls.ClientCertificatePath
242+
cfg.ClientCertificateKeyPath = tls.ClientCertificateKeyPath
243+
}
228244
}
229245
return &cfg, nil
230246
}

0 commit comments

Comments
 (0)