-
Notifications
You must be signed in to change notification settings - Fork 22
/
SshShellAutoConfiguration.java
198 lines (177 loc) · 9.14 KB
/
SshShellAutoConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (c) 2020 François Onimus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.fonimus.ssh.shell;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fonimus.ssh.shell.auth.SshShellAuthenticationProvider;
import com.github.fonimus.ssh.shell.auth.SshShellPasswordAuthenticationProvider;
import com.github.fonimus.ssh.shell.auth.SshShellSecurityAuthenticationProvider;
import com.github.fonimus.ssh.shell.listeners.SshShellListener;
import com.github.fonimus.ssh.shell.listeners.SshShellListenerService;
import com.github.fonimus.ssh.shell.postprocess.provided.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.sshd.server.SshServer;
import org.jline.terminal.Terminal;
import org.jline.utils.AttributedString;
import org.jline.utils.AttributedStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.shell.boot.LineReaderAutoConfiguration;
import org.springframework.shell.boot.SpringShellAutoConfiguration;
import org.springframework.shell.boot.SpringShellProperties;
import org.springframework.shell.context.InteractionMode;
import org.springframework.shell.context.ShellContext;
import org.springframework.shell.jline.PromptProvider;
import org.springframework.shell.standard.ValueProvider;
import javax.annotation.PostConstruct;
import java.util.List;
import static com.github.fonimus.ssh.shell.SshShellProperties.SSH_SHELL_ENABLE;
import static com.github.fonimus.ssh.shell.SshShellProperties.SSH_SHELL_PREFIX;
/**
* <p>Ssh shell auto configuration</p>
* <p>Can be disabled by property <b>ssh.shell.enable=false</b></p>
*/
@Slf4j
@Configuration
@ConditionalOnClass(SshServer.class)
@ConditionalOnProperty(name = SSH_SHELL_ENABLE, havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties({SshShellProperties.class})
@AutoConfigureAfter(value = {
SpringShellAutoConfiguration.class, LineReaderAutoConfiguration.class
}, name = {
"org.springframework.boot.actuate.autoconfigure.audit.AuditEventsEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.beans.BeansEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.condition.ConditionsReportEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.context.properties.ConfigurationPropertiesReportEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.context.ShutdownEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.endpoint.jmx.JmxEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.env.EnvironmentEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.flyway.FlywayEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.health.HealthEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.jolokia.JolokiaEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.liquibase.LiquibaseEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.logging.LogFileWebEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.logging.LoggersEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.management.HeapDumpWebEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.management.ThreadDumpEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.metrics.MetricsEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.scheduling.ScheduledTasksEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.session.SessionsEndpointAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.web.exchanges.HttpExchangesAutoConfiguration",
"org.springframework.boot.actuate.autoconfigure.web.mappings.MappingsEndpointAutoConfiguration"
})
@ComponentScan(basePackages = {"com.github.fonimus.ssh.shell"})
@AllArgsConstructor
public class SshShellAutoConfiguration {
private final ApplicationContext context;
private final SshShellProperties properties;
private final SpringShellProperties springShellProperties;
private final ShellContext shellContext;
/**
* Initialize ssh shell auto config
*/
@PostConstruct
public void init() {
// override some spring shell properties
springShellProperties.getHistory().setName(properties.getHistoryFile().getAbsolutePath());
// set interactive mode so that ThrowableResultHandler.showShortError() returns true
shellContext.setInteractionMode(InteractionMode.INTERACTIVE);
}
@Bean
@ConditionalOnProperty(value = "spring.main.lazy-initialization", havingValue = "true")
public ApplicationListener<ContextRefreshedEvent> lazyInitApplicationListener() {
return event -> {
LOGGER.info("Lazy initialization enabled, calling configuration beans explicitly to start ssh server and initialize shell correctly");
context.getBean(SshShellConfiguration.SshServerLifecycle.class);
context.getBeansOfType(Terminal.class);
context.getBeansOfType(ValueProvider.class);
};
}
// post processors
@Bean
@ConditionalOnClass(name = "com.fasterxml.jackson.databind.ObjectMapper")
public JsonPointerPostProcessor jsonPointerPostProcessor(ObjectMapper mapper) {
return new JsonPointerPostProcessor(mapper);
}
@Bean
@ConditionalOnClass(name = "com.fasterxml.jackson.databind.ObjectMapper")
public PrettyJsonPostProcessor prettyJsonPostProcessor(ObjectMapper mapper) {
return new PrettyJsonPostProcessor(mapper);
}
@Bean
public SavePostProcessor savePostProcessor() {
return new SavePostProcessor();
}
@Bean
public GrepPostProcessor grepPostProcessor() {
return new GrepPostProcessor();
}
@Bean
public HighlightPostProcessor highlightPostProcessor() {
return new HighlightPostProcessor();
}
@Bean
public SshShellHelper sshShellHelper() {
return new SshShellHelper(properties.getConfirmationWords());
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnClass(name = "org.springframework.security.authentication.AuthenticationManager")
@ConditionalOnProperty(value = SSH_SHELL_PREFIX + ".authentication", havingValue = "security")
public SshShellAuthenticationProvider sshShellSecurityAuthenticationProvider() {
return new SshShellSecurityAuthenticationProvider(context, properties.getAuthProviderBeanName());
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(value = SSH_SHELL_PREFIX + ".authentication", havingValue = "simple", matchIfMissing = true)
public SshShellAuthenticationProvider sshShellSimpleAuthenticationProvider() {
return new SshShellPasswordAuthenticationProvider(properties.getUser(), properties.getPassword());
}
/**
* Primary prompt provider
*
* @return prompt provider
*/
@Bean
@ConditionalOnMissingBean
public PromptProvider sshPromptProvider() {
return () -> new AttributedString(properties.getPrompt().getText(),
AttributedStyle.DEFAULT.foreground(properties.getPrompt().getColor().toJlineAttributedStyle()));
}
/**
* Creates ssh listener service
*
* @param listeners found listeners in context
* @return listener service
*/
@Bean
public SshShellListenerService sshShellListenerService(@Autowired(required = false) List<SshShellListener> listeners) {
return new SshShellListenerService(listeners);
}
}