Class Utils
java.lang.Object
org.jboss.as.test.integration.security.common.CoreUtils
org.jboss.as.test.integration.security.common.Utils
public class Utils
extends org.jboss.as.test.integration.security.common.CoreUtils
Common utilities for JBoss AS security tests.
- Author:
- Jan Lanik, Josef Cacek
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.apache.http.client.RedirectStrategyThe REDIRECT_STRATEGY for Apache HTTP Clientstatic final StringFields inherited from class org.jboss.as.test.integration.security.common.CoreUtils
IBM_JDK, KEYSTORE_CLIENT_ALIAS, KEYSTORE_SERVER_ALIAS, KEYSTORE_UNTRUSTED_ALIAS -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidapplyUpdate(org.jboss.dmr.ModelNode update, org.jboss.as.controller.client.ModelControllerClient client) static voidapplyUpdates(List<org.jboss.dmr.ModelNode> updates, org.jboss.as.controller.client.ModelControllerClient client) static voidcreateKeyMaterial(File workingFolder) Copies server and clients keystores and truststores from this package to the given folder.static FilecreateTemporaryFolder(String prefix) Creates a temporary folder name with given name prefix.static StringcreateUsersFromRoles(String... roles) Creates content of users.properties and/or roles.properties files for given array of role names.static StringencodeQueryParam(String paramName, String paramValue) Returns param/value pair in form "urlEncodedName=urlEncodedValue".static voidfixApacheDSTransportAddress(ManagedCreateLdapServer createLdapServer, String address) Fixes/replaces LDAP bind address in the CreateTransport annotation of ApacheDS.static StringgetContent(org.apache.http.HttpResponse response) Read the contents of an HttpResponse's entity and return it as a String.static StringgetDefaultHost(boolean canonical) Returns hostname - either read from the "node0" system property or the loopback address "127.0.0.1".static StringgetHost(org.jboss.as.arquillian.container.ManagementClient managementClient) Returns management address (host) from the givemManagementClient.static org.jboss.shrinkwrap.api.asset.AssetgetJBossDeploymentStructure(String... dependencies) Generates content of the jboss-deployment-structure.xml deployment descriptor as a ShrinkWrap asset.static org.jboss.shrinkwrap.api.asset.AssetgetJBossEjb3XmlAsset(String securityDomain) Generates content of jboss-ejb3.xml file as a ShrinkWrap asset with the given security domain name.static org.jboss.shrinkwrap.api.asset.AssetgetJBossWebXmlAsset(String securityDomain, String... valveClassNames) Generates content of jboss-web.xml file as a ShrinkWrap asset with the given security domain name and given valve class.static ConfigurationReturns installed login configuration.static URLgetResource(String name) static StringgetSecondaryTestAddress(org.jboss.as.arquillian.container.ManagementClient mgmtClient) Returns "secondary.test.address" system property if such exists.static StringgetSecondaryTestAddress(org.jboss.as.arquillian.container.ManagementClient mgmtClient, boolean useCanonicalHost) Returns "secondary.test.address" system property if such exists.static Stringstatic LoginContextloginWithKerberos(Krb5LoginConfiguration krb5Configuration, String user, String pass) Creates login context for givenKrb5LoginConfigurationand credentials and calls theLoginContext.login()method on it.static voidMakes HTTP call with FORM authentication.static StringMakes HTTP call without authentication.static StringmakeCallWithBasicAuthn(URL url, String user, String pass, int expectedStatusCode) Returns response body for the given URL request as a String.static StringmakeCallWithBasicAuthn(URL url, String user, String pass, int expectedStatusCode, boolean checkFollowupAuthState) Returns response body for the given URL request as a String.static StringmakeCallWithHttpClient(URL url, org.apache.http.client.HttpClient httpClient, int expectedStatusCode) Requests given URL and checks if the returned HTTP status code is the expected one.static StringmakeCallWithTokenAuthn(URL url, String token, int expectedStatusCode) Do HTTP request using given client with BEARER_TOKEN authentication.The implementation makes 2 calls - the first without Authorization header provided (just to check response code and WWW-Authenticate header value), the second with Authorization header.static voidstop()stops execution of the program indefinitely useful in testsuite debuggingstatic voidstop(long delay) stop test execution for a given time interval useful for debuggingstatic StringStrips square brackets - '[' and ']' from the given string.static StringtoHex(byte[] bytes) Methods inherited from class org.jboss.as.test.integration.security.common.CoreUtils
createKeyMaterial, getCannonicalHost, hashMD5, propertiesReplacer, propertiesReplacer, propertiesReplacer, replaceHost, saveArchive, saveArchiveToFolder, setSystemProperty
-
Field Details
-
UTF_8
- See Also:
-
REDIRECT_STRATEGY
public static final org.apache.http.client.RedirectStrategy REDIRECT_STRATEGYThe REDIRECT_STRATEGY for Apache HTTP Client
-
-
Constructor Details
-
Utils
public Utils()
-
-
Method Details
-
hash
-
toHex
-
getResource
-
stop
public static void stop()stops execution of the program indefinitely useful in testsuite debugging -
stop
public static void stop(long delay) stop test execution for a given time interval useful for debugging- Parameters:
delay- interval (milliseconds), if delay<=0, interval is considered to be infinite (Long.MAX_VALUE)
-
applyUpdates
public static void applyUpdates(List<org.jboss.dmr.ModelNode> updates, org.jboss.as.controller.client.ModelControllerClient client) throws Exception - Throws:
Exception
-
applyUpdate
public static void applyUpdate(org.jboss.dmr.ModelNode update, org.jboss.as.controller.client.ModelControllerClient client) throws Exception - Throws:
Exception
-
getContent
Read the contents of an HttpResponse's entity and return it as a String. The content is converted using the character set from the entity (if any), failing that, "ISO-8859-1" is used.- Parameters:
response-- Returns:
- Throws:
IOException
-
makeCall
public static void makeCall(String URL, String user, String pass, int expectedStatusCode) throws Exception Makes HTTP call with FORM authentication.- Parameters:
URL-user-pass-expectedStatusCode-- Throws:
Exception
-
getSecondaryTestAddress
public static String getSecondaryTestAddress(org.jboss.as.arquillian.container.ManagementClient mgmtClient, boolean useCanonicalHost) Returns "secondary.test.address" system property if such exists. If not found, then there is a fallback toManagementClient.getMgmtAddress()orgetDefaultHost(boolean)(when mgmtClient isnull). Returned value can be converted to canonical hostname if useCanonicalHost==true. Returned value is not formatted for URLs (i.e. square brackets are not placed around IPv6 addr - for instance "::1")- Parameters:
mgmtClient- management client instance (may benull)useCanonicalHost-- Returns:
-
getSecondaryTestAddress
public static String getSecondaryTestAddress(org.jboss.as.arquillian.container.ManagementClient mgmtClient) Returns "secondary.test.address" system property if such exists. If not found, then there is a fallback toManagementClient.getMgmtAddress(). Returned value is formatted to use in URLs (i.e. if it's IPv6 address, then square brackets are placed around - e.g. "[::1]")- Parameters:
mgmtClient- management client instance (may benull)- Returns:
-
makeCallWithHttpClient
public static String makeCallWithHttpClient(URL url, org.apache.http.client.HttpClient httpClient, int expectedStatusCode) throws IOException, URISyntaxException Requests given URL and checks if the returned HTTP status code is the expected one. Returns HTTP response body- Parameters:
url- URL to which the request should be madehttpClient- DefaultHttpClient to test multiple accessexpectedStatusCode- expected status code returned from the requested server- Returns:
- HTTP response body
- Throws:
IOExceptionURISyntaxException
-
makeCallWithBasicAuthn
public static String makeCallWithBasicAuthn(URL url, String user, String pass, int expectedStatusCode) throws IOException, URISyntaxException Returns response body for the given URL request as a String. It also checks if the returned HTTP status code is the expected one. If the server returnsHttpServletResponse.SC_UNAUTHORIZEDand username is provided, then a new request is created with the provided credentials (basic authentication).- Parameters:
url- URL to which the request should be madeuser- Username (may be null)pass- Password (may be null)expectedStatusCode- expected status code returned from the requested server- Returns:
- HTTP response body
- Throws:
IOExceptionURISyntaxException
-
makeCallWithBasicAuthn
public static String makeCallWithBasicAuthn(URL url, String user, String pass, int expectedStatusCode, boolean checkFollowupAuthState) throws IOException, URISyntaxException Returns response body for the given URL request as a String. It also checks if the returned HTTP status code is the expected one. If the server returnsHttpServletResponse.SC_UNAUTHORIZEDand username is provided, then a new request is created with the provided credentials (basic authentication).- Parameters:
url- URL to which the request should be madeuser- Username (may be null)pass- Password (may be null)expectedStatusCode- expected status code returned from the requested servercheckFollowupAuthState- whether to check auth state for followup request - if set to true, followup request is sent to server and 200 OK is expected directly (no re-authentication challenge - 401 Unauthorized - is expected)- Returns:
- HTTP response body
- Throws:
IOExceptionURISyntaxException
-
makeCallWithTokenAuthn
public static String makeCallWithTokenAuthn(URL url, String token, int expectedStatusCode) throws URISyntaxException, IOException, org.apache.http.client.ClientProtocolException Do HTTP request using given client with BEARER_TOKEN authentication.The implementation makes 2 calls - the first without Authorization header provided (just to check response code and WWW-Authenticate header value), the second with Authorization header.- Parameters:
url- URL to make request totoken- bearer tokenexpectedStatusCode- expected status code- Returns:
- response body
- Throws:
URISyntaxExceptionUnsupportedEncodingExceptionorg.apache.http.client.ClientProtocolExceptionIOException
-
getJBossEjb3XmlAsset
Generates content of jboss-ejb3.xml file as a ShrinkWrap asset with the given security domain name.- Parameters:
securityDomain- security domain name- Returns:
- Asset instance
-
getJBossWebXmlAsset
public static org.jboss.shrinkwrap.api.asset.Asset getJBossWebXmlAsset(String securityDomain, String... valveClassNames) Generates content of jboss-web.xml file as a ShrinkWrap asset with the given security domain name and given valve class.- Parameters:
securityDomain- security domain name (not-null)valveClassNames- valve class (e.g. an Authenticator) which should be added to jboss-web file (may benull)- Returns:
- Asset instance
-
getJBossDeploymentStructure
public static org.jboss.shrinkwrap.api.asset.Asset getJBossDeploymentStructure(String... dependencies) Generates content of the jboss-deployment-structure.xml deployment descriptor as a ShrinkWrap asset. It fills the given dependencies (module names) into it.- Parameters:
dependencies- AS module names- Returns:
-
createUsersFromRoles
Creates content of users.properties and/or roles.properties files for given array of role names.For instance if you provide 2 roles - "role1", "role2" then the result will be:
role1=role1 role2=role2
If you use it as users.properties and roles.properties, thenroleName == userName == password- Parameters:
roles- role names (used also as user names and passwords)- Returns:
- not-
nullcontent of users.properties and/or roles.properties
-
stripSquareBrackets
Strips square brackets - '[' and ']' from the given string. It can be used for instance to remove the square brackets around IPv6 address in a URL.- Parameters:
str- string to strip- Returns:
- str without square brackets in it
-
fixApacheDSTransportAddress
public static void fixApacheDSTransportAddress(ManagedCreateLdapServer createLdapServer, String address) Fixes/replaces LDAP bind address in the CreateTransport annotation of ApacheDS.- Parameters:
createLdapServer-address-
-
createKeyMaterial
public static void createKeyMaterial(File workingFolder) throws IOException, IllegalArgumentException Copies server and clients keystores and truststores from this package to the given folder. Server truststore has accepted certificate from client keystore and vice-versa- Parameters:
workingFolder- folder to which key material should be copied- Throws:
IOException- copying of keystores failsIllegalArgumentException- workingFolder is null or it's not a directory
-
makeCall
Makes HTTP call without authentication. Returns response body as a String.- Parameters:
uri- requested URLexpectedStatusCode- expected status code - it's checked after the request is executed- Throws:
Exception
-
encodeQueryParam
Returns param/value pair in form "urlEncodedName=urlEncodedValue". It can be used for instance in HTTP get queries.- Parameters:
paramName- parameter nameparamValue- parameter value- Returns:
- "[urlEncodedName]=[urlEncodedValue]" string
-
getHost
Returns management address (host) from the givemManagementClient. If the returned value is IPv6 address then square brackets around are stripped.- Parameters:
managementClient-- Returns:
-
getDefaultHost
Returns hostname - either read from the "node0" system property or the loopback address "127.0.0.1".- Parameters:
canonical- return hostname in canonical form- Returns:
-
getLoginConfiguration
Returns installed login configuration.- Returns:
- Configuration
-
loginWithKerberos
public static LoginContext loginWithKerberos(Krb5LoginConfiguration krb5Configuration, String user, String pass) throws LoginException Creates login context for givenKrb5LoginConfigurationand credentials and calls theLoginContext.login()method on it. This method contains workaround for IBM JDK issue described in bugzilla https://bugzilla.redhat.com/show_bug.cgi?id=1206177.- Parameters:
krb5Configuration-user-pass-- Returns:
- Throws:
LoginException
-
createTemporaryFolder
Creates a temporary folder name with given name prefix.- Parameters:
prefix- folder name prefix- Returns:
- created folder
- Throws:
IOException
-