-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from optiopay/config
Refactor configuration hadling
- Loading branch information
Showing
5 changed files
with
243 additions
and
87 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/optiopay/klar/clair" | ||
"github.com/optiopay/klar/docker" | ||
"github.com/optiopay/klar/utils" | ||
) | ||
|
||
const ( | ||
optionClairOutput = "CLAIR_OUTPUT" | ||
optionClairAddress = "CLAIR_ADDR" | ||
optionKlarTrace = "KLAR_TRACE" | ||
optionClairThreshold = "CLAIR_THRESHOLD" | ||
optionJSONOutput = "JSON_OUTPUT" | ||
optionDockerUser = "DOCKER_USER" | ||
optionDockerPassword = "DOCKER_PASSWORD" | ||
optionDockerInsecure = "DOCKER_INSECURE" | ||
optionRegistryInsecure = "REGISTRY_INSECURE" | ||
) | ||
|
||
var priorities = []string{"Unknown", "Negligible", "Low", "Medium", "High", "Critical", "Defcon1"} | ||
|
||
func parseOutputPriority() (string, error) { | ||
clairOutput := priorities[0] | ||
outputEnv := os.Getenv(optionClairOutput) | ||
if outputEnv != "" { | ||
output := strings.Title(strings.ToLower(outputEnv)) | ||
correct := false | ||
for _, sev := range priorities { | ||
if sev == output { | ||
clairOutput = sev | ||
correct = true | ||
break | ||
} | ||
} | ||
|
||
if !correct { | ||
return "", fmt.Errorf("Clair output level %s is not supported, only support %v\n", outputEnv, priorities) | ||
} | ||
} | ||
return clairOutput, nil | ||
} | ||
|
||
func parseIntOption(key string) int { | ||
val := 0 | ||
valStr := os.Getenv(key) | ||
if valStr != "" { | ||
val, _ = strconv.Atoi(valStr) | ||
} | ||
return val | ||
} | ||
|
||
func parseBoolOption(key string) bool { | ||
val := false | ||
if envVal, err := strconv.ParseBool(os.Getenv(key)); err == nil { | ||
val = envVal | ||
} | ||
return val | ||
} | ||
|
||
type jsonOutput struct { | ||
LayerCount int | ||
Vulnerabilities map[string][]*clair.Vulnerability | ||
} | ||
|
||
type config struct { | ||
ClairAddr string | ||
ClairOutput string | ||
Threshold int | ||
JSONOutput bool | ||
DockerConfig docker.Config | ||
} | ||
|
||
func newConfig(args []string) (*config, error) { | ||
clairAddr := os.Getenv(optionClairAddress) | ||
if clairAddr == "" { | ||
return nil, fmt.Errorf("Clair address must be provided\n") | ||
} | ||
|
||
if os.Getenv(optionKlarTrace) != "" { | ||
utils.Trace = true | ||
} | ||
|
||
clairOutput, err := parseOutputPriority() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &config{ | ||
ClairAddr: clairAddr, | ||
ClairOutput: clairOutput, | ||
Threshold: parseIntOption(optionClairThreshold), | ||
JSONOutput: parseBoolOption(optionJSONOutput), | ||
DockerConfig: docker.Config{ | ||
ImageName: args[1], | ||
User: os.Getenv(optionDockerUser), | ||
Password: os.Getenv(optionDockerPassword), | ||
InsecureTLS: parseBoolOption(optionDockerInsecure), | ||
InsecureRegistry: parseBoolOption(optionRegistryInsecure), | ||
}, | ||
}, nil | ||
} |
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,98 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestParseIntOption(t *testing.T) { | ||
cases := []struct { | ||
value string | ||
expected int | ||
}{ | ||
{ | ||
value: "", | ||
expected: 0, | ||
}, | ||
{ | ||
value: "0", | ||
expected: 0, | ||
}, | ||
{ | ||
value: "10", | ||
expected: 10, | ||
}, | ||
{ | ||
value: "x", | ||
expected: 0, | ||
}, | ||
} | ||
for _, tc := range cases { | ||
os.Setenv(optionClairThreshold, tc.value) | ||
if got := parseIntOption(optionClairThreshold); got != tc.expected { | ||
t.Errorf("expected %d got %d", tc.expected, got) | ||
} | ||
} | ||
} | ||
|
||
func TestParseBoolOption(t *testing.T) { | ||
cases := []struct { | ||
value string | ||
expected bool | ||
}{ | ||
{ | ||
value: "", | ||
expected: false, | ||
}, | ||
{ | ||
value: "x", | ||
expected: false, | ||
}, | ||
{ | ||
value: "true", | ||
expected: true, | ||
}, | ||
{ | ||
value: "false", | ||
expected: false, | ||
}, | ||
} | ||
for _, tc := range cases { | ||
os.Setenv(optionDockerInsecure, tc.value) | ||
if got := parseBoolOption(optionDockerInsecure); got != tc.expected { | ||
t.Errorf("%q: expected %v got %v", tc.value, tc.expected, got) | ||
} | ||
} | ||
} | ||
|
||
func TestParseOutputPriority(t *testing.T) { | ||
cases := []struct { | ||
priority string | ||
expected string | ||
shouldFail bool | ||
}{ | ||
{ | ||
priority: priorities[1], | ||
expected: priorities[1], | ||
}, | ||
{ | ||
priority: "", | ||
expected: priorities[0], | ||
}, | ||
{ | ||
priority: "xxx", | ||
shouldFail: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
os.Setenv(optionClairOutput, tc.priority) | ||
p, err := parseOutputPriority() | ||
if (err != nil) != tc.shouldFail { | ||
t.Fatalf("expected error: %v, got: %v", tc.expected, err) | ||
} | ||
if p != tc.expected { | ||
t.Fatalf("expected output priority %s, got %s", tc.expected, p) | ||
} | ||
} | ||
} |
Oops, something went wrong.