-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
efi: change how HostEnvironment overrides EFI variables
The HostEnvironment interface contains a ReadVar method for abstracting reads of EFI variables. The default environment just calls efi.ReadVariable, with other implementations providing mocked variables. The github.com/canonical/go-efilib package provides a lot more APIs now that read from (and some that write to) EFI variables and some of these will be used from the new efi/preinstall package (these APIs are mostly boot and secure boot related). I wanted a way to be able to override the environment for the preinstall.RunChecks call (particularly as this is very useful for testing), but the existing HostEnvironment approach doesn't work well for this. I initially considered a couple of options: - Make it possible to mock each of the individual calls to go-efilib functions via the HostEnvironment interface. - Export varsBackend from go-efilib and provide a function in to mock the backend by overriding the system one. The first approach scales poorly, so my initial approach was going to be the second one. The problem with this though is that HostEnvironment will be part of a public, production API, and mocking the backend is global to the whole process. I don't think this is appropriate in production code where it might be possible that there may be one goroutine reading EFI variables from the system when another goroutine decides to mock the backend based on a supplied HostEnvironment. I've taken a different approach instead, by modifying every go-efilib function that interacts with EFI variables to accept a context.Context argument. The context contains a VarsBackend keyed from it, and the package exports a DefaultVarContext symbol corresponding to the default system backend (efivarfs if it is detected, or a null backend if it isn't detected). This approach permits individual call sites to override the backend to use for individual functions that interact with EFI variables (not just ReadVariable, WriteVariable and ListVariables, but there's a whole set of extra functions related to secure boot and boot configuration that make use of reading and writing EFI variables). The ReadVar method on the HostEnvironment interface has gone, and a new method that returns a context has been added. The internal.DefaultEnv implementation just returns efi.DefaultVarContext. The MockVars type in internal/efitest has been updated to implement efi.VarsBackend, and the MockHostEnvironment implementation in internal/efitest returns a context that keys to this implementation. This should hopefully allow preinstall.RunChecks to be able to take a HostEnvironment argument and benefit from mocked variables even when interacting with variables indirectly using higher level functions exported from go-efilib.
- Loading branch information
1 parent
9222896
commit aaba2c5
Showing
15 changed files
with
137 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
||
/* | ||
* Copyright (C) 2024 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 internal | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/canonical/tcglog-parser" | ||
) | ||
|
||
// HostEnvironment is an interface that abstracts out an EFI environment, so that | ||
// consumers of the API can provide a custom mechanism to read EFI variables or parse | ||
// the TCG event log. This needs to be kept in sync with [efi.HostEnvironment]. | ||
type HostEnvironment interface { | ||
// VarContext returns a context containing a VarsBackend, keyed by efi.VarsBackendKey, | ||
// for interacting with EFI variables via go-efilib. This context can be passed to any | ||
// go-efilib function that interacts with EFI variables. | ||
VarContext() context.Context | ||
|
||
// ReadEventLog reads the TCG event log | ||
ReadEventLog() (*tcglog.Log, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.