zeroflag opened a new pull request, #567:
URL: https://github.com/apache/knox/pull/567
## What changes were proposed in this pull request?
Starting multiple knox instances can cause token state service
initialization failures when they try to create the same database tables.
```
ERROR knox.gateway (TokenStateServiceFactory.java:createService(63)) - Error
while initiatalizing
org.apache.knox.gateway.services.token.impl.JDBCTokenStateService: Error while
initiating JDBCTokenStateService: org.postgresql.util.PSQLException: ERROR:
relation "knox_tokens" already exists
```
To fix this issue I added a `IF NOT EXISTS` clause to the create table
statement. This is unfortunately not supported by Derby (which is only used for
unit testing), so I replaced derby with hsql.
## How was this patch tested?
I tested manually with both mysql and postgres.
Postgres gateway-site config:
```xml
<property>
<name>gateway.service.tokenstate.impl</name>
<value>org.apache.knox.gateway.services.token.impl.JDBCTokenStateService</value>
</property>
<property>
<name>gateway.database.type</name>
<value>postgresql</value>
</property>
<property>
<name>gateway.database.connection.url</name>
<value>jdbc:postgresql://localhost:5432/postgres?user=postgres</value>
</property>
```
```bash
$ docker run --name posttest -d -p 5432:5432 -e
POSTGRES_HOST_AUTH_METHOD=trust postgres:alpine
```
```bash
$ docker exec -it posttest psql -U postgres
postgres=# select * from knox_tokens;
token_id | issue_time | expiration |
max_lifetime
--------------------------------------+---------------+---------------+---------------
ad4953c9-38dd-4214-8ce1-e3b873a81f13 | 1650961012152 | 1650964612097 |
1651565812152
d37f744c-ef5b-4086-a9b7-6c1c0640e596 | 1650961013088 | 1650964613065 |
1651565813088
058b4e5c-642c-42fc-9e5a-9d23a56256d5 | 1650961026745 | 1650964626709 |
1651565826745
(3 rows)
```
MySql gateway-site config:
```xml
<property>
<name>gateway.service.tokenstate.impl</name>
<value>org.apache.knox.gateway.services.token.impl.JDBCTokenStateService</value>
</property>
<property>
<name>gateway.database.type</name>
<value>mysql</value>
</property>
<property>
<name>gateway.database.name</name>
<value>mysql</value>
</property>
<property>
<name>gateway.database.host</name>
<value>localhost</value>
</property>
<property>
<name>gateway.database.port</name>
<value>3306</value>
</property>
```
```bash
./bin/knoxcli.sh create-alias gateway_database_user --value root
./bin/knoxcli.sh create-alias gateway_database_password --value password
$ docker exec -it mysql1 mysql -u root -p
```
--
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]