Skip to content

Commit e1bc8f9

Browse files
committed
Wrote helper function for getting uid/gid, function for setting user or daemon auth
1 parent ff3bb81 commit e1bc8f9

File tree

3 files changed

+58
-53
lines changed

3 files changed

+58
-53
lines changed

pkg/eosclient/eosgrpc/eosgrpc.go

+24-51
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,10 @@ func (c *Client) initNSRequest(ctx context.Context, auth eosclient.Authorization
247247
// cbox is a sudo'er, so we become the user specified in UID/GID, if it is set
248248
rq.Authkey = c.opt.Authkey
249249

250-
if auth.Role.UID != "" && auth.Role.GID != "" {
251-
uidInt, err := strconv.ParseUint(auth.Role.UID, 10, 64)
252-
if err != nil {
253-
return nil, err
254-
}
255-
gidInt, err := strconv.ParseUint(auth.Role.GID, 10, 64)
256-
if err != nil {
257-
return nil, err
258-
}
259-
rq.Role.Uid = uidInt
260-
rq.Role.Gid = gidInt
250+
uid, gid, err := utils.ExtractUidGid(auth)
251+
if err == nil {
252+
rq.Role.Uid = uid
253+
rq.Role.Gid = gid
261254
}
262255
}
263256

@@ -288,17 +281,10 @@ func (c *Client) initMDRequest(ctx context.Context, auth eosclient.Authorization
288281
// cbox is a sudo'er, so we become the user specified in UID/GID, if it is set
289282
rq.Authkey = c.opt.Authkey
290283

291-
if auth.Role.UID != "" && auth.Role.GID != "" {
292-
uidInt, err := strconv.ParseUint(auth.Role.UID, 10, 64)
293-
if err != nil {
294-
return nil, err
295-
}
296-
gidInt, err := strconv.ParseUint(auth.Role.GID, 10, 64)
297-
if err != nil {
298-
return nil, err
299-
}
300-
rq.Role.Uid = uidInt
301-
rq.Role.Gid = gidInt
284+
uid, gid, err := utils.ExtractUidGid(auth)
285+
if err == nil {
286+
rq.Role.Uid = uid
287+
rq.Role.Gid = gid
302288
}
303289
}
304290

@@ -738,12 +724,13 @@ func (c *Client) GetFileInfoByPath(ctx context.Context, userAuth eosclient.Autho
738724
log := appctx.GetLogger(ctx)
739725
log.Debug().Str("func", "GetFileInfoByPath").Str("uid,gid", userAuth.Role.UID+","+userAuth.Role.GID).Str("path", path).Msg("entering")
740726

741-
daemonAuth := utils.GetDaemonAuth()
727+
// UserAuth may not be sufficient, because the user may not have access to the file
728+
// e.g. in the case of a guest account. So we check if a uid/gid is set, and if not,
729+
// revert to the daemon account
730+
auth := utils.GetEOSAuth(userAuth)
742731

743732
// Initialize the common fields of the MDReq
744-
// We do this as the daemon account, because the user may not have access to the file
745-
// e.g. in the case of a guest account
746-
mdrq, err := c.initMDRequest(ctx, daemonAuth)
733+
mdrq, err := c.initMDRequest(ctx, auth)
747734
if err != nil {
748735
return nil, err
749736
}
@@ -800,7 +787,7 @@ func (c *Client) GetFileInfoByPath(ctx context.Context, userAuth eosclient.Autho
800787
}
801788

802789
log.Info().Str("func", "GetFileInfoByPath").Str("path", path).Uint64("info.Inode", info.Inode).Uint64("size", info.Size).Str("etag", info.ETag).Msg("result")
803-
return c.fixupACLs(ctx, daemonAuth, info), nil
790+
return c.fixupACLs(ctx, auth, info), nil
804791
}
805792

806793
// GetFileInfoByFXID returns the FileInfo by the given file id in hexadecimal.
@@ -986,13 +973,11 @@ func (c *Client) Chown(ctx context.Context, auth, chownAuth eosclient.Authorizat
986973

987974
msg := new(erpc.NSRequest_ChownRequest)
988975
msg.Owner = new(erpc.RoleId)
989-
msg.Owner.Uid, err = strconv.ParseUint(chownAuth.Role.UID, 10, 64)
990-
if err != nil {
991-
return err
992-
}
993-
msg.Owner.Gid, err = strconv.ParseUint(chownAuth.Role.GID, 10, 64)
994-
if err != nil {
995-
return err
976+
977+
uid, gid, err := utils.ExtractUidGid(chownAuth)
978+
if err == nil {
979+
msg.Owner.Uid = uid
980+
msg.Owner.Gid = gid
996981
}
997982

998983
msg.Id = new(erpc.MDId)
@@ -1225,9 +1210,8 @@ func (c *Client) Rename(ctx context.Context, auth eosclient.Authorization, oldPa
12251210
}
12261211

12271212
// List the contents of the directory given by path.
1228-
func (c *Client) List(ctx context.Context, userAuth eosclient.Authorization, dpath string) ([]*eosclient.FileInfo, error) {
1213+
func (c *Client) List(ctx context.Context, auth eosclient.Authorization, dpath string) ([]*eosclient.FileInfo, error) {
12291214
log := appctx.GetLogger(ctx)
1230-
log.Info().Str("func", "List").Str("uid,gid", userAuth.Role.UID+","+userAuth.Role.GID).Str("dpath", dpath).Msg("")
12311215

12321216
// Stuff filename, uid, gid into the FindRequest type
12331217
fdrq := new(erpc.FindRequest)
@@ -1238,23 +1222,12 @@ func (c *Client) List(ctx context.Context, userAuth eosclient.Authorization, dpa
12381222

12391223
fdrq.Role = new(erpc.RoleId)
12401224

1241-
var auth eosclient.Authorization
1242-
if userAuth.Role.UID == "" || userAuth.Role.GID == "" {
1243-
auth = utils.GetDaemonAuth()
1244-
} else {
1245-
auth = userAuth
1246-
}
1247-
1248-
uidInt, err := strconv.ParseUint(auth.Role.UID, 10, 64)
1249-
if err != nil {
1250-
return nil, err
1251-
}
1252-
gidInt, err := strconv.ParseUint(auth.Role.GID, 10, 64)
1225+
uid, gid, err := utils.ExtractUidGid(auth)
12531226
if err != nil {
1254-
return nil, err
1227+
return nil, errors.Wrap(err, "Failed to extract uid/gid from auth")
12551228
}
1256-
fdrq.Role.Uid = uidInt
1257-
fdrq.Role.Gid = gidInt
1229+
fdrq.Role.Uid = uid
1230+
fdrq.Role.Gid = gid
12581231

12591232
fdrq.Authkey = c.opt.Authkey
12601233

pkg/storage/utils/eosfs/eosfs.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1172,10 +1172,11 @@ func (fs *eosfs) listWithNominalHome(ctx context.Context, p string) (finfos []*p
11721172
if err != nil {
11731173
return nil, errors.Wrap(err, "eosfs: no user in ctx")
11741174
}
1175-
auth, err := fs.getUserAuth(ctx, u, fn)
1175+
userAuth, err := fs.getUserAuth(ctx, u, fn)
11761176
if err != nil {
11771177
return nil, err
11781178
}
1179+
auth := utils.GetEOSAuth(userAuth)
11791180

11801181
eosFileInfos, err := fs.c.List(ctx, auth, fn)
11811182
if err != nil {
@@ -1503,10 +1504,12 @@ func (fs *eosfs) ListRevisions(ctx context.Context, ref *provider.Reference) ([]
15031504
return nil, errtypes.PermissionDenied("eosfs: user doesn't have permissions to list revisions")
15041505
}
15051506
} else {
1506-
fn, auth, err = fs.resolveRefAndGetAuth(ctx, ref)
1507+
var userAuth eosclient.Authorization
1508+
fn, userAuth, err = fs.resolveRefAndGetAuth(ctx, ref)
15071509
if err != nil {
15081510
return nil, err
15091511
}
1512+
auth = utils.GetEOSAuth(userAuth)
15101513
}
15111514

15121515
eosRevisions, err := fs.c.ListVersions(ctx, auth, fn)

pkg/utils/utils.go

+29
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"path/filepath"
2929
"reflect"
3030
"regexp"
31+
"strconv"
3132
"strings"
3233
"time"
3334

@@ -456,3 +457,31 @@ func GetDaemonAuth() eosclient.Authorization {
456457
func GetEmptyAuth() eosclient.Authorization {
457458
return eosclient.Authorization{}
458459
}
460+
461+
// Returns the userAuth if this is a valid auth object,
462+
// otherwise returns daemonAuth
463+
func GetEOSAuth(userAuth eosclient.Authorization) eosclient.Authorization {
464+
if userAuth.Role.UID == "" || userAuth.Role.GID == "" {
465+
return GetDaemonAuth()
466+
} else {
467+
return userAuth
468+
}
469+
}
470+
471+
// Extract uid and gid from auth object
472+
func ExtractUidGid(auth eosclient.Authorization) (uid, gid uint64, err error) {
473+
// $ id nobody
474+
// uid=65534(nobody) gid=65534(nobody) groups=65534(nobody)
475+
nobody := uint64(65534)
476+
477+
uid, err = strconv.ParseUint(auth.Role.UID, 10, 64)
478+
if err != nil {
479+
return nobody, nobody, err
480+
}
481+
gid, err = strconv.ParseUint(auth.Role.GID, 10, 64)
482+
if err != nil {
483+
return nobody, nobody, err
484+
}
485+
486+
return uid, gid, nil
487+
}

0 commit comments

Comments
 (0)