[WIP] interfaces/hooks: expose attrs to the interface API, snapctl enhancements (step #4) #3120

Closed
wants to merge 82 commits into
from

Conversation

Projects
None yet
6 participants
Contributor

stolowski commented Mar 31, 2017

Note: #3810 has the new API in separate PR and should be reviewed first (it will reduce the size of this diff a bit once landed).

  • New ValidatePlug/ValidateSlot methods.
  • make attributes persistent (stored in the state) and expose them to the interfaces API.
  • support complex structures for attributes via snapctl.
  • check attributes set by interface hooks against assertions.

stolowski added some commits Mar 28, 2017

@stolowski stolowski changed the title from [WIP] interfaces: expose attrs to the interface API, snapctl enhancements (step #4) to interfaces: expose attrs to the interface API, snapctl enhancements (step #4) Mar 31, 2017

@stolowski stolowski changed the title from interfaces: expose attrs to the interface API, snapctl enhancements (step #4) to interfaces/hooks: expose attrs to the interface API, snapctl enhancements (step #4) Mar 31, 2017

stolowski added some commits Mar 31, 2017

Removed Validate* methods from interfaces which don't have attributes…
…. Don't define it in core as it's going to be optional.

stolowski and others added some commits Apr 3, 2017

Merge branch 'master' of github.com:snapcore/snapd into iface-hooks-a…
…pi-new

I also ported the joystick interface to the newer APIs

@stolowski stolowski requested a review from niemeyer Apr 12, 2017

stolowski added some commits Apr 12, 2017

interfaces/policy/policy.go
BaseDeclaration *asserts.BaseDeclaration
}
func (connc *ConnectCandidate) plugAttrs() map[string]interface{} {
- return connc.Plug.Attrs
+ return connc.PlugAttrs
@pedronis

pedronis Apr 26, 2017

Contributor

ConnectCandidate is also meant to write easily tests about the correctness of decls, so I think I would prefer this to have a fallback:

if connc.PlugAttrs != nil {
    return connc.PlugAttrs
}
return connc.Plug.Attrs

and then have in policy just a couple of tests that test the new paths

@pedronis

pedronis Apr 26, 2017

Contributor

there's just a couple of places where is used for real in ifacestate so the risk of getting it wrong there assuming good tests there should not be big, either way we would need test to check that those fields are set because if they are left empty we get would get wrong behavior either way

@pedronis

pedronis Apr 27, 2017

Contributor

as I said in the standup with the this change, while we'll need to test the new paths, we can undo the changes to many tests, at least most in policy itself and basedeclaration

interfaces/policy/policy.go
}
func (connc *ConnectCandidate) slotAttrs() map[string]interface{} {
- return connc.Slot.Attrs
+ return connc.SlotAttrs
@pedronis

pedronis Apr 26, 2017

Contributor

same

stolowski added some commits Apr 27, 2017

@@ -62,6 +62,8 @@ plugs:
return &policy.ConnectCandidate{
Plug: plugSnap.Plugs[iface],
Slot: slotSnap.Slots[iface],
+ PlugAttrs: plugSnap.Plugs[iface].Attrs,
+ SlotAttrs: slotSnap.Slots[iface].Attrs,
@pedronis

pedronis Apr 28, 2017

Contributor

this can be undone as well

@stolowski

stolowski Apr 28, 2017

Contributor

Done.

stolowski added some commits Apr 28, 2017

niemeyer added a commit that referenced this pull request May 10, 2017

interfaces: API additions for interface hooks (#3119)
Changes to the interface of interfaces: extended AddConnectedPlug/Slot methods to take slot/plug attributes maps. Added stubs for ValidatePlug/Slot methods which will be called to validate dynamic attributes coming from interface hooks (this is implemented in PR #3120).

The API will be used in a followup PR.
Contributor

zyga commented May 11, 2017

@stolowski the tests are failing on related code, please have a look

Contributor

stolowski commented May 12, 2017

Yes, I'm aware of the test failure; this spread test is abusing content interface and needs some reworking now.

Some comments for your consideration.

interfaces/repo.go
@@ -29,6 +29,12 @@ import (
"github.com/snapcore/snapd/snap"
)
+// InterfaceAttrs is a container for plug and slot attributes of given connection
+type InterfaceAttrs struct {
@zyga

zyga May 16, 2017

Contributor

Maybe this could be just called ConnectionAttrs then?

interfaces/repo.go
@@ -42,17 +48,21 @@ type Repository struct {
// given a plug and a slot, are they connected?
plugSlots map[*Plug]map[*Slot]bool
backends map[SecuritySystem]SecurityBackend
+ // attributes of plugs and slots; the attributes include attribute values from
+ // the yaml and provided at runtime via interface hooks.
+ attributes map[*Plug]map[*Slot]*InterfaceAttrs
@zyga

zyga May 16, 2017

Contributor

Just looking at the layout of this. I wonder if we should be following the slotPlugs and plugSlots so that we can look up information about a connection from either (plug, slot) and (slot, plug) equally. We could actually store a connectionInfo or whatever there so that we could host attributes and know this connection exists in the first place. We already have code managing the two attributes there and we might just re-purpose them for storing connection state.

some high-level, maybe my misunderstanding, comments

@@ -438,7 +448,7 @@ func (r *Repository) ResolveDisconnect(plugSnapName, plugName, slotSnapName, slo
// Connect establishes a connection between a plug and a slot.
// The plug and the slot must have the same interface.
-func (r *Repository) Connect(ref ConnRef) error {
+func (r *Repository) Connect(ref ConnRef, plugAttrs map[string]interface{}, slotAttrs map[string]interface{}) error {
@pedronis

pedronis May 17, 2017

Contributor

shouldn't this or somewhere call Sanitize* on the dynamic attrs, that's the place that can set default value afaiu

also we should remember the issue of not passing nil to methods that can write on attrs

overlord/ifacestate/handlers.go
+ type validateSlot interface {
+ ValidateSlot(slot *interfaces.Slot, slotAttrs map[string]interface{}) error
+ }
+ if validate, ok := iface.(validatePlug); ok {
@pedronis

pedronis May 17, 2017

Contributor

it's strange to do this here, it seems repo should grow some ValidateConnection or something like that

overlord/ifacestate/handlers.go
+func getTaskHookAttributes(task *state.Task) (attrs interfaces.InterfaceAttrs, err error) {
+ attrs.PlugAttrs = make(map[string]interface{})
+ attrs.SlotAttrs = make(map[string]interface{})
+ if err = task.Get("plug-attrs", &attrs.PlugAttrs); err == nil {
@pedronis

pedronis May 18, 2017

Contributor

does plug-attrs contains the original attrs from the yaml + what the hook changed? or just new things? the extra in the arg name to Validate* would make think the latter, but if that's the case the connection check is wrong, that one needs the updated full set or we need to tweak the code in policy a bit differently

stolowski added some commits May 24, 2017

interfaces/policy/helpers.go
@@ -81,15 +81,26 @@ func checkOnClassic(c *asserts.OnClassicConstraint) error {
}
func checkPlugConnectionConstraints1(connc *ConnectCandidate, cstrs *asserts.PlugConnectionConstraints) error {
@pedronis

pedronis May 25, 2017

Contributor

I don't think we really want to change this code like this, way really need to check the final set of attributes, merged as it makes sense, the rules are about attributes as whole, so checking subsets separately doesn't have the same meaning

Member

chipaca commented Jul 10, 2017

What's the state of this PR?

@chipaca chipaca added the Decaying label Jul 10, 2017

Contributor

stolowski commented Jul 10, 2017

@chipaca it's WIP after the recent discussions.

@stolowski stolowski changed the title from interfaces/hooks: expose attrs to the interface API, snapctl enhancements (step #4) to [WIP] interfaces/hooks: expose attrs to the interface API, snapctl enhancements (step #4) Jul 10, 2017

stolowski added some commits Jul 11, 2017

@chipaca chipaca removed the Decaying label Aug 25, 2017

Member

chipaca commented Aug 25, 2017

removed the 'Decaying' label as you're obviously keeping it up to date. Nevertheless if you're not going to be able to fix those tests soon I'd suggest you close this so we don't get used to seeing it on the queue before it's reviewable again.

stolowski added some commits Aug 25, 2017

codecov-io commented Aug 25, 2017

Codecov Report

Merging #3120 into master will decrease coverage by 0.05%.
The diff coverage is 76.61%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3120      +/-   ##
==========================================
- Coverage   75.82%   75.76%   -0.06%     
==========================================
  Files         402      403       +1     
  Lines       34793    35170     +377     
==========================================
+ Hits        26381    26648     +267     
- Misses       6539     6631      +92     
- Partials     1873     1891      +18
Impacted Files Coverage Δ
interfaces/policy/helpers.go 98.73% <ø> (ø) ⬆️
interfaces/ifacetest/spec.go 5.88% <0%> (ø) ⬆️
interfaces/core.go 100% <100%> (ø) ⬆️
interfaces/builtin/content.go 84.17% <100%> (-0.75%) ⬇️
interfaces/builtin/avahi_control.go 100% <100%> (ø) ⬆️
interfaces/builtin/unity7.go 67.85% <100%> (ø) ⬆️
interfaces/builtin/udisks2.go 100% <100%> (ø) ⬆️
interfaces/builtin/i2c.go 73.77% <100%> (+6.42%) ⬆️
interfaces/builtin/network_manager.go 78.04% <100%> (ø) ⬆️
interfaces/builtin/mir.go 75.86% <100%> (ø) ⬆️
... and 52 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 0586e14...2cce3ef. Read the comment docs.

Contributor

niemeyer commented Aug 28, 2017

@stolowski I'm closing this as it has plenty of reviews (unclear which ones are done), it says [WIP] in the summary still, it needs conflict solving, and it has a very dirty history:

image

Please open a new one once it's ready for more attention.

@niemeyer niemeyer closed this Aug 28, 2017

@niemeyer niemeyer removed their request for review Aug 29, 2017

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