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
160 stars 100 forks source link

Okta Configuration: Invalid URI #691

Closed itanne99 closed 5 months ago

itanne99 commented 10 months ago

Describe the bug?

Attempting to use SDK for API calls, but cannot get passed System.UriFormatException: 'Invalid URI: The hostname could not be parsed.'

Providing code snippet below with secrets redacted.

var oktaDomain = "https://dev-****.okta.com";
var clientId = ****;

var privateKey = new JsonWebKeyConfiguration
{
    D = ****,
    P = ****,
    Q = ****,
    Dp = ****,
    Dq = ****,
    Qi = ****,
    Kty = "RSA",
    E = "AQAB",
    Kid = ****,
    N = ****
};

var oktaConfiguration = new Configuration
{
    OktaDomain = oktaDomain,
    AuthorizationMode = AuthorizationMode.PrivateKey,
    ClientId = clientId,
    Scopes = new HashSet<string> { "okta.users.read" }, // Add all the scopes you need
    PrivateKey = privateKey,
};            

var userApi = new UserApi(oktaConfiguration);
var users = userApi.ListUsers().ToListAsync().Result;

What is expected to happen?

List of Users is returned

What is the actual behavior?

The following Exception is thrown back.

System.UriFormatException: 'Invalid URI: The hostname could not be parsed.'

Reproduction Steps?

Generate a private key through a service application. Fill in the data as demonstrated in the documentation. Attempt to make an API call with the SDK.

Additional Information?

No response

.NET Version

7.0.312

SDK Version

7.0.4

OS version

laura-rodriguez commented 10 months ago

Hi @itanne99, Thanks for reporting this issue. Would you mind sharing a network log? That will help us to debug the issue.

itanne99 commented 10 months ago

Hey there @laura-rodriguez, I wasn't able to get a network log, but here is the stack trace of the error I'm getting. Redacted file path, with ****, for security reasons.

Unhandled exception. System.AggregateException: One or more errors occurred. (Invalid URI: The hostname could not be parsed.)
 ---> System.UriFormatException: Invalid URI: The hostname could not be parsed.
   at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind, UriCreationOptions& creationOptions)
   at System.Uri..ctor(String uriString)
   at System.Net.WebProxy.CreateProxyUri(String address, Nullable`1 port)
   at System.Net.WebProxy..ctor(String Host, Int32 Port)
   at Okta.Sdk.Client.ProxyConfiguration.GetProxy(ProxyConfiguration configuration)
   at Okta.Sdk.Client.ApiClient.GetConfiguredClient(IReadableConfiguration configuration)
   at Okta.Sdk.Client.ApiClient.ExecAsync[T](RestRequest req, IReadableConfiguration configuration, CancellationToken cancellationToken)
   at Okta.Sdk.Client.OktaPagedCollectionEnumerator`1.MoveNextAsync()
   at Okta.Sdk.Client.OktaCollectionAsyncEnumerator`1.MoveNextAsync()
   at System.Linq.AsyncEnumerable.<ToListAsync>g__Core|620_0[TSource](IAsyncEnumerable`1 source, CancellationToken cancellationToken) in /_/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToList.cs:line 36
   at System.Linq.AsyncEnumerable.<ToListAsync>g__Core|620_0[TSource](IAsyncEnumerable`1 source, CancellationToken cancellationToken) in /_/Ix.NET/Source/System.Linq.Async/System/Linq/Operators/ToList.cs:line 36
   at CreateUserData.Functional.OktaAPIRepository.GetUsersBySDK() in ****\Back End\Batch Processing\Functional\CreateUserData\Functional\OktaAPIRepository.cs:line 53
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
   at CreateUserData.Program.Main(String[] args) in ****\Back End\Batch Processing\Functional\CreateUserData\Program.cs:line 15
laura-rodriguez commented 10 months ago

Thanks for sharing @itanne99 . Are you trying to use a proxy? Looking at the stack trace, it seems there was an error with the Proxy hostname?

itanne99 commented 10 months ago

Hey @laura-rodriguez, No proxy at all. Just trying to make a direct connection to my Okta Environment through the Private Key. I also attempted using an api token, but still hit the same issue. Maybe the SDK thinks I'm trying to connect through a proxy, is there a way to disable that flag to let the SDK know to not try and connect through a proxy?

I was trying to follow the Oauth 2.0 guide that's in the READ.ME, Oauth 2.0 READ.ME

laura-rodriguez commented 10 months ago

Ok, Interesting. I'll file an internal ticket to be prioritized by the team, but it would be great if we could narrow down the causes. We have tests that verify OAuth is working as expected, which I just checked, and it's working fine.

If you have a chance, would you mind cloning the project and referencing the repo in your project so you can debug the error? That will definitely speed up the fix process.

laura-rodriguez commented 7 months ago

Internal Ref: OKTA-723081

pevgeniev commented 6 months ago

Hey @laura-rodriguez, No proxy at all. Just trying to make a direct connection to my Okta Environment through the Private Key. I also attempted using an api token, but still hit the same issue. Maybe the SDK thinks I'm trying to connect through a proxy, is there a way to disable that flag to let the SDK know to not try and connect through a proxy?

I was trying to follow the Oauth 2.0 guide that's in the READ.ME, Oauth 2.0 READ.ME

The Okta.Sdk.Client.Configuration GetConfigurationOrDefault method binds indiscriminately to global config file in line 858: configBuilder.Build().Bind(compiledConfig);

If by any chance you have a 'Proxy' section in appsettings, which might not have anything to do with Okta proxy, then this method call will create a non null Proxy property, with its members being null and you'll get this exception when ApiClient tries to GetProxy in its GetConfiguredClient:

else if (configuration.Proxy != null) { clientOptions.Proxy = ProxyConfiguration.GetProxy(configuration.Proxy); }

Don't ask how many hours I've lost on this ...

bryanapellanes-okta commented 5 months ago

@itanne99 @pevgeniev Thank you for your patience while we review this issue. I've submitted a draft PR to address the issue described by @pevgeniev . Please review #722 to determine if the proposed change is sufficient to address the issue; if so, I need to port the change to the generator templates and the Sdk regenerated.

pevgeniev commented 5 months ago

@bryanapellanes-okta, Hi, thank you, looks good.

itanne99 commented 5 months ago

Hey @bryanapellanes-okta, I apologize for the late response. It's great to hear you fixed the issue!

Thank you so much for looking into this :)

SoledadIce commented 5 months ago

Hi @bryanapellanes-okta,

I just ran into the same issue today. The issue persists in 8.0.1 after the PR #722 And I daggered into the code for a while, the root cause is that the newly introduced UseProxy config hasn't been added to the extension method MergeConfigurations. So, the config is not properly set for most of the Apis.

Could you please kindly make a hotfix? Thanks!