-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package authz0 | ||
|
||
const DefaultFile = "authz0.yaml" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package new | ||
|
||
import ( | ||
"io/ioutil" | ||
"strconv" | ||
|
||
models "github.com/hahwul/authz0/pkg/models" | ||
utils "github.com/hahwul/authz0/pkg/utils" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type NewArguments struct { | ||
Filename string | ||
Name string | ||
IncludeURLs string | ||
IncludeRoles string | ||
AssertSuccessStatus string | ||
AssertFailStatus string | ||
AssertFailRegex string | ||
AssertFailSize int | ||
} | ||
|
||
func Generate(options NewArguments) { | ||
var template models.Template | ||
template.Name = options.Name | ||
|
||
if options.AssertSuccessStatus != "" { | ||
assert := setAssert("success-status", options.AssertSuccessStatus) | ||
template.Asserts = append(template.Asserts, assert) | ||
} | ||
if options.AssertFailStatus != "" { | ||
assert := setAssert("fail-status", options.AssertFailStatus) | ||
template.Asserts = append(template.Asserts, assert) | ||
} | ||
if options.AssertFailRegex != "" { | ||
assert := setAssert("fail-regex", options.AssertFailRegex) | ||
template.Asserts = append(template.Asserts, assert) | ||
} | ||
if options.AssertFailSize != -1 { | ||
assert := setAssert("fail-size", strconv.Itoa(options.AssertFailSize)) | ||
template.Asserts = append(template.Asserts, assert) | ||
} | ||
|
||
if options.IncludeURLs != "" { | ||
urls, err := utils.ReadLinesOrLiteral(options.IncludeURLs) | ||
if err != nil { | ||
|
||
} | ||
for _, line := range urls { | ||
url := models.URL{ | ||
URL: line, | ||
} | ||
template.URLs = append(template.URLs, url) | ||
} | ||
} | ||
if options.IncludeRoles != "" { | ||
roles, err := utils.ReadLinesOrLiteral(options.IncludeRoles) | ||
if err != nil { | ||
|
||
} | ||
for _, line := range roles { | ||
role := models.Role{ | ||
Name: line, | ||
} | ||
template.Roles = append(template.Roles, role) | ||
} | ||
} | ||
yamlData, err := yaml.Marshal(&template) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = ioutil.WriteFile(options.Filename, yamlData, 0644) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func setAssert(t, v string) models.Assert { | ||
assert := models.Assert{ | ||
Type: t, | ||
Value: v, | ||
} | ||
return assert | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package utils | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
) | ||
|
||
// a slice of strings, returning the slice and any error | ||
func readLines(filename string) ([]string, error) { | ||
f, err := os.Open(filename) | ||
if err != nil { | ||
return []string{}, err | ||
} | ||
defer f.Close() | ||
|
||
lines := make([]string, 0) | ||
sc := bufio.NewScanner(f) | ||
for sc.Scan() { | ||
lines = append(lines, sc.Text()) | ||
} | ||
|
||
return lines, sc.Err() | ||
} | ||
|
||
// ReadLinesOrLiteral tries to read lines from a file, returning | ||
// the arg in a string slice if the file doesn't exist, unless | ||
// the arg matches its default value | ||
func ReadLinesOrLiteral(arg string) ([]string, error) { | ||
if isFile(arg) { | ||
return readLines(arg) | ||
} | ||
|
||
// if the argument isn't a file, but it is the default, don't | ||
// treat it as a literal value | ||
|
||
return []string{arg}, nil | ||
} | ||
|
||
// isFile returns true if its argument is a regular file | ||
func isFile(path string) bool { | ||
f, err := os.Stat(path) | ||
return err == nil && f.Mode().IsRegular() | ||
} | ||
|
||
// unique is .. | ||
func unique(intSlice []string) []string { | ||
keys := make(map[string]bool) | ||
list := []string{} | ||
for _, entry := range intSlice { | ||
if _, value := keys[entry]; !value { | ||
keys[entry] = true | ||
list = append(list, entry) | ||
} | ||
} | ||
return list | ||
} |