snapd: generate snap cookies on startup #3491

Merged
merged 8 commits into from Jul 7, 2017

Conversation

Projects
None yet
4 participants
Contributor

stolowski commented Jun 19, 2017

Generate snap cookies for snaps that miss them on snapd startup. Missing cookie files cause a confusing warning from snap-confine.

Normally we generate cookies when installing snaps, but cookies will be missing for snaps already present in the system when upgrading to the new snapd that adds support for running snapctl outside of hooks.

This branch also adjust the error message from snap-confine to clearly state it's a warning, although with the snapd fix from this branch we should never see the warning again under normal conditions.

stolowski added some commits Jun 19, 2017

Looks good, thanks for doing this work. Some small comments inline.

overlord/snapstate/snapmgr.go
+func (m *SnapManager) GenerateCookies(st *state.State) error {
+ var snapNames map[string]*json.RawMessage
+ err := st.Get("snaps", &snapNames)
+ if err != nil && err != state.ErrNoState {
@mvo5

mvo5 Jun 20, 2017

Collaborator

This could be written in a single line: if err := ...; err != nil && err != state.ErrNoState {

@stolowski

stolowski Jun 20, 2017

Contributor

Done.

overlord/snapstate/snapmgr.go
+ }
+
+ var contexts map[string]string
+ err = st.Get("snap-cookies", &contexts)
@mvo5

mvo5 Jun 20, 2017

Collaborator

This could be written in a single line: if err := ...; err != nil {`

@stolowski

stolowski Jun 20, 2017

Contributor

Done.

overlord/snapstate/snapstate_test.go
+ c.Assert(contexts, HasLen, 2)
+
+ var cookie, snapName string
+ for cookie, snapName = range contexts {
@mvo5

mvo5 Jun 20, 2017

Collaborator

contexts is a map, no? So instead of searching, could you just do cookie := contexts["some-snap"]?

@stolowski

stolowski Jun 20, 2017

Contributor

I couldn't quite do that since the map is indexed by cookie value.. However you made me realize I'm reading the cookie file a few lines below, so I can just reorder these checks a little bit and get rid of this loop. Thanks :)

+ echo "Simulate upgrade from old snapd with no cookie support"
+ systemctl stop snapd.{service,socket}
+ rm -rf $COOKIE_FILE
+ jq -c 'del(.data["snap-cookies"])' /var/lib/snapd/state.json > /var/lib/snapd/state.json.new
@mvo5

mvo5 Jun 20, 2017

Collaborator

Nice

stolowski added some commits Jun 20, 2017

codecov-io commented Jun 23, 2017

Codecov Report

Merging #3491 into master will decrease coverage by <.01%.
The diff coverage is 37.5%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #3491      +/-   ##
==========================================
- Coverage   76.78%   76.78%   -0.01%     
==========================================
  Files         379      379              
  Lines       26285    26304      +19     
==========================================
+ Hits        20184    20198      +14     
- Misses       4307     4310       +3     
- Partials     1794     1796       +2
Impacted Files Coverage Δ
overlord/overlord.go 79.26% <0%> (-0.98%) ⬇️
overlord/snapstate/snapmgr.go 79.74% <42.85%> (-1.72%) ⬇️
overlord/ifacestate/helpers.go 65.54% <0%> (ø) ⬆️
interfaces/builtin/network_control.go 100% <0%> (ø) ⬆️
overlord/snapstate/snapstate.go 81.52% <0%> (+0.23%) ⬆️
overlord/assertstate/assertstate.go 81.57% <0%> (+1.05%) ⬆️
cmd/snap/cmd_aliases.go 96% <0%> (+2%) ⬆️

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 2d483fe...600094f. Read the comment docs.

mvo5 approved these changes Jun 23, 2017

Looks good, thank you. One nitpick inside.

overlord/snapstate/snapmgr.go
+ for snap := range snapNames {
+ if _, ok := contexts[snap]; !ok {
+ err := m.createSnapCookie(st, snap)
+ if err != nil {
@mvo5

mvo5 Jun 23, 2017

Collaborator

(nitpick) this could be a single line: if err := m.createSnapCookie(...; err != nil {

@stolowski

stolowski Jun 26, 2017

Contributor

Ok, fixed.

zyga approved these changes Jul 6, 2017

+1, two comments for your consideration

overlord/snapstate/snapmgr.go
+// before the feature of running snapctl outside of hooks was introduced, leading to a warning
+// from snap-confine).
+func (m *SnapManager) GenerateCookies(st *state.State) error {
+ var snapNames map[string]*json.RawMessage
@zyga

zyga Jul 6, 2017

Contributor

Do we need to lock the state here? In case that this is caller's responsibility could we perhaps indicate that in the documentation?

tests/main/snapctl-from-snap/task.yaml
+ echo "Cookie file $COOKIE_FILE is missing"
+ exit 1
+ fi
+ if [ $(stat -c%a $COOKIE_FILE) != "600" ]; then
@zyga

zyga Jul 6, 2017

Contributor

Nitpick: I'd prefer a space before -c and %a

stolowski added some commits Jul 6, 2017

@stolowski stolowski merged commit ed8d3c5 into snapcore:master Jul 7, 2017

7 checks passed

artful-amd64 autopkgtest finished (success)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
xenial-amd64 autopkgtest finished (success)
Details
xenial-i386 autopkgtest finished (success)
Details
xenial-ppc64el autopkgtest finished (success)
Details
yakkety-amd64 autopkgtest finished (success)
Details
zesty-amd64 autopkgtest finished (success)
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment