提交 1eca5f65 编写于 作者: J John Ferlan

secret: Move virStorageSecretType and rename

Move the enum into a new src/util/virsecret.h, rename it to be
virSecretLookupType. Add a src/util/virsecret.h in order to perform
a couple of simple operations on the secret XML and virSecretLookupTypeDef
for clearing and copying.

This includes quite a bit of collateral damage, but the goal is to remove
the "virStorage*" and replace with the virSecretLookupType so that it's
easier to to add new lookups that aren't necessarily storage pool related.
Signed-off-by: NJohn Ferlan <jferlan@redhat.com>
上级 e808d3f2
...@@ -161,6 +161,7 @@ UTIL_SOURCES = \ ...@@ -161,6 +161,7 @@ UTIL_SOURCES = \
util/virrotatingfile.h util/virrotatingfile.c \ util/virrotatingfile.h util/virrotatingfile.c \
util/virscsi.c util/virscsi.h \ util/virscsi.c util/virscsi.h \
util/virseclabel.c util/virseclabel.h \ util/virseclabel.c util/virseclabel.h \
util/virsecret.c util/virsecret.h \
util/virsexpr.c util/virsexpr.h \ util/virsexpr.c util/virsexpr.h \
util/virsocketaddr.h util/virsocketaddr.c \ util/virsocketaddr.h util/virsocketaddr.c \
util/virstats.c util/virstats.h \ util/virstats.c util/virstats.h \
......
...@@ -35,7 +35,7 @@ struct _virSecretDef { ...@@ -35,7 +35,7 @@ struct _virSecretDef {
bool isprivate; bool isprivate;
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; int usage_type; /* virSecretUsageType */
union { union {
char *volume; /* May be NULL */ char *volume; /* May be NULL */
char *ceph; char *ceph;
......
...@@ -2218,6 +2218,11 @@ virSecurityLabelDefFree; ...@@ -2218,6 +2218,11 @@ virSecurityLabelDefFree;
virSecurityLabelDefNew; virSecurityLabelDefNew;
# util/virsecret.h
virSecretLookupDefClear;
virSecretLookupDefCopy;
# util/virsexpr.h # util/virsexpr.h
sexpr2string; sexpr2string;
sexpr_append; sexpr_append;
......
...@@ -656,7 +656,7 @@ libxlMakeNetworkDiskSrc(virStorageSourcePtr src, char **srcstr) ...@@ -656,7 +656,7 @@ libxlMakeNetworkDiskSrc(virStorageSourcePtr src, char **srcstr)
if (!(conn = virConnectOpen("xen:///system"))) if (!(conn = virConnectOpen("xen:///system")))
goto cleanup; goto cleanup;
if (virSecretGetSecretString(conn, src->auth, if (virSecretGetSecretString(conn, &src->auth->seclookupdef,
VIR_SECRET_USAGE_TYPE_CEPH, VIR_SECRET_USAGE_TYPE_CEPH,
&secret, &secretlen) < 0) &secret, &secretlen) < 0)
goto cleanup; goto cleanup;
......
...@@ -835,7 +835,7 @@ qemuDomainSecretPlainSetup(virConnectPtr conn, ...@@ -835,7 +835,7 @@ qemuDomainSecretPlainSetup(virConnectPtr conn,
if (protocol == VIR_STORAGE_NET_PROTOCOL_RBD) if (protocol == VIR_STORAGE_NET_PROTOCOL_RBD)
secretType = VIR_SECRET_USAGE_TYPE_CEPH; secretType = VIR_SECRET_USAGE_TYPE_CEPH;
return virSecretGetSecretString(conn, authdef, secretType, return virSecretGetSecretString(conn, &authdef->seclookupdef, secretType,
&secinfo->s.plain.secret, &secinfo->s.plain.secret,
&secinfo->s.plain.secretlen); &secinfo->s.plain.secretlen);
} }
...@@ -908,7 +908,7 @@ qemuDomainSecretAESSetup(virConnectPtr conn, ...@@ -908,7 +908,7 @@ qemuDomainSecretAESSetup(virConnectPtr conn,
goto cleanup; goto cleanup;
/* Grab the unencoded secret */ /* Grab the unencoded secret */
if (virSecretGetSecretString(conn, authdef, secretType, if (virSecretGetSecretString(conn, &authdef->seclookupdef, secretType,
&secret, &secretlen) < 0) &secret, &secretlen) < 0)
goto cleanup; goto cleanup;
......
...@@ -36,12 +36,12 @@ VIR_LOG_INIT("secret.secret_util"); ...@@ -36,12 +36,12 @@ VIR_LOG_INIT("secret.secret_util");
/* virSecretGetSecretString: /* virSecretGetSecretString:
* @conn: Pointer to the connection driver to make secret driver call * @conn: Pointer to the connection driver to make secret driver call
* @authdef: Pointer to the disk storage authentication * @seclookupdef: Secret lookup def
* @secretUsageType: Type of secret usage for authdef lookup * @secretUsageType: Type of secret usage for usage lookup
* @secret: returned secret as a sized stream of unsigned chars * @secret: returned secret as a sized stream of unsigned chars
* @secret_size: Return size of the secret - either raw text or base64 * @secret_size: Return size of the secret - either raw text or base64
* *
* Lookup the secret for the authdef usage type and return it as raw text. * Lookup the secret for the usage type and return it as raw text.
* It is up to the caller to encode the secret further. * It is up to the caller to encode the secret further.
* *
* Returns 0 on success, -1 on failure. On success the memory in secret * Returns 0 on success, -1 on failure. On success the memory in secret
...@@ -49,7 +49,7 @@ VIR_LOG_INIT("secret.secret_util"); ...@@ -49,7 +49,7 @@ VIR_LOG_INIT("secret.secret_util");
*/ */
int int
virSecretGetSecretString(virConnectPtr conn, virSecretGetSecretString(virConnectPtr conn,
virStorageAuthDefPtr authdef, virSecretLookupTypeDefPtr seclookupdef,
virSecretUsageType secretUsageType, virSecretUsageType secretUsageType,
uint8_t **secret, uint8_t **secret,
size_t *secret_size) size_t *secret_size)
...@@ -57,14 +57,14 @@ virSecretGetSecretString(virConnectPtr conn, ...@@ -57,14 +57,14 @@ virSecretGetSecretString(virConnectPtr conn,
virSecretPtr sec = NULL; virSecretPtr sec = NULL;
int ret = -1; int ret = -1;
switch (authdef->secretType) { switch (seclookupdef->type) {
case VIR_STORAGE_SECRET_TYPE_UUID: case VIR_SECRET_LOOKUP_TYPE_UUID:
sec = conn->secretDriver->secretLookupByUUID(conn, authdef->secret.uuid); sec = conn->secretDriver->secretLookupByUUID(conn, seclookupdef->u.uuid);
break; break;
case VIR_STORAGE_SECRET_TYPE_USAGE: case VIR_SECRET_LOOKUP_TYPE_USAGE:
sec = conn->secretDriver->secretLookupByUsage(conn, secretUsageType, sec = conn->secretDriver->secretLookupByUsage(conn, secretUsageType,
authdef->secret.usage); seclookupdef->u.usage);
break; break;
} }
......
...@@ -19,17 +19,17 @@ ...@@ -19,17 +19,17 @@
* *
*/ */
#ifndef __VIR_SECRET_H__ #ifndef __VIR_SECRET_UTIL_H__
# define __VIR_SECRET_H__ # define __VIR_SECRET_UTIL_H__
# include "internal.h" # include "internal.h"
# include "virstoragefile.h" # include "virsecret.h"
int virSecretGetSecretString(virConnectPtr conn, int virSecretGetSecretString(virConnectPtr conn,
virStorageAuthDefPtr authdef, virSecretLookupTypeDefPtr seclookupdef,
virSecretUsageType secretUsageType, virSecretUsageType secretUsageType,
uint8_t **ret_secret, uint8_t **ret_secret,
size_t *ret_secret_size) size_t *ret_secret_size)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(4)
ATTRIBUTE_NONNULL(5) ATTRIBUTE_RETURN_CHECK; ATTRIBUTE_NONNULL(5) ATTRIBUTE_RETURN_CHECK;
#endif /* __VIR_SECRET_H__ */ #endif /* __VIR_SECRET_UTIL_H__ */
...@@ -286,8 +286,8 @@ virStorageBackendISCSISetAuth(const char *portal, ...@@ -286,8 +286,8 @@ virStorageBackendISCSISetAuth(const char *portal,
if (!authdef || authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE) if (!authdef || authdef->authType == VIR_STORAGE_AUTH_TYPE_NONE)
return 0; return 0;
VIR_DEBUG("username='%s' authType=%d secretType=%d", VIR_DEBUG("username='%s' authType=%d seclookupdef.type=%d",
authdef->username, authdef->authType, authdef->secretType); authdef->username, authdef->authType, authdef->seclookupdef.type);
if (authdef->authType != VIR_STORAGE_AUTH_TYPE_CHAP) { if (authdef->authType != VIR_STORAGE_AUTH_TYPE_CHAP) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("iscsi pool only supports 'chap' auth type")); _("iscsi pool only supports 'chap' auth type"));
...@@ -301,7 +301,8 @@ virStorageBackendISCSISetAuth(const char *portal, ...@@ -301,7 +301,8 @@ virStorageBackendISCSISetAuth(const char *portal,
return -1; return -1;
} }
if (virSecretGetSecretString(conn, authdef, VIR_SECRET_USAGE_TYPE_ISCSI, if (virSecretGetSecretString(conn, &authdef->seclookupdef,
VIR_SECRET_USAGE_TYPE_ISCSI,
&secret_value, &secret_size) < 0) &secret_value, &secret_size) < 0)
goto cleanup; goto cleanup;
......
...@@ -85,7 +85,8 @@ virStorageBackendRBDOpenRADOSConn(virStorageBackendRBDStatePtr ptr, ...@@ -85,7 +85,8 @@ virStorageBackendRBDOpenRADOSConn(virStorageBackendRBDStatePtr ptr,
return -1; return -1;
} }
if (virSecretGetSecretString(conn, authdef, VIR_SECRET_USAGE_TYPE_CEPH, if (virSecretGetSecretString(conn, &authdef->seclookupdef,
VIR_SECRET_USAGE_TYPE_CEPH,
&secret_value, &secret_value_size) < 0) &secret_value, &secret_value_size) < 0)
goto cleanup; goto cleanup;
......
/*
* virsecret.c: secret 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 "viralloc.h"
#include "virerror.h"
#include "virlog.h"
#include "virsecret.h"
#include "virstring.h"
#define VIR_FROM_THIS VIR_FROM_NONE
VIR_LOG_INIT("util.secret");
void
virSecretLookupDefClear(virSecretLookupTypeDefPtr def)
{
if (def->type == VIR_SECRET_LOOKUP_TYPE_USAGE)
VIR_FREE(def->u.usage);
else if (def->type == VIR_SECRET_LOOKUP_TYPE_UUID)
memset(&def->u.uuid, 0, VIR_UUID_BUFLEN);
}
int
virSecretLookupDefCopy(virSecretLookupTypeDefPtr dst,
const virSecretLookupTypeDef *src)
{
dst->type = src->type;
if (dst->type == VIR_SECRET_LOOKUP_TYPE_UUID) {
memcpy(dst->u.uuid, src->u.uuid, VIR_UUID_BUFLEN);
} else if (dst->type == VIR_SECRET_LOOKUP_TYPE_USAGE) {
if (VIR_STRDUP(dst->u.usage, src->u.usage) < 0)
return -1;
}
return 0;
}
/*
* virsecret.h: secret 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/>.
*
*/
#ifndef __VIR_SECRET_H__
# define __VIR_SECRET_H__
# include "internal.h"
typedef enum {
VIR_SECRET_LOOKUP_TYPE_NONE,
VIR_SECRET_LOOKUP_TYPE_UUID,
VIR_SECRET_LOOKUP_TYPE_USAGE,
VIR_SECRET_LOOKUP_TYPE_LAST
} virSecretLookupType;
typedef struct _virSecretLookupTypeDef virSecretLookupTypeDef;
typedef virSecretLookupTypeDef *virSecretLookupTypeDefPtr;
struct _virSecretLookupTypeDef {
int type; /* virSecretLookupType */
union {
unsigned char uuid[VIR_UUID_BUFLEN];
char *usage;
} u;
};
void virSecretLookupDefClear(virSecretLookupTypeDefPtr def);
int virSecretLookupDefCopy(virSecretLookupTypeDefPtr dst,
const virSecretLookupTypeDef *src);
#endif /* __VIR_SECRET_H__ */
...@@ -1506,8 +1506,7 @@ virStorageAuthDefFree(virStorageAuthDefPtr authdef) ...@@ -1506,8 +1506,7 @@ virStorageAuthDefFree(virStorageAuthDefPtr authdef)
VIR_FREE(authdef->username); VIR_FREE(authdef->username);
VIR_FREE(authdef->secrettype); VIR_FREE(authdef->secrettype);
if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_USAGE) virSecretLookupDefClear(&authdef->seclookupdef);
VIR_FREE(authdef->secret.usage);
VIR_FREE(authdef); VIR_FREE(authdef);
} }
...@@ -1526,13 +1525,10 @@ virStorageAuthDefCopy(const virStorageAuthDef *src) ...@@ -1526,13 +1525,10 @@ virStorageAuthDefCopy(const virStorageAuthDef *src)
if (VIR_STRDUP(ret->secrettype, src->secrettype) < 0) if (VIR_STRDUP(ret->secrettype, src->secrettype) < 0)
goto error; goto error;
ret->authType = src->authType; ret->authType = src->authType;
ret->secretType = src->secretType;
if (ret->secretType == VIR_STORAGE_SECRET_TYPE_UUID) { if (virSecretLookupDefCopy(&ret->seclookupdef, &src->seclookupdef) < 0)
memcpy(ret->secret.uuid, src->secret.uuid, sizeof(ret->secret.uuid)); goto error;
} else if (ret->secretType == VIR_STORAGE_SECRET_TYPE_USAGE) {
if (VIR_STRDUP(ret->secret.usage, src->secret.usage) < 0)
goto error;
}
return ret; return ret;
error: error:
...@@ -1573,16 +1569,16 @@ virStorageAuthDefParseSecret(xmlXPathContextPtr ctxt, ...@@ -1573,16 +1569,16 @@ virStorageAuthDefParseSecret(xmlXPathContextPtr ctxt,
} }
if (uuid) { if (uuid) {
if (virUUIDParse(uuid, authdef->secret.uuid) < 0) { if (virUUIDParse(uuid, authdef->seclookupdef.u.uuid) < 0) {
virReportError(VIR_ERR_XML_ERROR, "%s", virReportError(VIR_ERR_XML_ERROR, "%s",
_("invalid auth secret uuid")); _("invalid auth secret uuid"));
goto cleanup; goto cleanup;
} }
authdef->secretType = VIR_STORAGE_SECRET_TYPE_UUID; authdef->seclookupdef.type = VIR_SECRET_LOOKUP_TYPE_UUID;
} else { } else {
authdef->secret.usage = usage; authdef->seclookupdef.u.usage = usage;
usage = NULL; usage = NULL;
authdef->secretType = VIR_STORAGE_SECRET_TYPE_USAGE; authdef->seclookupdef.type = VIR_SECRET_LOOKUP_TYPE_USAGE;
} }
ret = 0; ret = 0;
...@@ -1625,7 +1621,7 @@ virStorageAuthDefParseXML(xmlXPathContextPtr ctxt) ...@@ -1625,7 +1621,7 @@ virStorageAuthDefParseXML(xmlXPathContextPtr ctxt)
VIR_FREE(authtype); VIR_FREE(authtype);
} }
authdef->secretType = VIR_STORAGE_SECRET_TYPE_NONE; authdef->seclookupdef.type = VIR_SECRET_LOOKUP_TYPE_NONE;
if (virStorageAuthDefParseSecret(ctxt, authdef) < 0) if (virStorageAuthDefParseSecret(ctxt, authdef) < 0)
goto error; goto error;
...@@ -1680,12 +1676,12 @@ virStorageAuthDefFormat(virBufferPtr buf, ...@@ -1680,12 +1676,12 @@ virStorageAuthDefFormat(virBufferPtr buf,
else else
virBufferAddLit(buf, "<secret"); virBufferAddLit(buf, "<secret");
if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_UUID) { if (authdef->seclookupdef.type == VIR_SECRET_LOOKUP_TYPE_UUID) {
virUUIDFormat(authdef->secret.uuid, uuidstr); virUUIDFormat(authdef->seclookupdef.u.uuid, uuidstr);
virBufferAsprintf(buf, " uuid='%s'/>\n", uuidstr); virBufferAsprintf(buf, " uuid='%s'/>\n", uuidstr);
} else if (authdef->secretType == VIR_STORAGE_SECRET_TYPE_USAGE) { } else if (authdef->seclookupdef.type == VIR_SECRET_LOOKUP_TYPE_USAGE) {
virBufferEscapeString(buf, " usage='%s'/>\n", virBufferEscapeString(buf, " usage='%s'/>\n",
authdef->secret.usage); authdef->seclookupdef.u.usage);
} else { } else {
virBufferAddLit(buf, "/>\n"); virBufferAddLit(buf, "/>\n");
} }
......
/* /*
* virstoragefile.h: file utility functions for FS storage backend * virstoragefile.h: file utility functions for FS storage backend
* *
* Copyright (C) 2007-2009, 2012-2014 Red Hat, Inc. * Copyright (C) 2007-2009, 2012-2016 Red Hat, Inc.
* Copyright (C) 2007-2008 Daniel P. Berrange * Copyright (C) 2007-2008 Daniel P. Berrange
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
# include "virseclabel.h" # include "virseclabel.h"
# include "virstorageencryption.h" # include "virstorageencryption.h"
# include "virutil.h" # include "virutil.h"
# include "virsecret.h"
/* Minimum header size required to probe all known formats with /* Minimum header size required to probe all known formats with
* virStorageFileProbeFormat, or obtain metadata from a known format. * virStorageFileProbeFormat, or obtain metadata from a known format.
...@@ -201,25 +202,13 @@ typedef enum { ...@@ -201,25 +202,13 @@ typedef enum {
} virStorageAuthType; } virStorageAuthType;
VIR_ENUM_DECL(virStorageAuth) VIR_ENUM_DECL(virStorageAuth)
typedef enum {
VIR_STORAGE_SECRET_TYPE_NONE,
VIR_STORAGE_SECRET_TYPE_UUID,
VIR_STORAGE_SECRET_TYPE_USAGE,
VIR_STORAGE_SECRET_TYPE_LAST
} virStorageSecretType;
typedef struct _virStorageAuthDef virStorageAuthDef; typedef struct _virStorageAuthDef virStorageAuthDef;
typedef virStorageAuthDef *virStorageAuthDefPtr; typedef virStorageAuthDef *virStorageAuthDefPtr;
struct _virStorageAuthDef { struct _virStorageAuthDef {
char *username; char *username;
char *secrettype; /* <secret type='%s' for disk source */ char *secrettype; /* <secret type='%s' for disk source */
int authType; /* virStorageAuthType */ int authType; /* virStorageAuthType */
int secretType; /* virStorageSecretType */ virSecretLookupTypeDef seclookupdef;
union {
unsigned char uuid[VIR_UUID_BUFLEN];
char *usage;
} secret;
}; };
typedef struct _virStorageDriverData virStorageDriverData; typedef struct _virStorageDriverData virStorageDriverData;
......
...@@ -36,8 +36,8 @@ static int testSanitizeDef(virDomainDefPtr vmdef) ...@@ -36,8 +36,8 @@ static int testSanitizeDef(virDomainDefPtr vmdef)
virDomainDiskDefPtr disk = vmdef->disks[i]; virDomainDiskDefPtr disk = vmdef->disks[i];
if (disk->src->auth) { if (disk->src->auth) {
disk->src->auth->secretType = VIR_STORAGE_SECRET_TYPE_USAGE; disk->src->auth->seclookupdef.type = VIR_SECRET_LOOKUP_TYPE_USAGE;
if (VIR_STRDUP(disk->src->auth->secret.usage, if (VIR_STRDUP(disk->src->auth->seclookupdef.u.usage,
"qemuargv2xml_usage") < 0) "qemuargv2xml_usage") < 0)
goto fail; goto fail;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册