Skip to content

Commit 99b79cb

Browse files
committed
add create_db_and_user.sql
1 parent f936f2d commit 99b79cb

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535

3636
![数据表](docs/figures/database.png)
3737

38+
> - 请根据 [server/sql/bootstrap.sql](server/sql/bootstrap.sql) 创建表
39+
> - 请根据 [server/sql/create_db_and_user.sql](server/sql/create_db_and_user.sql) 创建数据库和用户
40+
3841
## 部署
3942

4043
### 配置
@@ -64,3 +67,4 @@ lain secret add ${LAIN-Domain} web /lain/app/prod.json -f example.json
6467
## Licensing
6568
6669
Entry is released under [MIT](https://github.com/laincloud/entry/blob/master/LICENSE) license.
70+

server/handler/handle_websocket.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,14 @@ func handleRequest(conn *websocket.Conn, s *models.Session, sessionWriter io.Wri
9696
if commandContent != "" {
9797
command := models.Command{
9898
SessionID: s.SessionID,
99-
Session: *s,
10099
User: s.User,
101100
Content: commandContent,
102101
}
103102
g.DB.Create(&command)
104103
if command.IsRisky() {
105104
log.Warnf("Dangerous command! Will alert entry owners... Command.Content: %v, session: %+v.", command.Content, s)
106105
go func() {
107-
if err1 := command.Alert(g); err1 != nil {
106+
if err1 := command.Alert(*s, g); err1 != nil {
108107
log.Errorf("command.Alert() failed, error: %v.", err1)
109108
}
110109
}()

server/models/command.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
const (
15-
mailTemplate = `Subject: [Entry@{{.LAINDomain}}][{{.Command.Session.AppName}}] - Dangerous Command
15+
mailTemplate = `Subject: [Entry@{{.LAINDomain}}][{{.Session.AppName}}] - Dangerous Command
1616
MIME-version: 1.0;
1717
Content-Type: text/html; charset="UTF-8";
1818
@@ -64,32 +64,32 @@ Content-Type: text/html; charset="UTF-8";
6464
<caption>Additional Infomation</caption>
6565
<tr>
6666
<td>App Name</td>
67-
<td>{{.Command.Session.AppName}}</td>
67+
<td>{{.Session.AppName}}</td>
6868
</tr>
6969
7070
<tr>
7171
<td>User</td>
72-
<td>{{.Command.Session.User}}</td>
72+
<td>{{.Session.User}}</td>
7373
</tr>
7474
7575
<tr>
7676
<td>Source IP</td>
77-
<td>{{.Command.Session.SourceIP}}</td>
77+
<td>{{.Session.SourceIP}}</td>
7878
</tr>
7979
8080
<tr>
8181
<td>Proc Name</td>
82-
<td>{{.Command.Session.ProcName}}</td>
82+
<td>{{.Session.ProcName}}</td>
8383
</tr>
8484
8585
<tr>
8686
<td>Instance No</td>
87-
<td>{{.Command.Session.InstanceNo}}</td>
87+
<td>{{.Session.InstanceNo}}</td>
8888
</tr>
8989
9090
<tr>
9191
<td>Node IP</td>
92-
<td>{{.Command.Session.NodeIP}}</td>
92+
<td>{{.Session.NodeIP}}</td>
9393
</tr>
9494
9595
<tr>
@@ -171,20 +171,21 @@ func isRisky(commandContent string) bool {
171171
// MailData will be inserted into mailTemplate
172172
type MailData struct {
173173
Command Command
174+
Session Session
174175
LAINDomain string
175176
}
176177

177178
// Alert alert dangerous command
178-
func (c Command) Alert(g *global.Global) error {
179-
msg, err := c.newMailMessage(g.LAINDomain)
179+
func (c Command) Alert(s Session, g *global.Global) error {
180+
msg, err := c.newMailMessage(g.LAINDomain, s)
180181
if err != nil {
181182
return err
182183
}
183184

184185
return util.SendMail(msg, g)
185186
}
186187

187-
func (c Command) newMailMessage(lainDomain string) ([]byte, error) {
188+
func (c Command) newMailMessage(lainDomain string, s Session) ([]byte, error) {
188189
t, err := template.New("mail").Parse(mailTemplate)
189190
if err != nil {
190191
return nil, err
@@ -193,6 +194,7 @@ func (c Command) newMailMessage(lainDomain string) ([]byte, error) {
193194
var buf bytes.Buffer
194195
data := MailData{
195196
Command: c,
197+
Session: s,
196198
LAINDomain: lainDomain,
197199
}
198200
if err = t.Execute(&buf, data); err != nil {

server/sql/create_db_and_user.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
create database entry;
2+
3+
create user entry@'%' identified by 'password';
4+
5+
grant select, insert, update(status, ended_at, updated_at) on entry.sessions to entry@'%';
6+
grant select, insert on entry.commands to entry@'%';
7+
flush privileges;

server/util/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func AuthContainer(token, appName string, g *global.Global) (*sso.User, error) {
5252

5353
// AuthAPI authorizes whether the client with this token has right to access the API
5454
func AuthAPI(accessToken string, g *global.Global) (*sso.User, error) {
55-
user, err := g.SSOClient.GetUser(accessToken)
55+
user, err := g.SSOClient.GetMe(accessToken)
5656
if err != nil {
5757
return nil, err
5858
}

0 commit comments

Comments
 (0)