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

engine: resources: file: Add File -> Owner/Group Auto Edges #764

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
49 changes: 48 additions & 1 deletion engine/resources/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,54 @@ func (obj *FileRes) AutoEdges() (engine.AutoEdge, error) {
}) // build list
}

if obj.Owner != "" {
var reversed = true // cheat by passing a pointer
if uid, err := strconv.ParseInt(obj.Owner, 10, 0); err == nil {
var uint32_uid = uint32(uid)
data = append(data, &UserUID{
BaseUID: engine.BaseUID{
Name: obj.Name(),
Kind: obj.Kind(),
Reversed: &reversed,
},
uid: &uint32_uid,
})
} else {
data = append(data, &UserUID{
BaseUID: engine.BaseUID{
Name: obj.Name(),
Kind: obj.Kind(),
Reversed: &reversed,
},
name: obj.Owner,
})
}
}

if obj.Group != "" {
var reversed = true // cheat by passing a pointer
if gid, err := strconv.ParseInt(obj.Group, 10, 0); err == nil {
var uint32_gid = uint32(gid)
data = append(data, &GroupUID{
BaseUID: engine.BaseUID{
Name: obj.Name(),
Kind: obj.Kind(),
Reversed: &reversed,
},
gid: &uint32_gid,
})
} else {
data = append(data, &GroupUID{
BaseUID: engine.BaseUID{
Name: obj.Name(),
Kind: obj.Kind(),
Reversed: &reversed,
},
name: obj.Group,
})
}
}

// Ensure any file or dir fragments come first.
frags := []engine.ResUID{}
for _, frag := range obj.Fragments {
Expand All @@ -1450,7 +1498,6 @@ func (obj *FileRes) AutoEdges() (engine.AutoEdge, error) {
},
path: frag, // what matters
}) // build list

}

return &FileResAutoEdges{
Expand Down
177 changes: 177 additions & 0 deletions engine/resources/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,183 @@ func TestFileAutoEdge1(t *testing.T) {
}
}

func TestFileAutoEdgeOwnerUID(t *testing.T) {
g, err := pgraph.NewGraph("TestGraph")
if err != nil {
t.Errorf("error creating graph: %v", err)
return
}

fname := &FileRes{
Path: "/tmp/user_owned",
Owner: "test_user",
}

fid := &FileRes{
Path: "/tmp/uid_owned",
Owner: "1234",
}

uid := uint32(1234)

user := &UserRes{
UID: &uid,
}

// Name cannot be set directly in the struct definition
user.SetName("test_user")

g.AddVertex(fname, fid, user)

if i := g.NumEdges(); i != 0 {
t.Errorf("should have 0 edges instead of: %d", i)
}

debug := testing.Verbose() // set via the -test.v flag to `go test`
logf := func(format string, v ...interface{}) {
t.Logf("test: "+format, v...)
}
// run artificially without the entire engine
if err := autoedge.AutoEdge(g, debug, logf); err != nil {
t.Errorf("error running autoedges: %v", err)
}

if e := g.FindEdge(user, fname); e == nil {
t.Errorf("should have an edge from user name -> file, got nil")
}

if e := g.FindEdge(user, fid); e == nil {
t.Errorf("should have an edge from uid -> file, got nil")
}
}

func TestFileAutoEdgeGroup(t *testing.T) {
g, err := pgraph.NewGraph("TestGraph")
if err != nil {
t.Errorf("error creating graph: %v", err)
return
}

fname := &FileRes{
Path: "/tmp/group_owned",
Group: "test_group",
}

fid := &FileRes{
Path: "/tmp/gid_owned",
Group: "1234",
}

gid := uint32(1234)

group := &GroupRes{
GID: &gid,
}

// Name cannot be set directly in the struct definition
group.SetName("test_group")

g.AddVertex(fname, fid, group)

if i := g.NumEdges(); i != 0 {
t.Errorf("should have 0 edges instead of: %d", i)
}

debug := testing.Verbose() // set via the -test.v flag to `go test`
logf := func(format string, v ...interface{}) {
t.Logf("test: "+format, v...)
}
// run artificially without the entire engine
if err := autoedge.AutoEdge(g, debug, logf); err != nil {
t.Errorf("error running autoedges: %v", err)
}

if e := g.FindEdge(group, fname); e == nil {
t.Errorf("should have an edge from group name -> file, got nil")
}

if e := g.FindEdge(group, fid); e == nil {
t.Errorf("should have an edge from gid -> file, got nil")
}
}

func TestFileAutoEdgeOwnerGroup(t *testing.T) {
g, err := pgraph.NewGraph("TestGraph")
if err != nil {
t.Errorf("error creating graph: %v", err)
return
}

fname := &FileRes{
Path: "/tmp/name_owned",
Owner: "test_user",
Group: "test_group",
}

fid := &FileRes{
Path: "/tmp/id_owned",
Owner: "1234",
Group: "5678",
}

fnameid := &FileRes{
Path: "/tmp/nameid_owned",
Owner: "1234",
Group: "test_group",
}

fidname := &FileRes{
Path: "/tmp/idname_owned",
Owner: "test_user",
Group: "5678",
}

uid := uint32(1234)

user := &UserRes{
UID: &uid,
}

// Name cannot be set directly in the struct definition
user.SetName("test_user")

gid := uint32(5678)

group := &GroupRes{
GID: &gid,
}

// Name cannot be set directly in the struct definition
group.SetName("test_group")

g.AddVertex(fname, fid, fnameid, fidname, user, group)

if i := g.NumEdges(); i != 0 {
t.Errorf("should have 0 edges instead of: %d", i)
}

debug := testing.Verbose() // set via the -test.v flag to `go test`
logf := func(format string, v ...interface{}) {
t.Logf("test: "+format, v...)
}
// run artificially without the entire engine
if err := autoedge.AutoEdge(g, debug, logf); err != nil {
t.Errorf("error running autoedges: %v", err)
}

resources := []engine.Res{fname, fid, fnameid, fidname}

for _, fres := range resources {
if e := g.FindEdge(user, fres); e == nil {
t.Errorf("should have an edge from user -> file, got nil")
}

if e := g.FindEdge(group, fres); e == nil {
t.Errorf("should have an edge from group -> file, got nil")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I need a bit of help here, the test keeps failing with these edges (group -> file missing) - my guess is I'm adding to data with something that isn't unique enough, but I can't figure it out

Copy link
Owner

Choose a reason for hiding this comment

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

Awesome, thank you! I'll have a look at this within about a day.

Copy link
Owner

Choose a reason for hiding this comment

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

I think I know what the issue is, just confirming now...

}
}
}

func TestMiscEncodeDecode1(t *testing.T) {
var err error

Expand Down
Loading