interfaces/builtin: improve the bluez interface #1078

Merged
merged 19 commits into from Apr 29, 2016

Conversation

Projects
None yet
6 participants
Contributor

zyga commented Apr 26, 2016

No description provided.

Simon Fels and others added some commits Apr 17, 2016

interfaces: bluez: create dbus policy permantenly
If we don't define the dbus policy snippet for the permanent slot bluez
wouldn't start automatically as long as not plug is connected. We want
the bluez service to run even when no connection exists.

Signed-off-by: Simon Fels <simon.fels@canonical.com>
interfaces: bluez: Fix the plug dbus peer label
The AppArmor peer label for the plug side was incorrect. This change
fixes a denial on the bluetoothctl client trying to connect
interfaces: bluez: Allow obexd to claim org.bluez.obexd
Add a missing dbus rule to allow obexd to claim its dbus interface
Add a FIXME comment to the plug-side AppArmor rule
We want to replace the hardcoded peer label with one generated from the
connected plug.
interfaces/builtin: add plug/slot for bluez tests
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/builtin: don't hard-code slot name in connected plug apparmor
This patch replaces the hard-coded peer label (snap.bluez.*) for connected
plugs to the security tag glob for the snap that has the slot. In practice
the content should be exactly the same but now it will not be hard-coded.

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

jdstrand commented on interfaces/builtin/bluez.go in 16228ad Apr 26, 2016

This is not sufficient because it turns into @SLOT_SECURITY_TAG_GLOB@ 'snap.bluez.*'. Granted, it is better than hardcoding but we need this to actually be: 'snap.bluez.' where '' is whatever service in the slot snap's 'apps' is being connected to. Fine to commit as is, but please add a FIXME stating this needs to be snap...

Owner

zyga replied Apr 26, 2016

This would have to be a list of apps because (it's possible) that bluez snap will have many apps bound to this slot. Is that something we're okay with?

jdstrand replied Apr 26, 2016

As for list, yes. Here would be the various patterns as I see them:

  • snap..* # connected to snap, not snap.app
  • snap.. # connected to snap.app
  • snap..{app1,app2,app3} # connected to snap.app1, snap.app2 and snap.app3, but not snap.app4
Owner

zyga replied Apr 26, 2016

Oh, nice, I like {app1,...} approach. I'll update this pull request to use this.

interfaces/builtin: use precise labels for bluez
This patch makes the bluez interface use precise apparmor labels for
dbus communication. From now on, connected plugs will be able to talk to
just those apps that are bound to the bluez slot.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/builtin: specialize apparmor label based on bound apps
This patch specialized the bluez label used for dbus policy based on
how apps are bound to the bluez slot.

If there is exactly one app bound to the slot the label is precise, e.g.
"snap.bluez.bluez". If a subset of apps are bound to the slot (but not a
proper subset) then the label uses alternation, e.g.
"snap.bluez.{app1,app2}". Lastly if all the apps are bound to the bluez
slot then a wildcard is used, e.g. "snap.bluez.*".

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/builtin/bluez.go
}
- fmt.Fprintf(buf, appName)
+ fmt.Fprintf(buf, "}")
+ new = buf.Bytes()
}
@jdstrand

jdstrand Apr 26, 2016

Contributor

All this logic looks fine and I fully agree with the three cases. Personal preference would be to not use a case statement here since to my eyes it isn't as clear (if/else if/else would be clearer to me), but I won't block on that.

Contributor

jdstrand commented Apr 26, 2016

@zyga 's latest commit LGTM wrt AppArmor policy syntax and semantics.

interfaces/builtin/bluez.go
+ var new []byte
+ switch {
+ case len(slot.Apps) == 1:
+ for appName := range slot.Apps {
@mvo5

mvo5 Apr 27, 2016

Collaborator

I see len(slot.Apps) == 1 and then a range slot.Apps. That loop seems to be not required if we know the size is just one? Or am I missing something?

@zyga

zyga Apr 27, 2016

Contributor

How do I get the name of the only key in a map?

interfaces/builtin/bluez.go
+ appNames = append(appNames, appName)
+ }
+ sort.Strings(appNames)
+ for i, appName := range appNames {
@mvo5

mvo5 Apr 27, 2016

Collaborator

I wonder if this is more readable with: fmt.Fprintf(buf, "snap.%s.{%s}",slot.Snap.Name(), strings.Join(appNames, ",")). Or is the loop doing something that I miss that strings.Join() is not doing?

@morphis

morphis Apr 27, 2016

Contributor

We should also move this code somewhere else as I will otherwise copy&paste it to the networkmanager interface where we have to do the same thing.

@zyga

zyga Apr 27, 2016

Contributor

Yes, I was thinking the same thing. I'll move that to dbus support code in the same package.

@zyga

zyga Apr 27, 2016

Contributor

@mvo5 I don't have any preference, I can use join if you think that would look better.

@zyga

zyga Apr 28, 2016

Contributor

Done

zyga added some commits Apr 28, 2016

interfaces/buitlin: add helper slotAppLabelExpr
This patch moves code from the bluez interface to the new helper
function slotAppLabelExpr. The same helper is applicable to network
manager and perhaps other interfaces that are being developed.

Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/builtin: refactor slotAppLabelExpr
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/builtin: further refactor of slotAppLabelExpr
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Contributor

zyga commented Apr 28, 2016

This is ready for final review, I think we can merge it

interfaces/builtin/bluez.go
case interfaces.SecurityAppArmor:
- return bluezConnectedPlugAppArmor, nil
+ old := []byte("@SLOT_SECURITY_TAGS@")
@niemeyer

niemeyer Apr 29, 2016

Contributor

We already have a pattern for replacements. Can we please stick to a single one of those so we're not introducing these for each independent feature? (@foo@ vs. ###FOO### vs. {{foo}} vs ...).

@zyga

zyga Apr 29, 2016

Contributor

Ah, sorry, you are completely right. I guess I was looking at apparmor for too long. I'll fix this quickly.

@zyga

zyga Apr 29, 2016

Contributor

Fixed now

+// - "snap.$snap.$app" if there is exactly one app bound
+// - "snap.$snap.{$app1,...$appN}" if there are some, but not all, apps bound
+// - "snap.$snap.*" if all apps are bound to the slot
+func slotAppLabelExpr(slot *interfaces.Slot) []byte {
@niemeyer

niemeyer Apr 29, 2016

Contributor

Nice!!

interfaces/builtin/utils.go
+ snapName := slot.Snap.Name()
+ if len(slot.Apps) == 1 {
+ for appName := range slot.Apps {
+ return []byte(fmt.Sprintf("snap.%s.%s", snapName, appName))
@niemeyer

niemeyer Apr 29, 2016

Contributor

Let's please use a single bytes.Buffer, and have a single result at the end with return buf.Bytes().

@zyga

zyga Apr 29, 2016

Contributor

Done

zyga added some commits Apr 29, 2016

interfaces/builtin: use ####var### for template variables
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/builtin: use single bytes.Buffer for slotAppLabelExpr
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/builtin: small refactor of slotAppLabelExpr
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/builtin/utils.go
+ }
+ appNames := make([]string, 0, len(slot.Apps))
+ for appName := range slot.Apps {
+ appNames = append(appNames, appName)
@niemeyer

niemeyer Apr 29, 2016

Contributor
buf.WriteByte('{')
for appName := range slot.Apps {
        buf.WriteString(appName)
        buf.WriteByte(',')
}
buf.Truncate(buf.Len()-1)
buf.WriteByte('}')
@zyga

zyga Apr 29, 2016

Contributor

Ha, if you look at the history, this is very similar to what I did earlier :) (see 6e730c1)

@zyga

zyga Apr 29, 2016

Contributor

@niemeyer tweaked but kept the sorted output, I think it's nice to be deterministic in this case.

zyga added some commits Apr 29, 2016

interfaces/builtin: drop strings.Join
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
interfaces/builtin: use Fprintf less often
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@canonical.com>
Contributor

niemeyer commented Apr 29, 2016

LGTM

@zyga zyga merged commit e34e863 into snapcore:master Apr 29, 2016

4 checks passed

Integration tests Success
Details
autopkgtest Success
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage decreased (-0.04%) to 79.436%
Details

@zyga zyga deleted the zyga:bluez-fix-rules branch Dec 12, 2016

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