# Linux Keyring

<figure><img src="https://859776093-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FPL6k1aGsLiFiiJ3Y7zCl%2Fuploads%2FvSJn1salMnelP3AE6aCY%2Fimage.png?alt=media&#x26;token=5de8e6d5-2103-4783-a9da-f9153487c9d5" alt=""><figcaption></figcaption></figure>

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

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

Keeperからは、Linuxキーリングユーティリティというユーティリティがご利用になれます。このユーティリティは、ネイティブLinux APIと対話し、[Secret Service API](https://www.gnu.org/software/emacs/manual/html_node/auth/Secret-Service-API.html)を使用してキーリングからシークレットを保存および取得します。このユーティリティは、あらゆる統合、プラグイン、コードベースで使用でき、あらゆるLinuxキーリングに認証情報、シークレット、パスワードを簡単に保存および取得できます。

Linuxキーリングユーティリティのコードベースは、以下から入手できます。

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

上記のユーティリティを使用するために必要なバイナリは、以下から入手できます。

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

Linuxキーリングユーティリティを使用するには、以下の方法があります。

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

## Linuxキーリングユーティリティ <a href="#linux-keyring-utility" id="linux-keyring-utility"></a>

Linuxキーリングユーティリティは、[D-Bus](https://dbus.freedesktop.org/doc/dbus-tutorial.html)[シークレットサービス](https://specifications.freedesktop.org/secret-service/latest/)を使用して、Linux[キーリング内の](http://man7.org/linux/man-pages/man7/keyrings.7.html)シークレットを取得および設定します。

Linuxキーリングユーティリティは、[GNOME Keyring](https://wiki.gnome.org/Projects/GnomeKeyring/)および[KDE Wallet Manager](https://userbase.kde.org/KDE_Wallet_Manager)でテスト済みで、D -Bus Secrets Serviceのどの実装でも動作するはずです。

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

`dbus_secrets`と`secret_collection`の2 つのパッケージがあります。`secret_collection`オブジェクトは`dbus_secrets`の関数を使用します。このオブジェクトは、D-Busの接続、セッション、コレクションサービスオブジェクトを統合し、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()`は、デフォルトのエイリアスが参照するコレクションを返します。*デフォルトの*エイリアスが設定されていない場合はエラーが発生します。通常はログインキーリングを指します。ほとんどのLinuxキーリングインターフェイスでは、ユーザーが設定できます。

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

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

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

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

## 使用方法 - バイナリインターフェイス (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用語ではシークレットラベルを指します。

`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` を使用するとエラーが発生します。

**例**

```
# set has no output
lkru set root_cred '{
    "username": "root"
    "password": "rand0m."
}'
# get prints (to stdout) whatever was set
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>

ログインコレクションが存在しない理由は、キーリング自体が存在しないためです。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`で作成されていない場合は、「Agent」、「Application」、「ID」のような[属性](https://github.com/Keeper-Security/linux-keyring-utility/blob/main/Keeper-Security/linux-keyring-utility/blob/main/pkg/dbus_secrets/dbus_secrets.go#L41)が一致していない可能性があります。

{% 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>

シークレットサービスをホストする 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 %}
