Skip to content

Commit e9ce98c

Browse files
committed
Add function that validate ssh_public_key of vm/lxc template parameter
1 parent ba21474 commit e9ce98c

File tree

6 files changed

+116
-11
lines changed

6 files changed

+116
-11
lines changed

api/executor/executor.pb.go

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1.pb.go

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

handlers/vm/lxc/lxc.go

+51
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import (
55
"flag"
66
"fmt"
77
"io"
8+
"strings"
89

910
"github.com/axsh/openvdc/handlers"
1011
"github.com/axsh/openvdc/handlers/vm"
1112
"github.com/axsh/openvdc/model"
1213
"github.com/golang/protobuf/proto"
1314
"github.com/pkg/errors"
15+
"golang.org/x/crypto/ssh"
16+
"io/ioutil"
1417
)
1518

1619
func init() {
@@ -55,9 +58,57 @@ func (h *LxcHandler) ParseTemplate(in json.RawMessage) (model.ResourceTemplate,
5558
return nil, handlers.ErrInvalidTemplate(h, "lxc_image or lxc_template must exist")
5659
}
5760

61+
switch (tmpl.AuthenticationType) {
62+
case model.LxcTemplate_NONE:
63+
case model.LxcTemplate_PUB_KEY:
64+
if (tmpl.SshPublicKey == "") {
65+
return nil, handlers.ErrInvalidTemplate(h, "ssh_public_key is not set")
66+
}
67+
key, err := ioutil.ReadFile(tmpl.SshPublicKey)
68+
if err != nil {
69+
return nil, handlers.ErrInvalidTemplate(h, "unable to read ssh_public_key key")
70+
}
71+
72+
isValidate := validatePublicKey(key)
73+
if !isValidate {
74+
return nil, handlers.ErrInvalidTemplate(h, "ssh_public_key is invalid")
75+
}
76+
77+
default:
78+
return nil, handlers.ErrInvalidTemplate(h, "Unknown authentication_type parameter" + tmpl.AuthenticationType.String())
79+
}
80+
5881
return tmpl, nil
5982
}
6083

84+
func validatePublicKey (key []byte)(bool){
85+
// Check that the key is in RFC4253 binary format.
86+
_, err := ssh.ParsePublicKey(key)
87+
if err == nil {
88+
return true
89+
}
90+
91+
keyStr := string(key[:]);
92+
// Check that the key is in OpenSSH format.
93+
keyNames := []string{"ssh-rsa", "ssh-dss", "ecdsa-sha2-nistp256", "ssh-ed25519"}
94+
firstStr := strings.Fields(keyStr)
95+
for _, name := range keyNames {
96+
if firstStr[0] == name {
97+
return true
98+
}
99+
}
100+
101+
// Check that the key is in SECSH format.
102+
keyNames = []string{"SSH2 ", "RSA", ""}
103+
for _, name := range keyNames {
104+
if ( strings.Contains(keyStr, "---- BEGIN " + name + "PUBLIC KEY ----") &&
105+
strings.Contains(keyStr, "---- END " + name + "PUBLIC KEY ----")) {
106+
return true
107+
}
108+
}
109+
return false
110+
}
111+
61112
func (h *LxcHandler) SetTemplateItem(t *model.Template, m model.ResourceTemplate) {
62113
t.Item = &model.Template_Lxc{
63114
Lxc: m.(*model.LxcTemplate),

handlers/vm/lxc/lxc_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,40 @@ const jsonLxcTemplate1 = `{
3737
}
3838
}`
3939

40+
const jsonLxcTemplate2 = `{
41+
"type": "vm/lxc",
42+
"lxc_template": {
43+
"download": {
44+
"distro": "ubuntu",
45+
"release": "xenial"
46+
}
47+
},
48+
"authentication_type":0
49+
}`
50+
51+
const jsonLxcTemplate3 = `{
52+
"type": "vm/lxc",
53+
"lxc_template": {
54+
"download": {
55+
"distro": "ubuntu",
56+
"release": "xenial"
57+
}
58+
},
59+
"authentication_type":1,
60+
"ssh_public_key":""
61+
}`
62+
63+
const jsonLxcTemplate4 = `{
64+
"type": "vm/lxc",
65+
"lxc_template": {
66+
"download": {
67+
"distro": "ubuntu",
68+
"release": "xenial"
69+
}
70+
},
71+
"authentication_type":1
72+
}`
73+
4074
func TestLxcHandler_ParseTemplate(t *testing.T) {
4175
assert := assert.New(t)
4276
h := &LxcHandler{}
@@ -53,4 +87,27 @@ func TestLxcHandler_ParseTemplate(t *testing.T) {
5387
modellxc = m.(*model.LxcTemplate)
5488
assert.Nil(modellxc.GetLxcImage())
5589
assert.NotNil(modellxc.GetLxcTemplate())
90+
assert.Equal(model.LxcTemplate_NONE, modellxc.AuthenticationType, "none")
91+
92+
m, err = h.ParseTemplate(bytes.NewBufferString(jsonLxcTemplate2).Bytes())
93+
assert.NoError(err)
94+
assert.IsType((*model.LxcTemplate)(nil), m)
95+
modellxc = m.(*model.LxcTemplate)
96+
assert.Nil(modellxc.GetLxcImage())
97+
assert.NotNil(modellxc.GetLxcTemplate())
98+
assert.Equal(model.LxcTemplate_NONE, modellxc.AuthenticationType, "none")
99+
100+
//m, err = h.ParseTemplate(bytes.NewBufferString(jsonLxcTemplate3).Bytes())
101+
//assert.NoError(err)
102+
//assert.IsType((*model.LxcTemplate)(nil), m)
103+
//modellxc = m.(*model.LxcTemplate)
104+
//assert.Nil(modellxc.GetLxcImage())
105+
//assert.NotNil(modellxc.GetLxcTemplate())
106+
//assert.Equal(model.LxcTemplate_PUB_KEY, modellxc.AuthenticationType, "pub_key")
107+
//assert.NotEmpty(modellxc.SshPublicKey)
108+
109+
m, err = h.ParseTemplate(bytes.NewBufferString(jsonLxcTemplate4).Bytes())
110+
// assert.EqualError(err,"ssh_public_key is not set")
56111
}
112+
113+

model/cluster.pb.go

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/vendor.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@
214214
"revisionTime": "2013-11-06T22:25:44Z"
215215
},
216216
{
217-
"checksumSHA1": "eIjJhEqZZmQwt++0jlQhbIhAcH4=",
217+
"checksumSHA1": "Cdsm9pkjn7WC0TP2KKPDSApiQKQ=",
218218
"path": "github.com/kr/pty",
219219
"revision": "ce7fa45920dc37a92de8377972e52bc55ffa8d57",
220220
"revisionTime": "2016-07-16T20:46:20Z"
@@ -461,7 +461,7 @@
461461
"revisionTime": "2016-09-25T22:06:09Z"
462462
},
463463
{
464-
"checksumSHA1": "MR10lNwh25urwTtpm+YxwQOASVM=",
464+
"checksumSHA1": "O14StIX7nfDHAE3E9JKalGcag9I=",
465465
"path": "github.com/ulikunitz/xz",
466466
"revision": "3807218c9f4ed05861fa9eb75b8fb8afd3325a34",
467467
"revisionTime": "2017-02-15T20:57:12Z"
@@ -575,7 +575,7 @@
575575
"revisionTime": "2016-10-26T17:59:44Z"
576576
},
577577
{
578-
"checksumSHA1": "uTQtOqR0ePMMcvuvAIksiIZxhqU=",
578+
"checksumSHA1": "Xhsm+TevJogC8U4sG6FO+czBMps=",
579579
"path": "golang.org/x/sys/unix",
580580
"revision": "d75a52659825e75fff6158388dddc6a5b04f9ba5",
581581
"revisionTime": "2016-12-14T18:38:57Z"
@@ -587,7 +587,7 @@
587587
"revisionTime": "2016-12-29T11:00:09Z"
588588
},
589589
{
590-
"checksumSHA1": "Vircurgvsnt4k26havmxPM67PUA=",
590+
"checksumSHA1": "ZKCa+wAQGqlSqljoSFqx9pOOaW8=",
591591
"path": "golang.org/x/text/unicode/norm",
592592
"revision": "44f4f658a783b0cee41fe0a23b8fc91d9c120558",
593593
"revisionTime": "2016-12-29T11:00:09Z"
@@ -671,5 +671,5 @@
671671
"revisionTime": "2016-09-28T15:37:09Z"
672672
}
673673
],
674-
"rootPath": "/github.com/axsh/openvdc"
674+
"rootPath": "github.com/axsh/openvdc"
675675
}

0 commit comments

Comments
 (0)