-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add application lib dir and ssh keypair generation
- Loading branch information
Showing
5 changed files
with
107 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package ssh | ||
|
||
import ( | ||
"golang.org/x/crypto/ed25519" | ||
"os" | ||
"path" | ||
) | ||
|
||
// CreateSSHKeyPairIfNotExists creates a new ed25519 key pair inside dir only if no key exists | ||
func CreateSSHKeyPairIfNotExists(dir string) error { | ||
if _, err := os.Stat(path.Join(dir, "id_ed25519")); os.IsNotExist(err) { | ||
return CreateSSHKeyPair(dir) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// CreateSSHKeyPair creates a new ed25519 key pair inside dir | ||
func CreateSSHKeyPair(dir string) error { | ||
privateKey, publicKey, err := ed25519.GenerateKey(nil) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := os.WriteFile(path.Join(dir, "id_ed25519"), privateKey, 0600); err != nil { | ||
return err | ||
} | ||
|
||
if err := os.WriteFile(path.Join(dir, "id_ed25519.pub"), publicKey, 0600); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ssh_test | ||
|
||
import ( | ||
"bwizard/internal/pkg/ssh" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"path" | ||
"testing" | ||
) | ||
|
||
func TestCreateSSHKeyPair(t *testing.T) { | ||
dir := t.TempDir() | ||
|
||
err := ssh.CreateSSHKeyPair(dir) | ||
require.NoError(t, err) | ||
|
||
privateKey, err := os.ReadFile(path.Join(dir, "id_ed25519")) | ||
require.NoError(t, err) | ||
|
||
publicKey, err := os.ReadFile(path.Join(dir, "id_ed25519.pub")) | ||
require.NoError(t, err) | ||
|
||
assert.True(t, len(privateKey) > 0) | ||
assert.True(t, len(publicKey) > 0) | ||
} | ||
|
||
func TestCreateSSHKeyPairIfNotExists(t *testing.T) { | ||
dir := t.TempDir() | ||
|
||
err := ssh.CreateSSHKeyPair(dir) | ||
require.NoError(t, err) | ||
|
||
oldPrivateKey, err := os.ReadFile(path.Join(dir, "id_ed25519")) | ||
require.NoError(t, err) | ||
|
||
oldPublicKey, err := os.ReadFile(path.Join(dir, "id_ed25519.pub")) | ||
require.NoError(t, err) | ||
|
||
err = ssh.CreateSSHKeyPairIfNotExists(dir) | ||
require.NoError(t, err) | ||
|
||
newPrivateKey, err := os.ReadFile(path.Join(dir, "id_ed25519")) | ||
require.NoError(t, err) | ||
|
||
newPublicKey, err := os.ReadFile(path.Join(dir, "id_ed25519.pub")) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, oldPrivateKey, newPrivateKey) | ||
assert.Equal(t, oldPublicKey, newPublicKey) | ||
} |