Skip to content

Commit 7e7a8dd

Browse files
refactor: ssh manager
1 parent aab79b8 commit 7e7a8dd

5 files changed

Lines changed: 36 additions & 82 deletions

File tree

‎ssh-manager/generate_ecdsa.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,25 @@ import (
99
"fmt"
1010
)
1111

12+
// GenerateEcdsa creates a new ECDSA SSH key pair.
1213
func (m *SshManager) GenerateEcdsa(
1314
ctx context.Context,
15+
// The remote host address (e.g., github.com).
1416
remoteHost string,
17+
// The user name on the remote host.
1518
remoteUser string,
19+
// The hostname of the local machine used for key naming.
1620
// +optional
1721
// +default="dagger"
1822
localHostname string,
23+
// The bit size for the ECDSA curve (256, 384, or 521).
1924
// +optional
2025
// +default=521
2126
bits int,
27+
// Automatically generate a 32-character passphrase for the private key.
2228
// +optional
2329
autoPassphrase bool,
30+
// An existing SSH config file to append the new configuration to.
2431
// +optional
2532
existingConfig *dagger.File,
2633
) (*KeyResult, error) {
@@ -33,7 +40,7 @@ func (m *SshManager) GenerateEcdsa(
3340
case 521:
3441
curve = elliptic.P521()
3542
default:
36-
return nil, fmt.Errorf("invalid bits for ecdsa")
43+
return nil, fmt.Errorf("invalid bits for ecdsa: must be 256, 384, or 521")
3744
}
3845
priv, err := ecdsa.GenerateKey(curve, rand.Reader)
3946
if err != nil {

‎ssh-manager/generate_ed25519.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,21 @@ import (
77
"dagger/ssh-manager/internal/dagger"
88
)
99

10+
// GenerateEd25519 creates a new Ed25519 SSH key pair. This is the recommended modern algorithm.
1011
func (m *SshManager) GenerateEd25519(
1112
ctx context.Context,
13+
// The remote host address (e.g., github.com).
1214
remoteHost string,
15+
// The user name on the remote host.
1316
remoteUser string,
17+
// The hostname of the local machine used for key naming.
1418
// +optional
1519
// +default="dagger"
1620
localHostname string,
21+
// Automatically generate a 32-character passphrase for the private key.
1722
// +optional
1823
autoPassphrase bool,
24+
// An existing SSH config file to append the new configuration to.
1925
// +optional
2026
existingConfig *dagger.File,
2127
) (*KeyResult, error) {

‎ssh-manager/main.go‎

Lines changed: 14 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// SSH Manager module for generating secure SSH key pairs and configurations.
12
package main
23

34
import (
@@ -15,89 +16,22 @@ import (
1516
"golang.org/x/crypto/ssh"
1617
)
1718

18-
type SshManager struct{}
19+
// SshManager provides services to generate and manage SSH identities.
20+
type SshManager struct{} //revive:disable-line
1921

22+
// KeyResult represents the collection of generated SSH components.
2023
type KeyResult struct {
21-
PublicKey *dagger.File `json:"publicKey"`
22-
PrivateKey *dagger.Secret `json:"privateKey"`
23-
Config *dagger.File `json:"config"`
24-
Files *dagger.Directory `json:"files"`
24+
// The OpenSSH formatted public key file.
25+
PublicKey *dagger.File `json:"publicKey"`
26+
// The OpenSSH formatted private key, stored as a secret.
27+
PrivateKey *dagger.Secret `json:"privateKey"`
28+
// The SSH configuration file containing host and proxy settings.
29+
Config *dagger.File `json:"config"`
30+
// A directory containing the private key, public key, and config for easy export.
31+
Files *dagger.Directory `json:"files"`
2532
}
2633

27-
// func (m *SshManager) GenerateRsa(
28-
// ctx context.Context,
29-
// remoteHost string,
30-
// remoteUser string,
31-
// // +optional
32-
// // +default="dagger"
33-
// localHostname string,
34-
// // +optional
35-
// // +default=4096
36-
// bits int,
37-
// // +optional
38-
// autoPassphrase bool,
39-
// // +optional
40-
// existingConfig *dagger.File,
41-
// ) (*KeyResult, error) {
42-
// priv, err := rsa.GenerateKey(rand.Reader, bits)
43-
// if err != nil {
44-
// return nil, err
45-
// }
46-
// return m.finalize(ctx, priv, priv.Public(), "rsa", remoteHost, remoteUser, localHostname, autoPassphrase, existingConfig)
47-
// }
48-
49-
/* func (m *SshManager) GenerateEd25519(
50-
ctx context.Context,
51-
remoteHost string,
52-
remoteUser string,
53-
// +optional
54-
// +default="dagger"
55-
localHostname string,
56-
// +optional
57-
autoPassphrase bool,
58-
// +optional
59-
existingConfig *dagger.File,
60-
) (*KeyResult, error) {
61-
pub, priv, err := ed25519.GenerateKey(rand.Reader)
62-
if err != nil {
63-
return nil, err
64-
}
65-
return m.finalize(ctx, priv, pub, "ed25519", remoteHost, remoteUser, localHostname, autoPassphrase, existingConfig)
66-
} */
67-
68-
// func (m *SshManager) GenerateEcdsa(
69-
// ctx context.Context,
70-
// remoteHost string,
71-
// remoteUser string,
72-
// // +optional
73-
// // +default="dagger"
74-
// localHostname string,
75-
// // +optional
76-
// // +default=521
77-
// bits int,
78-
// // +optional
79-
// autoPassphrase bool,
80-
// // +optional
81-
// existingConfig *dagger.File,
82-
// ) (*KeyResult, error) {
83-
// var curve elliptic.Curve
84-
// switch bits {
85-
// case 256:
86-
// curve = elliptic.P256()
87-
// case 384:
88-
// curve = elliptic.P384()
89-
// case 521:
90-
// curve = elliptic.P521()
91-
// default:
92-
// return nil, fmt.Errorf("invalid bits for ecdsa")
93-
// }
94-
// priv, err := ecdsa.GenerateKey(curve, rand.Reader)
95-
// if err != nil {
96-
// return nil, err
97-
// }
98-
// return m.finalize(ctx, priv, priv.Public(), "ecdsa", remoteHost, remoteUser, localHostname, autoPassphrase, existingConfig)
99-
// }
100-
34+
// finalize is an internal helper to process raw keys into formatted files and secrets.
10135
func (m *SshManager) finalize(
10236
ctx context.Context,
10337
priv any,
@@ -169,6 +103,7 @@ func (m *SshManager) finalize(
169103
}, nil
170104
}
171105

106+
// randomString generates a cryptographically secure random string for passphrases.
172107
func (m *SshManager) randomString(n int) string {
173108
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"
174109
ret := make([]byte, n)

‎ssh-manager/rsa.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@ import (
77
"dagger/ssh-manager/internal/dagger"
88
)
99

10+
// GenerateRsa creates a new RSA SSH key pair.
1011
func (m *SshManager) GenerateRsa(
1112
ctx context.Context,
13+
// The remote host address (e.g., github.com).
1214
remoteHost string,
15+
// The user name on the remote host.
1316
remoteUser string,
17+
// The hostname of the local machine used for key naming.
1418
// +optional
1519
// +default="dagger"
1620
localHostname string,
21+
// The bit length for the RSA key.
1722
// +optional
1823
// +default=4096
1924
bits int,
25+
// Automatically generate a 32-character passphrase for the private key.
2026
// +optional
2127
autoPassphrase bool,
28+
// An existing SSH config file to append the new configuration to.
2229
// +optional
2330
existingConfig *dagger.File,
2431
) (*KeyResult, error) {

‎ssh-manager/test/main.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
// The first line in this comment block is a short description line and the
1212
// rest is a long description with more detail on the module's purpose or usage,
1313
// if appropriate. All modules should have a short description.
14-
1514
package main
1615

1716
import (
@@ -23,7 +22,7 @@ import (
2322
"github.com/sourcegraph/conc/pool"
2423
)
2524

26-
type SshManagertest struct{}
25+
type SshManagertest struct{} //revive:disable-line
2726

2827
// All runs all SSH key generation tests in parallel.
2928
func (m *SshManagertest) All(ctx context.Context) error {

0 commit comments

Comments
 (0)