Debian Codegraph @

internal/mock/mocks.go

raw
1 // Copyright The Notary Project Authors.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package mock
15
16 import (
17 "context"
18 _ "embed"
19
20 "github.com/notaryproject/notation-core-go/signature"
21 "github.com/notaryproject/notation-plugin-framework-go/plugin"
22 "github.com/opencontainers/go-digest"
23 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
24 )
25
26 //go:embed testdata/ca_valid_sig_env.json
27 var MockCaValidSigEnv []byte
28
29 //go:embed testdata/ca_invalid_sig_env.json
30 var MockCaInvalidSigEnv []byte
31
32 //go:embed testdata/sa_valid_sig_env.json
33 var MockSaValidSigEnv []byte
34
35 //go:embed testdata/ca_plugin_sig_env.json
36 var MockCaPluginSigEnv []byte // extended attributes are "SomeKey":"SomeValue", "io.cncf.notary.verificationPlugin":"plugin-name"
37
38 //go:embed testdata/sa_invalid_sig_env.json
39 var MockSaInvalidSigEnv []byte
40
41 //go:embed testdata/ca_expired_sig_env.json
42 var MockCaExpiredSigEnv []byte
43
44 //go:embed testdata/sa_expired_sig_env.json
45 var MockSaExpiredSigEnv []byte
46
47 //go:embed testdata/sa_plugin_sig_env.json
48 var MockSaPluginSigEnv []byte // extended attributes are "SomeKey":"SomeValue", "io.cncf.notary.verificationPlugin":"plugin-name"
49
50 //go:embed testdata/sig_env_with_metadata.json
51 var MockSigEnvWithMetadata []byte
52
53 //go:embed testdata/ca_incompatible_pluginver_sig_env_1.0.9.json
54 var MockCaIncompatiblePluginVerSigEnv_1_0_9 []byte
55
56 //go:embed testdata/ca_incompatible_pluginver_sig_env_1.0.1.json
57 var MockCaIncompatiblePluginVerSigEnv_1_0_1 []byte
58
59 //go:embed testdata/ca_incompatible_pluginver_sig_env_1.2.3.json
60 var MockCaIncompatiblePluginVerSigEnv_1_2_3 []byte
61
62 //go:embed testdata/ca_incompatible_pluginver_sig_env_1.1.0-alpha.json
63 var MockCaIncompatiblePluginVerSigEnv_1_1_0_alpha []byte
64
65 //go:embed testdata/ca_compatible_pluginver_sig_env_0.0.9.json
66 var MockCaCompatiblePluginVerSigEnv_0_0_9 []byte
67
68 //go:embed testdata/ca_compatible_pluginver_sig_env_1.0.0-alpha.json
69 var MockCaCompatiblePluginVerSigEnv_1_0_0_alpha []byte
70
71 //go:embed testdata/ca_compatible_pluginver_sig_env_1.0.0-alpha.beta.json
72 var MockCaCompatiblePluginVerSigEnv_1_0_0_alpha_beta []byte
73
74 //go:embed testdata/ca_compatible_pluginver_sig_env_1.0.0.json
75 var MockCaCompatiblePluginVerSigEnv_1_0_0 []byte
76
77 var (
78 SampleArtifactUri = "registry.acme-rockets.io/software/net-monitor@sha256:60043cf45eaebc4c0867fea485a039b598f52fd09fd5b07b0b2d2f88fad9d74e"
79 SampleDigest = digest.Digest("sha256:60043cf45eaebc4c0867fea485a039b598f52fd09fd5b07b0b2d2f88fad9d74e")
80 ZeroDigest = digest.Digest("sha256:0000000000000000000000000000000000000000000000000000000000000000")
81 Annotations = map[string]string{"key": "value"}
82 ImageDescriptor = ocispec.Descriptor{
83 MediaType: "application/vnd.docker.distribution.manifest.v2+json",
84 Digest: SampleDigest,
85 Size: 528,
86 Annotations: Annotations,
87 }
88 SigManfiestDescriptor = ocispec.Descriptor{
89 MediaType: "application/vnd.oci.image.manifest.v1+json",
90 Digest: SampleDigest,
91 Size: 300,
92 Annotations: Annotations,
93 }
94 TestImageDescriptor = ocispec.Descriptor{
95 MediaType: "application/vnd.docker.distribution.manifest.v2+json",
96 Digest: digest.Digest("sha256:fe7e9333395060c2f5e63cf36a38fba10176f183b4163a5794e081a480abba5f"),
97 Size: 942,
98 Annotations: nil,
99 }
100 JwsSigEnvDescriptor = ocispec.Descriptor{
101 MediaType: "application/jose+json",
102 Digest: SampleDigest,
103 Size: 100,
104 Annotations: Annotations,
105 }
106 PluginExtendedCriticalAttribute = signature.Attribute{
107 Key: "SomeKey",
108 Critical: true,
109 Value: "SomeValue",
110 }
111 MetadataSigEnvDescriptor = ocispec.Descriptor{
112 MediaType: "application/vnd.docker.distribution.manifest.v2+json",
113 Digest: digest.Digest("sha256:5a07385af4e6b6af81b0ebfd435aedccdfa3507f0609c658209e1aba57159b2b"),
114 Size: 942,
115 Annotations: map[string]string{"io.wabbit-networks.buildId": "123", "io.wabbit-networks.buildTime": "1672944615"},
116 }
117 )
118
119 type Repository struct {
120 ResolveResponse ocispec.Descriptor
121 ResolveError error
122 ListSignaturesResponse []ocispec.Descriptor
123 ListSignaturesError error
124 FetchSignatureBlobResponse []byte
125 FetchSignatureBlobError error
126 MissMatchDigest bool
127 ExceededNumOfSignatures bool
128 PushSignatureError error
129 }
130
131 func NewRepository() Repository {
132 return Repository{
133 ResolveResponse: ImageDescriptor,
134 ListSignaturesResponse: []ocispec.Descriptor{SigManfiestDescriptor},
135 FetchSignatureBlobResponse: MockCaValidSigEnv,
136 }
137 }
138
139 func (t Repository) Resolve(ctx context.Context, reference string) (ocispec.Descriptor, error) {
140 if t.MissMatchDigest {
141 return ocispec.Descriptor{
142 MediaType: "application/vnd.docker.distribution.manifest.v2+json",
143 Digest: ZeroDigest,
144 Size: 528,
145 Annotations: Annotations,
146 }, nil
147 }
148 return t.ResolveResponse, t.ResolveError
149 }
150
151 func (t Repository) ListSignatures(ctx context.Context, desc ocispec.Descriptor, fn func(signatureManifests []ocispec.Descriptor) error) error {
152 if t.ExceededNumOfSignatures {
153 t.ListSignaturesResponse = []ocispec.Descriptor{SigManfiestDescriptor, SigManfiestDescriptor}
154 }
155 err := fn(t.ListSignaturesResponse)
156 if err != nil {
157 return err
158 }
159 return t.ListSignaturesError
160 }
161
162 func (t Repository) FetchSignatureBlob(ctx context.Context, desc ocispec.Descriptor) ([]byte, ocispec.Descriptor, error) {
163 return t.FetchSignatureBlobResponse, JwsSigEnvDescriptor, t.FetchSignatureBlobError
164 }
165
166 func (t Repository) PushSignature(ctx context.Context, mediaType string, blob []byte, subject ocispec.Descriptor, annotations map[string]string) (blobDesc, manifestDesc ocispec.Descriptor, err error) {
167 if t.PushSignatureError != nil {
168 return ocispec.Descriptor{}, ocispec.Descriptor{}, t.PushSignatureError
169 }
170
171 return ocispec.Descriptor{}, ocispec.Descriptor{}, nil
172 }
173
174 type PluginMock struct {
175 Metadata plugin.GetMetadataResponse
176 ExecuteResponse interface{}
177 ExecuteError error
178 }
179
180 func (p *PluginMock) GetMetadata(ctx context.Context, req *plugin.GetMetadataRequest) (*plugin.GetMetadataResponse, error) {
181 return &p.Metadata, nil
182 }
183
184 func (p *PluginMock) VerifySignature(ctx context.Context, req *plugin.VerifySignatureRequest) (*plugin.VerifySignatureResponse, error) {
185 if resp, ok := p.ExecuteResponse.(*plugin.VerifySignatureResponse); ok {
186 return resp, nil
187 }
188 return nil, p.ExecuteError
189 }
190
191 func (p *PluginMock) DescribeKey(ctx context.Context, req *plugin.DescribeKeyRequest) (*plugin.DescribeKeyResponse, error) {
192 panic("not implemented") // TODO: Implement
193 }
194
195 func (p *PluginMock) GenerateSignature(ctx context.Context, req *plugin.GenerateSignatureRequest) (*plugin.GenerateSignatureResponse, error) {
196 panic("not implemented") // TODO: Implement
197 }
198
199 func (p *PluginMock) GenerateEnvelope(ctx context.Context, req *plugin.GenerateEnvelopeRequest) (*plugin.GenerateEnvelopeResponse, error) {
200 panic("not implemented") // TODO: Implement
201 }
202
203 type PluginManager struct {
204 PluginCapabilities []plugin.Capability
205 GetPluginError error
206 PluginRunnerLoadError error
207 PluginRunnerExecuteResponse interface{}
208 PluginRunnerExecuteError error
209 }
210
211 func (pm PluginManager) Get(ctx context.Context, name string) (plugin.Plugin, error) {
212 return &PluginMock{
213 Metadata: plugin.GetMetadataResponse{
214 Name: "plugin-name",
215 Description: "for mocking in unit tests",
216 Version: "1.0.0",
217 URL: ".",
218 SupportedContractVersions: []string{"1.0"},
219 Capabilities: pm.PluginCapabilities,
220 },
221 ExecuteResponse: pm.PluginRunnerExecuteResponse,
222 ExecuteError: pm.PluginRunnerExecuteError,
223 }, pm.GetPluginError
224 }
225
226 func (pm PluginManager) List(ctx context.Context) ([]string, error) {
227 panic("not implemented")
228 }