forked from kubernetes-sigs/cluster-api-provider-azure
-
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.
Merge pull request kubernetes-sigs#2 from kubernetes-sigs/master
Sync latest repo
- Loading branch information
Showing
38 changed files
with
465 additions
and
400 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
!/api/** | ||
!/cloud/** | ||
!/controllers/** | ||
!/exp/** | ||
!/pkg/** | ||
!/main.go | ||
!/go.mod | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 v1alpha3 | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
|
||
"github.com/pkg/errors" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
// SetDefaultSSHPublicKey sets the default SSHPublicKey for an AzureMachine | ||
func (m *AzureMachine) SetDefaultSSHPublicKey() error { | ||
sshKeyData := m.Spec.SSHPublicKey | ||
if sshKeyData == "" { | ||
privateKey, perr := rsa.GenerateKey(rand.Reader, 2048) | ||
if perr != nil { | ||
return errors.Wrap(perr, "Failed to generate private key") | ||
} | ||
|
||
publicRsaKey, perr := ssh.NewPublicKey(&privateKey.PublicKey) | ||
if perr != nil { | ||
return errors.Wrap(perr, "Failed to generate public key") | ||
} | ||
m.Spec.SSHPublicKey = string(ssh.MarshalAuthorizedKey(publicRsaKey)) | ||
} | ||
|
||
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,60 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 v1alpha3 | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestAzureMachine_SetDefaultSSHPublicKey(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
type test struct { | ||
machine *AzureMachine | ||
} | ||
|
||
existingPublicKey := "testpublickey" | ||
publicKeyExistTest := test{machine: createMachineWithSSHPublicKey(t, existingPublicKey)} | ||
publicKeyNotExistTest := test{machine: createMachineWithSSHPublicKey(t, "")} | ||
|
||
err := publicKeyExistTest.machine.SetDefaultSSHPublicKey() | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(publicKeyExistTest.machine.Spec.SSHPublicKey).To(Equal(existingPublicKey)) | ||
|
||
err = publicKeyNotExistTest.machine.SetDefaultSSHPublicKey() | ||
g.Expect(err).To(BeNil()) | ||
g.Expect(publicKeyNotExistTest.machine.Spec.SSHPublicKey).To(Not(BeEmpty())) | ||
} | ||
|
||
func createMachineWithSSHPublicKey(t *testing.T, sshPublicKey string) *AzureMachine { | ||
return &AzureMachine{ | ||
Spec: AzureMachineSpec{ | ||
SSHPublicKey: sshPublicKey, | ||
Image: &Image{ | ||
SharedGallery: &AzureSharedGalleryImage{ | ||
SubscriptionID: "SUB123", | ||
ResourceGroup: "RG123", | ||
Name: "NAME123", | ||
Gallery: "GALLERY1", | ||
Version: "1.0.0", | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
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 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 v1alpha3 | ||
|
||
import ( | ||
"golang.org/x/crypto/ssh" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
// ValidateSSHKey validates an SSHKey | ||
func ValidateSSHKey(sshKey string, fldPath *field.Path) field.ErrorList { | ||
allErrs := field.ErrorList{} | ||
|
||
if _, _, _, _, err := ssh.ParseAuthorizedKey([]byte(sshKey)); err != nil { | ||
allErrs = append(allErrs, field.Required(fldPath, "the SSH public key is not valid")) | ||
return allErrs | ||
} | ||
|
||
return allErrs | ||
} |
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,65 @@ | ||
/* | ||
Copyright 2020 The Kubernetes Authors. | ||
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 v1alpha3 | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"golang.org/x/crypto/ssh" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
func TestAzureMachine_ValidateSSHKey(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
tests := []struct { | ||
name string | ||
sshKey string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid ssh key", | ||
sshKey: generateSSHPublicKey(), | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid ssh key", | ||
sshKey: "invalid ssh key", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
err := ValidateSSHKey(tc.sshKey, field.NewPath("sshPublicKey")) | ||
if tc.wantErr { | ||
g.Expect(err).ToNot(HaveLen(0)) | ||
} else { | ||
g.Expect(err).To(HaveLen(0)) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func generateSSHPublicKey() string { | ||
privateKey, _ := rsa.GenerateKey(rand.Reader, 2048) | ||
publicRsaKey, _ := ssh.NewPublicKey(&privateKey.PublicKey) | ||
return string(ssh.MarshalAuthorizedKey(publicRsaKey)) | ||
} |
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
Oops, something went wrong.