Skip to content

Commit

Permalink
refactor: transform persisted api custom endpoint to new schema (#275)
Browse files Browse the repository at this point in the history
* fix: translate old api to new api endpoint

* chore: added more tests

* fix: transform snyk.io/api urls
  • Loading branch information
ShawkyZ authored Jun 13, 2024
1 parent a947c0e commit d3f8f9a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 18 deletions.
17 changes: 16 additions & 1 deletion Snyk.Common/Service/ApiEndpointResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Snyk.Common.Service
public class ApiEndpointResolver
{
private readonly ISnykOptions options;
private const string DefaultApiEndpoint = "https://api.snyk.io";
public const string DefaultApiEndpoint = "https://api.snyk.io";
public const string DefaultAppEndpoint = "https://app.snyk.io";

/// <summary>
Expand Down Expand Up @@ -106,6 +106,21 @@ public static string GetCustomEndpointUrlFromSnykApi(string apiEndpoint, string
return uriBuilder.ToString().RemoveTrailingSlashes();
}

public static string TranslateOldApiToNewApiEndpoint(string apiEndpoint)
{
if (apiEndpoint.Contains("https://snyk.io/api"))
apiEndpoint = "https://app.snyk.io/api";

if (!apiEndpoint.Contains("app.") || !apiEndpoint.RemoveTrailingSlashes().EndsWith("/api"))
return apiEndpoint;

var endpointUri = new Uri(apiEndpoint);

var newEndpoint = endpointUri.Host.Replace("app.", "api.");
var uriBuilder = new UriBuilder(endpointUri.Scheme, newEndpoint);
return uriBuilder.ToString().RemoveTrailingSlashes();
}

private bool IsLocalEngine() => this.options.SastSettings?.LocalCodeEngineEnabled ?? false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ public bool IsFedramp()
/// <returns></returns>
public bool IsAnalyticsPermitted()
{
var endpointUri = new Uri(this.GetBaseAppUrl());
var endpointUri = new Uri(this.GetCustomApiEndpoint());

var permittedHosts = new string[] { "app.snyk.io", "app.us.snyk.io" };
var permittedHosts = new string[] { "api.snyk.io", "api.us.snyk.io" };
return permittedHosts.Contains(endpointUri.Host.ToLower());
}

Expand All @@ -136,7 +136,8 @@ public string CustomEndpoint
value = string.Empty;
}

if (this.customEndpoint == value)
var newApiEndpoint = ApiEndpointResolver.TranslateOldApiToNewApiEndpoint(value);
if (this.customEndpoint == newApiEndpoint)
{
return;
}
Expand All @@ -146,7 +147,7 @@ public string CustomEndpoint
var cli = this.ServiceProvider?.NewCli();
cli?.UnsetApiToken(); // This setter can be called before initialization, so ServiceProvider can be null

this.customEndpoint = value;
this.customEndpoint = newApiEndpoint;
this.FireSettingsChangedEvent();
}
}
Expand Down Expand Up @@ -415,12 +416,17 @@ public bool Authenticate()

private void FireSettingsChangedEvent() => this.SettingsChanged?.Invoke(this, new SnykSettingsChangedEventArgs());

private string GetBaseAppUrl()
public string GetCustomApiEndpoint()
{
return string.IsNullOrEmpty(customEndpoint) ? ApiEndpointResolver.DefaultApiEndpoint : ApiEndpointResolver.TranslateOldApiToNewApiEndpoint(customEndpoint);
}

public string GetBaseAppUrl()
{
if (string.IsNullOrEmpty(customEndpoint))
return ApiEndpointResolver.DefaultAppEndpoint;

var result = ApiEndpointResolver.GetCustomEndpointUrlFromSnykApi(customEndpoint, "app");
var result = ApiEndpointResolver.GetCustomEndpointUrlFromSnykApi(GetCustomApiEndpoint(), "app");

return string.IsNullOrEmpty(result) ? ApiEndpointResolver.DefaultAppEndpoint : result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
namespace Snyk.VisualStudio.Extension.Tests
{
using System.Collections.Generic;
using System.Net;
using Moq;
using Snyk.VisualStudio.Extension.Shared.CLI;
using Snyk.VisualStudio.Extension.Shared.Service;
using Snyk.VisualStudio.Extension.Shared.Settings;
using Xunit;
using Moq;
using Snyk.VisualStudio.Extension.Shared.CLI;
using Snyk.VisualStudio.Extension.Shared.Service;
using Snyk.VisualStudio.Extension.Shared.Settings;
using Xunit;

public class GeneralOptionsDialogPage
namespace Snyk.VisualStudio.Extension.Tests
{
public class GeneralOptionsDialogPageTest
{
[Fact]
public void ApiEndpointChanged_InvalidatesCliToken()
Expand All @@ -30,7 +28,6 @@ public void ApiEndpointChanged_InvalidatesCliToken()
}

[Theory]
[InlineData("https://snyk.io/api", true)]
[InlineData("https://app.snyk.io/api", true)]
[InlineData("https://app.us.snyk.io/api", true)]
[InlineData("https://app.eu.snyk.io/api", false)]
Expand Down Expand Up @@ -69,5 +66,38 @@ public void SnykCodeSettingsUrl(string endpoint, string expected)
optionsDialogPage.CustomEndpoint = endpoint;
Assert.Equal(expected, optionsDialogPage.SnykCodeSettingsUrl);
}

[Theory]
[InlineData(null, "https://api.snyk.io")]
[InlineData("", "https://api.snyk.io")]
[InlineData("https://snyk.io/api", "https://api.snyk.io")]
[InlineData("https://app.snyk.io/api", "https://api.snyk.io")]
[InlineData("https://app.snykgov.io/api", "https://api.snykgov.io")]
[InlineData("https://app.eu.snyk.io/api", "https://api.eu.snyk.io")]
[InlineData("https://api.snyk.io", "https://api.snyk.io")]
[InlineData("https://api.snykgov.io", "https://api.snykgov.io")]
[InlineData("https://api.eu.snyk.io", "https://api.eu.snyk.io")]
public void TransformApiToNewSchema(string endpoint, string expected)
{
var optionsDialogPage = new SnykGeneralOptionsDialogPage();
optionsDialogPage.CustomEndpoint = endpoint;
Assert.Equal(expected, optionsDialogPage.GetCustomApiEndpoint());
}

[Theory]
[InlineData(null, "https://app.snyk.io")]
[InlineData("", "https://app.snyk.io")]
[InlineData("https://app.snyk.io/api", "https://app.snyk.io")]
[InlineData("https://app.snykgov.io/api", "https://app.snykgov.io")]
[InlineData("https://app.eu.snyk.io/api", "https://app.eu.snyk.io")]
[InlineData("https://api.snyk.io", "https://app.snyk.io")]
[InlineData("https://api.snykgov.io", "https://app.snykgov.io")]
[InlineData("https://api.eu.snyk.io", "https://app.eu.snyk.io")]
public void GetBaseAppUrl(string endpoint, string expected)
{
var optionsDialogPage = new SnykGeneralOptionsDialogPage();
optionsDialogPage.CustomEndpoint = endpoint;
Assert.Equal(expected, optionsDialogPage.GetBaseAppUrl());
}
}
}

0 comments on commit d3f8f9a

Please sign in to comment.