Skip to content

Commit

Permalink
upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
xinliangnote committed Feb 3, 2021
1 parent e5ea279 commit a470130
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 5 deletions.
44 changes: 44 additions & 0 deletions deploy/loki/loki.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
auth_enabled: false

server:
http_listen_port: 3100

ingester:
lifecycler:
address: 127.0.0.1
ring:
kvstore:
store: inmemory
replication_factor: 1
final_sleep: 0s
chunk_idle_period: 5m
chunk_retain_period: 30s

schema_config:
configs:
- from: 2020-01-01
store: boltdb
object_store: filesystem
schema: v9
index:
prefix: index_
period: 168h # 每张表的时间范围6天

storage_config:
boltdb:
directory: /data/loki/index # 索引文件存储地址

filesystem:
directory: /data/loki/chunks # 块存储地址

limits_config:
enforce_metric_name: false
reject_old_samples: true
reject_old_samples_max_age: 168h

chunk_store_config:
max_look_back_period: 0s

table_manager:
retention_deletes_enabled: false
retention_period: 0s
20 changes: 20 additions & 0 deletions deploy/loki/promtail.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
server:
http_listen_port: 9080
grpc_listen_port: 0

# Positions
positions:
filename: /data/loki/positions.yaml

# Loki服务器的地址
clients:
- url: http://127.0.0.1:3100/loki/api/v1/push

scrape_configs:
- job_name: go-gin-api
static_configs:
- targets:
- localhost
labels:
job: accesslog
__path__: /data/logs/*.log # 日志目录
3 changes: 2 additions & 1 deletion internal/api/controller/user_handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/xinliangnote/go-gin-api/internal/pkg/cache"
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
"github.com/xinliangnote/go-gin-api/internal/pkg/db"
"github.com/xinliangnote/go-gin-api/pkg/ddm"

"go.uber.org/zap"
)
Expand Down Expand Up @@ -169,7 +170,7 @@ func (u *userDemo) Detail() core.HandlerFunc {
res.Id = user.Id
res.UserName = user.UserName
res.NickName = user.NickName
res.Mobile = user.Mobile
res.Mobile = ddm.Mobile(user.Mobile)
c.Payload(code.OK.WithData(res))
}
}
10 changes: 6 additions & 4 deletions internal/api/model/user_model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package user_model

import (
"time"

"github.com/xinliangnote/go-gin-api/pkg/ddm"
)

// 用户Demo表
Expand Down Expand Up @@ -54,8 +56,8 @@ type DetailRequest struct {

// user_handler Detail Response
type DetailResponse struct {
Id uint `json:"id"` // 用户主键ID
UserName string `json:"user_name"` // 用户名
NickName string `json:"nick_name"` // 昵称
Mobile string `json:"mobile"` // 手机号
Id uint `json:"id"` // 用户主键ID
UserName string `json:"user_name"` // 用户名
NickName string `json:"nick_name"` // 昵称
Mobile ddm.Mobile `json:"mobile"` // 手机号(脱敏)
}
26 changes: 26 additions & 0 deletions pkg/ddm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## DDM

动态数据掩码(Dynamic Data Masking,简称为DDM)能够防止把敏感数据暴露给未经授权的用户。

| 类型 | 要求 | 示例 | 说明
| ---- | ---- | ---- | ----
| 手机号 | 前 3 后 4 | 132****7986 | 定长 11 位数字
| 邮箱地址 | 前 1 后 1 | l**[email protected] | 仅对 @ 之前的邮箱名称进行掩码
| 姓名 | 隐姓 | *鸿章 | 将姓氏隐藏
| 密码 | 不输出 | ****** |
| 银行卡卡号 | 前 6 后 4 | 622888******5676 | 银行卡卡号最多 19 位数字
| 身份证号 | 前 1 后 1 | 1******7 | 定长 18 位

#### 代码示例

```
// 返回值
type message struct {
Email ddm.Email `json:"email"`
}
msg := new(message)
msg.Email = ddm.Email("[email protected]")
...
```
62 changes: 62 additions & 0 deletions pkg/ddm/mark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package ddm

import (
"fmt"
"strings"
)

func (m Mobile) MarshalJSON() ([]byte, error) {
if len(m) != 11 {
return []byte(`"` + m + `"`), nil
}

v := fmt.Sprintf("%s****%s", m[:3], m[len(m)-4:])
return []byte(`"` + v + `"`), nil
}

func (bc BankCard) MarshalJSON() ([]byte, error) {
if len(bc) > 19 || len(bc) < 16 {
return []byte(`"` + bc + `"`), nil
}

v := fmt.Sprintf("%s******%s", bc[:6], bc[len(bc)-4:])
return []byte(`"` + v + `"`), nil
}

func (card IDCard) MarshalJSON() ([]byte, error) {
if len(card) != 18 {
return []byte(`"` + card + `"`), nil
}

v := fmt.Sprintf("%s******%s", card[:1], card[len(card)-1:])
return []byte(`"` + v + `"`), nil
}

func (name IDName) MarshalJSON() ([]byte, error) {
if len(name) < 1 {
return []byte(`""`), nil
}

nameRune := []rune(name)
v := fmt.Sprintf("*%s", string(nameRune[1:]))
return []byte(`"` + v + `"`), nil
}

func (pw PassWord) MarshalJSON() ([]byte, error) {
v := "******"
return []byte(`"` + v + `"`), nil
}

func (e Email) MarshalJSON() ([]byte, error) {
if !strings.Contains(string(e), "@") {
return []byte(`"` + e + `"`), nil
}

split := strings.Split(string(e), "@")
if len(split[0]) < 1 || len(split[1]) < 1 {
return []byte(`"` + e + `"`), nil
}

v := fmt.Sprintf("%s***%s", split[0][:1], split[0][len(split[0])-1:])
return []byte(`"` + v + "@" + split[1] + `"`), nil
}
21 changes: 21 additions & 0 deletions pkg/ddm/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package ddm

// 手机号 132****7986
type Mobile string

// 银行卡号 622888******5676
type BankCard string

// 身份证号 1******7
type IDCard string

// 姓名 *鸿章
// TODO:参考 https://blog.thinkeridea.com/201910/go/efficient_string_truncation.html
// Deprecated:有更好的性能选择
type IDName string

// 密码 ******
type PassWord string

// 邮箱 l***[email protected]
type Email string
32 changes: 32 additions & 0 deletions pkg/ddm/type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ddm

import (
"encoding/json"
"testing"
)

type message struct {
Name IDName `json:"name"`
Mobile Mobile `json:"mobile"`
IDCard IDCard `json:"id_card"`
PassWord PassWord `json:"password"`
Email Email `json:"email"`
BankCard1 BankCard `json:"bank_card_1"`
BankCard2 BankCard `json:"bank_card_2"`
BankCard3 BankCard `json:"bank_card_3"`
}

func TestMarshalJSON(t *testing.T) {
msg := new(message)
msg.Name = IDName("李鸿章")
msg.Mobile = Mobile("13288887986")
msg.IDCard = IDCard("125252525252525252")
msg.PassWord = PassWord("123456")
msg.Email = Email("[email protected]")
msg.BankCard1 = BankCard("6545654565456545")
msg.BankCard2 = BankCard("65485269874569852")
msg.BankCard3 = BankCard("6548526987456985298")

marshal, _ := json.Marshal(msg)
t.Log(string(marshal))
}

0 comments on commit a470130

Please sign in to comment.