Skip to content

Commit 0f9787d

Browse files
henrybarretogustavosbarreto
authored andcommitted
fix(ssh): rename magickey's function due typo
1 parent 3c29b97 commit 0f9787d

File tree

5 files changed

+96
-8
lines changed

5 files changed

+96
-8
lines changed

ssh/pkg/magickey/magickey.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// Package magickey provides RSA key generation for ShellHub SSH service.
12
package magickey
23

34
import (
@@ -8,9 +9,9 @@ import (
89
log "github.com/sirupsen/logrus"
910
)
1011

11-
// GetRerefence gets a reference for a [*rsa.PrivateKey] used on ShellHub's SSH service. This reference is generate once
12-
// and them used on every subsequence call.
13-
var GetRerefence = sync.OnceValue(func() *rsa.PrivateKey {
12+
// GetReference returns a singleton RSA private key for ShellHub SSH service.
13+
// The key is generated once and reused across all subsequent calls.
14+
var GetReference = sync.OnceValue(func() *rsa.PrivateKey {
1415
key, err := rsa.GenerateKey(rand.Reader, 2048)
1516
if err != nil {
1617
log.WithError(err).Fatal()

ssh/pkg/magickey/magickey_test.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package magickey
2+
3+
import (
4+
"crypto/rsa"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestGetReference(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
test func(t *testing.T)
14+
}{
15+
{
16+
name: "success when called twice returns singleton key",
17+
test: func(t *testing.T) {
18+
key1 := GetReference()
19+
key2 := GetReference()
20+
assert.Same(t, key1, key2)
21+
},
22+
},
23+
{
24+
name: "success when key is valid RSA 2048",
25+
test: func(t *testing.T) {
26+
key := GetReference()
27+
assert.NotNil(t, key)
28+
assert.Equal(t, 2048, key.N.BitLen())
29+
assert.NotNil(t, key.PublicKey)
30+
assert.Equal(t, 2048, key.PublicKey.N.BitLen())
31+
},
32+
},
33+
{
34+
name: "success when key is usable for operations",
35+
test: func(t *testing.T) {
36+
key := GetReference()
37+
assert.NotNil(t, key.Primes)
38+
assert.Len(t, key.Primes, 2)
39+
assert.NotNil(t, key.Precomputed)
40+
},
41+
},
42+
{
43+
name: "success when multiple calls return same key",
44+
test: func(t *testing.T) {
45+
keys := make([]*rsa.PrivateKey, 10)
46+
for i := 0; i < 10; i++ {
47+
keys[i] = GetReference()
48+
}
49+
firstKey := keys[0]
50+
for _, key := range keys {
51+
assert.Same(t, firstKey, key)
52+
}
53+
},
54+
},
55+
}
56+
57+
for _, tc := range tests {
58+
t.Run(tc.name, tc.test)
59+
}
60+
}
61+
62+
func TestGetReference_Concurrency(t *testing.T) {
63+
t.Run("success when called concurrently returns singleton", func(t *testing.T) {
64+
const numGoroutines = 100
65+
keys := make(chan *rsa.PrivateKey, numGoroutines)
66+
for i := 0; i < numGoroutines; i++ {
67+
go func() {
68+
keys <- GetReference()
69+
}()
70+
}
71+
collectedKeys := make([]*rsa.PrivateKey, numGoroutines)
72+
for i := 0; i < numGoroutines; i++ {
73+
collectedKeys[i] = <-keys
74+
}
75+
firstKey := collectedKeys[0]
76+
for _, key := range collectedKeys {
77+
assert.Same(t, firstKey, key)
78+
}
79+
})
80+
}
81+
82+
func BenchmarkGetReference(b *testing.B) {
83+
b.ResetTimer()
84+
for i := 0; i < b.N; i++ {
85+
_ = GetReference()
86+
}
87+
}

ssh/session/auther.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (p *publicKeyAuth) Evaluate(session *Session) error {
9696

9797
fingerprint := gossh.FingerprintLegacyMD5(p.pk)
9898

99-
magic, err := gossh.NewPublicKey(&magickey.GetRerefence().PublicKey)
99+
magic, err := gossh.NewPublicKey(&magickey.GetReference().PublicKey)
100100
if err != nil {
101101
return err
102102
}

ssh/web/pkg/token/token.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func NewToken(_ *rsa.PrivateKey) (*Token, error) {
2525

2626
token, err := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{
2727
"id": identifier,
28-
}).SignedString(magickey.GetRerefence())
28+
}).SignedString(magickey.GetReference())
2929
if err != nil {
3030
return nil, err
3131
}
@@ -41,7 +41,7 @@ func Parse(token string) (*Token, error) {
4141
return nil, fmt.Errorf("unexpected method: %s", jwtToken.Header["alg"])
4242
}
4343

44-
return magickey.GetRerefence().Public().(*rsa.PublicKey), nil
44+
return magickey.GetReference().Public().(*rsa.PublicKey), nil
4545
}); err != nil {
4646
return nil, err
4747
}

ssh/web/web.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func NewSSHServerBridge(router *echo.Echo, cache cache.Cache) {
4747
return
4848
}
4949

50-
key := magickey.GetRerefence()
50+
key := magickey.GetReference()
5151

5252
token, err := token.NewToken(key)
5353
if err != nil {
@@ -111,7 +111,7 @@ func NewSSHServerBridge(router *echo.Echo, cache cache.Cache) {
111111

112112
go conn.KeepAlive()
113113

114-
creds.decryptPassword(magickey.GetRerefence()) //nolint:errcheck
114+
creds.decryptPassword(magickey.GetReference()) //nolint:errcheck
115115

116116
if err := newSession(
117117
wsconn.Request().Context(),

0 commit comments

Comments
 (0)