static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) { char *inXmlData = NULL; char *outXmlData = NULL; char *actual = NULL; int ret = -1; virSecretDefPtr secret = NULL; if (virtTestLoadFile(inxml, &inXmlData) < 0) goto fail; if (virtTestLoadFile(outxml, &outXmlData) < 0) goto fail; if (!(secret = virSecretDefParseString(inXmlData))) goto fail; if (!(actual = virSecretDefFormat(secret))) goto fail; if (STRNEQ(outXmlData, actual)) { virtTestDifference(stderr, outXmlData, actual); goto fail; } ret = 0; fail: VIR_FREE(inXmlData); VIR_FREE(outXmlData); VIR_FREE(actual); virSecretDefFree(secret); return ret; }
static void virSecretObjDispose(void *obj) { virSecretObjPtr secret = obj; virSecretDefFree(secret->def); if (secret->value) { /* Wipe before free to ensure we don't leave a secret on the heap */ memset(secret->value, 0, secret->value_size); VIR_FREE(secret->value); } VIR_FREE(secret->configFile); VIR_FREE(secret->base64File); }
static int testCompareXMLToXMLFiles(const char *inxml, const char *outxml) { char *actual = NULL; int ret = -1; virSecretDefPtr secret = NULL; if (!(secret = virSecretDefParseFile(inxml))) goto fail; if (!(actual = virSecretDefFormat(secret))) goto fail; if (virTestCompareToFile(actual, outxml) < 0) goto fail; ret = 0; fail: VIR_FREE(actual); virSecretDefFree(secret); return ret; }
static virSecretDefPtr secretXMLParseNode(xmlDocPtr xml, xmlNodePtr root) { xmlXPathContextPtr ctxt = NULL; virSecretDefPtr def = NULL, ret = NULL; char *prop = NULL; char *uuidstr = NULL; if (!xmlStrEqual(root->name, BAD_CAST "secret")) { virReportError(VIR_ERR_XML_ERROR, _("unexpected root element <%s>, " "expecting <secret>"), root->name); goto cleanup; } ctxt = xmlXPathNewContext(xml); if (ctxt == NULL) { virReportOOMError(); goto cleanup; } ctxt->node = root; if (VIR_ALLOC(def) < 0) goto cleanup; prop = virXPathString("string(./@ephemeral)", ctxt); if (prop != NULL) { if (STREQ(prop, "yes")) { def->isephemeral = true; } else if (STREQ(prop, "no")) { def->isephemeral = false; } else { virReportError(VIR_ERR_XML_ERROR, "%s", _("invalid value of 'ephemeral'")); goto cleanup; } VIR_FREE(prop); } prop = virXPathString("string(./@private)", ctxt); if (prop != NULL) { if (STREQ(prop, "yes")) { def->isprivate = true; } else if (STREQ(prop, "no")) { def->isprivate = false; } else { virReportError(VIR_ERR_XML_ERROR, "%s", _("invalid value of 'private'")); goto cleanup; } VIR_FREE(prop); } uuidstr = virXPathString("string(./uuid)", ctxt); if (!uuidstr) { if (virUUIDGenerate(def->uuid)) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Failed to generate UUID")); goto cleanup; } } else { if (virUUIDParse(uuidstr, def->uuid) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("malformed uuid element")); goto cleanup; } VIR_FREE(uuidstr); } def->description = virXPathString("string(./description)", ctxt); if (virXPathNode("./usage", ctxt) != NULL && virSecretDefParseUsage(ctxt, def) < 0) goto cleanup; ret = def; def = NULL; cleanup: VIR_FREE(prop); VIR_FREE(uuidstr); virSecretDefFree(def); xmlXPathFreeContext(ctxt); return ret; }
static virSecretPtr secretDefineXML(virConnectPtr conn, const char *xml, unsigned int flags) { virSecretPtr ret = NULL; virSecretObjPtr obj = NULL; virSecretDefPtr objDef; virSecretDefPtr backup = NULL; virSecretDefPtr def; virObjectEventPtr event = NULL; virCheckFlags(0, NULL); if (!(def = virSecretDefParseString(xml))) return NULL; if (virSecretDefineXMLEnsureACL(conn, def) < 0) goto cleanup; if (!(obj = virSecretObjListAdd(driver->secrets, def, driver->configDir, &backup))) goto cleanup; VIR_STEAL_PTR(objDef, def); if (!objDef->isephemeral) { if (backup && backup->isephemeral) { if (virSecretObjSaveData(obj) < 0) goto restore_backup; } if (virSecretObjSaveConfig(obj) < 0) { if (backup && backup->isephemeral) { /* Undo the virSecretObjSaveData() above; ignore errors */ virSecretObjDeleteData(obj); } goto restore_backup; } } else if (backup && !backup->isephemeral) { if (virSecretObjDeleteConfig(obj) < 0) goto restore_backup; virSecretObjDeleteData(obj); } /* Saved successfully - drop old values */ virSecretDefFree(backup); event = virSecretEventLifecycleNew(objDef->uuid, objDef->usage_type, objDef->usage_id, VIR_SECRET_EVENT_DEFINED, 0); ret = virGetSecret(conn, objDef->uuid, objDef->usage_type, objDef->usage_id); goto cleanup; restore_backup: /* If we have a backup, then secret was defined before, so just restore * the backup; otherwise, this is a new secret, thus remove it. */ if (backup) { virSecretObjSetDef(obj, backup); VIR_STEAL_PTR(def, objDef); } else { virSecretObjListRemove(driver->secrets, obj); virObjectUnref(obj); obj = NULL; } cleanup: virSecretDefFree(def); virSecretObjEndAPI(&obj); if (event) virObjectEventStateQueue(driver->secretEventState, event); return ret; }
static virSecretPtr secretDefineXML(virConnectPtr conn, const char *xml, unsigned int flags) { virSecretPtr ret = NULL; virSecretObjPtr secret = NULL; virSecretDefPtr backup = NULL; virSecretDefPtr new_attrs; virCheckFlags(0, NULL); if (!(new_attrs = virSecretDefParseString(xml))) return NULL; if (virSecretDefineXMLEnsureACL(conn, new_attrs) < 0) goto cleanup; if (!(secret = virSecretObjListAdd(driver->secrets, new_attrs, driver->configDir, &backup))) goto cleanup; if (!new_attrs->isephemeral) { if (secretEnsureDirectory() < 0) goto cleanup; if (backup && backup->isephemeral) { if (virSecretObjSaveData(secret) < 0) goto restore_backup; } if (virSecretObjSaveConfig(secret) < 0) { if (backup && backup->isephemeral) { /* Undo the virSecretObjSaveData() above; ignore errors */ virSecretObjDeleteData(secret); } goto restore_backup; } } else if (backup && !backup->isephemeral) { if (virSecretObjDeleteConfig(secret) < 0) goto restore_backup; virSecretObjDeleteData(secret); } /* Saved successfully - drop old values */ virSecretDefFree(backup); ret = virGetSecret(conn, new_attrs->uuid, new_attrs->usage_type, virSecretUsageIDForDef(new_attrs)); new_attrs = NULL; goto cleanup; restore_backup: /* If we have a backup, then secret was defined before, so just restore * the backup. The current (new_attrs) will be handled below. * Otherwise, this is a new secret, thus remove it. */ if (backup) virSecretObjSetDef(secret, backup); else virSecretObjListRemove(driver->secrets, secret); cleanup: virSecretDefFree(new_attrs); virSecretObjEndAPI(&secret); return ret; }