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).
This commit is contained in:
Peter Krempa 2016-05-13 13:32:48 +02:00
parent eb2116fd9a
commit 1d632c3924
5 changed files with 9 additions and 42 deletions

View File

@ -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

View File

@ -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)))

View File

@ -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;

View File

@ -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;

View File

@ -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__ */