Skip to content

Commit f842da4

Browse files
author
Kazuyoshi Kato
committed
images: prepare for typeurl.Any
typeurl.MarshalAny returns typeurl.Any since containerd/typeurl#32. Signed-off-by: Kazuyoshi Kato <[email protected]>
1 parent 6fdd981 commit f842da4

3 files changed

Lines changed: 88 additions & 3 deletions

File tree

images/encryption/any.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package encryption
18+
19+
import "github.com/gogo/protobuf/types"
20+
21+
type anyMap map[string]*types.Any
22+
23+
type any interface {
24+
GetTypeUrl() string
25+
GetValue() []byte
26+
}
27+
28+
func fromAny(from any) *types.Any {
29+
if from == nil {
30+
return nil
31+
}
32+
33+
pbany, ok := from.(*types.Any)
34+
if ok {
35+
return pbany
36+
}
37+
38+
return &types.Any{
39+
TypeUrl: from.GetTypeUrl(),
40+
Value: from.GetValue(),
41+
}
42+
}

images/encryption/any_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package encryption
18+
19+
import (
20+
"testing"
21+
22+
"github.com/gogo/protobuf/types"
23+
)
24+
25+
func TestFromAny(t *testing.T) {
26+
pbany := &types.Any{}
27+
28+
var testcases = []struct {
29+
input any
30+
expected *types.Any
31+
}{
32+
{input: nil, expected: nil},
33+
{input: pbany, expected: pbany},
34+
}
35+
36+
for _, tc := range testcases {
37+
actual := fromAny(tc.input)
38+
if actual != tc.expected {
39+
t.Fatalf("expected %v, but got %v", tc.expected, actual)
40+
}
41+
}
42+
}

images/encryption/client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,25 @@ import (
2828
"github.com/containerd/typeurl"
2929

3030
encconfig "github.com/containers/ocicrypt/config"
31-
"github.com/gogo/protobuf/types"
3231
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3332
)
3433

3534
// WithDecryptedUnpack allows to pass parameters the 'layertool' needs to the applier
3635
func WithDecryptedUnpack(data *imgcrypt.Payload) diff.ApplyOpt {
3736
return func(_ context.Context, desc ocispec.Descriptor, c *diff.ApplyConfig) error {
3837
if c.ProcessorPayloads == nil {
39-
c.ProcessorPayloads = make(map[string]*types.Any)
38+
c.ProcessorPayloads = make(anyMap)
4039
}
4140
data.Descriptor = desc
4241
any, err := typeurl.MarshalAny(data)
4342
if err != nil {
4443
return fmt.Errorf("failed to marshal payload: %w", err)
4544
}
4645

46+
pbany := fromAny(any)
47+
4748
for _, id := range imgcrypt.PayloadToolIDs {
48-
c.ProcessorPayloads[id] = any
49+
c.ProcessorPayloads[id] = pbany
4950
}
5051
return nil
5152
}

0 commit comments

Comments
 (0)