Skip to content

Commit a3fc61d

Browse files
author
Harri Klingsten
committed
Version 0.6
1 parent 5aff0df commit a3fc61d

File tree

10 files changed

+80
-19
lines changed

10 files changed

+80
-19
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,21 @@ This application is intended to use **CycloneDX** and **Dependency Track** softw
1010

1111
If you want to setup **CycloneDX** and **Dependency Track** in any other way, I recommend you to look at their respective documentation, links to their repos are present at the end of this document. You also need to adjust the ```PowerCommandsConfiguration.yaml``` configuration file with the appropriate API endpoints. You do not need to run the ```start``` command if the software is installed and already running on your machine or a server else where.
1212

13+
## Setup
14+
### Setup docker containers
15+
```setup --docker```
16+
17+
This will pull down the docker images needed if you want to run this with Docker Desktop, which is:
18+
- ghcr.io/cyclonedx/cdxgen:v8.6.0
19+
- dependencytrack/apiserver
20+
- dependencytrack/frontend
21+
22+
### Setup Dependency Track API key
23+
```setup dt-key```
24+
25+
Paste the API key copied from the Dependency Track Admin, this is only needed if you intend to automatically import your sbom file into Dependency Track (and why shouldn't you do that?).
26+
You must of course start Dependency Track with the ```start``` command described below.
27+
1328
## Start
1429
```start```
1530

dt-api-key.png

-883 Bytes
Loading

src/SecTools/PainKiller.PowerCommands.PowerCommandsConsole/PainKiller.PowerCommands.PowerCommandsConsole.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<ImplicitUsings>enable</ImplicitUsings>
88
<Nullable>enable</Nullable>
99
<AssemblyName>p-sec-tools</AssemblyName>
10-
<Version>0.5.0.0</Version>
10+
<Version>0.6.0.0</Version>
1111
</PropertyGroup>
1212

1313
<ItemGroup>

src/SecTools/SecToolsCommands/Commands/SbomCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public override async Task<RunResult> RunAsync()
2020

2121
if (HasOption("upload"))
2222
{
23-
var response = await DependencyTrackManager.PostSbom(Configuration.DependencyTracker.SbomApiUrl, jsonData, name, Configuration.Secret.DecryptSecret("##DT_PowerCommand##"));
23+
var response = await DependencyTrackManager.PostSbom($"{Configuration.DependencyTracker.ApiUrl}{Configuration.DependencyTracker.SbomApiUrl}", jsonData, name, Configuration.Secret.DecryptSecret("##DT_PowerCommand##"));
2424
if (response.StartsWith("Request failed with status code:")) WriteFailureLine(response);
2525
else WriteSuccessLine(response);
2626
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using PainKiller.PowerCommands.Core.Commands;
2+
using SecToolsCommands.Managers;
3+
4+
namespace SecToolsCommands.Commands;
5+
6+
[PowerCommandDesign( description: "Pull necessary docker images.",
7+
options: "docker|dt-key",
8+
example: "setup")]
9+
public class SetupCommand : CommandBase<PowerCommandsConfiguration>
10+
{
11+
public SetupCommand(string identifier, PowerCommandsConfiguration configuration) : base(identifier, configuration) { }
12+
13+
public override RunResult Run()
14+
{
15+
if (HasOption("docker"))
16+
{
17+
DockerDesktopManager.StartDockerDesktop(Configuration.DockerDesktop.Path, Configuration.DockerDesktop.StartupTime);
18+
19+
DockerDesktopManager.Pull(Configuration.Cdxgen.ImageUrl);
20+
WriteSuccessLine("\nCdxgen setup done!");
21+
22+
DockerDesktopManager.Pull(Configuration.DependencyTracker.ApiServerImage);
23+
DockerDesktopManager.Pull(Configuration.DependencyTracker.FrontendImage);
24+
25+
WriteSuccessLine("\nDependency track setup done!");
26+
}
27+
else if (HasOption("dt-key"))
28+
{
29+
var secretCommand = new SecretCommand("secret", Configuration);
30+
secretCommand.InitializeAndValidateInput("secret --create \"DT_PowerCommand\"".Interpret());
31+
secretCommand.Run();
32+
}
33+
return Ok();
34+
}
35+
}

src/SecTools/SecToolsCommands/Commands/StartCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ public override RunResult Run()
1717
var xCfg = Configuration.Cdxgen;
1818
CycloneDxManager.Start(xCfg.HostMount, xCfg.ContainerMount, xCfg.HostPort, xCfg.ContainerPort, xCfg.SdxGenServerVolumeMount, xCfg.ImageUrl, xCfg.ServerHost);
1919

20-
DependencyTrackManager.Start(Configuration.DependencyTracker.UrlToDockerComposeFile, Configuration.DependencyTracker.AdminUrl, Configuration.DependencyTracker.StartupTime);
20+
var dCfg = Configuration.DependencyTracker;
21+
DependencyTrackManager.Start(dCfg.ApiUrl, dCfg.ApiServerImage, dCfg.ApiServerContainer, dCfg.ApiPorts, dCfg.FrontendImage, dCfg.FrontendContainer, dCfg.FrontendPorts, dCfg.AdminUrl, dCfg.StartupTime);
2122

2223
WriteHeadLine("You are now ready to create SBOM files\n");
2324
WriteLine("Notice that to automatically upload the SBOM to Dependency Track, you first need to do two things:");
24-
WriteLine("1) Create a Team in Dependency Track give the Team BOM_UPLOAD, PROJECT_CREATION, PORTFOLIO_MANAGEMENT permissions");
25+
WriteLine("1) Create a Team in Dependency Track give the Team BOM_UPLOAD, PROJECT_CREATION");
2526
WriteLine("2) Copy the API key, and create a secret in PowerCommands like this:");
2627
WriteCodeExample("secret","--create \"DT_PowerCommand\"");
2728

src/SecTools/SecToolsCommands/Configuration/DependencyTrackerConfiguration.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33
public class DependencyTrackerConfiguration
44
{
55
public string UrlToDockerComposeFile { get; set; } = "https://dependencytrack.org/docker-compose.yml";
6-
public string SbomApiUrl { get; set; } = "http://localhost:8081/api/v1/bom";
6+
public string SbomApiUrl { get; set; } = "/api/v1/bom";
77
public string AdminUrl { get; set; } = "http://localhost:8080";
8+
public string ApiUrl { get; set; } = "http://localhost:8081";
9+
public string ApiPorts { get; set; } = "8081:8080";
10+
public string ApiServerImage { get; set; } = "dependencytrack/apiserver";
11+
public string ApiServerContainer { get; set; } = "dtrack-frontend";
12+
public string FrontendImage { get; set; } = "dependencytrack/frontend";
13+
public string FrontendContainer { get; set; } = "dtrack-apiserver";
14+
public string FrontendPorts { get; set; } = "8080:8080";
815
public int StartupTime { get; set; } = 5;
16+
917
}

src/SecTools/SecToolsCommands/Managers/DependencyTrackManager.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,10 @@ public static async Task<string> PostSbom(string apiUrl, string sbomJson, string
1919
var responseBody = await response.Content.ReadAsStringAsync();
2020
return responseBody;
2121
}
22-
23-
public static void Start(string urlToDockerComposeFile, string adminUrl, int startupTime)
22+
public static void Start(string apiUrl, string apiServerImage, string apiServerContainer, string apiPorts, string frontendImage, string frontendContainer, string frontendPorts, string adminUrl, int startupTime)
2423
{
25-
var dockerComposeFileName = Path.Combine(PowerCommandsConfiguration.AppDataFolderDependencyTrack, "docker-compose.yml");
26-
if (!File.Exists(dockerComposeFileName))
27-
{
28-
var httpClient = new HttpClient();
29-
var yamlData = httpClient.GetStringAsync(urlToDockerComposeFile).Result;
30-
File.WriteAllText(dockerComposeFileName, yamlData);
31-
ConsoleService.Service.WriteSuccessLine(nameof(DependencyTrackManager), "Docker compose file downloaded and save in app directory.");;
32-
}
33-
34-
ShellService.Service.Execute("docker-compose", "up -d", PowerCommandsConfiguration.AppDataFolderDependencyTrack);
24+
ShellService.Service.Execute("docker", $"run -d --name {apiServerContainer} -p {apiPorts} {apiServerImage}", "");
25+
ShellService.Service.Execute("docker", $"run -d --name {frontendContainer} -e API_BASE_URL={apiUrl} -p {frontendPorts} --restart unless-stopped {frontendImage}", "");
3526
ConsoleService.Service.WriteSuccessLine(nameof(DependencyTrackManager), "Dependency Track container starting...");;
3627

3728
PauseService.Pause(startupTime);

src/SecTools/SecToolsCommands/Managers/DockerDesktopManager.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
namespace SecToolsCommands.Managers;
2-
32
public static class DockerDesktopManager
43
{
54
public static void StartDockerDesktop(string fullFileName, int startupTime)
@@ -22,4 +21,9 @@ public static void StartDockerDesktop(string fullFileName, int startupTime)
2221
};
2322
ShellService.Service.Execute("docker", arguments: "info", workingDirectory: "", reader, fileExtension: "", waitForExit: true);
2423
}
24+
public static void Pull(string image)
25+
{
26+
Console.WriteLine($"Pull image {image}... please wait, result will not show before the whole process is done.");
27+
ShellService.Service.Execute("docker", $"pull {image}", workingDirectory: "", waitForExit: true);
28+
}
2529
}

src/SecTools/SecToolsCommands/PowerCommandsConfiguration.yaml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ configuration:
2020
dependencyTracker:
2121
urlToDockerComposeFile: https://dependencytrack.org/docker-compose.yml
2222
adminUrl: http://localhost:8080
23-
sbomApiUrl: http://localhost:8081/api/v1/bom
23+
apiUrl: http://localhost:8081
24+
sbomApiUrl: /api/v1/bom
2425
startupTime: 5
26+
apiServerImage: dependencytrack/apiserver
27+
apiServerContainer: dtrack-apiserver
28+
apiPorts: 8081:8080
29+
frontendImage: dependencytrack/frontend
30+
frontendContainer: dtrack-frontend
31+
frontendPorts: 8080:8080
2532
startupToolbar:
2633
hideToolbarOption: OnCommandHighlighted
2734
toolbarItems:

0 commit comments

Comments
 (0)