Implement auth2 serviceaccount in the client, upgrade auth2

main v0.0.2
René Jochum 2 years ago
parent 23ac2eff14
commit 3966399285
Signed by: jochum
GPG Key ID: F7D906F5E51E8E5E

@ -13,7 +13,7 @@ require (
github.com/urfave/cli/v2 v2.16.3
go-micro.dev/v4 v4.8.1
google.golang.org/protobuf v1.28.1
jochum.dev/jo-micro/auth2 v0.5.5
jochum.dev/jo-micro/auth2 v0.5.6
jochum.dev/jo-micro/buncomponent v0.0.7
jochum.dev/jo-micro/components v0.3.2
jochum.dev/jo-micro/logruscomponent v0.0.5

@ -1919,6 +1919,8 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
jochum.dev/jo-micro/auth2 v0.5.5 h1:M3o8gye0TROScFyyubCyMhd7dvOblcILbmX7hn+YC2A=
jochum.dev/jo-micro/auth2 v0.5.5/go.mod h1:SIgJ4EXjhX/H/IsG6ZGP6+WvJGAFmPCHyHe35TixlBU=
jochum.dev/jo-micro/auth2 v0.5.6 h1:XfEqhSex6guh9ogfGpRoYjW+RjEW8dAK2u0mHdqVZ28=
jochum.dev/jo-micro/auth2 v0.5.6/go.mod h1:6+SpPpiER7v4WQRWT2oMq3mnXDXqQM7wIJEl7vmrnMQ=
jochum.dev/jo-micro/buncomponent v0.0.7 h1:IGiT/8NrUbUImfZL/MgfYIjW5FVl/mAij5ciUliMBA8=
jochum.dev/jo-micro/buncomponent v0.0.7/go.mod h1:7ppDQR0pY2uXmYaHHbjz09O5gUIfXo/tdoZ9dWyfFas=
jochum.dev/jo-micro/components v0.3.2 h1:Z6Od76Uh2C2+bKhfZvaDLbry8vWGe4Ie/rDfrObE1pg=

@ -10,6 +10,7 @@ import (
"github.com/google/uuid"
"github.com/urfave/cli/v2"
"jochum.dev/jo-micro/auth2"
"jochum.dev/jo-micro/components"
"jochum.dev/jo-micro/settings/cmd/microsettingsd/config"
"jochum.dev/jo-micro/settings/proto/settingspb"
@ -50,15 +51,24 @@ func MustReg(cReg *components.Registry) *Handler {
return cReg.Must(Name).(*Handler)
}
func (h *Handler) sClient() (settingspb.SettingsService, error) {
func (h *Handler) client(ctx context.Context) (settingspb.SettingsService, context.Context, error) {
// Wait until the service is here
_, err := utils.ServiceRetryGet(h.cReg.Service(), config.Name, 10)
if err != nil {
return nil, err
return nil, ctx, err
}
service := settingspb.NewSettingsService(config.Name, h.cReg.Service().Client())
return service, nil
// Optional Service Account
if err := auth2.RegHasClientAuth(h.cReg); err == nil {
ctx, err = auth2.ClientAuthMustReg(h.cReg).Plugin().ServiceContext(ctx)
if err != nil {
return nil, ctx, err
}
}
return service, ctx, nil
}
@ -97,7 +107,7 @@ func (c *Handler) Initialized() bool {
func (h *Handler) Init(cReg *components.Registry, cli *cli.Context) error {
if h.initialized {
return nil
return errors.InternalServerError("ALREADY_INITIALIZED", "already initialized")
}
h.cReg = cReg
@ -111,15 +121,15 @@ func (h *Handler) Stop() error {
return nil
}
func (c *Handler) Health(context context.Context) error {
if !c.Initialized() {
return errors.InternalServerError("NOT_INITIALIZED", "Not initialized")
func (h *Handler) Health(context context.Context) error {
if !h.Initialized() {
return errors.InternalServerError("NOT_INITIALIZED", "not initialized")
}
return nil
}
func (c *Handler) Get(ctx context.Context, id, ownerId, service, name string) (*Setting, error) {
func (h *Handler) Get(ctx context.Context, id, ownerId, service, name string) (*Setting, error) {
// Build the request
req := &settingspb.GetRequest{}
cacheKey := ""
@ -147,14 +157,14 @@ func (c *Handler) Get(ctx context.Context, id, ownerId, service, name string) (*
}
// Check cache and return from cache
c.cacheGetLock.RLock()
if result, ok := c.cacheGet[cacheKey]; ok {
c.cacheGetLock.RUnlock()
h.cacheGetLock.RLock()
if result, ok := h.cacheGet[cacheKey]; ok {
h.cacheGetLock.RUnlock()
return result, nil
}
c.cacheGetLock.RUnlock()
h.cacheGetLock.RUnlock()
client, err := c.sClient()
client, ctx, err := h.client(ctx)
if err != nil {
return nil, err
}
@ -170,14 +180,14 @@ func (c *Handler) Get(ctx context.Context, id, ownerId, service, name string) (*
}
// Store the result in cache
c.cacheGetLock.Lock()
c.cacheGet[cacheKey] = cResult
c.cacheGetLock.Unlock()
h.cacheGetLock.Lock()
h.cacheGet[cacheKey] = cResult
h.cacheGetLock.Unlock()
return cResult, nil
}
func (c *Handler) List(ctx context.Context, id, ownerId, service, name string) ([]*Setting, error) {
func (h *Handler) List(ctx context.Context, id, ownerId, service, name string) ([]*Setting, error) {
// Build the request
req := &settingspb.ListRequest{}
cacheKey := ""
@ -205,15 +215,15 @@ func (c *Handler) List(ctx context.Context, id, ownerId, service, name string) (
}
// Check cache and return from cache
c.cacheListLock.RLock()
if result, ok := c.cacheList[cacheKey]; ok {
c.cacheListLock.RUnlock()
h.cacheListLock.RLock()
if result, ok := h.cacheList[cacheKey]; ok {
h.cacheListLock.RUnlock()
return result, nil
}
c.cacheListLock.RUnlock()
h.cacheListLock.RUnlock()
// Fetch
client, err := c.sClient()
client, ctx, err := h.client(ctx)
if err != nil {
return nil, err
}
@ -232,16 +242,16 @@ func (c *Handler) List(ctx context.Context, id, ownerId, service, name string) (
}
// Store the result in cache
c.cacheListLock.Lock()
c.cacheList[cacheKey] = cResult
c.cacheListLock.Unlock()
h.cacheListLock.Lock()
h.cacheList[cacheKey] = cResult
h.cacheListLock.Unlock()
return cResult, nil
}
func (c *Handler) Create(ctx context.Context, req CreateRequest) (*Setting, error) {
func (h *Handler) Create(ctx context.Context, req CreateRequest) (*Setting, error) {
// Create
client, err := c.sClient()
client, ctx, err := h.client(ctx)
if err != nil {
return nil, err
}
@ -262,9 +272,9 @@ func (c *Handler) Create(ctx context.Context, req CreateRequest) (*Setting, erro
return serviceToClient(result)
}
func (c *Handler) Update(ctx context.Context, req UpdateRequest) (*Setting, error) {
func (h *Handler) Update(ctx context.Context, req UpdateRequest) (*Setting, error) {
// Update
client, err := c.sClient()
client, ctx, err := h.client(ctx)
if err != nil {
return nil, err
}
@ -281,9 +291,9 @@ func (c *Handler) Update(ctx context.Context, req UpdateRequest) (*Setting, erro
return serviceToClient(result)
}
func (c *Handler) Upsert(ctx context.Context, req *UpsertRequest) (*Setting, error) {
func (h *Handler) Upsert(ctx context.Context, req *UpsertRequest) (*Setting, error) {
// Upsert
client, err := c.sClient()
client, ctx, err := h.client(ctx)
if err != nil {
return nil, err
}

Loading…
Cancel
Save