/
extract_tools.go
660 lines (572 loc) · 19.5 KB
/
extract_tools.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package release
import (
"archive/tar"
"archive/zip"
"bufio"
"bytes"
"compress/gzip"
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"
"syscall"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/crypto/openpgp"
"k8s.io/klog"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/MakeNowJust/heredoc"
imagereference "github.com/openshift/origin/pkg/image/apis/image/reference"
"github.com/openshift/origin/pkg/oc/cli/image/extract"
)
// extractTarget describes how a file in the release image can be extracted to disk.
type extractTarget struct {
OS string
Command string
TargetName string
InjectReleaseImage bool
ArchiveFormat string
AsArchive bool
AsZip bool
Readme string
LinkTo []string
Mapping extract.Mapping
}
// extractTools extracts all referenced commands as archives in the target dir.
func (o *ExtractOptions) extractTools() error {
return o.extractCommand("")
}
var (
readmeInstallUnix = heredoc.Doc(`
# OpenShift Install
The OpenShift installer \u0060openshift-install\u0060 makes it easy to get a cluster
running on the public cloud or your local infrastructure.
To learn more about installing OpenShift, visit [docs.openshift.com](https://docs.openshift.com)
and select the version of OpenShift you are using.
## Installing the tools
After extracting this archive, you can move the \u0060openshift-install\u0060 binary
to a location on your PATH such as \u0060/usr/local/bin\u0060, or keep it in a temporary
directory and reference it via \u0060./openshift-install\u0060.
## License
OpenShift is licensed under the Apache Public License 2.0. The source code for this
program is [located on github](https://github.com/openshift/installer).
`)
readmeCLIUnix = heredoc.Doc(`
# OpenShift Clients
The OpenShift client \u0060oc\u0060 simplifies working with Kubernetes and OpenShift
clusters, offering a number of advantages over \u0060kubectl\u0060 such as easy login,
kube config file management, and access to developer tools. The \u0060kubectl\u0060
binary is included alongside for when strict Kubernetes compliance is necessary.
To learn more about OpenShift, visit [docs.openshift.com](https://docs.openshift.com)
and select the version of OpenShift you are using.
## Installing the tools
After extracting this archive, move the \u0060oc\u0060 and \u0060kubectl\u0060 binaries
to a location on your PATH such as \u0060/usr/local/bin\u0060. Then run:
oc login [API_URL]
to start a session against an OpenShift cluster. After login, run \u0060oc\u0060 and
\u0060oc help\u0060 to learn more about how to get started with OpenShift.
## License
OpenShift is licensed under the Apache Public License 2.0. The source code for this
program is [located on github](https://github.com/openshift/origin).
`)
readmeCLIWindows = heredoc.Doc(`
# OpenShift Clients
The OpenShift client \u0060oc.exe\u0060 simplifies working with Kubernetes and OpenShift
clusters, offering a number of advantages over \u0060kubectl.exe\u0060 such as easy login,
kube config file management, and access to developer tools.
To learn more about OpenShift, visit [docs.openshift.com](https://docs.openshift.com)
and select the version of OpenShift you are using.
## Installing the tools
After extracting this archive, move the \u0060oc.exe\u0060 binary to a location on your
PATH. Then run:
oc login [API_URL]
to start a session against an OpenShift cluster. After login, run \u0060oc.exe\u0060 and
\u0060oc.exe help\u0060 to learn more about how to get started with OpenShift.
If you would like to use \u0060kubectl.exe\u0060 instead, copy the \u0060oc.exe\u0060 file
and rename it to \u0060kubectl.exe\u0060. The interface will follow the conventions of that
CLI.
## License
OpenShift is licensed under the Apache Public License 2.0. The source code for this
program is [located on github](https://github.com/openshift/origin).
`)
)
// extractTools extracts specific commands out of images referenced by the release image.
// TODO: in the future the metadata this command contains might be loaded from the release
// image, but we must maintain compatibility with older payloads if so
func (o *ExtractOptions) extractCommand(command string) error {
// Available targets is treated as a GA API and may not be changed without backwards
// compatibility of at least N-2 releases.
availableTargets := []extractTarget{
{
OS: "darwin",
Command: "oc",
Mapping: extract.Mapping{Image: "cli-artifacts", From: "usr/share/openshift/mac/oc"},
LinkTo: []string{"kubectl"},
Readme: readmeCLIUnix,
ArchiveFormat: "openshift-client-mac-%s.tar.gz",
},
{
OS: "linux",
Command: "oc",
Mapping: extract.Mapping{Image: "cli", From: "usr/bin/oc"},
LinkTo: []string{"kubectl"},
Readme: readmeCLIUnix,
ArchiveFormat: "openshift-client-linux-%s.tar.gz",
},
{
OS: "windows",
Command: "oc",
Mapping: extract.Mapping{Image: "cli-artifacts", From: "usr/share/openshift/windows/oc.exe"},
Readme: readmeCLIWindows,
ArchiveFormat: "openshift-client-windows-%s.zip",
AsZip: true,
},
{
OS: "darwin",
Command: "openshift-install",
Mapping: extract.Mapping{Image: "installer-artifacts", From: "usr/share/openshift/mac/openshift-install"},
Readme: readmeInstallUnix,
InjectReleaseImage: true,
ArchiveFormat: "openshift-install-mac-%s.tar.gz",
},
{
OS: "linux",
Command: "openshift-install",
Mapping: extract.Mapping{Image: "installer", From: "usr/bin/openshift-install"},
Readme: readmeInstallUnix,
InjectReleaseImage: true,
ArchiveFormat: "openshift-install-linux-%s.tar.gz",
},
}
currentOS := runtime.GOOS
if len(o.CommandOperatingSystem) > 0 {
currentOS = o.CommandOperatingSystem
}
if currentOS == "mac" {
currentOS = "darwin"
}
// select the subset of targets based on command line input
var willArchive bool
var targets []extractTarget
if len(command) > 0 {
hasCommand := false
for _, target := range availableTargets {
if target.Command != command {
continue
}
hasCommand = true
if target.OS == currentOS || currentOS == "*" {
targets = []extractTarget{target}
break
}
}
if len(targets) == 0 {
if hasCommand {
return fmt.Errorf("command %q does not support the operating system %q", o.Command, currentOS)
}
return fmt.Errorf("the supported commands are 'oc' and 'openshift-install'")
}
} else {
willArchive = true
targets = availableTargets
for i := range targets {
targets[i].AsArchive = true
targets[i].AsZip = targets[i].OS == "windows"
}
}
var hashFn = sha256.New
var signer *openpgp.Entity
if willArchive && len(o.SigningKey) > 0 {
key, err := ioutil.ReadFile(o.SigningKey)
if err != nil {
return err
}
keyring, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(key))
if err != nil {
return err
}
for _, key := range keyring {
if !key.PrivateKey.CanSign() {
continue
}
fmt.Fprintf(o.Out, "Enter password for private key: ")
password, err := terminal.ReadPassword(int(syscall.Stdin))
fmt.Fprintln(o.Out)
if err != nil {
return err
}
if err := key.PrivateKey.Decrypt(password); err != nil {
return fmt.Errorf("unable to decrypt signing key: %v", err)
}
for i, subkey := range key.Subkeys {
if err := subkey.PrivateKey.Decrypt(password); err != nil {
return fmt.Errorf("unable to decrypt signing subkey %d: %v", i, err)
}
}
signer = key
break
}
if signer == nil {
return fmt.Errorf("no private key exists in %s capable of signing the output", o.SigningKey)
}
}
// load the release image
dir := o.Directory
infoOptions := NewInfoOptions(o.IOStreams)
release, err := infoOptions.LoadReleaseInfo(o.From, false)
if err != nil {
return err
}
releaseName := release.PreferredName()
refExact := release.ImageRef
refExact.Tag = ""
refExact.ID = release.Digest.String()
exactReleaseImage := refExact.String()
// resolve target image references to their pull specs
missing := sets.NewString()
var validTargets []extractTarget
for _, target := range targets {
if currentOS != "*" && target.OS != currentOS {
klog.V(2).Infof("Skipping %s, does not match current OS %s", target.ArchiveFormat, target.OS)
continue
}
spec, err := findImageSpec(release.References, target.Mapping.Image, o.From)
if err != nil {
missing.Insert(target.Mapping.Image)
continue
}
klog.V(2).Infof("Will extract %s from %s", target.Mapping.From, spec)
ref, err := imagereference.Parse(spec)
if err != nil {
return err
}
target.Mapping.Image = spec
target.Mapping.ImageRef = ref
if target.AsArchive {
willArchive = true
target.Mapping.Name = fmt.Sprintf(target.ArchiveFormat, releaseName)
target.Mapping.To = filepath.Join(dir, target.Mapping.Name)
} else {
target.Mapping.To = filepath.Join(dir, filepath.Base(target.Mapping.From))
target.Mapping.Name = fmt.Sprintf("%s-%s", target.OS, target.Command)
}
validTargets = append(validTargets, target)
}
if len(validTargets) == 0 {
if len(missing) == 1 {
return fmt.Errorf("the image %q containing the desired command is not available", missing.List()[0])
}
return fmt.Errorf("some required images are missing: %s", strings.Join(missing.List(), ", "))
}
if len(missing) > 0 {
fmt.Fprintf(o.ErrOut, "warning: Some commands can not be extracted due to missing images: %s\n", strings.Join(missing.List(), ", "))
}
// will extract in parallel
opts := extract.NewOptions(genericclioptions.IOStreams{Out: o.Out, ErrOut: o.ErrOut})
opts.ParallelOptions = o.ParallelOptions
opts.SecurityOptions = o.SecurityOptions
opts.OnlyFiles = true
// create the mapping lookup of the valid targets
var extractLock sync.Mutex
targetsByName := make(map[string]extractTarget)
for _, target := range validTargets {
targetsByName[target.Mapping.Name] = target
opts.Mappings = append(opts.Mappings, target.Mapping)
}
hashByTargetName := make(map[string]string)
// ensure to is a directory
if err := os.MkdirAll(dir, 0777); err != nil {
return err
}
// as each layer is extracted, take the output binary and write it to disk
opts.TarEntryCallback = func(hdr *tar.Header, layer extract.LayerInfo, r io.Reader) (bool, error) {
// ensure we don't process the same mapping twice due to programmer error
target, ok := func() (extractTarget, bool) {
extractLock.Lock()
defer extractLock.Unlock()
target, ok := targetsByName[layer.Mapping.Name]
return target, ok
}()
if !ok {
return false, fmt.Errorf("unable to find target with mapping name %s", layer.Mapping.Name)
}
// open the file
f, err := os.OpenFile(layer.Mapping.To, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0755)
if err != nil {
return false, err
}
// if we need to write an archive, wrap the file appropriately to create a single
// entry
var w io.Writer = f
bw := bufio.NewWriterSize(w, 16*1024)
w = bw
var hash hash.Hash
closeFn := func() error { return nil }
if target.AsArchive {
text := strings.Replace(target.Readme, `\u0060`, "`", -1)
hash = hashFn()
w = io.MultiWriter(hash, w)
if target.AsZip {
klog.V(2).Infof("Writing %s as a ZIP archive %s", hdr.Name, layer.Mapping.To)
zw := zip.NewWriter(w)
if len(text) > 0 {
text = strings.Replace(text, "\n", "\r\n", -1)
zh := &zip.FileHeader{
Method: zip.Deflate,
Name: "README.md",
UncompressedSize64: uint64(len(text)),
Modified: hdr.ModTime,
}
zh.SetMode(os.FileMode(0755))
fw, err := zw.CreateHeader(zh)
if err != nil {
return false, err
}
if _, err := fmt.Fprintf(fw, text); err != nil {
return false, err
}
}
zh := &zip.FileHeader{
Method: zip.Deflate,
Name: hdr.Name,
UncompressedSize64: uint64(hdr.Size),
Modified: hdr.ModTime,
}
zh.SetMode(os.FileMode(0755))
fw, err := zw.CreateHeader(zh)
if err != nil {
return false, err
}
w = fw
closeFn = func() error { return zw.Close() }
} else {
klog.V(2).Infof("Writing %s as a tar.gz archive %s", hdr.Name, layer.Mapping.To)
gw, err := gzip.NewWriterLevel(w, 3)
if err != nil {
return false, err
}
tw := tar.NewWriter(gw)
if len(text) > 0 {
if err := tw.WriteHeader(&tar.Header{
Name: "README.md",
Mode: int64(os.FileMode(0644).Perm()),
Size: int64(len(text)),
Typeflag: tar.TypeReg,
ModTime: hdr.ModTime,
}); err != nil {
return false, err
}
if _, err := fmt.Fprintf(tw, text); err != nil {
return false, err
}
}
if err := tw.WriteHeader(&tar.Header{
Name: hdr.Name,
Mode: int64(os.FileMode(0755).Perm()),
Size: hdr.Size,
Typeflag: tar.TypeReg,
ModTime: hdr.ModTime,
}); err != nil {
return false, err
}
w = tw
closeFn = func() error {
for _, link := range target.LinkTo {
if err := tw.WriteHeader(&tar.Header{
Name: link,
Mode: int64(os.FileMode(0755).Perm()),
Size: 0,
Typeflag: tar.TypeLink,
ModTime: hdr.ModTime,
Linkname: hdr.Name,
}); err != nil {
return err
}
}
if err := tw.Close(); err != nil {
return err
}
return gw.Close()
}
}
}
// copy the input to disk
if target.InjectReleaseImage {
var matched bool
matched, err = copyAndReplaceReleaseImage(w, r, 4*1024, exactReleaseImage)
if !matched {
fmt.Fprintf(o.ErrOut, "warning: Unable to replace release image location into %s, installer will not be locked to the correct image\n", target.TargetName)
}
} else {
_, err = io.Copy(w, r)
}
if err != nil {
closeFn()
f.Close()
os.Remove(f.Name())
return false, err
}
// ensure the file is written to disk
if err := closeFn(); err != nil {
return false, err
}
if err := bw.Flush(); err != nil {
return false, err
}
if err := f.Close(); err != nil {
return false, err
}
if err := os.Chtimes(f.Name(), hdr.ModTime, hdr.ModTime); err != nil {
klog.V(2).Infof("Unable to set extracted file modification time: %v", err)
}
func() {
extractLock.Lock()
defer extractLock.Unlock()
delete(targetsByName, layer.Mapping.Name)
if hash != nil {
hashByTargetName[layer.Mapping.To] = hex.EncodeToString(hash.Sum(nil))
}
}()
return false, nil
}
if err := opts.Run(); err != nil {
return err
}
if willArchive {
buf := &bytes.Buffer{}
fmt.Fprintf(buf, heredoc.Doc(`
Client tools for OpenShift
--------------------------
These archives contain the client tooling for [OpenShift](https://docs.openshift.com).
To verify the contents of this directory, use the 'gpg' and 'shasum' tools to
ensure the archives you have downloaded match those published from this location.
The openshift-install binary has been preconfigured to install the following release:
---
`))
if err := describeReleaseInfo(buf, release, false, true, false); err != nil {
return err
}
filename := "release.txt"
if err := ioutil.WriteFile(filepath.Join(dir, filename), buf.Bytes(), 0644); err != nil {
return err
}
hash := hashFn()
hash.Write(buf.Bytes())
hashByTargetName[filename] = hex.EncodeToString(hash.Sum(nil))
}
// write a checksum of the tar files to disk as sha256sum.txt.asc
if len(hashByTargetName) > 0 {
var keys []string
for k := range hashByTargetName {
keys = append(keys, k)
}
sort.Strings(keys)
var lines []string
for _, k := range keys {
hash := hashByTargetName[k]
lines = append(lines, fmt.Sprintf("%s %s", hash, filepath.Base(k)))
}
// ensure a trailing newline
if len(lines[len(lines)-1]) != 0 {
lines = append(lines, "")
}
// write the content manifest
data := []byte(strings.Join(lines, "\n"))
filename := "sha256sum.txt"
if err := ioutil.WriteFile(filepath.Join(dir, filename), data, 0644); err != nil {
return fmt.Errorf("unable to write checksum file: %v", err)
}
// sign the content manifest
if signer != nil {
buf := &bytes.Buffer{}
if err := openpgp.ArmoredDetachSign(buf, signer, bytes.NewBuffer(data), nil); err != nil {
return fmt.Errorf("unable to sign the sha256sum.txt file: %v", err)
}
if err := ioutil.WriteFile(filepath.Join(dir, filename+".asc"), buf.Bytes(), 0644); err != nil {
return fmt.Errorf("unable to write signed manifest: %v", err)
}
}
}
// if we did not process some targets, report that to the user and error if necessary
if len(targetsByName) > 0 {
var missing []string
for _, target := range targetsByName {
missing = append(missing, target.Mapping.From)
}
sort.Strings(missing)
if len(missing) == 1 {
return fmt.Errorf("image did not contain %s", missing[0])
}
return fmt.Errorf("unable to find multiple files: %s", strings.Join(missing, ", "))
}
return nil
}
const (
// installerReplacement is the location within the installer binary that we can insert our
// release payload string
installerReplacement = "\x00_RELEASE_IMAGE_LOCATION_\x00XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\x00"
)
// copyAndReplaceReleaseImage performs a targeted replacement for binaries that contain a special marker string
// as a constant, replacing the marker with releaseImage and a NUL terminating byte. It returns true if the
// replacement was performed.
func copyAndReplaceReleaseImage(w io.Writer, r io.Reader, bufferSize int, releaseImage string) (bool, error) {
if len(releaseImage)+1 > len(installerReplacement) {
return false, fmt.Errorf("the release image pull spec is longer than the maximum replacement length for the installer binary")
}
if bufferSize < len(installerReplacement) {
return false, fmt.Errorf("the buffer size must be greater than %d bytes", len(installerReplacement))
}
match := []byte(installerReplacement[:len(releaseImage)+1])
offset := 0
max := bufferSize
buf := make([]byte, max+offset)
matched := false
for {
n, err := io.ReadFull(r, buf[offset:])
// search in the buffer for the expected match
end := offset + n
if n > 0 {
index := bytes.Index(buf[:end], match)
if index != -1 {
klog.V(2).Infof("Found match at %d (len=%d, offset=%d, n=%d)", index, len(buf), offset, n)
// the replacement starts at the beginning of the match, contains the release string and a terminating NUL byte
copy(buf[index:index+len(releaseImage)], []byte(releaseImage))
buf[index+len(releaseImage)] = 0x00
matched = true
}
}
// write everything that we have already searched (excluding the end of the buffer that will
// be checked next pass)
nextOffset := end - len(installerReplacement)
if nextOffset < 0 || matched {
nextOffset = 0
}
_, wErr := w.Write(buf[:end-nextOffset])
if wErr != nil {
return matched, wErr
}
if err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
return matched, nil
}
return matched, err
}
// once we complete a single match, we can copy the rest of the file without processing
if matched {
_, err := io.Copy(w, r)
return matched, err
}
// ensure the beginning of the buffer matches the end of the current buffer so that we
// can search for matches that span buffers
copy(buf[:nextOffset], buf[end-nextOffset:end])
offset = nextOffset
}
}