Skip to content

Commit 457e342

Browse files
authored
fix: Change receiver name for Remote (#1727)
Signed-off-by: Terry Howe <[email protected]>
1 parent dd10541 commit 457e342

File tree

1 file changed

+78
-78
lines changed

1 file changed

+78
-78
lines changed

cmd/oras/internal/option/remote.go

Lines changed: 78 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,15 @@ type Remote struct {
8080
}
8181

8282
// EnableDistributionSpecFlag set distribution specification flag as applicable.
83-
func (opts *Remote) EnableDistributionSpecFlag() {
84-
opts.applyDistributionSpec = true
83+
func (remo *Remote) EnableDistributionSpecFlag() {
84+
remo.applyDistributionSpec = true
8585
}
8686

8787
// ApplyFlags applies flags to a command flag set.
88-
func (opts *Remote) ApplyFlags(fs *pflag.FlagSet) {
89-
opts.ApplyFlagsWithPrefix(fs, "", "")
90-
fs.BoolVar(&opts.secretFromStdin, passwordFromStdinFlag, false, "read password from stdin")
91-
fs.BoolVar(&opts.secretFromStdin, identityTokenFromStdinFlag, false, "read identity token from stdin")
88+
func (remo *Remote) ApplyFlags(fs *pflag.FlagSet) {
89+
remo.ApplyFlagsWithPrefix(fs, "", "")
90+
fs.BoolVar(&remo.secretFromStdin, passwordFromStdinFlag, false, "read password from stdin")
91+
fs.BoolVar(&remo.secretFromStdin, identityTokenFromStdinFlag, false, "read identity token from stdin")
9292
}
9393

9494
func applyPrefix(prefix, description string) (flagPrefix, notePrefix string) {
@@ -100,7 +100,7 @@ func applyPrefix(prefix, description string) (flagPrefix, notePrefix string) {
100100

101101
// ApplyFlagsWithPrefix applies flags to a command flag set with a prefix string.
102102
// Commonly used for non-unary remote targets.
103-
func (opts *Remote) ApplyFlagsWithPrefix(fs *pflag.FlagSet, prefix, description string) {
103+
func (remo *Remote) ApplyFlagsWithPrefix(fs *pflag.FlagSet, prefix, description string) {
104104
var (
105105
shortUser string
106106
shortPassword string
@@ -111,26 +111,26 @@ func (opts *Remote) ApplyFlagsWithPrefix(fs *pflag.FlagSet, prefix, description
111111
shortUser, shortPassword = "u", "p"
112112
shortHeader = "H"
113113
}
114-
opts.flagPrefix, notePrefix = applyPrefix(prefix, description)
114+
remo.flagPrefix, notePrefix = applyPrefix(prefix, description)
115115

116-
if opts.applyDistributionSpec {
117-
opts.DistributionSpec.ApplyFlagsWithPrefix(fs, prefix, description)
116+
if remo.applyDistributionSpec {
117+
remo.DistributionSpec.ApplyFlagsWithPrefix(fs, prefix, description)
118118
}
119-
fs.StringVarP(&opts.Username, opts.flagPrefix+usernameFlag, shortUser, "", notePrefix+"registry username")
120-
fs.StringVarP(&opts.Secret, opts.flagPrefix+passwordFlag, shortPassword, "", notePrefix+"registry password or identity token")
121-
fs.StringVar(&opts.Secret, opts.flagPrefix+identityTokenFlag, "", notePrefix+"registry identity token")
122-
fs.BoolVar(&opts.Insecure, opts.flagPrefix+"insecure", false, "allow connections to "+notePrefix+"SSL registry without certs")
123-
plainHTTPFlagName := opts.flagPrefix + "plain-http"
119+
fs.StringVarP(&remo.Username, remo.flagPrefix+usernameFlag, shortUser, "", notePrefix+"registry username")
120+
fs.StringVarP(&remo.Secret, remo.flagPrefix+passwordFlag, shortPassword, "", notePrefix+"registry password or identity token")
121+
fs.StringVar(&remo.Secret, remo.flagPrefix+identityTokenFlag, "", notePrefix+"registry identity token")
122+
fs.BoolVar(&remo.Insecure, remo.flagPrefix+"insecure", false, "allow connections to "+notePrefix+"SSL registry without certs")
123+
plainHTTPFlagName := remo.flagPrefix + "plain-http"
124124
plainHTTP := fs.Bool(plainHTTPFlagName, false, "allow insecure connections to "+notePrefix+"registry without SSL check")
125-
opts.plainHTTP = func() (bool, bool) {
125+
remo.plainHTTP = func() (bool, bool) {
126126
return *plainHTTP, fs.Changed(plainHTTPFlagName)
127127
}
128-
fs.StringVar(&opts.CACertFilePath, opts.flagPrefix+caFileFlag, "", "server certificate authority file for the remote "+notePrefix+"registry")
129-
fs.StringVarP(&opts.CertFilePath, opts.flagPrefix+certFileFlag, "", "", "client certificate file for the remote "+notePrefix+"registry")
130-
fs.StringVarP(&opts.KeyFilePath, opts.flagPrefix+keyFileFlag, "", "", "client private key file for the remote "+notePrefix+"registry")
131-
fs.StringArrayVar(&opts.resolveFlag, opts.flagPrefix+"resolve", nil, "customized DNS for "+notePrefix+"registry, formatted in `host:port:address[:address_port]`")
132-
fs.StringArrayVar(&opts.Configs, opts.flagPrefix+"registry-config", nil, "`path` of the authentication file for "+notePrefix+"registry")
133-
fs.StringArrayVarP(&opts.headerFlags, opts.flagPrefix+"header", shortHeader, nil, "add custom headers to "+notePrefix+"requests")
128+
fs.StringVar(&remo.CACertFilePath, remo.flagPrefix+caFileFlag, "", "server certificate authority file for the remote "+notePrefix+"registry")
129+
fs.StringVarP(&remo.CertFilePath, remo.flagPrefix+certFileFlag, "", "", "client certificate file for the remote "+notePrefix+"registry")
130+
fs.StringVarP(&remo.KeyFilePath, remo.flagPrefix+keyFileFlag, "", "", "client private key file for the remote "+notePrefix+"registry")
131+
fs.StringArrayVar(&remo.resolveFlag, remo.flagPrefix+"resolve", nil, "customized DNS for "+notePrefix+"registry, formatted in `host:port:address[:address_port]`")
132+
fs.StringArrayVar(&remo.Configs, remo.flagPrefix+"registry-config", nil, "`path` of the authentication file for "+notePrefix+"registry")
133+
fs.StringArrayVarP(&remo.headerFlags, remo.flagPrefix+"header", shortHeader, nil, "add custom headers to "+notePrefix+"requests")
134134
}
135135

136136
// CheckStdinConflict checks if PasswordFromStdin or IdentityTokenFromStdin of a
@@ -146,10 +146,10 @@ func CheckStdinConflict(flags *pflag.FlagSet) error {
146146
}
147147

148148
// Parse tries to read password with optional cmd prompt.
149-
func (opts *Remote) Parse(cmd *cobra.Command) error {
150-
usernameAndIdTokenFlags := []string{opts.flagPrefix + usernameFlag, opts.flagPrefix + identityTokenFlag}
151-
passwordAndIdTokenFlags := []string{opts.flagPrefix + passwordFlag, opts.flagPrefix + identityTokenFlag}
152-
certFileAndKeyFileFlags := []string{opts.flagPrefix + certFileFlag, opts.flagPrefix + keyFileFlag}
149+
func (remo *Remote) Parse(cmd *cobra.Command) error {
150+
usernameAndIdTokenFlags := []string{remo.flagPrefix + usernameFlag, remo.flagPrefix + identityTokenFlag}
151+
passwordAndIdTokenFlags := []string{remo.flagPrefix + passwordFlag, remo.flagPrefix + identityTokenFlag}
152+
certFileAndKeyFileFlags := []string{remo.flagPrefix + certFileFlag, remo.flagPrefix + keyFileFlag}
153153
if cmd.Flags().Lookup(identityTokenFromStdinFlag) != nil {
154154
usernameAndIdTokenFlags = append(usernameAndIdTokenFlags, identityTokenFromStdinFlag)
155155
passwordAndIdTokenFlags = append(passwordAndIdTokenFlags, identityTokenFromStdinFlag)
@@ -163,45 +163,45 @@ func (opts *Remote) Parse(cmd *cobra.Command) error {
163163
if err := oerrors.CheckMutuallyExclusiveFlags(cmd.Flags(), passwordAndIdTokenFlags...); err != nil {
164164
return err
165165
}
166-
if err := opts.parseCustomHeaders(); err != nil {
166+
if err := remo.parseCustomHeaders(); err != nil {
167167
return err
168168
}
169169
if err := oerrors.CheckRequiredTogetherFlags(cmd.Flags(), certFileAndKeyFileFlags...); err != nil {
170170
return err
171171
}
172-
return opts.readSecret(cmd)
172+
return remo.readSecret(cmd)
173173
}
174174

175175
// readSecret tries to read password or identity token with
176176
// optional cmd prompt.
177-
func (opts *Remote) readSecret(cmd *cobra.Command) (err error) {
177+
func (remo *Remote) readSecret(cmd *cobra.Command) (err error) {
178178
if cmd.Flags().Changed(identityTokenFlag) {
179179
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "WARNING! Using --identity-token via the CLI is insecure. Use --identity-token-stdin.")
180180
} else if cmd.Flags().Changed(passwordFlag) {
181181
_, _ = fmt.Fprintln(cmd.ErrOrStderr(), "WARNING! Using --password via the CLI is insecure. Use --password-stdin.")
182-
} else if opts.secretFromStdin {
182+
} else if remo.secretFromStdin {
183183
// Prompt for credential
184184
secret, err := io.ReadAll(os.Stdin)
185185
if err != nil {
186186
return err
187187
}
188-
opts.Secret = strings.TrimSuffix(string(secret), "\n")
189-
opts.Secret = strings.TrimSuffix(opts.Secret, "\r")
188+
remo.Secret = strings.TrimSuffix(string(secret), "\n")
189+
remo.Secret = strings.TrimSuffix(remo.Secret, "\r")
190190
}
191191
return nil
192192
}
193193

194194
// parseResolve parses resolve flag.
195-
func (opts *Remote) parseResolve(baseDial onet.DialFunc) (onet.DialFunc, error) {
196-
if len(opts.resolveFlag) == 0 {
195+
func (remo *Remote) parseResolve(baseDial onet.DialFunc) (onet.DialFunc, error) {
196+
if len(remo.resolveFlag) == 0 {
197197
return baseDial, nil
198198
}
199199

200200
formatError := func(param, message string) error {
201201
return fmt.Errorf("failed to parse resolve flag %q: %s", param, message)
202202
}
203203
var dialer onet.Dialer
204-
for _, r := range opts.resolveFlag {
204+
for _, r := range remo.resolveFlag {
205205
parts := strings.SplitN(r, ":", 4)
206206
length := len(parts)
207207
if length < 3 {
@@ -231,19 +231,19 @@ func (opts *Remote) parseResolve(baseDial onet.DialFunc) (onet.DialFunc, error)
231231
}
232232

233233
// tlsConfig assembles the tls config.
234-
func (opts *Remote) tlsConfig() (*tls.Config, error) {
234+
func (remo *Remote) tlsConfig() (*tls.Config, error) {
235235
config := &tls.Config{
236-
InsecureSkipVerify: opts.Insecure,
236+
InsecureSkipVerify: remo.Insecure,
237237
}
238-
if opts.CACertFilePath != "" {
238+
if remo.CACertFilePath != "" {
239239
var err error
240-
config.RootCAs, err = crypto.LoadCertPool(opts.CACertFilePath)
240+
config.RootCAs, err = crypto.LoadCertPool(remo.CACertFilePath)
241241
if err != nil {
242242
return nil, err
243243
}
244244
}
245-
if opts.CertFilePath != "" && opts.KeyFilePath != "" {
246-
cert, err := tls.LoadX509KeyPair(opts.CertFilePath, opts.KeyFilePath)
245+
if remo.CertFilePath != "" && remo.KeyFilePath != "" {
246+
cert, err := tls.LoadX509KeyPair(remo.CertFilePath, remo.KeyFilePath)
247247
if err != nil {
248248
return nil, err
249249
}
@@ -253,14 +253,14 @@ func (opts *Remote) tlsConfig() (*tls.Config, error) {
253253
}
254254

255255
// authClient assembles a oras auth client.
256-
func (opts *Remote) authClient(registry string, debug bool) (client *auth.Client, err error) {
257-
config, err := opts.tlsConfig()
256+
func (remo *Remote) authClient(registry string, debug bool) (client *auth.Client, err error) {
257+
config, err := remo.tlsConfig()
258258
if err != nil {
259259
return nil, err
260260
}
261261
baseTransport := http.DefaultTransport.(*http.Transport).Clone()
262262
baseTransport.TLSClientConfig = config
263-
dialContext, err := opts.parseResolve(baseTransport.DialContext)
263+
dialContext, err := remo.parseResolve(baseTransport.DialContext)
264264
if err != nil {
265265
return nil, err
266266
}
@@ -272,44 +272,44 @@ func (opts *Remote) authClient(registry string, debug bool) (client *auth.Client
272272
Transport: retry.NewTransport(baseTransport),
273273
},
274274
Cache: auth.NewCache(),
275-
Header: opts.headers,
275+
Header: remo.headers,
276276
}
277277
client.SetUserAgent("oras/" + version.GetVersion())
278278
if debug {
279279
client.Client.Transport = trace.NewTransport(client.Client.Transport)
280280
}
281281

282-
cred := opts.Credential()
282+
cred := remo.Credential()
283283
if cred != auth.EmptyCredential {
284284
client.Credential = func(ctx context.Context, s string) (auth.Credential, error) {
285285
return cred, nil
286286
}
287287
} else {
288288
var err error
289-
opts.store, err = credential.NewStore(opts.Configs...)
289+
remo.store, err = credential.NewStore(remo.Configs...)
290290
if err != nil {
291291
return nil, err
292292
}
293-
client.Credential = credentials.Credential(opts.store)
293+
client.Credential = credentials.Credential(remo.store)
294294
}
295295
return
296296
}
297297

298298
// ConfigPath returns the config path of the credential store.
299-
func (opts *Remote) ConfigPath() (string, error) {
300-
if opts.store == nil {
299+
func (remo *Remote) ConfigPath() (string, error) {
300+
if remo.store == nil {
301301
return "", errors.New("no credential store initialized")
302302
}
303-
if ds, ok := opts.store.(*credentials.DynamicStore); ok {
303+
if ds, ok := remo.store.(*credentials.DynamicStore); ok {
304304
return ds.ConfigPath(), nil
305305
}
306306
return "", errors.New("store doesn't support getting config path")
307307
}
308308

309-
func (opts *Remote) parseCustomHeaders() error {
310-
if len(opts.headerFlags) != 0 {
309+
func (remo *Remote) parseCustomHeaders() error {
310+
if len(remo.headerFlags) != 0 {
311311
headers := map[string][]string{}
312-
for _, h := range opts.headerFlags {
312+
for _, h := range remo.headerFlags {
313313
name, value, found := strings.Cut(h, ":")
314314
if !found || strings.TrimSpace(name) == "" {
315315
// In conformance to the RFC 2616 specification
@@ -318,24 +318,24 @@ func (opts *Remote) parseCustomHeaders() error {
318318
}
319319
headers[name] = append(headers[name], value)
320320
}
321-
opts.headers = headers
321+
remo.headers = headers
322322
}
323323
return nil
324324
}
325325

326326
// Credential returns a credential based on the remote options.
327-
func (opts *Remote) Credential() auth.Credential {
328-
return credential.Credential(opts.Username, opts.Secret)
327+
func (remo *Remote) Credential() auth.Credential {
328+
return credential.Credential(remo.Username, remo.Secret)
329329
}
330330

331-
func (opts *Remote) handleWarning(registry string, logger logrus.FieldLogger) func(warning remote.Warning) {
332-
if opts.warned == nil {
333-
opts.warned = make(map[string]*sync.Map)
331+
func (remo *Remote) handleWarning(registry string, logger logrus.FieldLogger) func(warning remote.Warning) {
332+
if remo.warned == nil {
333+
remo.warned = make(map[string]*sync.Map)
334334
}
335-
warned := opts.warned[registry]
335+
warned := remo.warned[registry]
336336
if warned == nil {
337337
warned = &sync.Map{}
338-
opts.warned[registry] = warned
338+
remo.warned[registry] = warned
339339
}
340340
logger = logger.WithField("registry", registry)
341341
return func(warning remote.Warning) {
@@ -346,22 +346,22 @@ func (opts *Remote) handleWarning(registry string, logger logrus.FieldLogger) fu
346346
}
347347

348348
// NewRegistry assembles a oras remote registry.
349-
func (opts *Remote) NewRegistry(registry string, common Common, logger logrus.FieldLogger) (reg *remote.Registry, err error) {
349+
func (remo *Remote) NewRegistry(registry string, common Common, logger logrus.FieldLogger) (reg *remote.Registry, err error) {
350350
reg, err = remote.NewRegistry(registry)
351351
if err != nil {
352352
return nil, err
353353
}
354354
registry = reg.Reference.Registry
355-
reg.PlainHTTP = opts.isPlainHttp(registry)
356-
reg.HandleWarning = opts.handleWarning(registry, logger)
357-
if reg.Client, err = opts.authClient(registry, common.Debug); err != nil {
355+
reg.PlainHTTP = remo.isPlainHttp(registry)
356+
reg.HandleWarning = remo.handleWarning(registry, logger)
357+
if reg.Client, err = remo.authClient(registry, common.Debug); err != nil {
358358
return nil, err
359359
}
360360
return
361361
}
362362

363363
// NewRepository assembles a oras remote repository.
364-
func (opts *Remote) NewRepository(reference string, common Common, logger logrus.FieldLogger) (repo *remote.Repository, err error) {
364+
func (remo *Remote) NewRepository(reference string, common Common, logger logrus.FieldLogger) (repo *remote.Repository, err error) {
365365
repo, err = remote.NewRepository(reference)
366366
if err != nil {
367367
if errors.Unwrap(err) == errdef.ErrInvalidReference {
@@ -370,23 +370,23 @@ func (opts *Remote) NewRepository(reference string, common Common, logger logrus
370370
return nil, err
371371
}
372372
registry := repo.Reference.Registry
373-
repo.PlainHTTP = opts.isPlainHttp(registry)
374-
repo.HandleWarning = opts.handleWarning(registry, logger)
375-
if repo.Client, err = opts.authClient(registry, common.Debug); err != nil {
373+
repo.PlainHTTP = remo.isPlainHttp(registry)
374+
repo.HandleWarning = remo.handleWarning(registry, logger)
375+
if repo.Client, err = remo.authClient(registry, common.Debug); err != nil {
376376
return nil, err
377377
}
378378
repo.SkipReferrersGC = true
379-
if opts.ReferrersAPI != nil {
380-
if err := repo.SetReferrersCapability(*opts.ReferrersAPI); err != nil {
379+
if remo.ReferrersAPI != nil {
380+
if err := repo.SetReferrersCapability(*remo.ReferrersAPI); err != nil {
381381
return nil, err
382382
}
383383
}
384384
return
385385
}
386386

387387
// isPlainHttp returns the plain http flag for a given registry.
388-
func (opts *Remote) isPlainHttp(registry string) bool {
389-
plainHTTP, enforced := opts.plainHTTP()
388+
func (remo *Remote) isPlainHttp(registry string) bool {
389+
plainHTTP, enforced := remo.plainHTTP()
390390
if enforced {
391391
return plainHTTP
392392
}
@@ -399,11 +399,11 @@ func (opts *Remote) isPlainHttp(registry string) bool {
399399
}
400400

401401
// Modify modifies error during cmd execution.
402-
func (opts *Remote) Modify(cmd *cobra.Command, err error) (error, bool) {
402+
func (remo *Remote) Modify(cmd *cobra.Command, err error) (error, bool) {
403403
var errResp *errcode.ErrorResponse
404404

405405
if errors.Is(err, auth.ErrBasicCredentialNotFound) {
406-
return opts.DecorateCredentialError(err), true
406+
return remo.DecorateCredentialError(err), true
407407
}
408408

409409
if errors.As(err, &errResp) {
@@ -416,9 +416,9 @@ func (opts *Remote) Modify(cmd *cobra.Command, err error) (error, bool) {
416416
}
417417

418418
// DecorateCredentialError decorate error with recommendation.
419-
func (opts *Remote) DecorateCredentialError(err error) *oerrors.Error {
419+
func (remo *Remote) DecorateCredentialError(err error) *oerrors.Error {
420420
configPath := " "
421-
if path, pathErr := opts.ConfigPath(); pathErr == nil {
421+
if path, pathErr := remo.ConfigPath(); pathErr == nil {
422422
configPath += fmt.Sprintf("at %q ", path)
423423
}
424424
return &oerrors.Error{

0 commit comments

Comments
 (0)