github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/overlord/assertstate/assertstate.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016-2019 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  // Package assertstate implements the manager and state aspects responsible
    21  // for the enforcement of assertions in the system and manages the system-wide
    22  // assertion database.
    23  package assertstate
    24  
    25  import (
    26  	"fmt"
    27  	"strings"
    28  
    29  	"github.com/snapcore/snapd/asserts"
    30  	"github.com/snapcore/snapd/asserts/snapasserts"
    31  	"github.com/snapcore/snapd/httputil"
    32  	"github.com/snapcore/snapd/logger"
    33  	"github.com/snapcore/snapd/overlord/snapstate"
    34  	"github.com/snapcore/snapd/overlord/state"
    35  	"github.com/snapcore/snapd/release"
    36  	"github.com/snapcore/snapd/snap"
    37  )
    38  
    39  // Add the given assertion to the system assertion database.
    40  func Add(s *state.State, a asserts.Assertion) error {
    41  	// TODO: deal together with asserts itself with (cascading) side effects of possible assertion updates
    42  	return cachedDB(s).Add(a)
    43  }
    44  
    45  // AddBatch adds the given assertion batch to the system assertion database.
    46  func AddBatch(s *state.State, batch *asserts.Batch, opts *asserts.CommitOptions) error {
    47  	return batch.CommitTo(cachedDB(s), opts)
    48  }
    49  
    50  func findError(format string, ref *asserts.Ref, err error) error {
    51  	if asserts.IsNotFound(err) {
    52  		return fmt.Errorf(format, ref)
    53  	} else {
    54  		return fmt.Errorf(format+": %v", ref, err)
    55  	}
    56  }
    57  
    58  // RefreshSnapDeclarations refetches all the current snap declarations and their prerequisites.
    59  func RefreshSnapDeclarations(s *state.State, userID int) error {
    60  	deviceCtx, err := snapstate.DevicePastSeeding(s, nil)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	snapStates, err := snapstate.All(s)
    66  	if err != nil {
    67  		return nil
    68  	}
    69  
    70  	err = bulkRefreshSnapDeclarations(s, snapStates, userID, deviceCtx)
    71  	if err == nil {
    72  		// done
    73  		return nil
    74  	}
    75  	if _, ok := err.(*bulkAssertionFallbackError); !ok {
    76  		// not an error that indicates the server rejecting/failing
    77  		// the bulk request itself
    78  		return err
    79  	}
    80  	logger.Noticef("bulk refresh of snap-declarations failed, falling back to one-by-one assertion fetching: %v", err)
    81  
    82  	modelAs := deviceCtx.Model()
    83  
    84  	fetching := func(f asserts.Fetcher) error {
    85  		for instanceName, snapst := range snapStates {
    86  			sideInfo := snapst.CurrentSideInfo()
    87  			if sideInfo.SnapID == "" {
    88  				continue
    89  			}
    90  			if err := snapasserts.FetchSnapDeclaration(f, sideInfo.SnapID); err != nil {
    91  				if notRetried, ok := err.(*httputil.PersistentNetworkError); ok {
    92  					return notRetried
    93  				}
    94  				return fmt.Errorf("cannot refresh snap-declaration for %q: %v", instanceName, err)
    95  			}
    96  		}
    97  
    98  		// fetch store assertion if available
    99  		if modelAs.Store() != "" {
   100  			err := snapasserts.FetchStore(f, modelAs.Store())
   101  			if err != nil && !asserts.IsNotFound(err) {
   102  				return err
   103  			}
   104  		}
   105  
   106  		return nil
   107  	}
   108  	return doFetch(s, userID, deviceCtx, fetching)
   109  }
   110  
   111  type refreshControlError struct {
   112  	errs []error
   113  }
   114  
   115  func (e *refreshControlError) Error() string {
   116  	if len(e.errs) == 1 {
   117  		return e.errs[0].Error()
   118  	}
   119  	l := []string{""}
   120  	for _, e := range e.errs {
   121  		l = append(l, e.Error())
   122  	}
   123  	return fmt.Sprintf("refresh control errors:%s", strings.Join(l, "\n - "))
   124  }
   125  
   126  // ValidateRefreshes validates the refresh candidate revisions represented by
   127  // the snapInfos, looking for the needed refresh control validation assertions,
   128  // it returns a validated subset in validated and a summary error if not all
   129  // candidates validated. ignoreValidation is a set of snap-instance-names that
   130  // should not be gated.
   131  func ValidateRefreshes(s *state.State, snapInfos []*snap.Info, ignoreValidation map[string]bool, userID int, deviceCtx snapstate.DeviceContext) (validated []*snap.Info, err error) {
   132  	// maps gated snap-ids to gating snap-ids
   133  	controlled := make(map[string][]string)
   134  	// maps gating snap-ids to their snap names
   135  	gatingNames := make(map[string]string)
   136  
   137  	db := DB(s)
   138  	snapStates, err := snapstate.All(s)
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  	for instanceName, snapst := range snapStates {
   143  		info, err := snapst.CurrentInfo()
   144  		if err != nil {
   145  			return nil, err
   146  		}
   147  		if info.SnapID == "" {
   148  			continue
   149  		}
   150  		gatingID := info.SnapID
   151  		if gatingNames[gatingID] != "" {
   152  			continue
   153  		}
   154  		a, err := db.Find(asserts.SnapDeclarationType, map[string]string{
   155  			"series":  release.Series,
   156  			"snap-id": gatingID,
   157  		})
   158  		if err != nil {
   159  			return nil, fmt.Errorf("internal error: cannot find snap declaration for installed snap %q: %v", instanceName, err)
   160  		}
   161  		decl := a.(*asserts.SnapDeclaration)
   162  		control := decl.RefreshControl()
   163  		if len(control) == 0 {
   164  			continue
   165  		}
   166  		gatingNames[gatingID] = decl.SnapName()
   167  		for _, gatedID := range control {
   168  			controlled[gatedID] = append(controlled[gatedID], gatingID)
   169  		}
   170  	}
   171  
   172  	var errs []error
   173  	for _, candInfo := range snapInfos {
   174  		if ignoreValidation[candInfo.InstanceName()] {
   175  			validated = append(validated, candInfo)
   176  			continue
   177  		}
   178  		gatedID := candInfo.SnapID
   179  		gating := controlled[gatedID]
   180  		if len(gating) == 0 { // easy case, no refresh control
   181  			validated = append(validated, candInfo)
   182  			continue
   183  		}
   184  
   185  		var validationRefs []*asserts.Ref
   186  
   187  		fetching := func(f asserts.Fetcher) error {
   188  			for _, gatingID := range gating {
   189  				valref := &asserts.Ref{
   190  					Type:       asserts.ValidationType,
   191  					PrimaryKey: []string{release.Series, gatingID, gatedID, candInfo.Revision.String()},
   192  				}
   193  				err := f.Fetch(valref)
   194  				if notFound, ok := err.(*asserts.NotFoundError); ok && notFound.Type == asserts.ValidationType {
   195  					return fmt.Errorf("no validation by %q", gatingNames[gatingID])
   196  				}
   197  				if err != nil {
   198  					return fmt.Errorf("cannot find validation by %q: %v", gatingNames[gatingID], err)
   199  				}
   200  				validationRefs = append(validationRefs, valref)
   201  			}
   202  			return nil
   203  		}
   204  		err := doFetch(s, userID, deviceCtx, fetching)
   205  		if err != nil {
   206  			errs = append(errs, fmt.Errorf("cannot refresh %q to revision %s: %v", candInfo.InstanceName(), candInfo.Revision, err))
   207  			continue
   208  		}
   209  
   210  		var revoked *asserts.Validation
   211  		for _, valref := range validationRefs {
   212  			a, err := valref.Resolve(db.Find)
   213  			if err != nil {
   214  				return nil, findError("internal error: cannot find just fetched %v", valref, err)
   215  			}
   216  			if val := a.(*asserts.Validation); val.Revoked() {
   217  				revoked = val
   218  				break
   219  			}
   220  		}
   221  		if revoked != nil {
   222  			errs = append(errs, fmt.Errorf("cannot refresh %q to revision %s: validation by %q (id %q) revoked", candInfo.InstanceName(), candInfo.Revision, gatingNames[revoked.SnapID()], revoked.SnapID()))
   223  			continue
   224  		}
   225  
   226  		validated = append(validated, candInfo)
   227  	}
   228  
   229  	if errs != nil {
   230  		return validated, &refreshControlError{errs}
   231  	}
   232  
   233  	return validated, nil
   234  }
   235  
   236  // BaseDeclaration returns the base-declaration assertion with policies governing all snaps.
   237  func BaseDeclaration(s *state.State) (*asserts.BaseDeclaration, error) {
   238  	// TODO: switch keeping this in the DB and have it revisioned/updated
   239  	// via the store
   240  	baseDecl := asserts.BuiltinBaseDeclaration()
   241  	if baseDecl == nil {
   242  		return nil, &asserts.NotFoundError{Type: asserts.BaseDeclarationType}
   243  	}
   244  	return baseDecl, nil
   245  }
   246  
   247  // SnapDeclaration returns the snap-declaration for the given snap-id if it is present in the system assertion database.
   248  func SnapDeclaration(s *state.State, snapID string) (*asserts.SnapDeclaration, error) {
   249  	db := DB(s)
   250  	a, err := db.Find(asserts.SnapDeclarationType, map[string]string{
   251  		"series":  release.Series,
   252  		"snap-id": snapID,
   253  	})
   254  	if err != nil {
   255  		return nil, err
   256  	}
   257  	return a.(*asserts.SnapDeclaration), nil
   258  }
   259  
   260  // Publisher returns the account assertion for publisher of the given snap-id if it is present in the system assertion database.
   261  func Publisher(s *state.State, snapID string) (*asserts.Account, error) {
   262  	db := DB(s)
   263  	a, err := db.Find(asserts.SnapDeclarationType, map[string]string{
   264  		"series":  release.Series,
   265  		"snap-id": snapID,
   266  	})
   267  	if err != nil {
   268  		return nil, err
   269  	}
   270  	snapDecl := a.(*asserts.SnapDeclaration)
   271  	a, err = db.Find(asserts.AccountType, map[string]string{
   272  		"account-id": snapDecl.PublisherID(),
   273  	})
   274  	if err != nil {
   275  		return nil, fmt.Errorf("internal error: cannot find account assertion for the publisher of snap %q: %v", snapDecl.SnapName(), err)
   276  	}
   277  	return a.(*asserts.Account), nil
   278  }
   279  
   280  // Store returns the store assertion with the given name/id if it is
   281  // present in the system assertion database.
   282  func Store(s *state.State, store string) (*asserts.Store, error) {
   283  	db := DB(s)
   284  	a, err := db.Find(asserts.StoreType, map[string]string{
   285  		"store": store,
   286  	})
   287  	if err != nil {
   288  		return nil, err
   289  	}
   290  	return a.(*asserts.Store), nil
   291  }
   292  
   293  // AutoAliases returns the explicit automatic aliases alias=>app mapping for the given installed snap.
   294  func AutoAliases(s *state.State, info *snap.Info) (map[string]string, error) {
   295  	if info.SnapID == "" {
   296  		// without declaration
   297  		return nil, nil
   298  	}
   299  	decl, err := SnapDeclaration(s, info.SnapID)
   300  	if err != nil {
   301  		return nil, fmt.Errorf("internal error: cannot find snap-declaration for installed snap %q: %v", info.InstanceName(), err)
   302  	}
   303  	explicitAliases := decl.Aliases()
   304  	if len(explicitAliases) != 0 {
   305  		return explicitAliases, nil
   306  	}
   307  	// XXX: old header fallback, just to keep edge working while we fix the
   308  	// store, to remove before next release!
   309  	oldAutoAliases := decl.AutoAliases()
   310  	if len(oldAutoAliases) == 0 {
   311  		return nil, nil
   312  	}
   313  	res := make(map[string]string, len(oldAutoAliases))
   314  	for _, alias := range oldAutoAliases {
   315  		app := info.LegacyAliases[alias]
   316  		if app == nil {
   317  			// not a known alias anymore or yet, skip
   318  			continue
   319  
   320  		}
   321  		res[alias] = app.Name
   322  	}
   323  	return res, nil
   324  }
   325  
   326  func delayedCrossMgrInit() {
   327  	// hook validation of refreshes into snapstate logic
   328  	snapstate.ValidateRefreshes = ValidateRefreshes
   329  	// hook auto refresh of assertions into snapstate
   330  	snapstate.AutoRefreshAssertions = AutoRefreshAssertions
   331  	// hook retrieving auto-aliases into snapstate logic
   332  	snapstate.AutoAliases = AutoAliases
   333  }
   334  
   335  // AutoRefreshAssertions tries to refresh all assertions
   336  func AutoRefreshAssertions(s *state.State, userID int) error {
   337  	return RefreshSnapDeclarations(s, userID)
   338  }
   339  
   340  // RefreshValidationSetAssertions tries to refresh all validation set
   341  // assertions.
   342  func RefreshValidationSetAssertions(s *state.State, userID int) error {
   343  	deviceCtx, err := snapstate.DevicePastSeeding(s, nil)
   344  	if err != nil {
   345  		return err
   346  	}
   347  
   348  	vsets, err := ValidationSets(s)
   349  	if err != nil {
   350  		return err
   351  	}
   352  	if len(vsets) == 0 {
   353  		return nil
   354  	}
   355  
   356  	return bulkRefreshValidationSetAsserts(s, vsets, userID, deviceCtx)
   357  }
   358  
   359  // ResolveOptions carries extra options for ValidationSetAssertionForMonitor.
   360  type ResolveOptions struct {
   361  	AllowLocalFallback bool
   362  }
   363  
   364  // ValidationSetAssertionForMonitor tries to fetch or refresh the validation
   365  // set assertion with accountID/name/sequence (sequence is optional) using pool.
   366  // If assertion cannot be fetched but exists locally and opts.AllowLocalFallback
   367  // is set then the local one is returned
   368  func ValidationSetAssertionForMonitor(st *state.State, accountID, name string, sequence int, pinned bool, userID int, opts *ResolveOptions) (as *asserts.ValidationSet, local bool, err error) {
   369  	if opts == nil {
   370  		opts = &ResolveOptions{}
   371  	}
   372  	deviceCtx, err := snapstate.DevicePastSeeding(st, nil)
   373  	if err != nil {
   374  		return nil, false, err
   375  	}
   376  
   377  	var vs asserts.Assertion
   378  	headers := map[string]string{
   379  		"series":     release.Series,
   380  		"account-id": accountID,
   381  		"name":       name,
   382  	}
   383  
   384  	db := cachedDB(st)
   385  
   386  	// try to get existing one from db
   387  	if sequence > 0 {
   388  		headers["sequence"] = fmt.Sprintf("%d", sequence)
   389  		vs, err = db.Find(asserts.ValidationSetType, headers)
   390  	} else {
   391  		// find latest
   392  		vs, err = db.FindSequence(asserts.ValidationSetType, headers, -1, -1)
   393  	}
   394  	if err != nil && !asserts.IsNotFound(err) {
   395  		return nil, false, err
   396  	}
   397  	if err == nil {
   398  		as = vs.(*asserts.ValidationSet)
   399  	}
   400  
   401  	// try to resolve or update with pool
   402  	pool := asserts.NewPool(db, maxGroups)
   403  	atSeq := &asserts.AtSequence{
   404  		Type:        asserts.ValidationSetType,
   405  		SequenceKey: []string{release.Series, accountID, name},
   406  		Sequence:    sequence,
   407  		Pinned:      pinned,
   408  	}
   409  	if as != nil {
   410  		atSeq.Revision = as.Revision()
   411  	} else {
   412  		atSeq.Revision = asserts.RevisionNotKnown
   413  	}
   414  
   415  	// resolve if not found locally, otherwise add for update
   416  	if as == nil {
   417  		if err := pool.AddUnresolvedSequence(atSeq, atSeq.Unique()); err != nil {
   418  			return nil, false, err
   419  		}
   420  	} else {
   421  		atSeq.Sequence = as.Sequence()
   422  		// found locally, try to update
   423  		atSeq.Revision = as.Revision()
   424  		if err := pool.AddSequenceToUpdate(atSeq, atSeq.Unique()); err != nil {
   425  			return nil, false, err
   426  		}
   427  	}
   428  
   429  	if err := resolvePoolNoFallback(st, pool, userID, deviceCtx); err != nil {
   430  		rerr, ok := err.(*resolvePoolError)
   431  		if ok && as != nil && opts.AllowLocalFallback {
   432  			if e := rerr.errors[atSeq.Unique()]; asserts.IsNotFound(e) {
   433  				// fallback: support the scenario of local assertion (snap ack)
   434  				// not available in the store.
   435  				return as, true, nil
   436  			}
   437  		}
   438  		return nil, false, err
   439  	}
   440  
   441  	// fetch the requested assertion again
   442  	if pinned {
   443  		vs, err = db.Find(asserts.ValidationSetType, headers)
   444  	} else {
   445  		vs, err = db.FindSequence(asserts.ValidationSetType, headers, -1, asserts.ValidationSetType.MaxSupportedFormat())
   446  	}
   447  	if err == nil {
   448  		as = vs.(*asserts.ValidationSet)
   449  	}
   450  	return as, false, err
   451  }