How to authenticate using API Key in a DB2 JDBC Data Souce

Jean Ferreira
Giga Guru

I have a Data Source where type is JDBC and Format is Db2 - Universal

The authentication method I use is API Key, so I don't have neither a user nor a password. I only have a token.

If I add the token in the password field, the connection does not work and I get a "JDBC Syntax Error" as log.

I could establish a connection adding the token in the Connection URL properties field, then replacing the ; with a : in the Connection URL field.

 

Is there any other way to establish this type of connection without expose the Token I have?

 

Screenshot 2024-04-08 at 16.26.21.png

 

1 ACCEPTED SOLUTION

Jean Ferreira
Giga Guru

I solved the problem creating a schedule data import and a system properties.

In the Connection URL Parameters field I added a sort of variable. Then I used the pre and post scripts to replace the variable, run the import, then replace the key again.

 

Pre script

var dataSourceGR = new GlideRecord('sys_data_source');
var DATA_SOURCE_ID = 'YOUR_ID';
var apiKey = gs.getProperty('YOUR_PROPERTY', '');
var connectionURL = '';
var connectionURLParameters = '';

if (apiKey != '' && dataSourceGR.get(DATA_SOURCE_ID)) {
    connectionURL = '' + dataSourceGR.getValue('connection_url');
    connectionURLParameters = '' + dataSourceGR.getValue('connection_url_parameters');
    connectionURL = connectionURL.replace(';${apiKey}', ':apiKey=' + apiKey);
    connectionURLParameters = connectionURLParameters.replace('${apiKey}', 'apiKey=' + apiKey);
    dataSourceGR.setValue('connection_url', connectionURL);
    dataSourceGR.setValue('connection_url_parameters', connectionURLParameters);
    dataSourceGR.update();
}

Post Script

var dataSourceGR = new GlideRecord('sys_data_source');
var DATA_SOURCE_ID = 'YOUR_DATA_SOURCE_ID';
var connectionURLParameters = '';
var regex = /apiKey=[a-zA-Z0-9_]*/g;

if (dataSourceGR.get(DATA_SOURCE_ID)) {
    connectionURLParameters = '' + dataSourceGR.getValue('connection_url_parameters');
    connectionURLParameters = connectionURLParameters.replace(regex, '${apiKey}');
    dataSourceGR.setValue('connection_url_parameters', connectionURLParameters);
    dataSourceGR.update();
}

 

View solution in original post

2 REPLIES 2

luffy3478
Tera Guru

except the import admin or admin. rest will not be able to see the token.

 

else store it in a property and update the datasource record when exectued from scheduled import using pre processing script

Jean Ferreira
Giga Guru

I solved the problem creating a schedule data import and a system properties.

In the Connection URL Parameters field I added a sort of variable. Then I used the pre and post scripts to replace the variable, run the import, then replace the key again.

 

Pre script

var dataSourceGR = new GlideRecord('sys_data_source');
var DATA_SOURCE_ID = 'YOUR_ID';
var apiKey = gs.getProperty('YOUR_PROPERTY', '');
var connectionURL = '';
var connectionURLParameters = '';

if (apiKey != '' && dataSourceGR.get(DATA_SOURCE_ID)) {
    connectionURL = '' + dataSourceGR.getValue('connection_url');
    connectionURLParameters = '' + dataSourceGR.getValue('connection_url_parameters');
    connectionURL = connectionURL.replace(';${apiKey}', ':apiKey=' + apiKey);
    connectionURLParameters = connectionURLParameters.replace('${apiKey}', 'apiKey=' + apiKey);
    dataSourceGR.setValue('connection_url', connectionURL);
    dataSourceGR.setValue('connection_url_parameters', connectionURLParameters);
    dataSourceGR.update();
}

Post Script

var dataSourceGR = new GlideRecord('sys_data_source');
var DATA_SOURCE_ID = 'YOUR_DATA_SOURCE_ID';
var connectionURLParameters = '';
var regex = /apiKey=[a-zA-Z0-9_]*/g;

if (dataSourceGR.get(DATA_SOURCE_ID)) {
    connectionURLParameters = '' + dataSourceGR.getValue('connection_url_parameters');
    connectionURLParameters = connectionURLParameters.replace(regex, '${apiKey}');
    dataSourceGR.setValue('connection_url_parameters', connectionURLParameters);
    dataSourceGR.update();
}