提交 bd300b71 编写于 作者: D Daniel P. Berrange

conf: simplify internal virSecretDef handling of usage

The public virSecret object has a single "usage_id" field
but the virSecretDef object has a different 'char *' field
for each usage type, but the code all assumes every usage
type has a corresponding single string. Get rid of the
pointless union in virSecretDef and just use "usage_id"
everywhere. This doesn't impact public XML format, only
the internal handling.
Signed-off-by: NDaniel P. Berrange <berrange@redhat.com>
上级 df740caf
...@@ -303,7 +303,7 @@ virAccessDriverPolkitCheckSecret(virAccessManagerPtr manager, ...@@ -303,7 +303,7 @@ virAccessDriverPolkitCheckSecret(virAccessManagerPtr manager,
const char *attrs[] = { const char *attrs[] = {
"connect_driver", driverName, "connect_driver", driverName,
"secret_uuid", uuidstr, "secret_uuid", uuidstr,
"secret_usage_volume", secret->usage.volume, "secret_usage_volume", secret->usage_id,
NULL, NULL,
}; };
...@@ -316,7 +316,7 @@ virAccessDriverPolkitCheckSecret(virAccessManagerPtr manager, ...@@ -316,7 +316,7 @@ virAccessDriverPolkitCheckSecret(virAccessManagerPtr manager,
const char *attrs[] = { const char *attrs[] = {
"connect_driver", driverName, "connect_driver", driverName,
"secret_uuid", uuidstr, "secret_uuid", uuidstr,
"secret_usage_ceph", secret->usage.ceph, "secret_usage_ceph", secret->usage_id,
NULL, NULL,
}; };
...@@ -329,7 +329,7 @@ virAccessDriverPolkitCheckSecret(virAccessManagerPtr manager, ...@@ -329,7 +329,7 @@ virAccessDriverPolkitCheckSecret(virAccessManagerPtr manager,
const char *attrs[] = { const char *attrs[] = {
"connect_driver", driverName, "connect_driver", driverName,
"secret_uuid", uuidstr, "secret_uuid", uuidstr,
"secret_usage_target", secret->usage.target, "secret_usage_target", secret->usage_id,
NULL, NULL,
}; };
...@@ -342,7 +342,7 @@ virAccessDriverPolkitCheckSecret(virAccessManagerPtr manager, ...@@ -342,7 +342,7 @@ virAccessDriverPolkitCheckSecret(virAccessManagerPtr manager,
const char *attrs[] = { const char *attrs[] = {
"connect_driver", driverName, "connect_driver", driverName,
"secret_uuid", uuidstr, "secret_uuid", uuidstr,
"secret_usage_name", secret->usage.name, "secret_usage_name", secret->usage_id,
NULL, NULL,
}; };
......
...@@ -41,31 +41,6 @@ VIR_LOG_INIT("conf.secret_conf"); ...@@ -41,31 +41,6 @@ VIR_LOG_INIT("conf.secret_conf");
VIR_ENUM_IMPL(virSecretUsage, VIR_SECRET_USAGE_TYPE_LAST, VIR_ENUM_IMPL(virSecretUsage, VIR_SECRET_USAGE_TYPE_LAST,
"none", "volume", "ceph", "iscsi", "tls") "none", "volume", "ceph", "iscsi", "tls")
const char *
virSecretUsageIDForDef(virSecretDefPtr def)
{
switch (def->usage_type) {
case VIR_SECRET_USAGE_TYPE_NONE:
return "";
case VIR_SECRET_USAGE_TYPE_VOLUME:
return def->usage.volume;
case VIR_SECRET_USAGE_TYPE_CEPH:
return def->usage.ceph;
case VIR_SECRET_USAGE_TYPE_ISCSI:
return def->usage.target;
case VIR_SECRET_USAGE_TYPE_TLS:
return def->usage.name;
default:
return NULL;
}
}
void void
virSecretDefFree(virSecretDefPtr def) virSecretDefFree(virSecretDefPtr def)
{ {
...@@ -73,30 +48,7 @@ virSecretDefFree(virSecretDefPtr def) ...@@ -73,30 +48,7 @@ virSecretDefFree(virSecretDefPtr def)
return; return;
VIR_FREE(def->description); VIR_FREE(def->description);
switch (def->usage_type) { VIR_FREE(def->usage_id);
case VIR_SECRET_USAGE_TYPE_NONE:
break;
case VIR_SECRET_USAGE_TYPE_VOLUME:
VIR_FREE(def->usage.volume);
break;
case VIR_SECRET_USAGE_TYPE_CEPH:
VIR_FREE(def->usage.ceph);
break;
case VIR_SECRET_USAGE_TYPE_ISCSI:
VIR_FREE(def->usage.target);
break;
case VIR_SECRET_USAGE_TYPE_TLS:
VIR_FREE(def->usage.name);
break;
default:
VIR_ERROR(_("unexpected secret usage type %d"), def->usage_type);
break;
}
VIR_FREE(def); VIR_FREE(def);
} }
...@@ -127,8 +79,8 @@ virSecretDefParseUsage(xmlXPathContextPtr ctxt, ...@@ -127,8 +79,8 @@ virSecretDefParseUsage(xmlXPathContextPtr ctxt,
break; break;
case VIR_SECRET_USAGE_TYPE_VOLUME: case VIR_SECRET_USAGE_TYPE_VOLUME:
def->usage.volume = virXPathString("string(./usage/volume)", ctxt); def->usage_id = virXPathString("string(./usage/volume)", ctxt);
if (!def->usage.volume) { if (!def->usage_id) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("volume usage specified, but volume path is missing")); _("volume usage specified, but volume path is missing"));
return -1; return -1;
...@@ -136,8 +88,8 @@ virSecretDefParseUsage(xmlXPathContextPtr ctxt, ...@@ -136,8 +88,8 @@ virSecretDefParseUsage(xmlXPathContextPtr ctxt,
break; break;
case VIR_SECRET_USAGE_TYPE_CEPH: case VIR_SECRET_USAGE_TYPE_CEPH:
def->usage.ceph = virXPathString("string(./usage/name)", ctxt); def->usage_id = virXPathString("string(./usage/name)", ctxt);
if (!def->usage.ceph) { if (!def->usage_id) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Ceph usage specified, but name is missing")); _("Ceph usage specified, but name is missing"));
return -1; return -1;
...@@ -145,8 +97,8 @@ virSecretDefParseUsage(xmlXPathContextPtr ctxt, ...@@ -145,8 +97,8 @@ virSecretDefParseUsage(xmlXPathContextPtr ctxt,
break; break;
case VIR_SECRET_USAGE_TYPE_ISCSI: case VIR_SECRET_USAGE_TYPE_ISCSI:
def->usage.target = virXPathString("string(./usage/target)", ctxt); def->usage_id = virXPathString("string(./usage/target)", ctxt);
if (!def->usage.target) { if (!def->usage_id) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("iSCSI usage specified, but target is missing")); _("iSCSI usage specified, but target is missing"));
return -1; return -1;
...@@ -154,8 +106,8 @@ virSecretDefParseUsage(xmlXPathContextPtr ctxt, ...@@ -154,8 +106,8 @@ virSecretDefParseUsage(xmlXPathContextPtr ctxt,
break; break;
case VIR_SECRET_USAGE_TYPE_TLS: case VIR_SECRET_USAGE_TYPE_TLS:
def->usage.name = virXPathString("string(./usage/name)", ctxt); def->usage_id = virXPathString("string(./usage/name)", ctxt);
if (!def->usage.name) { if (!def->usage_id) {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("TLS usage specified, but name is missing")); _("TLS usage specified, but name is missing"));
return -1; return -1;
...@@ -303,19 +255,19 @@ virSecretDefFormatUsage(virBufferPtr buf, ...@@ -303,19 +255,19 @@ virSecretDefFormatUsage(virBufferPtr buf,
break; break;
case VIR_SECRET_USAGE_TYPE_VOLUME: case VIR_SECRET_USAGE_TYPE_VOLUME:
virBufferEscapeString(buf, "<volume>%s</volume>\n", def->usage.volume); virBufferEscapeString(buf, "<volume>%s</volume>\n", def->usage_id);
break; break;
case VIR_SECRET_USAGE_TYPE_CEPH: case VIR_SECRET_USAGE_TYPE_CEPH:
virBufferEscapeString(buf, "<name>%s</name>\n", def->usage.ceph); virBufferEscapeString(buf, "<name>%s</name>\n", def->usage_id);
break; break;
case VIR_SECRET_USAGE_TYPE_ISCSI: case VIR_SECRET_USAGE_TYPE_ISCSI:
virBufferEscapeString(buf, "<target>%s</target>\n", def->usage.target); virBufferEscapeString(buf, "<target>%s</target>\n", def->usage_id);
break; break;
case VIR_SECRET_USAGE_TYPE_TLS: case VIR_SECRET_USAGE_TYPE_TLS:
virBufferEscapeString(buf, "<name>%s</name>\n", def->usage.name); virBufferEscapeString(buf, "<name>%s</name>\n", def->usage_id);
break; break;
default: default:
......
...@@ -36,16 +36,9 @@ struct _virSecretDef { ...@@ -36,16 +36,9 @@ struct _virSecretDef {
unsigned char uuid[VIR_UUID_BUFLEN]; unsigned char uuid[VIR_UUID_BUFLEN];
char *description; /* May be NULL */ char *description; /* May be NULL */
int usage_type; /* virSecretUsageType */ int usage_type; /* virSecretUsageType */
union { char *usage_id; /* May be NULL */
char *volume; /* May be NULL */
char *ceph;
char *target;
char *name;
} usage;
}; };
const char *virSecretUsageIDForDef(virSecretDefPtr def);
void virSecretDefFree(virSecretDefPtr def); void virSecretDefFree(virSecretDefPtr def);
virSecretDefPtr virSecretDefParseString(const char *xml); virSecretDefPtr virSecretDefParseString(const char *xml);
virSecretDefPtr virSecretDefParseFile(const char *filename); virSecretDefPtr virSecretDefParseFile(const char *filename);
......
...@@ -218,31 +218,9 @@ virSecretObjSearchName(const void *payload, ...@@ -218,31 +218,9 @@ virSecretObjSearchName(const void *payload,
if (secret->def->usage_type != data->usageType) if (secret->def->usage_type != data->usageType)
goto cleanup; goto cleanup;
switch (data->usageType) { if (data->usageType != VIR_SECRET_USAGE_TYPE_NONE &&
case VIR_SECRET_USAGE_TYPE_NONE: STREQ(secret->def->usage_id, data->usageID))
/* never match this */ found = 1;
break;
case VIR_SECRET_USAGE_TYPE_VOLUME:
if (STREQ(secret->def->usage.volume, data->usageID))
found = 1;
break;
case VIR_SECRET_USAGE_TYPE_CEPH:
if (STREQ(secret->def->usage.ceph, data->usageID))
found = 1;
break;
case VIR_SECRET_USAGE_TYPE_ISCSI:
if (STREQ(secret->def->usage.target, data->usageID))
found = 1;
break;
case VIR_SECRET_USAGE_TYPE_TLS:
if (STREQ(secret->def->usage.name, data->usageID))
found = 1;
break;
}
cleanup: cleanup:
virObjectUnlock(secret); virObjectUnlock(secret);
...@@ -352,7 +330,6 @@ virSecretObjListAddLocked(virSecretObjListPtr secrets, ...@@ -352,7 +330,6 @@ virSecretObjListAddLocked(virSecretObjListPtr secrets,
{ {
virSecretObjPtr secret; virSecretObjPtr secret;
virSecretObjPtr ret = NULL; virSecretObjPtr ret = NULL;
const char *newUsageID = virSecretUsageIDForDef(def);
char uuidstr[VIR_UUID_STRING_BUFLEN]; char uuidstr[VIR_UUID_STRING_BUFLEN];
char *configFile = NULL, *base64File = NULL; char *configFile = NULL, *base64File = NULL;
...@@ -361,17 +338,14 @@ virSecretObjListAddLocked(virSecretObjListPtr secrets, ...@@ -361,17 +338,14 @@ virSecretObjListAddLocked(virSecretObjListPtr secrets,
/* Is there a secret already matching this UUID */ /* Is there a secret already matching this UUID */
if ((secret = virSecretObjListFindByUUIDLocked(secrets, def->uuid))) { if ((secret = virSecretObjListFindByUUIDLocked(secrets, def->uuid))) {
const char *oldUsageID;
virObjectLock(secret); virObjectLock(secret);
oldUsageID = virSecretUsageIDForDef(secret->def); if (STRNEQ_NULLABLE(secret->def->usage_id, def->usage_id)) {
if (STRNEQ(oldUsageID, newUsageID)) {
virUUIDFormat(secret->def->uuid, uuidstr); virUUIDFormat(secret->def->uuid, uuidstr);
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("a secret with UUID %s is already defined for " _("a secret with UUID %s is already defined for "
"use with %s"), "use with %s"),
uuidstr, oldUsageID); uuidstr, secret->def->usage_id);
goto cleanup; goto cleanup;
} }
...@@ -391,13 +365,13 @@ virSecretObjListAddLocked(virSecretObjListPtr secrets, ...@@ -391,13 +365,13 @@ virSecretObjListAddLocked(virSecretObjListPtr secrets,
* try look for matching usage instead */ * try look for matching usage instead */
if ((secret = virSecretObjListFindByUsageLocked(secrets, if ((secret = virSecretObjListFindByUsageLocked(secrets,
def->usage_type, def->usage_type,
newUsageID))) { def->usage_id))) {
virObjectLock(secret); virObjectLock(secret);
virUUIDFormat(secret->def->uuid, uuidstr); virUUIDFormat(secret->def->uuid, uuidstr);
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("a secret with UUID %s already defined for " _("a secret with UUID %s already defined for "
"use with %s"), "use with %s"),
uuidstr, newUsageID); uuidstr, def->usage_id);
goto cleanup; goto cleanup;
} }
...@@ -577,7 +551,7 @@ virSecretObjListPopulate(void *payload, ...@@ -577,7 +551,7 @@ virSecretObjListPopulate(void *payload,
if (!(secret = virGetSecret(data->conn, obj->def->uuid, if (!(secret = virGetSecret(data->conn, obj->def->uuid,
obj->def->usage_type, obj->def->usage_type,
virSecretUsageIDForDef(obj->def)))) { obj->def->usage_id))) {
data->error = true; data->error = true;
goto cleanup; goto cleanup;
} }
......
...@@ -678,14 +678,13 @@ virGetSecret(virConnectPtr conn, const unsigned char *uuid, ...@@ -678,14 +678,13 @@ virGetSecret(virConnectPtr conn, const unsigned char *uuid,
virCheckConnectGoto(conn, error); virCheckConnectGoto(conn, error);
virCheckNonNullArgGoto(uuid, error); virCheckNonNullArgGoto(uuid, error);
virCheckNonNullArgGoto(usageID, error);
if (!(ret = virObjectNew(virSecretClass))) if (!(ret = virObjectNew(virSecretClass)))
return NULL; return NULL;
memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN); memcpy(&(ret->uuid[0]), uuid, VIR_UUID_BUFLEN);
ret->usageType = usageType; ret->usageType = usageType;
if (VIR_STRDUP(ret->usageID, usageID) < 0) if (VIR_STRDUP(ret->usageID, usageID ? usageID : "") < 0)
goto error; goto error;
ret->conn = virObjectRef(conn); ret->conn = virObjectRef(conn);
......
...@@ -838,7 +838,6 @@ virSecretDefFormat; ...@@ -838,7 +838,6 @@ virSecretDefFormat;
virSecretDefFree; virSecretDefFree;
virSecretDefParseFile; virSecretDefParseFile;
virSecretDefParseString; virSecretDefParseString;
virSecretUsageIDForDef;
virSecretUsageTypeFromString; virSecretUsageTypeFromString;
virSecretUsageTypeToString; virSecretUsageTypeToString;
......
...@@ -170,7 +170,7 @@ secretLookupByUUID(virConnectPtr conn, ...@@ -170,7 +170,7 @@ secretLookupByUUID(virConnectPtr conn,
ret = virGetSecret(conn, ret = virGetSecret(conn,
def->uuid, def->uuid,
def->usage_type, def->usage_type,
virSecretUsageIDForDef(def)); def->usage_id);
cleanup: cleanup:
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&secret);
...@@ -201,7 +201,7 @@ secretLookupByUsage(virConnectPtr conn, ...@@ -201,7 +201,7 @@ secretLookupByUsage(virConnectPtr conn,
ret = virGetSecret(conn, ret = virGetSecret(conn,
def->uuid, def->uuid,
def->usage_type, def->usage_type,
virSecretUsageIDForDef(def)); def->usage_id);
cleanup: cleanup:
virSecretObjEndAPI(&secret); virSecretObjEndAPI(&secret);
...@@ -259,7 +259,7 @@ secretDefineXML(virConnectPtr conn, ...@@ -259,7 +259,7 @@ secretDefineXML(virConnectPtr conn,
ret = virGetSecret(conn, ret = virGetSecret(conn,
new_attrs->uuid, new_attrs->uuid,
new_attrs->usage_type, new_attrs->usage_type,
virSecretUsageIDForDef(new_attrs)); new_attrs->usage_id);
new_attrs = NULL; new_attrs = NULL;
goto cleanup; goto cleanup;
......
...@@ -630,7 +630,7 @@ virStorageGenerateQcowEncryption(virConnectPtr conn, ...@@ -630,7 +630,7 @@ virStorageGenerateQcowEncryption(virConnectPtr conn,
goto cleanup; goto cleanup;
def->usage_type = VIR_SECRET_USAGE_TYPE_VOLUME; def->usage_type = VIR_SECRET_USAGE_TYPE_VOLUME;
if (VIR_STRDUP(def->usage.volume, vol->target.path) < 0) if (VIR_STRDUP(def->usage_id, vol->target.path) < 0)
goto cleanup; goto cleanup;
xml = virSecretDefFormat(def); xml = virSecretDefFormat(def);
virSecretDefFree(def); virSecretDefFree(def);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册