virsh-secret.c 14.2 KB
Newer Older
1 2 3
/*
 * virsh-secret.c: Commands to manage secret
 *
4
 * Copyright (C) 2005, 2007-2015 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
17
 * License along with this library.  If not, see
18 19 20 21 22 23 24 25
 * <http://www.gnu.org/licenses/>.
 *
 *  Daniel Veillard <veillard@redhat.com>
 *  Karel Zak <kzak@redhat.com>
 *  Daniel P. Berrange <berrange@redhat.com>
 *
 */

E
Eric Blake 已提交
26 27 28 29 30
#include <config.h>
#include "virsh-secret.h"

#include "internal.h"
#include "base64.h"
31
#include "virbuffer.h"
32
#include "viralloc.h"
33
#include "virfile.h"
34
#include "virutil.h"
J
John Ferlan 已提交
35
#include "conf/secret_conf.h"
E
Eric Blake 已提交
36

37
static virSecretPtr
38
virshCommandOptSecret(vshControl *ctl, const vshCmd *cmd, const char **name)
39 40 41 42
{
    virSecretPtr secret = NULL;
    const char *n = NULL;
    const char *optname = "secret";
43
    virshControlPtr priv = ctl->privData;
44

45
    if (vshCommandOptStringReq(ctl, cmd, optname, &n) < 0)
46 47 48 49 50 51 52 53
        return NULL;

    vshDebug(ctl, VSH_ERR_DEBUG,
             "%s: found option <%s>: %s\n", cmd->def->name, optname, n);

    if (name != NULL)
        *name = n;

54
    secret = virSecretLookupByUUIDString(priv->conn, n);
55 56 57 58 59 60 61 62 63 64 65

    if (secret == NULL)
        vshError(ctl, _("failed to get secret '%s'"), n);

    return secret;
}

/*
 * "secret-define" command
 */
static const vshCmdInfo info_secret_define[] = {
66 67 68 69 70 71 72
    {.name = "help",
     .data = N_("define or modify a secret from an XML file")
    },
    {.name = "desc",
     .data = N_("Define or modify a secret.")
    },
    {.name = NULL}
73 74 75
};

static const vshCmdOptDef opts_secret_define[] = {
76 77 78 79 80 81
    {.name = "file",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("file containing secret attributes in XML")
    },
    {.name = NULL}
82 83 84 85 86 87 88 89 90
};

static bool
cmdSecretDefine(vshControl *ctl, const vshCmd *cmd)
{
    const char *from = NULL;
    char *buffer;
    virSecretPtr res;
    char uuid[VIR_UUID_STRING_BUFLEN];
91
    bool ret = false;
92
    virshControlPtr priv = ctl->privData;
93

94
    if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0)
95 96
        return false;

E
Eric Blake 已提交
97
    if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
98 99
        return false;

100
    if (!(res = virSecretDefineXML(priv->conn, buffer, 0))) {
101
        vshError(ctl, _("Failed to set attributes from %s"), from);
102
        goto cleanup;
103
    }
104

105 106
    if (virSecretGetUUIDString(res, &(uuid[0])) < 0) {
        vshError(ctl, "%s", _("Failed to get UUID of created secret"));
107
        goto cleanup;
108
    }
109

110
    vshPrint(ctl, _("Secret %s created\n"), uuid);
111 112
    ret = true;

113
 cleanup:
114
    VIR_FREE(buffer);
115 116
    if (res)
        virSecretFree(res);
117
    return ret;
118 119 120 121 122 123
}

/*
 * "secret-dumpxml" command
 */
static const vshCmdInfo info_secret_dumpxml[] = {
124 125 126 127 128 129 130
    {.name = "help",
     .data = N_("secret attributes in XML")
    },
    {.name = "desc",
     .data = N_("Output attributes of a secret as an XML dump to stdout.")
    },
    {.name = NULL}
131 132 133
};

static const vshCmdOptDef opts_secret_dumpxml[] = {
134 135 136 137 138 139
    {.name = "secret",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("secret UUID")
    },
    {.name = NULL}
140 141 142 143 144 145 146 147 148
};

static bool
cmdSecretDumpXML(vshControl *ctl, const vshCmd *cmd)
{
    virSecretPtr secret;
    bool ret = false;
    char *xml;

149
    secret = virshCommandOptSecret(ctl, cmd, NULL);
150 151 152 153 154 155 156 157 158 159
    if (secret == NULL)
        return false;

    xml = virSecretGetXMLDesc(secret, 0);
    if (xml == NULL)
        goto cleanup;
    vshPrint(ctl, "%s", xml);
    VIR_FREE(xml);
    ret = true;

160
 cleanup:
161 162 163 164 165 166 167 168
    virSecretFree(secret);
    return ret;
}

/*
 * "secret-set-value" command
 */
static const vshCmdInfo info_secret_set_value[] = {
169 170 171 172 173 174 175
    {.name = "help",
     .data = N_("set a secret value")
    },
    {.name = "desc",
     .data = N_("Set a secret value.")
    },
    {.name = NULL}
176 177 178
};

static const vshCmdOptDef opts_secret_set_value[] = {
179 180 181 182 183 184 185 186 187 188 189
    {.name = "secret",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("secret UUID")
    },
    {.name = "base64",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("base64-encoded secret value")
    },
    {.name = NULL}
190 191 192 193 194 195 196 197 198 199 200 201
};

static bool
cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd)
{
    virSecretPtr secret;
    size_t value_size;
    const char *base64 = NULL;
    char *value;
    int res;
    bool ret = false;

202
    if (!(secret = virshCommandOptSecret(ctl, cmd, NULL)))
203 204
        return false;

205
    if (vshCommandOptStringReq(ctl, cmd, "base64", &base64) < 0)
206 207 208 209 210 211 212 213
        goto cleanup;

    if (!base64_decode_alloc(base64, strlen(base64), &value, &value_size)) {
        vshError(ctl, "%s", _("Invalid base64 data"));
        goto cleanup;
    }
    if (value == NULL) {
        vshError(ctl, "%s", _("Failed to allocate memory"));
214
        goto cleanup;
215 216 217 218 219 220 221 222 223 224 225 226 227
    }

    res = virSecretSetValue(secret, (unsigned char *)value, value_size, 0);
    memset(value, 0, value_size);
    VIR_FREE(value);

    if (res != 0) {
        vshError(ctl, "%s", _("Failed to set secret value"));
        goto cleanup;
    }
    vshPrint(ctl, "%s", _("Secret value set\n"));
    ret = true;

228
 cleanup:
229 230 231 232 233 234 235 236
    virSecretFree(secret);
    return ret;
}

/*
 * "secret-get-value" command
 */
static const vshCmdInfo info_secret_get_value[] = {
237 238 239 240 241 242 243
    {.name = "help",
     .data = N_("Output a secret value")
    },
    {.name = "desc",
     .data = N_("Output a secret value to stdout.")
    },
    {.name = NULL}
244 245 246
};

static const vshCmdOptDef opts_secret_get_value[] = {
247 248 249 250 251 252
    {.name = "secret",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("secret UUID")
    },
    {.name = NULL}
253 254 255 256 257 258 259 260 261 262 263
};

static bool
cmdSecretGetValue(vshControl *ctl, const vshCmd *cmd)
{
    virSecretPtr secret;
    char *base64;
    unsigned char *value;
    size_t value_size;
    bool ret = false;

264
    secret = virshCommandOptSecret(ctl, cmd, NULL);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    if (secret == NULL)
        return false;

    value = virSecretGetValue(secret, &value_size, 0);
    if (value == NULL)
        goto cleanup;

    base64_encode_alloc((char *)value, value_size, &base64);
    memset(value, 0, value_size);
    VIR_FREE(value);

    if (base64 == NULL) {
        vshError(ctl, "%s", _("Failed to allocate memory"));
        goto cleanup;
    }
    vshPrint(ctl, "%s", base64);
    memset(base64, 0, strlen(base64));
    VIR_FREE(base64);
    ret = true;

285
 cleanup:
286 287 288 289 290 291 292 293
    virSecretFree(secret);
    return ret;
}

/*
 * "secret-undefine" command
 */
static const vshCmdInfo info_secret_undefine[] = {
294 295 296 297 298 299 300
    {.name = "help",
     .data = N_("undefine a secret")
    },
    {.name = "desc",
     .data = N_("Undefine a secret.")
    },
    {.name = NULL}
301 302 303
};

static const vshCmdOptDef opts_secret_undefine[] = {
304 305 306 307 308 309
    {.name = "secret",
     .type = VSH_OT_DATA,
     .flags = VSH_OFLAG_REQ,
     .help = N_("secret UUID")
    },
    {.name = NULL}
310 311 312 313 314 315 316 317 318
};

static bool
cmdSecretUndefine(vshControl *ctl, const vshCmd *cmd)
{
    virSecretPtr secret;
    bool ret = false;
    const char *uuid;

319
    secret = virshCommandOptSecret(ctl, cmd, &uuid);
320 321 322 323 324 325 326 327 328 329
    if (secret == NULL)
        return false;

    if (virSecretUndefine(secret) < 0) {
        vshError(ctl, _("Failed to delete secret %s"), uuid);
        goto cleanup;
    }
    vshPrint(ctl, _("Secret %s deleted\n"), uuid);
    ret = true;

330
 cleanup:
331 332 333 334
    virSecretFree(secret);
    return ret;
}

335
static int
336
virshSecretSorter(const void *a, const void *b)
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
{
    virSecretPtr *sa = (virSecretPtr *) a;
    virSecretPtr *sb = (virSecretPtr *) b;
    char uuid_sa[VIR_UUID_STRING_BUFLEN];
    char uuid_sb[VIR_UUID_STRING_BUFLEN];

    if (*sa && !*sb)
        return -1;

    if (!*sa)
        return *sb != NULL;

    virSecretGetUUIDString(*sa, uuid_sa);
    virSecretGetUUIDString(*sb, uuid_sb);

    return vshStrcasecmp(uuid_sa, uuid_sb);
}

355
struct virshSecretList {
356 357 358
    virSecretPtr *secrets;
    size_t nsecrets;
};
359
typedef struct virshSecretList *virshSecretListPtr;
360 361

static void
362
virshSecretListFree(virshSecretListPtr list)
363
{
364
    size_t i;
365

366
    if (list && list->secrets) {
367 368 369 370 371 372 373 374 375
        for (i = 0; i < list->nsecrets; i++) {
            if (list->secrets[i])
                virSecretFree(list->secrets[i]);
        }
        VIR_FREE(list->secrets);
    }
    VIR_FREE(list);
}

376 377 378
static virshSecretListPtr
virshSecretListCollect(vshControl *ctl,
                       unsigned int flags)
379
{
380
    virshSecretListPtr list = vshMalloc(ctl, sizeof(*list));
381
    size_t i;
382 383 384 385 386 387
    int ret;
    virSecretPtr secret;
    bool success = false;
    size_t deleted = 0;
    int nsecrets = 0;
    char **uuids = NULL;
388
    virshControlPtr priv = ctl->privData;
389 390

    /* try the list with flags support (0.10.2 and later) */
391
    if ((ret = virConnectListAllSecrets(priv->conn,
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
                                        &list->secrets,
                                        flags)) >= 0) {
        list->nsecrets = ret;
        goto finished;
    }

    /* check if the command is actually supported */
    if (last_error && last_error->code == VIR_ERR_NO_SUPPORT)
        goto fallback;

    /* there was an error during the call */
    vshError(ctl, "%s", _("Failed to list node secrets"));
    goto cleanup;


407
 fallback:
408 409 410 411 412 413 414 415
    /* fall back to old method (0.10.1 and older) */
    vshResetLibvirtError();

    if (flags) {
        vshError(ctl, "%s", _("Filtering is not supported by this libvirt"));
        goto cleanup;
    }

416
    nsecrets = virConnectNumOfSecrets(priv->conn);
417 418 419 420 421 422 423 424 425 426
    if (nsecrets < 0) {
        vshError(ctl, "%s", _("Failed to count secrets"));
        goto cleanup;
    }

    if (nsecrets == 0)
        return list;

    uuids = vshMalloc(ctl, sizeof(char *) * nsecrets);

427
    nsecrets = virConnectListSecrets(priv->conn, uuids, nsecrets);
428 429 430 431 432 433 434 435 436
    if (nsecrets < 0) {
        vshError(ctl, "%s", _("Failed to list secrets"));
        goto cleanup;
    }

    list->secrets = vshMalloc(ctl, sizeof(virSecretPtr) * (nsecrets));
    list->nsecrets = 0;

    /* get the secrets */
437
    for (i = 0; i < nsecrets; i++) {
438
        if (!(secret = virSecretLookupByUUIDString(priv->conn, uuids[i])))
439 440 441 442 443 444 445
            continue;
        list->secrets[list->nsecrets++] = secret;
    }

    /* truncate secrets that weren't found */
    deleted = nsecrets - list->nsecrets;

446
 finished:
447 448 449
    /* sort the list */
    if (list->secrets && list->nsecrets)
        qsort(list->secrets, list->nsecrets,
450
              sizeof(*list->secrets), virshSecretSorter);
451 452 453 454 455 456 457

    /* truncate the list for not found secret objects */
    if (deleted)
        VIR_SHRINK_N(list->secrets, list->nsecrets, deleted);

    success = true;

458
 cleanup:
459 460 461 462 463
    if (nsecrets > 0) {
        for (i = 0; i < nsecrets; i++)
            VIR_FREE(uuids[i]);
        VIR_FREE(uuids);
    }
464 465

    if (!success) {
466
        virshSecretListFree(list);
467 468 469 470 471 472
        list = NULL;
    }

    return list;
}

473 474 475 476
/*
 * "secret-list" command
 */
static const vshCmdInfo info_secret_list[] = {
477 478 479 480 481 482 483
    {.name = "help",
     .data = N_("list secrets")
    },
    {.name = "desc",
     .data = N_("Returns a list of secrets")
    },
    {.name = NULL}
484 485
};

486
static const vshCmdOptDef opts_secret_list[] = {
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
    {.name = "ephemeral",
     .type = VSH_OT_BOOL,
     .help = N_("list ephemeral secrets")
    },
    {.name = "no-ephemeral",
     .type = VSH_OT_BOOL,
     .help = N_("list non-ephemeral secrets")
    },
    {.name = "private",
     .type = VSH_OT_BOOL,
     .help = N_("list private secrets")
    },
    {.name = "no-private",
     .type = VSH_OT_BOOL,
     .help = N_("list non-private secrets")
    },
    {.name = NULL}
504 505
};

506 507 508
static bool
cmdSecretList(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_UNUSED)
{
509
    size_t i;
510
    virshSecretListPtr list = NULL;
511 512
    bool ret = false;
    unsigned int flags = 0;
513

514 515
    if (vshCommandOptBool(cmd, "ephemeral"))
        flags |= VIR_CONNECT_LIST_SECRETS_EPHEMERAL;
516

517 518 519 520 521
    if (vshCommandOptBool(cmd, "no-ephemeral"))
        flags |= VIR_CONNECT_LIST_SECRETS_NO_EPHEMERAL;

    if (vshCommandOptBool(cmd, "private"))
        flags |= VIR_CONNECT_LIST_SECRETS_PRIVATE;
522

523 524 525
    if (vshCommandOptBool(cmd, "no-private"))
        flags |= VIR_CONNECT_LIST_SECRETS_NO_PRIVATE;

526
    if (!(list = virshSecretListCollect(ctl, flags)))
527
        return false;
528

529 530 531
    vshPrintExtra(ctl, " %-36s  %s\n", _("UUID"), _("Usage"));
    vshPrintExtra(ctl, "----------------------------------------"
                       "----------------------------------------\n");
532

533 534
    for (i = 0; i < list->nsecrets; i++) {
        virSecretPtr sec = list->secrets[i];
J
John Ferlan 已提交
535
        int usageType = virSecretGetUsageType(sec);
536
        const char *usageStr = virSecretUsageTypeToString(usageType);
537
        char uuid[VIR_UUID_STRING_BUFLEN];
J
John Ferlan 已提交
538

539
        if (virSecretGetUUIDString(sec, uuid) < 0) {
540 541 542 543
            vshError(ctl, "%s", _("Failed to get uuid of secret"));
            goto cleanup;
        }

544
        if (usageType) {
545
            vshPrint(ctl, " %-36s  %s %s\n",
J
John Ferlan 已提交
546
                     uuid, usageStr,
547 548
                     virSecretGetUsageID(sec));
        } else {
549
            vshPrint(ctl, " %-36s  %s\n",
550
                     uuid, _("Unused"));
551 552
        }
    }
553 554 555

    ret = true;

556
 cleanup:
557
    virshSecretListFree(list);
558
    return ret;
559
}
560

E
Eric Blake 已提交
561
const vshCmdDef secretCmds[] = {
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
    {.name = "secret-define",
     .handler = cmdSecretDefine,
     .opts = opts_secret_define,
     .info = info_secret_define,
     .flags = 0
    },
    {.name = "secret-dumpxml",
     .handler = cmdSecretDumpXML,
     .opts = opts_secret_dumpxml,
     .info = info_secret_dumpxml,
     .flags = 0
    },
    {.name = "secret-get-value",
     .handler = cmdSecretGetValue,
     .opts = opts_secret_get_value,
     .info = info_secret_get_value,
     .flags = 0
    },
    {.name = "secret-list",
     .handler = cmdSecretList,
     .opts = opts_secret_list,
     .info = info_secret_list,
     .flags = 0
    },
    {.name = "secret-set-value",
     .handler = cmdSecretSetValue,
     .opts = opts_secret_set_value,
     .info = info_secret_set_value,
     .flags = 0
    },
    {.name = "secret-undefine",
     .handler = cmdSecretUndefine,
     .opts = opts_secret_undefine,
     .info = info_secret_undefine,
     .flags = 0
    },
    {.name = NULL}
599
};