Browse Source

nbd: allow authorization with nbd-server-start QMP command

As with the previous patch to qemu-nbd, the nbd-server-start QMP command
also needs to be able to specify authorization when enabling TLS encryption.

First the client must create a QAuthZ object instance using the
'object-add' command:

   {
     'execute': 'object-add',
     'arguments': {
       'qom-type': 'authz-list',
       'id': 'authz0',
       'parameters': {
         'policy': 'deny',
         'rules': [
           {
             'match': '*CN=fred',
             'policy': 'allow'
           }
         ]
       }
     }
   }

They can then reference this in the new 'tls-authz' parameter when
executing the 'nbd-server-start' command:

   {
     'execute': 'nbd-server-start',
     'arguments': {
       'addr': {
           'type': 'inet',
           'host': '127.0.0.1',
           'port': '9000'
       },
       'tls-creds': 'tls0',
       'tls-authz': 'authz0'
     }
   }

Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Juan Quintela <quintela@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Message-Id: <2019022716.18543-3-berrange@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
tags/v4.0.0-rc0
Daniel P. Berrange 3 months ago
parent
commit
000194556b
4 changed files with 17 additions and 6 deletions
  1. 8
    3
      blockdev-nbd.c
  2. 1
    1
      hmp.c
  3. 1
    1
      include/block/nbd.h
  4. 7
    1
      qapi/block.json

+ 8
- 3
blockdev-nbd.c View File

@@ -23,6 +23,7 @@
23 23
 typedef struct NBDServerData {
24 24
     QIONetListener *listener;
25 25
     QCryptoTLSCreds *tlscreds;
26
+    char *tlsauthz;
26 27
 } NBDServerData;
27 28
 
28 29
 static NBDServerData *nbd_server;
@@ -36,7 +37,7 @@ static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
36 37
                        gpointer opaque)
37 38
 {
38 39
     qio_channel_set_name(QIO_CHANNEL(cioc), "nbd-server");
39
-    nbd_client_new(cioc, nbd_server->tlscreds, NULL,
40
+    nbd_client_new(cioc, nbd_server->tlscreds, nbd_server->tlsauthz,
40 41
                    nbd_blockdev_client_closed);
41 42
 }
42 43
 
@@ -52,6 +53,7 @@ static void nbd_server_free(NBDServerData *server)
52 53
     if (server->tlscreds) {
53 54
         object_unref(OBJECT(server->tlscreds));
54 55
     }
56
+    g_free(server->tlsauthz);
55 57
 
56 58
     g_free(server);
57 59
 }
@@ -87,7 +89,7 @@ static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
87 89
 
88 90
 
89 91
 void nbd_server_start(SocketAddress *addr, const char *tls_creds,
90
-                      Error **errp)
92
+                      const char *tls_authz, Error **errp)
91 93
 {
92 94
     if (nbd_server) {
93 95
         error_setg(errp, "NBD server already running");
@@ -117,6 +119,8 @@ void nbd_server_start(SocketAddress *addr, const char *tls_creds,
117 119
         }
118 120
     }
119 121
 
122
+    nbd_server->tlsauthz = g_strdup(tls_authz);
123
+
120 124
     qio_net_listener_set_client_func(nbd_server->listener,
121 125
                                      nbd_accept,
122 126
                                      NULL,
@@ -131,11 +135,12 @@ void nbd_server_start(SocketAddress *addr, const char *tls_creds,
131 135
 
132 136
 void qmp_nbd_server_start(SocketAddressLegacy *addr,
133 137
                           bool has_tls_creds, const char *tls_creds,
138
+                          bool has_tls_authz, const char *tls_authz,
134 139
                           Error **errp)
135 140
 {
136 141
     SocketAddress *addr_flat = socket_address_flatten(addr);
137 142
 
138
-    nbd_server_start(addr_flat, tls_creds, errp);
143
+    nbd_server_start(addr_flat, tls_creds, tls_authz, errp);
139 144
     qapi_free_SocketAddress(addr_flat);
140 145
 }
141 146
 

+ 1
- 1
hmp.c View File

@@ -2373,7 +2373,7 @@ void hmp_nbd_server_start(Monitor *mon, const QDict *qdict)
2373 2373
         goto exit;
2374 2374
     }
2375 2375
 
2376
-    nbd_server_start(addr, NULL, &local_err);
2376
+    nbd_server_start(addr, NULL, NULL, &local_err);
2377 2377
     qapi_free_SocketAddress(addr);
2378 2378
     if (local_err != NULL) {
2379 2379
         goto exit;

+ 1
- 1
include/block/nbd.h View File

@@ -332,7 +332,7 @@ void nbd_client_get(NBDClient *client);
332 332
 void nbd_client_put(NBDClient *client);
333 333
 
334 334
 void nbd_server_start(SocketAddress *addr, const char *tls_creds,
335
-                      Error **errp);
335
+                      const char *tls_authz, Error **errp);
336 336
 
337 337
 /* nbd_read
338 338
  * Reads @size bytes from @ioc. Returns 0 on success.

+ 7
- 1
qapi/block.json View File

@@ -225,6 +225,11 @@
225 225
 #
226 226
 # @addr: Address on which to listen.
227 227
 # @tls-creds: (optional) ID of the TLS credentials object. Since 2.6
228
+# @tls-authz: ID of the QAuthZ authorization object used to validate
229
+#             the client's x509 distinguished name. This object is
230
+#             is only resolved at time of use, so can be deleted and
231
+#             recreated on the fly while the NBD server is active.
232
+#             If missing, it will default to denying access (since 4.0).
228 233
 #
229 234
 # Returns: error if the server is already running.
230 235
 #
@@ -232,7 +237,8 @@
232 237
 ##
233 238
 { 'command': 'nbd-server-start',
234 239
   'data': { 'addr': 'SocketAddressLegacy',
235
-            '*tls-creds': 'str'} }
240
+            '*tls-creds': 'str',
241
+            '*tls-authz': 'str'} }
236 242
 
237 243
 ##
238 244
 # @nbd-server-add:

Loading…
Cancel
Save