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

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017-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
    21  
    22  import (
    23  	"fmt"
    24  
    25  	"github.com/snapcore/snapd/asserts"
    26  	"github.com/snapcore/snapd/snapdenv"
    27  )
    28  
    29  const (
    30  	encodedGenericAccount = `type: account
    31  authority-id: canonical
    32  account-id: generic
    33  display-name: Generic
    34  timestamp: 2017-07-27T00:00:00.0Z
    35  username: generic
    36  validation: certified
    37  sign-key-sha3-384: -CvQKAwRQ5h3Ffn10FILJoEZUXOv6km9FwA80-Rcj-f-6jadQ89VRswHNiEB9Lxk
    38  
    39  AcLDXAQAAQoABgUCWYuVIgAKCRDUpVvql9g3II66IACcoxSoX8+PQLa9TNuNBUs3bdTW6V5ZOdE8
    40  vnziIg+yqu3qYfWHcRf1qu7K9Igv5lH3uM5jh2AHlndaoX4Qg1Rm9rOZCkRr1dDUmdRDBXN2pdTA
    41  oydd0Ivpeai4ATbSZs11h50/vN/mxBwM6TzdGHqRNt6lvygAPe7VtfchSW/J0NsSIHr9SUeuIHkJ
    42  C79DV27B+9/m8pnpKJo/Fv8nKGs4sMduKVjrj9Po3UhpZEQWf3I3SeDI5IE4TgoDe+O7neGUtT6W
    43  D9wnMWLphC+rHbJguxXG/fmnUYiM2U8o4WVrs/fjF0zDRH7rY3tbLPbFXf2OD4qfOvS//VLQWeCK
    44  KAgKhwz0d5CqaHyKSplywSvwO/dxlrqOjt39k3EjYxVuNS5UQk/BzPoDZD5maisCFm9JZwqBlWHP
    45  6XTj8rhHSkNAPXezs2ZpVSsdtNYmpLLzWIFsAviuoMjYYDyL6jZrD4RBNrNOvSNQGLezB+eyI5DW
    46  9vr2ppCw8zr49epPvJ4uqj/AILgr52zworl7v/27X67BOSoRMmE4AOnvjSJ8cN6Yt83AuEI4aZbP
    47  DlF2Znqp8o/srtmJ3ZMpsjIsAqVhCeTU6eWXbYfNUlIMSmC6CDwQQzsukU4M6NEwUQbWddiM3iNL
    48  FdeFsBscXg4Qm/0Y3PULriDoct+VpBUhzwVXG+Lj6rjtcX7n1C/7u9i/+WIBJ7jU4FBjwOdgpSCQ
    49  DSCb0PgTM2PfbScFpn3KVYs0kT/Jc40Lpw6CUG9iUIdz5qlJzhbRiuhU8yjEg9q/5lWizAuxcP+P
    50  anNhmNXsme46IJh7WnlzPAVMsToz8bWY01LC3t33pPGlRJo109PMbNK7reMIb4KFiL4Hy7gVmTj9
    51  uydReVBUTZuMLRq1ShAJNScZ+HTpWruLoiC87Rf1++1KakahmtWYCdlJv/JSOyjSh8D9h0GEmqON
    52  lKmzrNgQS8QhLh5uBcITN2Kt1UFGu2o9I8l0TgD5Uh9fG/R/A536fpcvIzOA/lhVttO6P9POwUVv
    53  RIBZ3TpVOSzQ+ADpDexRUouPLPkgUwVBSctcgafMaj/w8DsULvlOYd3Sqq9a+zg6bZr9oPBKutUn
    54  YkIUWmLW1OsdQ2eBS9dFqzJTAOELxNOUq37UGnIrMbk3Vn8hLK+S/+W9XL6WVxzmL1PT9FJZZ41p
    55  KdaFV+mvrTfyoxuzXxkWbMwQkc56Ifn+IojbDwMI4FcTcl4dOeUrlnqwBJmTTwEhLVkYDvzYsVV9
    56  4joFUWhp10JMm3lO+3596m0kYWMhyvGfYnH7QcQ3GtMAz82yRHc1X+seeWyD/aIjlHYNYfaJ5Ogs
    57  VC76lXi7swMtA9jV5FJIGmQufLo9f93NSYxqwpa8
    58  `
    59  
    60  	encodedGenericModelsAccountKey = `type: account-key
    61  authority-id: canonical
    62  public-key-sha3-384: d-JcZF9nD9eBw7bwMnH61x-bklnQOhQud1Is6o_cn2wTj8EYDi9musrIT9z2MdAa
    63  account-id: generic
    64  name: models
    65  since: 2017-07-27T00:00:00.0Z
    66  body-length: 717
    67  sign-key-sha3-384: -CvQKAwRQ5h3Ffn10FILJoEZUXOv6km9FwA80-Rcj-f-6jadQ89VRswHNiEB9Lxk
    68  
    69  AcbBTQRWhcGAARAAoRakbLAMMoPeMz5MLCzAR6ALu/xxP9PuCdkknHH5lJrKE2adFj22DMwjWKj6
    70  0pZU1Ushv4r7eb1NmFfl7a6Pz5ert+O5Qt53feK30+yiZF+Pgsx46SVTGy8QvicxhDhChdJ7ugW2
    71  Vbz8dXDT9gv1E5hLl2BiuxxZHtMMTitO3bCtQcM/YwUeFljZZYd1FwxtgolnA5IUcHomIEQ5Xw6X
    72  dCYGNkVjenb8aLBfi/ZZ84LHQjSbo3b87KP7syeEH2uuFJ2W8ZwGfUCll84gF+lYiLO6BQk8psIR
    73  aRqnPfdjeuYg0ZLhdNV2Gu6GTNYMSrGLJ4vafAoIoMOifeIfK/DjN0XpfUIYwrM3UIvssEaLyE0L
    74  i30PN5bpmmyfj5EDkJj9DqHzBly1kc20ciEtVCwOUijhQr4UjjfPiJFyed1/yndY1z/L85iATcsb
    75  mwAw/wOyHKge/mlVztXV2H8DywcLV8Kbo5/ZZzcdKhDgL9URosQ5bMeYDPWwPsS02exHFl150dpR
    76  p6MmeSCFPiQQjDrM3nWXLv/uemBE1IgX5q2eW6kJbSvRn519O3OrFEs2NBMEgvE3mIvewNlxFbDj
    77  96Oj54Zh3rVtYu/g9yo2Bb2uf9gpOGS6TxrqN3aP5FigZzxkMCGFG8UOOFI7k2eQjMd8va5V8JTZ
    78  ijWZgBjDB1YuQ1MAEQEAAQ==
    79  
    80  AcLDXAQAAQoABgUCWYuU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Ejh1ZC1V8emzfkLzEFYP/qUk1c4NjKsTjj5d463Gq
    94  cn31Mr83tt5l7HWwP8bvTMIj98bOIJapsncGOzPYhs8cjZeOy0Q7EcvHjGRrj26CGWZacT3f0A0e
    95  kb66ocAxV4nH1FDsfn8KdLKFgmSmW6SXkD2nqY94/pommJzUBF6s54DijZMXqHRwIRyPA8ymrCGt
    96  t4shJh7dobC8Tg6RA84Bf9HkeqI97PQYFYMuNX0U59x2s0IQsOAYjH53NIf/jSPC4GDvLs7k+O76
    97  R2PJK1VN6/ckJZAb3Rum5Ak5sbLTpRAVHIAVU1NAjHc5lYUHhxXJmJsbw6Jawb9Xb3T96s+WdD3Y
    98  062upMY95pr0ZPf1tVGgzpcVCEw7yAOw+SkMksx+
    99  `
   100  
   101  	encodedGenericClassicModel = `type: model
   102  authority-id: generic
   103  series: 16
   104  brand-id: generic
   105  model: generic-classic
   106  classic: true
   107  timestamp: 2017-07-27T00:00:00.0Z
   108  sign-key-sha3-384: d-JcZF9nD9eBw7bwMnH61x-bklnQOhQud1Is6o_cn2wTj8EYDi9musrIT9z2MdAa
   109  
   110  AcLBXAQAAQoABgUCWYuXiAAKCRAdLQyY+/mCiST0D/0XGQauzV2bbTEy6DkrR1jlNbI6x8vfIdS8
   111  KvEWYvzOWNhNlVSfwNOkFjs3uMHgCO6/fCg03wGXTyV9D7ZgrMeUzWrYp6EmXk8/LQSaBnff86XO
   112  4/vYyfyvEYavhF0kQ6QGg8Cqr0EaMyw0x9/zWEO/Ll9fH/8nv9qcQq8N4AbebNvNxtGsCmJuXpSe
   113  2rxl3Dw8XarYBmqgcBQhXxRNpa6/AgaTNBpPOTqgNA8ZtmbZwYLuaFjpZP410aJSs+evSKepy/ce
   114  +zTA7RB3384YQVeZDdTudX2fGtuCnBZBAJ+NYlk0t8VFXxyOhyMSXeylSpNSx4pCqmUZRyaf5SDS
   115  g1XxJet4IP0stZH1SfPOwc9oE81/bJlKsb9QIQKQRewvtUCLfe9a6Vy/CYd2elvcWOmeANVrJK0m
   116  nRaz6VBm09RJTuwUT6vNugXSOCeF7W3WN1RHJuex0zw+nP3eCehxFSr33YrVniaA7zGfjXvS8tKx
   117  AINNQB4g2fpfet4na6lPPMYM41WHIHPCMTz/fJQ6dZBSEg6UUZ/GiQhGEfWPBteK7yd9pQ8qB3fj
   118  ER4UvKnR7hcVI26e3NGNkXP5kp0SFCkV5NQs8rzXzokpB7p/V5Pnqp3Km6wu45cU6UiTZFhR2IMT
   119  l+6AMtrS4gDGHktOhwfmOMWqmhvR/INF+TjaWbsB6g==
   120  `
   121  )
   122  
   123  var (
   124  	genericAssertions        []asserts.Assertion
   125  	genericStagingAssertions []asserts.Assertion
   126  	genericExtraAssertions   []asserts.Assertion
   127  
   128  	genericClassicModel         *asserts.Model
   129  	genericStagingClassicModel  *asserts.Model
   130  	genericClassicModelOverride *asserts.Model
   131  )
   132  
   133  func init() {
   134  	genericAccount, err := asserts.Decode([]byte(encodedGenericAccount))
   135  	if err != nil {
   136  		panic(fmt.Sprintf(`cannot decode "generic"'s account: %v`, err))
   137  	}
   138  	genericModelsAccountKey, err := asserts.Decode([]byte(encodedGenericModelsAccountKey))
   139  	if err != nil {
   140  		panic(fmt.Sprintf(`cannot decode "generic"'s "models" account-key: %v`, err))
   141  	}
   142  
   143  	genericAssertions = []asserts.Assertion{genericAccount, genericModelsAccountKey}
   144  
   145  	a, err := asserts.Decode([]byte(encodedGenericClassicModel))
   146  	if err != nil {
   147  		panic(fmt.Sprintf(`cannot decode "generic"'s "generic-classic" model: %v`, err))
   148  	}
   149  	genericClassicModel = a.(*asserts.Model)
   150  }
   151  
   152  // Generic returns a copy of the current set of predefined assertions for the 'generic' authority as used by Open.
   153  func Generic() []asserts.Assertion {
   154  	generic := []asserts.Assertion(nil)
   155  	if !snapdenv.UseStagingStore() {
   156  		generic = append(generic, genericAssertions...)
   157  	} else {
   158  		generic = append(generic, genericStagingAssertions...)
   159  	}
   160  	generic = append(generic, genericExtraAssertions...)
   161  	return generic
   162  }
   163  
   164  // InjectGeneric injects further predefined assertions into the set used Open.
   165  // Returns a restore function to reinstate the previous set. Useful
   166  // for tests or called globally without worrying about restoring.
   167  func InjectGeneric(extra []asserts.Assertion) (restore func()) {
   168  	prev := genericExtraAssertions
   169  	genericExtraAssertions = make([]asserts.Assertion, len(prev)+len(extra))
   170  	copy(genericExtraAssertions, prev)
   171  	copy(genericExtraAssertions[len(prev):], extra)
   172  	return func() {
   173  		genericExtraAssertions = prev
   174  	}
   175  }
   176  
   177  // GenericClassicModel returns the model assertion for the "generic"'s "generic-classic" fallback model.
   178  func GenericClassicModel() *asserts.Model {
   179  	if genericClassicModelOverride != nil {
   180  		return genericClassicModelOverride
   181  	}
   182  	if !snapdenv.UseStagingStore() {
   183  		return genericClassicModel
   184  	} else {
   185  		return genericStagingClassicModel
   186  	}
   187  }
   188  
   189  // MockGenericClassicModel mocks the predefined generic-classic model returned by GenericClassicModel.
   190  func MockGenericClassicModel(mod *asserts.Model) (restore func()) {
   191  	prevOverride := genericClassicModelOverride
   192  	genericClassicModelOverride = mod
   193  	return func() {
   194  		genericClassicModelOverride = prevOverride
   195  	}
   196  }