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

Adding middleware to inject Auth token for internal requests to frontend #4364

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f477bd1
New struct added to dispatcher
iamrodrigo Aug 14, 2021
4f920bf
Merge branch 'master' into adding-middleware-for-frontend
iamrodrigo Aug 14, 2021
348a195
Adding middleware to dispatcher
iamrodrigo Aug 17, 2021
8b39a8a
Fixing test
iamrodrigo Aug 17, 2021
ce6ac31
Merge branch 'master' into adding-middleware-for-frontend
iamrodrigo Aug 17, 2021
60e5ebe
moving auth creation client to common function
iamrodrigo Aug 18, 2021
dcd8197
Fixing conflict
iamrodrigo Aug 18, 2021
2a5d7dc
files modified after running make bins
iamrodrigo Aug 18, 2021
d8f1464
bringing back comment
iamrodrigo Aug 18, 2021
08f03fe
Merge branch 'adding-middleware-for-frontend' of github.com:noiarek/c…
iamrodrigo Aug 18, 2021
2b1c39d
Using private key from current cluster and removing private key
iamrodrigo Aug 20, 2021
3545030
Merge branch 'master' into adding-middleware-for-frontend
iamrodrigo Aug 20, 2021
d309a11
Rolling back changes on gen files
iamrodrigo Aug 20, 2021
301a793
fixing import
iamrodrigo Aug 20, 2021
75e3dcf
Deleting references to privatekey
iamrodrigo Aug 20, 2021
36e20d8
Running make bins with go 1.16.7
iamrodrigo Aug 20, 2021
cd757b6
Revert "Running make bins with go 1.16.7"
iamrodrigo Aug 20, 2021
135fa76
running make fmt
iamrodrigo Aug 20, 2021
a9b119d
removing test and privatekey path
iamrodrigo Aug 20, 2021
2e74730
Updating field in struct to not to use pointer in interface
iamrodrigo Aug 22, 2021
99de104
Fixing conflicts
iamrodrigo Aug 27, 2021
d6fc4f5
fixing import
iamrodrigo Aug 27, 2021
d5ac6f0
Merge branch 'master' into adding-middleware-for-frontend
longquanzheng Aug 27, 2021
b43e960
Merge branch 'master' into adding-middleware-for-frontend
longquanzheng Aug 30, 2021
236c639
fix go.sum
longquanzheng Aug 30, 2021
1e0a9c3
reset .gen
longquanzheng Aug 30, 2021
521254a
fix mock
longquanzheng Aug 30, 2021
5e3b073
reset unrelated changes
longquanzheng Aug 30, 2021
dc95838
reset
longquanzheng Aug 30, 2021
4122cb2
optimize
longquanzheng Aug 30, 2021
75acde7
improve logs
longquanzheng Aug 30, 2021
1898ce8
fix kafka compose file
longquanzheng Aug 30, 2021
de48b9b
Merge branch 'master' into adding-middleware-for-frontend
longquanzheng Aug 30, 2021
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
12 changes: 9 additions & 3 deletions client/clientBean.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (
"sync/atomic"
"time"

"go.uber.org/cadence/internal/common/auth"
clientworker "go.uber.org/cadence/worker"
"go.uber.org/yarpc"
"go.uber.org/yarpc/api/peer"
"go.uber.org/yarpc/api/transport"
Expand Down Expand Up @@ -65,9 +67,13 @@ type (
SetRemoteFrontendClient(cluster string, client frontend.Client)
}

// DispatcherProvider provides a diapatcher to a given address
DispatcherOptions struct {
AuthProvider *auth.AuthorizationProvider
}

// DispatcherProvider provides a dispatcher to a given address
DispatcherProvider interface {
Get(name string, address string) (*yarpc.Dispatcher, error)
Get(name string, address string, options *DispatcherOptions) (*yarpc.Dispatcher, error)
}

clientBeanImpl struct {
Expand Down Expand Up @@ -256,7 +262,7 @@ func NewDNSYarpcDispatcherProvider(logger log.Logger, interval time.Duration) Di
}
}

func (p *dnsDispatcherProvider) Get(serviceName string, address string) (*yarpc.Dispatcher, error) {
func (p *dnsDispatcherProvider) Get(serviceName string, address string, options *DispatcherOptions) (*yarpc.Dispatcher, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I need to run something to get the latest mock for this? Quoting you @longquanzheng

There are two implementation, one is mock. The real one is dnsYarpcDispatcherProvider.

tchanTransport, err := tchannel.NewTransport(
tchannel.ServiceName(serviceName),
// this aim to get rid of the annoying popup about accepting incoming network connections
Expand Down
8 changes: 7 additions & 1 deletion cmd/server/cadence/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"
clientworker "go.uber.org/cadence/worker"

"github.com/uber/cadence/client"
"github.com/uber/cadence/common"
Expand Down Expand Up @@ -220,8 +221,13 @@ func (s *server) startService() common.Daemon {
}
}

dispatcher, err := params.DispatcherProvider.Get(common.FrontendServiceName, s.cfg.PublicClient.HostPort)
// will return empty array if not enabled
privateKey, err := s.cfg.Authorization.OAuthAuthorizer.GetPrivateKey()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like the comment, use clusterMetadata.clusterInfomation..AuthorizationProvider

You may need to create a new wrapper to create AuthProvider based on the config as it will be used in clientBean.go

if err != nil {
log.Fatalf("invalid private key path %s", s.cfg.Authorization.OAuthAuthorizer.JwtCredentials.PrivateKey)
}
authProvider := clientworker.NewJwtAuthorizationProvider(privateKey)
dispatcher, err := params.DispatcherProvider.Get(common.FrontendServiceName, s.cfg.PublicClient.HostPort, &client.DispatcherOptions{AuthProvider: &authProvider}); if err != nil {
log.Fatalf("failed to construct dispatcher: %v", err)
}
params.PublicClient = workflowserviceclient.New(dispatcher.ClientConfig(common.FrontendServiceName))
Expand Down
25 changes: 25 additions & 0 deletions common/config/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package config

import (
"fmt"
"io/ioutil"

"github.com/cristalhq/jwt/v3"
)
Expand Down Expand Up @@ -58,3 +59,27 @@ func (a *Authorization) validateOAuth() error {
}
return nil
}

func (o *OAuthAuthorizer) GetPrivateKey() ([]byte, error) {
if !o.Enable || len(o.JwtCredentials.PrivateKeyLoaded) > 0 {
return o.JwtCredentials.PrivateKeyLoaded, nil
}
var err error
o.JwtCredentials.PrivateKeyLoaded, err = ioutil.ReadFile(o.JwtCredentials.PrivateKey)
if err != nil {
return nil, fmt.Errorf("invalid private key path %s", o.JwtCredentials.PrivateKey)
}
return o.JwtCredentials.PrivateKeyLoaded, nil
}

func (o *OAuthAuthorizer) GetPublicKey() ([]byte, error) {
if !o.Enable || len(o.JwtCredentials.PublicKeyLoaded) > 0{
return o.JwtCredentials.PublicKeyLoaded, nil
}
var err error
o.JwtCredentials.PublicKeyLoaded, err = ioutil.ReadFile(o.JwtCredentials.PublicKey)
if err != nil {
return nil, fmt.Errorf("invalid public key path %s", o.JwtCredentials.PublicKey)
}
return o.JwtCredentials.PublicKeyLoaded, nil
}
6 changes: 6 additions & 0 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ type (
Algorithm string `yaml:"algorithm"`
// Public Key Path for verifying JWT token passed in from external clients
PublicKey string `yaml:"publicKey"`
// Public Key loaded once
PublicKeyLoaded []byte `yaml:"-"`
// Private Key Path for creating JWT token
PrivateKey string `yaml:"privateKey"`
// Private Key loaded once
PrivateKeyLoaded []byte `yaml:"-"`
iamrodrigo marked this conversation as resolved.
Show resolved Hide resolved
}

// Service contains the service specific config items
Expand Down Expand Up @@ -321,6 +325,8 @@ type (
RPCName string `yaml:"rpcName"`
// Address indicate the remote service address(Host:Port). Host can be DNS name.
RPCAddress string `yaml:"rpcAddress"`
// Private Key Path
PrivateKey string `yaml:"privateKey"`
iamrodrigo marked this conversation as resolved.
Show resolved Hide resolved
}

// DCRedirectionPolicy contains the frontend datacenter redirection policy
Expand Down
1 change: 1 addition & 0 deletions common/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func (h *serviceImpl) Start() {
client.NewRPCClientFactory(h.rpcFactory, h.membershipMonitor, h.metricsClient, h.dynamicCollection, h.numberOfHistoryShards, h.logger),
h.dispatcherProvider,
h.clusterMetadata,

)
if err != nil {
h.logger.WithTags(tag.Error(err)).Fatal("fail to initialize client bean")
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/benbjohnson/clock v0.0.0-20161215174838-7dc76406b6d3 // indirect
github.com/cactus/go-statsd-client/statsd v0.0.0-20191106001114-12b4e2b38748
github.com/cch123/elasticsql v0.0.0-20190321073543-a1a440758eb9
github.com/cristalhq/jwt/v3 v3.0.14
github.com/cristalhq/jwt/v3 v3.1.0
github.com/davecgh/go-spew v1.1.1
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13
github.com/dmarkham/enumer v1.5.1
Expand All @@ -22,7 +22,7 @@ require (
github.com/fatih/color v1.10.0
github.com/go-sql-driver/mysql v1.5.0
github.com/gocql/gocql v0.0.0-20191126110522-1982a06ad6b9
github.com/gogo/protobuf v1.3.1
github.com/gogo/protobuf v1.3.2
github.com/golang/mock v1.4.4
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/go-cmp v0.5.4 // indirect
Expand Down Expand Up @@ -60,12 +60,12 @@ require (
github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
go.opencensus.io v0.22.5 // indirect
go.uber.org/atomic v1.7.0
go.uber.org/cadence v0.17.1-0.20210609205819-61495d91ff9d
go.uber.org/cadence v0.17.1-0.20210806184645-7c70757e7c7f
go.uber.org/config v1.4.0
go.uber.org/fx v1.10.0
go.uber.org/fx v1.13.1
go.uber.org/multierr v1.6.0
go.uber.org/thriftrw v1.25.0
go.uber.org/yarpc v1.53.2
go.uber.org/yarpc v1.55.0
go.uber.org/zap v1.13.0
golang.org/x/net v0.0.0-20201031054903-ff519b6c9102
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
Expand Down
22 changes: 14 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/cristalhq/jwt/v3 v3.0.14 h1:u/QujUAmvKZbvSKEgHc5vQznGIJ/sUYW0cWQYuxNcVs=
github.com/cristalhq/jwt/v3 v3.0.14/go.mod h1:XOnIXst8ozq/esy5N1XOlSyQqBd+84fxJ99FK+1jgL8=
github.com/cristalhq/jwt/v3 v3.1.0 h1:iLeL9VzB0SCtjCy9Kg53rMwTcrNm+GHyVcz2eUujz6s=
github.com/cristalhq/jwt/v3 v3.1.0/go.mod h1:XOnIXst8ozq/esy5N1XOlSyQqBd+84fxJ99FK+1jgL8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -137,8 +137,9 @@ github.com/gogo/googleapis v1.3.2 h1:kX1es4djPJrsDhY7aZKJy7aZasdcB5oSOEphMjSB53c
github.com/gogo/googleapis v1.3.2/go.mod h1:5YRNX2z1oM5gXdAkurHa942MDgEJyk02w4OecKY87+c=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls=
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/gogo/status v1.1.0 h1:+eIkrewn5q6b30y+g/BJINVVdi2xH7je5MPJ3ZPK3JA=
github.com/gogo/status v1.1.0/go.mod h1:BFv9nrluPLmrS0EmGVvLaPNmRosr9KapBYd5/hpY1WM=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
Expand Down Expand Up @@ -230,8 +231,9 @@ github.com/jstemmer/go-junit-report v0.9.1 h1:6QPYqodiu3GuPL+7mfx+NwDdp2eTkp9IfE
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/jung-kurt/gofpdf v1.0.3-0.20190309125859-24315acbbda5/go.mod h1:7Id9E/uU8ce6rXgefFLlgrJj/GYY22cpxn+r32jIOes=
github.com/kisielk/errcheck v1.2.0 h1:reN85Pxc5larApoH1keMBiu2GWtPqXQ1nc9gx+jOU+E=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0 h1:e8esj/e4R+SAOwFwN+n3zr0nYeCyeweozKfO23MvHzY=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s=
Expand Down Expand Up @@ -413,14 +415,16 @@ go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.5.1/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/cadence v0.17.1-0.20210609205819-61495d91ff9d h1:y7vc87iC3d+1hLUAe5LhmW5nsOe9CyW/WqsQ5volgo0=
go.uber.org/cadence v0.17.1-0.20210609205819-61495d91ff9d/go.mod h1:WNw63zcxMVCzEUU6cFOFuMh9s//jbc57kr2IfH1cyfs=
go.uber.org/cadence v0.17.1-0.20210806184645-7c70757e7c7f h1:A8R/NsyX9nHkvFB62MXQsGY6k+ClQpwWvpcUKwpdtpQ=
go.uber.org/cadence v0.17.1-0.20210806184645-7c70757e7c7f/go.mod h1:sGTCtpVbS/CSJtiEwi/a2dhhUvJ7hCloBUyVA7LzkZg=
go.uber.org/config v1.4.0 h1:upnMPpMm6WlbZtXoasNkK4f0FhxwS+W4Iqz5oNznehQ=
go.uber.org/config v1.4.0/go.mod h1:aCyrMHmUAc/s2h9sv1koP84M9ZF/4K+g2oleyESO/Ig=
go.uber.org/dig v1.8.0 h1:1rR6hnL/bu1EVcjnRDN5kx1vbIjEJDTGhSQ2B3ddpcI=
go.uber.org/dig v1.8.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
go.uber.org/fx v1.10.0 h1:S2K/H8oNied0Je/mLKdWzEWKZfv9jtxSDm8CnwK+5Fg=
go.uber.org/dig v1.10.0 h1:yLmDDj9/zuDjv3gz8GQGviXMs9TfysIUMUilCpgzUJY=
go.uber.org/dig v1.10.0/go.mod h1:X34SnWGr8Fyla9zQNO2GSO2D+TIuqB14OS8JhYocIyw=
go.uber.org/fx v1.10.0/go.mod h1:vLRicqpG/qQEzno4SYU86iCwfT95EZza+Eba0ItuxqY=
go.uber.org/fx v1.13.1 h1:CFNTr1oin5OJ0VCZ8EycL3wzF29Jz2g0xe55RFsf2a4=
go.uber.org/fx v1.13.1/go.mod h1:bREWhavnedxpJeTq9pQT53BbvwhUv7TcpsOqcH4a+3w=
go.uber.org/goleak v0.10.0/go.mod h1:VCZuO8V8mFPlL0F5J5GK1rtHV3DrFcQ1R8ryq7FK0aI=
go.uber.org/goleak v1.0.0 h1:qsup4IcBdlmsnGfqyLl4Ntn3C2XCCuKAE7DwHpScyUo=
go.uber.org/goleak v1.0.0/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
Expand Down Expand Up @@ -616,6 +620,8 @@ golang.org/x/tools v0.0.0-20200312045724-11d5b4c81c7d/go.mod h1:o4KQGtdN14AW+yjs
golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8=
golang.org/x/tools v0.0.0-20200409170454-77362c5149f0/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200522201501-cb1345f3a375/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
4 changes: 2 additions & 2 deletions host/onebox.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func (c *cadenceImpl) startHistory(
integrationClient := newIntegrationConfigClient(dynamicconfig.NewNopClient())
c.overrideHistoryDynamicConfig(integrationClient)
params.DynamicConfig = integrationClient
dispatcher, err := params.DispatcherProvider.Get(common.FrontendServiceName, c.FrontendAddress())
dispatcher, err := params.DispatcherProvider.Get(common.FrontendServiceName, c.FrontendAddress(), nil)
if err != nil {
c.logger.Fatal("Failed to get dispatcher for history", tag.Error(err))
}
Expand Down Expand Up @@ -590,7 +590,7 @@ func (c *cadenceImpl) startWorker(hosts map[string][]string, startWG *sync.WaitG
c.logger.Fatal("Failed to copy persistence config for worker", tag.Error(err))
}

dispatcher, err := params.DispatcherProvider.Get(common.FrontendServiceName, c.FrontendAddress())
dispatcher, err := params.DispatcherProvider.Get(common.FrontendServiceName, c.FrontendAddress(), nil)
if err != nil {
c.logger.Fatal("Failed to get dispatcher for worker", tag.Error(err))
}
Expand Down