Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JDBC sample app to show use of Auth Tokens to connect the database and get the tokens from endpoints #23

Open
wants to merge 26 commits into
base: main
Choose a base branch
from

Conversation

rvpanchal1202
Copy link

-> Review Comments are addressed
-> Testing of functionality is done with Keyclock IDP

… token

using the refresh token, we can get the new access token in case of a wrong access token or an expired access token. 

Added API to get the new access_token using refresh_token

note: When we request for new access_token using refresh token, This request will give us both the tokens. which we can save later to original tokens and start using new Tokens instead of old tokens
Changes in the client application done as per comments and discussion. 

SetUpDbForOAuth() -> Adding authentication record in Vertica database is having issues. It's dropping the user and authentication method, but while setting the Authentication record, there is some error because of which Token authentication is failing and Database connection with access token is unable to establish. 

Without SetUpDbForOAuth() method call, Application is woking good.
Commented SetUpDbForOAuth() and TearDown() call.
-> Removed all @commandline and picoli imports
-> Removed all the blank lines from the file
-> Indentation issue resolved. Note:Please adjust the tab stop as 4 to view the file content properly indented.
-> Removed if else for first element in the map for framing the connectionString. 
-> All the Start braces and end braces are aligned and made the same in every place 
-> Added null check for an access token and refresh token after getting it from token URL
Added config.properties file which would be required for running the sample app. User have to update the values as per configuration. 

Note: Given Sample example for the Connection string. Because Connection string will be accepted in this format. <KeyName>=<Value>;<KeyName>=<Value>;......
-> Added functions for the redundant code for copying data to the buffered output stream.
-> removed unnecessary imports.
Removed ClientID, ClientSecret, Tokenurl from getconnection Call.
Added Line between imports and class
Debug prints removed
Deprecated method use JsonParser().parse(res); removed. 
used JsonParser.parseString(res);
- Added Fix for setup of Authentication record to Database. 
- Uncommented DBSetup and Teardown for Authentication setup before OAuth sample test run
Added DiscoveryUrl to be provided in config.properties file for OAuth Authentication
Added RefreshToken check and If both the tokens are available, then it will use those tokens, Or else it will get new tokens using password grant.
Check if access token is not available, Get the tokens.
else 
if a refresh token is not available, Get the tokens.
else 
set both the tokens to properties.
Indentation issue resolved
Indentation resolved
Comment indentation resolved
Internal review comments are addressed. 
-> Redundant code for getting parameters from connection string addressed.
-> Formating of comments for method explanation addressed.
@rvpanchal1202 rvpanchal1202 marked this pull request as ready for review May 10, 2024 08:01
Review comments are addressed. 
Changes ensureAccessToken as per suggestion given
Comments and print statements are added
-> Spacing between Functions and indentation formating is done
- Unused variable CONNECTION_STRING removed. 
- Added comments for the required calls
- Added Refresh token check code in EnsureAccessToken Call
Refresh Token check is removed as it might be that we will get only access Token and not refresh token. As per comment from Kevin.
-> Updated comment for EnsureAccessToken
Changed the function name from connectToDb to getConnection.
Added null and empty checks for refresh token to save in the properties.
@kevinkarch88
Copy link
Contributor

I don't have any major issues with the code as is, it should work as intended.

public class OAuthSampleApp {
private static Properties prop;
private static Map<String, String> csProp = new HashMap<String, String>();
private static String OAUTH_ACCESS_TOKEN_VAR_STRING = "OAUTH_SAMPLE_ACCESS_TOKEN";
Copy link
Contributor

Choose a reason for hiding this comment

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

In java we declare constants as final so the constant properties here should be private static final

Constants are also generally grouped together and declared first in a class, so I would move these two constants to be the first properties defined in the class followed by a newline and then followed by the rest of the properties

Copy link
Author

Choose a reason for hiding this comment

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

Changed OAUTH_ACCESS_TOKEN_VAR_STRING, OAUTH_REFRESH_TOKEN_VAR_STRING to constant "private static final"

import com.google.gson.JsonObject;
import com.vertica.jdbc.VerticaConnection;

public class OAuthSampleApp {
Copy link
Contributor

Choose a reason for hiding this comment

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

Unless it is just rendering poorly in github, the spacing in this class is all off. Indentation should simply be four spaces per indentation level and they should be actual spaces, not tabs.

Copy link
Author

Choose a reason for hiding this comment

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

Hi Danny,
This indentation formatting is done using the auto-indent feature(Source->Format) in Eclipse. So If we make any changes here in git for indentation, It will reflect wrong when we open the file in Eclipse or other IDE.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you are going to use the auto indent feature you need to update it to use four spaces instead of tabs, there should be an option for this. A space is a space, it's the same everywhere. A tab character may not render the same everywhere like we see here

Copy link
Author

@rvpanchal1202 rvpanchal1202 May 30, 2024

Choose a reason for hiding this comment

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

Ok. As per the last commit, All indentation was good and no issues observed. However, I have changed the indentation from the tabs to Spaces and committed the changes. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm still seeing tabs in the PR. There is a setting in eclipse to use spaces instead of tabs when formatting code. If you have not enabled that, I suggest you change that and format the code again.


// Add/Create Authentication record in database. Create User and grant the
// permissions for User
private static void SetUpDbForOAuth() throws Exception {
Copy link
Contributor

@DMickens DMickens May 23, 2024

Choose a reason for hiding this comment

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

In java convention for method names is to use camelCase. It's okay for the casing of the names in this sample to differ from the names in the ADO implementation in order to follow the convention. Please ensure all method names start with a lowercase letter and adhere to camelCase.

Copy link
Author

Choose a reason for hiding this comment

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

Changed all method names to camelCase.

private static void SetUpDbForOAuth() throws Exception {
String connectionString = getConnectionString();
vConnection = DriverManager.getConnection(connectionString);
String USER = prop.getProperty("User");
Copy link
Contributor

Choose a reason for hiding this comment

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

As these are not constant it should not be in all caps with underscore spacing. Please change these to be camelCase

Copy link
Author

Choose a reason for hiding this comment

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

Changed non constant vars to camelCase.

st.execute("DROP USER IF EXISTS " + USER + " CASCADE;");
st.execute("DROP AUTHENTICATION IF EXISTS v_oauth CASCADE;");
st.execute("CREATE AUTHENTICATION v_oauth METHOD 'oauth' LOCAL;");
st.execute("ALTER AUTHENTICATION v_oauth SET client_id= '" + CLIENT_ID + "';");
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: either none should have spaces before the equal sign, or they all should. I would prefer that they all do, but lets make them consistent

Copy link
Author

Choose a reason for hiding this comment

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

Done.

String CLIENT_SECRET = prop.getProperty("ClientSecret");
String DISCOVERY_URL = prop.getProperty("DiscoveryUrl");
Statement st = vConnection.createStatement();
st.execute("DROP USER IF EXISTS " + USER + " CASCADE;");
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you put all of these commands into an array of strings, then iterate through the array and call st.execute() for each element of the array. I asked Kevin to make that change in the ADO sample previously as well. It makes it easier to see everything that is being executed and it's easy to modify if need be.

Copy link
Author

Choose a reason for hiding this comment

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

Added queries to a string array and executed the queries using iterating through the array.

private static void TearDown() {
try {
Statement st = vConnection.createStatement();
String USER = prop.getProperty("User");
Copy link
Contributor

Choose a reason for hiding this comment

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

not a constant, change to user

Copy link
Author

Choose a reason for hiding this comment

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

Done

// If access token is Invalid/Expired, Get new tokens using password/refresh grant
private static void EnsureAccessToken() throws Exception {
try {
String accessToken = System.getenv(OAUTH_ACCESS_TOKEN_VAR_STRING);
Copy link
Contributor

Choose a reason for hiding this comment

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

System.getenv will not throw an exception, just return null. So these should be moved outside of the try block

Copy link
Author

Choose a reason for hiding this comment

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

Moved System.getenv outside of try block

@DMickens
Copy link
Contributor

I really want to be able to compare this side by side with the ado.net sample application, but the spacing issue is making that very difficult. When splitting the screen to review side by side, this code is stretched out and split over many lines with large spaces in between sections of code and it makes it difficult to review. Would you mind fixing that issue first and committing the change? Then I will finish my review, I'm not sure if it's an issue from using tabs or just too many spaces or what but making indentation level 4 manual spaces should fix it.

@rvpanchal1202
Copy link
Author

Code rearrangement of methods are done with respect to ADO.Net Sample App.

Review comments addressed, 
-> Code alliagnment 
-> Contant and non constant variables consistency adjusted,
-> functions are rearranged as per ADO.Net Sample app
Copy link
Contributor

@williammatherjones williammatherjones left a comment

Choose a reason for hiding this comment

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

Other than the tab/space issue, the sample looks really good. Please push a commit with the formatting changes and I'll approve.

import com.google.gson.JsonObject;
import com.vertica.jdbc.VerticaConnection;

public class OAuthSampleApp {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm still seeing tabs in the PR. There is a setting in eclipse to use spaces instead of tabs when formatting code. If you have not enabled that, I suggest you change that and format the code again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants