apache / apache/mina-sshd

Client Session contextual information

Open
#880 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1.1k
Forks
400
Avg merge
5d 1h
Merged PRs (30d)
1

Description

### Description

Hi Thomas,

I have a question regarding the AttributeRepository context that we can pass while connecting the ssh session.
It is a bit confusing from the Java doc to which session it is attached to.

I opened a previous ticket where https://github.com/apache/mina-sshd/issues/439 where I needed to override at runtime the different algorithms that we could potentially used to customize them.

Thanks to you I managed to do it thanks to listeners SessionListener#sessionCreated(Session session), but after some code changes I saw this AttributeRepository and tried to something more generic to pass my attribute directly from there instead of tryin to match the host and port of the ClientSession.

Here I have one listener / sshdConfiguration that I apply if host and port matches...

````
private MyConfig sshdConfiguration;

@Override
public void sessionCreated(Session session) {
InetSocketAddress remoteAddress = (InetSocketAddress)session.getIoSession().getRemoteAddress();
LOG.info("User name {}", session.getUsername());

if (remoteAddress.getHostName().equals(sshdConfiguration.getHost())
&& remoteAddress.getPort() == sshdConfiguration.getPort()) {
// The do something

}
}
````

So instead I tried only one listener where I get from the session itself the context...

````
@Override
public void sessionCreated(Session session) {
LOG.info("User name {}", session.getUsername());
// Not working
SshdConfiguration attribute = session.getAttribute(SshdConfiguration.SSHD_CONFIGURATION_ATTRIBUTE_KEY);

// Working
attribute =
((AttributeRepository)session.getIoSession().getAttribute(AttributeRepository.class)).getAttribute(
SshdConfiguration.SSHD_CONFIGURATION_ATTRIBUTE_KEY);

}

````

I had hard time to find where this AttributeRepository context was added (Nio2Connector.ConnectionCompletionHandler#onCompleted), to be able to retrieve it:

````
Nio2Session session = createSession(propertyResolver, handler, socket);
if (context != null) {
session.setAttribute(AttributeRepository.class, context);
}
````

My question is, am I doing good doing this way.

Also Why not attached attributes directly to the org.apache.sshd.common.session.Session object created directly ?

Many Thanks.

### Motivation

it would really simplify to pass contextual information when creating a new session. In our application we cannot set default algorithms for all our clients and need to be really flexible regarding the algorithms used.

### Alternatives considered

_No response_

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with Nio2Connector.ConnectionCompletionHandler#onCompleted and trace how the AttributeRepository context reaches the session and I/O session. Read the Session and AttributeRepository APIs and their Javadocs to clarify the intended scope of the attributes. Done requires a maintainer-approved decision on the API or documentation change, but the issue does not specify a concrete implementation target.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
networking
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.