Skip to content

Commit

Permalink
fix(misconf): get user from Config.User (#6070)
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Feb 7, 2024
1 parent 6ccc0a5 commit 7fec991
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pkg/fanal/analyzer/imgconf/dockerfile/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func (a *historyAnalyzer) Analyze(ctx context.Context, input analyzer.ConfigAnal
return nil, nil
}
dockerfile := new(bytes.Buffer)
var userFound bool
baseLayerIndex := image.GuessBaseImageIndex(input.Config.History)
for i := baseLayerIndex + 1; i < len(input.Config.History); i++ {
h := input.Config.History[i]
Expand All @@ -64,6 +65,7 @@ func (a *historyAnalyzer) Analyze(ctx context.Context, input analyzer.ConfigAnal
case strings.HasPrefix(h.CreatedBy, "USER"):
// USER instruction
createdBy = h.CreatedBy
userFound = true
case strings.HasPrefix(h.CreatedBy, "HEALTHCHECK"):
// HEALTHCHECK instruction
var interval, timeout, startPeriod, retries, command string
Expand All @@ -86,6 +88,11 @@ func (a *historyAnalyzer) Analyze(ctx context.Context, input analyzer.ConfigAnal
dockerfile.WriteString(strings.TrimSpace(createdBy) + "\n")
}

if !userFound && input.Config.Config.User != "" {
user := fmt.Sprintf("USER %s", input.Config.Config.User)
dockerfile.WriteString(user)
}

fsys := mapfs.New()
if err := fsys.WriteVirtualFile("Dockerfile", dockerfile.Bytes(), 0600); err != nil {
return nil, xerrors.Errorf("mapfs write error: %w", err)
Expand Down
5 changes: 1 addition & 4 deletions pkg/fanal/analyzer/imgconf/dockerfile/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func Test_historyAnalyzer_Analyze(t *testing.T) {
Interval: time.Second * 10,
Timeout: time.Second * 3,
},
User: "1002",
},
History: []v1.History{
{
Expand All @@ -126,10 +127,6 @@ func Test_historyAnalyzer_Analyze(t *testing.T) {
CreatedBy: "RUN /bin/sh -c ls -hl /foo # buildkit",
EmptyLayer: false,
},
{
CreatedBy: "USER foo",
EmptyLayer: true,
},
{
CreatedBy: `HEALTHCHECK &{["CMD-SHELL" "curl -sS 127.0.0.1 || exit 1"] "10s" "3s" "0s" '\x00'}`,
EmptyLayer: true,
Expand Down

0 comments on commit 7fec991

Please sign in to comment.