Skip to content

Commit f87a806

Browse files
ZLBerseefloodXunzhuo
authored
feat: dynamic secret (#574)
Co-authored-by: seeflood <[email protected]> Co-authored-by: Xunzhuo <[email protected]>
1 parent 8aa188f commit f87a806

File tree

17 files changed

+550
-8
lines changed

17 files changed

+550
-8
lines changed

components/custom/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
// limitations under the License.
1414
package custom
1515

16+
import "mosn.io/layotto/components/ref"
17+
1618
type Config struct {
19+
ref.Config
1720
Type string `json:"type"`
1821
Version string `json:"version"`
1922
Metadata map[string]string `json:"metadata"`

components/lock/types.go

+3
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
// limitations under the License.
1414
package lock
1515

16+
import "mosn.io/layotto/components/ref"
17+
1618
type Feature string
1719

1820
// Lock's metadata
1921
type Config struct {
22+
ref.Config
2023
Type string `json:"type"`
2124
Metadata map[string]string `json:"metadata"`
2225
}

components/ref/config.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2021 Layotto Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package ref
18+
19+
//Config is ref json config
20+
type Config struct {
21+
SecretRef []*Item `json:"secret_ref"`
22+
}
23+
24+
type Item struct {
25+
Name string `json:"name"`
26+
Key string `json:"key"`
27+
}

components/sequencer/types.go

+4
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
1415
package sequencer
1516

17+
import "mosn.io/layotto/components/ref"
18+
1619
type Config struct {
20+
ref.Config
1721
Type string `json:"type"`
1822
BiggerThan map[string]int64 `json:"biggerThan"`
1923
Metadata map[string]string `json:"metadata"`

configs/config_ref_example.json

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"servers": [
3+
{
4+
"default_log_path": "stdout",
5+
"default_log_level": "DEBUG",
6+
"routers": [
7+
{
8+
"router_config_name": "actuator_dont_need_router"
9+
}
10+
],
11+
"listeners": [
12+
{
13+
"name": "grpc",
14+
"address": "127.0.0.1:34904",
15+
"bind_port": true,
16+
"filter_chains": [
17+
{
18+
"filters": [
19+
{
20+
"type": "grpc",
21+
"config": {
22+
"server_name": "runtime",
23+
"grpc_config": {
24+
"hellos": {
25+
"helloworld": {
26+
"type": "helloworld",
27+
"hello": "greeting"
28+
}
29+
},
30+
"secret_store": {
31+
"local.file": {
32+
"type":"local.file",
33+
"metadata": {
34+
"secretsFile": "../../configs/secret/config_secret_local_file.json"
35+
}
36+
},
37+
"local.env": {
38+
"type":"local.env",
39+
"metadata": {
40+
}
41+
}
42+
},
43+
"sequencer": {
44+
"redis": {
45+
"type":"redis",
46+
"metadata": {
47+
"redisHost": "127.0.0.1:6380",
48+
"redisPassword": ""
49+
},
50+
"secret_ref": [
51+
{
52+
"name": "local.file",
53+
"key": "redisPassword"
54+
}
55+
]
56+
}
57+
},
58+
"app": {
59+
"app_id": "app1",
60+
"grpc_callback_port": 9999
61+
}
62+
}
63+
}
64+
}
65+
]
66+
}
67+
]
68+
},
69+
{
70+
"name": "actuator",
71+
"address": "127.0.0.1:34999",
72+
"bind_port": true,
73+
"filter_chains": [
74+
{
75+
"filters": [
76+
{
77+
"type": "proxy",
78+
"config": {
79+
"downstream_protocol": "Http1",
80+
"upstream_protocol": "Http1",
81+
"router_config_name": "actuator_dont_need_router"
82+
}
83+
}
84+
]
85+
}
86+
],
87+
"stream_filters": [
88+
{
89+
"type": "actuator_filter"
90+
}
91+
]
92+
}
93+
]
94+
}
95+
]
96+
}

configs/secret/config_secret_local_file.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"db-user-pass": {
33
"username": "devuser",
44
"password": "S!S*d$zDsb="
5-
}
5+
},
6+
"redisPassword": "redis123"
67
}

docs/en/start/secret/secret_ref.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# use Secret Ref to obtain secret
2+
3+
we can inject secrets to other components using secret store.
4+
5+
using `secret_ref` config:
6+
7+
```json
8+
{
9+
"sequencer": {
10+
"redis": {
11+
"type": "redis",
12+
"metadata": {
13+
"redisHost": "127.0.0.1:6380",
14+
"redisPassword": ""
15+
},
16+
"secret_ref": [
17+
{
18+
"name": "local.file",
19+
"key": "redisPassword"
20+
}
21+
]
22+
}
23+
}
24+
}
25+
```
26+
27+
## Quick start
28+
29+
This example shows how to inject redis password to sequencer component using redis store
30+
31+
### Step 0: Run Redis with password
32+
33+
```shell
34+
docker run --name redis -p 6380:6379 -d --restart=always redis:5.0.3 redis-server --appendonly yes --requirepass "redis123"
35+
```
36+
37+
### Step 1: Run Layotto
38+
39+
After downloading the project code to the local, switch the code directory and compile:
40+
41+
```shell
42+
cd ${project_path}/cmd/layotto
43+
```
44+
45+
build:
46+
47+
```shell @if.not.exist layotto
48+
go build -o layotto
49+
```
50+
51+
Once finished, the layotto file will be generated in the directory, run it:
52+
53+
```shell @background
54+
./layotto start -c ../../configs/config_ref_example.json
55+
```
56+
57+
### Step 2: Run the client program and call Layotto to get the sequence
58+
59+
```shell
60+
cd ${project_path}/demo/sequencer/common/
61+
```
62+
63+
```shell @if.not.exist client
64+
go build -o client
65+
```
66+
67+
```shell
68+
./client -s "redis"
69+
```
70+
71+
If the following information is printed, the demo is successful:
72+
73+
```bash
74+
Try to get next id.Key:key666
75+
Next id:next_id:1
76+
Next id:next_id:2
77+
Next id:next_id:3
78+
Next id:next_id:4
79+
Next id:next_id:5
80+
Next id:next_id:6
81+
Next id:next_id:7
82+
Next id:next_id:8
83+
Next id:next_id:9
84+
Next id:next_id:10
85+
Demo success!
86+
87+
```

docs/zh/start/secret/secret_ref.md

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 使用 Secret Ref 注入secret
2+
3+
我们可以用secret store将secrets注入到其他组件。
4+
5+
`secret_ref` 来配置:
6+
7+
```json
8+
{
9+
"sequencer": {
10+
"redis": {
11+
"type": "redis",
12+
"metadata": {
13+
"redisHost": "127.0.0.1:6380",
14+
"redisPassword": ""
15+
},
16+
"secret_ref": [
17+
{
18+
"name": "local.file",
19+
"key": "redisPassword"
20+
}
21+
]
22+
}
23+
}
24+
}
25+
```
26+
27+
## 快速开始
28+
29+
该示例展示了如何注入redis password到sequencer组件
30+
31+
32+
### Step 0: 运行redis并初试密码
33+
34+
```shell
35+
docker run --name redis -p 6380:6379 -d --restart=always redis:5.0.3 redis-server --appendonly yes --requirepass "redis123"
36+
```
37+
38+
### Step 1: 运行 Layotto
39+
40+
将项目代码下载到本地后,切换代码目录、编译:
41+
42+
```shell
43+
cd ${project_path}/cmd/layotto
44+
```
45+
46+
build:
47+
48+
```shell @if.not.exist layotto
49+
go build -o layotto
50+
```
51+
52+
完成后目录下会生成layotto文件,运行它:
53+
54+
```shell @background
55+
./layotto start -c ../../configs/config_ref_example.json
56+
```
57+
58+
### 第二步:运行客户端程序,调用 Layotto 获取sequence
59+
60+
```shell
61+
cd ${project_path}/demo/sequencer/common/
62+
```
63+
64+
```shell @if.not.exist client
65+
go build -o client
66+
```
67+
68+
```shell
69+
./client -s "redis"
70+
```
71+
72+
打印出如下信息则代表调用成功:
73+
74+
```bash
75+
Try to get next id.Key:key666
76+
Next id:next_id:1
77+
Next id:next_id:2
78+
Next id:next_id:3
79+
Next id:next_id:4
80+
Next id:next_id:5
81+
Next id:next_id:6
82+
Next id:next_id:7
83+
Next id:next_id:8
84+
Next id:next_id:9
85+
Next id:next_id:10
86+
Demo success!
87+
88+
```

etc/script/test-quickstart.sh

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ quickstarts_in_default="docs/en/start/configuration/start.md
3939
docs/zh/start/wasm/start.md
4040
docs/en/start/secret/start.md
4141
docs/zh/start/secret/start.md
42+
docs/en/start/secret/secret_ref.md
43+
docs/zh/start/secret/secret_ref.md
4244
"
4345

4446
# In advance mod, we test these docs with golang 1.17

pkg/runtime/bindings/metadata.go

+3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
package bindings
1818

19+
import "mosn.io/layotto/components/ref"
20+
1921
type Metadata struct {
22+
ref.Config
2023
Type string `json:"type"`
2124
Version string
2225
Metadata map[string]string `json:"metadata"`

pkg/runtime/pubsub/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
package pubsub
1818

19+
import "mosn.io/layotto/components/ref"
20+
1921
// Config wraps configuration for a pubsub implementation
2022
type Config struct {
23+
ref.Config
2124
Type string `json:"type"`
2225
Metadata map[string]string `json:"metadata"`
2326
}

0 commit comments

Comments
 (0)