Skip to content

Commit 1e7d7b3

Browse files
freeznetnodece
andauthored
support oauth2 with auth params (streamnative#876)
* support oauth2 with auth params * goimport * fix style * fix style * Update pkg/auth/oauth2.go Co-authored-by: Zixuan Liu <[email protected]> * Update pkg/auth/oauth2.go Co-authored-by: Zixuan Liu <[email protected]> * Update pkg/auth/oauth2.go Co-authored-by: Zixuan Liu <[email protected]> * Update pkg/auth/oauth2.go Co-authored-by: Zixuan Liu <[email protected]> Co-authored-by: Zixuan Liu <[email protected]>
1 parent 7a10055 commit 1e7d7b3

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

pkg/auth/auth_provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ func GetAuthProvider(config *common.Config) (Provider, error) {
5252
fallthrough
5353
case TokePluginShortName:
5454
provider, err = NewAuthenticationTokenFromAuthParams(config.AuthParams, defaultTransport)
55+
case OAuth2PluginName:
56+
fallthrough
57+
case OAuth2PluginShortName:
58+
provider, err = NewAuthenticationOAuth2FromAuthParams(config.AuthParams, defaultTransport)
5559
default:
5660
switch {
5761
case len(config.TLSCertFile) > 0 && len(config.TLSKeyFile) > 0:

pkg/auth/oauth2.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package auth
1919

2020
import (
21+
"encoding/json"
2122
"net/http"
2223
"path/filepath"
2324

@@ -32,6 +33,19 @@ import (
3233
xoauth2 "golang.org/x/oauth2"
3334
)
3435

36+
const (
37+
OAuth2PluginName = "org.apache.pulsar.client.impl.auth.oauth2.AuthenticationOAuth2"
38+
OAuth2PluginShortName = "oauth2"
39+
)
40+
41+
type OAuth2ClientCredentials struct {
42+
IssuerURL string `json:"issuerUrl,omitempty"`
43+
Audience string `json:"audience,omitempty"`
44+
Scope string `json:"scope,omitempty"`
45+
PrivateKey string `json:"privateKey,omitempty"`
46+
ClientID string `json:"clientId,omitempty"`
47+
}
48+
3549
type OAuth2Provider struct {
3650
clock clock2.RealClock
3751
issuer oauth2.Issuer
@@ -85,6 +99,18 @@ func NewAuthenticationOAuth2WithDefaultFlow(issuer oauth2.Issuer, keyFile string
8599
return p, p.loadGrant()
86100
}
87101

102+
func NewAuthenticationOAuth2FromAuthParams(encodedAuthParam string,
103+
transport http.RoundTripper) (*OAuth2Provider, error) {
104+
105+
var paramsJSON OAuth2ClientCredentials
106+
err := json.Unmarshal([]byte(encodedAuthParam), &paramsJSON)
107+
if err != nil {
108+
return nil, err
109+
}
110+
return NewAuthenticationOAuth2WithParams(paramsJSON.IssuerURL, paramsJSON.ClientID, paramsJSON.Audience,
111+
paramsJSON.Scope, transport)
112+
}
113+
88114
func NewAuthenticationOAuth2WithParams(
89115
issuerEndpoint,
90116
clientID,

pkg/oauth2/active.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,22 @@ func activateCmd(vc *cmdutils.VerbCmd) {
6262
"The path to the private key file")
6363
set.StringVar(&c.Scope, "scope", c.Scope,
6464
"The OAuth 2.0 scope(s) to request")
65+
set.StringVar(
66+
&c.AuthParams,
67+
"auth-params",
68+
c.AuthParams,
69+
"Authentication parameters are used to configure the OAuth 2.0 provider.\n"+
70+
" OAuth2 example: \"{\"audience\":\"test\",\"issuerUrl\":\"https://sample\","+
71+
"\"privateKey\":\"/mnt/secrets/auth.json\",\"scope\":\"api://default/\"}\"\n")
6572
})
6673
vc.EnableOutputFlagSet()
6774
}
6875

6976
func doActivate(vc *cmdutils.VerbCmd, config *cmdutils.ClusterConfig) error {
77+
config, err := applyClientCredentialsToConfig(config)
78+
if err != nil {
79+
return err
80+
}
7081
if config.KeyFile == "" {
7182
return errors.New("required: key-file")
7283
}

pkg/oauth2/login.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,22 @@ func loginCmd(vc *cmdutils.VerbCmd) {
6363
"The OAuth 2.0 client identifier for pulsarctl")
6464
set.StringVar(&c.Scope, "scope", c.Scope,
6565
"The OAuth 2.0 scope(s) to request")
66+
set.StringVar(
67+
&c.AuthParams,
68+
"auth-params",
69+
c.AuthParams,
70+
"Authentication parameters are used to configure the OAuth 2.0 provider.\n"+
71+
" OAuth2 example: \"{\"audience\":\"test\",\"issuerUrl\":\"https://sample\","+
72+
"\"privateKey\":\"/mnt/secrets/auth.json\",\"scope\":\"api://default/\"}\"\n")
6673
})
6774
vc.EnableOutputFlagSet()
6875
}
6976

7077
func doLogin(vc *cmdutils.VerbCmd, config *cmdutils.ClusterConfig, noRefresh bool) error {
78+
config, err := applyClientCredentialsToConfig(config)
79+
if err != nil {
80+
return err
81+
}
7182
if config.IssuerEndpoint == "" {
7283
return errors.New("required: issuer-endpoint")
7384
}

pkg/oauth2/oauth2.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@
1818
package oauth2
1919

2020
import (
21+
"encoding/json"
22+
2123
"github.com/spf13/cobra"
2224
"github.com/streamnative/pulsarctl/pkg/cmdutils"
2325
)
2426

27+
type ClientCredentials struct {
28+
IssuerURL string `json:"issuerUrl,omitempty"`
29+
Audience string `json:"audience,omitempty"`
30+
Scope string `json:"scope,omitempty"`
31+
PrivateKey string `json:"privateKey,omitempty"`
32+
ClientID string `json:"clientId,omitempty"`
33+
}
34+
2535
func Command(grouping *cmdutils.FlagGrouping) *cobra.Command {
2636
resourceCmd := cmdutils.NewResourceCmd(
2737
"oauth2",
@@ -34,3 +44,19 @@ func Command(grouping *cmdutils.FlagGrouping) *cobra.Command {
3444

3545
return resourceCmd
3646
}
47+
48+
func applyClientCredentialsToConfig(config *cmdutils.ClusterConfig) (*cmdutils.ClusterConfig, error) {
49+
if config.AuthParams != "" && config.KeyFile == "" &&
50+
config.IssuerEndpoint == "" && config.Audience == "" && config.Scope == "" {
51+
var paramsJSON ClientCredentials
52+
err := json.Unmarshal([]byte(config.AuthParams), &paramsJSON)
53+
if err != nil {
54+
return config, err
55+
}
56+
config.IssuerEndpoint = paramsJSON.IssuerURL
57+
config.Audience = paramsJSON.Audience
58+
config.Scope = paramsJSON.Scope
59+
config.KeyFile = paramsJSON.PrivateKey
60+
}
61+
return config, nil
62+
}

0 commit comments

Comments
 (0)