12.10. 数据源安全

数据源安全性指的是为数据源连接加密或模糊密码。这些密码可以以纯文本形式存储在配置文件中,但这代表了安全风险。

您可以通过多种方法实现数据源安全性。下面列出了每个示例。

使用安全域保护数据源

使用以下步骤通过安全域保护数据源:

  1. 创建新的安全域。

    /subsystem=security/security-domain=DsRealm:add(cache-type=default)
    /subsystem=security/security-domain=DsRealm/authentication=classic:add(login-modules=[{code=ConfiguredIdentity,flag=required,module-options={userName=sa,
    principal=sa, password=sa}}])

    定义了数据源的安全域。以下 XML 提取是调用 CLI 命令的结果。

     <security-domain name="DsRealm" cache-type="default">
      <authentication>
        <login-module code="ConfiguredIdentity" flag="required">
          <module-option name="userName" value="sa"/>
          <module-option name="principal" value="sa"/>
          <module-option name="password" value="sa"/>
        </login-module>
      </authentication>
    </security-domain>
  2. 添加新数据源.

    data-source add --name=securityDs
    --jndi-name=java:jboss/datasources/securityDs
    --connection-url=jdbc:h2:mem:test;DB_CLOSE_DELAY=-1 --driver-name=h2
    --new-connection-sql="select current_user()"
  3. 设置数据源上的安全域。

    data-source --name=securityDs --security-domain=DsRealm
  4. 重新加载服务器,让更改生效。

    reload
注意

如果您使用具有多个数据源的安全域,请在安全域中禁用缓存。这可以通过将 cache-type 属性的值设置为 none 或完全删除属性来完成;但是,如果需要缓存,请为每个数据源使用单独的安全域。

以下 XML 提取显示了通过 DsRealm 保护的数据源。

<datasources>
  <datasource jndi-name="java:jboss/datasources/securityDs"
    pool-name="securityDs">
    <connection-url>jdbc:h2:mem:test;DB_CLOSE_DELAY=-1</connection-url>
      <driver>h2</driver>
      <new-connection-sql>select current_user()</new-connection-sql>
      <security>
        <security-domain>DsRealm</security-domain>
      </security>
    </datasource>
</datasources>

有关使用安全域的更多信息,请参阅如何配置身份管理

使用密码 Vault 保护数据源

使用以下步骤使用密码 vault 保护数据源:

  1. 为 ExampleDS 数据源设置密码库。

    data-source --name=ExampleDS
    --password=${VAULT::ds_ExampleDS::password::N2NhZDYzOTMtNWE0OS00ZGQ0LWE4MmEtMWNlMDMyNDdmNmI2TElORV9CUkVBS3ZhdWx0}
  2. 重新加载服务器以实施更改。

    reload

以下 XML 安全元素添加到使用密码库保护的 ExampleDS 数据源中:

<security>
  <user-name>admin</user-name>
  <password>${VAULT::ds_ExampleDS::password::N2NhZDYzOTMtNWE0OS00ZGQ0LWE4MmEtMWNlMDMyNDdmNmI2TElORV9CUkVBS3ZhdWx0}</password>
</security>

有关使用密码库的更多信息,请参阅 JBoss EAP How to Configure Server Security 指南中的 Password Vault 部分。

使用凭证存储保护数据源

您还可以使用凭据存储来提供密码。elytron 子系统提供创建凭据存储的功能,以便在整个 JBoss EAP 中安全存放和使用您的密码。您可以在 JBoss EAP How to Configure Server Security 指南的 Credential Store 部分中找到关于创建和使用凭据存储的更多详细信息。

为示例DS 添加凭证存储参考

/subsystem=datasources/data-source=ExampleDS:write-attribute(name=credential-reference,value={store=exampleCS, alias=example-ds-pw})

使用身份验证上下文保护数据源

您还可以使用 Elytron 身份验证上下文来提供用户名和密码。

使用以下步骤配置和使用数据源安全性的身份验证上下文。

  1. 删除 密码user-name

    /subsystem=datasources/data-source=ExampleDS:undefine-attribute(name=password)
    /subsystem=datasources/data-source=ExampleDS:undefine-attribute(name=user-name)
  2. 为数据源启用 Elytron 安全性。

    /subsystem=datasources/data-source=ExampleDS:write-attribute(name=elytron-enabled,value=true)
    
    reload
  3. 为您的凭证创建 身份验证配置

    身份验证配置包含您希望在进行连接时使用的数据源的凭据。以下示例使用对凭据存储的引用,但也可以使用 Elytron 安全域。

    /subsystem=elytron/authentication-configuration=exampleAuthConfig:add(authentication-name=sa,credential-reference={clear-text=sa})
  4. 创建 身份验证上下文.

    /subsystem=elytron/authentication-context=exampleAuthContext:add(match-rules=[{authentication-configuration=exampleAuthConfig}])
  5. 更新数据源以使用身份验证上下文。

    以下示例更新了 ExampleDS 以使用身份验证上下文。

    /subsystem=datasources/data-source=ExampleDS:write-attribute(name=authentication-context,value=exampleAuthContext)
    
    reload
    注意

    如果没有设置 authentication-context 属性,且 elytron-enabled 属性设为 true,则 JBoss EAP 将使用当前上下文进行身份验证。

使用 Kerberos 保护数据源

要使用 kerberos 身份验证保护数据源,需要以下配置:

  • Kerberos 在数据库服务器上配置。
  • JBoss EAP 主机服务器具有数据库服务器的 keytab 条目。

使用 kerberos 保护数据源:

  1. 将 JBoss EAP 配置为使用 kerberos.

    /system-property=java.security.krb5.conf:add(value="/path/to/krb5.conf")
    /system-property=sun.security.krb5.debug:add(value="false")
    /system-property=sun.security.spnego.debug:add(value="false")

    若要进行调试,请将 sun.security.krb5.debugsun.security.spnego.debug 的值更改为 true。在生产环境中,建议将值设为 false

  2. 配置安全性.

    您可以使用旧安全性或 Elytron 安全来保护数据源。

    • 将 kerberos 与旧安全性一起使用:

      1. 配置 infinispan 缓存,以定期从缓存中删除过期的票据。

        batch
        /subsystem=infinispan/cache-container=security:add(default-cache=auth-cache)
        /subsystem=infinispan/cache-container=security/local-cache=auth-cache:add()
        /subsystem=infinispan/cache-container=security/local-cache=auth-cache/expiration=EXPIRATION:add(lifespan=3540000,max-idle=3540000)
        /subsystem=infinispan/cache-container=security/local-cache=auth-cache/memory=object:add(size=1000)
        run-batch

        以下属性定义 ticket 过期:

        • Life span:间隔,以毫秒为单位,从 KDC 请求新证书。将lifespan 属性 的值设置为比 KDC 定义的寿命小。
        • max-idle : Interval,以毫秒为单位,如果缓存未使用,则应从缓存中删除有效票据。
        • max-entries :要保留在缓存中的 kerberos ticket 的最大数量副本。该值对应于数据源中配置的连接数量。
      2. 创建安全域.

        batch
        /subsystem=security/security-domain=KerberosDatabase:add(cache-type=infinispan)
        /subsystem=security/security-domain=KerberosDatabase/authentication=classic:add
        /subsystem=security/security-domain=KerberosDatabase/authentication=classic/login-module="KerberosDatabase-Module":add(code="org.jboss.security.negotiation.KerberosLoginModule",module="org.jboss.security.negotiation",flag=required, module-options={ "debug" => "false", "storeKey" => "false", "useKeyTab" => "true", "keyTab" => "/path/to/eap.keytab", "principal" => "PRINCIPAL@SERVER.COM", "doNotPrompt" => "true", "refreshKrb5Config" => "true", "isInitiator" => "true", "addGSSCredential" => "true", "credentialLifetime" => "-1"})
        run-batch
        • 将 Microsoft JDBC 驱动程序用于 SQL 服务器时,请在 module-options 中添加属性和值 "wrapGSSCredential" SAS "true"。
        • 为进行调试,请将 module-optionsdebug 属性的值更改为 true
    • 在 Elytron 中使用 kerberos:

      1. 在 Elytron 中设置 kerberos 工厂。

        /subsystem=elytron/kerberos-security-factory=krbsf:add(debug=false, principal=PRINCIPAL@SERVER.COM, path=/path/to/keytab, request-lifetime=-1, obtain-kerberos-ticket=true, server=false)
      2. 创建身份验证配置以使用 kerberos 工厂。

        /subsystem=elytron/authentication-configuration=kerberos-conf:add(kerberos-security-factory=krbsf)
      3. 创建身份验证上下文.

        /subsystem=elytron/authentication-context=ds-context:add(match-rules=[{authentication-configuration=kerberos-conf}])
  3. 使用 Kerberos 保护数据源.

    • 如果您使用旧安全性:

      1. 将数据源配置为使用安全域。

        /subsystem=datasources/data-source=KerberosDS:add(connection-url="URL", min-pool-size=0, max-pool-size=10, jndi-name="java:jboss/datasource/KerberosDS", driver-name=<jdbc-driver>.jar, security-domain=KerberosDatabase, allow-multiple-users=false, pool-prefill=false, pool-use-strict-min=false, idle-timeout-minutes=2)
      2. 配置特定于供应商的连接属性。

        /subsystem=datasources/data-source=KerberosDS/connection-properties=<connection-property-name>:add(value="(<kerberos-value>)")

        示例:Oracle 数据库的连接属性.

        /subsystem=datasources/data-source=KerberosDS/connection-properties=oracle.net.authentication_services:add(value="(KERBEROS5)")
    • 如果使用 Elytron:

      1. 配置数据源以使用身份验证上下文。

        /subsystem=datasources/data-source=KerberosDS:add(connection-url="URL", min-pool-size=0, max-pool-size=10, jndi-name="java:jboss/datasource/KerberosDS", driver-name=<jdbc-driver>.jar, elytron-enabled=true, authentication-context=ds-context, allow-multiple-users=false, pool-prefill=false, pool-use-strict-min=false, idle-timeout-minutes=2)
      2. 配置特定于供应商的连接属性。

        /subsystem=datasources/data-source=KerberosDS/connection-properties=<connection-property-name>:add(value="(<kerberos-value>)")

        示例:Oracle 数据库的连接属性

        /subsystem=datasources/data-source=KerberosDS/connection-properties=oracle.net.authentication_services:add(value="(KERBEROS5)")

      在使用 kerberos 身份验证时,建议对数据源使用以下属性和值:

      • pool-prefill=false
      • pool-use-strict-min=false
      • idle-timeout-minutes

    如需支持的属性列表,请参阅 Datasource Attributes