Class ExampleUtil
java.lang.Object
co.ankatech.ankasecure.sdk.examples.ExampleUtil
Utility class providing shared support for all ANKASecure SDK example scenarios.
This class centralizes common functionality used by the Example* programs, including:
- Loading CLI configuration from files or classpath resources
- Authenticating via encrypted credentials or a pre-issued JWT token
- JSON serialization and deserialization with ISO-8601 date/time support
- Deriving and decrypting cryptographic keys (PBKDF2, AES-GCM)
- Ensuring and managing a temporary working directory for example artifacts
- Uniform error handling that classifies SDK exceptions (timeout, HTTP, I/O, etc.)
- Rendering operation metadata (encrypt, decrypt, sign, verify, re-encrypt)
- Comparing input streams for equality
All methods are static and intended for one-off use by the ExampleScenario classes.
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic AnkaSecureSdk
authenticate
(Properties props) Authenticates and initializes an instance ofAnkaSecureSdk
using the provided properties.static AnkaSecureSdk
authenticateWithToken
(Properties props) Creates an AnkaSecureSdk instance using a manually provided JWT token.static void
Ensures that the temporary directory represented by the constant `TEMP_DIR` exists.static void
ensureTempDir
(Path dir) Ensures that the specified temporary directory exists.static void
Logs a fatal error message to the standard error stream, provides additional context if the error is related to the SDK, and terminates the application with exit code 1.static Properties
Loads properties from a configuration file.static String
Returns a non-null, safe string representation of the provided input.static void
Logs decryption metadata in a human-readable format.static void
Logs decryption-side metadata in a human-readable format.static void
Logs decryption metadata in a human-readable format, including information about the requested key, actual key used, algorithm used, and any warnings.static void
Logs metadata associated with an encryption operation in a human-readable format.static void
Logs metadata associated with an encryption operation in a human-readable format.static void
Logs metadata associated with the re-encryption operation in a human-readable format, including information about keys and algorithms used during the process, as well as any warnings.static void
Logs metadata associated with the re-signing operation in a human-readable format.static void
Logs metadata associated with the signing operation in a human-readable format.static void
printSignMeta
(String heading, SignResult m) Logs metadata associated with the signing operation in a human-readable format, including a descriptive heading for context.static void
Logs metadata associated with the verification operation in a human-readable format.static void
printVerifyMeta
(String heading, VerifySignatureResult m) Logs metadata associated with the verification operation in a human-readable format, including information about validation, keys used, algorithm, and warnings if any.static void
printWarnings
(List<String> warnings) Prints a list of warning messages to the console in a formatted manner.static boolean
Compares two InputStream objects to determine if they have identical content.static String
Converts a given object into its JSON string representation.
-
Field Details
-
TEMP_DIR
Working directory for the temporary artifacts produced by examples.
-
-
Method Details
-
loadProperties
Loads properties from a configuration file. The method checks for a local "cli.properties" file, a file specified by the "cli.config" system property, and a default "cli.properties" file on the classpath. If none of these are found, an exception is thrown.- Returns:
- a Properties object containing the key-value pairs from the loaded configuration file
-
authenticateWithToken
Creates an AnkaSecureSdk instance using a manually provided JWT token.This method bypasses the authentication flow entirely. It is useful for debugging or trusted execution environments where the token is issued externally (e.g. from a deployed auth-service).
Requires the presence of a property
client.accessToken
in thecli.properties
file.Warning: It is your responsibility to ensure the token is valid and not expired.
- Parameters:
props
- CLI properties loaded vialoadProperties()
- Returns:
- a token-initialized
AnkaSecureSdk
instance - Throws:
AnkaSecureSdkException
-
authenticate
Authenticates and initializes an instance ofAnkaSecureSdk
using the provided properties. The method retrieves client-specific credentials (UUID, salt, encrypted client ID, and encrypted client secret) from the givenProperties
object, derives a cryptographic key, decrypts the credentials, and performs application authentication. On successful authentication, it returns an instance ofAnkaSecureSdk
.- Parameters:
props
- TheProperties
containing the necessary authentication information: - `client.uuid`: The unique identifier for the client. - `client.salt`: The salt value for key derivation. - `clientIdEnc`: The encrypted client ID. - `clientSecretEnc`: The encrypted client secret. All four properties must be non-null and properly initialized.- Returns:
- An instance of
AnkaSecureSdk
if authentication is successful. Returnsnull
in case of authentication or decryption errors (execution will halt with a fatal error log).
-
toJson
Converts a given object into its JSON string representation.- Parameters:
o
- the object to be converted into JSON format- Returns:
- a JSON string representation of the given object
- Throws:
UncheckedIOException
- if the JSON serialization process fails
-
ensureTempDir
Ensures that the specified temporary directory exists. If it does not exist, it attempts to create it.- Parameters:
dir
- The path to the directory that needs to be checked or created.
-
fatal
Logs a fatal error message to the standard error stream, provides additional context if the error is related to the SDK, and terminates the application with exit code 1.- Parameters:
msg
- The error message to be logged.t
- The exception causing the fatal error, which may be an instance ofAnkaSecureSdkException
or anotherThrowable
. Can be null.
-
printEncryptionMeta
Logs metadata associated with an encryption operation in a human-readable format. This includes information about the requested key, actual key used, algorithm utilized, and any warnings generated during the process.- Parameters:
r
- theEncryptResult
containing encryption metadata, which includes: - the key ID originally requested by the client, - the key ID actually used by the server, - the algorithm used for encryption, - and any warnings or informational messages associated with the operation.
-
printDecryptionMeta
Logs decryption metadata in a human-readable format. The metadata includes details about the key requested, the actual key used, the algorithm used in the decryption process, and any non-fatal warnings encountered.- Parameters:
r
- theDecryptResultMetadata
containing decryption-related information, including: - the key ID originally requested, - the key ID actually used, - the algorithm utilized, - and any warnings, if present.
-
printWarnings
Prints a list of warning messages to the console in a formatted manner. Each warning is indented and prefixed with a bullet point.- Parameters:
warnings
- a list of strings representing warning messages to be displayed. If the list is null or empty, no output will be generated.
-
nullSafe
Returns a non-null, safe string representation of the provided input. If the input string is null or blank, the method returns the string "(none)".- Parameters:
s
- the input string that may be null or blank- Returns:
- the input string if it is not null or blank; otherwise, the string "(none)"
-
ensureTempDir
public static void ensureTempDir()Ensures that the temporary directory represented by the constant `TEMP_DIR` exists. Creates the directory, including any necessary but nonexistent parent directories. If the operation fails, this method terminates the JVM with an error message. Any issues encountered during the creation process are handled by invoking thefatal(String, Throwable)
method, which outputs the error message and exits the application. This method is typically used to guarantee the existence of a directory for temporary file storage required by the application.- Throws:
UncheckedIOException
- if the directory cannot be created
-
printSignMeta
Logs metadata associated with the signing operation in a human-readable format. Includes details such as the requested key, actual key used, algorithm, and any warnings.- Parameters:
r
- theSignResult
containing metadata about the signing operation, which includes: - the key ID originally requested by the client, - the key ID actually used by the server, - the algorithm used during the signing process, - and any non-fatal warnings, if present.
-
printVerifyMeta
Logs metadata associated with the verification operation in a human-readable format. Includes details about the requested key, actual key used, algorithm utilized, and any warnings generated.- Parameters:
r
- theVerifySignatureResult
containing verification metadata, which includes: - the key ID originally requested by the client, - the key ID actually used by the server, - the algorithm used during the verification process, - and any non-fatal warnings, if present.
-
printEncryptMeta
Logs metadata associated with an encryption operation in a human-readable format. Includes details about the key requested, actual key used, algorithm utilized, and any warnings generated.- Parameters:
m
- theEncryptResult
containing encryption metadata, which includes: - the key ID originally requested by the client, - the key ID actually used by the server, - the algorithm used during the encryption process, - and any non-fatal warnings, if present.
-
printDecryptMeta
Logs decryption metadata in a human-readable format, including information about the requested key, actual key used, algorithm used, and any warnings.- Parameters:
m
- theDecryptResultMetadata
containing the metadata for the decryption process. It includes: - the key ID originally requested by the client, - the key ID actually used by the server, - the algorithm negotiated by the crypto engine, - and non-fatal warnings, if any.
-
streamsAreEqual
Compares two InputStream objects to determine if they have identical content. This method reads both streams concurrently and compares their data byte by byte.- Parameters:
a
- the first InputStream to compareb
- the second InputStream to compare- Returns:
- true if both InputStreams have identical content and reach EOF simultaneously; false otherwise
- Throws:
IOException
- if an I/O error occurs while reading from either InputStream
-
printReencryptMeta
Logs metadata associated with the re-encryption operation in a human-readable format, including information about keys and algorithms used during the process, as well as any warnings.- Parameters:
m
- theReencryptResult
containing metadata related to the re-encryption operation, including details such as the old key requested, old key used, old algorithm, new key requested, new key used, new algorithm, and any warnings associated.
-
printSignMeta
Logs metadata associated with the signing operation in a human-readable format, including a descriptive heading for context.- Parameters:
heading
- a contextual or descriptive heading to prepend to the logged outputm
- theSignResult
containing metadata about the signing process
-
printResignMeta
Logs metadata associated with the re-signing operation in a human-readable format.- Parameters:
m
- theResignResult
containing metadata about the re-signing process
-
printVerifyMeta
Logs metadata associated with the verification operation in a human-readable format, including information about validation, keys used, algorithm, and warnings if any.- Parameters:
heading
- a descriptive heading or context label for the logged outputm
- theVerifySignatureResult
containing verification metadata
-
printDecryptMeta
Logs decryption-side metadata in a human-readable format.- Parameters:
r
- theDecryptResult
containing the decryption metadata and decrypted data. Its metadata includes: - the key ID originally requested by the client, - the key ID actually used by the server (ornull
if identical to the requested key), - the algorithm negotiated by the crypto-engine, - and non-fatal warnings, if any.
-