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

Add support to connect to the LoginFlow AI service #5971

Open
wants to merge 536 commits into
base: master
Choose a base branch
from

Conversation

sahandilshan
Copy link
Contributor

Proposed changes in this pull request

With this PR, the product-is will be able to connect the loginflow AI microservice and generate AI results accordingly

Related Issue(s)

@CLAassistant
Copy link

CLAassistant commented Sep 30, 2024

CLA assistant check
All committers have signed the CLA.

@@ -0,0 +1,23 @@
package org.wso2.carbon.identity.application.mgt.ai;
Copy link
Contributor

Choose a reason for hiding this comment

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

missing copywrite text

<module>org.wso2.carbon.ai.service.mgt.server.feature</module>
</modules>

</project>
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing new line

Copy link

codecov bot commented Oct 18, 2024

Codecov Report

Attention: Patch coverage is 77.50000% with 54 lines in your changes missing coverage. Please review.

Project coverage is 40.71%. Comparing base (fb21729) to head (d928d9a).
Report is 28 commits behind head on master.

Files with missing lines Patch % Lines
...ity/application/mgt/ai/LoginFlowAIManagerImpl.java 0.00% 19 Missing ⚠️
...lication/mgt/ai/constant/LoginFlowAIConstants.java 0.00% 17 Missing ⚠️
...bon/ai/service/mgt/token/AIAccessTokenManager.java 88.63% 4 Missing and 6 partials ⚠️
...2/carbon/ai/service/mgt/util/AIHttpClientUtil.java 91.89% 3 Missing and 3 partials ⚠️
...2/carbon/ai/service/mgt/constants/AIConstants.java 93.33% 1 Missing ⚠️
...nternal/ApplicationManagementServiceComponent.java 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #5971      +/-   ##
============================================
- Coverage     40.88%   40.71%   -0.17%     
- Complexity    14516    14601      +85     
============================================
  Files          1772     1781       +9     
  Lines        117769   118681     +912     
  Branches      19116    20275    +1159     
============================================
+ Hits          48146    48321     +175     
- Misses        62316    63053     +737     
  Partials       7307     7307              
Flag Coverage Δ
unit 25.32% <77.50%> (+0.24%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

org.apache.http.concurrent; version="${httpcore.version.osgi.import.range}",
</Import-Package>
<Export-Package>
org.wso2.carbon.ai.service.mgt.*; version="${carbon.identity.package.export.version}"
Copy link
Member

@omindu omindu Nov 28, 2024

Choose a reason for hiding this comment

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

private AccessTokenRequestHelper createDefaultHelper() {

return new AccessTokenRequestHelper(LOGIN_FLOW_AI_KEY, LOGIN_FLOW_AI_TOKEN_ENDPOINT,
HttpAsyncClients.createDefault());
Copy link
Member

Choose a reason for hiding this comment

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

Can we use connection pooling here ?

Comment on lines +56 to +57
private static volatile AIAccessTokenManager instance; // Volatile for thread safety.
private static final Object lock = new Object(); // Lock for synchronization.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check if we want to change the naming

private String clientId;

private AIAccessTokenManager() {
// Prevent from initialization.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove comment

private final String key;
private final String aiServiceTokenEndpoint;
private static final int MAX_RETRIES = IdentityUtil.getProperty(
"AIServices.LoginFlow.TokenRequestMaxRetries") != null ?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove LoginFlow config since this a common component

private final String aiServiceTokenEndpoint;
private static final int MAX_RETRIES = IdentityUtil.getProperty(
"AIServices.LoginFlow.TokenRequestMaxRetries") != null ?
Integer.parseInt(IdentityUtil.getProperty("AIServices.LoginFlow.TokenRequestMaxRetries")) : 3;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use constants

private static final int MAX_RETRIES = IdentityUtil.getProperty(
"AIServices.LoginFlow.TokenRequestMaxRetries") != null ?
Integer.parseInt(IdentityUtil.getProperty("AIServices.LoginFlow.TokenRequestMaxRetries")) : 3;
private static final long TIMEOUT = IdentityUtil.getProperty(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Move these into a method

post.setHeader("Content-Type", "application/x-www-form-urlencoded");

StringEntity entity = new StringEntity("grant_type=client_credentials");
entity.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/x-www-form-urlencoded"));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use constants for headers (There should be reusable constants

post.setEntity(entity);

CountDownLatch latch = new CountDownLatch(1);
final String[] accessToken = new String[1];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

***NOTE: What should we do in multi node setup, should we use multiple JWT tokens or persist the access token in DB?

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String responseBody = EntityUtils.toString(response.getEntity());
Map<String, Object> responseMap = gson.fromJson(responseBody, Map.class);
accessToken[0] = (String) responseMap.get("access_token");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Use constants for all keys. Check all places

// Decode the JWT to extract client ID.
String[] jwtParts = accessToken[0].split("\\.");
if (jwtParts.length == 3) {
String payloadJson = new String(Base64.getUrlDecoder().decode(jwtParts[1]),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check if we need to decode the token also check if we actually need the token type

response.getStatusLine().getStatusCode());
}
} catch (IOException | JsonSyntaxException e) {
LOG.error("Error parsing token response: " + e.getMessage(), e);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Change to debug logs

wso2-jenkins-bot and others added 29 commits December 20, 2024 07:56
Introduce SAMLSSOPersistenceManagerFactory to switch between different persistence implementation
…nd-prov-issue

Revert "Revert "Fix: Unable to Update Google Connector after Configuring Outbound Provisioning for Google""
- Define constants for reusable keys
- Remove token decoding and get the clientId from the AI KEY
# Conflicts:
#	components/ai-services-mgt/org.wso2.carbon.ai.service.mgt/src/main/java/org/wso2/carbon/ai/service/mgt/constants/AIConstants.java
#	components/ai-services-mgt/org.wso2.carbon.ai.service.mgt/src/main/java/org/wso2/carbon/ai/service/mgt/token/AIAccessTokenManager.java
#	components/ai-services-mgt/org.wso2.carbon.ai.service.mgt/src/main/java/org/wso2/carbon/ai/service/mgt/util/AIHttpClientUtil.java
#	components/application-mgt/org.wso2.carbon.identity.application.mgt/pom.xml
#	pom.xml
 - Get tokens with token binding
 - Add j2 configs
 - Modify AIHttpUtil tests to use call mockwebserver
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.