github.com/anonymouse64/snapd@v0.0.0-20210824153203-04c4c42d842d/asserts/sysdb/sysdb.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2015-2020 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 sysdb supports the system-wide assertion database with ways to open it and to manage the trusted set of assertions founding it.
    21  package sysdb
    22  
    23  import (
    24  	"github.com/snapcore/snapd/asserts"
    25  	"github.com/snapcore/snapd/dirs"
    26  )
    27  
    28  func openDatabaseAt(path string, cfg *asserts.DatabaseConfig) (*asserts.Database, error) {
    29  	bs, err := asserts.OpenFSBackstore(path)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	keypairMgr, err := asserts.OpenFSKeypairManager(path)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	cfg.Backstore = bs
    38  	cfg.KeypairManager = keypairMgr
    39  	return asserts.OpenDatabase(cfg)
    40  }
    41  
    42  // OpenAt opens a system assertion database at the given location with
    43  // the trusted assertions set configured.
    44  func OpenAt(path string) (*asserts.Database, error) {
    45  	cfg := &asserts.DatabaseConfig{
    46  		Trusted:         Trusted(),
    47  		OtherPredefined: Generic(),
    48  	}
    49  	return openDatabaseAt(path, cfg)
    50  }
    51  
    52  // Open opens the system-wide assertion database with the trusted assertions
    53  // set configured.
    54  func Open() (*asserts.Database, error) {
    55  	return OpenAt(dirs.SnapAssertsDBDir)
    56  }