LCOV - code coverage report
Current view: top level - pkg/storaged/crypto - actions.jsx Coverage Total Hit
Test: cockpit Lines: 87.5 % 64 56
Test Date: 2026-07-17 12:03:54

            Line data    Source code
       1              : /*
       2              :  * Copyright (C) 2023 Red Hat, Inc.
       3              :  * SPDX-License-Identifier: LGPL-2.1-or-later
       4              :  */
       5              : 
       6          113 : import cockpit from "cockpit";
       7              : import client from "../client";
       8              : 
       9              : import { get_existing_passphrase, unlock_with_type } from "./keyslots.jsx";
      10              : import { set_crypto_auto_option, get_active_usage, teardown_active_usage, block_name } from "../utils.js";
      11              : import { dialog_open, PassInput, init_teardown_usage, TeardownMessage, BlockingMessage } from "../dialog.jsx";
      12              : 
      13          113 : const _ = cockpit.gettext;
      14              : 
      15            3 : export function unlock(block) {
      16            3 :     const crypto = client.blocks_crypto[block.path];
      17            3 :     if (!crypto)
      18            3 :         return;
      19              : 
      20            3 :     function unlock_with_passphrase() {
      21            3 :         const crypto = client.blocks_crypto[block.path];
      22            3 :         if (!crypto)
      23            3 :             return;
      24              : 
      25            3 :         dialog_open({
      26            3 :             Title: _("Unlock"),
      27            3 :             Fields: [
      28            3 :                 PassInput("passphrase", _("Passphrase"), {})
      29            3 :             ],
      30            3 :             Action: {
      31            3 :                 Title: _("Unlock"),
      32            3 :                 action: async function (vals) {
      33            3 :                     await unlock_with_type(client, block, vals.passphrase, null);
      34            3 :                     await set_crypto_auto_option(block, true);
      35            3 :                 }
      36            3 :             }
      37            3 :         });
      38            3 :     }
      39              : 
      40            3 :     return get_existing_passphrase(block, true).then(type => {
      41            3 :         return (unlock_with_type(client, block, null, type)
      42            0 :                 .then(() => set_crypto_auto_option(block, true))
      43            3 :                 .catch(() => unlock_with_passphrase()));
      44            3 :     });
      45            3 : }
      46              : 
      47            2 : export function lock(block) {
      48            2 :     const crypto = client.blocks_crypto[block.path];
      49            2 :     if (!crypto)
      50            2 :         return;
      51              : 
      52            2 :     const name = block_name(block);
      53            2 :     const usage = get_active_usage(client, block.path, _("lock"));
      54              : 
      55            0 :     if (usage.Blocking) {
      56            0 :         dialog_open({
      57            0 :             Title: cockpit.format(_("$0 is in use"), name),
      58            0 :             Body: BlockingMessage(usage)
      59            0 :         });
      60            0 :         return;
      61            0 :     }
      62              : 
      63            2 :     dialog_open({
      64            2 :         Title: cockpit.format(_("Lock $0?"), name),
      65            2 :         Teardown: TeardownMessage(usage),
      66            2 :         Action: {
      67            2 :             Title: _("Lock"),
      68            2 :             action: async function () {
      69            2 :                 await teardown_active_usage(client, usage);
      70            2 :                 await crypto.Lock({});
      71            2 :                 await set_crypto_auto_option(block, false);
      72            2 :             }
      73            2 :         },
      74            2 :         Inits: [
      75            2 :             init_teardown_usage(client, usage)
      76            2 :         ]
      77            2 :     });
      78            2 : }
      79              : 
      80          112 : export function std_lock_action(backing_block, content_block) {
      81          112 :     if (backing_block == content_block)
      82          112 :         return null;
      83              : 
      84            2 :     return { title: _("Lock"), action: () => lock(backing_block) };
      85          112 : }
        

Generated by: LCOV version 2.0-1