Package com.mwdle.bitwarden.cli
Class BitwardenCLI
java.lang.Object
com.mwdle.bitwarden.cli.BitwardenCLI
A utility class for executing Bitwarden CLI commands.
This class contains only static methods and holds no state. It is a thin wrapper around the
bw
executable, responsible for the low-level logic of constructing and running
ProcessBuilder
commands and interpreting their results.
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
configServer
(String serverUrl) Configures the Bitwarden CLI to point to a specific server URL.static BitwardenItem
Fetches a single, complete item from the vault by its ID.static List<BitwardenItemMetadata>
listItemsMetadata
(Secret sessionToken) Fetches a list of all item metadata from the vault.static void
login
(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials apiKey) Logs into the Bitwarden CLI using an API key.static void
logout()
Logs out of the Bitwarden CLI.static BitwardenStatus
Checks the status of the Bitwarden CLI session.static void
Syncs the local CLI database with the remote Bitwarden vault.static Secret
unlock
(org.jenkinsci.plugins.plaincredentials.StringCredentials masterPassword) Unlocks the vault using the Master Password and returns the session token.static String
version()
Fetches the version of the installed Bitwarden CLI.
-
Method Details
-
version
Fetches the version of the installed Bitwarden CLI.- Returns:
- The version string from the CLI.
- Throws:
IOException
- if the CLI command fails.InterruptedException
- if the thread is interrupted.
-
login
public static void login(com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials apiKey) throws IOException, InterruptedException Logs into the Bitwarden CLI using an API key.- Parameters:
apiKey
- The Jenkins credential containing the Bitwarden Client ID and Client Secret.- Throws:
IOException
- if the CLI command fails.BitwardenConnectionException
- if a network error occurs.BitwardenAuthenticationException
- if the provided API key is incorrect.InterruptedException
- if the thread is interrupted.
-
logout
Logs out of the Bitwarden CLI. This is a best-effort operation, and failures are ignored.- Throws:
InterruptedException
- if the thread is interrupted.
-
unlock
public static Secret unlock(org.jenkinsci.plugins.plaincredentials.StringCredentials masterPassword) throws IOException, InterruptedException Unlocks the vault using the Master Password and returns the session token.- Parameters:
masterPassword
- The Jenkins credential containing the Bitwarden Master Password.- Returns:
- The session token for subsequent commands.
- Throws:
IOException
- if the CLI command fails.BitwardenConnectionException
- if a network error occurs.BitwardenAuthenticationException
- if the provided Master Password is incorrect.InterruptedException
- if the thread is interrupted.
-
sync
Syncs the local CLI database with the remote Bitwarden vault.- Parameters:
sessionToken
- The active session token to use for authentication.- Throws:
IOException
- if the CLI command fails.BitwardenConnectionException
- if a network error occurs.InterruptedException
- if the thread is interrupted.
-
status
Checks the status of the Bitwarden CLI session.- Parameters:
sessionToken
- The session token to validate.- Returns:
- A
BitwardenStatus
object representing the current state. - Throws:
IOException
- if the CLI command fails or JSON parsing fails.InterruptedException
- if the thread is interrupted.
-
listItemsMetadata
public static List<BitwardenItemMetadata> listItemsMetadata(Secret sessionToken) throws IOException, InterruptedException Fetches a list of all item metadata from the vault.- Parameters:
sessionToken
- The active session token to use for authentication.- Returns:
- A List of
BitwardenItemMetadata
objects. - Throws:
IOException
- if the CLI command fails or JSON parsing fails.InterruptedException
- if the command is interrupted.
-
getItem
public static BitwardenItem getItem(Secret sessionToken, String itemId) throws IOException, InterruptedException Fetches a single, complete item from the vault by its ID.- Parameters:
sessionToken
- The active session token.itemId
- The UUID of the item to fetch.- Returns:
- A complete
BitwardenItem
object. - Throws:
IOException
- if the CLI command fails or JSON parsing fails.InterruptedException
- if the command is interrupted.
-
configServer
Configures the Bitwarden CLI to point to a specific server URL.- Parameters:
serverUrl
- The URL of the self-hosted Bitwarden or Vaultwarden instance.- Throws:
IOException
- if the CLI command fails.InterruptedException
- if the CLI command is interrupted.
-