Permalink
Browse files

Add support for secrets

Untested, no secrets on this install to test with.
  • Loading branch information...
1 parent f7933f2 commit e9bc2315180226fe8a6124d14693bdc5ae1c01ba @msantos committed Aug 30, 2012
Showing with 271 additions and 14 deletions.
  1. +57 −0 bin/vert
  2. +1 −0 c_src/vert.c
  3. +1 −0 c_src/vert.h
  4. +14 −0 c_src/vert_func.h
  5. +142 −0 c_src/vert_secret.c
  6. +56 −14 src/vert.erl
View
@@ -523,6 +523,52 @@ call(["vol-wipe", Name | Arg]) ->
{ok, Vol} = storagevol(Connect, Name),
rp(vert:virStorageVolWipe(Vol));
+call(["secret-define", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ Res = case file:read_file(Name) of
+ {ok, XML} ->
+ vert:virSecretDefineXML(Connect, XML);
+ Error ->
+ Error
+ end,
+ rp(Res);
+
+call(["secret-dumpxml", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Secret} = secret(Connect, Name),
+ rp(vert:virSecretGetXMLDesc(Secret));
+
+call(["secret-get-value", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Secret} = secret(Connect, Name),
+ {ok, Bin} = vert:virSecretGetValue(Secret),
+ {ok, base64:encode_to_string(Bin)};
+
+call(["secret-list" | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Secrets} = vert:virConnectListSecrets(Connect),
+ rp({ok, [ begin
+ {ok, Secret} = secret(Connect, UUID),
+ {ok, Type} = vert:virSecretGetUsageType(Secret),
+ {UUID, [{usagetype, Type}]}
+ end || UUID <- Secrets ]});
+
+call(["secret-set-value", Name, Val | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Secret} = secret(Connect, Name),
+ rp(vert:virSecretSetValue(Secret, base64:decode(Val)));
+
+call(["secret-undefine", Name | Arg]) ->
+ Opt = getopt(Arg),
+ {ok, Connect} = connect(Opt),
+ {ok, Secret} = secret(Connect, Name),
+ rp(vert:virSecretUndefine(Secret));
+
call(_) ->
Help = [
"autostart",
@@ -576,6 +622,13 @@ call(_) ->
"pool-undefine",
"pool-uuid",
+ "secret-define",
+ "secret-dumpxml",
+ "secret-get-value",
+ "secret-list",
+ "secret-set-value",
+ "secret-undefine",
+
"vol-create",
"vol-create-from",
"vol-delete",
@@ -637,6 +690,10 @@ nwfilter(Connect, Name) ->
fun() -> vert:virNWFilterLookupByName(Connect, Name) end ],
lookup(Fun).
+secret(Connect, Name) ->
+ Fun = [ fun() -> vert:virSecretLookupByUUIDString(Connect, Name) end ],
+ lookup(Fun).
+
storagepool(Connect, Name) ->
Fun = [ fun() -> vert:virStoragePoolLookupByUUIDString(Connect, Name) end,
fun() -> vert:virStoragePoolLookupByName(Connect, Name) end ],
View
@@ -86,6 +86,7 @@ load(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM load_info)
atom_interface = enif_make_atom(env, "interface");
atom_network = enif_make_atom(env, "network");
atom_nwfilter = enif_make_atom(env, "nwfilter");
+ atom_secret = enif_make_atom(env, "secret");
atom_storagepool = enif_make_atom(env, "storagepool");
atom_storagevol = enif_make_atom(env, "storagevol");
atom_stream = enif_make_atom(env, "stream");
View
@@ -51,6 +51,7 @@ ERL_NIF_TERM atom_domain;
ERL_NIF_TERM atom_interface;
ERL_NIF_TERM atom_network;
ERL_NIF_TERM atom_nwfilter;
+ERL_NIF_TERM atom_secret;
ERL_NIF_TERM atom_storagepool;
ERL_NIF_TERM atom_storagevol;
ERL_NIF_TERM atom_stream;
View
@@ -144,6 +144,20 @@ ErlNifFunc vert_funcs[] = {
{"virNWFilterLookupByUUIDString", 2, vert_virNWFilterLookupByUUIDString},
{"virNWFilterUndefine", 1, vert_virNWFilterUndefine},
+ /* secret */
+ {"virSecretDefineXML", 2, vert_virSecretDefineXML},
+ {"virSecretGetUsageID", 1, vert_virSecretGetUsageID},
+ {"virSecretGetUsageType", 1, vert_virSecretGetUsageType},
+ {"virSecretGetUUID", 1, vert_virSecretGetUUID},
+ {"virSecretGetUUIDString", 1, vert_virSecretGetUUIDString},
+ {"virSecretGetValue", 2, vert_virSecretGetValue},
+ {"virSecretGetXMLDesc", 2, vert_virSecretGetXMLDesc},
+ {"virSecretLookupByUsage", 3, vert_virSecretLookupByUsage},
+ {"virSecretLookupByUUID", 2, vert_virSecretLookupByUUID},
+ {"virSecretLookupByUUIDString", 2, vert_virSecretLookupByUUIDString},
+ {"virSecretSetValue", 2, vert_virSecretSetValue},
+ {"virSecretUndefine", 1, vert_virSecretUndefine},
+
/* storagepool */
{"virStoragePoolBuild", 2, vert_virStoragePoolBuild},
{"virStoragePoolCreate", 2, vert_virStoragePoolCreate},
View
@@ -0,0 +1,142 @@
+/* Copyright (c) 2012, Michael Santos <michael.santos@gmail.com>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * Neither the name of the author nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "vert.h"
+#include "vert_util.h"
+
+
+VERT_FUN_GETUUID(virSecretGetUUID, VERT_RES_SECRET)
+VERT_FUN_GETUUIDSTRING(virSecretGetUUIDString, VERT_RES_SECRET)
+VERT_FUN_GETNAME(virSecretGetUsageID, VERT_RES_SECRET)
+VERT_FUN_INT_RES(virSecretGetUsageType, VERT_RES_SECRET)
+VERT_FUN_GETXMLDESC(virSecretGetXMLDesc, VERT_RES_SECRET)
+VERT_FUN_LOOKUPBYUUID(virSecretLookupByUUID, VERT_RES_SECRET, atom_secret)
+VERT_FUN_LOOKUPBYNAME(virSecretLookupByUUIDString, VERT_RES_SECRET, atom_secret)
+VERT_FUN_INT_RES(virSecretUndefine, VERT_RES_SECRET)
+
+
+ ERL_NIF_TERM
+vert_virSecretDefineXML(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{
+ VERT_RESOURCE *vp = NULL;
+ ErlNifBinary xml = {0};
+ u_int32_t flags = 0;
+
+ VERT_RESOURCE *sp = NULL;
+
+
+ VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
+ VERT_GET_IOLIST(1, xml);
+ VERT_GET_UINT(2, flags);
+
+ VERT_BIN_APPEND_NULL(xml);
+
+ VERT_RES_ALLOC(sp, VERT_RES_SECRET, vp->conn);
+
+ sp->res = virSecretDefineXML(vp->res, (char *)xml.data, flags);
+
+ if (sp->res == NULL) {
+ enif_release_resource(sp);
+ return verterr(env);
+ }
+
+ return vert_make_resource(env, sp, atom_secret);
+}
+
+ ERL_NIF_TERM
+vert_virSecretGetValue(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{
+ VERT_RESOURCE *sp = NULL;
+ u_int32_t flags = 0;
+ size_t value_size = 0;
+
+ unsigned char *secret = NULL;
+ ERL_NIF_TERM buf = {0};
+
+
+ VERT_GET_RESOURCE(0, sp, VERT_RES_SECRET);
+ VERT_GET_UINT(1, flags);
+
+ secret = virSecretGetValue(sp->res, &value_size, flags);
+ VERTERR(secret == NULL);
+
+ BINCOPY(buf, secret, value_size);
+
+ free(secret);
+
+ return enif_make_tuple2(env,
+ atom_ok,
+ buf);
+}
+
+ ERL_NIF_TERM
+vert_virSecretLookupByUsage(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{
+ VERT_RESOURCE *vp = NULL;
+ int usageType = 0;
+ ErlNifBinary usageID = {0};
+
+ VERT_RESOURCE *sp = NULL;
+
+ VERT_GET_RESOURCE(0, vp, VERT_RES_CONNECT);
+ VERT_GET_INT(1, usageType);
+ VERT_GET_IOLIST(2, usageID);
+
+ VERT_BIN_APPEND_NULL(usageID);
+
+ VERT_RES_ALLOC(sp, VERT_RES_SECRET, vp->res);
+
+ sp->res = virSecretLookupByUsage(vp->res, usageType, (char *)usageID.data);
+
+ if (sp->res == NULL) {
+ enif_release_resource(vp);
+ return verterr(env);
+ }
+
+ return vert_make_resource(env, sp, atom_secret);
+}
+
+ ERL_NIF_TERM
+vert_virSecretSetValue(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[])
+{
+ VERT_RESOURCE *sp = NULL;
+ ErlNifBinary value = {0};
+ u_int32_t flags = 0;
+
+
+ VERT_GET_RESOURCE(0, sp, VERT_RES_SECRET);
+ VERT_GET_IOLIST(1, value);
+ VERT_GET_UINT(2, flags);
+
+ VERTERR(virSecretSetValue(sp->res, value.data, value.size, flags) < 0);
+
+ return atom_ok;
+}
View
@@ -148,6 +148,19 @@
virConnectOpen/1,
virConnectOpenReadOnly/1,
+ virSecretDefineXML/2,
+ virSecretGetUsageID/1,
+ virSecretGetUsageType/1,
+ virSecretGetUUID/1,
+ virSecretGetUUIDString/1,
+ virSecretGetValue/1, virSecretGetValue/2,
+ virSecretGetXMLDesc/1, virSecretGetXMLDesc/2,
+ virSecretLookupByUsage/3,
+ virSecretLookupByUUID/2,
+ virSecretLookupByUUIDString/2,
+ virSecretSetValue/2,
+ virSecretUndefine/1,
+
virStoragePoolBuild/1, virStoragePoolBuild/2,
virStoragePoolCreate/1, virStoragePoolCreate/2,
virStoragePoolCreateXML/3,
@@ -435,20 +448,49 @@ virStoragePoolBuild(#resource{type = storagepool, res = Res}, Flags) ->
%%% Secret
%%-------------------------------------------------------------------------
-%virSecretUndefine(Secret) ->
-%virSecretSetValue(Secret, Value, Value_size, Flags) ->
-%virSecretLookupByUsage(Conn, UsageType, UsageID) ->
-%virSecretLookupByUUIDString(Conn, Uuidstr) ->
-%virSecretLookupByUUID(Conn, Uuid) ->
-%virSecretGetXMLDesc(Secret, Flags) ->
-%virSecretGetValue(Secret, Value_size, Flags) ->
-%virSecretGetUsageType(Secret) ->
-%virSecretGetUsageID(Secret) ->
-%virSecretGetUUIDString(Secret, Buf) ->
-%virSecretGetUUID(Secret, Uuid) ->
-%virSecretGetConnect(Secret) ->
-%virSecretFree(Secret) ->
-%virSecretDefineXML(Conn, Xml, Flags) ->
+virSecretUndefine(#resource{type = secret, res = Res}) ->
+ call(virSecretUndefine, [Res]).
+
+virSecretSetValue(Res, Value) ->
+ virSecretSetValue(Res, Value, 0).
+virSecretSetValue(#resource{type = secret, res = Res}, Value, Flags) ->
+ call(virSecretSetValue, [Res, Value, Flags]).
+
+virSecretLookupByUsage(#resource{type = connect, res = Res}, UsageType, UsageID) ->
+ call(virSecretLookupByUsage, [Res, UsageType, UsageID]).
+
+virSecretLookupByUUIDString(#resource{type = connect, res = Res}, UUID) ->
+ call(virSecretLookupByUUIDString, [Res, UUID]).
+
+virSecretLookupByUUID(#resource{type = connect, res = Res}, UUID) ->
+ call(virSecretLookupByUUID, [Res, UUID]).
+
+virSecretGetXMLDesc(Secret) ->
+ virSecretGetXMLDesc(Secret, 0).
+virSecretGetXMLDesc(#resource{type = secret, res = Res}, Flags) ->
+ call(virSecretGetXMLDesc, [Res, Flags]).
+
+virSecretGetValue(Secret) ->
+ virSecretGetValue(Secret, 0).
+virSecretGetValue(#resource{type = secret, res = Res}, Flags) ->
+ call(virSecretGetValue, [Res, Flags]).
+
+virSecretGetUsageType(#resource{type = secret, res = Res}) ->
+ call(virSecretGetUsageType, [Res]).
+
+virSecretGetUsageID(#resource{type = secret, res = Res}) ->
+ call(virSecretGetUsageID, [Res]).
+
+virSecretGetUUIDString(#resource{type = secret, res = Res}) ->
+ call(virSecretGetUUIDString, [Res]).
+
+virSecretGetUUID(#resource{type = secret, res = Res}) ->
+ call(virSecretGetUUID, [Res]).
+
+virSecretDefineXML(Res, XML) ->
+ virSecretDefineXML(Res, XML, 0).
+virSecretDefineXML(#resource{type = connect, res = Res}, XML, Flags) ->
+ call(virSecretDefineXML, [Res, XML, Flags]).
%%-------------------------------------------------------------------------

0 comments on commit e9bc231

Please sign in to comment.