File: auth_requestor_test.go

package info (click to toggle)
snapd 2.71-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,536 kB
  • sloc: ansic: 16,114; sh: 16,105; python: 9,941; makefile: 1,890; exp: 190; awk: 40; xml: 22
file content (161 lines) | stat: -rw-r--r-- 6,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// -*- Mode: Go; indent-tabs-mode: t -*-
//go:build !nosecboot

/*
 * Copyright (C) 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_test

import (
	"fmt"
	"os"
	"path/filepath"

	. "gopkg.in/check.v1"

	sb "github.com/snapcore/secboot"

	"github.com/snapcore/snapd/secboot"
	"github.com/snapcore/snapd/testutil"
)

type authRequestorSuite struct {
	testutil.BaseTest

	inputFile             string
	errorFile             string
	systemctlVersionFile  string
	systemdAskPasswordCmd *testutil.MockCmd
}

var _ = Suite(&authRequestorSuite{})

func (s *authRequestorSuite) setInput(c *C, input string) {
	err := os.WriteFile(s.inputFile, []byte(input), 0644)
	c.Assert(err, IsNil)
}

func (s *authRequestorSuite) setError(c *C, message string) {
	err := os.WriteFile(s.errorFile, []byte(message), 0644)
	c.Assert(err, IsNil)
}

func (s *authRequestorSuite) setSystemdVersion(c *C, version string) {
	fullVersion := fmt.Sprintf("systemd %[1]s (%[1]s.4-1ubuntu3)\n+PAM +AUDIT...\n", version)
	err := os.WriteFile(s.systemctlVersionFile, []byte(fullVersion), 0644)
	c.Assert(err, IsNil)
}

func (s *authRequestorSuite) SetUpTest(c *C) {
	commandInputs := c.MkDir()
	s.inputFile = filepath.Join(commandInputs, "input")
	s.errorFile = filepath.Join(commandInputs, "error")
	err := os.WriteFile(s.errorFile, []byte("unset"), 0644)
	c.Assert(err, IsNil)
	script := fmt.Sprintf(`if [ -r '%[1]s' ]; then cat '%[1]s'; else cat '%[2]s'; exit 1; fi`, s.inputFile, s.errorFile)
	fmt.Printf("%s\n", script)
	s.systemdAskPasswordCmd = testutil.MockCommand(c, "systemd-ask-password", script)
	s.AddCleanup(s.systemdAskPasswordCmd.Restore)
	systemctlResults := c.MkDir()
	s.systemctlVersionFile = filepath.Join(systemctlResults, "version")
	systemctl := fmt.Sprintf(`if [ "${1-}" = --version ]; then cat %[1]s; else exit 1; fi`, s.systemctlVersionFile)
	systemctlCmd := testutil.MockCommand(c, "systemctl", systemctl)
	s.AddCleanup(systemctlCmd.Restore)
}

func (s *authRequestorSuite) TestRequestPassphrase(c *C) {
	authRequestor := secboot.NewSystemdAuthRequestor()
	s.setInput(c, "thepassphrase\n")
	s.setSystemdVersion(c, "256")

	input, err := authRequestor.RequestPassphrase("some-volume", "some-device")
	c.Assert(err, IsNil)
	c.Check(input, Equals, "thepassphrase")

	c.Check(s.systemdAskPasswordCmd.Calls(), DeepEquals, [][]string{
		{"systemd-ask-password", "--icon", "drive-harddisk", "--id", "secboot.test:some-device", "--credential=snapd.passphrase", "Please enter the passphrase for volume some-volume for device some-device"},
	})
}

func (s *authRequestorSuite) TestRequestPassphraseOldSystemd(c *C) {
	authRequestor := secboot.NewSystemdAuthRequestor()
	s.setInput(c, "thepassphrase\n")
	s.setSystemdVersion(c, "248")

	input, err := authRequestor.RequestPassphrase("some-volume", "some-device")
	c.Assert(err, IsNil)
	c.Check(input, Equals, "thepassphrase")

	c.Check(s.systemdAskPasswordCmd.Calls(), DeepEquals, [][]string{
		{"systemd-ask-password", "--icon", "drive-harddisk", "--id", "secboot.test:some-device", "Please enter the passphrase for volume some-volume for device some-device"},
	})
}

func (s *authRequestorSuite) TestRequestPassphraseFailure(c *C) {
	authRequestor := secboot.NewSystemdAuthRequestor()
	s.setError(c, "blah blah\n")
	s.setSystemdVersion(c, "256")

	_, err := authRequestor.RequestPassphrase("some-volume", "some-device")
	c.Assert(err, ErrorMatches, "cannot execute systemd-ask-password: exit status 1")

	c.Check(s.systemdAskPasswordCmd.Calls(), DeepEquals, [][]string{
		{"systemd-ask-password", "--icon", "drive-harddisk", "--id", "secboot.test:some-device", "--credential=snapd.passphrase", "Please enter the passphrase for volume some-volume for device some-device"},
	})
}

func (s *authRequestorSuite) TestRequestPassphraseMissingNewLine(c *C) {
	authRequestor := secboot.NewSystemdAuthRequestor()
	s.setInput(c, "blah blah")
	s.setSystemdVersion(c, "256")

	_, err := authRequestor.RequestPassphrase("some-volume", "some-device")
	c.Assert(err, ErrorMatches, "systemd-ask-password output is missing terminating newline")

	c.Check(s.systemdAskPasswordCmd.Calls(), DeepEquals, [][]string{
		{"systemd-ask-password", "--icon", "drive-harddisk", "--id", "secboot.test:some-device", "--credential=snapd.passphrase", "Please enter the passphrase for volume some-volume for device some-device"},
	})
}

func (s *authRequestorSuite) TestRequestRecoveryKey(c *C) {
	authRequestor := secboot.NewSystemdAuthRequestor()
	s.setInput(c, "00001-00002-00003-00004-00005-00006-00007-00008\n")
	s.setSystemdVersion(c, "256")

	input, err := authRequestor.RequestRecoveryKey("some-volume", "some-device")
	c.Assert(err, IsNil)
	var expected sb.RecoveryKey
	copy(expected[:], []byte{1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0})
	c.Check(input, DeepEquals, expected)

	c.Check(s.systemdAskPasswordCmd.Calls(), DeepEquals, [][]string{
		{"systemd-ask-password", "--icon", "drive-harddisk", "--id", "secboot.test:some-device", "--credential=snapd.recovery", "Please enter the recovery key for volume some-volume for device some-device"},
	})
}

func (s *authRequestorSuite) TestRequestRecoveryKeyParseFailure(c *C) {
	authRequestor := secboot.NewSystemdAuthRequestor()
	s.setInput(c, "broken\n")
	s.setSystemdVersion(c, "256")

	_, err := authRequestor.RequestRecoveryKey("some-volume", "some-device")
	c.Assert(err, ErrorMatches, `cannot parse recovery key: .*`)

	c.Check(s.systemdAskPasswordCmd.Calls(), DeepEquals, [][]string{
		{"systemd-ask-password", "--icon", "drive-harddisk", "--id", "secboot.test:some-device", "--credential=snapd.recovery", "Please enter the recovery key for volume some-volume for device some-device"},
	})
}