File: vm.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (62 lines) | stat: -rw-r--r-- 1,624 bytes parent folder | download
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
package vm

import (
	"errors"
	"fmt"
	"regexp"

	"gitlab.com/gitlab-org/gitlab-runner/executors"
)

type Executor struct {
	executors.AbstractExecutor

	allowedImages []*regexp.Regexp
}

func (e *Executor) GetBaseName(defaultBaseName string) (string, error) {
	imageName := e.Build.GetAllVariables().ExpandValue(e.Build.Image.Name)

	// Use default name if no build image specified or name is identical to a default one.
	if imageName == "" || imageName == defaultBaseName {
		return defaultBaseName, nil
	}

	if len(e.allowedImages) == 0 {
		// Ignore YAML's image if no allowed_images parameter is provided for the sake of backward compatibility.
		// And warn user about this.
		e.Warningln(fmt.Sprintf(
			"No allowed_images configuration found for \"%s\", using image \"%s\"",
			e.Build.Image.Name,
			defaultBaseName,
		))
		return defaultBaseName, nil
	}

	for _, allowedImage := range e.allowedImages {
		if allowedImage.MatchString(imageName) {
			return imageName, nil
		}
	}

	e.Println()
	e.Errorln(fmt.Sprintf("The %q image is not present on list of allowed images", imageName))
	for _, allowedImage := range e.allowedImages {
		e.Println("-", allowedImage)
	}
	e.Println()
	e.Println("Please check runner's configuration")

	return "", errors.New("invalid image")
}

func (e *Executor) ValidateAllowedImages(allowedImages []string) error {
	for _, allowedImage := range allowedImages {
		re, err := regexp.Compile(allowedImage)
		if err != nil {
			return fmt.Errorf("invalid regexp pattern in allowed_images parameter: %s", allowedImage)
		}
		e.allowedImages = append(e.allowedImages, re)
	}
	return nil
}