Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions payloads/import_export.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package payloads

import "github.com/ovh/kmip-go"
import (
"errors"

"github.com/ovh/kmip-go"
"github.com/ovh/kmip-go/ttlv"
)

// init registers the Import and Export operation payloads with the KMIP package.
func init() {
Expand Down Expand Up @@ -28,11 +33,44 @@ type ImportRequestPayload struct {
// Otherwise the server SHALL store the object as provided.
KeyWrapType kmip.KeyWrapType `ttlv:",omitempty"`
// All of the object’s Attributes.
Attribute []kmip.Attribute `ttlv:",omitempty"`
Attribute []kmip.Attribute
// The object value being imported, in the same manner as the Register operation.
Object kmip.Object
}

func (pl *ImportRequestPayload) TagDecodeTTLV(d *ttlv.Decoder, tag int) error {
return d.Struct(tag, func(d *ttlv.Decoder) error {
if err := d.TagAny(kmip.TagUniqueIdentifier, &pl.UniqueIdentifier); err != nil {
return err
}

if err := d.Opt(kmip.TagReplaceExisting, &pl.ReplaceExisting); err != nil {
return err
}

if err := d.Opt(kmip.TagKeyWrapType, &pl.ReplaceExisting); err != nil {
return err
}

if err := d.Any(&pl.Attribute); err != nil {
return err
}

for i := range pl.Attribute {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you do not need to lookup the object type in the attribute list as the type is encoded in the tag of the object struct itself.

if pl.Attribute[i].AttributeName == kmip.AttributeNameObjectType {
if objType, ok := pl.Attribute[i].AttributeValue.(kmip.ObjectType); ok {
var err error
if pl.Object, err = kmip.NewObjectForType(objType); err != nil {
return err
}
return d.Any(&pl.Object)
}
}
}
return errors.New("object type attribute not found in import request")
})
}

// Operation returns the operation type for the ImportRequestPayload.
func (a *ImportRequestPayload) Operation() kmip.Operation {
return kmip.OperationImport
Expand Down Expand Up @@ -92,3 +130,23 @@ type ExportResponsePayload struct {
func (a *ExportResponsePayload) Operation() kmip.Operation {
return kmip.OperationExport
}

func (pl *ExportResponsePayload) TagDecodeTTLV(d *ttlv.Decoder, tag int) error {
return d.Struct(tag, func(d *ttlv.Decoder) error {
if err := d.Any(&pl.ObjectType); err != nil {
return err
}
if err := d.TagAny(kmip.TagUniqueIdentifier, &pl.UniqueIdentifier); err != nil {
return err
}
if err := d.Any(&pl.Attribute); err != nil {
return err
}

var err error
if pl.Object, err = kmip.NewObjectForType(pl.ObjectType); err != nil {
return err
}
return d.Any(&pl.Object)
})
}
51 changes: 51 additions & 0 deletions payloads/payloads_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,54 @@ func TestRegisterRequestPayload_Encode_Decode(t *testing.T) {
require.NoError(t, err)
require.EqualValues(t, req, decodedReq)
}

func TestImportRequestPayload_Encode_Decode(t *testing.T) {
secret := []byte("foobar")
req := &payloads.ImportRequestPayload{
UniqueIdentifier: "foo",
Attribute: []kmip.Attribute{
{AttributeName: kmip.AttributeNameName, AttributeValue: kmip.Name{NameValue: "bar", NameType: kmip.NameTypeUninterpretedTextString}},
{AttributeName: kmip.AttributeNameObjectType, AttributeValue: kmip.ObjectTypeSecretData},
},
Object: &kmip.SecretData{
SecretDataType: kmip.SecretDataTypePassword,
KeyBlock: kmip.KeyBlock{KeyFormatType: kmip.KeyFormatTypeRaw, KeyValue: &kmip.KeyValue{Plain: &kmip.PlainKeyValue{KeyMaterial: kmip.KeyMaterial{Bytes: &secret}}}},
},
}

enc := ttlv.NewTTLVEncoder()
enc.TagAny(kmip.TagRequestPayload, req)
ttlvReq := enc.Bytes()
decodedReq := &payloads.ImportRequestPayload{}
dec, err := ttlv.NewTTLVDecoder(ttlvReq)
require.NoError(t, err)
err = dec.TagAny(kmip.TagRequestPayload, decodedReq)
require.NoError(t, err)
require.EqualValues(t, req, decodedReq)
}

func TestExportResponsePayload_Encode_Decode(t *testing.T) {
secret := []byte("foobar")
req := &payloads.ExportResponsePayload{
ObjectType: kmip.ObjectTypeSecretData,
UniqueIdentifier: "foo",
Attribute: []kmip.Attribute{
{AttributeName: kmip.AttributeNameName, AttributeValue: kmip.Name{NameValue: "bar", NameType: kmip.NameTypeUninterpretedTextString}},
{AttributeName: kmip.AttributeNameObjectType, AttributeValue: kmip.ObjectTypeSecretData},
},
Object: &kmip.SecretData{
SecretDataType: kmip.SecretDataTypePassword,
KeyBlock: kmip.KeyBlock{KeyFormatType: kmip.KeyFormatTypeRaw, KeyValue: &kmip.KeyValue{Plain: &kmip.PlainKeyValue{KeyMaterial: kmip.KeyMaterial{Bytes: &secret}}}},
},
}

enc := ttlv.NewTTLVEncoder()
enc.TagAny(kmip.TagRequestPayload, req)
ttlvReq := enc.Bytes()
decodedReq := &payloads.ExportResponsePayload{}
dec, err := ttlv.NewTTLVDecoder(ttlvReq)
require.NoError(t, err)
err = dec.TagAny(kmip.TagRequestPayload, decodedReq)
require.NoError(t, err)
require.EqualValues(t, req, decodedReq)
}