From 5407e2acc924d11b62ada4413d49c81219dfa4b3 Mon Sep 17 00:00:00 2001 From: "konstantin.goncharov" Date: Mon, 1 Aug 2022 14:52:59 +0500 Subject: [PATCH] Credential values json serialize/deserialize * added cred vals serialization methods to header * added cred vals serialization methods to Golang code * updated value builder test * fixed comment Signed-off-by: konstantin.goncharov Signed-off-by: Ry Jones --- .gitignore | 1 + pkg/libursa/ursa/ursa_cl.h | 20 +++++++++++++++++++ pkg/libursa/ursa/value_builder.go | 27 ++++++++++++++++++++++++++ pkg/libursa/ursa/value_builder_test.go | 16 +++++++++++++++ 4 files changed, 64 insertions(+) diff --git a/.gitignore b/.gitignore index e76f356..41350eb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build .idea/ coverage.txt *.out +.vscode/ \ No newline at end of file diff --git a/pkg/libursa/ursa/ursa_cl.h b/pkg/libursa/ursa/ursa_cl.h index be5dc8a..30b6594 100644 --- a/pkg/libursa/ursa/ursa_cl.h +++ b/pkg/libursa/ursa/ursa_cl.h @@ -417,6 +417,26 @@ struct ExternError ursa_cl_credential_values_builder_finalize(const void *creden */ struct ExternError ursa_cl_credential_values_builder_new(const void **credential_values_builder_p); +/** + * Creates and returns credential values json. + * + * Note: Credential values instance deallocation must be performed by calling ursa_cl_credential_values_free. + * + * # Arguments + * * `credential_values_json` - Reference that contains credential values json. + * * `credential_values_p` - Reference that will contain credential values instance pointer. + */ +struct ExternError ursa_cl_credential_values_from_json(const char *credential_values_json, const void **credential_values_p); + +/** + * Returns json representation of credential values. + * + * # Arguments + * * `credential_values` - Reference that contains credential values instance pointer. + * * `credential_values_json_p` - Reference that will contain credential values json. + */ +struct ExternError ursa_cl_credential_values_to_json(const void *credential_values, const char **credential_values_json_p); + /** * Deallocates credential values instance. * diff --git a/pkg/libursa/ursa/value_builder.go b/pkg/libursa/ursa/value_builder.go index 57171f3..17f574d 100644 --- a/pkg/libursa/ursa/value_builder.go +++ b/pkg/libursa/ursa/value_builder.go @@ -30,6 +30,20 @@ func NewValueBuilder() (*CredentialValuesBuilder, error) { return &CredentialValuesBuilder{builder}, nil } +// CredentialValuesFromJSON creates and returns credential values from JSON +func CredentialValuesFromJSON(jsn []byte) (*CredentialValues, error) { + var ptr unsafe.Pointer + cjson := C.CString(string(jsn)) + defer C.free(unsafe.Pointer(cjson)) + + result := C.ursa_cl_credential_values_from_json(cjson, &ptr) + if result.code != 0 { + return nil, ursaError(C.GoString(result.message)) + } + + return &CredentialValues{ptr}, nil +} + // AddDecHidden adds new hidden attribute dec_value to credential values map func (r *CredentialValuesBuilder) AddDecHidden(attr, decValue string) error { cattr := C.CString(attr) @@ -100,6 +114,19 @@ func (r *CredentialValues) Free() error { return nil } +// ToJSON returns JSON representation of credential values +func (r *CredentialValues) ToJSON() ([]byte, error) { + var d *C.char + defer C.free(unsafe.Pointer(d)) + + result := C.ursa_cl_credential_values_to_json(r.ptr, &d) + if result.code != 0 { + return nil, ursaError(C.GoString(result.message)) + } + + return []byte(C.GoString(d)), nil +} + // EncodeValue encodes any value into decimal representation func EncodeValue(val interface{}) (string, string) { var raw, enc string diff --git a/pkg/libursa/ursa/value_builder_test.go b/pkg/libursa/ursa/value_builder_test.go index 970d7e0..b868ad8 100644 --- a/pkg/libursa/ursa/value_builder_test.go +++ b/pkg/libursa/ursa/value_builder_test.go @@ -11,6 +11,22 @@ func TestNewValueBuilder(t *testing.T) { builder, err := NewValueBuilder() assert.Empty(t, err) assert.NotEmpty(t, builder) + + err = builder.AddDecHidden("master_secret", "122345") + assert.NoError(t, err) + + vals, err := builder.Finalize() + assert.NoError(t, err) + + str, err := vals.ToJSON() + assert.NoError(t, err) + + err = vals.Free() + assert.NoError(t, err) + + newVals, err := CredentialValuesFromJSON(str) + assert.NoError(t, err) + assert.NotNil(t, newVals) }) }