LCOV - code coverage report
Current view: top level - sshd/commands - CloseConnection.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 13 21 61.9 %
Date: 2022-11-19 15:00:39 Functions: 4 4 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2015 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.sshd.commands;
      16             : 
      17             : import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE;
      18             : 
      19             : import com.google.common.flogger.FluentLogger;
      20             : import com.google.gerrit.common.data.GlobalCapability;
      21             : import com.google.gerrit.extensions.annotations.RequiresCapability;
      22             : import com.google.gerrit.sshd.AdminHighPriorityCommand;
      23             : import com.google.gerrit.sshd.CommandMetaData;
      24             : import com.google.gerrit.sshd.SshCommand;
      25             : import com.google.gerrit.sshd.SshDaemon;
      26             : import com.google.gerrit.sshd.SshUtil;
      27             : import com.google.inject.Inject;
      28             : import java.io.IOException;
      29             : import java.util.ArrayList;
      30             : import java.util.List;
      31             : import org.apache.sshd.common.future.CloseFuture;
      32             : import org.kohsuke.args4j.Argument;
      33             : import org.kohsuke.args4j.Option;
      34             : 
      35             : /** Close specified SSH connections */
      36             : @AdminHighPriorityCommand
      37             : @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
      38             : @CommandMetaData(
      39             :     name = "close-connection",
      40             :     description = "Close the specified SSH connection",
      41             :     runsAt = MASTER_OR_SLAVE)
      42           1 : final class CloseConnection extends SshCommand {
      43           1 :   private static final FluentLogger logger = FluentLogger.forEnclosingClass();
      44             : 
      45             :   @Inject private SshDaemon sshDaemon;
      46             : 
      47           1 :   @Argument(
      48             :       index = 0,
      49             :       multiValued = true,
      50             :       required = true,
      51             :       metaVar = "SESSION_ID",
      52             :       usage = "List of SSH session IDs to be closed")
      53             :   private List<String> sessionIds = new ArrayList<>();
      54             : 
      55             :   @Option(name = "--wait", usage = "wait for connection to close before exiting")
      56             :   private boolean wait;
      57             : 
      58             :   @Override
      59             :   protected void run() throws Failure {
      60           1 :     enableGracefulStop();
      61           1 :     SshUtil.forEachSshSession(
      62             :         sshDaemon,
      63             :         (k, sshSession, abstractSession, ioSession) -> {
      64           1 :           String sessionId = String.format("%08x", sshSession.getSessionId());
      65           1 :           if (sessionIds.remove(sessionId)) {
      66           1 :             stdout.println("closing connection " + sessionId + "...");
      67           1 :             CloseFuture future = ioSession.close(true);
      68           1 :             if (wait) {
      69             :               try {
      70           0 :                 future.await();
      71           0 :                 stdout.println("closed connection " + sessionId);
      72           0 :               } catch (IOException e) {
      73           0 :                 logger.atWarning().log(
      74           0 :                     "Wait for connection to close interrupted: %s", e.getMessage());
      75           0 :               }
      76             :             }
      77             :           }
      78           1 :         });
      79           1 :     for (String sessionId : sessionIds) {
      80           0 :       stderr.print("close connection " + sessionId + ": no such connection\n");
      81           0 :     }
      82           1 :   }
      83             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750