-
Notifications
You must be signed in to change notification settings - Fork 18
/
repository.go
143 lines (127 loc) · 3.42 KB
/
repository.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
package dockerconfig
import (
"bytes"
"fmt"
"os"
"strings"
"sync"
"github.com/docker/cli/cli/config"
"github.com/docker/cli/cli/config/configfile"
"github.com/docker/cli/cli/config/types"
"github.com/mandelsoft/goutils/errors"
"github.com/open-component-model/ocm/pkg/common"
"github.com/open-component-model/ocm/pkg/contexts/credentials/cpi"
"github.com/open-component-model/ocm/pkg/contexts/datacontext"
"github.com/open-component-model/ocm/pkg/runtimefinalizer"
"github.com/open-component-model/ocm/pkg/utils"
)
type Repository struct {
lock sync.RWMutex
ctx cpi.Context
propagate bool
path string
data []byte
config *configfile.ConfigFile
}
func NewRepository(ctx cpi.Context, path string, data []byte, propagate bool) (*Repository, error) {
r := &Repository{
ctx: datacontext.InternalContextRef(ctx),
propagate: propagate,
path: path,
data: data,
}
if path != "" && len(data) > 0 {
return nil, fmt.Errorf("only config file or config data possible")
}
err := r.Read(true)
return r, err
}
var _ cpi.Repository = &Repository{}
func (r *Repository) ExistsCredentials(name string) (bool, error) {
err := r.Read(false)
if err != nil {
return false, err
}
r.lock.RLock()
defer r.lock.RUnlock()
_, err = r.config.GetAuthConfig(name)
return err != nil, err
}
func (r *Repository) LookupCredentials(name string) (cpi.Credentials, error) {
err := r.Read(false)
if err != nil {
return nil, err
}
r.lock.RLock()
defer r.lock.RUnlock()
auth, err := r.config.GetAuthConfig(name)
if err != nil {
return nil, err
}
return newCredentials(auth), nil
}
func (r *Repository) WriteCredentials(name string, creds cpi.Credentials) (cpi.Credentials, error) {
return nil, errors.ErrNotSupported("write", "credentials", Type)
}
func (r *Repository) Read(force bool) error {
r.lock.Lock()
defer r.lock.Unlock()
if !force && r.config != nil {
return nil
}
var (
data []byte
err error
id runtimefinalizer.ObjectIdentity
)
if r.path != "" {
path, err := utils.ResolvePath(r.path)
if err != nil {
return errors.Wrapf(err, "cannot resolve path %q", r.path)
}
data, err = os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file '%s': %w", path, err)
}
id = cpi.ProviderIdentity(PROVIDER + "/" + path)
} else if len(r.data) > 0 {
data = r.data
id = runtimefinalizer.NewObjectIdentity(PROVIDER)
}
cfg, err := config.LoadFromReader(bytes.NewBuffer(data))
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}
if r.propagate {
r.ctx.RegisterConsumerProvider(id, &ConsumerProvider{cfg})
}
r.config = cfg
return nil
}
func newCredentials(auth types.AuthConfig) cpi.Credentials {
props := common.Properties{
cpi.ATTR_USERNAME: norm(auth.Username),
cpi.ATTR_PASSWORD: norm(auth.Password),
}
props.SetNonEmptyValue("auth", auth.Auth)
props.SetNonEmptyValue(cpi.ATTR_SERVER_ADDRESS, norm(auth.ServerAddress))
props.SetNonEmptyValue(cpi.ATTR_IDENTITY_TOKEN, norm(auth.IdentityToken))
props.SetNonEmptyValue(cpi.ATTR_REGISTRY_TOKEN, norm(auth.RegistryToken))
return cpi.NewCredentials(props)
}
func norm(s string) string {
for strings.HasSuffix(s, "\n") {
s = s[:len(s)-1]
}
return s
}
// IsEmptyAuthConfig validates if the resulting auth config contains credentials.
func IsEmptyAuthConfig(auth types.AuthConfig) bool {
if len(auth.Auth) != 0 {
return false
}
if len(auth.Username) != 0 {
return false
}
return true
}