-
Notifications
You must be signed in to change notification settings - Fork 109
/
image_configuration.go
259 lines (213 loc) · 7.95 KB
/
image_configuration.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2022, 2023 Chainguard, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package types
import (
"context"
"fmt"
"hash"
"os"
"strings"
"github.com/chainguard-dev/clog"
"github.com/google/go-cmp/cmp"
"github.com/jinzhu/copier"
"gopkg.in/yaml.v3"
"chainguard.dev/apko/pkg/fetch"
"chainguard.dev/apko/pkg/vcs"
)
// Attempt to probe an upstream VCS URL if known.
func (ic *ImageConfiguration) ProbeVCSUrl(ctx context.Context, imageConfigPath string) {
log := clog.FromContext(ctx)
url, err := vcs.ProbeDirFromPath(imageConfigPath)
if err != nil {
log.Debugf("failed to probe VCS URL: %v", err)
return
}
if url != "" {
ic.VCSUrl = url
log.Infof("detected %s as VCS URL", ic.VCSUrl)
}
}
// Parse a configuration blob into an ImageConfiguration struct.
func (ic *ImageConfiguration) parse(ctx context.Context, configData []byte, configHasher hash.Hash) error {
log := clog.FromContext(ctx)
configHasher.Write(configData)
if err := yaml.Unmarshal(configData, ic); err != nil {
return fmt.Errorf("failed to parse image configuration: %w", err)
}
if ic.Include != "" {
log.Infof("including %s for configuration", ic.Include)
baseIc := ImageConfiguration{}
if err := baseIc.Load(ctx, ic.Include, configHasher); err != nil {
return fmt.Errorf("failed to read include file: %w", err)
}
mergedIc := ImageConfiguration{}
// Merge packages, repositories and keyrings from base and overlay configurations
keyring := append([]string{}, baseIc.Contents.Keyring...)
keyring = append(keyring, ic.Contents.Keyring...)
repos := append([]string{}, baseIc.Contents.Repositories...)
repos = append(repos, ic.Contents.Repositories...)
pkgs := append([]string{}, baseIc.Contents.Packages...)
pkgs = append(pkgs, ic.Contents.Packages...)
// Copy the base configuration...
if err := copier.Copy(&mergedIc, &baseIc); err != nil {
return fmt.Errorf("failed to copy base configuration: %w", err)
}
// ... and then overlay the local configuration on top.
if err := copier.CopyWithOption(&mergedIc, ic, copier.Option{IgnoreEmpty: true}); err != nil {
return fmt.Errorf("failed to overlay specific configuration: %w", err)
}
// Now copy the merged configuration back to ic.
if err := copier.Copy(ic, &mergedIc); err != nil {
return fmt.Errorf("failed to copy merged configuration: %w", err)
}
// Finally, update the repeated fields to the merged ones.
ic.Contents.Keyring = keyring
ic.Contents.Repositories = repos
ic.Contents.Packages = pkgs
}
repos := make([]string, 0, len(ic.Contents.Repositories))
for _, repo := range ic.Contents.Repositories {
repo = strings.TrimRight(repo, "/")
repos = append(repos, repo)
}
ic.Contents.Repositories = repos
// The top level components restriction is on the conservative side. Some of them would probably work out of the box.
// If someone needs any of them, it should be a matter of testing and hopefully doing minor changes.
if ic.Contents.BaseImage != nil {
if !cmp.Equal((ImageEntrypoint{}), ic.Entrypoint) ||
ic.Cmd != "" ||
ic.StopSignal != "" ||
ic.WorkDir != "" ||
!cmp.Equal((ImageAccounts{}), ic.Accounts) ||
len(ic.Environment) != 0 ||
len(ic.Paths) != 0 ||
len(ic.Annotations) != 0 {
return fmt.Errorf("when using base image, the only supported image specification are: contents, archs and includes")
}
}
return nil
}
func (ic *ImageConfiguration) maybeLoadRemote(ctx context.Context, imageConfigPath string, configHasher hash.Hash) error {
data, err := fetch.Fetch(imageConfigPath)
if err != nil {
return fmt.Errorf("unable to fetch remote include from git: %w", err)
}
return ic.parse(ctx, data, configHasher)
}
// Load - loads an image configuration given a configuration file path.
// Populates configHasher with the configuration data loaded from the imageConfigPath and the other referenced files.
// You can pass any dummy hasher (like fnv.New32()), if you don't care about the hash of the configuration.
func (ic *ImageConfiguration) Load(ctx context.Context, imageConfigPath string, configHasher hash.Hash) error {
log := clog.FromContext(ctx)
data, err := os.ReadFile(imageConfigPath)
if err != nil {
log.Warnf("loading config file failed: %v", err)
log.Warnf("attempting to load remote configuration")
log.Warnf("NOTE: remote configurations are an experimental feature and subject to change.")
if err := ic.maybeLoadRemote(ctx, imageConfigPath, configHasher); err == nil {
return nil
} else {
// At this point, we're doing a remote config file.
log.Warnf("loading remote configuration failed: %v", err)
}
return err
}
return ic.parse(ctx, data, configHasher)
}
// Do preflight checks and mutations on an image configuration.
func (ic *ImageConfiguration) Validate() error {
if ic.Entrypoint.Type == "service-bundle" {
if err := ic.ValidateServiceBundle(); err != nil {
return err
}
}
for _, u := range ic.Accounts.Users {
if u.UserName == "" {
return fmt.Errorf("configured user %v has no configured user name", u)
}
if u.UID == 0 {
return fmt.Errorf("configured user %v has UID 0 (to run as root, use `run-as: 0`)", u)
}
}
for _, g := range ic.Accounts.Groups {
if g.GroupName == "" {
return fmt.Errorf("configured group %v has no configured group name", g)
}
if g.GID == 0 {
return fmt.Errorf("configured group %v has GID 0", g)
}
}
if ic.OSRelease.ID == "" {
ic.OSRelease.ID = "unknown"
}
if ic.OSRelease.Name == "" {
ic.OSRelease.Name = "apko-generated image"
ic.OSRelease.PrettyName = "apko-generated image"
}
if ic.OSRelease.VersionID == "" {
ic.OSRelease.VersionID = "unknown"
}
if ic.OSRelease.HomeURL == "" {
ic.OSRelease.HomeURL = "https://github.com/chainguard-dev/apko"
}
return nil
}
// Do preflight checks and mutations on an image configured to manage
// a service bundle.
func (ic *ImageConfiguration) ValidateServiceBundle() error {
ic.Entrypoint.Command = "/bin/s6-svscan /sv"
// It's harmless to have a duplicate entry in /etc/apk/world,
// apk will fix it up when the fixate op happens.
ic.Contents.Packages = append(ic.Contents.Packages, "s6")
return nil
}
func (ic *ImageConfiguration) Summarize(ctx context.Context) {
log := clog.FromContext(ctx)
log.Infof("image configuration:")
log.Infof(" contents:")
log.Infof(" repositories: %v", ic.Contents.Repositories)
log.Infof(" keyring: %v", ic.Contents.Keyring)
log.Infof(" packages: %v", ic.Contents.Packages)
if ic.Entrypoint.Type != "" || ic.Entrypoint.Command != "" || len(ic.Entrypoint.Services) != 0 {
log.Infof(" entrypoint:")
log.Infof(" type: %s", ic.Entrypoint.Type)
log.Infof(" command: %s", ic.Entrypoint.Command)
log.Infof(" service: %v", ic.Entrypoint.Services)
log.Infof(" shell fragment: %v", ic.Entrypoint.ShellFragment)
}
if ic.Cmd != "" {
log.Infof(" cmd: %s", ic.Cmd)
}
if ic.StopSignal != "" {
log.Infof(" stop signal: %s", ic.StopSignal)
}
if ic.Accounts.RunAs != "" || len(ic.Accounts.Users) != 0 || len(ic.Accounts.Groups) != 0 {
log.Infof(" accounts:")
log.Infof(" runas: %s", ic.Accounts.RunAs)
log.Infof(" users:")
for _, u := range ic.Accounts.Users {
log.Infof(" - uid=%d(%s) gid=%d", u.UID, u.UserName, u.GID)
}
log.Infof(" groups:")
for _, g := range ic.Accounts.Groups {
log.Infof(" - gid=%d(%s) members=%v", g.GID, g.GroupName, g.Members)
}
}
if len(ic.Annotations) > 0 {
log.Infof(" annotations:")
for k, v := range ic.Annotations {
log.Infof(" %s: %s", k, v)
}
}
}