Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -825,4 +825,7 @@ void failedToDiscoverClusterServices(String clusterName, String topologyName,

@Message(level = MessageLevel.ERROR, text = "LDAP service not found or not properly registered")
void ldapServiceNotFound();

@Message( level = MessageLevel.WARN, text = "Postgres type already exists exception caught. Tables already exist skipping creation." )
void typeAlreadyExistsCaught();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
package org.apache.knox.gateway.services.token.impl;

import org.apache.commons.codec.binary.Base64;
import org.apache.knox.gateway.GatewayMessages;
import org.apache.knox.gateway.database.DatabaseType;
import org.apache.knox.gateway.database.JDBCUtils;
import org.apache.knox.gateway.i18n.messages.MessagesFactory;
import org.apache.knox.gateway.services.security.token.KnoxToken;
import org.apache.knox.gateway.services.security.token.TokenMetadata;
import org.postgresql.util.PSQLException;

import javax.sql.DataSource;
import java.sql.Connection;
Expand All @@ -38,6 +41,9 @@
import static java.nio.charset.StandardCharsets.UTF_8;

public class TokenStateDatabase {

private static final GatewayMessages LOG = MessagesFactory.get(GatewayMessages.class);

static final String TOKENS_TABLE_NAME = "KNOX_TOKENS";
static final String TOKEN_METADATA_TABLE_NAME = "KNOX_TOKEN_METADATA";
private static final String ADD_TOKEN_SQL = "INSERT INTO " + TOKENS_TABLE_NAME + "(token_id, issue_time, expiration, max_lifetime) VALUES(?, ?, ?, ?)";
Expand All @@ -60,11 +66,20 @@ public class TokenStateDatabase {

private final DataSource dataSource;

private static final String POSTGRES_DUPLICATE_OBJECT_STATE = "42710";

TokenStateDatabase(DataSource dataSource, String dbType) throws Exception {
this.dataSource = dataSource;
DatabaseType databaseType = DatabaseType.fromString(dbType);
createTableIfNotExists(TOKENS_TABLE_NAME, databaseType.tokensTableSql());
createTableIfNotExists(TOKEN_METADATA_TABLE_NAME, databaseType.metadataTableSql());
try {
createTableIfNotExists(TOKENS_TABLE_NAME, databaseType.tokensTableSql());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily for this PR, but I wonder if executing these in a single Statement execution might help minimize the potential for the "already exists" error while improving efficiency slightly.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that not all the supported DB vendors have CREATE TALE IF EXISTS support (or at least this was true a couple years ago).

createTableIfNotExists(TOKEN_METADATA_TABLE_NAME, databaseType.metadataTableSql());
} catch (PSQLException psqlException) {
if (!psqlException.getSQLState().equals(POSTGRES_DUPLICATE_OBJECT_STATE)) {
throw psqlException;
}
LOG.typeAlreadyExistsCaught();
}
}

private void createTableIfNotExists(String tableName, String createSqlFileName) throws Exception {
Expand Down
Loading