-
Notifications
You must be signed in to change notification settings - Fork 19
/
type.go
96 lines (78 loc) · 3.01 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
package gardenerconfig
import (
"fmt"
"github.com/mandelsoft/goutils/generics"
"github.com/open-component-model/ocm/pkg/contexts/credentials/cpi"
"github.com/open-component-model/ocm/pkg/contexts/credentials/internal"
gardenercfgcpi "github.com/open-component-model/ocm/pkg/contexts/credentials/repositories/gardenerconfig/cpi"
"github.com/open-component-model/ocm/pkg/contexts/credentials/repositories/gardenerconfig/identity"
"github.com/open-component-model/ocm/pkg/runtime"
"github.com/open-component-model/ocm/pkg/utils"
)
const (
Type = "GardenerConfig"
TypeV1 = Type + runtime.VersionSeparator + "v1"
)
func init() {
cpi.RegisterRepositoryType(cpi.NewRepositoryType[*RepositorySpec](Type))
cpi.RegisterRepositoryType(cpi.NewRepositoryType[*RepositorySpec](TypeV1))
}
// RepositorySpec describes a secret server based credential repository interface.
type RepositorySpec struct {
runtime.ObjectVersionedType `json:",inline"`
URL string `json:"url"`
ConfigType gardenercfgcpi.ConfigType `json:"configType"`
Cipher Cipher `json:"cipher"`
PropagateConsumerIdentity *bool `json:"propagateConsumerIdentity,omitempty"`
}
var _ cpi.ConsumerIdentityProvider = (*RepositorySpec)(nil)
// NewRepositorySpec creates a new memory RepositorySpec.
func NewRepositorySpec(url string, configType gardenercfgcpi.ConfigType, cipher Cipher, propagateConsumerIdentity ...bool) *RepositorySpec {
return &RepositorySpec{
ObjectVersionedType: runtime.NewVersionedTypedObject(Type),
URL: url,
ConfigType: configType,
Cipher: cipher,
PropagateConsumerIdentity: generics.Pointer(utils.OptionalDefaultedBool(true, propagateConsumerIdentity...)),
}
}
func (a *RepositorySpec) GetType() string {
return Type
}
func (a *RepositorySpec) Repository(ctx cpi.Context, creds cpi.Credentials) (cpi.Repository, error) {
r := ctx.GetAttributes().GetOrCreateAttribute(ATTR_REPOS, newRepositories)
repos, ok := r.(*Repositories)
if !ok {
return nil, fmt.Errorf("failed to assert type %T to Responsitories", r)
}
key, err := getKey(ctx, a.URL)
if err != nil {
return nil, fmt.Errorf("unable to get key from context: %w", err)
}
return repos.GetRepository(ctx, a.URL, a.ConfigType, a.Cipher, key, utils.AsBool(a.PropagateConsumerIdentity, true))
}
func (a *RepositorySpec) GetConsumerId(uctx ...internal.UsageContext) internal.ConsumerIdentity {
id, err := identity.GetConsumerId(a.URL)
if err != nil {
return nil
}
return id
}
func (a *RepositorySpec) GetIdentityMatcher() string {
return identity.CONSUMER_TYPE
}
func getKey(cctx cpi.Context, configURL string) ([]byte, error) {
id, err := identity.GetConsumerId(configURL)
if err != nil {
return nil, err
}
creds, err := cpi.CredentialsForConsumer(cctx, id)
if err != nil {
return nil, err
}
var key string
if creds != nil {
key = creds.GetProperty(identity.ATTR_KEY)
}
return []byte(key), nil
}