You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by rx...@apache.org on 2020/08/17 01:47:45 UTC
[pulsar-client-go] branch master updated: Support specified the
oauth2 private key with prefix 'file://' and 'data://' (#343)
This is an automated email from the ASF dual-hosted git repository.
rxl pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-go.git
The following commit(s) were added to refs/heads/master by this push:
new a8204e2 Support specified the oauth2 private key with prefix 'file://' and 'data://' (#343)
a8204e2 is described below
commit a8204e23178bccf2a1945fa92c74e2782377dd5c
Author: Yong Zhang <zh...@gmail.com>
AuthorDate: Mon Aug 17 09:47:38 2020 +0800
Support specified the oauth2 private key with prefix 'file://' and 'data://' (#343)
---
Master Issue: #<xyz>
*Motivation*
Make the oauth2 read the private key can handle with 'file://' schema and 'data://' schema.
---
oauth2/client_credentials_provider.go | 18 +++++++++-
pulsar/internal/auth/oauth2_test.go | 63 ++++++++++++++++++++++++-----------
2 files changed, 61 insertions(+), 20 deletions(-)
diff --git a/oauth2/client_credentials_provider.go b/oauth2/client_credentials_provider.go
index 731b399..78c7228 100644
--- a/oauth2/client_credentials_provider.go
+++ b/oauth2/client_credentials_provider.go
@@ -20,6 +20,12 @@ package oauth2
import (
"encoding/json"
"io/ioutil"
+ "strings"
+)
+
+const (
+ FILE = "file://"
+ DATA = "data://"
)
type KeyFileProvider struct {
@@ -43,7 +49,17 @@ func NewClientCredentialsProviderFromKeyFile(keyFile string) *KeyFileProvider {
var _ ClientCredentialsProvider = &KeyFileProvider{}
func (k *KeyFileProvider) GetClientCredentials() (*KeyFile, error) {
- keyFile, err := ioutil.ReadFile(k.KeyFile)
+ var keyFile []byte
+ var err error
+ switch {
+ case strings.HasPrefix(k.KeyFile, FILE):
+ filename := strings.TrimPrefix(k.KeyFile, FILE)
+ keyFile, err = ioutil.ReadFile(filename)
+ case strings.HasPrefix(k.KeyFile, "data://"):
+ keyFile = []byte(strings.TrimPrefix(k.KeyFile, DATA))
+ default:
+ keyFile, err = ioutil.ReadFile(k.KeyFile)
+ }
if err != nil {
return nil, err
}
diff --git a/pulsar/internal/auth/oauth2_test.go b/pulsar/internal/auth/oauth2_test.go
index f1f9e07..9373aec 100644
--- a/pulsar/internal/auth/oauth2_test.go
+++ b/pulsar/internal/auth/oauth2_test.go
@@ -91,27 +91,52 @@ func TestNewAuthenticationOAuth2WithParams(t *testing.T) {
t.Fatal(errors.Wrap(err, "create mocked key file failed"))
}
- params := map[string]string{
- ConfigParamType: ConfigParamTypeClientCredentials,
- ConfigParamIssuerURL: server.URL,
- ConfigParamClientID: "client-id",
- ConfigParamAudience: "audience",
- ConfigParamKeyFile: kf,
+ testData := []map[string]string{
+ {
+ ConfigParamType: ConfigParamTypeClientCredentials,
+ ConfigParamIssuerURL: server.URL,
+ ConfigParamClientID: "client-id",
+ ConfigParamAudience: "audience",
+ ConfigParamKeyFile: kf,
+ },
+ {
+ ConfigParamType: ConfigParamTypeClientCredentials,
+ ConfigParamIssuerURL: server.URL,
+ ConfigParamClientID: "client-id",
+ ConfigParamAudience: "audience",
+ ConfigParamKeyFile: fmt.Sprintf("file://%s", kf),
+ },
+ {
+ ConfigParamType: ConfigParamTypeClientCredentials,
+ ConfigParamIssuerURL: server.URL,
+ ConfigParamClientID: "client-id",
+ ConfigParamAudience: "audience",
+ ConfigParamKeyFile: "data://" + fmt.Sprintf(`{
+ "type":"resource",
+ "client_id":"client-id",
+ "client_secret":"client-secret",
+ "client_email":"oauth@test.org",
+ "issuer_url":"%s"
+}`, server.URL),
+ },
}
- auth, err := NewAuthenticationOAuth2WithParams(params)
- if err != nil {
- t.Fatal(err)
- }
- err = auth.Init()
- if err != nil {
- t.Fatal(err)
- }
+ for i := range testData {
+ params := testData[i]
+ auth, err := NewAuthenticationOAuth2WithParams(params)
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = auth.Init()
+ if err != nil {
+ t.Fatal(err)
+ }
- token, err := auth.GetData()
- if err != nil {
- t.Fatal(err)
- }
+ token, err := auth.GetData()
+ if err != nil {
+ t.Fatal(err)
+ }
- assert.Equal(t, "token-content", string(token))
+ assert.Equal(t, "token-content", string(token))
+ }
}