2017-05-27 06:12:13 +00:00
|
|
|
package source
|
2017-05-27 05:10:26 +00:00
|
|
|
|
|
|
|
import (
|
2017-12-14 00:33:56 +00:00
|
|
|
"encoding/json"
|
|
|
|
"strconv"
|
2017-05-27 05:10:26 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/reference"
|
2018-07-26 20:51:28 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
2017-12-14 00:33:56 +00:00
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2017-11-11 02:10:10 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2018-06-24 05:52:19 +00:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2017-05-27 05:10:26 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errInvalid = errors.New("invalid")
|
|
|
|
errNotFound = errors.New("not found")
|
|
|
|
)
|
|
|
|
|
2018-07-25 01:18:53 +00:00
|
|
|
type ResolveMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ResolveModeDefault ResolveMode = iota
|
|
|
|
ResolveModeForcePull
|
|
|
|
ResolveModePreferLocal
|
|
|
|
)
|
|
|
|
|
2017-05-27 05:10:26 +00:00
|
|
|
const (
|
|
|
|
DockerImageScheme = "docker-image"
|
2017-07-07 21:35:10 +00:00
|
|
|
GitScheme = "git"
|
2017-07-11 17:12:12 +00:00
|
|
|
LocalScheme = "local"
|
2020-07-18 16:11:39 +00:00
|
|
|
HTTPScheme = "http"
|
|
|
|
HTTPSScheme = "https"
|
2017-05-27 05:10:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Identifier interface {
|
|
|
|
ID() string // until sources are in process this string comparison could be avoided
|
|
|
|
}
|
|
|
|
|
|
|
|
func FromString(s string) (Identifier, error) {
|
|
|
|
// TODO: improve this
|
|
|
|
parts := strings.SplitN(s, "://", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, errors.Wrapf(errInvalid, "failed to parse %s", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch parts[0] {
|
|
|
|
case DockerImageScheme:
|
|
|
|
return NewImageIdentifier(parts[1])
|
2017-07-07 21:35:10 +00:00
|
|
|
case GitScheme:
|
|
|
|
return NewGitIdentifier(parts[1])
|
2017-07-11 17:12:12 +00:00
|
|
|
case LocalScheme:
|
|
|
|
return NewLocalIdentifier(parts[1])
|
2020-07-18 16:11:39 +00:00
|
|
|
case HTTPSScheme:
|
|
|
|
return NewHTTPIdentifier(parts[1], true)
|
|
|
|
case HTTPScheme:
|
|
|
|
return NewHTTPIdentifier(parts[1], false)
|
2017-05-27 05:10:26 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Wrapf(errNotFound, "unknown schema %s", parts[0])
|
|
|
|
}
|
|
|
|
}
|
2018-07-25 06:18:21 +00:00
|
|
|
|
2018-06-24 05:52:19 +00:00
|
|
|
func FromLLB(op *pb.Op_Source, platform *pb.Platform) (Identifier, error) {
|
2017-12-14 00:33:56 +00:00
|
|
|
id, err := FromString(op.Source.Identifier)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-25 01:18:53 +00:00
|
|
|
|
|
|
|
if id, ok := id.(*ImageIdentifier); ok {
|
|
|
|
if platform != nil {
|
|
|
|
id.Platform = &specs.Platform{
|
|
|
|
OS: platform.OS,
|
|
|
|
Architecture: platform.Architecture,
|
|
|
|
Variant: platform.Variant,
|
|
|
|
OSVersion: platform.OSVersion,
|
|
|
|
OSFeatures: platform.OSFeatures,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range op.Source.Attrs {
|
|
|
|
switch k {
|
|
|
|
case pb.AttrImageResolveMode:
|
2018-07-25 06:18:21 +00:00
|
|
|
rm, err := ParseImageResolveMode(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-07-25 01:18:53 +00:00
|
|
|
}
|
2018-07-25 06:18:21 +00:00
|
|
|
id.ResolveMode = rm
|
2018-07-26 20:51:28 +00:00
|
|
|
case pb.AttrImageRecordType:
|
|
|
|
rt, err := parseImageRecordType(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id.RecordType = rt
|
2018-07-25 01:18:53 +00:00
|
|
|
}
|
2018-06-24 05:52:19 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-14 00:33:56 +00:00
|
|
|
if id, ok := id.(*GitIdentifier); ok {
|
|
|
|
for k, v := range op.Source.Attrs {
|
|
|
|
switch k {
|
|
|
|
case pb.AttrKeepGitDir:
|
|
|
|
if v == "true" {
|
|
|
|
id.KeepGitDir = true
|
|
|
|
}
|
2018-05-29 04:15:05 +00:00
|
|
|
case pb.AttrFullRemoteURL:
|
2020-12-10 02:40:34 +00:00
|
|
|
if !isGitTransport(v) {
|
|
|
|
v = "https://" + v
|
|
|
|
}
|
2018-05-29 04:15:05 +00:00
|
|
|
id.Remote = v
|
2020-06-16 23:45:17 +00:00
|
|
|
case pb.AttrAuthHeaderSecret:
|
|
|
|
id.AuthHeaderSecret = v
|
|
|
|
case pb.AttrAuthTokenSecret:
|
|
|
|
id.AuthTokenSecret = v
|
2020-11-10 20:42:33 +00:00
|
|
|
case pb.AttrKnownSSHHosts:
|
|
|
|
id.KnownSSHHosts = v
|
|
|
|
case pb.AttrMountSSHSock:
|
2020-11-13 01:29:49 +00:00
|
|
|
id.MountSSHSock = v
|
2017-12-14 00:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if id, ok := id.(*LocalIdentifier); ok {
|
|
|
|
for k, v := range op.Source.Attrs {
|
|
|
|
switch k {
|
|
|
|
case pb.AttrLocalSessionID:
|
|
|
|
id.SessionID = v
|
|
|
|
if p := strings.SplitN(v, ":", 2); len(p) == 2 {
|
|
|
|
id.Name = p[0] + "-" + id.Name
|
|
|
|
id.SessionID = p[1]
|
|
|
|
}
|
|
|
|
case pb.AttrIncludePatterns:
|
|
|
|
var patterns []string
|
|
|
|
if err := json.Unmarshal([]byte(v), &patterns); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id.IncludePatterns = patterns
|
2017-12-14 02:49:14 +00:00
|
|
|
case pb.AttrExcludePatterns:
|
|
|
|
var patterns []string
|
|
|
|
if err := json.Unmarshal([]byte(v), &patterns); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id.ExcludePatterns = patterns
|
2018-06-04 21:08:29 +00:00
|
|
|
case pb.AttrFollowPaths:
|
|
|
|
var paths []string
|
|
|
|
if err := json.Unmarshal([]byte(v), &paths); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id.FollowPaths = paths
|
2017-12-14 05:01:01 +00:00
|
|
|
case pb.AttrSharedKeyHint:
|
|
|
|
id.SharedKeyHint = v
|
2017-12-14 00:33:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 16:11:39 +00:00
|
|
|
if id, ok := id.(*HTTPIdentifier); ok {
|
2017-12-14 00:33:56 +00:00
|
|
|
for k, v := range op.Source.Attrs {
|
|
|
|
switch k {
|
|
|
|
case pb.AttrHTTPChecksum:
|
|
|
|
dgst, err := digest.Parse(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id.Checksum = dgst
|
|
|
|
case pb.AttrHTTPFilename:
|
|
|
|
id.Filename = v
|
|
|
|
case pb.AttrHTTPPerm:
|
|
|
|
i, err := strconv.ParseInt(v, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id.Perm = int(i)
|
|
|
|
case pb.AttrHTTPUID:
|
|
|
|
i, err := strconv.ParseInt(v, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id.UID = int(i)
|
|
|
|
case pb.AttrHTTPGID:
|
|
|
|
i, err := strconv.ParseInt(v, 0, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
id.GID = int(i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id, nil
|
|
|
|
}
|
2017-05-27 05:10:26 +00:00
|
|
|
|
|
|
|
type ImageIdentifier struct {
|
2018-07-25 01:18:53 +00:00
|
|
|
Reference reference.Spec
|
|
|
|
Platform *specs.Platform
|
|
|
|
ResolveMode ResolveMode
|
2018-07-26 20:51:28 +00:00
|
|
|
RecordType client.UsageRecordType
|
2017-05-27 05:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewImageIdentifier(str string) (*ImageIdentifier, error) {
|
|
|
|
ref, err := reference.Parse(str)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ref.Object == "" {
|
|
|
|
return nil, errors.WithStack(reference.ErrObjectRequired)
|
|
|
|
}
|
|
|
|
return &ImageIdentifier{Reference: ref}, nil
|
|
|
|
}
|
|
|
|
|
2020-07-18 16:11:39 +00:00
|
|
|
func (*ImageIdentifier) ID() string {
|
2017-05-27 05:10:26 +00:00
|
|
|
return DockerImageScheme
|
|
|
|
}
|
2017-07-11 17:12:12 +00:00
|
|
|
|
|
|
|
type LocalIdentifier struct {
|
2017-08-25 20:08:18 +00:00
|
|
|
Name string
|
|
|
|
SessionID string
|
|
|
|
IncludePatterns []string
|
2017-12-14 02:49:14 +00:00
|
|
|
ExcludePatterns []string
|
2018-06-04 21:08:29 +00:00
|
|
|
FollowPaths []string
|
2017-12-14 05:01:01 +00:00
|
|
|
SharedKeyHint string
|
2017-07-11 17:12:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLocalIdentifier(str string) (*LocalIdentifier, error) {
|
|
|
|
return &LocalIdentifier{Name: str}, nil
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:35 +00:00
|
|
|
func (*LocalIdentifier) ID() string {
|
2017-07-11 17:12:12 +00:00
|
|
|
return LocalScheme
|
|
|
|
}
|
2017-11-11 02:10:10 +00:00
|
|
|
|
2020-07-18 16:11:39 +00:00
|
|
|
func NewHTTPIdentifier(str string, tls bool) (*HTTPIdentifier, error) {
|
2017-11-11 02:10:10 +00:00
|
|
|
proto := "https://"
|
|
|
|
if !tls {
|
|
|
|
proto = "http://"
|
|
|
|
}
|
2020-07-18 16:11:39 +00:00
|
|
|
return &HTTPIdentifier{TLS: tls, URL: proto + str}, nil
|
2017-11-11 02:10:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 16:11:39 +00:00
|
|
|
type HTTPIdentifier struct {
|
2017-11-11 02:10:10 +00:00
|
|
|
TLS bool
|
|
|
|
URL string
|
|
|
|
Checksum digest.Digest
|
2017-12-04 03:38:37 +00:00
|
|
|
Filename string
|
|
|
|
Perm int
|
|
|
|
UID int
|
|
|
|
GID int
|
2017-11-11 02:10:10 +00:00
|
|
|
}
|
|
|
|
|
2020-07-18 16:11:39 +00:00
|
|
|
func (*HTTPIdentifier) ID() string {
|
|
|
|
return HTTPSScheme
|
2017-11-11 02:10:10 +00:00
|
|
|
}
|
2018-07-25 06:18:21 +00:00
|
|
|
|
2018-08-17 14:33:34 +00:00
|
|
|
func (r ResolveMode) String() string {
|
|
|
|
switch r {
|
|
|
|
case ResolveModeDefault:
|
|
|
|
return pb.AttrImageResolveModeDefault
|
|
|
|
case ResolveModeForcePull:
|
|
|
|
return pb.AttrImageResolveModeForcePull
|
|
|
|
case ResolveModePreferLocal:
|
|
|
|
return pb.AttrImageResolveModePreferLocal
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 06:18:21 +00:00
|
|
|
func ParseImageResolveMode(v string) (ResolveMode, error) {
|
|
|
|
switch v {
|
|
|
|
case pb.AttrImageResolveModeDefault, "":
|
|
|
|
return ResolveModeDefault, nil
|
|
|
|
case pb.AttrImageResolveModeForcePull:
|
|
|
|
return ResolveModeForcePull, nil
|
|
|
|
case pb.AttrImageResolveModePreferLocal:
|
|
|
|
return ResolveModePreferLocal, nil
|
|
|
|
default:
|
|
|
|
return 0, errors.Errorf("invalid resolvemode: %s", v)
|
|
|
|
}
|
|
|
|
}
|
2018-07-26 20:51:28 +00:00
|
|
|
|
|
|
|
func parseImageRecordType(v string) (client.UsageRecordType, error) {
|
|
|
|
switch client.UsageRecordType(v) {
|
|
|
|
case "", client.UsageRecordTypeRegular:
|
|
|
|
return client.UsageRecordTypeRegular, nil
|
|
|
|
case client.UsageRecordTypeInternal:
|
|
|
|
return client.UsageRecordTypeInternal, nil
|
|
|
|
case client.UsageRecordTypeFrontend:
|
|
|
|
return client.UsageRecordTypeFrontend, nil
|
|
|
|
default:
|
|
|
|
return "", errors.Errorf("invalid record type %s", v)
|
|
|
|
}
|
|
|
|
}
|