Skip to content

Commit

Permalink
add unit tests (#8)
Browse files Browse the repository at this point in the history
* feat: add unit tests

* feat: add info

* fix: change from 644 to 664

* fix: permission

* fix: unit test

* feat: using sudo

* feat: full sudo acc
ess

* fix: naming

* chore: debug

* chore: debug

* chore: debug

* chore: debug

* feat: test using docker instead
  • Loading branch information
habibiefaried authored Apr 13, 2022
1 parent c0c2321 commit 6dceb89
Show file tree
Hide file tree
Showing 17 changed files with 295 additions and 246 deletions.
14 changes: 2 additions & 12 deletions .github/workflows/unittest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ jobs:
- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Get dependencies & Test
- name: Run docker test
run: |
go get -v -t -d ./...
if [ -f Gopkg.toml ]; then
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
dep ensure
fi
go test -v ./...
docker build . --no-cache -f Dockerfile.test -t axolotl-test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ outputs
config.yml
.bin
*.pdf
testworkdir
temporarydir
testworkdir
6 changes: 6 additions & 0 deletions Dockerfile.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM golang:1.17-alpine3.15 as builder

COPY . /goapp
WORKDIR /goapp

RUN apk add git build-base && go test -v ./...
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ build:

dev-install:
sudo cp .bin/axolotl-amd64-linux /usr/bin/axolotl

254 changes: 127 additions & 127 deletions cmd/gen.go
Original file line number Diff line number Diff line change
@@ -1,127 +1,127 @@
package cmd

import (
"os"
"fmt"
"log"
"bytes"
"strings"
"os/exec"
"io/ioutil"
"text/template"

"github.com/k1m0ch1/axolotl/utils"
"github.com/spf13/cobra"
)

var (
Output string
Template string
)

func init(){
genCmd.PersistentFlags().StringVarP(&Output, "out", "O", "", "File Output")
genCmd.PersistentFlags().StringVarP(&Template, "template", "T", "", "Markdown Template")
}

var genCmd = &cobra.Command{
Use: "gen",
Short: "Generate report from Markdown Template into PDF Format",
Run: func(cmd *cobra.Command, args []string) {
var cfg utils.UserConfig
cfg.Load("config.yml")

_, err := exec.LookPath("pandoc")
if err != nil {
log.Println("[!] you need to install `pandoc` on your machine")
log.Println("[!] here is how to install https://pandoc.org/installing.html")
log.Println("[!] or you can run the file install-pandoc.sh in here:")
log.Println("[!] https://github.com/k1m0ch1/axolotl/blob/master/install-pandoc.sh")
log.Fatal(err)
}
out, err := exec.Command("pandoc", "-v").Output()
if err != nil {
log.Fatal(err)
}

Pandoc := strings.Split(string(out),"\n")
pandorDirConfig := strings.ReplaceAll(strings.Split(Pandoc[3], ":")[1], " ", "")
templateIsExist := fmt.Sprintf("%s/templates/eisvogel.latex", pandorDirConfig)
if _, err := os.Stat(templateIsExist); os.IsNotExist(err) {
log.Printf("[!] File template eisvogel does not exist at %s/templates", pandorDirConfig)
log.Println("[!] you need to download file templates from here :")
log.Println("[!] https://github.com/Wandmalfarbe/pandoc-latex-template/blob/master/eisvogel.tex")
log.Printf("[!] and put the file as %s/templates/eisvogel.latex", pandorDirConfig)
}else{
if Output != "" && Template != "" {
var domain utils.HostIdentity
var ReportVar utils.ReportVar
var buf bytes.Buffer
var vulns []utils.Finding
domain.Load(fmt.Sprintf("./%s/%s.yml", cfg.DirConfig.HostsIdentityDir, Domain))
pathVuln := fmt.Sprintf("./%s/%s/", cfg.DirConfig.VulnDir, domain.ID)

if _, err := os.Stat(pathVuln); os.IsNotExist(err) {
fmt.Println("0 Result Vulnerability")
os.Exit(0)
}else{
// get many vuln-type found
VulnFile, err := utils.WalkMatch(pathVuln, "*.yml")
if err != nil {
fmt.Println(err)
}
for _, f := range VulnFile {
var v utils.Finding
v.Load(f)
vulns = append(vulns, v)
}
}

ReportVar.Host = domain
ReportVar.User = cfg
ReportVar.Vulns = vulns
content, _ := ioutil.ReadFile(Template)

sContent := string(content)

tmpl, err := template.New("test").Funcs(template.FuncMap{
"add": func(a, b int) int {
return a + b
},
}).Parse(sContent)
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(&buf, ReportVar)
if err != nil {
log.Fatal(err)
}

result := buf.String()
tmpFile, err := ioutil.TempFile("", "axolotl-templates*.md")
if err != nil {
log.Fatal(err)
}

if _, err := tmpFile.Write([]byte(result)); err != nil {
fmt.Println(err)
}
_, err = ioutil.ReadFile(tmpFile.Name())
if err != nil {
fmt.Println(err)
}
_, err = exec.Command("pandoc", tmpFile.Name(), "-o", Output, "--from", "markdown+yaml_metadata_block+raw_html", "--template", "eisvogel", "--table-of-contents", "--toc-depth", "6", "--number-sections", "--top-level-division=chapter", "--highlight-style", "breezedark").Output()
if err != nil {
log.Fatal(err)
}

defer os.Remove(tmpFile.Name())
}
}
},
}
package cmd

import (
"bytes"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"strings"
"text/template"

"github.com/k1m0ch1/axolotl/utils"
"github.com/spf13/cobra"
)

var (
Output string
Template string
)

func init() {
genCmd.PersistentFlags().StringVarP(&Output, "out", "O", "", "File Output")
genCmd.PersistentFlags().StringVarP(&Template, "template", "T", "", "Markdown Template")
}

var genCmd = &cobra.Command{
Use: "gen",
Short: "Generate report from Markdown Template into PDF Format",
Run: func(cmd *cobra.Command, args []string) {
var cfg utils.UserConfig
cfg.Load("config.yml")

_, err := exec.LookPath("pandoc")
if err != nil {
log.Println("[!] you need to install `pandoc` on your machine")
log.Println("[!] here is how to install https://pandoc.org/installing.html")
log.Println("[!] or you can run the file install-pandoc.sh in here:")
log.Println("[!] https://github.com/k1m0ch1/axolotl/blob/master/install-pandoc.sh")
log.Fatal(err)
}

out, err := exec.Command("pandoc", "-v").Output()
if err != nil {
log.Fatal(err)
}

Pandoc := strings.Split(string(out), "\n")
pandorDirConfig := strings.ReplaceAll(strings.Split(Pandoc[3], ":")[1], " ", "")
templateIsExist := fmt.Sprintf("%s/templates/eisvogel.latex", pandorDirConfig)
if _, err := os.Stat(templateIsExist); os.IsNotExist(err) {
log.Printf("[!] File template eisvogel does not exist at %s/templates", pandorDirConfig)
log.Println("[!] you need to download file templates from here :")
log.Println("[!] https://github.com/Wandmalfarbe/pandoc-latex-template/blob/master/eisvogel.tex")
log.Printf("[!] and put the file as %s/templates/eisvogel.latex", pandorDirConfig)
} else {
if Output != "" && Template != "" {
var domain utils.HostIdentity
var ReportVar utils.ReportVar
var buf bytes.Buffer
var vulns []utils.Finding
domain.Load(fmt.Sprintf("./%s/%s.yml", cfg.DirConfig.HostsIdentityDir, Domain))
pathVuln := fmt.Sprintf("./%s/%s/", cfg.DirConfig.VulnDir, domain.ID)

if _, err := os.Stat(pathVuln); os.IsNotExist(err) {
fmt.Println("0 Result Vulnerability")
os.Exit(0)
} else {
// get many vuln-type found
VulnFile, err := utils.WalkMatch(pathVuln, "*.yml")
if err != nil {
fmt.Println(err)
}

for _, f := range VulnFile {
var v utils.Finding
v.Load(f)
vulns = append(vulns, v)
}
}

ReportVar.Host = domain
ReportVar.User = cfg
ReportVar.Vulns = vulns
content, _ := ioutil.ReadFile(Template)

sContent := string(content)

tmpl, err := template.New("test").Funcs(template.FuncMap{
"add": func(a, b int) int {
return a + b
},
}).Parse(sContent)
if err != nil {
log.Fatal(err)
}
err = tmpl.Execute(&buf, ReportVar)
if err != nil {
log.Fatal(err)
}

result := buf.String()

tmpFile, err := ioutil.TempFile("", "axolotl-templates*.md")
if err != nil {
log.Fatal(err)
}

if _, err := tmpFile.Write([]byte(result)); err != nil {
fmt.Println(err)
}

_, err = ioutil.ReadFile(tmpFile.Name())
if err != nil {
fmt.Println(err)
}

_, err = exec.Command("pandoc", tmpFile.Name(), "-o", Output, "--from", "markdown+yaml_metadata_block+raw_html", "--template", "eisvogel", "--table-of-contents", "--toc-depth", "6", "--number-sections", "--top-level-division=chapter", "--highlight-style", "breezedark").Output()
if err != nil {
log.Fatal(err)
}

defer os.Remove(tmpFile.Name())
}
}
},
}
Loading

0 comments on commit 6dceb89

Please sign in to comment.