-
Notifications
You must be signed in to change notification settings - Fork 6
/
backend.go
86 lines (75 loc) · 2.59 KB
/
backend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
Copyright 2021 Adevinta
*/
package backend
import (
"context"
"errors"
"strings"
"github.com/distribution/reference"
)
// Constants defining environment variables that a check expects.
const (
CheckIDVar = "VULCAN_CHECK_ID"
ChecktypeNameVar = "VULCAN_CHECKTYPE_NAME"
ChecktypeVersionVar = "VULCAN_CHECKTYPE_VERSION"
CheckTargetVar = "VULCAN_CHECK_TARGET"
CheckAssetTypeVar = "VULCAN_CHECK_ASSET_TYPE"
CheckOptionsVar = "VULCAN_CHECK_OPTIONS"
CheckLogLevelVar = "VULCAN_CHECK_LOG_LVL"
AgentAddressVar = "VULCAN_AGENT_ADDRESS"
)
// ErrNonZeroExitCode is returned by the docker backend when a container
// finished with an exit code different from 0.
var ErrNonZeroExitCode = errors.New("container finished unexpectedly")
// RunResult defines the info that must be returned when a check is
// finished.
type RunResult struct {
Output []byte
Error error
}
type RunParams struct {
CheckID string
CheckTypeName string
ChecktypeVersion string
Image string
Target string
AssetType string
Options string
RequiredVars []string
Metadata map[string]string
}
// CheckVars contains the static checks vars that some checks needs to be
// injected in their docker to run.
type CheckVars = map[string]string
// APIConfig defines address where a component of the agent will be listening to
// the http requests sent by the checks running.
type APIConfig struct {
Port string `json:"port"` // Port where the api for for the check should listen on
IName string `json:"iname" toml:"iname"` // Interface name that defines the ip a check should use to reach the agent api.
Host string `json:"host" toml:"host"` // Hostname a check should use to reach the agent. Overrides the IName config param.
}
// Backend defines the shape of the backend that executes checks.
type Backend interface {
Run(ctx context.Context, params RunParams) (<-chan RunResult, error)
}
// ParseImage validates and enrich the image with domain (docker.io if domain missing), tag (latest if missing),.
func ParseImage(image string) (domain, path, tag string, err error) {
named, err := reference.ParseNormalizedNamed(image)
if err != nil {
return
}
// add latests if tag is missing
named = reference.TagNameOnly(named)
domain = reference.Domain(named)
path = reference.Path(named)
tagged, isTagged := named.(reference.Tagged)
if domain == "docker.io" {
// Ignore docker.io and "library/" as we don't expect a check in docker.io/library.
path = strings.TrimPrefix(path, "library/")
}
if isTagged {
tag = tagged.Tag()
}
return
}