Commit b7648ca3 authored by Stef Walter's avatar Stef Walter
Browse files

secret-store: Set the schema name correctly on loaded items

 * When we loaded items from the keyring we didn't set the schema
   correctly.
 * This causes any searches for the item that include a schema
   in the search parameters to fail.
 * Also caused problems storing items, when it was expected that
   the item would replace any already stored. This uses a search
   internally.
 * Fix and add a test for this case, both for encrypted and
   plaintext keyring files.

https://bugzilla.gnome.org/show_bug.cgi?id=681727
parent 34467afa
......@@ -738,15 +738,18 @@ static void
setup_item_from_info (GkmSecretItem *item, GkmSecretData *data, ItemInfo *info)
{
GkmSecretObject *obj = GKM_SECRET_OBJECT (item);
const gchar *schema_name;
GkmSecret *secret;
const gchar *type;
gkm_secret_object_set_label (obj, info->display_name);
gkm_secret_object_set_created (obj, info->ctime);
gkm_secret_object_set_modified (obj, info->mtime);
type = gkm_secret_compat_format_item_type (info->type);
gkm_secret_item_set_schema (item, type);
schema_name = g_hash_table_lookup (info->attributes, GKM_SECRET_FIELD_SCHEMA);
if (schema_name == NULL)
schema_name = gkm_secret_compat_format_item_type (info->type);
gkm_secret_item_set_schema (item, schema_name);
gkm_secret_item_set_fields (item, info->attributes);
/* Collection is locked */
......
......@@ -122,7 +122,10 @@ generate_attributes (GKeyFile *file, GkmSecretItem *item)
}
static void
parse_attributes (GKeyFile *file, GkmSecretItem *item, const gchar **groups)
parse_attributes (GKeyFile *file,
GkmSecretItem *item,
const gchar **groups,
gint compat_type)
{
GHashTable *attributes;
const gchar *identifier;
......@@ -130,6 +133,7 @@ parse_attributes (GKeyFile *file, GkmSecretItem *item, const gchar **groups)
gchar *prefix;
gchar *name, *type;
guint64 number;
const gchar *schema_name;
/* Now do the attributes */
......@@ -163,6 +167,12 @@ parse_attributes (GKeyFile *file, GkmSecretItem *item, const gchar **groups)
}
gkm_secret_item_set_fields (item, attributes);
schema_name = g_hash_table_lookup (attributes, GKM_SECRET_FIELD_SCHEMA);
if (schema_name == NULL)
schema_name = gkm_secret_compat_format_item_type (compat_type);
gkm_secret_item_set_schema (item, schema_name);
g_hash_table_unref (attributes);
g_free (prefix);
}
......@@ -331,7 +341,6 @@ parse_item (GKeyFile *file, GkmSecretItem *item, GkmSecretData *sdata,
g_clear_error (&err);
type = 0;
}
gkm_secret_item_set_schema (item, gkm_secret_compat_format_item_type (type));
val = g_key_file_get_string (file, identifier, "display-name", NULL);
gkm_secret_object_set_label (obj, val);
......@@ -374,7 +383,7 @@ parse_item (GKeyFile *file, GkmSecretItem *item, GkmSecretData *sdata,
gkm_secret_object_set_created (obj, num);
/* Now the other stuff */
parse_attributes (file, item, groups);
parse_attributes (file, item, groups, type);
parse_acl (file, item, groups);
}
......
[keyring]
display-name=test
ctime=0
mtime=0
lock-on-idle=false
lock-after=false
[1]
item-type=0
display-name=Secret Test Credentials
secret=pa$$w0rd
mtime=1350055423
ctime=0
[1:attribute0]
name=rishi-identity
type=string
value=rishi-identity
[1:attribute1]
name=xdg:schema
type=string
value=se.lostca.is.rishi.secret
......@@ -225,6 +225,26 @@ test_read_created_on_rhel (Test *test, gconstpointer unused)
g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
}
static void
test_read_with_schema (Test *test,
gconstpointer unused)
{
GkmDataResult res;
GkmSecret *master;
GkmSecretItem *item;
master = gkm_secret_new_from_password ("test");
gkm_secret_data_set_master (test->sdata, master);
g_object_unref (master);
res = check_read_keyring_file (test, SRCDIR "/files/encrypted-with-schema.keyring");
g_assert_cmpint (res, ==, GKM_DATA_SUCCESS);
item = gkm_secret_collection_get_item (test->collection, "1");
g_assert (item != NULL);
g_assert_cmpstr (gkm_secret_item_get_schema (item), ==, "se.lostca.is.rishi.secret");
}
int
main (int argc, char **argv)
{
......@@ -239,6 +259,7 @@ main (int argc, char **argv)
g_test_add ("/secret-store/binary/remove_unavailable", Test, NULL, setup, test_remove_unavailable, teardown);
g_test_add ("/secret-store/binary/created_on_rhel", Test, NULL, setup, test_read_created_on_rhel, teardown);
g_test_add ("/secret-store/binary/created_on_solaris_opencsw", Test, NULL, setup, test_read_created_on_solaris_opencsw, teardown);
g_test_add ("/secret-store/binary/read_with_schema", Test, NULL, setup, test_read_with_schema, teardown);
return g_test_run ();
}
......@@ -189,6 +189,28 @@ test_remove_unavailable (Test *test, gconstpointer unused)
g_free (data);
}
static void
test_read_with_schema (Test *test,
gconstpointer unused)
{
GkmDataResult res;
GkmSecretItem *item;
gchar *data;
gsize n_data;
if (!g_file_get_contents (SRCDIR "/files/plain-with-schema.keyring", &data, &n_data, NULL))
g_assert_not_reached ();
res = gkm_secret_textual_read (test->collection, test->sdata, data, n_data);
g_assert (res == GKM_DATA_SUCCESS);
item = gkm_secret_collection_get_item (test->collection, "1");
g_assert (item != NULL);
g_assert_cmpstr (gkm_secret_item_get_schema (item), ==, "se.lostca.is.rishi.secret");
g_free (data);
}
int
main (int argc, char **argv)
{
......@@ -200,6 +222,7 @@ main (int argc, char **argv)
g_test_add ("/secret-store/search/read_bad_number", Test, NULL, setup, test_read_bad_number, teardown);
g_test_add ("/secret-store/search/write", Test, NULL, setup, test_write, teardown);
g_test_add ("/secret-store/search/remove_unavailable", Test, NULL, setup, test_remove_unavailable, teardown);
g_test_add ("/secret-store/search/read-with-schema", Test, NULL, setup, test_read_with_schema, teardown);
return g_test_run ();
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment