skills: add basic grant-revoke methods #374

Merged
merged 14 commits into from Feb 1, 2016

Conversation

Projects
None yet
3 participants
Contributor

zyga commented Jan 25, 2016

This branch adds the four basic grant / revoke methods

Repository.Grant() grants a skill to a slot.
Repository.Revoke() revokes an existing grant.
Repository.GrantedTo() returns information about all skills granted to particular slots in a given snap.
Repository.GrantedBy() returns information about all skills, from a particular snap, granted to various slots in various snaps.

There are several related internal changes. Removing used skills or
occupied slots is no longer allowed and now returns an error.

Internally tests were tweaked to ensure that test skill and test slot
don't belong to the same snap. This helps in making asymmetric tests for
granted-to vs granted-by easier to follow.

skills: add basic grant / revoke methods
This patch adds the four basic grant / revoke methods:

Repository.Grant() grants a skill to a slot.
Repository.Revoke() revokes an existing grant.
Repository.GrantedTo() returns information about all skills granted to
particular slots in a given snap.
Repository.GrantedBy() returns information about all skills, from a
particular snap, granted to various slots in various snaps.

There are several related internal changes. Removing used skills or
occupied slots is no longer allowed and now returns an error.

Internally tests were tweaked to ensure that test skill and test slot
don't belong to the same snap. This helps in making asymmetric tests for
granted-to vs granted-by easier to follow.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>

zyga added some commits Jan 26, 2016

skills: use fmt.Errorf-style errors
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
skills: use maps to model granted skills
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
skills: differentiate skill and slot names in test data
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
skills: use shorter snap names in test data
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
skills/repo.go
+ skills map[string]map[string]*Skill
+ slots map[string]map[string]*Slot
+ skillUsedBySlot map[*Slot]map[*Skill]bool
+ slotsUsingSkill map[*Skill]map[*Slot]bool
@niemeyer

niemeyer Jan 29, 2016

Contributor

skillUsedBySlot => slotSkills
slotsUsingSkill => skillSlots

skills/repo.go
+ }
+ // Ensure that skill and slot are compatible
+ if slot.Type != skill.Type {
+ return fmt.Errorf("cannot grant skill %q from snap %q to slot %q from snap %q, skill type %q doesn't match slot type %q",
@niemeyer

niemeyer Jan 29, 2016

Contributor

In these cases I think we should go short-hand, with ":":

cannot grant skill "%s:%s" (skill type %q) to "%s:%s" (skill type %q)

The names were validated, so we can trust them to not contain funky chars.

skills/repo.go
+ }
+ // Ensure that slot and skill are not connected yet
+ if r.skillUsedBySlot[slot][skill] {
+ return fmt.Errorf("cannot grant skill %q from snap %q to slot %q from snap %q twice",
@niemeyer

niemeyer Jan 29, 2016

Contributor

This should probably not be an error. The exact semantics the function call requested are already in place, so there's no reason to blow it up I think. We should just return nil, and the call sites will be happy that the state they want is active.

skills/repo.go
+ skillName, skillSnapName, slotName, slotSnapName)
+ }
+ delete(r.skillUsedBySlot[slot], skill)
+ delete(r.slotsUsingSkill[skill], slot)
@niemeyer

niemeyer Jan 29, 2016

Contributor

As in skill and slot removals, the base maps themselves should have the keys deleted once the respective value is empty, so they don't grow out of bounds.

Contributor

niemeyer commented Jan 29, 2016

LGTM once you're happy about these points.

zyga added some commits Feb 1, 2016

skills: s/skillsUsedBySlot/slotSkills/g
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
skills: s/slotsUsingSkill/skillSlots/g
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
skills: tweak error message when trying to grant incompatible type
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
skills: remove unused grant maps
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
skills: don't error on duplicate grants
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Contributor

zyga commented Feb 1, 2016

Refreshed, please have a 2nd look.

Member

chipaca commented Feb 1, 2016

👍

zyga added a commit that referenced this pull request Feb 1, 2016

Merge pull request #374 from zyga/skills-repo-grantrevoke
skills: add basic grant-revoke methods

@zyga zyga merged commit 8233127 into snapcore:master Feb 1, 2016

2 of 3 checks passed

Integration tests 10 tests run, 0 skipped, 10 failed.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.2%) to 69.627%
Details

@zyga zyga deleted the zyga:skills-repo-grantrevoke branch Mar 8, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment