開発者用SDK

Keeper Secrets Manager用のサンプルコードとSDKの実装ガイド

概要

Keeper Secrets Manager SDKは、すべての一般的な言語からシークレットの管理機能への極めて簡単/高速/効率的なアクセスを実現するために特別に設計されています。

インストール

mavenのスナップショットリポジトリを参照: repositories { mavenCentral() maven { url "https://s01.oss.sonatype.org/content/groups/public/" } }

プロジェクトに次の依存関係を追加:

  • com.keepersecurity.secrets-manager:core:16.0.1-SNAPSHOT

Java SDKはJDKバージョン8から13.02+8をサポートしています

認証

Secrets Manager SDKは、ワンタイムアクセストークンまたはローカル設定ファイル内で生成された鍵を使用して、Keeperボルトに対して認証を行います。 Commander CLIで1つまたは複数のワンタイムアクセストークンを生成するには、secrets-manager client addコマンドを使用します。

$ keeper shell

... login ...

My Vault> secrets-manager client add --app MyApplication --unlock-ip

初期化

SecretsManager SDKは、設定ファイルを使用して接続のトークンと設定を格納します。以下のサンプルコードで、SDKとワンタイムアクセストークンを使用して設定ファイルを作成する方法を示します。

import com.keepersecurity.secretsManager.core.*;
import static com.keepersecurity.secretsManager.core.SecretsManager.*;

public class KSMSample {

    public static void main(String[] args){
        // oneTimeTokenはストレージの初期化に一度だけ使用
        // 初回実行以降の呼び出しでは、ksm-config.jsonを使用
        String oneTimeToken = "[ONE TIME TOKEN]";
        KeyValueStorage storage = new LocalConfigStorage("ksm-config.json");
        try {
            // 初回の実行後、この行は削除可能
            initializeStorage(storage, oneTimeToken);

            SecretsManagerOptions options = new SecretsManagerOptions(storage);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        
        // `options`を使用する他のコード
    }
}

設定ファイルを初期化したら、ワンタイムアクセストークンをコードから削除する必要があります

この初期化コードは、以下のキーを含むJSON設定ファイルをデバイスのローカルストレージに作成します。

生成された設定ファイルの例を以下に示します。

ksm-config.json
{
  "hostname": "keepersecurity.com",
  "clientId": "ab2x3z/Acz0QFTiilm8UxIlqNLlNa25KMj=TpOqznwa4Si-h9tY7n3zvFwlXXDoVWkIs3xrMjcLGwgu3ilmq7Q==",
  "privateKey":"MLSHAgABCDEFGyqGSM49AEGCCqGSM49AwEHBG0wawIWALTARgmcnWx/DH+r7cKh4kokasdasdaDbvHmLABstNbqDwaCWhRANCAARjunta9SJdZE/LVXfVb22lpIfK4YMkJEDaFMOAyoBt0BrQ8aEhvrHN5/Z1BgZ/WpDm9dMR7E5ASIQuYUiAw0t9",
  "serverPublicKeyId":"10",
  "appKey":"RzhSIyKxbpjNu045TUrKaNREYIns+Hk9Kn8YtT+CtK0=",
  "appOwnerPublicKey":"Sq1W1OAnTwi8V/Vs/lhsin2sfSoaRfOwwDDBqoP+EO9bsBMWCzQdl9ClauDiKLXGmlmyx2xmSAdH+hlxvBRs6kU="
}

設定ファイルを作成するその他の方法については、設定ファイルのドキュメントをご参照ください。

すべてのシークレットを取得

import com.keepersecurity.secretsManager.core.*;
import static com.keepersecurity.secretsManager.core.SecretsManager.*;
import java.io.FileOutputStream;

public class KSMSample {
  
  public static void main(String[] args){
    // 事前に初期化されたストレージを取得
    KeyValueStorage storage = new LocalConfigStorage("ksm-config.json");
    try { 
        SecretsManagerOptions options = new SecretsManagerOptions(storage);
        
        // 使用可能なすべてのシークレットを取得
        KeeperSecrets secrets = SecretsManager.getSecrets(options);
        
        // 記録の詳細を出力
        System.out.println(secrets.getRecords());
      } catch (Exception e) {
        System.out.println(e.getMessage());
      } 
    }
}

これらの例では、Secrets Managerの設定ファイルがすでに初期化済みであることを前提としています。

設定ファイルを初期化する方法については、初期化セクションをご参照ください。

個々のシークレットを取得

記録タイトルでシークレットを取得

import com.keepersecurity.secretsManager.core.*;
import java.util.List;

public class KSMSample {
    public static void main(String[] args){
        // 事前に初期化されたストレージを取得
        KeyValueStorage storage = new LocalConfigStorage("ksm-config.json");
        try {
            SecretsManagerOptions options = new SecretsManagerOptions(storage);

            // 取得する記録のタイトル
            String recordTitle = "My Credentials";
            
            // タイトルで記録を検索
            KeeperRecord myCredentials = secrets.getRecords().getSecretByTitle(recordTitle);

            // 記録の詳細を出力
            System.out.println("Record UID: " + myCredentials.getRecordUid());
            System.out.println("Title: " + myCredentials.getData().getTitle());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

UID記録でシークレットを取得

この例では、UID記録はXXXになっています

import com.keepersecurity.secretsManager.core.*;
import java.util.List;

public class KSMSample {
    public static void main(String[] args){
        // 事前に初期化されたストレージを取得
        KeyValueStorage storage = new LocalConfigStorage("ksm-config.json");
        try {
            SecretsManagerOptions options = new SecretsManagerOptions(storage);

            // 必要な記録のUIDを含むフィルタを作成
            List<String> uidFilter = List.of("[XXX]");

            // フィルタを使用してシークレットを取得
            KeeperSecrets secrets = SecretsManager.getSecrets(options, uidFilter);

            // 取得結果から目的のシークレットを取得
            KeeperRecord myCredentials = secrets.getRecords().get(0);

            // 記録の詳細を出力
            System.out.println("Record UID: " + myCredentials.getRecordUid());
            System.out.println("Title: " + myCredentials.getData().getTitle());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

これらの例では、Secrets Managerの設定ファイルがすでに初期化済みであることを前提としています。

設定ファイルを初期化する方法については、初期化セクションをご参照ください。

パスワードを取得

個々の記録のパスワードフィールドを取得する例。

この例では、UID記録はXXXになっています

import com.keepersecurity.secretsManager.core.*;
import java.util.List;

public class KSMSample {
    public static void main(String[] args){
        // 事前に初期化されたストレージを取得
        KeyValueStorage storage = new LocalConfigStorage("ksm-config.json");
        try {
            SecretsManagerOptions options = new SecretsManagerOptions(storage);

            // 必要な記録のUIDを含むフィルタを作成
            List<String> uidFilter = List.of("XXX");

            // フィルタを使用してシークレットを取得
            KeeperSecrets secrets = SecretsManager.getSecrets(options, uidFilter);

            // 取得結果から目的のシークレットを取得
            KeeperRecord myCredentials = secrets.getRecords().get(0);

            // パスワードを取得して出力
            String pwd = myCredentials.getPassword();
            System.out.println("Password from Keeper: " + pwd);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

これらの例では、Secrets Managerの設定ファイルがすでに初期化済みであることを前提としています。

設定ファイルを初期化する方法については、初期化セクションをご参照ください。

ファイル添付のダウンロード

import com.keepersecurity.secretsManager.core.*;

import java.io.FileOutputStream;
import java.util.List;

public class KSMSample {
    public static void main(String[] args){
        // 事前に初期化されたストレージを取得
        KeyValueStorage storage = new LocalConfigStorage("ksm-config.json");
        try {
            SecretsManagerOptions options = new SecretsManagerOptions(storage);

            // 必要な記録のUIDを含むフィルタを作成
            List<String> uidFilter = List.of("XXX");

            // フィルタを使用してシークレットを取得
            KeeperSecrets secrets = SecretsManager.getSecrets(options, uidFilter);

            // 取得結果から目的のシークレットを取得
            KeeperRecord myCredentials = secrets.getRecords().get(0);

            // 記録からファイル名によるファイル参照を取得
            KeeperFile file = myCredentials.getFileByName("acme.cer");

            // ファイルをダウンロード
            byte[] fileBytes = SecretsManager.downloadFile(file);
            String filename = file.getData().getName();
            FileOutputStream fos = new FileOutputStream(filename);
            fos.write(fileBytes);
            System.out.println("Downloaded File: " + filename);
        } catch (Exception e) {
            System.out.println("KSM ran into an problem: " + e.getMessage());
        }
    }
}

これらの例では、Secrets Managerの設定ファイルがすでに初期化済みであることを前提としています。

設定ファイルを初期化する方法については、初期化セクションをご参照ください。

ファイル添付のアップロード

import com.keepersecurity.secretsManager.core.*;

import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;

public class KSMSample {
    public static void main(String[] args){
        // 事前に初期化されたストレージを取得
        KeyValueStorage storage = new LocalConfigStorage("ksm-config.json");
        try {
            SecretsManagerOptions options = new SecretsManagerOptions(storage);

            // 必要な記録のUIDを含むフィルタを作成
            List<String> uidFilter = List.of("XXX");

            // フィルタを使用してシークレットを取得
            KeeperSecrets secrets = SecretsManager.getSecrets(options, uidFilter);

            // ファイルのアップロード先のシークレットを取得
            KeeperRecord ownerRecord = secrets.getRecords().get(0);
        
            // アップロードするファイルからバイトを取得
            File file = new File("./myFile.json");
            FileInputStream fl = new FileInputStream(file);
            byte[] fileBytes = new byte[(int)file.length()];
            fl.read(fileBytes);
            fl.close();
            
            // アップロードするKeeperファイルを作成
            KeeperFileUpload myFile = new KeeperFileUpload(
                "myFile.json",
                "My File",
                "application/json",
                fileBytes
            );

            // 選択した記録にファイルをアップロード
            SecretsManager.uploadFile(options, ownerRecord, myFile);
            
        } catch (Exception e) {
            System.out.println("KSM ran into an problem: " + e.getMessage());
        }
    }
}

これらの例では、Secrets Managerの設定ファイルがすでに初期化済みであることを前提としています

設定ファイルを初期化する方法については、初期化セクションをご参照ください。

TOTPコードを取得

import com.keepersecurity.secretsManager.core.*;

public class KSMSample {
    public static void main(String[] args){
        // 事前に初期化されたストレージを取得
        KeyValueStorage storage = new LocalConfigStorage("ksm-config.json");
        try {
            SecretsManagerOptions options = new SecretsManagerOptions(storage);

            // シークレットを取得
            KeeperSecrets secrets = SecretsManager.getSecrets(options);

            // 目的のシークレットからtotp urlを取得
            String totpUrl = Notation.getValue(secrets, "XXX/field/oneTimeCode");

            // TOTPコードを取得
            TotpCode totpCode = TotpCode.uriToTotpCode(totpUrl);
            System.out.println("TOTP: " + totpCode.getCode());
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

これらの例では、Secrets Managerの設定ファイルがすでに初期化済みであることを前提としています。

設定ファイルを初期化する方法については、初期化セクションをご参照ください。

パスワードの更新

import com.keepersecurity.secretsManager.core.*;
import java.util.List;

public class KSMSample {
    public static void main(String[] args){
        // 事前に初期化されたストレージを取得
        KeyValueStorage storage = new LocalConfigStorage("ksm-config.json");
        try {
            SecretsManagerOptions options = new SecretsManagerOptions(storage);

            // 必要な記録のUIDを含むフィルタを作成
            List<String> uidFilter = List.of("XXX");

            // フィルタを使用してシークレットを取得
            KeeperSecrets secrets = SecretsManager.getSecrets(options, uidFilter);

            // 取得結果から目的のシークレットを取得
            KeeperRecord myCredentials = secrets.getRecords().get(0);

            // パスワードを更新し、変更を記録に保存
            myCredentials.updatePassword("aP1$t367QOCvL$eM$bG#");
            SecretsManager.updateSecret(options, myCredentials);

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

パスワードの生成

import com.keepersecurity.secretsManager.core.*;
import java.util.List;
import java.util.Arrays;

public class KSMSample {
    public static void main(String[] args){
        // 事前に初期化されたストレージを取得
        KeyValueStorage storage = new LocalConfigStorage("ksm-config.json");
        try {
            SecretsManagerOptions options = new SecretsManagerOptions(storage);

            // 必要な記録のUIDを含むフィルタを作成
            List<String> uidFilter = Arrays.asList("XXX");

            // フィルタを使用してシークレットを取得
            KeeperSecrets secrets = SecretsManager.getSecrets(options, uidFilter);

            // 取得結果から目的のシークレットを取得
            KeeperRecord myCredentials = secrets.getRecords().get(0);

            // 新しいパスワードを作成
            String password = CryptoUtils.generatePassword();

            // 新しいパスワードを記録に設定
            myCredentials.updatePassword(password);
            
            // パスワードをKeeperに保存
            SecretsManager.updateSecret(options, myCredentials);

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

これらの例では、Secrets Managerの設定ファイルがすでに初期化済みであることを前提としています。

設定ファイルを初期化する方法については、初期化セクションをご参照ください。

シークレットの作成

Secrets Manager SDKを使用して、Keeperボルトに記録を作成できます。

記録を作成するには、Secrets Managerアプリケーションがアクセスできる共有フォルダのUIDが必要です。 また、共有フォルダの編集権限があり、フォルダに少なくとも1つの他の記録があることも必要です。新しい記録は、そのフォルダに格納されます。以下の例では、[FOLDER UID]を共有フォルダのUIDに置き換えます。

import com.keepersecurity.secretsManager.core.*;

import java.util.List;

public class KSMSample {
    // 新しい記録が格納されるフォルダ
    static String folderUid = "[FOLDER UID]";

    public static void addLoginRecord(){
        LocalConfigStorage storage = new LocalConfigStorage("ksm-config.txt");
        SecretsManagerOptions options = new SecretsManagerOptions(storage);

        KeeperSecrets secrets = SecretsManager.getSecrets(options);

        KeeperRecordData newRecordData = new KeeperRecordData(
                "Sample Example KSM Record",
                "login",
                List.of(
                        new Login("username@email.com"),
                        new Password("Pa$$word123")
                ),
                null,
                "\tThis record was created\n\tvia KSM Documentation Java Example"
        );

        SecretsManager.createSecret(options, folderUid, newRecordData, secrets);
    }

    public static void main(String[] args) {
        addLoginRecord();
    }
}

その他のシークレット作成機能や記録タイプ記録フィールドの検証は、Keeper Secrets Managerの今後のリリースで対応予定です。

シークレットの削除

Secrets Managerを使用して、Keeperボルトから記録を削除できます。

記録を削除するには、記録が格納されている共有フォルダにSecrets Managerアプリケーションがアクセスでき、アプリケーションに編集権限があることが必要です。 Secrets Manager SDKで記録を削除するには、UID記録が必要です。

// Secrets Managerを設定
val storage = LocalConfigStorage("ksm-config.json")
//initializeStorage(storage, "<One Time Access Token>")
val smOptions = SecretsManagerOptions(storage)

// UID記録で特定のシークレットを削除
deleteSecret(smOptions, List.of("EG6KdJaaLG7esRZbMnfbFA"));

スクリプトの実装

Keeper Secrets Manager CLIは、任意のシステムコールを実行し、環境変数をKeeperボルトの値に置き換えるラッパー関数を搭載しています。

Secrets Manager CLI Execコマンド

ボルトおよび管理者用SDK

ボルトおよび管理レベルの高度な機能については、様々な開発ツールへのリンクが記載されたボルトのSDKページをご参照ください。

ボルトのSDK

最終更新