1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Package snapasserts offers helpers to handle snap related assertions and their checking for installation.
package snapasserts
import (
"fmt"
"github.com/snapcore/snapd/asserts"
"github.com/snapcore/snapd/release"
"github.com/snapcore/snapd/snap"
)
type Finder interface {
// Find an assertion based on arbitrary headers. Provided
// headers must contain the primary key for the assertion
// type. It returns a asserts.NotFoundError if the assertion
// cannot be found.
Find(assertionType *asserts.AssertionType, headers map[string]string) (asserts.Assertion, error)
}
func findSnapDeclaration(snapID, name string, db Finder) (*asserts.SnapDeclaration, error) {
a, err := db.Find(asserts.SnapDeclarationType, map[string]string{
"series": release.Series,
"snap-id": snapID,
})
if err != nil {
return nil, fmt.Errorf("internal error: cannot find snap declaration for %q: %s", name, snapID)
}
snapDecl := a.(*asserts.SnapDeclaration)
if snapDecl.SnapName() == "" {
return nil, fmt.Errorf("cannot install snap %q with a revoked snap declaration", name)
}
return snapDecl, nil
}
// CrossCheck tries to cross check the instance name, hash digest and size of a snap plus its metadata in a SideInfo with the relevant snap assertions in a database that should have been populated with them.
func CrossCheck(instanceName, snapSHA3_384 string, snapSize uint64, si *snap.SideInfo, db Finder) error {
// get relevant assertions and do cross checks
a, err := db.Find(asserts.SnapRevisionType, map[string]string{
"snap-sha3-384": snapSHA3_384,
})
if err != nil {
return fmt.Errorf("internal error: cannot find pre-populated snap-revision assertion for %q: %s", instanceName, snapSHA3_384)
}
snapRev := a.(*asserts.SnapRevision)
if snapRev.SnapSize() != snapSize {
return fmt.Errorf("snap %q file does not have expected size according to signatures (download is broken or tampered): %d != %d", instanceName, snapSize, snapRev.SnapSize())
}
snapID := si.SnapID
if snapRev.SnapID() != snapID || snapRev.SnapRevision() != si.Revision.N {
return fmt.Errorf("snap %q does not have expected ID or revision according to assertions (metadata is broken or tampered): %s / %s != %d / %s", instanceName, si.Revision, snapID, snapRev.SnapRevision(), snapRev.SnapID())
}
snapDecl, err := findSnapDeclaration(snapID, instanceName, db)
if err != nil {
return err
}
if snapDecl.SnapName() != snap.InstanceSnap(instanceName) {
return fmt.Errorf("cannot install %q, snap %q is undergoing a rename to %q", instanceName, snap.InstanceSnap(instanceName), snapDecl.SnapName())
}
return nil
}
// DeriveSideInfo tries to construct a SideInfo for the given snap using its digest to find the relevant snap assertions with the information in the given database. It will fail with an asserts.NotFoundError if it cannot find them.
func DeriveSideInfo(snapPath string, db Finder) (*snap.SideInfo, error) {
snapSHA3_384, snapSize, err := asserts.SnapFileSHA3_384(snapPath)
if err != nil {
return nil, err
}
// get relevant assertions and reconstruct metadata
a, err := db.Find(asserts.SnapRevisionType, map[string]string{
"snap-sha3-384": snapSHA3_384,
})
if err != nil {
return nil, err
}
snapRev := a.(*asserts.SnapRevision)
if snapRev.SnapSize() != snapSize {
return nil, fmt.Errorf("snap %q does not have expected size according to signatures (broken or tampered): %d != %d", snapPath, snapSize, snapRev.SnapSize())
}
snapID := snapRev.SnapID()
snapDecl, err := findSnapDeclaration(snapID, snapPath, db)
if err != nil {
return nil, err
}
return SideInfoFromSnapAssertions(snapDecl, snapRev), nil
}
// SideInfoFromSnapAssertions returns a *snap.SideInfo reflecting the given snap assertions.
func SideInfoFromSnapAssertions(snapDecl *asserts.SnapDeclaration, snapRev *asserts.SnapRevision) *snap.SideInfo {
return &snap.SideInfo{
RealName: snapDecl.SnapName(),
SnapID: snapDecl.SnapID(),
Revision: snap.R(snapRev.SnapRevision()),
}
}
// FetchSnapAssertions fetches the assertions matching the snap file digest using the given fetcher.
func FetchSnapAssertions(f asserts.Fetcher, snapSHA3_384 string) error {
// for now starting from the snap-revision will get us all other relevant assertions
ref := &asserts.Ref{
Type: asserts.SnapRevisionType,
PrimaryKey: []string{snapSHA3_384},
}
return f.Fetch(ref)
}
// FetchSnapDeclaration fetches the snap declaration and its prerequisites for the given snap id using the given fetcher.
func FetchSnapDeclaration(f asserts.Fetcher, snapID string) error {
ref := &asserts.Ref{
Type: asserts.SnapDeclarationType,
PrimaryKey: []string{release.Series, snapID},
}
return f.Fetch(ref)
}
// FetchStore fetches the store assertion and its prerequisites for the given store id using the given fetcher.
func FetchStore(f asserts.Fetcher, storeID string) error {
ref := &asserts.Ref{
Type: asserts.StoreType,
PrimaryKey: []string{storeID},
}
return f.Fetch(ref)
}
|