Skip to content

Commit

Permalink
Do not try to continue when machine auth fails. DRY up code
Browse files Browse the repository at this point in the history
  • Loading branch information
aduffeck committed May 11, 2022
1 parent 766a20b commit ba249c1
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions extensions/search/pkg/search/provider/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
"github.com/cs3org/reva/v2/pkg/errtypes"
"github.com/cs3org/reva/v2/pkg/events"
"google.golang.org/grpc/metadata"
)
Expand Down Expand Up @@ -120,35 +121,38 @@ func (p *Provider) handleEvent(ev interface{}) {
}

func (p *Provider) statResource(ref *provider.Reference, owner *user.User) (*provider.StatResponse, error) {
// Get auth
ownerCtx := ctxpkg.ContextSetUser(context.Background(), owner)
authRes, err := p.gwClient.Authenticate(ownerCtx, &gateway.AuthenticateRequest{
Type: "machine",
ClientId: "userid:" + owner.Id.OpaqueId,
ClientSecret: p.machineAuthAPIKey,
})
if err != nil || authRes.GetStatus().GetCode() != rpc.Code_CODE_OK {
p.logger.Error().Err(err).Interface("authRes", authRes).Msg("error using machine auth")
ownerCtx, err := p.getAuthContext(owner)
if err != nil {
return nil, err
}
ownerCtx = metadata.AppendToOutgoingContext(ownerCtx, ctxpkg.TokenHeader, authRes.Token)

// Stat changed resource resource
return p.gwClient.Stat(ownerCtx, &provider.StatRequest{Ref: ref})
}

func (p *Provider) getPath(id *provider.ResourceId, owner *user.User) (*provider.GetPathResponse, error) {
// Get auth
ownerCtx, err := p.getAuthContext(owner)
if err != nil {
return nil, err
}

// Stat changed resource resource
return p.gwClient.GetPath(ownerCtx, &provider.GetPathRequest{ResourceId: id})
}

func (p *Provider) getAuthContext(owner *user.User) (context.Context, error) {
ownerCtx := ctxpkg.ContextSetUser(context.Background(), owner)
authRes, err := p.gwClient.Authenticate(ownerCtx, &gateway.AuthenticateRequest{
Type: "machine",
ClientId: "userid:" + owner.Id.OpaqueId,
ClientSecret: p.machineAuthAPIKey,
})
if err != nil || authRes.GetStatus().GetCode() != rpc.Code_CODE_OK {
if err == nil && authRes.GetStatus().GetCode() != rpc.Code_CODE_OK {
err = errtypes.NewErrtypeFromStatus(authRes.Status)
}
if err != nil {
p.logger.Error().Err(err).Interface("authRes", authRes).Msg("error using machine auth")
return nil, err
}
ownerCtx = metadata.AppendToOutgoingContext(ownerCtx, ctxpkg.TokenHeader, authRes.Token)

// Stat changed resource resource
return p.gwClient.GetPath(ownerCtx, &provider.GetPathRequest{ResourceId: id})
return metadata.AppendToOutgoingContext(ownerCtx, ctxpkg.TokenHeader, authRes.Token), nil
}

0 comments on commit ba249c1

Please sign in to comment.