Skip to content

Commit

Permalink
Display secrets list
Browse files Browse the repository at this point in the history
  • Loading branch information
clarsonneur committed Jun 30, 2018
1 parent 1a2226d commit cb4e750
Show file tree
Hide file tree
Showing 10 changed files with 413 additions and 5 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
"secrets",
"--infra-path",
"/home/larsonsh/src/forj/src/forj-oss-infra/",
"--show",
]
},
{
Expand Down
13 changes: 13 additions & 0 deletions forjfile/users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package forjfile

type UsersStruct map[string]*UserStruct

func (u UsersStruct) mergeFrom(source string, from UsersStruct) {
for k, userFrom := range from {
if user, found := u[k]; found {
user.mergeFrom(source, userFrom)
} else {
u[k] = userFrom
}
}
}
10 changes: 7 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package: forjj
import:
- package: github.com/alecthomas/kingpin
repo: https://github.com/clarsonneur/kingpin
version: get-commands-from-context
repo: https://github.com/clarsonneur/kingpin
- package: github.com/kvz/logstreamer
- package: github.com/forj-oss/goforjj
- package: github.com/forj-oss/forjj-modules
- package: golang.org/x/net
subpackages:
- proxy
- package: gopkg.in/yaml.v2
- package: golang.org/x/crypto
subpackages:
- ssh/terminal
82 changes: 81 additions & 1 deletion secrets.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package main

import (
"fmt"
"forjj/scandrivers"
"forjj/utils"
"os"
"sort"
"strings"

"github.com/forj-oss/forjj-modules/trace"

"github.com/alecthomas/kingpin"
"github.com/forj-oss/forjj-modules/cli"
"github.com/forj-oss/forjj-modules/cli/interface"
"github.com/forj-oss/forjj-modules/cli/kingpinCli"
"github.com/forj-oss/goforjj"
"golang.org/x/crypto/ssh/terminal"
)

type secrets struct {
Expand Down Expand Up @@ -160,7 +167,7 @@ func (s *secrets) showList() {
info.keyPath = objectName + "/" + instanceName + "/"
keyName := name
if flagPrefix != "" {
keyName = flagPrefix + "-" + name
keyName = flagPrefix + name
}
info.keyPath += keyName

Expand All @@ -172,4 +179,77 @@ func (s *secrets) showList() {
})

scan.DoScanDriversObject()

gotrace.Trace("secrets elements found: %d", len(s.list.elements))

stdin := int(os.Stdin.Fd())
var terminalMax int
if terminal.IsTerminal(stdin) {
terminalMax, _, _ = terminal.GetSize(stdin)
}
if terminalMax < 80 {
terminalMax = 80
}

sortedList := make([]string, len(s.list.elements))
max := utils.NewEvalValues(5)
max.Eval(0, len("path"))
max.Eval(1, len("Source"))
max.Eval(2, len("secret"))
for secretPath, secretValue := range s.list.elements {
sortedList[max.CountOf(0)-1] = secretPath
max.Eval(0, len(secretPath))
max.Eval(1, len(secretValue.source))

if *s.list.show {
max.Eval(2, len(strings.Replace(secretValue.value, "\n", "", -1)))
}
if secretValue.found {
max.Eval(4, 1)
}
}

colSize := 3
if max.ValueOf(0)+max.ValueOf(1)+max.ValueOf(2)+colSize*2 > terminalMax {
max.Eval(3, terminalMax-(max.ValueOf(0)+max.ValueOf(1)+colSize*2))
max.Eval(3, utils.StringCompressMin)
} else {
max.Eval(3, max.ValueOf(2))
}

printFormat := max.PrintfFormat("%%-%ds | %%-%ds | %%-%ds\n", 0, 1, 3)
tableFormat := max.PrintfFormat("%%%ds-+-%%%ds-+-%%%ds\n", 0, 1, 3)

sort.Strings(sortedList)

fmt.Print("List of secrets in forjj:\n\n")

fmt.Printf(printFormat, "Path", "Source", "Secret")
fmt.Printf(tableFormat,
strings.Repeat("-", max.ValueOf(0)),
strings.Repeat("-", max.ValueOf(1)),
strings.Repeat("-", max.ValueOf(3)),
)
for _, secretPath := range sortedList {
value := ""
secretValue := s.list.elements[secretPath]
if secretValue.found {
if *s.list.show {
value = utils.StringCompress(strings.Replace(secretValue.value, "\n", "", -1), 0, max.ValueOf(3))
} else {
value = "***"
}

}

fmt.Printf(printFormat, secretPath, secretValue.source, value)
}
fmt.Printf(tableFormat,
strings.Repeat("-", max.ValueOf(0)),
strings.Repeat("-", max.ValueOf(1)),
strings.Repeat("-", max.ValueOf(3)),
)

gotrace.Info("%d/%d secrets found", max.CountOf(4), len(s.list.elements))

}
9 changes: 9 additions & 0 deletions secrets_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

type secretInfo struct {
keyPath string
value string
source string
found bool
}

36 changes: 36 additions & 0 deletions sources_info/sources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package sourcesinfo

type Sources struct {
keys map[string]string
}

func newSources() (ret *Sources) {
ret = new(Sources)
ret.keys = make(map[string]string)
return
}

func (s *Sources)Set(source, key, value string) (ret *Sources) {
if s == nil {
ret = newSources()
} else {
ret = s
}

if value == "" {
delete(ret.keys, key)
} else {
ret.keys[key] = source
}
return
}

func (s *Sources)Get(key string) (source string) {
if s == nil {
return
}
if v, found := s.keys[key] ; found {
return v
}
return
}
117 changes: 117 additions & 0 deletions sources_info/sources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package sourcesinfo

import (
"testing"
)

func TestSet(t *testing.T) {
t.Log("Expect set to initialize the sources struct.")

var sources *Sources

const (
src1 = "src1"
src2 = "src2"
key1 = "key1"
key2 = "key2"
value1 = "value1"
value2 = ""
)
// ------------ Run function to test
// sources is nil
sources = sources.Set(src1, key1, value1)

// ------------ Test result
if sources == nil {
t.Error("Expect set to return a new object")
} else if v1, f1 := sources.keys[key1]; !f1 {
t.Error("Expect set to set a source key. Not found")
} else if v1 != src1 {
t.Errorf("Expect set to set a source value = '%s'. got '%s'", src1, v1)
}

// ------------ update context
// Change source information
sources = sources.Set(src2, key1, value1)
// ------------ Test result
if sources == nil {
t.Error("Expect set to return a new object")
} else if v1, f1 := sources.keys[key1]; !f1 {
t.Error("Expect set to set a source key. Not found")
} else if v1 != src2 {
t.Errorf("Expect set to set a source value = '%s'. got '%s'", src2, v1)
}

// ------------ update context
// add a new key, with a different source
sources = sources.Set(src1, key2, value1)
// ------------ Test result
if sources == nil {
t.Error("Expect set to return a new object")
} else if v1, f1 := sources.keys[key1]; !f1 {
t.Errorf("Expect set to set a source key '%s'. Not found", key1)
} else if v1 != src2 {
t.Errorf("Expect set to set a source value = '%s'. got '%s'", src2, v1)
} else if v2, f2 := sources.keys[key2]; !f2 {
t.Errorf("Expect set to set a source key '%s'. Not found", key2)
} else if v2 != src1 {
t.Errorf("Expect set to set a source value = '%s'. got '%s'", src2, v1)
}
// ------------ update context
// add a new key wth value empty
sources = sources.Set(src1, key2, value2)
// ------------ Test result
if sources == nil {
t.Error("Expect set to return a new object")
} else if v1, f1 := sources.keys[key1]; !f1 {
t.Errorf("Expect set to set a source key '%s'. Not found", key1)
} else if v1 != src2 {
t.Errorf("Expect set to set a source value = '%s'. got '%s'", src2, v1)
} else if _, f2 := sources.keys[key2]; f2 {
t.Errorf("Expect set to unset a source key '%s'. found it", key2)
}
}

func TestGet(t *testing.T) {
t.Log("Expect get to retrun the source information.")

var sources *Sources

const (
src1 = "src1"
src2 = "src2"
key1 = "key1"
key2 = "key2"
key3 = "key3"
value1 = "value1"
value2 = ""
)
// ------------ Run function to test
// sources is nil
ret := sources.Get(key1)

// ------------ Test result

if ret != "" {
t.Errorf("Expect get to return an empty string. Got '%s'", ret)
}

// ------------ update context
sources = sources.Set(src1, key1, value1)
sources = sources.Set(src2, key2, value1)

// ------------ Run function to test
// check source information returned
ret = sources.Get(key1)
ret2 := sources.Get(key2)
ret3 := sources.Get(key3)

// ------------ Test result
if ret != src1 {
t.Errorf("Expect get to return '%s'. Got '%s'", src1, ret)
} else if ret2 != src2 {
t.Errorf("Expect get to return '%s'. Got '%s'", src1, ret2)
} else if ret3 == src2 {
t.Errorf("Expect get to return ''. Got '%s'", ret3)
}
}
Loading

0 comments on commit cb4e750

Please sign in to comment.