-
Notifications
You must be signed in to change notification settings - Fork 22
Add AuthRequestor that can return credentials from systemd creds #523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chrisccoulson
wants to merge
1
commit into
canonical:master
Choose a base branch
from
chrisccoulson:add-sdcreds-auth-requestor
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,124 @@ | ||
| // -*- Mode: Go; indent-tabs-mode: t -*- | ||
|
|
||
| /* | ||
| * Copyright (C) 2026 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 secboot | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| ) | ||
|
|
||
| type systemdCredsRequestUserCredentialContext struct { | ||
| Path string | ||
| CredPath string | ||
| } | ||
|
|
||
| type systemdCredsAuthRequestor struct { | ||
| console io.Writer | ||
| prefix string | ||
| credsDir string | ||
|
|
||
| lastRequestUserCredentialCtx systemdCredsRequestUserCredentialContext | ||
| } | ||
|
|
||
| func (r *systemdCredsAuthRequestor) RequestUserCredential(ctx context.Context, name, path string, authTypes UserAuthType) (string, UserAuthType, error) { | ||
| pathStr := strings.ReplaceAll(strings.TrimPrefix(path, "/"), "/", "-") | ||
|
|
||
| for _, info := range []struct { | ||
| authTypeStr string | ||
| authType UserAuthType | ||
| }{ | ||
| {authTypeStr: "passphrase", authType: UserAuthTypePassphrase}, | ||
| {authTypeStr: "pin", authType: UserAuthTypePIN}, | ||
| {authTypeStr: "recoverykey", authType: UserAuthTypeRecoveryKey}, | ||
| } { | ||
| if info.authType&authTypes == 0 { | ||
| continue | ||
| } | ||
|
|
||
| credPath := filepath.Join(r.credsDir, fmt.Sprintf("%s.%s.%s", r.prefix, pathStr, info.authTypeStr)) | ||
| data, err := os.ReadFile(credPath) | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| continue | ||
| } | ||
| if err != nil { | ||
| return "", 0, fmt.Errorf("cannot read credential from %s: %w", path, err) | ||
| } | ||
|
|
||
| r.lastRequestUserCredentialCtx = systemdCredsRequestUserCredentialContext{ | ||
| Path: path, | ||
| CredPath: credPath, | ||
| } | ||
| return string(data), info.authType, nil | ||
| } | ||
|
|
||
| return "", 0, ErrAuthRequestorNotAvailable | ||
| } | ||
|
|
||
| func (r *systemdCredsAuthRequestor) NotifyUserAuthResult(ctx context.Context, result UserAuthResult, authTypes, exhaustedAuthTypes UserAuthType) error { | ||
| switch result { | ||
| case UserAuthResultFailed: | ||
| fmt.Fprintf(r.console, "Incorrect %s from credential %s for %s\n", formatUserAuthTypeString(authTypes), r.lastRequestUserCredentialCtx.CredPath, r.lastRequestUserCredentialCtx.Path) | ||
| case UserAuthResultInvalidFormat: | ||
| fmt.Fprintf(r.console, "Incorrectly formatted %s from credential %s\n", formatUserAuthTypeString(authTypes), r.lastRequestUserCredentialCtx.CredPath) | ||
| } | ||
|
|
||
| r.lastRequestUserCredentialCtx = systemdCredsRequestUserCredentialContext{} | ||
| return nil | ||
| } | ||
|
|
||
| // NewSystemdCredsAuthRequestor creates an implementation of AuthRequestor that | ||
| // returns credentials from systemd credentials. The console argument is used by | ||
| // the implementation of [AuthRequestor.NotifyUserAuthResult] where result is | ||
| // not [UserAuthResultSuccess]. If not provided, it defaults to [os.Stderr]. | ||
| // The prefix argument can be used to customize the prefix used for looking up | ||
| // credentials. It defaults to "ubuntu-fde" if not provided. | ||
| // | ||
| // Credentials can be provided by using the following format for their name: | ||
| // <prefix>.<path>.<type> | ||
| // ... where <path> is the path with the leading separator removed and remaining | ||
| // separators replaced with '-', and <type> is one of "passphrase", "pin" or | ||
| // "recoverykey". | ||
| // | ||
| // This will return [ErrAuthRequestorNotAvailable] if the CREDENTIALS_DIRECTORY | ||
| // environment variable is not set. | ||
| func NewSystemdCredsAuthRequestor(console io.Writer, prefix string) (AuthRequestor, error) { | ||
| dir, exists := os.LookupEnv("CREDENTIALS_DIRECTORY") | ||
| if !exists { | ||
| return nil, ErrAuthRequestorNotAvailable | ||
| } | ||
|
|
||
| if console == nil { | ||
| console = os.Stderr | ||
| } | ||
| if prefix == "" { | ||
| prefix = "ubuntu-fde" | ||
| } | ||
|
|
||
| return &systemdCredsAuthRequestor{ | ||
| console: console, | ||
| prefix: prefix, | ||
| credsDir: dir, | ||
| }, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the path like? I do no think it is very practical to have to give a path for a recovery key, given all a recovery keys are supposed to be the same anyway. And is not it a node? In theory, we cannot always guess nodes before booting. And also depending on the type of VM, we might get different names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could have some names, that we could add through an activate option. So from snapd side we can control it. Like give it "data" or "save".