-
Notifications
You must be signed in to change notification settings - Fork 18
/
credentialsspec.go
170 lines (142 loc) · 4.5 KB
/
credentialsspec.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"encoding/json"
"fmt"
"github.com/modern-go/reflect2"
"github.com/open-component-model/ocm/pkg/runtime"
)
// CredentialsSpec describes a dedicated credential provided by some repository.
type CredentialsSpec interface {
CredentialsSource
GetCredentialsName() string
GetRepositorySpec(Context) RepositorySpec
}
type DefaultCredentialsSpec struct {
RepositorySpec RepositorySpec
CredentialsName string
}
func NewCredentialsSpec(name string, repospec RepositorySpec) CredentialsSpec {
return &DefaultCredentialsSpec{
RepositorySpec: repospec,
CredentialsName: name,
}
}
func (s *DefaultCredentialsSpec) GetCredentialsName() string {
return s.CredentialsName
}
func (s *DefaultCredentialsSpec) GetRepositorySpec(Context) RepositorySpec {
return s.RepositorySpec
}
func (s *DefaultCredentialsSpec) Credentials(ctx Context, creds ...CredentialsSource) (Credentials, error) {
return ctx.CredentialsForSpec(s, creds...)
}
// MarshalJSON implements a custom json unmarshal method.
func (s DefaultCredentialsSpec) MarshalJSON() ([]byte, error) {
ocispec, err := runtime.ToUnstructuredTypedObject(s.RepositorySpec)
if err != nil {
return nil, err
}
specdata, err := runtime.ToUnstructuredObject(struct {
Name string `json:"credentialsName,omitempty"`
}{Name: s.CredentialsName})
if err != nil {
return nil, err
}
return json.Marshal(specdata.FlatMerge(ocispec.Object))
}
// UnmarshalJSON implements a custom default json unmarshal method.
// It should not be used because it always used the default context.
func (s *DefaultCredentialsSpec) UnmarshalJSON(data []byte) error {
repo, err := DefaultContext.RepositoryTypes().Decode(data, runtime.DefaultJSONEncoding)
if err != nil {
return fmt.Errorf("failed to decode data: %w", err)
}
specdata := &struct {
Name string `json:"credentialsName,omitempty"`
}{}
err = json.Unmarshal(data, specdata)
if err != nil {
return fmt.Errorf("failed ot unmarshal spec data: %w", err)
}
var ok bool
s.RepositorySpec, ok = repo.(RepositorySpec)
if !ok {
return fmt.Errorf("invalid repository spec type: %T", repo)
}
s.CredentialsName = specdata.Name
return nil
}
type GenericCredentialsSpec struct {
RepositorySpec *GenericRepositorySpec
CredentialsName string
}
func ToGenericCredentialsSpec(spec CredentialsSpec) (*GenericCredentialsSpec, error) {
if reflect2.IsNil(spec) {
return nil, nil
}
if g, ok := spec.(*GenericCredentialsSpec); ok {
return g, nil
}
data, err := json.Marshal(spec)
if err != nil {
return nil, err
}
return newGenericCredentialsSpec(data, runtime.DefaultJSONEncoding)
}
func newGenericCredentialsSpec(data []byte, unmarshaler runtime.Unmarshaler) (*GenericCredentialsSpec, error) {
gen := &GenericCredentialsSpec{}
if unmarshaler == nil {
unmarshaler = runtime.DefaultYAMLEncoding
}
err := unmarshaler.Unmarshal(data, gen)
if err != nil {
return nil, err
}
return gen, nil
}
func NewGenericCredentialsSpec(name string, repospec *GenericRepositorySpec) *GenericCredentialsSpec {
return &GenericCredentialsSpec{
RepositorySpec: repospec,
CredentialsName: name,
}
}
var _ CredentialsSpec = &GenericCredentialsSpec{}
func (s *GenericCredentialsSpec) GetCredentialsName() string {
return s.CredentialsName
}
func (s *GenericCredentialsSpec) GetRepositorySpec(context Context) RepositorySpec {
return s.RepositorySpec
}
func (s *GenericCredentialsSpec) Credentials(ctx Context, creds ...CredentialsSource) (Credentials, error) {
return ctx.CredentialsForSpec(s, creds...)
}
// MarshalJSON implements a custom json unmarshal method.
func (s GenericCredentialsSpec) MarshalJSON() ([]byte, error) {
specdata, err := runtime.ToUnstructuredObject(struct {
Name string `json:"credentialsName,omitempty"`
}{Name: s.CredentialsName})
if err != nil {
return nil, err
}
return json.Marshal(specdata.FlatMerge(s.RepositorySpec.Object))
}
// UnmarshalJSON implements a custom json unmarshal method for a unstructured typed object.
func (s *GenericCredentialsSpec) UnmarshalJSON(data []byte) error {
spec := &GenericRepositorySpec{}
err := json.Unmarshal(data, spec)
if err != nil {
return fmt.Errorf("failed to unmarshal spec data: %w", err)
}
s.CredentialsName = ""
if name, ok := spec.Object["credentialsName"]; ok {
if n, ok := name.(string); ok {
s.CredentialsName = n
}
}
delete(spec.Object, "credentialName")
s.RepositorySpec = spec
return nil
}