-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: support generate primary and public key for sshd (#8087)
(cherry picked from commit 83cfbf0)
- Loading branch information
Showing
7 changed files
with
193 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,55 @@ | ||
/* | ||
Copyright (C) 2022-2024 ApeCloud Co., Ltd | ||
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 gotemplate | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/charmbracelet/keygen" | ||
) | ||
|
||
const memoString = "kubeblocks@localhost" | ||
|
||
type SSHKeyPair struct { | ||
PrivateKey string `json:"private_key"` | ||
PublicKey string `json:"public_key"` | ||
} | ||
|
||
func sshKeyGenerate(passphrase ...string) (*SSHKeyPair, error) { | ||
options := []keygen.Option{ | ||
keygen.WithKeyType(keygen.RSA), | ||
} | ||
|
||
if len(passphrase) > 0 { | ||
options = append(options, keygen.WithPassphrase(passphrase[0])) | ||
} | ||
generator, err := keygen.New("", options...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rawPrimaryKey := generator.RawProtectedPrivateKey() | ||
if rawPrimaryKey == nil { | ||
return nil, keygen.ErrMissingSSHKeys | ||
} | ||
ak := generator.AuthorizedKey() | ||
return &SSHKeyPair{ | ||
PrivateKey: string(rawPrimaryKey), | ||
// memo is optional | ||
PublicKey: fmt.Sprintf("%s %s", ak, memoString), | ||
}, 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,52 @@ | ||
/* | ||
Copyright (C) 2022-2024 ApeCloud Co., Ltd | ||
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 gotemplate | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_sshKeyGenerate(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
passphrase string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "test", | ||
passphrase: "test", | ||
}, | ||
{ | ||
name: "test", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var err error | ||
var pairKey *SSHKeyPair | ||
if pairKey, err = sshKeyGenerate(tt.passphrase); (err != nil) != tt.wantErr { | ||
t.Errorf("sshKeyGenerate() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
if pairKey.PrivateKey == "" { | ||
t.Errorf("sshKeyGenerate() not generate private key") | ||
} | ||
if pairKey.PublicKey == "" { | ||
t.Errorf("sshKeyGenerate() not generate public key") | ||
} | ||
}) | ||
} | ||
} |
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