Overview
AccountFactory is a utility class that creates Account instances and populates them with credentials from various sources. It supports loading credentials from Vault, YAML files, or environment variables.
Class signature
software.tnb.common.account
Methods
create(Class)
Creates an instance of the specified Account class and populates it with credentials from configured loaders.The Account class to instantiate and populate
A new instance of the specified Account class with credentials loaded
T- Account type that extendsAccount
defaultLoader()
Creates the default credentials loader chain based on available configuration. Loaders are checked in priority order:- Vault (with token authentication)
- Vault (with AppRole authentication)
- YAML string from configuration
- YAML file from configuration
A DelegatingCredentialsLoader that tries each configured loader in sequence
setCredentialsLoader(CredentialsLoader)
Sets a custom credentials loader to override the default loading mechanism.The custom CredentialsLoader to use for loading account credentials
Credentials loading mechanism
AccountFactory uses a flexible credentials loading system:Loader priority
The default loader checks sources in this order:- Vault with token - If
test.credentials.vault.tokenis configured - Vault with AppRole - If
test.credentials.vault.role-idandtest.credentials.vault.secret-idare configured - YAML string - If
test.credentialscontains YAML content - YAML file - If
test.credentials.filepoints to a file
Credential IDs
For accounts implementingWithId, the factory resolves credential IDs hierarchically:
- Walks up the class hierarchy
- Collects IDs from each level that implements
WithId - Uses the ID list to look up credentials
Accounts that don’t implement
WithId are instantiated without credential loading.Configuration
Configure credential sources using system properties or test configuration:Custom credentials loaders
You can implement custom credential loading logic:Built-in credential loaders
VaultCredentialsLoader
Loads credentials from HashiCorp Vault:YamlCredentialsLoader
Loads credentials from YAML files or strings:DelegatingCredentialsLoader
Tries multiple loaders in sequence:Error handling
AccountFactory handles errors as follows:- No credentials needed: Accounts without
WithIdare created without loading - Load failure: Throws test failure via
fail()with detailed error message - Instantiation failure: Wraps exceptions in
RuntimeException
Related
- ServiceFactory - Factory for creating Service instances
- Service - Base class that uses accounts
- ConfigurableService - Service with configuration support