Skip to content
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

feat(crypto/keyring): add Linux's keyctl support #21653

Merged
merged 15 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Every module contains its own CHANGELOG.md. Please refer to the module you are i

* (baseapp) [#20291](https://github.com/cosmos/cosmos-sdk/pull/20291) Simulate nested messages.
* (cli) [#21372](https://github.com/cosmos/cosmos-sdk/pull/21372) Add a `bulk-add-genesis-account` genesis command to add many genesis accounts at once.
* (crypto/keyring) [#21653](https://github.com/cosmos/cosmos-sdk/pull/21653) New Linux-only backend that adds Linux kernel's `keyctl` support.

### Improvements

Expand Down
2 changes: 2 additions & 0 deletions crypto/keyring/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
// https://github.com/KDE/kwallet
// pass This backend uses the pass command line utility to store and retrieve keys:
// https://www.passwordstore.org/
// keyctl This backend leverages the Linux's kernel security key management system
// to store cryptographic keys securely in memory. This is available on Linux only.
// test This backend stores keys insecurely to disk. It does not prompt for a password to
// be unlocked and it should be used only for testing purposes.
// memory Same instance as returned by NewInMemory. This backend uses a transient storage. Keys
Expand Down
2 changes: 1 addition & 1 deletion crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func NewInMemoryWithKeyring(kr keyring.Keyring, cdc codec.Codec, opts ...Option)
// New creates a new instance of a keyring.
// Keyring options can be applied when generating the new instance.
// Available backends are "os", "file", "kwallet", "memory", "pass", "test".
func New(
func newKeyringGeneric(
appName, backend, rootDir string, userInput io.Reader, cdc codec.Codec, opts ...Option,
) (Keyring, error) {
var (
Expand Down
43 changes: 43 additions & 0 deletions crypto/keyring/keyring_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build linux
// +build linux

package keyring

import (
"fmt"
"io"

"github.com/99designs/keyring"
"github.com/cosmos/cosmos-sdk/codec"
)

// Linux-only backend options.
const BackendKeyctl = "keyctl"

func newKeyctlBackendConfig(appName, _ string, _ io.Reader) keyring.Config {
return keyring.Config{
AllowedBackends: []keyring.BackendType{keyring.KeyCtlBackend},
ServiceName: appName,
KeyCtlScope: "user",
KeyCtlPerm: 0x3f3f0000,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a clarification on which are the granted permissions here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am doing more work to provide users with sensible perms/scope options

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By default, user and session keyring give access to the owner (i.e. the creator) only

}
}

// New creates a new instance of a keyring.
// Keyring options can be applied when generating the new instance.
// Available backends are "os", "file", "kwallet", "memory", "pass", "test", "keyctl".
func New(
appName, backend, rootDir string, userInput io.Reader, cdc codec.Codec, opts ...Option,
) (Keyring, error) {

if backend != BackendKeyctl {
return newKeyringGeneric(appName, backend, rootDir, userInput, cdc, opts...)
}

db, err := keyring.Open(newKeyctlBackendConfig(appName, "", userInput))
if err != nil {
return nil, fmt.Errorf("couldn't open keyring for %q: %w", appName, err)
}

return newKeystore(db, cdc, backend, opts...), nil
}
16 changes: 16 additions & 0 deletions crypto/keyring/keyring_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !linux
// +build !linux

package keyring

import (
"io"

"github.com/cosmos/cosmos-sdk/codec"
)

func New(
appName, backend, rootDir string, userInput io.Reader, cdc codec.Codec, opts ...Option,
) (Keyring, error) {
return newKeyringGeneric(appName, backend, rootDir, userInput, cdc, opts...)
}
Loading