Skip to content

Commit

Permalink
print pretty name
Browse files Browse the repository at this point in the history
  • Loading branch information
niqdev committed Oct 8, 2023
1 parent 070d42f commit 80afd7d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 21 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ hckctl box arch --provider kube
hckctl box parrot --provider cloud
```

#### HTB example
#### HTB demo

Prerequisites
* start the retired [Postman](https://app.hackthebox.com/machines/Postman) machine in your account
Expand Down Expand Up @@ -112,7 +112,7 @@ hckctl task nuclei --inline -- -u https://example.com
tail -F ${HOME}/.local/state/hck/task/log/task-*
```

#### TryHackMe example
#### TryHackMe demo

> TODO

Expand Down Expand Up @@ -162,7 +162,7 @@ hckctl flow fuzz 127.0.0.1:8080
hckctl flow sql 127.0.0.1:3306
hckctl flow atomic-red-team 127.0.0.1 T1485
hckctl flow c2 ping
hckctl flow campaign/phishing @example.com
hckctl flow phishing @example.com
```

### Template
Expand Down Expand Up @@ -203,8 +203,6 @@ curl -sSL https://github.com/hckops/hckctl/releases/download/${HCKCTL_VERSION}/h

## Provider

List of currently supported providers

### Docker

Follow the official [instructions](https://docs.docker.com/engine/install) to install Docker Engine. The fastest way to get started is with the [convenience script](https://get.docker.com)
Expand Down Expand Up @@ -281,6 +279,7 @@ tail -F ${HOME}/.local/state/hck/log/hckctl-*.log
* cmd aliases e.g. start/up/create
* replace task/htb example with thm
* verify/support kube config relative path
* update cloud
TODO
* priority
Expand Down Expand Up @@ -327,7 +326,6 @@ TODO
- keep up-to-date directories to exclude in `resolvePath` e.g. charts
- add filters and review output e.g. table
* box
- print name partially resolved e.g. `box/preview/parrot-sec` or `preview/parrot-sec`
- print/event shared directory, same as envs, ports etc.
- review tty resize
- expose copy from/to ???
Expand Down Expand Up @@ -363,7 +361,6 @@ TODO
- rename output log file with timestamp?
- for debug purposes prepend file output with interpolated task (yaml) or command parameters
- add command to remove all logs
- print name partially resolved e.g. `task/scanner/<NAME>` or `scanner/<NAME>`
- skip output file for `help` and `version`
- add argument `--volume` to restrict shared directories/files
* version
Expand Down
5 changes: 3 additions & 2 deletions internal/command/box/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ func runBoxClient(sourceLoader template.SourceLoader[boxModel.BoxV1], provider b
return errors.New("invalid template")
}

templateName := commonCmd.PrettyName(boxTemplate, configRef.Config.Template.CacheDir, boxTemplate.Value.Data.Name)
loader := commonCmd.NewLoader()
loader.Start("loading template %s", boxTemplate.Value.Data.Name)
loader.Start("loading template %s", templateName)
defer loader.Stop()

log.Info().Msgf("loading template: provider=%s name=%s\n%s", provider, boxTemplate.Value.Data.Name, boxTemplate.Value.Data.Pretty())
log.Info().Msgf("loading template: provider=%s name=%s\n%s", provider, templateName, boxTemplate.Value.Data.Pretty())

boxClient, err := newDefaultBoxClient(provider, configRef, loader)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions internal/command/common/template.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package common

import (
"fmt"
"strings"

"github.com/hckops/hckctl/pkg/template"
)

Expand All @@ -13,3 +16,19 @@ func NewGitSourceOptions(cacheDir string, revision string) *template.GitSourceOp
AllowOffline: true,
}
}

func PrettyPath(cacheDir string, path string) string {
// remove prefix and suffix
return strings.NewReplacer(
fmt.Sprintf("%s/", cacheDir), "",
".yml", "",
".yaml", "",
).Replace(path)
}

func PrettyName[T template.TemplateType](info *template.TemplateInfo[T], cacheDir string, name string) string {
if info.SourceType == template.Git {
return fmt.Sprintf("%s@%s", PrettyPath(cacheDir, info.Path), info.Revision[:7])
}
return fmt.Sprintf("%s@%s", strings.ToLower(name), info.Revision)
}
5 changes: 3 additions & 2 deletions internal/command/lab/lab.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ func startLab(sourceLoader template.SourceLoader[labModel.LabV1], configRef *con
return errors.New("invalid template")
}

templateName := commonCmd.PrettyName(info, configRef.Config.Template.CacheDir, info.Value.Data.Name)
loader := commonCmd.NewLoader()
loader.Start("loading template %s", info.Value.Data.Name)
loader.Start("loading template %s", templateName)
defer loader.Stop()

log.Info().Msgf("loading template: name=%s\n%s", info.Value.Data.Name, info.Value.Data.Pretty())
log.Info().Msgf("loading template: name=%s\n%s", templateName, info.Value.Data.Pretty())

labClient, err := newDefaultLabClient(configRef, loader)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/command/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ func (opts *taskCmdOptions) runTask(sourceLoader template.SourceLoader[taskModel
return errors.New("invalid template")
}

// TODO resolve partial path e.g. "task/scanner/<NAME>"
templateName := strings.ToLower(info.Value.Data.Name)
templateName := commonCmd.PrettyName(info, opts.configRef.Config.Template.CacheDir, info.Value.Data.Name)
loader := commonCmd.NewLoader()
loader.Start("loading template %s", templateName)
defer loader.Stop()
Expand Down
9 changes: 1 addition & 8 deletions internal/command/template/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package template

import (
"fmt"
"strings"

"github.com/MakeNowJust/heredoc"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -80,12 +78,7 @@ func templateList(cacheDir string, revision string, offline bool) error {
for _, validation := range validations {
if validation.IsValid {
total = total + 1
// remove prefix and suffix
prettyPath := strings.NewReplacer(
fmt.Sprintf("%s/", sourceOpts.CachePath()), "",
".yml", "",
".yaml", "",
).Replace(validation.Path)
prettyPath := common.PrettyPath(sourceOpts.CachePath(), validation.Path)

log.Debug().Msgf("found template: kind=%s pretty=%s path=%s", validation.Value.Kind.String(), prettyPath, validation.Path)
fmt.Println(fmt.Sprintf("%s\t%s", validation.Value.Kind.String(), prettyPath))
Expand Down

0 comments on commit 80afd7d

Please sign in to comment.