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

Fix userkey create. #111

Merged
merged 2 commits into from Jan 3, 2019

Conversation

Projects
None yet
2 participants
@shawn111
Copy link
Contributor

shawn111 commented Dec 12, 2018

What this PR does / why we need it:
echo "ssh-rsa key ..." | ssh localhost -p 2222 -l admin userkey create shawn
Without the PR, it shows error: Key: non zero value required;AuthorizedKey: non zero value required;
For the AuthorizedKey, I compared the code in ssh.go, I thought it might be missing.

For the Key, it is a []byte field. The required valid cause Key: non zero value required.

Which issue this PR fixes: fixes #xxx, fixes #xxx...
fixes #46
Special notes for your reviewer:

--- a/db.go
+++ b/db.go
@@ -70,7 +70,7 @@ type Host struct {
 // UserKey defines a user public key used by sshportal to identify the user
 type UserKey struct {
        gorm.Model
-       Key           []byte `sql:"size:10000" valid:"required,length(1|10000)"`
+       Key           []byte `sql:"size:10000" valid:"length(1|10000)"` // FIXME
        AuthorizedKey string `sql:"size:10000" valid:"required,length(1|10000)"`
        UserID        uint   ``
        User          *User  `gorm:"ForeignKey:UserID"`
(END)
@shawn111

This comment has been minimized.

Copy link
Contributor

shawn111 commented Dec 12, 2018

Hi @moul,

I used isEmptyValue to test the field, userkey.Key.
Even it is not Empty but it is a []byte field.
govalidator.ValidateStruct would check each byte in the userkey.Key, the required check would be wrong.

func isEmptyValue(v reflect.Value) bool {
        switch v.Kind() {
        case reflect.String, reflect.Array:
                return v.Len() == 0
        case reflect.Map, reflect.Slice:
                return v.Len() == 0 || v.IsNil()
        case reflect.Bool:
                return !v.Bool()
        case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
                return v.Int() == 0
        case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
                return v.Uint() == 0
        case reflect.Float32, reflect.Float64:
                return v.Float() == 0
        case reflect.Interface, reflect.Ptr:
                return v.IsNil()
        }

        return reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface())
}
Fix UserKey.Key for govalidator.ValidateStruct.
UserKey.Key is a []byte field.
The valid: "required" would cause wrong Validation.

@shawn111 shawn111 changed the title Fix userkey create, missing AuthorizedKey Fix userkey create. Dec 13, 2018

@shawn111 shawn111 closed this Dec 17, 2018

@shawn111 shawn111 deleted the shawn111:missing_authorized_key branch Dec 17, 2018

@shawn111 shawn111 restored the shawn111:missing_authorized_key branch Dec 17, 2018

@shawn111 shawn111 reopened this Dec 17, 2018

@moul

moul approved these changes Jan 3, 2019

Copy link
Owner

moul left a comment

LGTM

@moul moul merged commit f220af5 into moul:master Jan 3, 2019

1 check passed

guardrails/scan no new security issues detected
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment