Skip to content

Commit 2fa28e7

Browse files
Allow host key configuration through configuration.Global.HostKeys (#17)
1 parent 31c19d1 commit 2fa28e7

File tree

9 files changed

+49
-8
lines changed

9 files changed

+49
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ You can customize the values of the helm deployment by using the following Value
132132
| `configuration.Global.Chroot.Directory` | Global chroot directory for the `sftp` user group. Can be overriden per-user | `"%h"` |
133133
| `configuration.Global.Chroot.StartPath` | Start path for the `sftp` user group. Can be overriden per-user | `"sftp"` |
134134
| `configuration.Global.Directories` | Directories that get created for all `sftp` users. Can be appended per user | `["sftp"]` |
135+
| `configuration.Global.HostKeys.Ed25519` | Set the server's ED25519 private key | `""` |
136+
| `configuration.Global.HostKeys.Rsa` | Set the server's RSA private key | `""` |
135137
| `configuration.Users` | Array of users and their properties | Contains `demo` user by default |
136138
| `configuration.Users[].Username` | Set the user's username | N/A |
137139
| `configuration.Users[].Password` | Set the user's password. If empty or `null`, password authentication is disabled | N/A |

src/ES.SFTP.Host/Business/Configuration/GlobalConfiguration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public class GlobalConfiguration
77
public ChrootDefinition Chroot { get; set; }
88
public List<string> Directories { get; set; } = new List<string>();
99
public LoggingDefinition Logging { get; set; }
10+
public HostKeyDefinition HostKeys { get; set; }
1011
}
1112
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace ES.SFTP.Host.Business.Configuration
2+
{
3+
public class HostKeyDefinition
4+
{
5+
public string Ed25519 { get; set; }
6+
public string Rsa { get; set; }
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ES.SFTP.Host.Business.Configuration
2+
{
3+
public class HostKeyType
4+
{
5+
public string Type { get; set; }
6+
public string KeygenArgs { get; set; }
7+
public string File { get; set; }
8+
}
9+
}

src/ES.SFTP.Host/Orchestrator.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public class Orchestrator : IRequestHandler<PamEventRequest, bool>
2424
private const string SshHostKeysDirPath = "/etc/ssh/keys";
2525
private const string SshConfigPath = "/etc/ssh/sshd_config";
2626

27-
private readonly Dictionary<string, string> _hostKeyFiles = new Dictionary<string, string>
27+
private readonly List<HostKeyType> _hostKeyTypes = new List<HostKeyType>
2828
{
29-
{"ssh_host_ed25519_key", "-t ed25519 -f {0} -N \"\""},
30-
{"ssh_host_rsa_key", "-t rsa -b 4096 -f {0} -N \"\""}
29+
new HostKeyType{Type = "Ed25519", KeygenArgs = "-t ed25519 -f {0} -N \"\"", File = "ssh_host_ed25519_key"},
30+
new HostKeyType{Type = "Rsa", KeygenArgs = "-t rsa -b 4096 -f {0} -N \"\"", File = "ssh_host_rsa_key"},
3131
};
3232

3333
private readonly ILogger<Orchestrator> _logger;
@@ -132,6 +132,7 @@ private Task PrepareAndValidateConfiguration()
132132
config.Global.Directories ??= new List<string>();
133133
config.Global.Logging ??= new LoggingDefinition();
134134
config.Global.Chroot ??= new ChrootDefinition();
135+
config.Global.HostKeys ??= new HostKeyDefinition();
135136
if (string.IsNullOrWhiteSpace(config.Global.Chroot.Directory)) config.Global.Chroot.Directory = "%h";
136137
if (string.IsNullOrWhiteSpace(config.Global.Chroot.StartPath)) config.Global.Chroot.StartPath = null;
137138

@@ -176,14 +177,22 @@ private async Task ImportOrCreateHostKeyFiles()
176177
if (!Directory.Exists(SshHostKeysDirPath))
177178
Directory.CreateDirectory(SshHostKeysDirPath);
178179

179-
180-
foreach (var hostKeyFile in _hostKeyFiles)
180+
foreach (var hostKeyType in _hostKeyTypes)
181181
{
182-
var filePath = Path.Combine(SshHostKeysDirPath, hostKeyFile.Key);
182+
var filePath = Path.Combine(SshHostKeysDirPath, hostKeyType.File);
183183
if (File.Exists(filePath)) continue;
184+
var keyConfig = (string)_config.Global.HostKeys.GetType().GetProperty(hostKeyType.Type).GetValue(_config.Global.HostKeys, null);
185+
if (!string.IsNullOrWhiteSpace(keyConfig))
186+
{
187+
_logger.LogDebug("Writing host key file '{file}' from config", filePath);
188+
await File.WriteAllTextAsync(filePath, keyConfig);
189+
}
190+
else
191+
{
184192
_logger.LogDebug("Generating host key file '{file}'", filePath);
185-
var keygenArgs = string.Format(hostKeyFile.Value, filePath);
193+
var keygenArgs = string.Format(hostKeyType.KeygenArgs, filePath);
186194
await ProcessUtil.QuickRun("ssh-keygen", keygenArgs);
195+
}
187196
}
188197

189198
foreach (var file in Directory.GetFiles(SshHostKeysDirPath))

src/deploy/docker-compose/docker-compose.override.dev.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ services:
1010
volumes:
1111
- ../samples/sample.dev.sftp.json:/app/config/sftp.json:ro
1212
- ../samples/.ssh/id_demo2_rsa.pub:/home/demo2/.ssh/keys/id_rsa.pub:ro
13+
- ../samples/.ssh/id_demo2_ed25519.pub:/home/demo2/.ssh/keys/id_ed25519.pub:ro
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-----BEGIN OPENSSH PRIVATE KEY-----
2+
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
3+
QyNTUxOQAAACCItsK7CZxhI38h+dvuQOSbUZpIV84n7QAmt7XXONbxLQAAAIgMsBerDLAX
4+
qwAAAAtzc2gtZWQyNTUxOQAAACCItsK7CZxhI38h+dvuQOSbUZpIV84n7QAmt7XXONbxLQ
5+
AAAECGtcsqvGH3fXmxHiuFdK+qYJsJrTpHVP6CCEPnMGByDIi2wrsJnGEjfyH52+5A5JtR
6+
mkhXziftACa3tdc41vEtAAAAAAECAwQF
7+
-----END OPENSSH PRIVATE KEY-----
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIi2wrsJnGEjfyH52+5A5JtRmkhXziftACa3tdc41vEt

src/deploy/samples/sample.dev.sftp.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"Directory": "%h",
55
"StartPath": "sftp"
66
},
7-
"Directories": ["sftp"]
7+
"Directories": ["sftp"],
8+
"HostKeys": {
9+
"Ed25519": "-----BEGIN OPENSSH PRIVATE KEY-----\nb3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW\nQyNTUxOQAAACBvlz4T2Fh9PKKeVhSupzXsBYVt44VJcb1554gRLKS2oAAAAIiJdbTtiXW0\n7QAAAAtzc2gtZWQyNTUxOQAAACBvlz4T2Fh9PKKeVhSupzXsBYVt44VJcb1554gRLKS2oA\nAAAEDI/igTE3dx3UC0As1d4kL0BNDaA3MkO9lDyWXqfErITm+XPhPYWH08op5WFK6nNewF\nhW3jhUlxvXnniBEspLagAAAAAAECAwQF\n-----END OPENSSH PRIVATE KEY-----\n"
10+
}
811
},
912
"Users": [
1013
{

0 commit comments

Comments
 (0)