2016-04-04 17:31:29 +00:00
|
|
|
/*
|
|
|
|
* secret_util.c: secret related utility functions
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "secret_util.h"
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virobject.h"
|
|
|
|
#include "viruuid.h"
|
|
|
|
#include "datatypes.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_SECRET
|
|
|
|
|
|
|
|
VIR_LOG_INIT("secret.secret_util");
|
|
|
|
|
|
|
|
|
|
|
|
/* virSecretGetSecretString:
|
|
|
|
* @conn: Pointer to the connection driver to make secret driver call
|
2016-05-28 12:43:23 +00:00
|
|
|
* @seclookupdef: Secret lookup def
|
|
|
|
* @secretUsageType: Type of secret usage for usage lookup
|
2016-05-12 15:43:39 +00:00
|
|
|
* @secret: returned secret as a sized stream of unsigned chars
|
|
|
|
* @secret_size: Return size of the secret - either raw text or base64
|
2016-04-04 17:31:29 +00:00
|
|
|
*
|
2016-05-28 12:43:23 +00:00
|
|
|
* Lookup the secret for the usage type and return it as raw text.
|
2016-05-12 15:43:39 +00:00
|
|
|
* It is up to the caller to encode the secret further.
|
2016-04-04 17:31:29 +00:00
|
|
|
*
|
2016-05-12 15:43:39 +00:00
|
|
|
* Returns 0 on success, -1 on failure. On success the memory in secret
|
|
|
|
* needs to be cleared and free'd after usage.
|
2016-04-04 17:31:29 +00:00
|
|
|
*/
|
2016-05-12 15:43:39 +00:00
|
|
|
int
|
2016-04-04 17:31:29 +00:00
|
|
|
virSecretGetSecretString(virConnectPtr conn,
|
2016-05-28 12:43:23 +00:00
|
|
|
virSecretLookupTypeDefPtr seclookupdef,
|
2016-05-12 15:43:39 +00:00
|
|
|
virSecretUsageType secretUsageType,
|
|
|
|
uint8_t **secret,
|
|
|
|
size_t *secret_size)
|
2016-04-04 17:31:29 +00:00
|
|
|
{
|
|
|
|
virSecretPtr sec = NULL;
|
2016-05-12 15:43:39 +00:00
|
|
|
int ret = -1;
|
2016-04-04 17:31:29 +00:00
|
|
|
|
2016-05-28 12:43:23 +00:00
|
|
|
switch (seclookupdef->type) {
|
|
|
|
case VIR_SECRET_LOOKUP_TYPE_UUID:
|
|
|
|
sec = conn->secretDriver->secretLookupByUUID(conn, seclookupdef->u.uuid);
|
2016-04-04 17:31:29 +00:00
|
|
|
break;
|
2016-05-13 11:32:48 +00:00
|
|
|
|
2016-05-28 12:43:23 +00:00
|
|
|
case VIR_SECRET_LOOKUP_TYPE_USAGE:
|
2016-05-13 11:32:48 +00:00
|
|
|
sec = conn->secretDriver->secretLookupByUsage(conn, secretUsageType,
|
2016-05-28 12:43:23 +00:00
|
|
|
seclookupdef->u.usage);
|
2016-04-04 17:31:29 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-05-13 11:32:48 +00:00
|
|
|
if (!sec)
|
2016-04-04 17:31:29 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2018-12-04 20:15:22 +00:00
|
|
|
/* NB: NONE is a byproduct of the qemuxml2argvtest test mocking
|
|
|
|
* for UUID lookups. Normal secret XML processing would fail if
|
|
|
|
* the usage type was NONE and since we have no way to set the
|
|
|
|
* expected usage in that environment, let's just accept NONE */
|
|
|
|
if (sec->usageType != VIR_SECRET_USAGE_TYPE_NONE &&
|
|
|
|
sec->usageType != secretUsageType) {
|
|
|
|
char uuidstr[VIR_UUID_STRING_BUFLEN];
|
|
|
|
|
|
|
|
virUUIDFormat(seclookupdef->u.uuid, uuidstr);
|
|
|
|
virReportError(VIR_ERR_INVALID_ARG,
|
|
|
|
_("secret with uuid %s is of type '%s' not "
|
|
|
|
"expected '%s' type"),
|
|
|
|
uuidstr, virSecretUsageTypeToString(sec->usageType),
|
|
|
|
virSecretUsageTypeToString(secretUsageType));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2016-05-12 15:43:39 +00:00
|
|
|
*secret = conn->secretDriver->secretGetValue(sec, secret_size, 0,
|
|
|
|
VIR_SECRET_GET_VALUE_INTERNAL_CALL);
|
2016-05-13 11:32:48 +00:00
|
|
|
|
2016-05-12 15:43:39 +00:00
|
|
|
if (!*secret)
|
2016-04-04 17:31:29 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2016-05-12 15:43:39 +00:00
|
|
|
ret = 0;
|
2016-04-04 17:31:29 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virObjectUnref(sec);
|
2016-05-12 15:43:39 +00:00
|
|
|
return ret;
|
2016-04-04 17:31:29 +00:00
|
|
|
}
|