Rust SDK

Detailed Rust SDK docs for Keeper Secrets Manager

Download and Installation

Adding as Package using Cargo

cargo add keeper-secrets-manager-core

Source Code

Find the Rust source code in the GitHub repository

Using the SDK

Initialise

Using token only to generate a new config (for later usage) requires at least one read operation to bind the token and fully populate config.json

SecretsManager::new(client_options)?

Parameter

Required

Description

Type

token

Yes

One Time Access Token

String

config

Yes

Storage Configuration

KeyValueStorage

Retrieve Secrets

let records_filter = Vec::new(); // add record filters of needed based on UID
let secrets = secrets_manager.get_secrets(records_filter)?;

Parameter

Type

Required

Default

Description

uids

Vec<String>

Optional

None

Record UIDs to fetch

Response

Type: Vec<Record>

All Keeper records, or records with the given UIDs

default - we will get all records which the token given has access to

Retrieve Values from secret

Retrieve a password

This shortcut gets the password of a secret once that secret has been retrieved from Keeper Secrets Manager.

secret.get_standard_field_value('password', true);

Retrieve Standard Fields

secret.get_standard_field_value(“FIELD_TYPE”.to_string(), true);
Parameter
Type
Required
Default
Description

field_type

String

Yes

None

Field type to get

single

boolean

Optional

False

Return only the first value

Field types are based on the Keeper Record Type. For a detailed list of available fields based on the Keeper Record Type, see the record-type-info command in Keeper Commander.

Retrieve Custom Fields

secret.get_custom_field_value(“FIELD_TYPE”, true);
Parameter
Type
Required
Default
Description

field_type

String

Yes

-

Field type to get

single

boolean

Optional

False

Return only the first value

Custom fields are any field that is not part of the record type definition but can be added by users.

Response

Type: String or Vec<String>

the value or values of the field. It will be a single value only if the single=true option is passed.

Records by Title

secrets_manager.get_secret_by_title(record_title);

Response

Type: Record<Option<Vec<Record>>>

Parameter
Type
Required
Description

record_title

&str

Yes

Title of the record to be fetched

Retrieve Values using Keeper Notation

secrets_manager.get_notation(query)

See Keeper Notation documentation to learn about Keeper Notation format and capabilities

Parameter
Type
Required
Default
Description

query

String

Yes

-

Keeper Notation query for getting a value from a specified field

Returns

The value of the queried field

Type: String or Vec<String>

Retrieve a TOTP Code

get_totp_code(&url)

Returns

Type: Result<TotpCode,KSMRError>

Parameter
Type
Required
Description

url

String

Yes

TOTP Url

Update a Secret

Save Changes to a Secret

secrets_manager.save(Record, UpdateTransactionType)
Parameter
Type
Required
Default
Description

record

Record

Yes

Storage and query configuration

transaction_type

UpdateTransactionType

Yes

Configuration for transactional update

Set field values using the set_standard_field_value_mut or the set_custom_field_value_mut method.

Fields are found by type.

For a list of field types, see the Record Types documentation. Some fields have multiple values in these cases, the value can be set to a list.

Update a Standard Field Value

secret.set_standard_field_value_mut(field_type, "new_field_value".into())
Parameter
Type
Required
Default
Description

field_type

String

Yes

Field type to get

transaction_type

UpdateTransactionType

Yes

None

Configuration for transactional update

Fields are found by type. For a list of field types, see the Record Types documentation.

Update a Custom Field Value

secret.set_custom_field_value_mut(field_type, "new_field_value".into());
Parameter
Type
Required
Default
Description

field_type

String

Yes

Field type to get

transaction_type

UpdateTransactionType

Yes

None

Configuration for transactional update

Generate a Random Password

generate_password_with_options(password_options);
Parameter
Type
Required
Default
Description

password_options

PasswordOptions

Yes

Configuration for the password

charset

String

Optional

Set of special characters to be included in the password

length

i32

Optional

64

Length of password

lowercase

i32

Optional

0

Count of lowercase characters in the password

uppercase

i32

Optional

0

Count of uppercase characters in the password

digits

i32

Optional

0

Count of digits in the password

special_characters

i32

Optional

0

Count of special characters in the password

Each parameter indicates the minimum number of a type of character to include. For example, 'uppercase' indicates the minimum number of uppercase letters to include.

Download a File

download_file(file_name, path);
Parameter
Type
Required
Default
Description

file_name

&str

Yes

Name of the file to be downloaded

path

&str

Yes

Path to download file

Upload File

upload_file(owner_record, keeper_file);

Upload File Parameters

Parameter
Type
Required
Default
Description

owner_record

Record

Yes

None

The record in which the file has to be uploaded

keeper_file

KeeperFileUpload

Yes

The file to be uploaded

File Parameters

Parameter
Type
Required
Default
Description

file_path

&str

Yes

Path to upload file

file_name

Option<&str>

Yes

Name of the file to be uploaded

file_title

Option<&str>

Yes

Title of the file to be uploaded

mime_type

Option<&str>

Yes

None

The type of data in the file. If none is provided, 'application/octet-stream' will be used

Returns

Type: String

The file UID of the attached file

Create a secret

Prerequisites:

  • Shared folder UID

    • The shared folder must be accessible by the Secrets Manager Application

    • You and the Secrets Manager application must have edit permission

    • There must be at least one record in the shared folder

  • Created records and record fields must be formatted correctly

    • See the documentation for expected field formats for each record type

  • TOTP fields accept only URL generated outside of the KSM SDK

  • After record creation, you can upload file attachments using upload_file

secrets_manager.create_secret(folder_uid, record);
Parameter
Type
Required
Default
Description

record_type

DefaultRecordType

Yes

None

Type of record to be created

title

String

Yes

The title of the created record

note

String

Yes

None

The note to be made in the created record

value

String

Yes

Value for the field

label

String

Yes

None

Label for the field

required

bool

Yes

false

Defines if the field is required

privacy_screen

bool

Yes

false

Defines if the field value should be hidden

Returns

Type: String

The record UID of the new record

Delete A Secret

The Rust KSM SDK can delete records in the Keeper Vault.

secrets_manager.delete_secret(vec![record_uid]);
Parameter
Type
Required
Default
Description

record_uid

String

Yes

None

The uid of the record to be deleted

Caching

To protect against losing access to your secrets when network access is lost, the Rust SDK allows caching of secrets to the local machine in an encrypted file.

Setup and Configure Cache

In order to setup caching in the Rust SDK, include a caching post function when creating a SecretsManager object.

The Rust SDK includes a default caching function in the KSMRCache class, which stores cached queries to a local file, thus serving as a disaster recovery function (as long as there's network connectivity, it always prefers network over cached data and will use cache only if the web vault is inaccessible).

use keeper_secrets_manager_core::{core::{ClientOptions, SecretsManager}, custom_error::KSMRError, storage::FileKeyValueStorage, cache::KSMRCache};
fn main()-> Result<(), KSMRError>{
    let cache = KSMRCache::new_file_cache(Some("./cache.bin"))?;

    let token = "<Token>".to_string();

    let file_name = FileKeyValueStorage::new_config_storage("test.json".to_string())?;
    
    let mut client_options = ClientOptions::new_client_options_with_token(token, file_name);
    client_options.set_cache(cache.into()); 
    
    let mut secrets_manager = SecretsManager::new(client_options)?;  
    let secrets = secrets_manager.get_secrets(Vec::new())?;
    for secret in secrets {
        info!("Secret: {}", secret);
    };
}

Folders

Folders have full CRUD support—create, read, update, and delete operations.

Read Folders

Downloads full folder hierarchy.

get_folders()

Returns

Type: Vec<KeeperFolder>

Create Folder

Requires CreateOptions and folder name to be provided. The folder UID parameter in CreateOptions is required—the UID of a shared folder, while sub-folder UID is optional, and if missing, a new regular folder is created directly under the parent (shared folder). There's no requirement for the sub-folder to be a direct descendant of the parent shared folder - it could be many levels deep.

create_folder(create_options: CreateOptions, folder_name: str, folders=None)
Parameter
Type
Required
Default
Description

create_options

CreateOptions

Yes

None

The parent and sub-folder UIDs

folder_name

str

Yes

The folder name

folders

Vec<KeeperFolder>

No

None

List of folders to use in the search for parent and sub-folder from CreateOptions

Update Folder

Updates the folder metadata—currently folder name only.

secrets_manager.update_folder(folder_uid: str, folder_name: str, folders=None)
Parameter
Type
Required
Default
Description

folder_uid

str

Yes

The folder uid

folder_name

str

Yes

The new folder name

folders

Vec<KeeperFolder>

No

None

List of folders to use in the search for parent folder

Delete Folders

Removes a list of folders. Use the force_deletion flag to remove non-empty folders.

Any folders UIDs missing from the vault or not shared to the KSM Application will not result in error.

When using force_deletion avoid sending parent with its children folder UIDs. Depending on the delete order you may get an error - ex. if parent force-deleted child first. There's no guarantee that list will always be processed in FIFO order.

delete_folder(vec![“<FOLDER_UID>”.to_string()], false);
Parameter
Type
Required
Default
Description

folder_uids

Vec<String>

Yes

The folder UID list

force_deletion

boolean

No

false

Force deletion of non-empty folders

Last updated

Was this helpful?