Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

PS-9609: locate_secrets_* operations added to kmippp #18

Merged
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
1 change: 1 addition & 0 deletions kmippp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ add_pp_demo(get_name)
add_pp_demo(register)
add_pp_demo(locate)
add_pp_demo(all)
add_pp_demo(all_secrets)
add_pp_demo(revoke)
add_pp_demo(get_secret)
add_pp_demo(register_secret)
Expand Down
41 changes: 41 additions & 0 deletions kmippp/demo_all_secrets.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@


#include "kmippp.h"
#include <iostream>

int
main (int argc, char **argv)
{

if (argc < 6)
{
std::cerr << "Usage: demo_locate <host> <port> <client_cert> "
"<client_key> <server_cert> [group_name]"
<< std::endl;
return -1;
}

kmippp::context ctx (argv[1], argv[2], argv[3], argv[4], argv[5]);
// auto keys = ctx.op_all_secrets();
const std::string group = argv[6]!=nullptr? argv[6] : "TestGroup";
auto keys = ctx.op_locate_secrets_by_group (group);
Comment on lines +20 to +21
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems these two lines have excessive indent of one space.

if(keys.empty ())
{
std::cerr << "No Secret Data found" << std::endl;
std::cerr << ctx.get_last_result () << std::endl;
return 1;
}
for (auto id : keys)
{
std::cout << "Key: " << id << " ";
auto secret = ctx.op_get_secret (id);
auto secret_name = ctx.op_get_name_attr (id);
std::cout << secret_name << " 0x";
for (auto const &c : secret)
{
std::cout << std::hex << ((int)c);
}
std::cout << std::endl;
}
return 0;
}
112 changes: 112 additions & 0 deletions kmippp/kmippp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,67 @@ context::op_locate_by_group (context::name_t group)
return ret;
}

context::ids_t
context::op_locate_secrets_by_group (context::name_t group)
{
Attribute a[2];
for (int i = 0; i < 2; i++)
{
kmip_init_attribute (&a[i]);
}

object_type loctype = KMIP_OBJTYPE_SECRET_DATA;
a[0].type = KMIP_ATTR_OBJECT_TYPE;
a[0].value = &loctype;

TextString ts2 = { 0, 0 };
ts2.value = const_cast<char *> (group.c_str ());
ts2.size = kmip_strnlen_s (ts2.value, 250);
a[1].type = KMIP_ATTR_OBJECT_GROUP;
a[1].value = &ts2;

TemplateAttribute ta = { 0 };
ta.attributes = a;
ta.attribute_count = ARRAY_LENGTH (a);

int upto = 0;
int all = 1; // TMP
Copy link
Contributor

Choose a reason for hiding this comment

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

What does TMP mean?

ids_t ret;

LocateResponse locate_result;

while (upto < all)
{
int result = kmip_bio_locate (bio_, a, 2, &locate_result, 16, upto);
Copy link
Contributor

Choose a reason for hiding this comment

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

I would use a named constant (e.g. attribute_count) instead of hardcoded 2.


if (result != 0)
{
return {};
}

for (int i = 0; i < locate_result.ids_size; ++i)
{
ret.push_back (locate_result.ids[i]);
}
if (locate_result.located_items != 0)
{
all = locate_result.located_items; // shouldn't change after its != 1
}
else
{
// Dummy server sometimes returns 0 for located_items
all += locate_result.ids_size;
if (locate_result.ids_size == 0)
{
--all;
}
}
Comment on lines +425 to +437
Copy link
Contributor

Choose a reason for hiding this comment

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

The logic with all, located_items and ids_size isn't crystal clear. Probably , a detailed comment would be helpful.

upto += locate_result.ids_size;
}

return ret;
}

context::ids_t
context::op_all ()
{
Expand Down Expand Up @@ -431,6 +492,57 @@ context::op_all ()
return ret;
}

context::ids_t
context::op_all_secrets ()
{
Attribute a[1];
for (int i = 0; i < 1; i++)
{
kmip_init_attribute (&a[i]);
}

object_type loctype = KMIP_OBJTYPE_SECRET_DATA;
a[0].type = KMIP_ATTR_OBJECT_TYPE;
a[0].value = &loctype;

LocateResponse locate_result;

int upto = 0;
int all = 1; // TMP
ids_t ret;

while (upto < all)
{
int result = kmip_bio_locate (bio_, a, 1, &locate_result, 16, upto);

if (result != 0)
{
return {};
}

for (int i = 0; i < locate_result.ids_size; ++i)
{
ret.push_back (locate_result.ids[i]);
}
if (locate_result.located_items != 0)
{
all = locate_result.located_items; // shouldn't change after its != 1
}
else
{
// Dummy server sometimes returns 0 for located_items
all += locate_result.ids_size;
if (locate_result.ids_size == 0)
{
--all;
}
}
upto += locate_result.ids_size;
}

return ret;
}

bool
context::op_revoke (id_t id, int reason, name_t message, time_t occurrence_time)
{
Expand Down
4 changes: 4 additions & 0 deletions kmippp/kmippp.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,16 @@ class context

ids_t op_locate_by_group (name_t group);

ids_t op_locate_secrets_by_group (name_t group);

bool op_destroy (id_t id);

// KMIP::locate operation, retrieve all symmetric keys
// note: name can be empty, and will retrieve all keys
ids_t op_all ();

ids_t op_all_secrets ();

// KMIP::revoke operation, revoke activated or not activated key. Deactivates
// active key
bool op_revoke (id_t id, int reason, name_t message, time_t occurrence_time);
Expand Down
2 changes: 1 addition & 1 deletion libkmip/include/libkmip_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#define KMIP_LIB_VERSION_MAJOR 0
#define KMIP_LIB_VERSION_MINOR 3
#define KMIP_LIB_VERSION_PATCH 1
#define KMIP_LIB_VERSION_PATCH 2

#define KMIP_LIB_STRINGIFY_I(x) #x
#define KMIP_LIB_TOSTRING_I(x) KMIP_LIB_STRINGIFY_I (x)
Expand Down