Do not try to continue when machine auth fails. DRY up code

This commit is contained in:
André Duffeck
2022-05-11 10:49:34 +02:00
parent 766a20b867
commit ba249c17e7
+21 -17
View File
@@ -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"
)
@@ -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 {
p.logger.Error().Err(err).Interface("authRes", authRes).Msg("error using machine auth")
if err == nil && authRes.GetStatus().GetCode() != rpc.Code_CODE_OK {
err = errtypes.NewErrtypeFromStatus(authRes.Status)
}
ownerCtx = metadata.AppendToOutgoingContext(ownerCtx, ctxpkg.TokenHeader, authRes.Token)
// Stat changed resource resource
return p.gwClient.GetPath(ownerCtx, &provider.GetPathRequest{ResourceId: id})
if err != nil {
p.logger.Error().Err(err).Interface("authRes", authRes).Msg("error using machine auth")
return nil, err
}
return metadata.AppendToOutgoingContext(ownerCtx, ctxpkg.TokenHeader, authRes.Token), nil
}