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

Support persistent session in IntrospectToken method of GRPC client, PLTFRM-72444 #33

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Semerokozlyat
Copy link

Support persistent session in IntrospectToken method of GRPC client.

Partly PLTFRM-72444

@@ -117,7 +122,11 @@ func (c *GRPCClient) IntrospectToken(
req.ScopeFilter[i] = &pb.IntrospectionScopeFilter{ResourceNamespace: scopeFilter[i].ResourceNamespace}
}

ctx = metadata.AppendToOutgoingContext(ctx, grpcMetaAuthorization, makeBearerToken(accessToken))
if c.sessionID != "" {
ctx = metadata.AppendToOutgoingContext(ctx, grpcMetaSessionID, c.sessionID)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What will happen if session id is already in context?

Copy link
Author

Choose a reason for hiding this comment

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

It will be replaced. Now it is considers only 1 active session could be used by a client.

@Semerokozlyat Semerokozlyat force-pushed the feature/PLTFRM-72444-caching-idp-client-session-support branch 2 times, most recently from fc3abda to ca77c0a Compare January 29, 2025 17:43
@@ -177,6 +195,18 @@ func (c *GRPCClient) IntrospectToken(
}, nil
}

func (c *GRPCClient) GetSessionID() string {
Copy link
Member

@vasayxtx vasayxtx Jan 30, 2025

Choose a reason for hiding this comment

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

Could you make GetSessionID and SetSessionID private? They appear to be used only internally, so exporting them may not be necessary.

Copy link
Author

Choose a reason for hiding this comment

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

Unfortunately, it is not possible because "grpc_client.go" and "grpc_client_test.go" files are related to different go packages, and unit tests require checking and explicit setting of session id.

Copy link
Member

@vasayxtx vasayxtx Jan 31, 2025

Choose a reason for hiding this comment

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

Please consider my suggestion below.

@@ -135,11 +144,20 @@ func (c *GRPCClient) IntrospectToken(
if err := c.do(ctx, "IDPTokenService/IntrospectToken", func(ctx context.Context) error {
var innerErr error
resp, innerErr = c.client.IntrospectToken(ctx, &req)
if errors.Is(innerErr, ErrUnauthenticated) {
Copy link
Member

Choose a reason for hiding this comment

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

ErrUnauthenticated is returned by the do() method, which means the session won’t be cleared. Could you fix this and add a corresponding unit test?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

@@ -162,6 +162,7 @@ func TestIntrospector_IntrospectToken(t *gotesting.T) {
introspectorOpts idptoken.IntrospectorOpts
tokenToIntrospect string
accessToken string
sessionID string
Copy link
Member

Choose a reason for hiding this comment

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

I couldn’t find where this field is used. Could you clarify? Ideally, we should verify that the mock gRPC server receives the correct session ID from the client.

Copy link
Author

Choose a reason for hiding this comment

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

Leftover, removed.

@Semerokozlyat Semerokozlyat force-pushed the feature/PLTFRM-72444-caching-idp-client-session-support branch 3 times, most recently from 51053f9 to e7d52f8 Compare January 30, 2025 15:47

for _, tc := range tCases {
t.Run(tc.name, func(t *gotesting.T) {
grpcClient.SetSessionID(tc.sessionID)
Copy link
Member

@vasayxtx vasayxtx Jan 31, 2025

Choose a reason for hiding this comment

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

Unfortunately, this test does not fully validate the behavior of GRPCClient. Currently, it only verifies that if the sessionID is set, it can be retrieved after making an IntrospectToken call.

Instead of retrieving the session ID from the client, you should check GRPCServerTokenIntrospectorMock.LastSessionMeta. This approach allows us to remove the unnecessary SetSessionID and GetSessionID methods, which are currently used only for testing purposes.

An ideal test should cover the following cases:

  1. When the gRPC server returns the x-session-id metadata, the gRPC client should use it for subsequent requests.
  2. When the gRPC server returns an authentication error, the gRPC client should attempt to use the access token for the next request.

@Semerokozlyat Semerokozlyat force-pushed the feature/PLTFRM-72444-caching-idp-client-session-support branch 6 times, most recently from 139bde2 to f838e2d Compare February 5, 2025 14:34
@Semerokozlyat Semerokozlyat force-pushed the feature/PLTFRM-72444-caching-idp-client-session-support branch from f838e2d to c4a838c Compare February 5, 2025 15:20
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.

5 participants