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

adding roles #86

Merged
merged 1 commit into from
Aug 27, 2023
Merged
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
23 changes: 23 additions & 0 deletions access.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,26 @@
func (u *User) Delete() error {
return u.client.Delete(fmt.Sprintf("/access/users/%s", u.UserID), nil)
}

func (c *Client) Role(roleid string) (role Permission, err error) {
err = c.Get(fmt.Sprintf("/access/roles/%s", roleid), &role)
return
}

func (c *Client) Roles() (roles Roles, err error) {
err = c.Get("/access/roles", &roles)
if nil == err {
for _, g := range roles {
g.client = c
}
}
return
}

func (r *Role) Update() error {
return r.client.Put(fmt.Sprintf("/access/roles/%s", r.RoleID), r, nil)

Check warning on line 180 in access.go

View check run for this annotation

Codecov / codecov/patch

access.go#L179-L180

Added lines #L179 - L180 were not covered by tests
}

func (r *Role) Delete() error {
return r.client.Delete(fmt.Sprintf("/access/roles/%s", r.RoleID), nil)

Check warning on line 184 in access.go

View check run for this annotation

Codecov / codecov/patch

access.go#L183-L184

Added lines #L183 - L184 were not covered by tests
}
25 changes: 25 additions & 0 deletions access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,28 @@ func TestUsers(t *testing.T) {
assert.Nil(t, err)
assert.Len(t, users, 4)
}

func TestRole(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
client := mockClient()

u, err := client.Role("Administrator")
assert.Nil(t, err)
assert.Contains(t, u, "SDN.Allocate")
assert.Len(t, u, 38)

u, err = client.Role("NoAccess")
assert.Nil(t, err)
assert.Len(t, u, 0)
}

func TestRoles(t *testing.T) {
mocks.On(mockConfig)
defer mocks.Off()
client := mockClient()

roles, err := client.Roles()
assert.Nil(t, err)
assert.Len(t, roles, 16)
}
141 changes: 141 additions & 0 deletions tests/mocks/pve7x/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,145 @@ func access() {
}
}`)

gock.New(config.C.URI).
Get("^/access/roles/Administrator$").
Reply(200).
JSON(`{
"data": {
"SDN.Allocate": 1,
"Datastore.AllocateSpace": 1,
"Permissions.Modify": 1,
"VM.Audit": 1,
"VM.Snapshot": 1,
"Datastore.Audit": 1,
"VM.Config.Network": 1,
"Pool.Audit": 1,
"SDN.Use": 1,
"Datastore.Allocate": 1,
"VM.Allocate": 1,
"VM.Snapshot.Rollback": 1,
"Sys.Syslog": 1,
"VM.Config.Disk": 1,
"VM.Console": 1,
"VM.Config.CDROM": 1,
"Realm.AllocateUser": 1,
"Sys.Audit": 1,
"Sys.PowerMgmt": 1,
"Sys.Modify": 1,
"VM.Monitor": 1,
"VM.Config.Memory": 1,
"VM.Backup": 1,
"Sys.Incoming": 1,
"VM.Migrate": 1,
"Realm.Allocate": 1,
"VM.Config.CPU": 1,
"User.Modify": 1,
"VM.Config.HWType": 1,
"VM.Clone": 1,
"SDN.Audit": 1,
"VM.Config.Cloudinit": 1,
"Group.Allocate": 1,
"VM.PowerMgmt": 1,
"Sys.Console": 1,
"Datastore.AllocateTemplate": 1,
"Pool.Allocate": 1,
"VM.Config.Options": 1
}
}`)

gock.New(config.C.URI).
Get("^/access/roles/NoAccess").
Reply(200).
JSON(`{
"data": {}
}`)

gock.New(config.C.URI).
Get("^/access/roles$").
Reply(200).
JSON(`{
"data": [
{
"roleid": "PVEVMAdmin",
"privs": "VM.Allocate,VM.Audit,VM.Backup,VM.Clone,VM.Config.CDROM,VM.Config.CPU,VM.Config.Cloudinit,VM.Config.Disk,VM.Config.HWType,VM.Config.Memory,VM.Config.Network,VM.Config.Options,VM.Console,VM.Migrate,VM.Monitor,VM.PowerMgmt,VM.Snapshot,VM.Snapshot.Rollback",
"special": 1
},
{
"roleid": "PVEDatastoreAdmin",
"special": 1,
"privs": "Datastore.Allocate,Datastore.AllocateSpace,Datastore.AllocateTemplate,Datastore.Audit"
},
{
"roleid": "PVEPoolUser",
"privs": "Pool.Audit",
"special": 1
},
{
"special": 1,
"privs": "",
"roleid": "NoAccess"
},
{
"roleid": "PVEAuditor",
"privs": "Datastore.Audit,Pool.Audit,SDN.Audit,Sys.Audit,VM.Audit",
"special": 1
},
{
"privs": "Permissions.Modify,Sys.Audit,Sys.Console,Sys.Syslog",
"special": 1,
"roleid": "PVESysAdmin"
},
{
"special": 1,
"privs": "Datastore.AllocateSpace,Datastore.Audit",
"roleid": "PVEDatastoreUser"
},
{
"roleid": "Administrator",
"special": 1,
"privs": "Datastore.Allocate,Datastore.AllocateSpace,Datastore.AllocateTemplate,Datastore.Audit,Group.Allocate,Permissions.Modify,Pool.Allocate,Pool.Audit,Realm.Allocate,Realm.AllocateUser,SDN.Allocate,SDN.Audit,SDN.Use,Sys.Audit,Sys.Console,Sys.Incoming,Sys.Modify,Sys.PowerMgmt,Sys.Syslog,User.Modify,VM.Allocate,VM.Audit,VM.Backup,VM.Clone,VM.Config.CDROM,VM.Config.CPU,VM.Config.Cloudinit,VM.Config.Disk,VM.Config.HWType,VM.Config.Memory,VM.Config.Network,VM.Config.Options,VM.Console,VM.Migrate,VM.Monitor,VM.PowerMgmt,VM.Snapshot,VM.Snapshot.Rollback"
},
{
"roleid": "PVETemplateUser",
"privs": "VM.Audit,VM.Clone",
"special": 1
},
{
"privs": "SDN.Audit,SDN.Use",
"special": 1,
"roleid": "PVESDNUser"
},
{
"special": 1,
"privs": "Datastore.Allocate,Datastore.AllocateSpace,Datastore.AllocateTemplate,Datastore.Audit,Group.Allocate,Permissions.Modify,Pool.Allocate,Pool.Audit,Realm.AllocateUser,SDN.Allocate,SDN.Audit,SDN.Use,Sys.Audit,Sys.Console,Sys.Syslog,User.Modify,VM.Allocate,VM.Audit,VM.Backup,VM.Clone,VM.Config.CDROM,VM.Config.CPU,VM.Config.Cloudinit,VM.Config.Disk,VM.Config.HWType,VM.Config.Memory,VM.Config.Network,VM.Config.Options,VM.Console,VM.Migrate,VM.Monitor,VM.PowerMgmt,VM.Snapshot,VM.Snapshot.Rollback",
"roleid": "PVEAdmin"
},
{
"roleid": "test",
"privs": "Pool.Audit",
"special": 0
},
{
"special": 1,
"privs": "VM.Audit,VM.Backup,VM.Config.CDROM,VM.Config.Cloudinit,VM.Console,VM.PowerMgmt",
"roleid": "PVEVMUser"
},
{
"special": 1,
"privs": "SDN.Allocate,SDN.Audit,SDN.Use",
"roleid": "PVESDNAdmin"
},
{
"roleid": "PVEUserAdmin",
"privs": "Group.Allocate,Realm.AllocateUser,User.Modify",
"special": 1
},
{
"privs": "Pool.Allocate,Pool.Audit",
"special": 1,
"roleid": "PVEPoolAdmin"
}
]
}`)

}
8 changes: 8 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1021,3 +1021,11 @@ type Token struct {
Expire int `json:"expire,omitempty"`
Privsep IntOrBool `json:"privsep,omitempty"`
}

type Roles []*Role
type Role struct {
client *Client
RoleID string `json:"roleid,omitempty"`
Privs string `json:"privs,omitempty"`
Special IntOrBool `json:"special,omitempty"`
}
Loading