okta / okta-sdk-dotnet

A .NET SDK for interacting with the Okta management API, enabling server-side code to manage Okta users, groups, applications, and more.
Other
157 stars 100 forks source link

OKTA-SDK-Dotnet null reference exception #646

Closed mmusaev closed 4 months ago

mmusaev commented 1 year ago

Describe the bug?

I followed this example to create an instance of GroupAPI:

var privateKey = new JsonWebKeyConfiguration { P = “{{P}}”, Kty = “RSA”, Q = “{{Q}}”, D = “{{D}}”, E = “{{E}}”, Kid = “{{P}}”, Qi = “{{Qi}}” };

var configuration = new Configuration { OktaDomain = “https://{{yourOktaDomain}}”, AuthorizationMode = AuthorizationMode.PrivateKey, ClientId = “{{clientId}}”, Scopes = new List { “okta.users.read”, “okta.apps.read”, “okta.groups.manage”, “okta.groups.manage” }, // Add all the scopes you need PrivateKey = privateKey };

services.AddScoped(_ => new GroupApi(configuration ));

However, I get null reference exception when creating GroupApi passing the configuration object. After ton of raised exceptions, the code eventually succeeds. I am able to connect to my okta tenant and get the response data from okta api calls.

Thank you, Marat

What is expected to happen?

There should not be any exceptions raised

What is the actual behavior?

I don't expect so many exceptions raised.

Reproduction Steps?

To reproduce the problem, select the checkbox in the Exceptions Settings window within Visual Studio 2022. I am using the latest okta-sdk-dotnet version.

Additional Information?

The code is used within NET 6 api project in Program.cs class.

.NET Version

The code is used within NET 6 api project in Program.cs class.

SDK Version

Happens both in OKTA.SDK 6.0.7 and 6.0.10.

OS version

Windows 10

laura-rodriguez commented 1 year ago

Hi @mmusaev , Thanks for reporting this issue. I'll file an internal ticket for our team.

Internal Ref: OKTA-611865

andrewqsmith commented 1 year ago

This is a great find @mmusaev and something I hope gets updated soon for the dotnet SDK.

chai-deshpande commented 1 year ago

The NullReferenceException is thrown while creating an instance of any API client. In my case its the UserApi. On digging further, its because of the call to configuration = Sdk.Client.Configuration.GetConfigurationOrDefault(configuration);

this in turn calls configBuilder.Build().GetSection("okta").GetSection("client").Bind(compiledConfig);

and since there is no okta:client or okta:testing section in the config, the null reference exception is thrown on the Bind method.

mmusaev commented 1 year ago

I modified OKTA SDK code to fix the issue locally. In my opinion, a library code should be not responsible for initializing the settings but it should be the responsibility of a client application to set the configuration(the separation of concerns). I added an extensibility point to the code to allow the client app to provide that logic. Otherwise, the library code will be making a best guess and provide extra options that will have unintended side effects, as is the case here.

mmoreno79 commented 7 months ago

I agree wholly with everyone's assessment here. Until a fix is in place, a quick dirty workaround is to use the overload accepting the IAsynchronousClient parameter. Keyword here is dirty but it accomplishes the same as the first constructor, just without configuration validation. I see there's a PR in place that fixes this issue but hasn't had any movement in months.

Workaround:

        var asyncClient = new ApiClient(config.OktaDomain);
        var userApi = new UserApi(asyncClient, config);
bryanapellanes-okta commented 5 months ago

@mmusaev I apologize for the delay in review and resolution of this issue. Using the reproduction code provided I am not able to reproduce the issue locally on a machine that lacks Okta configuration of any kind. Is this still an issue? Is there perhaps a specific kind of project where the exception occurs? If you are able to provide a reference project that reproduces the issue that would be greatly helpful. cc @mmoreno79. Thank you for your engagement and thanks for using Okta!

bryanapellanes-okta commented 4 months ago

closing due to inactivity.