From 1d632c392410d2be0c50b3a723f89884776b660b Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 13 May 2016 13:32:48 +0200 Subject: [PATCH] secret: util: Refactor virSecretGetSecretString Call the internal driver callbacks rather than the public APIs to avoid calling unnecessarily the error dispatching code and don't overwrite the error messages provided by the APIs. They are good enough to describe which secret is missing either by UUID or the usage (basically name). --- po/POTFILES.in | 1 - src/libxl/libxl_conf.c | 3 --- src/qemu/qemu_domain.c | 4 +--- src/secret/secret_util.c | 39 +++++++-------------------------------- src/secret/secret_util.h | 4 +--- 5 files changed, 9 insertions(+), 42 deletions(-) diff --git a/po/POTFILES.in b/po/POTFILES.in index 506d5352e1..0d924487f0 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -153,7 +153,6 @@ src/rpc/virnetsocket.c src/rpc/virnetsshsession.c src/rpc/virnettlscontext.c src/secret/secret_driver.c -src/secret/secret_util.c src/security/security_apparmor.c src/security/security_dac.c src/security/security_driver.c diff --git a/src/libxl/libxl_conf.c b/src/libxl/libxl_conf.c index 3c388c7796..6583afb27c 100644 --- a/src/libxl/libxl_conf.c +++ b/src/libxl/libxl_conf.c @@ -1027,14 +1027,11 @@ libxlMakeNetworkDiskSrc(virStorageSourcePtr src, char **srcstr) *srcstr = NULL; if (src->auth && src->protocol == VIR_STORAGE_NET_PROTOCOL_RBD) { - const char *protocol = virStorageNetProtocolTypeToString(src->protocol); - username = src->auth->username; if (!(conn = virConnectOpen("xen:///system"))) goto cleanup; if (!(secret = virSecretGetSecretString(conn, - protocol, true, src->auth, VIR_SECRET_USAGE_TYPE_CEPH))) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index b0eb3b6632..63075e6a75 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -872,7 +872,6 @@ qemuDomainSecretPlainSetup(virConnectPtr conn, { bool encode = false; int secretType = VIR_SECRET_USAGE_TYPE_ISCSI; - const char *protocolstr = virStorageNetProtocolTypeToString(protocol); secinfo->type = VIR_DOMAIN_SECRET_INFO_TYPE_PLAIN; if (VIR_STRDUP(secinfo->s.plain.username, authdef->username) < 0) @@ -885,8 +884,7 @@ qemuDomainSecretPlainSetup(virConnectPtr conn, } if (!(secinfo->s.plain.secret = - virSecretGetSecretString(conn, protocolstr, encode, - authdef, secretType))) + virSecretGetSecretString(conn, encode, authdef, secretType))) return -1; return 0; diff --git a/src/secret/secret_util.c b/src/secret/secret_util.c index 217584f8c4..d69f7ba9e0 100644 --- a/src/secret/secret_util.c +++ b/src/secret/secret_util.c @@ -37,7 +37,6 @@ VIR_LOG_INIT("secret.secret_util"); /* virSecretGetSecretString: * @conn: Pointer to the connection driver to make secret driver call - * @scheme: Unique enough string for error message to help determine cause * @encoded: Whether the returned secret needs to be base64 encoded * @authdef: Pointer to the disk storage authentication * @secretUsageType: Type of secret usage for authdef lookup @@ -50,7 +49,6 @@ VIR_LOG_INIT("secret.secret_util"); */ char * virSecretGetSecretString(virConnectPtr conn, - const char *scheme, bool encoded, virStorageAuthDefPtr authdef, virSecretUsageType secretUsageType) @@ -58,49 +56,26 @@ virSecretGetSecretString(virConnectPtr conn, size_t secret_size; virSecretPtr sec = NULL; char *secret = NULL; - char uuidStr[VIR_UUID_STRING_BUFLEN]; - /* look up secret */ switch (authdef->secretType) { case VIR_STORAGE_SECRET_TYPE_UUID: - sec = virSecretLookupByUUID(conn, authdef->secret.uuid); - virUUIDFormat(authdef->secret.uuid, uuidStr); + sec = conn->secretDriver->secretLookupByUUID(conn, authdef->secret.uuid); break; + case VIR_STORAGE_SECRET_TYPE_USAGE: - sec = virSecretLookupByUsage(conn, secretUsageType, - authdef->secret.usage); + sec = conn->secretDriver->secretLookupByUsage(conn, secretUsageType, + authdef->secret.usage); break; } - if (!sec) { - if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_UUID) { - virReportError(VIR_ERR_NO_SECRET, - _("%s no secret matches uuid '%s'"), - scheme, uuidStr); - } else { - virReportError(VIR_ERR_NO_SECRET, - _("%s no secret matches usage value '%s'"), - scheme, authdef->secret.usage); - } + if (!sec) goto cleanup; - } secret = (char *)conn->secretDriver->secretGetValue(sec, &secret_size, 0, VIR_SECRET_GET_VALUE_INTERNAL_CALL); - if (!secret) { - if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_UUID) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("could not get value of the secret for " - "username '%s' using uuid '%s'"), - authdef->username, uuidStr); - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("could not get value of the secret for " - "username '%s' using usage value '%s'"), - authdef->username, authdef->secret.usage); - } + + if (!secret) goto cleanup; - } if (encoded) { char *base64 = NULL; diff --git a/src/secret/secret_util.h b/src/secret/secret_util.h index c707599f9a..00864493a3 100644 --- a/src/secret/secret_util.h +++ b/src/secret/secret_util.h @@ -26,10 +26,8 @@ # include "virstoragefile.h" char *virSecretGetSecretString(virConnectPtr conn, - const char *scheme, bool encoded, virStorageAuthDefPtr authdef, virSecretUsageType secretUsageType) - ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4) - ATTRIBUTE_RETURN_CHECK; + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3) ATTRIBUTE_RETURN_CHECK; #endif /* __VIR_SECRET_H__ */