Skip to content

Commit

Permalink
feat: enabling SCLE for SAST [NEBULA-1456] (#248)
Browse files Browse the repository at this point in the history
* feat: enabling SCLE for SAST

* docs: adding a missing step in contributing.md
  • Loading branch information
metju90 authored Oct 20, 2023
1 parent b004ef0 commit ffac4a3
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 36 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Snyk Changelog

## [1.2.0]

### Added
- Support for Snyk Code Local Engine

## [1.1.38]

### Fixed
Expand Down
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Please, check the "Visual Studio extension development" checkbox to install exte

![Visual Studio Installer Visual Studio extension development](./doc/images/Contribution_screenshot_2.png)

## Execute snyk-visual-studio-plugin

Double click on `snyk-visual-studio-plugin.sln` ![snyk-visual-studio-plugin.sln](doc/images/Contribution_screenshot_8.png)

## Solution organization

Snyk Visual Studio extension consists of several main projects:
Expand Down
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
4 changes: 3 additions & 1 deletion 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 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,16 +322,7 @@ private void UpdateSnykCodeEnablementSettings(SastSettings sastSettings)
this.codeSecurityEnabledCheckBox.Enabled = snykCodeEnabled;
this.codeQualityEnabledCheckBox.Enabled = snykCodeEnabled;

if (sastSettings?.LocalCodeEngineEnabled ?? false)
{
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
if (!snykCodeEnabled)
{
this.snykCodeDisabledInfoLabel.Text = "Snyk Code is disabled by your organisation\'s configuration:";

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
Binary file added doc/images/Contribution_screenshot_8.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit ffac4a3

Please sign in to comment.