Skip to content

Commit 64b8ffe

Browse files
committed
container driver: copy ca and user tls registries certs
Signed-off-by: CrazyMax <[email protected]>
1 parent 084b6c0 commit 64b8ffe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+11388
-33
lines changed

docs/reference/buildx_create.md

+5
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ Specifies the configuration file for the buildkitd daemon to use. The configurat
8484
can be overridden by [`--buildkitd-flags`](#buildkitd-flags).
8585
See an [example buildkitd configuration file](https://github.com/moby/buildkit/blob/master/docs/buildkitd.toml.md).
8686

87+
Note that if you create a `docker-container` builder and have specified
88+
certificates for registries in the `buildkitd.toml` configuration, the files
89+
will be copied into the container under `/etc/buildkit/certs` and configuration
90+
will be updated to reflect that.
91+
8792
### <a name="driver"></a> Set the builder driver to use (--driver)
8893

8994
```

driver/docker-container/driver.go

+15-33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package docker
22

33
import (
4-
"archive/tar"
54
"bytes"
65
"context"
76
"io"
@@ -12,6 +11,7 @@ import (
1211

1312
"github.com/docker/buildx/driver"
1413
"github.com/docker/buildx/driver/bkimage"
14+
"github.com/docker/buildx/util/confutil"
1515
"github.com/docker/buildx/util/imagetools"
1616
"github.com/docker/buildx/util/progress"
1717
"github.com/docker/docker/api/types"
@@ -20,6 +20,7 @@ import (
2020
"github.com/docker/docker/api/types/mount"
2121
"github.com/docker/docker/api/types/network"
2222
dockerclient "github.com/docker/docker/client"
23+
dockerarchive "github.com/docker/docker/pkg/archive"
2324
"github.com/docker/docker/pkg/stdcopy"
2425
"github.com/moby/buildkit/client"
2526
"github.com/moby/buildkit/util/tracing/detect"
@@ -28,12 +29,6 @@ import (
2829

2930
const (
3031
volumeStateSuffix = "_state"
31-
32-
// containerStateDir is the location where buildkitd inside the container
33-
// stores its state. The container driver creates a Linux container, so
34-
// this should match the location for Linux, as defined in:
35-
// https://github.com/moby/buildkit/blob/v0.9.0/util/appdefaults/appdefaults_unix.go#L11-L15
36-
containerBuildKitRootDir = "/var/lib/buildkit"
3732
)
3833

3934
type Driver struct {
@@ -119,7 +114,7 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {
119114
{
120115
Type: mount.TypeVolume,
121116
Source: d.Name + volumeStateSuffix,
122-
Target: containerBuildKitRootDir,
117+
Target: confutil.DefaultBuildKitStateDir,
123118
},
124119
},
125120
}
@@ -139,11 +134,12 @@ func (d *Driver) create(ctx context.Context, l progress.SubLogger) error {
139134
return err
140135
}
141136
if f := d.InitConfig.ConfigFile; f != "" {
142-
buf, err := readFileToTar(f)
137+
configFiles, err := confutil.LoadConfigFiles(f)
143138
if err != nil {
144139
return err
145140
}
146-
if err := d.DockerAPI.CopyToContainer(ctx, d.Name, "/", buf, dockertypes.CopyToContainerOptions{}); err != nil {
141+
defer os.RemoveAll(configFiles)
142+
if err := d.copyToContainer(ctx, configFiles, "/"); err != nil {
147143
return err
148144
}
149145
}
@@ -205,6 +201,15 @@ func (d *Driver) copyLogs(ctx context.Context, l progress.SubLogger) error {
205201
return rc.Close()
206202
}
207203

204+
func (d *Driver) copyToContainer(ctx context.Context, srcPath string, dstDir string) error {
205+
srcArchive, err := dockerarchive.TarWithOptions(srcPath, &dockerarchive.TarOptions{})
206+
if err != nil {
207+
return err
208+
}
209+
defer srcArchive.Close()
210+
return d.DockerAPI.CopyToContainer(ctx, d.Name, dstDir, srcArchive, dockertypes.CopyToContainerOptions{})
211+
}
212+
208213
func (d *Driver) exec(ctx context.Context, cmd []string) (string, net.Conn, error) {
209214
execConfig := types.ExecConfig{
210215
Cmd: cmd,
@@ -366,29 +371,6 @@ func (d *demux) Read(dt []byte) (int, error) {
366371
return d.Reader.Read(dt)
367372
}
368373

369-
func readFileToTar(fn string) (*bytes.Buffer, error) {
370-
buf := bytes.NewBuffer(nil)
371-
tw := tar.NewWriter(buf)
372-
dt, err := ioutil.ReadFile(fn)
373-
if err != nil {
374-
return nil, err
375-
}
376-
if err := tw.WriteHeader(&tar.Header{
377-
Name: "/etc/buildkit/buildkitd.toml",
378-
Size: int64(len(dt)),
379-
Mode: 0644,
380-
}); err != nil {
381-
return nil, err
382-
}
383-
if _, err := tw.Write(dt); err != nil {
384-
return nil, err
385-
}
386-
if err := tw.Close(); err != nil {
387-
return nil, err
388-
}
389-
return buf, nil
390-
}
391-
392374
type logWriter struct {
393375
logger progress.SubLogger
394376
stream int

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ require (
3434
github.com/moby/buildkit v0.9.1-0.20211019185819-8778943ac3da
3535
github.com/opencontainers/go-digest v1.0.0
3636
github.com/opencontainers/image-spec v1.0.2-0.20210819154149-5ad6f50d6283
37+
github.com/pelletier/go-toml v1.9.4
3738
github.com/pkg/errors v0.9.1
3839
github.com/serialx/hashring v0.0.0-20190422032157-8b2912629002
3940
github.com/sirupsen/logrus v1.8.1

util/confutil/config.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package confutil
2+
3+
import (
4+
"os"
5+
6+
"github.com/pelletier/go-toml"
7+
"github.com/pkg/errors"
8+
)
9+
10+
// loadConfigTree loads BuildKit config toml tree
11+
func loadConfigTree(fp string) (*toml.Tree, error) {
12+
f, err := os.Open(fp)
13+
if err != nil {
14+
if errors.Is(err, os.ErrNotExist) {
15+
return nil, nil
16+
}
17+
return nil, errors.Wrapf(err, "failed to load config from %s", fp)
18+
}
19+
defer f.Close()
20+
t, err := toml.LoadReader(f)
21+
if err != nil {
22+
return t, errors.Wrap(err, "failed to parse config")
23+
}
24+
return t, nil
25+
}

util/confutil/container.go

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
package confutil
2+
3+
import (
4+
"io"
5+
"os"
6+
"path"
7+
8+
"github.com/pelletier/go-toml"
9+
"github.com/pkg/errors"
10+
)
11+
12+
const (
13+
// DefaultBuildKitStateDir and DefaultBuildKitConfigDir are the location
14+
// where buildkitd inside the container stores its state. Some drivers
15+
// create a Linux container, so this should match the location for Linux,
16+
// as defined in: https://github.com/moby/buildkit/blob/v0.9.0/util/appdefaults/appdefaults_unix.go#L11-L15
17+
DefaultBuildKitStateDir = "/var/lib/buildkit"
18+
DefaultBuildKitConfigDir = "/etc/buildkit"
19+
)
20+
21+
// LoadConfigFiles creates a temp directory with BuildKit config and
22+
// registry certificates ready to be copied to a container.
23+
func LoadConfigFiles(bkconfig string) (string, error) {
24+
if _, err := os.Stat(bkconfig); errors.Is(err, os.ErrNotExist) {
25+
return "", errors.Wrapf(err, "buildkit configuration file not found: %s", bkconfig)
26+
} else if err != nil {
27+
return "", errors.Wrapf(err, "invalid buildkit configuration file: %s", bkconfig)
28+
}
29+
30+
// Load config tree
31+
btoml, err := loadConfigTree(bkconfig)
32+
if err != nil {
33+
return "", err
34+
}
35+
36+
// Temp dir that will be copied to the container
37+
tmpDir, err := os.MkdirTemp("", "buildkitd-config")
38+
if err != nil {
39+
return "", err
40+
}
41+
42+
// Create BuildKit config folders
43+
tmpBuildKitConfigDir := path.Join(tmpDir, DefaultBuildKitConfigDir)
44+
tmpBuildKitCertsDir := path.Join(tmpBuildKitConfigDir, "certs")
45+
if err := os.MkdirAll(tmpBuildKitCertsDir, 0700); err != nil {
46+
return "", err
47+
}
48+
49+
// Iterate through registry config to copy certs and update
50+
// BuildKit config with the underlying certs' path in the container.
51+
//
52+
// The following BuildKit config:
53+
//
54+
// [registry."myregistry.io"]
55+
// ca=["/etc/config/myca.pem"]
56+
// [[registry."myregistry.io".keypair]]
57+
// key="/etc/config/key.pem"
58+
// cert="/etc/config/cert.pem"
59+
//
60+
// will be translated in the container as:
61+
//
62+
// [registry."myregistry.io"]
63+
// ca=["/etc/buildkit/certs/myregistry.io/myca.pem"]
64+
// [[registry."myregistry.io".keypair]]
65+
// key="/etc/buildkit/certs/myregistry.io/key.pem"
66+
// cert="/etc/buildkit/certs/myregistry.io/cert.pem"
67+
if btoml.Has("registry") {
68+
for regName := range btoml.GetArray("registry").(*toml.Tree).Values() {
69+
regConf := btoml.GetPath([]string{"registry", regName}).(*toml.Tree)
70+
if regConf == nil {
71+
continue
72+
}
73+
regCertsDir := path.Join(tmpBuildKitCertsDir, regName)
74+
if err := os.Mkdir(regCertsDir, 0755); err != nil {
75+
return "", err
76+
}
77+
if regConf.Has("ca") {
78+
regCAs := regConf.GetArray("ca").([]string)
79+
if len(regCAs) > 0 {
80+
var cas []string
81+
for _, ca := range regCAs {
82+
cas = append(cas, path.Join(DefaultBuildKitConfigDir, "certs", regName, path.Base(ca)))
83+
if err := copyfile(ca, path.Join(regCertsDir, path.Base(ca))); err != nil {
84+
return "", err
85+
}
86+
}
87+
regConf.Set("ca", cas)
88+
}
89+
}
90+
if regConf.Has("keypair") {
91+
regKeyPairs := regConf.GetArray("keypair").([]*toml.Tree)
92+
if len(regKeyPairs) == 0 {
93+
continue
94+
}
95+
for _, kp := range regKeyPairs {
96+
if kp == nil {
97+
continue
98+
}
99+
key := kp.Get("key").(string)
100+
if len(key) > 0 {
101+
kp.Set("key", path.Join(DefaultBuildKitConfigDir, "certs", regName, path.Base(key)))
102+
if err := copyfile(key, path.Join(regCertsDir, path.Base(key))); err != nil {
103+
return "", err
104+
}
105+
}
106+
cert := kp.Get("cert").(string)
107+
if len(cert) > 0 {
108+
kp.Set("cert", path.Join(DefaultBuildKitConfigDir, "certs", regName, path.Base(cert)))
109+
if err := copyfile(cert, path.Join(regCertsDir, path.Base(cert))); err != nil {
110+
return "", err
111+
}
112+
}
113+
}
114+
}
115+
}
116+
}
117+
118+
// Write BuildKit config
119+
bkfile, err := os.OpenFile(path.Join(tmpBuildKitConfigDir, "buildkitd.toml"), os.O_CREATE|os.O_WRONLY, 0600)
120+
if err != nil {
121+
return "", err
122+
}
123+
_, err = btoml.WriteTo(bkfile)
124+
if err != nil {
125+
return "", err
126+
}
127+
128+
return tmpDir, nil
129+
}
130+
131+
func copyfile(src string, dst string) error {
132+
sf, err := os.Open(src)
133+
if err != nil {
134+
return err
135+
}
136+
defer sf.Close()
137+
df, err := os.OpenFile(dst, os.O_CREATE|os.O_WRONLY, 0600)
138+
if err != nil {
139+
return err
140+
}
141+
defer df.Close()
142+
_, err = io.Copy(df, sf)
143+
return err
144+
}

vendor/github.com/containerd/containerd/pkg/userns/userns_linux.go

+62
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/containerd/containerd/pkg/userns/userns_unsupported.go

+25
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/docker/docker/pkg/archive/README.md

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)