diff --git a/engine/api/database/gorpmapping/dao.go b/engine/api/database/gorpmapping/dao.go index effbd1f0bd..722adc6f1d 100644 --- a/engine/api/database/gorpmapping/dao.go +++ b/engine/api/database/gorpmapping/dao.go @@ -27,6 +27,11 @@ func Insert(db gorp.SqlExecutor, i interface{}) error { return err } + _, has := getTabbleMapping(i) + if !has { + return sdk.WithStack(fmt.Errorf("unkown entity %T", i)) + } + err := db.Insert(i) if e, ok := err.(*pq.Error); ok { switch e.Code { @@ -52,12 +57,10 @@ func Insert(db gorp.SqlExecutor, i interface{}) error { return nil } -// Update value in given db. -func Update(db gorp.SqlExecutor, i interface{}) error { +func UpdateColumns(db gorp.SqlExecutor, i interface{}, columnFilter gorp.ColumnFilter) error { if err := checkDatabase(db); err != nil { return err } - mapping, has := getTabbleMapping(i) if !has { return sdk.WithStack(fmt.Errorf("unkown entity %T", i)) @@ -104,7 +107,7 @@ func Update(db gorp.SqlExecutor, i interface{}) error { } } - n, err := db.Update(i) + n, err := db.UpdateColumns(columnFilter, i) if e, ok := err.(*pq.Error); ok { switch e.Code { case ViolateUniqueKeyPGCode: @@ -131,6 +134,18 @@ func Update(db gorp.SqlExecutor, i interface{}) error { return nil } +func acceptAllFilter(col *gorp.ColumnMap) bool { + return true +} + +// Update value in given db. +func Update(db gorp.SqlExecutor, i interface{}) error { + if err := checkDatabase(db); err != nil { + return err + } + return UpdateColumns(db, i, acceptAllFilter) +} + // Delete value in given db. func Delete(db gorp.SqlExecutor, i interface{}) error { if err := checkDatabase(db); err != nil { diff --git a/engine/api/database/gorpmapping/encryption.go b/engine/api/database/gorpmapping/encryption.go index 4b3198da11..057b26d401 100644 --- a/engine/api/database/gorpmapping/encryption.go +++ b/engine/api/database/gorpmapping/encryption.go @@ -136,10 +136,15 @@ func resetEncryptedData(db gorp.SqlExecutor, i interface{}) error { } for _, f := range mapping.EncryptedFields { - // Reset the field to the zero value + // Reset the field to the zero value of the placeholder field := val.FieldByName(f.Name) - placeholder := reflect.ValueOf(sdk.PasswordPlaceholder) - field.Set(placeholder) + if field.Kind() == reflect.String { + placeholder := reflect.ValueOf(sdk.PasswordPlaceholder) + field.Set(placeholder) + } else { + placeholder := reflect.Zero(field.Type()) + field.Set(placeholder) + } } return nil } @@ -171,7 +176,6 @@ func getEncryptedData(db gorp.SqlExecutor, i interface{}) error { var encryptedColumnsSlice = make([]string, len(mapping.EncryptedFields)) var fieldsValue = make(map[int]*reflect.Value, len(mapping.EncryptedFields)) - var encryptedFieldsSlice = make([]interface{}, len(mapping.EncryptedFields)) var encryptedContents = make([]interface{}, len(mapping.EncryptedFields)) var extrasFieldsNames = make(map[int][]string, len(mapping.EncryptedFields)) @@ -190,9 +194,6 @@ func getEncryptedData(db gorp.SqlExecutor, i interface{}) error { field := val.FieldByName(f.Name) fieldsValue[idx] = &field - fi := field.Interface() - - encryptedFieldsSlice[idx] = &fi extrasFieldsNames[idx] = f.Extras } @@ -211,10 +212,13 @@ func getEncryptedData(db gorp.SqlExecutor, i interface{}) error { } var encryptedContent = encryptedContent.(*[]byte) - if err := Decrypt(*encryptedContent, encryptedFieldsSlice[idx], extras); err != nil { + var targetField = val.FieldByName(mapping.EncryptedFields[idx].Name) + var targetHolder = reflect.New(reflect.TypeOf(targetField.Interface())).Interface() + + if err := Decrypt(*encryptedContent, targetHolder, extras); err != nil { return err } - fieldsValue[idx].Set(reflect.ValueOf(*encryptedFieldsSlice[idx].(*interface{}))) + fieldsValue[idx].Set(reflect.ValueOf(targetHolder).Elem()) } return nil diff --git a/engine/api/database/gorpmapping/gorpmapping.go b/engine/api/database/gorpmapping/gorpmapping.go index 4962684ae7..659ed96c56 100644 --- a/engine/api/database/gorpmapping/gorpmapping.go +++ b/engine/api/database/gorpmapping/gorpmapping.go @@ -38,9 +38,14 @@ func deepFields(iface interface{}) []reflect.StructField { for i := 0; i < ift.NumField(); i++ { v := ifv.Field(i) + dbTag, hasDBTag := ift.Field(i).Tag.Lookup("db") + tagValues := strings.Split(dbTag, ",") + if len(tagValues) >= 0 && tagValues[0] == "-" { + continue + } - switch v.Kind() { - case reflect.Struct: + switch { + case v.Kind() == reflect.Struct && !hasDBTag: fields = append(fields, deepFields(v.Interface())...) default: fields = append(fields, ift.Field(i)) @@ -67,17 +72,22 @@ func New(target interface{}, name string, autoIncrement bool, keys ...string) Ta fields := deepFields(target) for i := 0; i < len(fields); i++ { - gmTag, ok := fields[i].Tag.Lookup("gorpmapping") - if ok { + dbTag, okDBTag := fields[i].Tag.Lookup("db") + if !okDBTag { + continue + } + + tagValues := strings.Split(dbTag, ",") + if len(tagValues) == 0 { + continue + } + + gmTag, okGMTag := fields[i].Tag.Lookup("gorpmapping") + if okGMTag { tagValues := strings.Split(gmTag, ",") if len(tagValues) == 0 { continue } - - dbTag, ok := fields[i].Tag.Lookup("db") - if !ok { - continue - } column := strings.SplitN(dbTag, ",", 2)[0] if tagValues[0] == "encrypted" { diff --git a/engine/api/database/gorpmapping/signature.go b/engine/api/database/gorpmapping/signature.go index 82ee0feb7f..78accfb24e 100644 --- a/engine/api/database/gorpmapping/signature.go +++ b/engine/api/database/gorpmapping/signature.go @@ -179,7 +179,7 @@ func checkSignature(i Canonicaller, k symmecrypt.Key, f *CanonicalForm, sig []by decryptedSig, err := k.Decrypt(sig) if err != nil { - return false, sdk.WrapError(err, "unable to decrypt content") + return false, sdk.WrapError(err, "unable to decrypt content (%s)", string(sig)) } res := clearContent.String() == string(decryptedSig) diff --git a/engine/api/test/gorp.go b/engine/api/test/gorp.go index 6495b131da..6daaf2f1c3 100644 --- a/engine/api/test/gorp.go +++ b/engine/api/test/gorp.go @@ -28,8 +28,11 @@ func (s *SqlExecutorMock) WithContext(ctx context.Context) gorp.SqlExecutor { re func (s *SqlExecutorMock) Get(i interface{}, keys ...interface{}) (interface{}, error) { return nil, nil } -func (s *SqlExecutorMock) Insert(list ...interface{}) error { return nil } -func (s *SqlExecutorMock) Update(list ...interface{}) (int64, error) { return 0, nil } +func (s *SqlExecutorMock) Insert(list ...interface{}) error { return nil } +func (s *SqlExecutorMock) Update(list ...interface{}) (int64, error) { return 0, nil } +func (s *SqlExecutorMock) UpdateColumns(columnFilter gorp.ColumnFilter, list ...interface{}) (int64, error) { + return 0, nil +} func (s *SqlExecutorMock) Delete(list ...interface{}) (int64, error) { return 0, nil } func (s *SqlExecutorMock) Exec(query string, args ...interface{}) (sql.Result, error) { return nil, nil } func (s *SqlExecutorMock) Select(i interface{}, query string, args ...interface{}) ([]interface{}, error) { diff --git a/go.mod b/go.mod index d7e947f54c..7138e19cea 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,6 @@ require ( github.com/Shopify/sarama v1.19.0 github.com/alecthomas/jsonschema v0.0.0-20200123075451-43663a393755 github.com/andygrunwald/go-gerrit v0.0.0-20181207071854-19ef3e9332a4 - github.com/apoydence/onpar v0.0.0-20190519213022-ee068f8ea4d1 // indirect github.com/araddon/gou v0.0.0-20180315155215-820e9f87cd05 // indirect github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect github.com/armon/go-radix v1.0.0 // indirect @@ -45,7 +44,7 @@ require ( github.com/duosecurity/duo_api_golang v0.0.0-20180315112207-d0530c80e49a // indirect github.com/eapache/go-resiliency v1.1.0 github.com/fatih/color v1.7.0 - github.com/fatih/structs v1.0.0 + github.com/fatih/structs v1.0.0 // indirect github.com/fortytw2/leaktest v1.2.0 // indirect github.com/frankban/quicktest v1.6.0 // indirect github.com/fsamin/go-dump v1.0.9 @@ -106,9 +105,9 @@ require ( github.com/kr/pty v1.1.8 // indirect github.com/kylelemons/godebug v1.1.0 // indirect github.com/lib/pq v1.0.0 - github.com/lytics/logrus v0.0.0-20170528191427-4389a17ed024 + github.com/lytics/logrus v0.0.0-20170528191427-4389a17ed024 // indirect github.com/magiconair/properties v1.8.1 // indirect - github.com/mailru/easyjson v0.0.0-20171120080333-32fa128f234d + github.com/mailru/easyjson v0.0.0-20171120080333-32fa128f234d // indirect github.com/marstr/guid v1.1.0 // indirect github.com/maruel/panicparse v1.3.0 github.com/mattbaird/elastigo v0.0.0-20170123220020-2fe47fd29e4b // indirect @@ -153,7 +152,6 @@ require ( github.com/poy/onpar v0.0.0-20190519213022-ee068f8ea4d1 // indirect github.com/prometheus/client_golang v1.1.0 // indirect github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 // indirect - github.com/prometheus/common v0.6.0 github.com/rcrowley/go-metrics v0.0.0-20190826022208-cac0b30c2563 // indirect github.com/rubenv/sql-migrate v0.0.0-20160620083229-6f4757563362 github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec // indirect @@ -193,7 +191,6 @@ require ( golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a golang.org/x/text v0.3.2 - google.golang.org/appengine v1.6.1 google.golang.org/genproto v0.0.0-20190817000702-55e96fffbd48 // indirect google.golang.org/grpc v1.23.0 gopkg.in/AlecAivazis/survey.v1 v1.7.1 @@ -225,7 +222,7 @@ require ( replace github.com/alecthomas/jsonschema => github.com/sguiheux/jsonschema v0.2.0 -replace github.com/go-gorp/gorp => github.com/yesnault/gorp v2.0.1-0.20190906143353-6210446a0d92+incompatible +replace github.com/go-gorp/gorp => github.com/yesnault/gorp v2.0.1-0.20200325154225-2dc6d8c2da37+incompatible replace github.com/docker/docker => github.com/docker/engine v0.0.0-20180816081446-320063a2ad06 diff --git a/go.sum b/go.sum index 58e6ee8ea2..692713148e 100644 --- a/go.sum +++ b/go.sum @@ -39,8 +39,6 @@ github.com/Shopify/sarama v1.19.0 h1:9oksLxC6uxVPHPVYUmq6xhr1BOF/hHobWH2UzO67z1s github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo= github.com/Shopify/toxiproxy v2.1.4+incompatible h1:TKdv8HiTLgE5wdJuEML90aBgNWsokNbMijUGhmcoBJc= github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= -github.com/alecthomas/jsonschema v0.0.0-20200123075451-43663a393755 h1:+xHAcx6b5TS1DiY+StyK2CrNvD0nQ97G7w45x4ljJFs= -github.com/alecthomas/jsonschema v0.0.0-20200123075451-43663a393755/go.mod h1:Juc2PrI3wtNfUwptSvAIeNx+HrETwHQs6nf+TkOJlOA= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY= @@ -51,8 +49,6 @@ github.com/aokoli/goutils v1.1.0 h1:jy4ghdcYvs5EIoGssZNslIASX5m+KNMfyyKvRQ0TEVE= github.com/aokoli/goutils v1.1.0/go.mod h1:SijmP0QR8LtwsmDs8Yii5Z/S4trXFGFC2oO5g9DP+DQ= github.com/apache/thrift v0.12.0 h1:pODnxUFNcjP9UTLZGTdeh+j16A8lJbRvD3rOtrk/7bs= github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ= -github.com/apoydence/onpar v0.0.0-20190519213022-ee068f8ea4d1 h1:vToH0A6ByBaoM/A/4AwxgHKFgzcufNcYQfDcp60uW3M= -github.com/apoydence/onpar v0.0.0-20190519213022-ee068f8ea4d1/go.mod h1:maauOJD0kdDqIz4xmkunipFVbBoTM6pFSy0kkWBcIUY= github.com/araddon/gou v0.0.0-20180315155215-820e9f87cd05 h1:MbFvRvOzFRujpuBALUI/2ll9+8PYi23cEp/DSQFEBfU= github.com/araddon/gou v0.0.0-20180315155215-820e9f87cd05/go.mod h1:ikc1XA58M+Rx7SEbf0bLJCfBkwayZ8T5jBo5FXK8Uz8= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 h1:G1bPvciwNyF7IUmKXNt9Ak3m6u9DE1rF+RmtIkBpVdA= @@ -142,8 +138,6 @@ github.com/frankban/quicktest v1.6.0 h1:Cd62nl66vQsx8Uv1t8M0eICyxIwZG7MxiAOrdnnU github.com/frankban/quicktest v1.6.0/go.mod h1:jaStnuzAqU1AJdCO0l53JDCJrVDKcS03DbaAcR7Ks/o= github.com/fsamin/go-dump v1.0.9 h1:3MAneAJLnGfKTJtFEAdgrD+QqqK2Hwj7EJUQMQZcDls= github.com/fsamin/go-dump v1.0.9/go.mod h1:ZgKd2aOXAFFbbFuUgvQhu7mwTlI3d3qnTICMWdvAa9o= -github.com/fsamin/go-repo v0.1.4 h1:P70wyLGKAHeaVDuxHckc1mCM1tsqDsTNGrF/tu1+Z1s= -github.com/fsamin/go-repo v0.1.4/go.mod h1:V7Te54EYyamyQIp6UKfONBbXBlPF4u3C1bvcH7OtdLM= github.com/fsamin/go-repo v0.1.5 h1:YON5aisDn+0DpJlXvqrPT9/EGv13JgYL3X/3GXRlLhY= github.com/fsamin/go-repo v0.1.5/go.mod h1:V7Te54EYyamyQIp6UKfONBbXBlPF4u3C1bvcH7OtdLM= github.com/fsamin/go-shredder v0.0.0-20180118184739-b2488aedb5be h1:UhjSvwE1gxUYfekK9BXZ/LL55we9Avg+2Pt0PIlMYCk= @@ -187,8 +181,6 @@ github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfb github.com/golang/mock v1.2.0/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.3.1 h1:qGJ6qTW+x6xX/my+8YUVl4WNpX9B7+/l2tRsHGZ7f2s= github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFUx0Y= -github.com/golang/mock v1.4.0 h1:Rd1kQnQu0Hq3qvJppYSG0HtP+f5LPPUiDswTLiEegLg= -github.com/golang/mock v1.4.0/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/mock v1.4.1 h1:ocYkMQY5RrXTYgXl7ICpV0IXwlEQGwKIsery4gyXa1U= github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -465,7 +457,6 @@ github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDf github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= github.com/prometheus/client_golang v1.1.0 h1:BQ53HtBmfOitExawJ6LokA4x8ov/z0SYYb0+HxJfRI8= github.com/prometheus/client_golang v1.1.0/go.mod h1:I1FGZT9+L76gKKOs5djB6ezCbFQP1xR9D75/vuwEF3g= -github.com/prometheus/client_golang v1.5.1 h1:bdHYieyGlH+6OLEk2YQha8THib30KP0/yD0YH9m6xcA= github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190115171406-56726106282f/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= @@ -575,8 +566,8 @@ github.com/yesnault/go-toml v0.0.0-20191205182532-f5ef6cee7945 h1:icS0gqYJLvPFpn github.com/yesnault/go-toml v0.0.0-20191205182532-f5ef6cee7945/go.mod h1:SsMrIuedaKvK8GekjwjVv8gMnHNAuvoINpKCARzdsEQ= github.com/yesnault/gorp v2.0.0+incompatible h1:4pwtvWrXQTAgGVnzAB0X5jX/mWRQEa8zmcdNT5U8jyg= github.com/yesnault/gorp v2.0.0+incompatible/go.mod h1:7nhqtxBPZoPXx86SqUzP/OFLd8prnhkydPk51uJthQc= -github.com/yesnault/gorp v2.0.1-0.20190906143353-6210446a0d92+incompatible h1:lJBr14ALmOEDTV6iIAQLR8WSBSUIqeS8cFZ628Zl2fo= -github.com/yesnault/gorp v2.0.1-0.20190906143353-6210446a0d92+incompatible/go.mod h1:7nhqtxBPZoPXx86SqUzP/OFLd8prnhkydPk51uJthQc= +github.com/yesnault/gorp v2.0.1-0.20200325154225-2dc6d8c2da37+incompatible h1:8M+L4IKModOdc0ZsH0kHJiqZ7CmWF4yH8yy4InZxKyY= +github.com/yesnault/gorp v2.0.1-0.20200325154225-2dc6d8c2da37+incompatible/go.mod h1:7nhqtxBPZoPXx86SqUzP/OFLd8prnhkydPk51uJthQc= github.com/yuin/gluare v0.0.0-20170607022532-d7c94f1a80ed h1:I1vcLHWU9m30rA90rMrKPu0eD3NDA4FBlkB8WMaDyUw= github.com/yuin/gluare v0.0.0-20170607022532-d7c94f1a80ed/go.mod h1:9w6KSdZh23UWqOywWsRLUcJUrUNjRh4Ql3z9uVgnSP4= github.com/yuin/gopher-lua v0.0.0-20170901023928-8c2befcd3908 h1:D1Gc3nOtLEadaHIZD98eX2ABnEi0OW7UZGogFWERsFI=