2018-04-19 13:07:27 -04:00
|
|
|
package llb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
_ "crypto/sha256"
|
|
|
|
"encoding/json"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/distribution/reference"
|
|
|
|
"github.com/moby/buildkit/solver/pb"
|
2018-08-11 15:04:13 -04:00
|
|
|
"github.com/moby/buildkit/util/apicaps"
|
2018-04-19 13:07:27 -04:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SourceOp struct {
|
2018-06-29 11:17:00 -04:00
|
|
|
MarshalCache
|
|
|
|
id string
|
|
|
|
attrs map[string]string
|
|
|
|
output Output
|
|
|
|
constraints Constraints
|
|
|
|
err error
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
func NewSource(id string, attrs map[string]string, c Constraints) *SourceOp {
|
2018-04-19 13:07:27 -04:00
|
|
|
s := &SourceOp{
|
2018-06-29 11:17:00 -04:00
|
|
|
id: id,
|
|
|
|
attrs: attrs,
|
|
|
|
constraints: c,
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2018-06-29 11:17:00 -04:00
|
|
|
s.output = &output{vertex: s, platform: c.Platform}
|
2018-04-19 13:07:27 -04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:06:33 -04:00
|
|
|
func (s *SourceOp) Validate(ctx context.Context) error {
|
2018-04-19 13:07:27 -04:00
|
|
|
if s.err != nil {
|
|
|
|
return s.err
|
|
|
|
}
|
|
|
|
if s.id == "" {
|
|
|
|
return errors.Errorf("source identifier can't be empty")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-20 09:49:37 -04:00
|
|
|
func (s *SourceOp) Marshal(ctx context.Context, constraints *Constraints) (digest.Digest, []byte, *pb.OpMetadata, []*SourceLocation, error) {
|
2018-06-29 11:17:00 -04:00
|
|
|
if s.Cached(constraints) {
|
|
|
|
return s.Load()
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
if err := s.Validate(ctx); err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
if strings.HasPrefix(s.id, "local://") {
|
|
|
|
if _, hasSession := s.attrs[pb.AttrLocalSessionID]; !hasSession {
|
|
|
|
uid := s.constraints.LocalUniqueID
|
|
|
|
if uid == "" {
|
|
|
|
uid = constraints.LocalUniqueID
|
|
|
|
}
|
|
|
|
s.attrs[pb.AttrLocalUniqueID] = uid
|
|
|
|
addCap(&s.constraints, pb.CapSourceLocalUnique)
|
|
|
|
}
|
|
|
|
}
|
2018-06-29 11:17:00 -04:00
|
|
|
proto, md := MarshalConstraints(constraints, &s.constraints)
|
|
|
|
|
|
|
|
proto.Op = &pb.Op_Source{
|
|
|
|
Source: &pb.SourceOp{Identifier: s.id, Attrs: s.attrs},
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2018-08-11 15:04:13 -04:00
|
|
|
|
|
|
|
if !platformSpecificSource(s.id) {
|
|
|
|
proto.Platform = nil
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
dt, err := proto.Marshal()
|
|
|
|
if err != nil {
|
2020-07-20 09:49:37 -04:00
|
|
|
return "", nil, nil, nil, err
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2018-06-29 11:17:00 -04:00
|
|
|
|
2020-07-20 09:49:37 -04:00
|
|
|
s.Store(dt, md, s.constraints.SourceLocations, constraints)
|
2018-06-29 11:17:00 -04:00
|
|
|
return s.Load()
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SourceOp) Output() Output {
|
|
|
|
return s.output
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SourceOp) Inputs() []Output {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Image(ref string, opts ...ImageOption) State {
|
|
|
|
r, err := reference.ParseNormalizedNamed(ref)
|
|
|
|
if err == nil {
|
2020-04-16 11:06:33 -04:00
|
|
|
r = reference.TagNameOnly(r)
|
|
|
|
ref = r.String()
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
var info ImageInfo
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt.SetImageOption(&info)
|
|
|
|
}
|
2018-08-11 15:04:13 -04:00
|
|
|
|
|
|
|
addCap(&info.Constraints, pb.CapSourceImage)
|
|
|
|
|
|
|
|
attrs := map[string]string{}
|
|
|
|
if info.resolveMode != 0 {
|
|
|
|
attrs[pb.AttrImageResolveMode] = info.resolveMode.String()
|
|
|
|
if info.resolveMode == ResolveModeForcePull {
|
|
|
|
addCap(&info.Constraints, pb.CapSourceImageResolveMode) // only require cap for security enforced mode
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.RecordType != "" {
|
|
|
|
attrs[pb.AttrImageRecordType] = info.RecordType
|
|
|
|
}
|
|
|
|
|
|
|
|
src := NewSource("docker-image://"+ref, attrs, info.Constraints) // controversial
|
2018-04-19 13:07:27 -04:00
|
|
|
if err != nil {
|
|
|
|
src.err = err
|
2020-04-16 11:06:33 -04:00
|
|
|
} else if info.metaResolver != nil {
|
|
|
|
if _, ok := r.(reference.Digested); ok || !info.resolveDigest {
|
|
|
|
return NewState(src.Output()).Async(func(ctx context.Context, st State) (State, error) {
|
|
|
|
_, dt, err := info.metaResolver.ResolveImageConfig(ctx, ref, ResolveImageConfigOpt{
|
|
|
|
Platform: info.Constraints.Platform,
|
|
|
|
ResolveMode: info.resolveMode.String(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return State{}, err
|
|
|
|
}
|
|
|
|
return st.WithImageConfig(dt)
|
|
|
|
})
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2020-04-16 11:06:33 -04:00
|
|
|
return Scratch().Async(func(ctx context.Context, _ State) (State, error) {
|
|
|
|
dgst, dt, err := info.metaResolver.ResolveImageConfig(context.TODO(), ref, ResolveImageConfigOpt{
|
|
|
|
Platform: info.Constraints.Platform,
|
|
|
|
ResolveMode: info.resolveMode.String(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return State{}, err
|
|
|
|
}
|
|
|
|
if dgst != "" {
|
|
|
|
r, err = reference.WithDigest(r, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return State{}, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NewState(NewSource("docker-image://"+r.String(), attrs, info.Constraints).Output()).WithImageConfig(dt)
|
|
|
|
})
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
return NewState(src.Output())
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImageOption interface {
|
|
|
|
SetImageOption(*ImageInfo)
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
type imageOptionFunc func(*ImageInfo)
|
2018-04-19 13:07:27 -04:00
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
func (fn imageOptionFunc) SetImageOption(ii *ImageInfo) {
|
2018-04-19 13:07:27 -04:00
|
|
|
fn(ii)
|
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
var MarkImageInternal = imageOptionFunc(func(ii *ImageInfo) {
|
|
|
|
ii.RecordType = "internal"
|
|
|
|
})
|
|
|
|
|
|
|
|
type ResolveMode int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ResolveModeDefault ResolveMode = iota
|
|
|
|
ResolveModeForcePull
|
|
|
|
ResolveModePreferLocal
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r ResolveMode) SetImageOption(ii *ImageInfo) {
|
|
|
|
ii.resolveMode = r
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-19 13:07:27 -04:00
|
|
|
type ImageInfo struct {
|
2018-06-29 11:17:00 -04:00
|
|
|
constraintsWrapper
|
2020-04-16 11:06:33 -04:00
|
|
|
metaResolver ImageMetaResolver
|
|
|
|
resolveDigest bool
|
|
|
|
resolveMode ResolveMode
|
|
|
|
RecordType string
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func Git(remote, ref string, opts ...GitOption) State {
|
|
|
|
url := ""
|
|
|
|
|
|
|
|
for _, prefix := range []string{
|
|
|
|
"http://", "https://", "git://", "git@",
|
|
|
|
} {
|
|
|
|
if strings.HasPrefix(remote, prefix) {
|
|
|
|
url = strings.Split(remote, "#")[0]
|
|
|
|
remote = strings.TrimPrefix(remote, prefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
id := remote
|
|
|
|
|
|
|
|
if ref != "" {
|
|
|
|
id += "#" + ref
|
|
|
|
}
|
|
|
|
|
|
|
|
gi := &GitInfo{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o.SetGitOption(gi)
|
|
|
|
}
|
|
|
|
attrs := map[string]string{}
|
|
|
|
if gi.KeepGitDir {
|
|
|
|
attrs[pb.AttrKeepGitDir] = "true"
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&gi.Constraints, pb.CapSourceGitKeepDir)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
if url != "" {
|
|
|
|
attrs[pb.AttrFullRemoteURL] = url
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&gi.Constraints, pb.CapSourceGitFullURL)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2018-08-11 15:04:13 -04:00
|
|
|
|
|
|
|
addCap(&gi.Constraints, pb.CapSourceGit)
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
source := NewSource("git://"+id, attrs, gi.Constraints)
|
2018-04-19 13:07:27 -04:00
|
|
|
return NewState(source.Output())
|
|
|
|
}
|
|
|
|
|
|
|
|
type GitOption interface {
|
|
|
|
SetGitOption(*GitInfo)
|
|
|
|
}
|
|
|
|
type gitOptionFunc func(*GitInfo)
|
|
|
|
|
|
|
|
func (fn gitOptionFunc) SetGitOption(gi *GitInfo) {
|
|
|
|
fn(gi)
|
|
|
|
}
|
|
|
|
|
|
|
|
type GitInfo struct {
|
2018-06-29 11:17:00 -04:00
|
|
|
constraintsWrapper
|
2018-04-19 13:07:27 -04:00
|
|
|
KeepGitDir bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func KeepGitDir() GitOption {
|
|
|
|
return gitOptionFunc(func(gi *GitInfo) {
|
|
|
|
gi.KeepGitDir = true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Scratch() State {
|
|
|
|
return NewState(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Local(name string, opts ...LocalOption) State {
|
|
|
|
gi := &LocalInfo{}
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o.SetLocalOption(gi)
|
|
|
|
}
|
|
|
|
attrs := map[string]string{}
|
|
|
|
if gi.SessionID != "" {
|
|
|
|
attrs[pb.AttrLocalSessionID] = gi.SessionID
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&gi.Constraints, pb.CapSourceLocalSessionID)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
if gi.IncludePatterns != "" {
|
|
|
|
attrs[pb.AttrIncludePatterns] = gi.IncludePatterns
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&gi.Constraints, pb.CapSourceLocalIncludePatterns)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2018-06-08 21:07:42 -04:00
|
|
|
if gi.FollowPaths != "" {
|
|
|
|
attrs[pb.AttrFollowPaths] = gi.FollowPaths
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&gi.Constraints, pb.CapSourceLocalFollowPaths)
|
2018-06-08 21:07:42 -04:00
|
|
|
}
|
2018-04-19 13:07:27 -04:00
|
|
|
if gi.ExcludePatterns != "" {
|
|
|
|
attrs[pb.AttrExcludePatterns] = gi.ExcludePatterns
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&gi.Constraints, pb.CapSourceLocalExcludePatterns)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
if gi.SharedKeyHint != "" {
|
|
|
|
attrs[pb.AttrSharedKeyHint] = gi.SharedKeyHint
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&gi.Constraints, pb.CapSourceLocalSharedKeyHint)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&gi.Constraints, pb.CapSourceLocal)
|
|
|
|
|
2018-06-29 11:17:00 -04:00
|
|
|
source := NewSource("local://"+name, attrs, gi.Constraints)
|
2018-04-19 13:07:27 -04:00
|
|
|
return NewState(source.Output())
|
|
|
|
}
|
|
|
|
|
|
|
|
type LocalOption interface {
|
|
|
|
SetLocalOption(*LocalInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
type localOptionFunc func(*LocalInfo)
|
|
|
|
|
|
|
|
func (fn localOptionFunc) SetLocalOption(li *LocalInfo) {
|
|
|
|
fn(li)
|
|
|
|
}
|
|
|
|
|
|
|
|
func SessionID(id string) LocalOption {
|
|
|
|
return localOptionFunc(func(li *LocalInfo) {
|
|
|
|
li.SessionID = id
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func IncludePatterns(p []string) LocalOption {
|
|
|
|
return localOptionFunc(func(li *LocalInfo) {
|
|
|
|
if len(p) == 0 {
|
|
|
|
li.IncludePatterns = ""
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dt, _ := json.Marshal(p) // empty on error
|
|
|
|
li.IncludePatterns = string(dt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-08 21:07:42 -04:00
|
|
|
func FollowPaths(p []string) LocalOption {
|
|
|
|
return localOptionFunc(func(li *LocalInfo) {
|
|
|
|
if len(p) == 0 {
|
|
|
|
li.FollowPaths = ""
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dt, _ := json.Marshal(p) // empty on error
|
|
|
|
li.FollowPaths = string(dt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-19 13:07:27 -04:00
|
|
|
func ExcludePatterns(p []string) LocalOption {
|
|
|
|
return localOptionFunc(func(li *LocalInfo) {
|
|
|
|
if len(p) == 0 {
|
|
|
|
li.ExcludePatterns = ""
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dt, _ := json.Marshal(p) // empty on error
|
|
|
|
li.ExcludePatterns = string(dt)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func SharedKeyHint(h string) LocalOption {
|
|
|
|
return localOptionFunc(func(li *LocalInfo) {
|
|
|
|
li.SharedKeyHint = h
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type LocalInfo struct {
|
2018-06-29 11:17:00 -04:00
|
|
|
constraintsWrapper
|
2018-04-19 13:07:27 -04:00
|
|
|
SessionID string
|
|
|
|
IncludePatterns string
|
|
|
|
ExcludePatterns string
|
2018-06-08 21:07:42 -04:00
|
|
|
FollowPaths string
|
2018-04-19 13:07:27 -04:00
|
|
|
SharedKeyHint string
|
|
|
|
}
|
|
|
|
|
|
|
|
func HTTP(url string, opts ...HTTPOption) State {
|
|
|
|
hi := &HTTPInfo{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o.SetHTTPOption(hi)
|
|
|
|
}
|
|
|
|
attrs := map[string]string{}
|
|
|
|
if hi.Checksum != "" {
|
|
|
|
attrs[pb.AttrHTTPChecksum] = hi.Checksum.String()
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&hi.Constraints, pb.CapSourceHTTPChecksum)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
if hi.Filename != "" {
|
|
|
|
attrs[pb.AttrHTTPFilename] = hi.Filename
|
|
|
|
}
|
|
|
|
if hi.Perm != 0 {
|
|
|
|
attrs[pb.AttrHTTPPerm] = "0" + strconv.FormatInt(int64(hi.Perm), 8)
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&hi.Constraints, pb.CapSourceHTTPPerm)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
if hi.UID != 0 {
|
|
|
|
attrs[pb.AttrHTTPUID] = strconv.Itoa(hi.UID)
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&hi.Constraints, pb.CapSourceHTTPUIDGID)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
2018-08-11 15:04:13 -04:00
|
|
|
if hi.GID != 0 {
|
2018-04-19 13:07:27 -04:00
|
|
|
attrs[pb.AttrHTTPGID] = strconv.Itoa(hi.GID)
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&hi.Constraints, pb.CapSourceHTTPUIDGID)
|
2018-04-19 13:07:27 -04:00
|
|
|
}
|
|
|
|
|
2018-08-11 15:04:13 -04:00
|
|
|
addCap(&hi.Constraints, pb.CapSourceHTTP)
|
2018-06-29 11:17:00 -04:00
|
|
|
source := NewSource(url, attrs, hi.Constraints)
|
2018-04-19 13:07:27 -04:00
|
|
|
return NewState(source.Output())
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPInfo struct {
|
2018-06-29 11:17:00 -04:00
|
|
|
constraintsWrapper
|
2018-04-19 13:07:27 -04:00
|
|
|
Checksum digest.Digest
|
|
|
|
Filename string
|
|
|
|
Perm int
|
|
|
|
UID int
|
|
|
|
GID int
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPOption interface {
|
|
|
|
SetHTTPOption(*HTTPInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpOptionFunc func(*HTTPInfo)
|
|
|
|
|
|
|
|
func (fn httpOptionFunc) SetHTTPOption(hi *HTTPInfo) {
|
|
|
|
fn(hi)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Checksum(dgst digest.Digest) HTTPOption {
|
|
|
|
return httpOptionFunc(func(hi *HTTPInfo) {
|
|
|
|
hi.Checksum = dgst
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Chmod(perm os.FileMode) HTTPOption {
|
|
|
|
return httpOptionFunc(func(hi *HTTPInfo) {
|
|
|
|
hi.Perm = int(perm) & 0777
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Filename(name string) HTTPOption {
|
|
|
|
return httpOptionFunc(func(hi *HTTPInfo) {
|
|
|
|
hi.Filename = name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Chown(uid, gid int) HTTPOption {
|
|
|
|
return httpOptionFunc(func(hi *HTTPInfo) {
|
|
|
|
hi.UID = uid
|
|
|
|
hi.GID = gid
|
|
|
|
})
|
|
|
|
}
|
2018-08-11 15:04:13 -04:00
|
|
|
|
|
|
|
func platformSpecificSource(id string) bool {
|
|
|
|
return strings.HasPrefix(id, "docker-image://")
|
|
|
|
}
|
|
|
|
|
|
|
|
func addCap(c *Constraints, id apicaps.CapID) {
|
|
|
|
if c.Metadata.Caps == nil {
|
|
|
|
c.Metadata.Caps = make(map[apicaps.CapID]bool)
|
|
|
|
}
|
|
|
|
c.Metadata.Caps[id] = true
|
|
|
|
}
|