2020-02-06 11:47:45 +02:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2023-07-04 14:19:02 +02:00
|
|
|
"bytes"
|
2021-11-04 11:19:33 +02:00
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2020-02-06 11:47:45 +02:00
|
|
|
"fmt"
|
2022-03-23 11:02:00 +02:00
|
|
|
"os"
|
2022-05-13 10:02:46 +02:00
|
|
|
"path"
|
2021-12-20 18:15:13 +02:00
|
|
|
"path/filepath"
|
2022-03-23 11:02:00 +02:00
|
|
|
"regexp"
|
2020-02-06 11:47:45 +02:00
|
|
|
"strings"
|
|
|
|
|
2023-07-04 14:19:02 +02:00
|
|
|
"github.com/docker/cli/cli/config"
|
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2022-03-23 11:02:00 +02:00
|
|
|
cranecmd "github.com/google/go-containerregistry/cmd/crane/cmd"
|
2022-03-30 13:58:16 +02:00
|
|
|
"github.com/google/go-containerregistry/pkg/authn"
|
2022-03-23 11:02:00 +02:00
|
|
|
"github.com/google/go-containerregistry/pkg/crane"
|
2020-02-06 11:47:45 +02:00
|
|
|
"github.com/google/go-containerregistry/pkg/name"
|
2022-03-30 13:58:16 +02:00
|
|
|
v1 "github.com/google/go-containerregistry/pkg/v1"
|
|
|
|
"github.com/google/go-containerregistry/pkg/v1/remote"
|
2023-11-30 11:06:31 +02:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/SAP/jenkins-library/pkg/log"
|
|
|
|
"github.com/SAP/jenkins-library/pkg/piperutils"
|
2020-02-06 11:47:45 +02:00
|
|
|
)
|
|
|
|
|
2021-11-04 11:19:33 +02:00
|
|
|
// AuthEntry defines base64 encoded username:password required inside a Docker config.json
|
|
|
|
type AuthEntry struct {
|
|
|
|
Auth string `json:"auth,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-07-04 14:19:02 +02:00
|
|
|
// MergeDockerConfigJSON merges two docker config.json files.
|
|
|
|
func MergeDockerConfigJSON(sourcePath, targetPath string, utils piperutils.FileUtils) error {
|
|
|
|
if exists, _ := utils.FileExists(sourcePath); !exists {
|
|
|
|
return fmt.Errorf("source dockerConfigJSON file %q does not exist", sourcePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
sourceReader, err := utils.Open(sourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to open file %q", sourcePath)
|
|
|
|
}
|
|
|
|
defer sourceReader.Close()
|
|
|
|
|
|
|
|
sourceConfig, err := config.LoadFromReader(sourceReader)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to read file %q", sourcePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
var targetConfig *configfile.ConfigFile
|
|
|
|
if exists, _ := utils.FileExists(targetPath); !exists {
|
|
|
|
log.Entry().Warnf("target dockerConfigJSON file %q does not exist, creating a new one", sourcePath)
|
|
|
|
targetConfig = configfile.New(targetPath)
|
|
|
|
} else {
|
|
|
|
targetReader, err := utils.Open(targetPath)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to open file %q", targetReader)
|
|
|
|
}
|
|
|
|
defer targetReader.Close()
|
|
|
|
targetConfig, err = config.LoadFromReader(targetReader)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to read file %q", targetPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for registry, auth := range sourceConfig.GetAuthConfigs() {
|
|
|
|
targetConfig.AuthConfigs[registry] = auth
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
err = targetConfig.SaveToWriter(buf)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to save file %q", targetPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = utils.MkdirAll(filepath.Dir(targetPath), 0777)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create directory path for the file %q: %w", targetPath, err)
|
|
|
|
}
|
|
|
|
err = utils.FileWrite(targetPath, buf.Bytes(), 0666)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write %q: %w", targetPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-04 11:19:33 +02:00
|
|
|
// CreateDockerConfigJSON creates / updates a Docker config.json with registry credentials
|
|
|
|
func CreateDockerConfigJSON(registryURL, username, password, targetPath, configPath string, utils piperutils.FileUtils) (string, error) {
|
|
|
|
|
|
|
|
if len(targetPath) == 0 {
|
|
|
|
targetPath = configPath
|
|
|
|
}
|
|
|
|
|
2023-11-30 11:06:31 +02:00
|
|
|
dockerConfigContent := []byte{}
|
2021-11-04 11:19:33 +02:00
|
|
|
dockerConfig := map[string]interface{}{}
|
2023-11-30 11:06:31 +02:00
|
|
|
if exists, err := utils.FileExists(configPath); exists {
|
|
|
|
dockerConfigContent, err = utils.FileRead(configPath)
|
2021-11-04 11:19:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to read file '%v': %w", configPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(dockerConfigContent, &dockerConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to unmarshal json file '%v': %w", configPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 11:06:31 +02:00
|
|
|
if registryURL == "" || password == "" || username == "" {
|
|
|
|
if err := fileWrite(targetPath, dockerConfigContent, utils); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return targetPath, nil
|
|
|
|
}
|
|
|
|
|
2021-11-04 11:19:33 +02:00
|
|
|
credentialsBase64 := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%v:%v", username, password)))
|
|
|
|
dockerAuth := AuthEntry{Auth: credentialsBase64}
|
|
|
|
|
|
|
|
if dockerConfig["auths"] == nil {
|
|
|
|
dockerConfig["auths"] = map[string]AuthEntry{registryURL: dockerAuth}
|
|
|
|
} else {
|
|
|
|
authEntries, ok := dockerConfig["auths"].(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("failed to read authentication entries from file '%v': format invalid", configPath)
|
|
|
|
}
|
|
|
|
authEntries[registryURL] = dockerAuth
|
|
|
|
dockerConfig["auths"] = authEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonResult, err := json.Marshal(dockerConfig)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to marshal Docker config.json: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-11-30 11:06:31 +02:00
|
|
|
if err := fileWrite(targetPath, jsonResult, utils); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return targetPath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func fileWrite(path string, content []byte, utils piperutils.FileUtils) error {
|
|
|
|
err := utils.MkdirAll(filepath.Dir(path), 0777)
|
2021-12-20 18:15:13 +02:00
|
|
|
if err != nil {
|
2023-11-30 11:06:31 +02:00
|
|
|
return fmt.Errorf("failed to create directory path for the Docker config.json file %v:%w", path, err)
|
2021-12-20 18:15:13 +02:00
|
|
|
}
|
2023-11-30 11:06:31 +02:00
|
|
|
err = utils.FileWrite(path, content, 0666)
|
2021-11-04 11:19:33 +02:00
|
|
|
if err != nil {
|
2023-11-30 11:06:31 +02:00
|
|
|
return fmt.Errorf("failed to write Docker config.json: %w", err)
|
2021-11-04 11:19:33 +02:00
|
|
|
}
|
|
|
|
|
2023-11-30 11:06:31 +02:00
|
|
|
return nil
|
2021-11-04 11:19:33 +02:00
|
|
|
}
|
|
|
|
|
2020-02-06 11:47:45 +02:00
|
|
|
// Client defines an docker client object
|
|
|
|
type Client struct {
|
|
|
|
imageName string
|
|
|
|
registryURL string
|
|
|
|
localPath string
|
|
|
|
includeLayers bool
|
2022-05-13 18:56:41 +02:00
|
|
|
imageFormat string
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientOptions defines the options to be set on the client
|
|
|
|
type ClientOptions struct {
|
2022-03-23 11:02:00 +02:00
|
|
|
ImageName string
|
|
|
|
RegistryURL string
|
|
|
|
LocalPath string
|
2022-05-13 18:56:41 +02:00
|
|
|
ImageFormat string
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 09:47:38 +02:00
|
|
|
// Download interface for download an image to a local path
|
2020-02-06 11:47:45 +02:00
|
|
|
type Download interface {
|
2022-03-23 11:02:00 +02:00
|
|
|
DownloadImage(imageSource, targetFile string) (v1.Image, error)
|
|
|
|
DownloadImageContent(imageSource, targetDir string) (v1.Image, error)
|
2022-03-30 13:58:16 +02:00
|
|
|
GetRemoteImageInfo(string) (v1.Image, error)
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetOptions sets options used for the docker client
|
|
|
|
func (c *Client) SetOptions(options ClientOptions) {
|
|
|
|
c.imageName = options.ImageName
|
|
|
|
c.registryURL = options.RegistryURL
|
|
|
|
c.localPath = options.LocalPath
|
2022-05-13 18:56:41 +02:00
|
|
|
c.imageFormat = options.ImageFormat
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 09:47:38 +02:00
|
|
|
// DownloadImageContent downloads the image content into the given targetDir. Returns with an error if the targetDir doesnt exist
|
2022-03-23 11:02:00 +02:00
|
|
|
func (c *Client) DownloadImageContent(imageSource, targetDir string) (v1.Image, error) {
|
|
|
|
if fileInfo, err := os.Stat(targetDir); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !fileInfo.IsDir() {
|
|
|
|
return nil, fmt.Errorf("specified target is not a directory: %s", targetDir)
|
|
|
|
}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
noOpts := []crane.Option{}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
imageRef, err := c.getImageRef(imageSource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
img, err := crane.Pull(imageRef.Name(), noOpts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
tmpFile, err := os.CreateTemp(".", ".piper-download-")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer os.Remove(tmpFile.Name())
|
2021-08-17 15:52:18 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
args := []string{imageRef.Name(), tmpFile.Name()}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
exportCmd := cranecmd.NewCmdExport(&noOpts)
|
|
|
|
exportCmd.SetArgs(args)
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
if err := exportCmd.Execute(); err != nil {
|
|
|
|
return nil, err
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
return img, piperutils.Untar(tmpFile.Name(), targetDir, 0)
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
// DownloadImage downloads the image and saves it as tar at the given path
|
|
|
|
func (c *Client) DownloadImage(imageSource, targetFile string) (v1.Image, error) {
|
|
|
|
noOpts := []crane.Option{}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
imageRef, err := c.getImageRef(imageSource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-06 11:47:45 +02:00
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
img, err := crane.Pull(imageRef.Name(), noOpts...)
|
2020-02-06 11:47:45 +02:00
|
|
|
if err != nil {
|
2022-03-23 11:02:00 +02:00
|
|
|
return nil, err
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
2022-03-23 11:02:00 +02:00
|
|
|
|
|
|
|
tmpFile, err := os.CreateTemp(".", ".piper-download-")
|
2020-02-06 11:47:45 +02:00
|
|
|
if err != nil {
|
2022-03-23 11:02:00 +02:00
|
|
|
return nil, err
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
2022-03-23 11:02:00 +02:00
|
|
|
|
|
|
|
craneCmd := cranecmd.NewCmdPull(&noOpts)
|
|
|
|
craneCmd.SetOut(log.Writer())
|
|
|
|
craneCmd.SetErr(log.Writer())
|
2024-02-12 09:56:40 +02:00
|
|
|
args := []string{imageRef.Name(), tmpFile.Name(), "--format=" + c.imageFormat}
|
|
|
|
craneCmd.SetArgs(args)
|
2022-03-23 11:02:00 +02:00
|
|
|
|
|
|
|
if err := craneCmd.Execute(); err != nil {
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Rename(tmpFile.Name(), targetFile); err != nil {
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return img, nil
|
|
|
|
}
|
|
|
|
|
2022-03-30 13:58:16 +02:00
|
|
|
// GetRemoteImageInfo retrieves information about the image (e.g. digest) without actually downoading it
|
|
|
|
func (c *Client) GetRemoteImageInfo(imageSource string) (v1.Image, error) {
|
|
|
|
ref, err := c.getImageRef(imageSource)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "parsing image reference")
|
|
|
|
}
|
|
|
|
|
|
|
|
return remote.Image(ref, remote.WithAuthFromKeychain(authn.DefaultKeychain))
|
|
|
|
}
|
|
|
|
|
2022-03-23 11:02:00 +02:00
|
|
|
func (c *Client) getImageRef(image string) (name.Reference, error) {
|
|
|
|
opts := []name.Option{}
|
2022-05-13 10:02:46 +02:00
|
|
|
registry := ""
|
2022-03-23 11:02:00 +02:00
|
|
|
|
|
|
|
if len(c.registryURL) > 0 {
|
|
|
|
re := regexp.MustCompile(`(?i)^https?://`)
|
2022-05-13 10:02:46 +02:00
|
|
|
registry = re.ReplaceAllString(c.registryURL, "")
|
2022-03-23 11:02:00 +02:00
|
|
|
opts = append(opts, name.WithDefaultRegistry(registry))
|
|
|
|
}
|
|
|
|
|
2022-05-13 10:02:46 +02:00
|
|
|
return name.ParseReference(path.Join(registry, image), opts...)
|
2020-02-06 11:47:45 +02:00
|
|
|
}
|
2022-02-07 08:58:41 +02:00
|
|
|
|
|
|
|
// ImageListWithFilePath compiles container image names based on all Dockerfiles found, considering excludes
|
|
|
|
// according to following search pattern: **/Dockerfile*
|
|
|
|
// Return value contains a map with image names and file path
|
|
|
|
// Examples for image names with imageName testImage
|
|
|
|
// * Dockerfile: `imageName`
|
|
|
|
// * sub1/Dockerfile: `imageName-sub1`
|
|
|
|
// * sub2/Dockerfile_proxy: `imageName-sub2-proxy`
|
2022-03-11 10:47:44 +02:00
|
|
|
func ImageListWithFilePath(imageName string, excludes []string, trimDir string, utils piperutils.FileUtils) (map[string]string, error) {
|
2022-02-07 08:58:41 +02:00
|
|
|
|
|
|
|
imageList := map[string]string{}
|
|
|
|
|
|
|
|
pattern := "**/Dockerfile*"
|
|
|
|
|
|
|
|
matches, err := utils.Glob(pattern)
|
|
|
|
if err != nil || len(matches) == 0 {
|
|
|
|
return imageList, fmt.Errorf("failed to retrieve Dockerfiles")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, dockerfilePath := range matches {
|
|
|
|
// make sure that the path we have is relative
|
|
|
|
// ToDo: needs rework
|
2023-11-30 11:06:31 +02:00
|
|
|
// dockerfilePath = strings.ReplaceAll(dockerfilePath, cwd, ".")
|
2022-02-07 08:58:41 +02:00
|
|
|
|
|
|
|
if piperutils.ContainsString(excludes, dockerfilePath) {
|
|
|
|
log.Entry().Infof("Discard %v since it is in the exclude list %v", dockerfilePath, excludes)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if dockerfilePath == "Dockerfile" {
|
|
|
|
imageList[imageName] = dockerfilePath
|
|
|
|
} else {
|
|
|
|
var finalName string
|
|
|
|
if base := filepath.Base(dockerfilePath); base == "Dockerfile" {
|
2022-03-11 10:47:44 +02:00
|
|
|
subName := strings.ReplaceAll(filepath.Dir(dockerfilePath), string(filepath.Separator), "-")
|
|
|
|
if len(trimDir) > 0 {
|
|
|
|
// allow to remove trailing sub directories
|
|
|
|
// example .ci/app/Dockerfile
|
|
|
|
// with trimDir = .ci/ imagename would only contain app part.
|
|
|
|
subName = strings.TrimPrefix(subName, strings.ReplaceAll(trimDir, "/", "-"))
|
|
|
|
// make sure that subName does not start with a - (e.g. due not configuring trailing slash for trimDir)
|
|
|
|
subName = strings.TrimPrefix(subName, "-")
|
|
|
|
}
|
|
|
|
finalName = fmt.Sprintf("%v-%v", imageName, subName)
|
2022-02-07 08:58:41 +02:00
|
|
|
} else {
|
|
|
|
parts := strings.FieldsFunc(base, func(separator rune) bool {
|
|
|
|
return separator == []rune("-")[0] || separator == []rune("_")[0]
|
|
|
|
})
|
|
|
|
if len(parts) == 1 {
|
|
|
|
return imageList, fmt.Errorf("wrong format of Dockerfile, must be inside a sub-folder or contain a separator")
|
|
|
|
}
|
|
|
|
parts[0] = imageName
|
|
|
|
finalName = strings.Join(parts, "-")
|
|
|
|
}
|
|
|
|
|
|
|
|
imageList[finalName] = dockerfilePath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageList, nil
|
|
|
|
}
|