Skip to content

Commit

Permalink
feat(secrets): Create PoC Python bindings that use secrets-core
Browse files Browse the repository at this point in the history
Signed-off-by: Trae Yelovich <[email protected]>
  • Loading branch information
traeok committed Oct 31, 2023
1 parent a85090c commit c0fbaec
Show file tree
Hide file tree
Showing 30 changed files with 189 additions and 1,431 deletions.
343 changes: 114 additions & 229 deletions src/secrets/src/keyring/Cargo.lock → src/secrets/src/core/Cargo.lock

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
[package]
name = "secrets_core"
version = "0.1.0"
edition = "2021"
name = "keyring"
version = "1.0.0"
authors = ["Zowe Project"]
license = "EPL-2.0"
repository = "https://github.com/zowe/zowe-cli"

[lib]
name = "keyring"
crate-type = ["cdylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cfg-if = "1.0"
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2", default-features = false, features = ["napi4"] }
napi-derive = "2"
thiserror = "1.0.38"

[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
Expand All @@ -36,12 +28,4 @@ glib = "0.18.2"
glib-sys = "0.18.1"
gio = "0.18.2"
libsecret = "0.4.0"
libsecret-sys = "0.4.0"

[build-dependencies]
napi-build = "2"

[profile.release]
lto = true
opt-level = "z" # Optimize for size.
strip = "symbols"
libsecret-sys = "0.4.0"
1 change: 1 addition & 0 deletions src/secrets/src/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod os;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ impl From<WIN32_ERROR> for KeyringError {

///
/// Helper function to convert the last Win32 error into a human-readable error message.
///
/// Returns:
///
/// Returns:
/// A `String` object containing the error message
///
///
fn win32_error_as_string(error: WIN32_ERROR) -> String {
let buffer: PWSTR = std::ptr::null_mut();

Expand Down Expand Up @@ -62,30 +62,30 @@ fn win32_error_as_string(error: WIN32_ERROR) -> String {

///
/// Helper function to encode a string as a null-terminated UTF-16 string for use w/ credential APIs.
///
///
/// Returns:
/// - `Some(val)` if the string was successfully converted to UTF-16, or `None` otherwise.
///
///
fn encode_utf16(str: &str) -> Vec<u16> {
let mut chars: Vec<u16> = str.encode_utf16().collect();
chars.push(0);
chars
}

///
///
/// Attempts to set a password for a given service and account.
///
///
/// - `service`: The service name for the new credential
/// - `account`: The account name for the new credential
///
///
/// Returns:
/// - `true` if the credential was stored successfully
/// - A `KeyringError` if there were any issues interacting with the credential vault
///
pub fn set_password(
service: &String,
account: &String,
password: &mut String,
password: &String,
) -> Result<bool, KeyringError> {
// Build WinAPI strings and object parameters from arguments
let target_bytes = encode_utf16(format!("{}/{}", service, account).as_str());
Expand Down Expand Up @@ -120,16 +120,16 @@ pub fn set_password(
Ok(true)
}

///
///
/// Returns a password contained in the given service and account, if found.
///
///
/// - `service`: The service name that matches the credential of interest
/// - `account`: The account name that matches the credential of interest
///
///
/// Returns:
/// - `Some(password)` if a matching credential was found; `None` otherwise
/// - A `KeyringError` if there were any issues interacting with the credential vault
///
///
pub fn get_password(service: &String, account: &String) -> Result<Option<String>, KeyringError> {
let mut cred: *mut CREDENTIALW = std::ptr::null_mut::<CREDENTIALW>();
let target_name = encode_utf16(format!("{}/{}", service, account).as_str());
Expand Down Expand Up @@ -173,16 +173,16 @@ pub fn get_password(service: &String, account: &String) -> Result<Option<String>
}
}

///
///
/// Attempts to delete the password associated with a given service and account.
///
///
/// - `service`: The service name of the credential to delete
/// - `account`: The account name of the credential to delete
///
///
/// Returns:
/// - `true` if a matching credential was deleted; `false` otherwise
/// - A `KeyringError` if there were any issues interacting with the credential vault
///
///
pub fn delete_password(service: &String, account: &String) -> Result<bool, KeyringError> {
let target_name = encode_utf16(format!("{}/{}", service, account).as_str());

Expand All @@ -204,15 +204,15 @@ pub fn delete_password(service: &String, account: &String) -> Result<bool, Keyri
Ok(true)
}

///
///
/// Returns the first password (if any) that matches the given service pattern.
///
///
/// - `service`: The service pattern that matches the credential of interest
///
///
/// Returns:
/// - `Some(password)` if a matching credential was found; `None` otherwise
/// - A `KeyringError` if there were any issues interacting with the credential vault
///
///
pub fn find_password(service: &String) -> Result<Option<String>, KeyringError> {
let filter = encode_utf16(format!("{}*", service).as_str());

Expand Down Expand Up @@ -257,16 +257,16 @@ pub fn find_password(service: &String) -> Result<Option<String>, KeyringError> {
}
}

///
///
/// Builds a vector of all credentials matching the given service pattern.
///
///
/// - `service`: The service pattern that matches the credential(s) of interest
/// - `credentials`: The vector consisting of (username, password) pairs for each credential that matches
///
/// - `credentials`: The vector consisting of (username, password) pairs for each credential that matches
///
/// Returns:
/// - `true` if at least 1 credential was found, `false` otherwise
/// - A `KeyringError` if there were any issues interacting with the credential vault
///
///
pub fn find_credentials(
service: &String,
credentials: &mut Vec<(String, String)>,
Expand Down
Loading

0 comments on commit c0fbaec

Please sign in to comment.