Skip to content
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
Merged

Fix userkey create. #111

merged 2 commits into from
Jan 3, 2019

Conversation

shawn111
Copy link
Contributor

@shawn111 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
Copy link
Contributor Author

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())
}

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 missing_authorized_key branch December 17, 2018 12:56
@shawn111 shawn111 restored the missing_authorized_key branch December 17, 2018 12:56
@shawn111 shawn111 reopened this Dec 17, 2018
Copy link
Owner

@moul moul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@moul moul merged commit f220af5 into moul:master Jan 3, 2019
@shawn111 shawn111 deleted the missing_authorized_key branch May 13, 2019 01:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

User key create freeze
2 participants