> For the complete documentation index, see [llms.txt](https://docs.keeper.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.keeper.io/keeperpam/jp/secrets-manager/integrations/linux-keyring.md).

# Linux Keyring

!\[]\(../../.gitbook/assets/keeper and Linux Keyring.jpg)

## 概要 <a href="#overview" id="overview"></a>

Keyringは、パスワードやシークレットなどの機密情報を保存し、アプリケーションから安全にアクセスできるようにするLinuxのセキュリティ機能です。

KeeperのLinux Keyring Utilityは、ネイティブLinux APIと連携し、[Secret Service API](https://www.gnu.org/software/emacs/manual/html_node/auth/Secret-Service-API.html)を使用してKeyringからシークレットを保存および取得します。任意の連携、プラグイン、コードベースから利用でき、Linux Keyringに認証情報、シークレット、パスワードをシンプルかつネイティブに保存および取得できます。

Linux Keyring Utilityのソースコードは以下で公開されています。

{% embed url="<https://github.com/Keeper-Security/linux-keyring-utility>" %}

利用に必要なバイナリは以下で入手できます。

{% embed url="<https://github.com/Keeper-Security/linux-keyring-utility/releases>" %}

Linux Keyring Utilityの利用方法は以下の2通りです。

* [リリースページ](https://github.com/Keeper-Security/linux-keyring-utility/releases)からビルド済みバイナリをデプロイする
* コードベースにインポートする

以下では、両方の方法を取り扱います。

## Linux Keyring Utility <a href="#linux-keyring-utility" id="linux-keyring-utility"></a>

Linux Keyring Utilityは、[D-Bus](https://dbus.freedesktop.org/doc/dbus-tutorial.html) [Secret Service](https://specifications.freedesktop.org/secret-service/latest/)を使用して、Linux [Keyring](http://man7.org/linux/man-pages/man7/keyrings.7.html)内のシークレットを取得および設定できます。

[GNOME Keyring](https://wiki.gnome.org/Projects/GnomeKeyring/)および[KDE Wallet Manager](https://userbase.kde.org/KDE_Wallet_Manager)でテスト済みです。D-Bus Secret Serviceを実装する環境であれば動作する想定です。

### インターフェース <a href="#interface" id="interface"></a>

`dbus_secrets` と `secret_collection` の2つのパッケージがあります。`secret_collection` オブジェクトは `dbus_secrets` の関数を使用します。D-BusのConnection、Session、Collection Serviceオブジェクトを統合し、CLI向けのシンプルなget/set/deleteインターフェースを構成します。

## 使用方法 - コードベースへのインポート <a href="#usage-importing-it-into-your-codebase" id="usage-importing-it-into-your-codebase"></a>

Go言語APIには `Get()`、`Set()`、`Delete()` メソッドがあります。最初の2つは `string` データを受け取り、返します。

### 例 (get) <a href="#example-get" id="example-get"></a>

```
package main

import (
    "os"
    sc "github.com/Keeper-Security/linux-keyring-utility/pkg/secret_collection"
)

func doit() {
    if collection, err := sc.DefaultCollection(); err == nil {
        if err := collection.Unlock(); err == nil {
            if secret, err := collection.Get("myapp", "mysecret"); err == nil {
                print(string(secret))
                os.Exit(0)
            }
        }
    }
    os.Exit(1)
}
```

`.DefaultCollection()` は、defaultエイリアスが参照するコレクションを返します。defaultエイリアスが設定されていない場合はエラーになります。通常はlogin Keyringを指します。多くのLinux Keyring UIでは、ユーザーが設定できます。

`.NamedCollection(string)` メソッドでは、名前でコレクションにアクセスできます。

### 例 (set) <a href="#example-set" id="example-set"></a>

`Set` はデータをパラメータとして受け取り、成功時は `nil`、失敗時はエラーのみを返します。シークレットデータの内容や長さに制限はありません。

```
if err := collection.Set("myapp", "mysecret", "mysecretdata"); err == nil {
    // 成功
}
```

## 使用方法 - バイナリインターフェース (CLI) <a href="#usage-binary-interface-cli" id="usage-binary-interface-cli"></a>

Linuxバイナリでは、以下の3つのサブコマンドが利用できます。

1. `get`
2. `set`
3. `del`

`get` と `del` には1つのパラメータ `name` が必要です。これはD-Bus API用語ではシークレットのLabelです。

`del` は1つ以上のシークレットラベルを受け取り、それらをすべて削除します。エラーが発生した時点で処理を停止します。

`set` では、第2パラメータとしてデータを単一の文字列で指定する必要があります。たとえば、`set foo bar baz` はエラーになりますが、`set foo 'bar baz'` は動作します。文字列として `-` を指定した場合、標準入力から読み込みます。

### Base64エンコード <a href="#base64-encoding" id="base64-encoding"></a>

`get` と `set` には `-b` または `--base64` フラグがあり、Base64を自動的に処理します。このフラグを使用すると、`set` は保存前に入力をエンコードし、`get` は出力前にデコードします。

Base64エンコードされていないシークレットに対して `get -b` を実行すると、エラーになります。

#### 例 <a href="#examples" id="examples"></a>

```
# setは出力なし
lkru set root_cred '{
    "username": "root"
    "password": "rand0m."
}'
# getは設定した値を標準出力に表示
lku get root_cred
{
    "username": "root"
    "password": "rand0m."
}
lkru set -b root_cred2 '{"username": "gollum", "password": "MyPrecious"}'
lkru get root_cred2
eyJ1c2VybmFtZSI6ICJnb2xsdW0iLCAicGFzc3dvcmQiOiAiTXlQcmVjaW91cyJ9
lkru get -b root_cred2
{"username": "gollum", "password": "MyPrecious"}
cat ./good_cred.json | lkru set -b root_cred3 -
lkru get root_cred3
ewogICJ1c2VybmFtZSI6ICJhZGFtIiwKICAicGFzc3dvcmQiOiAicGFzc3dvcmQxMjMuIgp9
```

## エラー <a href="#errors" id="errors"></a>

エラー出力は `stderr` に送られるため、コマンド末尾に `2>/dev/null` を付けると抑制できます。

### キーリングがない <a href="#no-keyring" id="no-keyring"></a>

loginコレクションが存在しないのは、Keyring自体が存在しないためです。KDEでは、GNOMEと同様にloginではなくkdewalletが作成される場合があります。

{% code overflow="wrap" %}

```
Unable to get secret 'test_cred': Unable to retrieve secret 'test_cred' for application 'lkru' from collection '/org/freedesktop/secrets/collection/login': Object does not exist at path “/org/freedesktop/secrets/collection/login”
```

{% endcode %}

### 一致するシークレットがない <a href="#no-matching-secret" id="no-matching-secret"></a>

同じラベルのシークレットが存在しても、値が返されない場合があります。`lkru` で作成していないシークレットでは、[属性](https://github.com/Keeper-Security/linux-keyring-utility/blob/main/pkg/dbus_secrets/dbus_secrets.go#L41) (Agent、Application、Id) が一致しない可能性があります。

{% code overflow="wrap" %}

```
Unable to get secret 'test_cred': Unable to retrieve secret 'test_cred' for application 'lkru' from collection '/org/freedesktop/secrets/aliases/default': org.freedesktop.Secret.Collection.SearchItems returned nothing
```

{% endcode %}

### D-Busセッションがない <a href="#no-d-bus-session" id="no-d-bus-session"></a>

Secret ServiceをホストするD-Busセッションが存在しない場合があります。GUIにログインしていないときに発生します。

```
Unable to get the default keyring: Unable to open a D-Bus session: The name org.freedesktop.secrets was not provided by any .service files
```

### D-Busがない <a href="#no-d-bus" id="no-d-bus"></a>

システムがD-Busをホストしていない場合があります。一部の軽量Linuxディストリビューションは、デフォルトでD-Busを含みません。

{% code overflow="wrap" %}

```
Unable to get the default keyring: Unable to connect to the D-Bus Session Bus: exec: "dbus-launch": executable file not found in $PATH
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.keeper.io/keeperpam/jp/secrets-manager/integrations/linux-keyring.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
