-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
aeaconf2
integration
#178
base: master
Are you sure you want to change the base?
aeaconf2
integration
#178
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,8 @@ import ( | |
"reflect" | ||
|
||
"github.com/BurntSushi/toml" | ||
"github.com/safinsingh/aeaconf2" | ||
"github.com/safinsingh/aeaconf2/compat" | ||
) | ||
|
||
// parseConfig takes the config content as a string and attempts to parse it | ||
|
@@ -17,17 +19,26 @@ func parseConfig(configContent string) { | |
fail("Configuration is empty!") | ||
os.Exit(1) | ||
} | ||
md, err := toml.Decode(configContent, &conf) | ||
|
||
headerRaw, checksRaw, err := compat.SeparateConfig([]byte(configContent)) | ||
if err != nil { | ||
fail("Error decoding TOML: " + err.Error()) | ||
fail("error separating config file: " + err.Error()) | ||
os.Exit(1) | ||
} | ||
if verboseEnabled { | ||
for _, undecoded := range md.Undecoded() { | ||
warn("Undecoded scoring configuration key \"" + undecoded.String() + "\" will not be used.") | ||
} | ||
|
||
cfg := new(config) | ||
err = toml.Unmarshal(headerRaw, cfg) | ||
if err != nil { | ||
fail("error parsing config file header: " + err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
ab := aeaconf2.DefaultAeaconfBuilder(checksRaw, funcRegistry). | ||
SetLineOffset(countLines(headerRaw)). | ||
SetMaxPoints(cfg.MaxPoints) | ||
|
||
cfg.Checks = ab.GetChecks() | ||
|
||
// If there's no remote, local must be enabled. | ||
if conf.Remote == "" { | ||
conf.Local = true | ||
|
@@ -62,19 +73,6 @@ func parseConfig(configContent string) { | |
info("Consider updating your config to include:") | ||
info(" version = '" + version + "'") | ||
} | ||
|
||
// Print warnings for impossible checks and undefined check types. | ||
for i, check := range conf.Check { | ||
if len(check.Pass) == 0 && len(check.PassOverride) == 0 { | ||
warn("Check " + fmt.Sprintf("%d", i+1) + " does not define any possible ways to pass!") | ||
} | ||
allConditions := append(append(append([]cond{}, check.Pass[:]...), check.Fail[:]...), check.PassOverride[:]...) | ||
for j, cond := range allConditions { | ||
if cond.Type == "" { | ||
warn("Check " + fmt.Sprintf("%d condition %d", i+1, j+1) + " does not have a check type!") | ||
} | ||
} | ||
} | ||
} | ||
|
||
// writeConfig writes the in-memory config to disk as the an encrypted | ||
|
@@ -157,21 +155,8 @@ func printConfig() { | |
if conf.EndDate != "" { | ||
pass("End Date:", conf.EndDate) | ||
} | ||
for i, check := range conf.Check { | ||
green("CHCK", fmt.Sprintf("Check %d (%d points):", i+1, check.Points)) | ||
fmt.Println("Message:", check.Message) | ||
for _, c := range check.Pass { | ||
fmt.Println("Pass Condition:") | ||
fmt.Print(c) | ||
} | ||
for _, c := range check.PassOverride { | ||
fmt.Println("PassOverride Condition:") | ||
fmt.Print(c) | ||
} | ||
for _, c := range check.Fail { | ||
fmt.Println("Fail Condition:") | ||
fmt.Print(c) | ||
} | ||
for i, check := range conf.Checks { | ||
green("CHCK", fmt.Sprintf("Check %d: %s", i+1, check.Debug())) | ||
} | ||
} | ||
|
||
|
@@ -182,64 +167,72 @@ func obfuscateConfig() { | |
if err := obfuscateData(&conf.Password); err != nil { | ||
fail(err.Error()) | ||
} | ||
for i, check := range conf.Check { | ||
if err := obfuscateData(&conf.Check[i].Message); err != nil { | ||
for i := range conf.Checks { | ||
if err := obfuscateData(&conf.Checks[i].Message); err != nil { | ||
fail(err.Error()) | ||
} | ||
if conf.Check[i].Hint != "" { | ||
if err := obfuscateData(&conf.Check[i].Hint); err != nil { | ||
fail(err.Error()) | ||
} | ||
} | ||
for j := range check.Pass { | ||
if err := obfuscateCond(&conf.Check[i].Pass[j]); err != nil { | ||
if conf.Checks[i].Hint != "" { | ||
if err := obfuscateData(&conf.Checks[i].Hint); err != nil { | ||
fail(err.Error()) | ||
} | ||
} | ||
for j := range check.PassOverride { | ||
if err := obfuscateCond(&conf.Check[i].PassOverride[j]); err != nil { | ||
fail(err.Error()) | ||
} | ||
} | ||
for j := range check.Fail { | ||
if err := obfuscateCond(&conf.Check[i].Fail[j]); err != nil { | ||
fail(err.Error()) | ||
} | ||
if err := obfuscateCond(conf.Checks[i].Condition); err != nil { | ||
fail(err.Error()) | ||
} | ||
} | ||
} | ||
|
||
// obfuscateCond is a convenience function to obfuscate all string fields of a | ||
// struct using reflection. It assumes all struct fields are strings. | ||
func obfuscateCond(c *cond) error { | ||
|
||
// ummmmmm | ||
func obfuscateCond(c aeaconf2.Condition) error { | ||
s := reflect.ValueOf(c).Elem() | ||
t := s.Type() | ||
|
||
for i := 0; i < s.NumField(); i++ { | ||
if s.Type().Field(i).Name == "regex" { | ||
continue | ||
} | ||
datum := s.Field(i).String() | ||
if err := obfuscateData(&datum); err != nil { | ||
return err | ||
field := s.Field(i) | ||
fieldType := t.Field(i).Type | ||
|
||
if fieldType.Kind() == reflect.String { | ||
datum := field.String() | ||
if err := obfuscateData(&datum); err != nil { | ||
return err | ||
} | ||
field.SetString(datum) | ||
} else if fieldType.Implements(reflect.TypeOf((*aeaconf2.Condition)(nil)).Elem()) { | ||
if err := obfuscateCond(field.Addr().Interface().(aeaconf2.Condition)); err != nil { | ||
return err | ||
} | ||
} | ||
s.Field(i).SetString(datum) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// deobfuscateCond is a convenience function to deobfuscate all string fields | ||
// of a struct using reflection. | ||
func deobfuscateCond(c *cond) error { | ||
func deobfuscateCond(c aeaconf2.Condition) error { | ||
s := reflect.ValueOf(c).Elem() | ||
t := s.Type() | ||
|
||
for i := 0; i < s.NumField(); i++ { | ||
if s.Type().Field(i).Name == "regex" { | ||
continue | ||
} | ||
datum := s.Field(i).String() | ||
if err := deobfuscateData(&datum); err != nil { | ||
return err | ||
field := s.Field(i) | ||
fieldType := t.Field(i).Type | ||
|
||
if fieldType.Kind() == reflect.String { | ||
datum := field.String() | ||
if err := deobfuscateData(&datum); err != nil { | ||
return err | ||
} | ||
field.SetString(datum) | ||
} else if fieldType.Implements(reflect.TypeOf((*aeaconf2.Condition)(nil)).Elem()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This may look better as a function on the aeaconf struct. Maybe something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I could probably just do that on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added on |
||
if err := deobfuscateCond(field.Addr().Interface().(aeaconf2.Condition)); err != nil { | ||
return err | ||
} | ||
} | ||
s.Field(i).SetString(datum) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can init this in var with type inference eg
var funcRegistry = make(map[string]reflect.Type)
. Having a map to a reflect type smells really bad though LOL I'll have to understand the code