/
credentials.go
125 lines (101 loc) · 3.55 KB
/
credentials.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
package ocm
import (
"context"
"fmt"
"net/url"
"github.com/open-component-model/ocm-controller/api/v1alpha1"
"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
credconfig "github.com/open-component-model/ocm/pkg/contexts/credentials/config"
"github.com/open-component-model/ocm/pkg/contexts/credentials/repositories/dockerconfig"
"github.com/open-component-model/ocm/pkg/contexts/ocm"
"github.com/open-component-model/ocm/pkg/runtime"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// ConfigureCredentials takes a repository url and secret ref and configures access to an OCI repository.
func ConfigureCredentials(ctx context.Context, ocmCtx ocm.Context, c client.Client, repositoryURL, secretRef, namespace string) error {
var secret corev1.Secret
secretKey := client.ObjectKey{
Namespace: namespace,
Name: secretRef,
}
if err := c.Get(ctx, secretKey, &secret); err != nil {
return fmt.Errorf("failed to locate secret: %w", err)
}
if dockerConfigBytes, ok := secret.Data[corev1.DockerConfigJsonKey]; ok {
if err := configureDockerConfigCredentials(ocmCtx, dockerConfigBytes); err != nil {
return err
}
return nil
}
if ocmConfigBytes, ok := secret.Data[v1alpha1.OCMCredentialConfigKey]; ok {
if err := configureOcmConfigCredentials(ocmCtx, ocmConfigBytes, secret); err != nil {
return err
}
return nil
}
// proceed to configure a credential context using username and password
// create the consumer id for credentials
consumerID, err := getConsumerIdentityForRepository(repositoryURL)
if err != nil {
return err
}
// fetch the credentials for the component storage
creds, err := getCredentialsForRepository(ctx, c, namespace, secretRef)
if err != nil {
return err
}
ocmCtx.CredentialsContext().SetCredentialsForConsumer(consumerID, creds)
return nil
}
func configureOcmConfigCredentials(ocmCtx ocm.Context, ocmConfigBytes []byte, secret corev1.Secret) error {
cfg, err := ocmCtx.ConfigContext().GetConfigForData(ocmConfigBytes, runtime.DefaultYAMLEncoding)
if err != nil {
return err
}
if cfg.GetKind() == credconfig.ConfigType {
if err := ocmCtx.ConfigContext().ApplyConfig(cfg, fmt.Sprintf("ocm config secret: %s/%s", secret.Namespace, secret.Name)); err != nil {
return err
}
}
return nil
}
func configureDockerConfigCredentials(ocmCtx ocm.Context, dockerConfigBytes []byte) error {
spec := dockerconfig.NewRepositorySpecForConfig(dockerConfigBytes, true)
if _, err := ocmCtx.CredentialsContext().RepositoryForSpec(spec); err != nil {
return fmt.Errorf("cannot create credentials from secret: %w", err)
}
return nil
}
func getConsumerIdentityForRepository(repositoryURL string) (credentials.ConsumerIdentity, error) {
regURL, err := url.Parse(repositoryURL)
if err != nil {
return nil, err
}
if regURL.Scheme == "" {
regURL, err = url.Parse(fmt.Sprintf("oci://%s", repositoryURL))
if err != nil {
return nil, err
}
}
return credentials.ConsumerIdentity{
"type": "OCIRegistry",
"hostname": regURL.Host,
}, nil
}
func getCredentialsForRepository(ctx context.Context, c client.Client, namespace string, secretName string) (credentials.Credentials, error) {
var secret corev1.Secret
secretKey := client.ObjectKey{
Namespace: namespace,
Name: secretName,
}
if err := c.Get(ctx, secretKey, &secret); err != nil {
return nil, err
}
props := make(common.Properties)
for key, value := range secret.Data {
props.SetNonEmptyValue(key, string(value))
}
return credentials.NewCredentials(props), nil
}