LCOV - code coverage report
Current view: top level - sshd - SshSession.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 44 49 89.8 %
Date: 2022-11-19 15:00:39 Functions: 18 18 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2010 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;
      16             : 
      17             : import com.google.gerrit.server.AccessPath;
      18             : import com.google.gerrit.server.CurrentUser;
      19             : import java.net.InetAddress;
      20             : import java.net.InetSocketAddress;
      21             : import java.net.SocketAddress;
      22             : import org.apache.sshd.common.AttributeRepository.AttributeKey;
      23             : 
      24             : /** Global data related to an active SSH connection. */
      25             : public class SshSession {
      26             :   /** ServerSession attribute key for this object instance. */
      27          17 :   public static final AttributeKey<SshSession> KEY = new AttributeKey<>();
      28             : 
      29             :   private final int sessionId;
      30             :   private final SocketAddress remoteAddress;
      31             :   private final String remoteAsString;
      32             : 
      33             :   private volatile CurrentUser identity;
      34             :   private volatile String username;
      35             :   private volatile String authError;
      36             :   private volatile String peerAgent;
      37             : 
      38          17 :   private volatile boolean gracefulStop = false;
      39             : 
      40          17 :   SshSession(int sessionId, SocketAddress peer) {
      41          17 :     this.sessionId = sessionId;
      42          17 :     this.remoteAddress = peer;
      43          17 :     this.remoteAsString = format(remoteAddress);
      44          17 :   }
      45             : 
      46           6 :   SshSession(SshSession parent, SocketAddress peer, CurrentUser user) {
      47           6 :     user.setAccessPath(AccessPath.SSH_COMMAND);
      48           6 :     this.sessionId = parent.sessionId;
      49           6 :     this.remoteAddress = peer;
      50           6 :     if (parent.remoteAddress == peer) {
      51           6 :       this.remoteAsString = parent.remoteAsString;
      52             :     } else {
      53           0 :       this.remoteAsString = format(peer) + "/" + parent.remoteAsString;
      54             :     }
      55           6 :     this.identity = user;
      56           6 :   }
      57             : 
      58             :   /** Unique session number, assigned during connect. */
      59             :   public int getSessionId() {
      60          17 :     return sessionId;
      61             :   }
      62             : 
      63             :   public boolean requiresGracefulStop() {
      64           1 :     return gracefulStop;
      65             :   }
      66             : 
      67             :   public void setGracefulStop(boolean gracefulStop) {
      68           9 :     this.gracefulStop = gracefulStop;
      69           9 :   }
      70             : 
      71             :   /** Identity of the authenticated user account on the socket. */
      72             :   public CurrentUser getUser() {
      73          17 :     return identity;
      74             :   }
      75             : 
      76             :   public SocketAddress getRemoteAddress() {
      77          17 :     return remoteAddress;
      78             :   }
      79             : 
      80             :   public String getRemoteAddressAsString() {
      81          17 :     return remoteAsString;
      82             :   }
      83             : 
      84             :   public String getPeerAgent() {
      85           9 :     return peerAgent;
      86             :   }
      87             : 
      88             :   public void setPeerAgent(String agent) {
      89           3 :     peerAgent = agent;
      90           3 :   }
      91             : 
      92             :   String getUsername() {
      93           1 :     return username;
      94             :   }
      95             : 
      96             :   String getAuthenticationError() {
      97           1 :     return authError;
      98             :   }
      99             : 
     100             :   void authenticationSuccess(String user, CurrentUser id) {
     101          17 :     username = user;
     102          17 :     identity = id;
     103          17 :     identity.setAccessPath(AccessPath.SSH_COMMAND);
     104          17 :     authError = null;
     105          17 :   }
     106             : 
     107             :   void authenticationError(String user, String error) {
     108           1 :     username = user;
     109           1 :     identity = null;
     110           1 :     authError = error;
     111           1 :   }
     112             : 
     113             :   void setAccessPath(AccessPath path) {
     114           9 :     identity.setAccessPath(path);
     115           9 :   }
     116             : 
     117             :   /** Returns {@code true} if the authentication did not succeed. */
     118             :   boolean isAuthenticationError() {
     119          17 :     return authError != null;
     120             :   }
     121             : 
     122             :   private static String format(SocketAddress remote) {
     123          17 :     if (remote instanceof InetSocketAddress) {
     124          17 :       final InetSocketAddress sa = (InetSocketAddress) remote;
     125             : 
     126          17 :       final InetAddress in = sa.getAddress();
     127          17 :       if (in != null) {
     128          17 :         return in.getHostAddress();
     129             :       }
     130             : 
     131           0 :       final String hostName = sa.getHostName();
     132           0 :       if (hostName != null) {
     133           0 :         return hostName;
     134             :       }
     135             :     }
     136           0 :     return remote.toString();
     137             :   }
     138             : }

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