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

    Fields
    Modifier and Type
    Field
    Description
    static final org.apache.http.client.RedirectStrategy
    The REDIRECT_STRATEGY for Apache HTTP Client
    static final String
     

    Fields 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    applyUpdate(org.jboss.dmr.ModelNode update, org.jboss.as.controller.client.ModelControllerClient client)
     
    static void
    applyUpdates(List<org.jboss.dmr.ModelNode> updates, org.jboss.as.controller.client.ModelControllerClient client)
     
    static void
    createKeyMaterial(File workingFolder)
    Copies server and clients keystores and truststores from this package to the given folder.
    static File
    Creates a temporary folder name with given name prefix.
    static String
    Creates content of users.properties and/or roles.properties files for given array of role names.
    static String
    encodeQueryParam(String paramName, String paramValue)
    Returns param/value pair in form "urlEncodedName=urlEncodedValue".
    static void
    Fixes/replaces LDAP bind address in the CreateTransport annotation of ApacheDS.
    static String
    getContent(org.apache.http.HttpResponse response)
    Read the contents of an HttpResponse's entity and return it as a String.
    static String
    getDefaultHost(boolean canonical)
    Returns hostname - either read from the "node0" system property or the loopback address "127.0.0.1".
    static String
    getHost(org.jboss.as.arquillian.container.ManagementClient managementClient)
    Returns management address (host) from the givem ManagementClient.
    static org.jboss.shrinkwrap.api.asset.Asset
    Generates content of the jboss-deployment-structure.xml deployment descriptor as a ShrinkWrap asset.
    static org.jboss.shrinkwrap.api.asset.Asset
    getJBossEjb3XmlAsset(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.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.
    Returns installed login configuration.
    static URL
     
    static String
    getSecondaryTestAddress(org.jboss.as.arquillian.container.ManagementClient mgmtClient)
    Returns "secondary.test.address" system property if such exists.
    static String
    getSecondaryTestAddress(org.jboss.as.arquillian.container.ManagementClient mgmtClient, boolean useCanonicalHost)
    Returns "secondary.test.address" system property if such exists.
    static String
    hash(String target, String algorithm, org.jboss.as.test.integration.security.common.Coding coding)
     
    loginWithKerberos(Krb5LoginConfiguration krb5Configuration, String user, String pass)
    Creates login context for given Krb5LoginConfiguration and credentials and calls the LoginContext.login() method on it.
    static void
    makeCall(String URL, String user, String pass, int expectedStatusCode)
    Makes HTTP call with FORM authentication.
    static String
    makeCall(URI uri, int expectedStatusCode)
    Makes HTTP call without authentication.
    static String
    makeCallWithBasicAuthn(URL url, String user, String pass, int expectedStatusCode)
    Returns response body for the given URL request as a String.
    static String
    makeCallWithBasicAuthn(URL url, String user, String pass, int expectedStatusCode, boolean checkFollowupAuthState)
    Returns response body for the given URL request as a String.
    static String
    makeCallWithHttpClient(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 String
    makeCallWithTokenAuthn(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 void
    stops execution of the program indefinitely useful in testsuite debugging
    static void
    stop(long delay)
    stop test execution for a given time interval useful for debugging
    static String
    Strips square brackets - '[' and ']' from the given string.
    static String
    toHex(byte[] bytes)
     

    Methods inherited from class org.jboss.as.test.integration.security.common.CoreUtils

    createKeyMaterial, getCannonicalHost, hashMD5, propertiesReplacer, propertiesReplacer, propertiesReplacer, replaceHost, saveArchive, saveArchiveToFolder, setSystemProperty

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • UTF_8

      public static final String UTF_8
      See Also:
    • REDIRECT_STRATEGY

      public static final org.apache.http.client.RedirectStrategy REDIRECT_STRATEGY
      The REDIRECT_STRATEGY for Apache HTTP Client
  • Constructor Details

    • Utils

      public Utils()
  • Method Details

    • hash

      public static String hash(String target, String algorithm, org.jboss.as.test.integration.security.common.Coding coding)
    • toHex

      public static String toHex(byte[] bytes)
    • getResource

      public static URL getResource(String name)
    • 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

      public static String getContent(org.apache.http.HttpResponse response) throws IOException
      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 to ManagementClient.getMgmtAddress() or getDefaultHost(boolean) (when mgmtClient is null). 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 be null)
      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 to ManagementClient.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 be null)
      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 made
      httpClient - DefaultHttpClient to test multiple access
      expectedStatusCode - expected status code returned from the requested server
      Returns:
      HTTP response body
      Throws:
      IOException
      URISyntaxException
    • 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 returns HttpServletResponse.SC_UNAUTHORIZED and username is provided, then a new request is created with the provided credentials (basic authentication).
      Parameters:
      url - URL to which the request should be made
      user - Username (may be null)
      pass - Password (may be null)
      expectedStatusCode - expected status code returned from the requested server
      Returns:
      HTTP response body
      Throws:
      IOException
      URISyntaxException
    • 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 returns HttpServletResponse.SC_UNAUTHORIZED and username is provided, then a new request is created with the provided credentials (basic authentication).
      Parameters:
      url - URL to which the request should be made
      user - Username (may be null)
      pass - Password (may be null)
      expectedStatusCode - expected status code returned from the requested server
      checkFollowupAuthState - 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:
      IOException
      URISyntaxException
    • 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 to
      token - bearer token
      expectedStatusCode - expected status code
      Returns:
      response body
      Throws:
      URISyntaxException
      UnsupportedEncodingException
      org.apache.http.client.ClientProtocolException
      IOException
    • getJBossEjb3XmlAsset

      public static org.jboss.shrinkwrap.api.asset.Asset getJBossEjb3XmlAsset(String securityDomain)
      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 be null)
      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

      public static String createUsersFromRoles(String... roles)
      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, then roleName == userName == password
      Parameters:
      roles - role names (used also as user names and passwords)
      Returns:
      not-null content of users.properties and/or roles.properties
    • stripSquareBrackets

      public static String stripSquareBrackets(String str)
      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 fails
      IllegalArgumentException - workingFolder is null or it's not a directory
    • makeCall

      public static String makeCall(URI uri, int expectedStatusCode) throws Exception
      Makes HTTP call without authentication. Returns response body as a String.
      Parameters:
      uri - requested URL
      expectedStatusCode - expected status code - it's checked after the request is executed
      Throws:
      Exception
    • encodeQueryParam

      public static String encodeQueryParam(String paramName, String paramValue)
      Returns param/value pair in form "urlEncodedName=urlEncodedValue". It can be used for instance in HTTP get queries.
      Parameters:
      paramName - parameter name
      paramValue - parameter value
      Returns:
      "[urlEncodedName]=[urlEncodedValue]" string
    • getHost

      public static String getHost(org.jboss.as.arquillian.container.ManagementClient managementClient)
      Returns management address (host) from the givem ManagementClient. If the returned value is IPv6 address then square brackets around are stripped.
      Parameters:
      managementClient -
      Returns:
    • getDefaultHost

      public static String getDefaultHost(boolean canonical)
      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

      public static Configuration getLoginConfiguration()
      Returns installed login configuration.
      Returns:
      Configuration
    • loginWithKerberos

      public static LoginContext loginWithKerberos(Krb5LoginConfiguration krb5Configuration, String user, String pass) throws LoginException
      Creates login context for given Krb5LoginConfiguration and credentials and calls the LoginContext.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

      public static File createTemporaryFolder(String prefix) throws IOException
      Creates a temporary folder name with given name prefix.
      Parameters:
      prefix - folder name prefix
      Returns:
      created folder
      Throws:
      IOException