-
Notifications
You must be signed in to change notification settings - Fork 233
/
pullsecret.go
189 lines (164 loc) · 4.8 KB
/
pullsecret.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
package cluster
import (
"bytes"
"compress/gzip"
"encoding/base64"
"errors"
"fmt"
"io"
"os"
"strings"
crcConfig "github.com/crc-org/crc/v2/pkg/crc/config"
"github.com/crc-org/crc/v2/pkg/crc/constants"
"github.com/crc-org/crc/v2/pkg/crc/logging"
"github.com/crc-org/crc/v2/pkg/crc/preset"
"github.com/crc-org/crc/v2/pkg/crc/validation"
crcTerminal "github.com/crc-org/crc/v2/pkg/os/terminal"
"github.com/AlecAivazis/survey/v2"
"github.com/zalando/go-keyring"
)
const (
keyringService = "crc"
keyringUser = "compressed-pull-secret"
)
type PullSecretLoader interface {
Value() (string, error)
}
type interactivePullSecretLoader struct {
nonInteractivePullSecretLoader *nonInteractivePullSecretLoader
}
func NewInteractivePullSecretLoader(config crcConfig.Storage) PullSecretLoader {
return &PullSecretMemoizer{
Getter: &interactivePullSecretLoader{
nonInteractivePullSecretLoader: &nonInteractivePullSecretLoader{
config: config,
},
},
}
}
func (loader *interactivePullSecretLoader) Value() (string, error) {
fromNonInteractive, err := loader.nonInteractivePullSecretLoader.Value()
if err == nil {
return fromNonInteractive, nil
}
pullSecret, err := promptUserForSecret()
if err != nil {
return "", err
}
if err := StoreInKeyring(pullSecret); err != nil {
logging.Warnf("Cannot add pull secret to keyring: %v", err)
}
return pullSecret, nil
}
type nonInteractivePullSecretLoader struct {
config crcConfig.Storage
path string
}
func NewNonInteractivePullSecretLoader(config crcConfig.Storage, path string) PullSecretLoader {
return &PullSecretMemoizer{
Getter: &nonInteractivePullSecretLoader{
config: config,
path: path,
},
}
}
func (loader *nonInteractivePullSecretLoader) Value() (string, error) {
// If crc is built from an OKD bundle or podman bundle is used, then use the fake pull secret in constants.
if crcConfig.GetPreset(loader.config) == preset.OKD {
return constants.OkdPullSecret, nil
}
if loader.path != "" {
fromPath, err := loadFile(loader.path)
if err == nil {
logging.Debugf("Using secret from path %q", loader.path)
return fromPath, nil
}
logging.Debugf("Cannot load secret from path %q: %v", loader.path, err)
}
fromConfig, err := loadFile(loader.config.Get(crcConfig.PullSecretFile).AsString())
if err == nil {
logging.Debugf("Using secret from configuration")
return fromConfig, nil
}
logging.Debugf("Cannot load secret from configuration: %v", err)
fromKeyring, err := loadFromKeyring()
if err == nil {
logging.Debugf("Using secret from keyring")
return fromKeyring, nil
}
logging.Debugf("Cannot load secret from keyring: %v", err)
return "", fmt.Errorf("unable to load pull secret from path %q or from configuration", loader.path)
}
func loadFromKeyring() (string, error) {
pullsecret, err := keyring.Get(keyringService, keyringUser)
if err != nil {
return "", err
}
decoded, err := base64.StdEncoding.DecodeString(pullsecret)
if err != nil {
return "", err
}
decompressor, err := gzip.NewReader(bytes.NewReader(decoded))
if err != nil {
return "", err
}
var b bytes.Buffer
// #nosec G110
if _, err := io.Copy(&b, decompressor); err != nil {
return "", err
}
if err := decompressor.Close(); err != nil {
return "", err
}
return b.String(), validation.ImagePullSecret(b.String())
}
func StoreInKeyring(pullSecret string) error {
var b bytes.Buffer
if err := validation.ImagePullSecret(pullSecret); err != nil {
return err
}
compressor := gzip.NewWriter(&b)
if _, err := compressor.Write([]byte(pullSecret)); err != nil {
return err
}
if err := compressor.Close(); err != nil {
return err
}
return keyring.Set(keyringService, keyringUser, base64.StdEncoding.EncodeToString(b.Bytes()))
}
func ForgetPullSecret() error {
_ = keyring.Delete(keyringService, keyringUser)
return nil
}
func loadFile(path string) (string, error) {
if path == "" {
return "", errors.New("empty path")
}
data, err := os.ReadFile(path)
if err != nil {
return "", err
}
pullsecret := strings.TrimSpace(string(data))
return pullsecret, validation.ImagePullSecret(pullsecret)
}
const helpMessage = `CRC requires a pull secret to download content from Red Hat.
You can copy it from the Pull Secret section of %s.
`
// promptUserForSecret can be used for any kind of secret like image pull
// secret or for password.
func promptUserForSecret() (string, error) {
if !crcTerminal.IsRunningInTerminal() {
return "", errors.New("cannot ask for secret, crc not launched by a terminal")
}
fmt.Printf(helpMessage, constants.CrcLandingPageURL)
var secret string
prompt := &survey.Password{
Message: "Please enter the pull secret",
}
if err := survey.AskOne(prompt, &secret, survey.WithValidator(func(ans interface{}) error {
return validation.ImagePullSecret(ans.(string))
})); err != nil {
return "", err
}
return secret, nil
}