forked from simon3z/image-inspector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
84 lines (78 loc) · 2.51 KB
/
Copy pathtypes.go
File metadata and controls
84 lines (78 loc) · 2.51 KB
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
package cmd
import (
"fmt"
server "github.com/simon3z/image-inspector/pkg/imageserver"
)
var (
ServerAuthOptions = []string{
string(server.AllowAll),
string(server.KubernetesToken),
}
)
// ImageInspectorOptions is the main inspector implementation and holds the configuration
// for an image inspector.
type ImageInspectorOptions struct {
// URI contains the location of the docker daemon socket to connect to.
URI string
// Image contains the docker image to inspect.
Image string
// DstPath is the destination path for image files.
DstPath string
// Serve holds the host and port for where to serve the image with webdav.
Serve string
// Chroot controls whether or not a chroot is excuted when serving the image with webdav.
Chroot bool
// DockerCfg is the location of the docker config file.
DockerCfg string
// Username is the username for authenticating to the docker registry.
Username string
// PasswordFile is the location of the file containing the password for authentication to the
// docker registry.
PasswordFile string
// ServerAuthType is the type of authentication used to access the server
ServerAuthType string
}
// NewDefaultImageInspectorOptions provides a new ImageInspectorOptions with default values.
func NewDefaultImageInspectorOptions() *ImageInspectorOptions {
return &ImageInspectorOptions{
URI: "unix:///var/run/docker.sock",
Image: "",
DstPath: "",
Serve: "",
Chroot: false,
DockerCfg: "",
Username: "",
PasswordFile: "",
ServerAuthType: "None",
}
}
// Validate performs validation on the field settings.
func (i *ImageInspectorOptions) Validate() error {
if len(i.URI) == 0 {
return fmt.Errorf("Docker socket connection must be specified")
}
if len(i.Image) == 0 {
return fmt.Errorf("Docker image to inspect must be specified")
}
if len(i.DockerCfg) > 0 && len(i.Username) > 0 {
return fmt.Errorf("Only specify dockercfg file or username/password pair for authentication")
}
if len(i.Username) > 0 && len(i.PasswordFile) == 0 {
return fmt.Errorf("Please specify password for the username")
}
if len(i.Serve) == 0 && i.Chroot {
return fmt.Errorf("Change root can be used only when serving the image through webdav")
}
if !stringInSlice(i.ServerAuthType, ServerAuthOptions) {
return fmt.Errorf("server-auth-type can only be one of %v", ServerAuthOptions)
}
return nil
}
func stringInSlice(str string, list []string) bool {
for _, t := range list {
if t == str {
return true
}
}
return false
}