-
Notifications
You must be signed in to change notification settings - Fork 137
Feature/some refactoring #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
68a9b83
some small refactoring
9e89ac8
do not need to export everything
e1ebeb3
early exit
ed3623b
scheme.Codecs & scheme.ParameterCodec has to be exported
26e3d46
do not touch generated code
afe46ca
fix merge conflict
435a497
add tests
d8c491c
add test cases
1f7836a
update error message
a4da72e
remove unnecessary lines
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -16,53 +16,69 @@ import ( | |
|
|
||
| const ( | ||
| privateKeyDataIndex = "id_rsa" | ||
|
|
||
| secretName = "machine-controller-ssh-key" | ||
| secretName = "machine-controller-ssh-key" | ||
| rsaPrivateKey = "RSA PRIVATE KEY" | ||
| ) | ||
|
|
||
| // EnsureSSHKeypairSecret | ||
| func EnsureSSHKeypairSecret(client kubernetes.Interface) (*rsa.PrivateKey, error) { | ||
| if client == nil { | ||
| return nil, fmt.Errorf("got an nil k8s client") | ||
| } | ||
| secret, err := client.CoreV1().Secrets(metav1.NamespaceSystem).Get(secretName, metav1.GetOptions{}) | ||
| if err != nil { | ||
| if errors.IsNotFound(err) { | ||
| glog.V(4).Info("generating master ssh keypair") | ||
| pk, err := NewPrivateKey() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to generate ssh keypair: %v", err) | ||
| } | ||
| if err == nil { | ||
| return keyFromSecret(secret) | ||
| } | ||
|
|
||
| if !errors.IsNotFound(err) { | ||
| return nil, err | ||
| } | ||
|
|
||
| privateKeyPEM := &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(pk)} | ||
| privBuf := bytes.Buffer{} | ||
| err = pem.Encode(&privBuf, privateKeyPEM) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| glog.V(4).Info("generating master ssh keypair") | ||
| pk, err := NewPrivateKey() | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to generate ssh keypair: %v", err) | ||
| } | ||
|
|
||
| secret := v1.Secret{} | ||
| secret.Name = secretName | ||
| secret.Type = v1.SecretTypeOpaque | ||
| privateKeyPEM := &pem.Block{Type: rsaPrivateKey, Bytes: x509.MarshalPKCS1PrivateKey(pk)} | ||
| privBuf := bytes.Buffer{} | ||
| err = pem.Encode(&privBuf, privateKeyPEM) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| secret.Data = map[string][]byte{ | ||
| privateKeyDataIndex: privBuf.Bytes(), | ||
| } | ||
| secret = &v1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: secretName, | ||
| }, | ||
| Type: v1.SecretTypeOpaque, | ||
| Data: map[string][]byte{ | ||
| privateKeyDataIndex: privBuf.Bytes(), | ||
| }, | ||
| } | ||
|
|
||
| _, err = client.CoreV1().Secrets(metav1.NamespaceSystem).Create(&secret) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return pk, nil | ||
| } | ||
| _, err = client.CoreV1().Secrets(metav1.NamespaceSystem).Create(secret) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return pk, nil | ||
|
|
||
| return keyFromSecret(secret) | ||
| } | ||
|
|
||
| func keyFromSecret(secret *v1.Secret) (*rsa.PrivateKey, error) { | ||
| b, exists := secret.Data[privateKeyDataIndex] | ||
| if !exists { | ||
| return nil, fmt.Errorf("key data not found in secret '%s/%s' (secret.data['%s']). remove it and a new one will be created", secret.Namespace, secret.Name, privateKeyDataIndex) | ||
| } | ||
| if len(b) == 0 { | ||
| return nil, fmt.Errorf("key data not found in secret '%s/%s' (secret.data['%s']). remove it and a new one will be created", secret.Namespace, secret.Name, privateKeyDataIndex) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would rather state something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
| } | ||
| decoded, _ := pem.Decode(b) | ||
|
|
||
| if decoded == nil { | ||
| return nil, fmt.Errorf("invalid PEM in secret '%s/%s'. remove it and a new one will be created", secret.Namespace, secret.Name) | ||
| } | ||
|
|
||
| pk, err := x509.ParsePKCS1PrivateKey(decoded.Bytes) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse private key: %v", err) | ||
|
|
||
This file contains hidden or 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,125 @@ | ||
| package ssh | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "crypto/rand" | ||
| "crypto/rsa" | ||
| "crypto/x509" | ||
| "encoding/pem" | ||
| "testing" | ||
|
|
||
| "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/kubernetes/fake" | ||
|
|
||
| "k8s.io/client-go/kubernetes" | ||
| ) | ||
|
|
||
| func generateByteSlice(n int) []byte { | ||
| b := make([]byte, n) | ||
| rand.Read(b) | ||
| return b | ||
| } | ||
|
|
||
| func generateValidPEM() []byte { | ||
| b := &bytes.Buffer{} | ||
| pk, _ := NewPrivateKey() | ||
|
|
||
| _ = pem.Encode(b, &pem.Block{Type: rsaPrivateKey, Bytes: x509.MarshalPKCS1PrivateKey(pk)}) | ||
| return b.Bytes() | ||
| } | ||
|
|
||
| func generateSecretWithCustomNameAndIndex(name, index string, b []byte) runtime.Object { | ||
| return &v1.Secret{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: name, | ||
| Namespace: metav1.NamespaceSystem, | ||
| }, | ||
| Data: map[string][]byte{ | ||
| index: b, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func generateSecretWithCustomName(name string, b []byte) runtime.Object { | ||
| return generateSecretWithCustomNameAndIndex(name, privateKeyDataIndex, b) | ||
| } | ||
|
|
||
| func generateSecretWithKey(b []byte) runtime.Object { | ||
| return generateSecretWithCustomName(secretName, b) | ||
| } | ||
|
|
||
| func fakeClientFactory(objs ...runtime.Object) kubernetes.Interface { | ||
| return fake.NewSimpleClientset(objs...) | ||
| } | ||
|
|
||
| func TestEnsureSSHKeypairSecret(t *testing.T) { | ||
| type args struct { | ||
| client kubernetes.Interface | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| args args | ||
| want *rsa.PrivateKey | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "nil client", | ||
| args: args{ | ||
| client: nil, | ||
| }, | ||
| want: nil, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "fake basis client without a key", | ||
| args: args{ | ||
| client: fakeClientFactory(generateSecretWithKey(nil)), | ||
| }, | ||
| want: nil, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "fake basis client with a malformed key", | ||
| args: args{ | ||
| client: fakeClientFactory(generateSecretWithKey(generateByteSlice(2048))), | ||
| }, | ||
| want: nil, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "fake basis client with a valid key", | ||
| args: args{ | ||
| client: fakeClientFactory(generateSecretWithKey(generateValidPEM())), | ||
| }, | ||
| want: nil, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "fake basis client with a valid key, but the wrong secrete name", | ||
| args: args{ | ||
| client: fakeClientFactory(generateSecretWithCustomName("blah", generateValidPEM())), | ||
| }, | ||
| want: nil, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "fake basis client with a valid key, but the wrong index", | ||
| args: args{ | ||
| client: fakeClientFactory(generateSecretWithCustomNameAndIndex(secretName, "blah", generateValidPEM())), | ||
| }, | ||
| want: nil, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| _, err := EnsureSSHKeypairSecret(tt.args.client) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("EnsureSSHKeypairSecret() error = %+v, wantErr %+v", err, tt.wantErr) | ||
| return | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really wan to do a nil check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes ;) make the code more stable