Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions auth_requestor_auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (
)

var (
newPlymouthAuthRequestor = NewPlymouthAuthRequestor
newSystemdAuthRequestor = NewSystemdAuthRequestor
newPlymouthAuthRequestor = NewPlymouthAuthRequestor
newSystemdAuthRequestor = NewSystemdAuthRequestor
newSystemdCredsAuthRequestor = NewSystemdCredsAuthRequestor
)

type autoAuthRequestor struct {
Expand Down Expand Up @@ -59,6 +60,7 @@ func (r *autoAuthRequestor) NotifyUserAuthResult(ctx context.Context, result Use

// NewAutoAuthRequestor creates an implementation of AuthRequestor that automatically
// selects the first available implementation in the following order:
// - systemd credential.
// - Plymouth.
// - systemd-ask-password.
//
Expand All @@ -71,9 +73,20 @@ func (r *autoAuthRequestor) NotifyUserAuthResult(ctx context.Context, result Use
// The caller supplies an implementation of AuthRequestorStringer that returns messages.
// The console argument is used by the systemd-ask-password implementation of
// [AuthRequestor.NotifyUserAuthResult] where result is not [UserAuthResultSuccess]. If not
// provided, it defaults to [os.Stderr].
func NewAutoAuthRequestor(stderr io.Writer, stringer AuthRequestorStringer) (AuthRequestor, error) {
// provided, it defaults to [os.Stderr]. The credPrefix argument is used to specify the
// prefix for systemd credentials.
func NewAutoAuthRequestor(stderr io.Writer, stringer AuthRequestorStringer, credPrefix string) (AuthRequestor, error) {
var requestors []AuthRequestor

switch sdcred, err := newSystemdCredsAuthRequestor(stderr, credPrefix); {
case errors.Is(err, ErrAuthRequestorNotAvailable):
// ignore
case err != nil:
return nil, fmt.Errorf("cannot create sdcreds AuthRequestor: %w", err)
default:
requestors = append(requestors, sdcred)
}

switch ply, err := newPlymouthAuthRequestor(stringer); {
case errors.Is(err, ErrAuthRequestorNotAvailable):
// ignore
Expand Down
82 changes: 62 additions & 20 deletions auth_requestor_auto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"errors"
"fmt"
"io"
"os"
"strings"

. "github.com/snapcore/secboot"
Expand All @@ -36,11 +37,13 @@ import (

type authRequestorAutoSuite struct {
snapd_testutil.BaseTest
authRequestorSdCredsTestMixin
authRequestorPlymouthTestMixin
authRequestorSystemdTestMixin
}

func (s *authRequestorAutoSuite) SetUpTest(c *C) {
s.AddCleanup(s.authRequestorSdCredsTestMixin.setUpTest(c))
s.AddCleanup(s.authRequestorPlymouthTestMixin.setUpTest(c))
s.AddCleanup(s.authRequestorSystemdTestMixin.setUpTest(c))
}
Expand Down Expand Up @@ -170,7 +173,22 @@ func (s *authRequestorAutoSuite) TestNewAuthRequestor(c *C) {
})
defer restore()

requestor, err := NewAutoAuthRequestor(sdConsole, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(sdConsole, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)
c.Assert(requestor, NotNil)
c.Assert(requestor, testutil.ConvertibleTo, &AutoAuthRequestor{})
c.Assert(requestor.(*AutoAuthRequestor).Requestors(), HasLen, 3)
c.Check(requestor.(*AutoAuthRequestor).Requestors()[0], testutil.ConvertibleTo, &SystemdCredsAuthRequestor{})
c.Check(requestor.(*AutoAuthRequestor).Requestors()[1], testutil.ConvertibleTo, &PlymouthAuthRequestor{})
c.Check(requestor.(*AutoAuthRequestor).Requestors()[2], testutil.ConvertibleTo, &SystemdAuthRequestor{})
}

func (s *authRequestorAutoSuite) TestNewAuthRequestorSdCredsNotAvailable(c *C) {
sdConsole := new(bytes.Buffer)

c.Assert(os.Unsetenv("CREDENTIALS_DIRECTORY"), IsNil)

requestor, err := NewAutoAuthRequestor(sdConsole, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)
c.Assert(requestor, NotNil)
c.Assert(requestor, testutil.ConvertibleTo, &AutoAuthRequestor{})
Expand All @@ -185,12 +203,13 @@ func (s *authRequestorAutoSuite) TestNewAuthRequestorPlymouthNotAvailable(c *C)
})
defer restore()

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)
c.Assert(requestor, NotNil)
c.Assert(requestor, testutil.ConvertibleTo, &AutoAuthRequestor{})
c.Assert(requestor.(*AutoAuthRequestor).Requestors(), HasLen, 1)
c.Check(requestor.(*AutoAuthRequestor).Requestors()[0], testutil.ConvertibleTo, &SystemdAuthRequestor{})
c.Assert(requestor.(*AutoAuthRequestor).Requestors(), HasLen, 2)
c.Check(requestor.(*AutoAuthRequestor).Requestors()[0], testutil.ConvertibleTo, &SystemdCredsAuthRequestor{})
c.Check(requestor.(*AutoAuthRequestor).Requestors()[1], testutil.ConvertibleTo, &SystemdAuthRequestor{})
}

func (s *authRequestorAutoSuite) TestNewAuthRequestorSystemdNotAvailable(c *C) {
Expand All @@ -199,15 +218,18 @@ func (s *authRequestorAutoSuite) TestNewAuthRequestorSystemdNotAvailable(c *C) {
})
defer restore()

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)
c.Assert(requestor, NotNil)
c.Assert(requestor, testutil.ConvertibleTo, &AutoAuthRequestor{})
c.Assert(requestor.(*AutoAuthRequestor).Requestors(), HasLen, 1)
c.Check(requestor.(*AutoAuthRequestor).Requestors()[0], testutil.ConvertibleTo, &PlymouthAuthRequestor{})
c.Assert(requestor.(*AutoAuthRequestor).Requestors(), HasLen, 2)
c.Check(requestor.(*AutoAuthRequestor).Requestors()[0], testutil.ConvertibleTo, &SystemdCredsAuthRequestor{})
c.Check(requestor.(*AutoAuthRequestor).Requestors()[1], testutil.ConvertibleTo, &PlymouthAuthRequestor{})
}

func (s *authRequestorAutoSuite) TestNewAuthRequestorNotAvailable(c *C) {
c.Assert(os.Unsetenv("CREDENTIALS_DIRECTORY"), IsNil)

restore := MockNewPlymouthAuthRequestor(func(_ AuthRequestorStringer) (AuthRequestor, error) {
return nil, ErrAuthRequestorNotAvailable
})
Expand All @@ -218,13 +240,13 @@ func (s *authRequestorAutoSuite) TestNewAuthRequestorNotAvailable(c *C) {
})
defer restore()

_, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
_, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Check(err, ErrorMatches, "the auth requestor is not available")
c.Check(errors.Is(err, ErrAuthRequestorNotAvailable), testutil.IsTrue)
}

func (s *authRequestorAutoSuite) TestNewAuthRequestorPlymouthError(c *C) {
_, err := NewAutoAuthRequestor(nil, nil)
_, err := NewAutoAuthRequestor(nil, nil, "")
c.Check(err, ErrorMatches, "cannot create Plymouth AuthRequestor: must supply an implementation of AuthRequestorStringer")
}

Expand All @@ -234,15 +256,35 @@ func (s *authRequestorAutoSuite) TestNewAuthRequestorSystemdError(c *C) {
})
defer restore()

_, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
_, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Check(err, ErrorMatches, "cannot create systemd AuthRequestor: some error")
}

func (s *authRequestorAutoSuite) TestRequestUserCredentialSdCred(c *C) {
// Ensure that a systemd cred is used first if available.
s.setPassphrase(c, "password")
s.writeCred(c, "foo", "/dev/sda1", "password", "passphrase")

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "foo")
c.Assert(err, IsNil)

passphrase, passphraseType, err := requestor.RequestUserCredential(context.Background(), "data", "/dev/sda1", UserAuthTypePassphrase)
c.Check(err, IsNil)
c.Check(passphrase, Equals, "password")
c.Check(passphraseType, Equals, UserAuthTypePassphrase)

c.Check(s.mockPlymouth.Calls(), HasLen, 0)
c.Check(s.mockSdAskPassword.Calls(), HasLen, 0)

c.Assert(requestor, testutil.ConvertibleTo, &AutoAuthRequestor{})
c.Check(requestor.(*AutoAuthRequestor).LastUsed(), testutil.ConvertibleTo, &SystemdCredsAuthRequestor{})
}

func (s *authRequestorAutoSuite) TestRequestUserCredentialPlymouth(c *C) {
// Ensure that plymouth is used first if available.
// Ensure that plymouth is used first if available and there is no systemd cred.
s.setPassphrase(c, "password")

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)

passphrase, passphraseType, err := requestor.RequestUserCredential(context.Background(), "data", "/dev/sda1", UserAuthTypePassphrase)
Expand All @@ -265,7 +307,7 @@ func (s *authRequestorAutoSuite) TestRequestUserCredentialSystemd(c *C) {
s.setPassphrase(c, "password")
s.stopPlymouthd(c)

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)

passphrase, passphraseType, err := requestor.RequestUserCredential(context.Background(), "data", "/dev/sda1", UserAuthTypePassphrase)
Expand All @@ -283,7 +325,7 @@ func (s *authRequestorAutoSuite) TestRequestUserCredentialSystemd(c *C) {
func (s *authRequestorAutoSuite) TestRequestUserCredentialDifferentName(c *C) {
s.setPassphrase(c, "password")

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)

passphrase, passphraseType, err := requestor.RequestUserCredential(context.Background(), "foo", "/dev/sda1", UserAuthTypePassphrase)
Expand All @@ -304,7 +346,7 @@ func (s *authRequestorAutoSuite) TestRequestUserCredentialDifferentName(c *C) {
func (s *authRequestorAutoSuite) TestRequestUserCredentialDifferentPath(c *C) {
s.setPassphrase(c, "password")

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)

passphrase, passphraseType, err := requestor.RequestUserCredential(context.Background(), "data", "/dev/nvme0n1p3", UserAuthTypePassphrase)
Expand All @@ -325,7 +367,7 @@ func (s *authRequestorAutoSuite) TestRequestUserCredentialDifferentPath(c *C) {
func (s *authRequestorAutoSuite) TestRequestUserCredentialDifferentCredentialType(c *C) {
s.setPassphrase(c, "password")

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)

passphrase, passphraseType, err := requestor.RequestUserCredential(context.Background(), "foo", "/dev/sda1", UserAuthTypePassphrase|UserAuthTypeRecoveryKey)
Expand All @@ -346,7 +388,7 @@ func (s *authRequestorAutoSuite) TestRequestUserCredentialDifferentCredentialTyp
func (s *authRequestorAutoSuite) TestRequestUserCredentialDifferentPassphrase(c *C) {
s.setPassphrase(c, "1234")

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)

passphrase, passphraseType, err := requestor.RequestUserCredential(context.Background(), "data", "/dev/sda1", UserAuthTypePassphrase)
Expand All @@ -367,7 +409,7 @@ func (s *authRequestorAutoSuite) TestRequestUserCredentialDifferentPassphrase(c
func (s *authRequestorAutoSuite) TestRequestUserCredentialCanceledContext(c *C) {
s.setPassphrase(c, "password")

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -382,7 +424,7 @@ func (s *authRequestorAutoSuite) TestRequestUserCredentialPlymouthFails(c *C) {
// Ensure we get an error if any implementation fails with an unexpected error.
s.authRequestorSystemdTestMixin.setPassphrase(c, "password")

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)

_, _, err = requestor.RequestUserCredential(context.Background(), "data", "/dev/sda1", UserAuthTypePassphrase)
Expand All @@ -398,7 +440,7 @@ func (s *authRequestorAutoSuite) TestRequestUserCredentialNotAvailable(c *C) {
})
defer restore()

requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer))
requestor, err := NewAutoAuthRequestor(nil, new(mockAutoAuthRequestorStringer), "")
c.Assert(err, IsNil)

_, _, err = requestor.RequestUserCredential(context.Background(), "data", "/dev/sda1", UserAuthTypePassphrase)
Expand Down
2 changes: 2 additions & 0 deletions auth_requestor_plymouth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ type authRequestorPlymouthSuite struct {
}

func (s *authRequestorPlymouthSuite) SetUpTest(c *C) {
s.BaseTest.SetUpTest(c)

s.AddCleanup(s.authRequestorPlymouthTestMixin.setUpTest(c))
}

Expand Down
124 changes: 124 additions & 0 deletions auth_requestor_sdcreds.go
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))
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Member

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".

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
}
Loading
Loading