-
Notifications
You must be signed in to change notification settings - Fork 23
/
type.go
186 lines (160 loc) · 5.28 KB
/
type.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package config
import (
"fmt"
cfgcpi "github.com/open-component-model/ocm/pkg/contexts/config/cpi"
"github.com/open-component-model/ocm/pkg/contexts/credentials/cpi"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/runtime"
)
const (
ConfigType = "credentials" + cfgcpi.OCM_CONFIG_TYPE_SUFFIX
ConfigTypeV1 = ConfigType + runtime.VersionSeparator + "v1"
)
func init() {
cfgcpi.RegisterConfigType(cfgcpi.NewConfigType[*Config](ConfigType, usage))
cfgcpi.RegisterConfigType(cfgcpi.NewConfigType[*Config](ConfigTypeV1, usage))
}
// Config describes a configuration for the config context.
type Config struct {
runtime.ObjectVersionedType `json:",inline"`
// Consumers describe predefine logical cosumer specs mapped to credentials
// These will (potentially) be evaluated if access objects requiring credentials
// are provided by other modules (e.g. oci repo access) without
// specifying crednentials. Then this module can request credentials here by passing
// an appropriate consumer spec.
Consumers []ConsumerSpec `json:"consumers,omitempty"`
// Repositories describe preloaded credential repositories with potential credential chain
Repositories []RepositorySpec `json:"repositories,omitempty"`
// Aliases describe logical credential repositories mapped to implementing repositories
Aliases map[string]RepositorySpec `json:"aliases,omitempty"`
}
type ConsumerSpec struct {
Identity cpi.ConsumerIdentity `json:"identity"`
Credentials []cpi.GenericCredentialsSpec `json:"credentials"`
}
type RepositorySpec struct {
Repository cpi.GenericRepositorySpec `json:"repository"`
Credentials []cpi.GenericCredentialsSpec `json:"credentials,omitempty"`
}
// NewConfigSpec creates a new memory ConfigSpec.
func New() *Config {
return &Config{
ObjectVersionedType: runtime.NewVersionedTypedObject(ConfigType),
}
}
func (a *Config) GetType() string {
return ConfigType
}
func (a *Config) MapCredentialsChain(creds ...cpi.CredentialsSpec) ([]cpi.GenericCredentialsSpec, error) {
var cgens []cpi.GenericCredentialsSpec
for _, c := range creds {
cgen, err := cpi.ToGenericCredentialsSpec(c)
if err != nil {
return nil, err
}
cgens = append(cgens, *cgen)
}
return cgens, nil
}
func (a *Config) AddConsumer(id cpi.ConsumerIdentity, creds ...cpi.CredentialsSpec) error {
cgens, err := a.MapCredentialsChain(creds...)
if err != nil {
return fmt.Errorf("failed to map credentials chain: %w", err)
}
spec := &ConsumerSpec{
Identity: id,
Credentials: cgens,
}
a.Consumers = append(a.Consumers, *spec)
return nil
}
func (a *Config) MapRepository(repo cpi.RepositorySpec, creds ...cpi.CredentialsSpec) (*RepositorySpec, error) {
rgen, err := cpi.ToGenericRepositorySpec(repo)
if err != nil {
return nil, err
}
cgens, err := a.MapCredentialsChain(creds...)
if err != nil {
return nil, err
}
return &RepositorySpec{
Repository: *rgen,
Credentials: cgens,
}, nil
}
func (a *Config) AddRepository(repo cpi.RepositorySpec, creds ...cpi.CredentialsSpec) error {
spec, err := a.MapRepository(repo, creds...)
if err != nil {
return fmt.Errorf("failed to map repository: %w", err)
}
a.Repositories = append(a.Repositories, *spec)
return nil
}
func (a *Config) AddAlias(name string, repo cpi.RepositorySpec, creds ...cpi.CredentialsSpec) error {
spec, err := a.MapRepository(repo, creds...)
if err != nil {
return fmt.Errorf("failed to map repository: %w", err)
}
if a.Aliases == nil {
a.Aliases = map[string]RepositorySpec{}
}
a.Aliases[name] = *spec
return nil
}
func (a *Config) ApplyTo(ctx cfgcpi.Context, target interface{}) error {
list := errors.ErrListf("applying config")
t, ok := target.(cpi.Context)
if !ok {
return cfgcpi.ErrNoContext(ConfigType)
}
for _, e := range a.Consumers {
t.SetCredentialsForConsumer(e.Identity, CredentialsChain(e.Credentials...))
}
sub := errors.ErrListf("applying aliases")
for n, e := range a.Aliases {
sub.Add(t.SetAlias(n, &e.Repository, CredentialsChain(e.Credentials...)))
}
list.Add(sub.Result())
sub = errors.ErrListf("applying repositories")
for i, e := range a.Repositories {
_, err := t.RepositoryForSpec(&e.Repository, CredentialsChain(e.Credentials...))
sub.Add(errors.Wrapf(err, "repository entry %d", i))
}
list.Add(sub.Result())
return list.Result()
}
func CredentialsChain(creds ...cpi.GenericCredentialsSpec) cpi.CredentialsChain {
r := make([]cpi.CredentialsSource, len(creds))
for i := range creds {
r[i] = &creds[i]
}
return r
}
const usage = `
The config type <code>` + ConfigType + `</code> can be used to define a list
of arbitrary configuration specifications:
<pre>
type: ` + ConfigType + `
consumers:
- identity:
<name>: <value>
...
credentials:
- <credential specification>
... credential chain
repositories:
- repository: <repository specification>
credentials:
- <credential specification>
... credential chain
aliases:
<name>:
repository: <repository specification>
credentials:
- <credential specification>
... credential chain
</pre>
`