Skip to content

Commit 3bcf35a

Browse files
ericsyhlabuladong
andauthored
feat: support oauth2 scope field (#151)
* feat: support oauth2 scope field Signed-off-by: ericsyh <ericshenyuhao@outlook.com> * restore unnecessary change * update go-client dependency --------- Signed-off-by: ericsyh <ericshenyuhao@outlook.com> Co-authored-by: labuladong <labuladong@foxmail.com>
1 parent 2d3e30b commit 3bcf35a

7 files changed

Lines changed: 20 additions & 7 deletions

File tree

api/v1alpha1/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ type PulsarAuthenticationOAuth2 struct {
6565
ClientID string `json:"clientID"`
6666
Audience string `json:"audience"`
6767
Key ValueOrSecretRef `json:"key"`
68+
Scope string `json:"scope"`
6869
}
6970

7071
// IsPulsarResourceReady returns true if resource satisfies with these condition

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/streamnative/pulsar-resources-operator
33
go 1.19
44

55
require (
6-
github.com/apache/pulsar-client-go v0.9.1-0.20230816081803-fbee610ddcbf
6+
github.com/apache/pulsar-client-go v0.9.1-0.20230925013839-ac9c1a639933
77
github.com/go-logr/logr v1.2.1
88
github.com/onsi/ginkgo v1.16.5
99
github.com/onsi/gomega v1.19.0

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
7272
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
7373
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
7474
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210826220005-b48c857c3a0e/go.mod h1:F7bn7fEU90QkQ3tnmaTx3LTKLEDqnwWODIYppRQ5hnY=
75-
github.com/apache/pulsar-client-go v0.9.1-0.20230816081803-fbee610ddcbf h1:k9hqsKPh5ncKf0e3CkzvBTYXLwCYNYFb1Vtk3qnYAvk=
76-
github.com/apache/pulsar-client-go v0.9.1-0.20230816081803-fbee610ddcbf/go.mod h1:Ea/yiZA7plgiaWRyOuO1B0k5/hjpl1thmiKig+D9PBQ=
75+
github.com/apache/pulsar-client-go v0.9.1-0.20230925013839-ac9c1a639933 h1:lYWPZnfWZZ/OZO0kS/WhNS/vmujPrT9fmuPse3X/ecc=
76+
github.com/apache/pulsar-client-go v0.9.1-0.20230925013839-ac9c1a639933/go.mod h1:Ea/yiZA7plgiaWRyOuO1B0k5/hjpl1thmiKig+D9PBQ=
7777
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
7878
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
7979
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=

pkg/admin/interface.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package admin
1717
import (
1818
"io/ioutil"
1919
"os"
20+
"strings"
2021

2122
"github.com/apache/pulsar-client-go/oauth2"
2223
"github.com/apache/pulsar-client-go/pulsaradmin/pkg/admin"
@@ -177,6 +178,7 @@ type PulsarAdminConfig struct {
177178
ClientID string
178179
Audience string
179180
Key string
181+
Scope string
180182
}
181183

182184
// NewPulsarAdmin initialize a pulsar admin client with configuration
@@ -208,16 +210,24 @@ func NewPulsarAdmin(conf PulsarAdminConfig) (PulsarAdmin, error) {
208210
config.ClientID = conf.ClientID
209211
config.Audience = conf.Audience
210212
config.KeyFile = keyFilePath
213+
config.Scope = conf.Scope
211214

212-
oauthProvider, err := auth.NewAuthenticationOAuth2WithDefaultFlow(oauth2.Issuer{
215+
oauthProvider, err := auth.NewAuthenticationOAuth2WithFlow(oauth2.Issuer{
213216
IssuerEndpoint: conf.IssuerEndpoint,
214217
ClientID: conf.ClientID,
215218
Audience: conf.Audience,
216-
}, keyFilePath)
219+
}, oauth2.ClientCredentialsFlowOptions{
220+
KeyFile: keyFilePath,
221+
AdditionalScopes: strings.Split(conf.Scope, " "),
222+
})
223+
224+
if err != nil {
225+
return nil, err
226+
}
227+
adminClient, err = admin.NewPulsarClientWithAuthProvider(config, oauthProvider)
217228
if err != nil {
218229
return nil, err
219230
}
220-
adminClient = admin.NewWithAuthProvider(config, oauthProvider)
221231
} else {
222232
config.Token = conf.Token
223233

pkg/connection/reconciler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func (r *PulsarConnectionReconciler) MakePulsarAdminConfig(ctx context.Context)
216216
cfg.IssuerEndpoint = oauth2.IssuerEndpoint
217217
cfg.ClientID = oauth2.ClientID
218218
cfg.Audience = oauth2.Audience
219+
cfg.Scope = oauth2.Scope
219220
value, err := GetValue(ctx, r.client, r.connection.Namespace, &oauth2.Key)
220221
if err != nil {
221222
return nil, err

tests/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.18
55
require (
66
// pulsar-client-go latest version is v0.9.0,
77
// it removed the go.mod from oauth2, this will cause the issue of ambiguous import
8-
github.com/apache/pulsar-client-go v0.9.1-0.20230816081803-fbee610ddcbf
8+
github.com/apache/pulsar-client-go v0.9.1-0.20230925013839-ac9c1a639933
99
github.com/onsi/ginkgo/v2 v2.1.3
1010
github.com/onsi/gomega v1.19.0
1111
github.com/sirupsen/logrus v1.8.1

tests/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kd
6868
github.com/apache/pulsar-client-go v0.8.1 h1:UZINLbH3I5YtNzqkju7g9vrl4CKrEgYSx2rbpvGufrE=
6969
github.com/apache/pulsar-client-go v0.8.1/go.mod h1:yJNcvn/IurarFDxwmoZvb2Ieylg630ifxeO/iXpk27I=
7070
github.com/apache/pulsar-client-go v0.9.1-0.20230816081803-fbee610ddcbf/go.mod h1:Ea/yiZA7plgiaWRyOuO1B0k5/hjpl1thmiKig+D9PBQ=
71+
github.com/apache/pulsar-client-go v0.9.1-0.20230925013839-ac9c1a639933/go.mod h1:Ea/yiZA7plgiaWRyOuO1B0k5/hjpl1thmiKig+D9PBQ=
7172
github.com/ardielle/ardielle-go v1.5.2 h1:TilHTpHIQJ27R1Tl/iITBzMwiUGSlVfiVhwDNGM3Zj4=
7273
github.com/ardielle/ardielle-go v1.5.2/go.mod h1:I4hy1n795cUhaVt/ojz83SNVCYIGsAFAONtv2Dr7HUI=
7374
github.com/ardielle/ardielle-tools v1.5.4/go.mod h1:oZN+JRMnqGiIhrzkRN9l26Cej9dEx4jeNG6A+AdkShk=

0 commit comments

Comments
 (0)