Skip to content

Commit

Permalink
crypt.go: allow adding names to legacy keyslots
Browse files Browse the repository at this point in the history
When reprovisioning with a newer snapd with old disks, we need to be
able to convert old keyslots to new ones with names. Otherwise we
cannot remove after reprovisioning is done.
  • Loading branch information
valentindavid committed Nov 20, 2024
1 parent b3ae517 commit 74675a7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
31 changes: 31 additions & 0 deletions crypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,3 +934,34 @@ func RenameLUKS2ContainerKey(devicePath, oldName, newName string) error {

return nil
}

func NameLegacyLUKS2ContainerKey(devicePath string, keyslot int, newName string) error {
view, err := newLUKSView(devicePath, luks2.LockModeBlocking)
if err != nil {
return xerrors.Errorf("cannot obtain LUKS header view: %w", err)
}

for _, name := range view.TokenNames() {
_, id, _ := view.TokenByName(name)
if keyslot == id {
return errors.New("keyslot already has a name")
}
}

token := &luksview.KeyDataToken{
TokenBase: luksview.TokenBase{
TokenName: newName,
TokenKeyslot: keyslot,
},
}

if err := luks2ImportToken(devicePath, token, nil); err != nil {
return xerrors.Errorf("cannot import token: %w", err)
}

if err := luks2SetSlotPriority(devicePath, keyslot, luks2.SlotPriorityHigh); err != nil {
return xerrors.Errorf("cannot change keyslot priority: %w", err)
}

return nil
}
52 changes: 52 additions & 0 deletions crypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4003,3 +4003,55 @@ func (s *cryptSuite) TestActivateVolumeWithLegacyPathsError(c *C) {

s.checkKeyDataKeysInKeyring(c, "", "/dev/some/path", unlockKey, primaryKey)
}

func (s *cryptSuite) TestNameLegacyLUKS2ContainerKey(c *C) {
firstKey := s.newPrimaryKey(c, 32)
secondKey := s.newPrimaryKey(c, 32)

m := &mockLUKS2Container{
tokens: map[int]luks2.Token{},
keyslots: map[int][]byte{
0: firstKey,
1: secondKey,
},
}

s.luks2.devices["/dev/foo1"] = m

err := NameLegacyLUKS2ContainerKey("/dev/foo1", 0, "some-name")
c.Check(err, IsNil)

token, hasToken := m.tokens[0]
c.Assert(hasToken, Equals, true)
c.Check(token, DeepEquals, &luksview.KeyDataToken{
TokenBase: luksview.TokenBase{
TokenKeyslot: 0,
TokenName: "some-name",
},
})
}

func (s *cryptSuite) TestNameLegacyLUKS2ContainerKeyNameExists(c *C) {
firstKey := s.newPrimaryKey(c, 32)
secondKey := s.newPrimaryKey(c, 32)

m := &mockLUKS2Container{
tokens: map[int]luks2.Token{
1: &luksview.KeyDataToken{
TokenBase: luksview.TokenBase{
TokenKeyslot: 1,
TokenName: "already",
},
},
},
keyslots: map[int][]byte{
0: firstKey,
1: secondKey,
},
}

s.luks2.devices["/dev/foo1"] = m

err := NameLegacyLUKS2ContainerKey("/dev/foo1", 1, "some-name")
c.Check(err, ErrorMatches, `keyslot already has a name`)
}

0 comments on commit 74675a7

Please sign in to comment.