crossplane

Форк
0
142 строки · 3.7 Кб
1
/*
2
Copyright 2023 The Crossplane Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
// Package credhelper provides a docker credential helper for the Upbound marketplace.
18
package credhelper
19

20
import (
21
	"strings"
22

23
	"github.com/docker/docker-credential-helpers/credentials"
24

25
	"github.com/crossplane/crossplane-runtime/pkg/errors"
26
	"github.com/crossplane/crossplane-runtime/pkg/logging"
27

28
	"github.com/crossplane/crossplane/internal/xpkg/upbound/config"
29
)
30

31
const (
32
	errUnimplemented     = "operation is not implemented"
33
	errInitializeSource  = "unable to initialize source"
34
	errExtractConfig     = "unable to extract config"
35
	errGetDefaultProfile = "unable to get default profile in config"
36
	errGetProfile        = "unable to get specified profile in config"
37
	errUnsupportedDomain = "supplied server URL is not supported"
38
)
39

40
const (
41
	defaultDockerUser = "_token"
42
)
43

44
// Helper is a docker credential helper for Upbound.
45
type Helper struct {
46
	log logging.Logger
47

48
	profile string
49
	domain  string
50
	src     config.Source
51
}
52

53
// Opt sets a helper option.
54
type Opt func(h *Helper)
55

56
// WithLogger sets the helper logger.
57
func WithLogger(l logging.Logger) Opt {
58
	return func(h *Helper) {
59
		h.log = l
60
	}
61
}
62

63
// WithDomain sets the allowed registry domain.
64
func WithDomain(d string) Opt {
65
	return func(h *Helper) {
66
		h.domain = d
67
	}
68
}
69

70
// WithProfile sets the helper profile.
71
func WithProfile(p string) Opt {
72
	return func(h *Helper) {
73
		h.profile = p
74
	}
75
}
76

77
// WithSource sets the source for the helper config.
78
func WithSource(src config.Source) Opt {
79
	return func(h *Helper) {
80
		h.src = src
81
	}
82
}
83

84
// New constructs a new Docker credential helper.
85
func New(opts ...Opt) *Helper {
86
	h := &Helper{
87
		log: logging.NewNopLogger(),
88
		src: config.NewFSSource(),
89
	}
90

91
	for _, o := range opts {
92
		o(h)
93
	}
94

95
	return h
96
}
97

98
// Add adds the supplied credentials.
99
func (h *Helper) Add(_ *credentials.Credentials) error {
100
	return errors.New(errUnimplemented)
101
}
102

103
// Delete deletes credentials for the supplied server.
104
func (h *Helper) Delete(_ string) error {
105
	return errors.New(errUnimplemented)
106
}
107

108
// List lists all the configured credentials.
109
func (h *Helper) List() (map[string]string, error) {
110
	return nil, errors.New(errUnimplemented)
111
}
112

113
// Get gets credentials for the supplied server.
114
func (h *Helper) Get(serverURL string) (string, string, error) {
115
	if !strings.Contains(serverURL, h.domain) {
116
		h.log.Debug("Supplied server URL is not supported by this credentials helper", "serverURL", serverURL, "domain", h.domain)
117
		return "", "", errors.New(errUnsupportedDomain)
118
	}
119
	h.log.Debug("Getting credentials for server", "serverURL", serverURL)
120
	if err := h.src.Initialize(); err != nil {
121
		return "", "", errors.Wrap(err, errInitializeSource)
122
	}
123
	conf, err := config.Extract(h.src)
124
	if err != nil {
125
		return "", "", errors.Wrap(err, errExtractConfig)
126
	}
127
	var p config.Profile
128
	if h.profile == "" {
129
		h.log.Debug("No profile specified, using default profile")
130
		_, p, err = conf.GetDefaultUpboundProfile()
131
		if err != nil {
132
			return "", "", errors.Wrap(err, errGetDefaultProfile)
133
		}
134
	} else {
135
		h.log.Debug("Using specified profile", "profile", h.profile)
136
		p, err = conf.GetUpboundProfile(h.profile)
137
		if err != nil {
138
			return "", "", errors.Wrap(err, errGetProfile)
139
		}
140
	}
141
	return defaultDockerUser, p.Session, nil
142
}
143

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.