smolnar82 commented on code in PR #1380: URL: https://github.com/apache/knox/pull/1380#discussion_r3948821575
########## gateway-server/src/main/java/org/apache/knox/gateway/database/H2DataSourceFactory.java: ########## @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.knox.gateway.database; + +import org.apache.knox.gateway.config.GatewayConfig; +import org.apache.knox.gateway.services.security.AliasService; +import org.apache.knox.gateway.services.security.AliasServiceException; +import org.h2.jdbcx.JdbcDataSource; + +import javax.sql.DataSource; +import java.sql.SQLException; + +/** + * Builds a {@link DataSource} for the H2 database. In the self-provisioning embedded case the + * connection URL points at a local file base ({@code jdbc:h2:${securityDir}/h2db/knoxdb}) that + * {@link EmbeddedH2Database} has configured via {@code gateway.database.name}; H2 creates the file + * on first connection, so no server or explicit database-creation step is required. Within a single + * JVM all Knox services connecting to the same URL share one embedded database instance. + * <p> + * When {@link GatewayConfig#isDatabaseH2EncryptionEnabled()} is {@code true}, at-rest encryption is + * enabled by appending {@code ;CIPHER=AES} to the URL and supplying the connection password as + * {@code "<filePassword> <userPassword>"} (H2 splits the password on the first space into the file + * password and the user password). The file password is the operator-provisioned passphrase resolved + * from the credential-store alias named by {@link GatewayConfig#getDatabaseH2EncryptionPassphraseAlias()}; + * initialization fails fast if that alias is unset (no silent unencrypted fallback). + */ +public class H2DataSourceFactory extends AbstractDataSourceFactory { + + @Override + public DataSource createDataSource(GatewayConfig gatewayConfig, AliasService aliasService) throws AliasServiceException, SQLException { + final JdbcDataSource dataSource = new JdbcDataSource(); + String url = "jdbc:h2:" + gatewayConfig.getDatabaseName(); Review Comment: Good point - the other factories honor `connectionUrl` because they front **external** databases where the operator owns the full JDBC URL. H2 here is deliberately the self-provisioning embedded backend, and I intentionally kept it URL-derived rather than URL-configurable, for two reasons: 1. The factory has to own the URL. For at-rest encryption it appends `;CIPHER=AES` and constructs the connection password as `<filePassword> <userPassword>` (H2 splits on the first space). A caller-supplied `connectionUrl` would bypass/conflict with that, so we'd be adding a config knob that silently breaks encryption. 2. The one thing operators legitimately need (the DB location) is already configurable via `gateway.database.name`. The URL is `jdbc:h2:${name}`, a custom `connectionUrl` would only add H2-specific URL options, which isn't a use case we intend to support for the embedded store. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
