Skip to content

Commit

Permalink
patch: updated the layout of the 'website' generated for GitHub pages
Browse files Browse the repository at this point in the history
  • Loading branch information
drew-viles committed Nov 2, 2022
1 parent c4c1713 commit 3584265
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 80 deletions.
93 changes: 47 additions & 46 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import (
"fmt"
"github.com/drew-viles/baskio/pkg/constants"
ostack "github.com/drew-viles/baskio/pkg/openstack"
"github.com/google/uuid"
"github.com/spf13/cobra"
"log"
"path/filepath"
"os"
"strings"
"time"
)

var (
Expand Down Expand Up @@ -82,52 +80,55 @@ func init() {
// Now we check to see if any env vars have been passed instead of flags. If so, set the flags to the env vars.
envs.CheckForEnvVars()

osClient := &ostack.Client{
Env: envs,
}
//osClient := &ostack.Client{
// Env: envs,
//}

//Build image
buildGitDir := fetchBuildRepo(envs.ImageRepo)
buildConfig := ostack.ParseBuildConfig(envs.OpenstackBuildConfigPath)
buildConfig.Networks = envs.NetworkID

scanDate := fmt.Sprintf("%d-%d-%d--%d-%d-%d", time.Now().Year(), time.Now().Month(), time.Now().Day(), time.Now().Hour(), time.Now().Minute(), time.Now().Second())
imageUUID, err := uuid.NewUUID()
if err != nil {
log.Fatalln(err)
}
imageName := fmt.Sprintf("%s-kube-%s-%s-%s", buildConfig.BuildName, buildConfig.KubernetesSemver, scanDate, imageUUID.String())
buildConfig.ImageName = imageName

generateVariablesFile(buildGitDir, buildConfig)

capiPath := filepath.Join(buildGitDir, "images/capi")
fetchDependencies(capiPath)
err = buildImage(capiPath, envs.BuildOS)
if err != nil {
log.Fatalln(err)
}
imgID, err := retrieveNewImageID()
if err != nil {
log.Fatalln(err)
}

//Scan image
osClient.OpenstackInit()
kp := osClient.CreateKeypair()
server, freeIP := osClient.CreateServer(kp, imgID, buildConfig.Flavor, buildConfig.Networks, enableConfigDriveFlag)

resultsFile, err := fetchResultsFromServer(freeIP, kp)
if err != nil {
removeScanningResources(server.ID, osClient)
log.Fatalln(err.Error())
}

defer resultsFile.Close()

//Cleanup the scanning resources
removeScanningResources(server.ID, osClient)

////Build image
//buildGitDir := fetchBuildRepo(envs.ImageRepo)

//scanDate := fmt.Sprintf("%d-%d-%d--%d-%d-%d", time.Now().Year(), time.Now().Month(), time.Now().Day(), time.Now().Hour(), time.Now().Minute(), time.Now().Second())
//imageUUID, err := uuid.NewUUID()
//if err != nil {
// log.Fatalln(err)
//}
//imageName := fmt.Sprintf("%s-kube-%s-%s-%s", buildConfig.BuildName, buildConfig.KubernetesSemver, scanDate, imageUUID.String())
//buildConfig.ImageName = imageName
//
//generateVariablesFile(buildGitDir, buildConfig)
//
//capiPath := filepath.Join(buildGitDir, "images/capi")
//fetchDependencies(capiPath)
//err = buildImage(capiPath, envs.BuildOS)
//if err != nil {
// log.Fatalln(err)
//}
//imgID, err := retrieveNewImageID()
//if err != nil {
// log.Fatalln(err)
//}
//
////Scan image
//osClient.OpenstackInit()
//kp := osClient.CreateKeypair()
//server, freeIP := osClient.CreateServer(kp, imgID, buildConfig.Flavor, buildConfig.Networks, enableConfigDriveFlag)
//
//resultsFile, err := fetchResultsFromServer(freeIP, kp)
//if err != nil {
// removeScanningResources(server.ID, osClient)
// log.Fatalln(err.Error())
//}
//
//defer resultsFile.Close()
//
////Cleanup the scanning resources
//removeScanningResources(server.ID, osClient)

imageName := "ubuntu-2204-kube-v1.25.3-2022-11-1--13-6-56-0fd58811-59e6-11ed-ac32-9e91db58e64a"
resultsFile, _ := os.Open("results.json")
//GitHub pages
pagesGitDir, pagesRepo, err := fetchPagesRepo(envs.GhUser, envs.GhToken, envs.GhProject, envs.GhPagesBranch)
if err != nil {
Expand All @@ -141,7 +142,7 @@ func init() {
checkErrorPagesWithCleanup(err, pagesGitDir)

results, err := parseReports(reports)
checkErrorPagesWithCleanup(err, pagesGitDir)
//checkErrorPagesWithCleanup(err, pagesGitDir)

err = buildPages(pagesGitDir, results)
checkErrorPagesWithCleanup(err, pagesGitDir)
Expand Down
8 changes: 6 additions & 2 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,15 @@ func parseReports(reports []string) (map[int]constants.Year, error) {
}

stripDirPrefix := strings.Split(v, "/")

reportName := stripDirPrefix[len(stripDirPrefix)-1:][0]
fileName := strings.Split(reportName, "-")

year, err := strconv.Atoi(fileName[1])
year, err := strconv.Atoi(fileName[4])
if err != nil {
return nil, err
}
month, err := strconv.Atoi(fileName[2])
month, err := strconv.Atoi(fileName[5])
if err != nil {
return nil, err
}
Expand All @@ -143,6 +144,9 @@ func parseReports(reports []string) (map[int]constants.Year, error) {
if allReports[year].Months[monthName].Reports == nil {
m := allReports[year].Months[monthName]
m.Reports = make(map[string]constants.ReportData)
shortSplit := strings.Split(reportName, "-")
shortName := shortSplit[len(shortSplit)-5:]
r.ShortName = strings.Join(shortName, "-")
m.Reports[reportName] = r
allReports[year].Months[monthName] = m
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/templates/index.html.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton">
{{- range $report, $rValue := $mValue.Reports}}
<li><a class="dropdown-item report-links" href="#" data-report-year="{{$year}}" data-report-month="{{$month}}" data-report-name="{{$report}}">{{$report}}</a></li>
<li><a class="dropdown-item report-links" href="#" data-report-year="{{$year}}" data-report-month="{{$month}}" data-report-name="{{$report}}">{{$rValue.ShortName}}</a></li>
{{- end}}
</ul>
</div>
Expand Down
8 changes: 2 additions & 6 deletions cmd/templates/js/class.js.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class ReportFile {

//Year takes the year from the document title.
Year() {
return this.file_name.split("-")[1];
return this.file_name.split("-")[4];
}

//MonthName converts the month number into a month name.
MonthName() {
const date = new Date();
date.setMonth(this.file_name.split("-")[2] - 1);
date.setMonth(this.file_name.split("-")[5]-1);

return date.toLocaleString('en-gb', {
month: 'long',
Expand All @@ -30,10 +30,6 @@ class ReportFile {
}
}

function loadIntoResults(report) {
console.log(report);
}

class CVE {
packages = [];

Expand Down
86 changes: 61 additions & 25 deletions cmd/templates/js/main.js.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -57,50 +57,86 @@ function loadResultsIntoAccordion(report) {
let critCount = 0;
let medCount = 0;
let lowCount = 0;
//let rowStart = `<div class="row">`;
//let rowEnd = `</div>`;
let cols = 4;
let rowStart = '<div class="row">';
let rowEnd = '</div>';

let lowAccord = "";
let medAccord = "";
let critAccord = "";

let cves = new CVEs(report.jsonContent.Results[0]);
cves.GetCVEs().forEach((cve) => {
let val = "";
switch (cve.severity) {
case "CRITICAL":
// if (critCount % 3 === 0) {
// criticalAccordion.innerHTML += rowStart;
// }
if (critCount % cols === 0) {
val += rowStart;
}

criticalAccordion.innerHTML += `${ButtonGen(cve)}`;
val += `${CardGen(cve)}`;

// if (critCount % 3 === 2) {
// criticalAccordion.innerHTML += rowEnd;
// }
if (critCount % cols === (cols-1)) {
val += rowEnd;
}
critAccord += val;
critCount++;
break;
case "MEDIUM":
// if (medCount % 3 === 0) {
// mediumAccordion.innerHTML += rowStart;
// }
mediumAccordion.innerHTML += `${ButtonGen(cve)}`;
// if (medCount % 3 === 2) {
// mediumAccordion.innerHTML += rowEnd;
// }
if (medCount % cols === 0) {
val += rowStart;
}

val += `${CardGen(cve)}`;

if (medCount % cols === (cols-1)) {
val += rowEnd;
}
medAccord += val;
medCount++;
break;
case "LOW":
// if (lowCount % 3 === 0) {
// lowAccordion.innerHTML += rowStart;
// }
lowAccordion.innerHTML += `${ButtonGen(cve)}`;
// if (lowCount % 3 === 2) {
// lowAccordion.innerHTML += rowEnd;
// }
console.log("raw:" + lowCount);
if (lowCount % cols === 0) {
val += rowStart;
}

val += `${CardGen(cve)}`;

if (lowCount % cols === (cols-1)) {
val += rowEnd;
}
lowAccord += val;

lowCount++;
break;
}
});


criticalAccordion.innerHTML = critAccord;
mediumAccordion.innerHTML = medAccord;
lowAccordion.innerHTML = lowAccord;

}

function ButtonGen(cve) {
return `<button class="btn btn-light" type="button" data-bs-toggle="modal" data-bs-target="#cveModal" data-cve-cve="${cve.id}" data-cve-pkg="${cve.packages}" data-cve-title="${cve.title}" data-cve-description="${cve.description}" data-cve-link="${cve.link}">${cve.id}</button>`
function CardGen(cve) {
return `<div class="col">
<div class="card" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">${cve.title.substring(0, 16) + "\u2026"}</h5>
<h6 class="card-subtitle mb-2 text-muted">${cve.id} - <span class="card-subtitle mb-2 text-muted">${cve.score}</span></h6>
<p class="card-text">${cve.description.substring(0, 50) + "\u2026"}</p>

<ul class="list-group list-group-flush">
${cve.packages.map(package => `<li class="list-group-item">${package}</li>`).join("")}
</ul>

<a href="#" class="card-link" data-bs-toggle="modal" data-bs-target="#cveModal" data-cve-cve="${cve.id}" data-cve-pkg="${cve.packages}" data-cve-title="${cve.title}" data-cve-description='${cve.description}' data-cve-uri="${cve.uri}">More info</a>
</div>
</div>

</div>`
}

// This allows the modal to be populated with the data from each of the CVE buttons that are generated.
Expand Down
1 change: 1 addition & 0 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Month struct {
// It is used for parsing and generating the static sites.
type ReportData struct {
Name string `json:"name"`
ShortName string `json:"short_name"`
SchemaVersion int `json:"schema_version"`
ArtifactName string `json:"artifact_name"`
ArtifactType string `json:"artifact_type"`
Expand Down

0 comments on commit 3584265

Please sign in to comment.