Skip to content

Commit

Permalink
feat: enabling SCLE for SAST
Browse files Browse the repository at this point in the history
  • Loading branch information
metju90 committed Oct 3, 2023
1 parent b004ef0 commit 3f9cf74
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 38 deletions.
26 changes: 26 additions & 0 deletions Snyk.Common.Tests/Service/ApiEndpointResolverTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,31 @@ public void AuthenticationMethod()
// Assert
Assert.Equal(AuthenticationType.OAuth, apiEndpointResolver.AuthenticationMethod);
}

[Fact]
public void ApiEndpointResolver_GetSnykCodeApiUrl_LocalEngine()
{
SastSettings mockedSettings = new SastSettings
{
SastEnabled = true,
LocalCodeEngine = new LocalCodeEngine
{
Enabled = true,
Url = "http://foo.bar/api"
}
};


var optionsMock = new Mock<ISnykOptions>();
optionsMock
.Setup(options => options.SastSettings)
.Returns(mockedSettings);

var apiEndpointResolver = new ApiEndpointResolver(optionsMock.Object);

var snykCodeApiUrl = apiEndpointResolver.GetSnykCodeApiUrl();

Assert.Equal("http://foo.bar/api/", snykCodeApiUrl);
}
}
}
7 changes: 7 additions & 0 deletions Snyk.Common/Service/ApiEndpointResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public string GetSnykApiEndpoint()
/// </summary>
public string GetSnykCodeApiUrl()
{
if (this.IsLocalEngine())
{
return this.options.SastSettings.LocalCodeEngine.Url + "/";
}

var endpoint = ResolveCustomEndpoint(this.options.CustomEndpoint);
var uri = new Uri(endpoint);

Expand Down Expand Up @@ -106,5 +111,7 @@ private bool IsSaaS(Uri uri) =>
/// </summary>
private bool IsSingleTenant(Uri uri) =>
uri.Host.StartsWith("app") && uri.Host.EndsWith("snyk.io");

private bool IsLocalEngine() => this.options.SastSettings?.LocalCodeEngineEnabled ?? false;
}
}
3 changes: 1 addition & 2 deletions Snyk.Common/Service/SastSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public class SastSettings
/// Gets a value indicating whether Snyk Code enabled.
/// Snyk Code enabled if SastEnabled = true and LocalCodeEngine.Enabled = false.
/// </summary>
public bool SnykCodeEnabled => this.SastEnabled && !this.LocalCodeEngineEnabled;

public bool SnykCodeEnabled => this.SastEnabled;
/// <summary>
/// Gets a value indicating whether local code engine enabled/disabled.
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions Snyk.Common/Service/SnykApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public async Task<SastSettings> GetSastSettingsAsync()

try
{
return Json.Deserialize<SastSettings>(responseContent);
SastSettings sastSettings = Json.Deserialize<SastSettings>(responseContent);
this.options.SastSettings = sastSettings;
return sastSettings;
}
catch (Exception e)
{
Expand All @@ -81,7 +83,8 @@ public async Task<HttpResponseMessage> SendSastSettingsRequestAsync()
{
httpRequest.Headers.Add("x-snyk-ide", $"{SnykExtension.IntegrationName}-{SnykExtension.Version}");

return await HttpClient.SendAsync(httpRequest);
var test = await HttpClient.SendAsync(httpRequest);
return test;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Snyk.Common/Settings/ISnykOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Threading.Tasks;
using Snyk.Common.Authentication;
using Snyk.Common.Service;

/// <summary>
/// Interface for Snyk Options/Settings in Visual Studio.
Expand Down Expand Up @@ -103,5 +104,7 @@ public interface ISnykOptions
/// Force Visual Studio to load Settings from storage.
/// </summary>
void LoadSettingsFromStorage();

SastSettings SastSettings { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public class SnykGeneralOptionsDialogPage : DialogPage, ISnykOptions
/// </summary>
public AuthenticationToken ApiToken => this.apiToken ?? AuthenticationToken.EmptyToken;

private SastSettings sastSettings;

private string RefreshToken()
{
var cli = this.ServiceProvider?.NewCli();
Expand Down Expand Up @@ -133,6 +135,20 @@ public string CustomEndpoint
/// <inheritdoc/>
public string SnykCodeSettingsUrl => $"{this.GetAppCustomEndpoint()}/manage/snyk-code";

public SastSettings SastSettings
{
get => this.sastSettings;

set
{
if (this.sastSettings == value)
{
return;
}

this.sastSettings = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether organization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,18 +322,9 @@ private void UpdateSnykCodeEnablementSettings(SastSettings sastSettings)
this.codeSecurityEnabledCheckBox.Enabled = snykCodeEnabled;
this.codeQualityEnabledCheckBox.Enabled = snykCodeEnabled;

if (sastSettings?.LocalCodeEngineEnabled ?? false)
if (!snykCodeEnabled)
{
this.snykCodeDisabledInfoLabel.Text =
"Snyk Code is configured to use a Local Code Engine instance. This setup is not yet supported by the extension.";

this.snykCodeDisabledInfoLabel.Visible = true;
this.snykCodeSettingsLinkLabel.Visible = false;
this.checkAgainLinkLabel.Visible = false;
}
else
{
this.snykCodeDisabledInfoLabel.Text = "Snyk Code is disabled by your organisation\'s configuration:";
this.snykCodeDisabledInfoLabel.Text = "Test!! Snyk Code is disabled by your organisation\'s configuration:";

this.snykCodeDisabledInfoLabel.Visible = !snykCodeEnabled;
this.snykCodeSettingsLinkLabel.Visible = !snykCodeEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public MessagePanel()
this.messagePanel,
this.overviewPanel,
this.scanningProjectMessagePanel,
this.localCodeEngineIsDisabledPanel,
};
}

Expand Down Expand Up @@ -76,11 +75,6 @@ public string Text
/// </summary>
public void ShowSelectIssueMessage() => this.ShowPanel(this.selectIssueMessagePanel);

/// <summary>
/// Shows the "local code engine is disabled" message.
/// </summary>
public void ShowDisabledDueToLocalCodeEngineMessage() => this.ShowPanel(this.localCodeEngineIsDisabledPanel);

/// <summary>
/// Show scanning message.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ public void OnSnykCodeDisabledHandler(object sender, SnykCodeScanEventArgs event
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

var disabledNodeState = eventArgs.LocalCodeEngineEnabled
? RootTreeNodeState.LocalCodeEngineIsEnabled : RootTreeNodeState.DisabledForOrganization;
var disabledNodeState = RootTreeNodeState.DisabledForOrganization;

this.resultsTree.CodeQualityRootNode.State = disabledNodeState;
this.resultsTree.CodeSecurityRootNode.State = disabledNodeState;
Expand Down Expand Up @@ -473,11 +472,6 @@ private RootTreeNodeState GetSnykCodeRootNodeState(SastSettings sastSettings, bo
return RootTreeNodeState.Disabled;
}

if (sastSettings.LocalCodeEngineEnabled)
{
return RootTreeNodeState.LocalCodeEngineIsEnabled;
}

if (!sastSettings.SastEnabled)
{
return RootTreeNodeState.DisabledForOrganization;
Expand Down Expand Up @@ -608,13 +602,6 @@ private void HandleRootTreeNodeSelected()
if (selectedItem is SnykCodeQualityRootTreeNode || selectedItem is SnykCodeSecurityRootTreeNode)
{
var rootTreeNode = selectedItem as RootTreeNode;

if (rootTreeNode.State == RootTreeNodeState.LocalCodeEngineIsEnabled)
{
this.messagePanel.ShowDisabledDueToLocalCodeEngineMessage();

return;
}
}

this.messagePanel.ShowSelectIssueMessage();
Expand Down
3 changes: 0 additions & 3 deletions Snyk.VisualStudio.Extension.Shared/UI/Tree/RootTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ public override string Title
case RootTreeNodeState.Error:
title = this.GetTitlePrefix() + " (error)";
break;
case RootTreeNodeState.LocalCodeEngineIsEnabled:
title = this.GetTitlePrefix() + " (disabled due to Local Code Engine)";
break;
case RootTreeNodeState.NoFilesForSnykCodeScan:
title = this.GetTitlePrefix() + " (no supported code available)";
break;
Expand Down

0 comments on commit 3f9cf74

Please sign in to comment.