Skip to content

Commit

Permalink
images: prepare for typeurl.Any
Browse files Browse the repository at this point in the history
typeurl.MarshalAny returns typeurl.Any since
containerd/typeurl#32.

Signed-off-by: Kazuyoshi Kato <[email protected]>
  • Loading branch information
kzys committed Mar 22, 2022
1 parent 6fdd981 commit f842da4
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
42 changes: 42 additions & 0 deletions images/encryption/any.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package encryption

import "github.com/gogo/protobuf/types"

type anyMap map[string]*types.Any

type any interface {
GetTypeUrl() string
GetValue() []byte
}

func fromAny(from any) *types.Any {
if from == nil {
return nil
}

pbany, ok := from.(*types.Any)
if ok {
return pbany
}

return &types.Any{
TypeUrl: from.GetTypeUrl(),
Value: from.GetValue(),
}
}
42 changes: 42 additions & 0 deletions images/encryption/any_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package encryption

import (
"testing"

"github.com/gogo/protobuf/types"
)

func TestFromAny(t *testing.T) {
pbany := &types.Any{}

var testcases = []struct {
input any
expected *types.Any
}{
{input: nil, expected: nil},
{input: pbany, expected: pbany},
}

for _, tc := range testcases {
actual := fromAny(tc.input)
if actual != tc.expected {
t.Fatalf("expected %v, but got %v", tc.expected, actual)
}
}
}
7 changes: 4 additions & 3 deletions images/encryption/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,25 @@ import (
"github.com/containerd/typeurl"

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

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

pbany := fromAny(any)

for _, id := range imgcrypt.PayloadToolIDs {
c.ProcessorPayloads[id] = any
c.ProcessorPayloads[id] = pbany
}
return nil
}
Expand Down

0 comments on commit f842da4

Please sign in to comment.