-
Notifications
You must be signed in to change notification settings - Fork 65
/
runs_as_nonroot.go
74 lines (59 loc) · 2.38 KB
/
runs_as_nonroot.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
package container
import (
"context"
"fmt"
"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/check"
"github.com/redhat-openshift-ecosystem/openshift-preflight/internal/image"
"github.com/go-logr/logr"
cranev1 "github.com/google/go-containerregistry/pkg/v1"
)
var _ check.Check = &RunAsNonRootCheck{}
// RunAsNonRootCheck evaluates the image to determine that the runtime UID is not 0,
// which correlates to the root user.
type RunAsNonRootCheck struct{}
func (p *RunAsNonRootCheck) Validate(ctx context.Context, imgRef image.ImageReference) (bool, error) {
user, err := p.getDataToValidate(imgRef.ImageInfo)
if err != nil {
return false, fmt.Errorf("could not get validation data: %v", err)
}
return p.validate(ctx, user)
}
func (p *RunAsNonRootCheck) getDataToValidate(image cranev1.Image) (string, error) {
configFile, err := image.ConfigFile()
if err != nil {
return "", fmt.Errorf("could not retrieve ConfigFile from Image: %w", err)
}
return configFile.Config.User, nil
}
func (p *RunAsNonRootCheck) validate(ctx context.Context, user string) (bool, error) {
logger := logr.FromContextOrDiscard(ctx)
if user == "" {
logger.Info("detected empty USER. Presumed to be running as root")
logger.Info("USER value must be provided and be a non-root value for this check to pass")
return false, nil
}
if user == "0" || user == "root" {
logger.Info("detected USER specified as root or UID 0")
logger.Info("USER other than root is required for this check to pass")
return false, nil
}
logger.Info(fmt.Sprintf("USER %s specified that is non-root", user))
return true, nil
}
func (p *RunAsNonRootCheck) Name() string {
return "RunAsNonRoot"
}
func (p *RunAsNonRootCheck) Metadata() check.Metadata {
return check.Metadata{
Description: "Checking if container runs as the root user because a container that does not specify a non-root user will fail the automatic certification, and will be subject to a manual review before the container can be approved for publication",
Level: "best",
KnowledgeBaseURL: certDocumentationURL,
CheckURL: certDocumentationURL,
}
}
func (p *RunAsNonRootCheck) Help() check.HelpText {
return check.HelpText{
Message: "Check RunAsNonRoot encountered an error. Please review the preflight.log file for more information.",
Suggestion: "Indicate a specific USER in the dockerfile or containerfile",
}
}