-
Notifications
You must be signed in to change notification settings - Fork 41
/
common.go
173 lines (159 loc) · 5.54 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright 2023 Ant Group Co., Ltd.
//
// 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 common
import (
"errors"
"fmt"
"net/http"
"unicode"
"golang.org/x/exp/slices"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/secretflow/scql/pkg/broker/storage"
"github.com/secretflow/scql/pkg/constant"
"github.com/secretflow/scql/pkg/parser"
"github.com/secretflow/scql/pkg/planner/core"
pb "github.com/secretflow/scql/pkg/proto-gen/scql"
"github.com/secretflow/scql/pkg/status"
"github.com/secretflow/scql/pkg/util/logutil"
"github.com/secretflow/scql/pkg/util/message"
prom "github.com/secretflow/scql/pkg/util/prometheus"
)
func VerifyTableMeta(meta storage.TableMeta) error {
// 1. check DB type
_, err := core.ParseDBType(meta.Table.DBType)
if err != nil {
return fmt.Errorf("VerifyTableMeta: %s", err)
}
// 2. check column type
for _, col := range meta.Columns {
if ok := constant.SupportTypes[col.DType]; !ok {
return fmt.Errorf("VerifyTableMeta: unsupported data type %s", col.DType)
}
}
// 3. check table/column name
if hasSpaceInString(meta.Table.ProjectID) {
return fmt.Errorf("VerifyTableMeta: illegal project id %s which contains space", meta.Table.ProjectID)
}
if hasSpaceInString(meta.Table.TableName) {
return fmt.Errorf("VerifyTableMeta: illegal table name %s which contains space", meta.Table.TableName)
}
createFormat := `
create table %s.%s (
%s
);
`
columnStr := ""
for i := 0; i < len(meta.Columns); i++ {
if hasSpaceInString(meta.Columns[i].ColumnName) {
return fmt.Errorf("VerifyTableMeta: illegal column name %s which contains space", meta.Columns[i].ColumnName)
}
columnStr += fmt.Sprintf("%s int", meta.Columns[i].ColumnName)
if i < len(meta.Columns)-1 {
columnStr += ",\n"
}
}
p := parser.New()
_, err = p.ParseOneStmt(fmt.Sprintf(createFormat, meta.Table.ProjectID, meta.Table.TableName, columnStr), "", "")
if err != nil {
return fmt.Errorf("VerifyTableMeta: %s", err)
}
// 4. check ref table
_, err = core.NewDbTableFromString(meta.Table.RefTable)
if err != nil {
return fmt.Errorf("VerifyTableMeta: %s", err)
}
return nil
}
func VerifyProjectID(projectID string) error {
if hasSpaceInString(projectID) {
return fmt.Errorf("VerifyProjectID: illegal project id %s which contains space", projectID)
}
p := parser.New()
// project id may work as db name
_, err := p.ParseOneStmt(fmt.Sprintf("create database %s;", projectID), "", "")
if err != nil {
return fmt.Errorf("VerifyProjectID: %s", err)
}
return nil
}
func hasSpaceInString(s string) bool {
for _, c := range s {
if unicode.IsSpace(c) {
return true
}
}
return false
}
func feedResponseStatus(c *gin.Context, response proto.Message, err error) {
if err != nil {
var statusPointer *status.Status
if !errors.As(err, &statusPointer) {
statusPointer = status.New(pb.Code_INTERNAL, err.Error())
}
statusDesc := response.ProtoReflect().Descriptor().Fields().ByJSONName("status")
response.ProtoReflect().Set(statusDesc, protoreflect.ValueOf(statusPointer.ToProto().ProtoReflect()))
c.Set(prom.ResponseStatusKey, statusPointer.Code().String())
} else {
c.Set(prom.ResponseStatusKey, pb.Code_OK.String())
}
}
func FeedResponse(c *gin.Context, response proto.Message, err error, encodingType message.ContentEncodingType) {
feedResponseStatus(c, response, err)
body, err := message.SerializeTo(response, encodingType)
if err != nil {
c.String(http.StatusInternalServerError, fmt.Sprintf("FeedResponse: unable to serialize response: %+v", response))
return
}
c.String(http.StatusOK, body)
}
func LogWithError(logEntry *logutil.BrokerMonitorLogEntry, err error) {
if err != nil {
logEntry.ErrorMsg = err.Error()
logrus.Errorf("%v", logEntry)
} else {
logrus.Infof("%v", logEntry)
}
}
// Check Archived/ProjectConf/Creator
func CheckInvitationCompatibleWithProj(invitation storage.Invitation, proj storage.Project) error {
if proj.Archived {
return fmt.Errorf("failed to reply invitation due to project %s archived", proj.ID)
}
if proj.ProjectConf != invitation.ProjectConf {
return fmt.Errorf("failed to check project config got %+v from project but expected %+v", proj.ProjectConf, invitation.ProjectConf)
}
if proj.Creator != invitation.Creator {
return fmt.Errorf("failed to check creator got %q from project but expected %q", proj.Creator, invitation.Creator)
}
return nil
}
// check project exist and current party is member
func CheckMemberExistInProject(manager *storage.MetaManager, projectID string, member string) error {
return manager.ExecInMetaTransaction(func(txn *storage.MetaTransaction) error {
members, err := txn.GetProjectMembers(projectID)
if err != nil {
return err
}
if len(members) == 0 {
return fmt.Errorf("CheckMemberExistInProject: project %s has no members or project doesn't exist", projectID)
}
if !slices.Contains(members, member) {
return fmt.Errorf("CheckMemberExistInProject: issuer code %s is not member of project %s", member, projectID)
}
return nil
})
}