Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: component ref #761

Merged
merged 61 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from 55 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
2f4557e
add secret ref key
ZLBer Jul 16, 2022
2ad136e
add secret ref key
ZLBer Jul 16, 2022
59f378c
add secret ref key
ZLBer Jul 16, 2022
82f45d8
Merge branch 'main' into secret_ref_key
seeflood Jul 18, 2022
25a6b1d
need sub_key
ZLBer Jul 18, 2022
8608eaf
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Jul 18, 2022
223758b
Merge branch 'main' into secret_ref_key
seeflood Jul 19, 2022
a7e8d81
rename field
ZLBer Jul 19, 2022
7f435af
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Jul 19, 2022
b4f3955
fix quickstart
ZLBer Jul 19, 2022
3b81ec3
Merge branch 'main' into secret_ref_key
seeflood Jul 19, 2022
72ad844
fix quickstart
ZLBer Jul 20, 2022
673ee45
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Jul 20, 2022
47cfc26
add component ref
ZLBer Aug 11, 2022
7393e3c
Merge branch 'main' into secret_ref_key
ZLBer Aug 11, 2022
5fe66ed
Merge branch 'main' into secret_ref_key
seeflood Aug 13, 2022
c33d8c9
Merge branch 'main' into secret_ref_key
seeflood Aug 19, 2022
8ca9ad6
add component ref
ZLBer Aug 22, 2022
3e31caf
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Aug 22, 2022
27145ef
add component ref
ZLBer Aug 23, 2022
ca59f1f
Merge branch 'main' into secret_ref_key
seeflood Aug 27, 2022
8c3015e
Merge branch 'main' into secret_ref_key
seeflood Aug 30, 2022
d72a348
Merge branch 'main' into secret_ref_key
ZLBer Sep 3, 2022
feececd
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Sep 3, 2022
75fce6a
add component ref
ZLBer Sep 3, 2022
69a7135
fix golint
ZLBer Sep 3, 2022
1606f07
fix golint
ZLBer Sep 3, 2022
207f374
fix golint
ZLBer Sep 3, 2022
93f0933
fix golint
ZLBer Sep 3, 2022
cb878f2
fix component ref
ZLBer Sep 7, 2022
156e5b7
fix component ref
ZLBer Sep 7, 2022
9259c2e
fix component ref
ZLBer Sep 7, 2022
cfcae53
Merge branch 'main' into secret_ref_key
seeflood Sep 8, 2022
856f6de
fix config
ZLBer Sep 8, 2022
8fc2b4f
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Sep 8, 2022
932dba2
fix ut
ZLBer Sep 8, 2022
24d2b96
fix ut
ZLBer Sep 8, 2022
5f9005c
fix ut
ZLBer Sep 8, 2022
6118a08
check ref config nil
ZLBer Sep 8, 2022
c909383
Merge branch 'main' into secret_ref_key
Xunzhuo Sep 14, 2022
f25c577
fix ci
ZLBer Sep 15, 2022
28c0c1a
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Sep 15, 2022
58fd181
fix ci
ZLBer Sep 15, 2022
0cf9b56
rename config
ZLBer Sep 18, 2022
3f51d96
clean code
ZLBer Sep 18, 2022
2d072f2
add more component inject
ZLBer Sep 18, 2022
889fc91
fix ci
ZLBer Sep 18, 2022
66a2b7f
Merge branch 'main' into secret_ref_key
seeflood Sep 18, 2022
33d9aa8
fix ut
ZLBer Sep 20, 2022
4db6cf9
Merge branch 'main' into secret_ref_key
seeflood Sep 23, 2022
0e929d2
Merge branch 'main' into secret_ref_key
seeflood Sep 26, 2022
743aa65
Merge branch 'main' into secret_ref_key
seeflood Sep 27, 2022
15d8560
rename
ZLBer Sep 28, 2022
2ac913d
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Sep 28, 2022
191e2d0
Merge branch 'main' into secret_ref_key
seeflood Sep 29, 2022
3fa775c
clean code
ZLBer Sep 29, 2022
c54e548
Merge remote-tracking branch 'origin/secret_ref_key' into secret_ref_key
ZLBer Sep 29, 2022
7fe729d
clean code
ZLBer Sep 29, 2022
06b5a57
clean code
ZLBer Sep 29, 2022
306f770
fix review
ZLBer Oct 1, 2022
2324e92
Merge branch 'main' into secret_ref_key
seeflood Oct 8, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/file/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package file
import (
"encoding/json"
"io"

"mosn.io/layotto/components/ref"
)

// FileConfig wraps configuration for a file implementation
type FileConfig struct {
ref.Config
Metadata json.RawMessage `json:"metadata"`
Type string `json:"type"`
}
Expand Down
9 changes: 2 additions & 7 deletions components/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,17 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.12.4
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.11.14
github.com/aws/aws-sdk-go-v2/service/s3 v1.26.10
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
github.com/dapr/components-contrib v1.5.1-rc.1
ZLBer marked this conversation as resolved.
Show resolved Hide resolved
github.com/go-redis/redis/v8 v8.8.0
github.com/go-zookeeper/zk v1.0.2
github.com/golang/mock v1.6.0
github.com/google/uuid v1.3.0
github.com/hashicorp/consul/api v1.3.0
github.com/jarcoal/httpmock v1.2.0
github.com/jinzhu/copier v0.3.6-0.20220506024824-3e39b055319a
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/minio/minio-go/v7 v7.0.15
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pkg/errors v0.9.1
github.com/qiniu/go-sdk/v7 v7.11.1
github.com/spf13/afero v1.2.2 // indirect
github.com/stretchr/testify v1.7.0
github.com/tencentyun/cos-go-sdk-v5 v0.7.33
github.com/valyala/fasthttp v1.26.0
Expand All @@ -36,9 +33,7 @@ require (
go.etcd.io/etcd/client/v3 v3.5.0
go.etcd.io/etcd/server/v3 v3.5.0
go.mongodb.org/mongo-driver v1.8.0
go.uber.org/atomic v1.7.0
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5 // indirect
go.uber.org/atomic v1.8.0
google.golang.org/grpc v1.38.0
mosn.io/api v1.1.0
mosn.io/mosn v1.1.0
Expand Down
565 changes: 548 additions & 17 deletions components/go.sum

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion components/hello/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@

package hello

import "context"
import (
"context"

"mosn.io/layotto/components/ref"
)

const ServiceName = "hello"

Expand All @@ -26,6 +30,7 @@ type HelloService interface {
}

type HelloConfig struct {
ref.Config
Type string `json:"type"`
HelloString string `json:"hello"`
}
Expand Down
18 changes: 17 additions & 1 deletion components/hello/helloworld/helloworld.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ import (
"context"
"sync/atomic"

"github.com/dapr/components-contrib/secretstores"

"mosn.io/layotto/components/configstores"
"mosn.io/layotto/components/hello"
)

type HelloWorld struct {
Say atomic.Value
Say atomic.Value
config configstores.Store
secretStore secretstores.SecretStore
}

func (hw *HelloWorld) ApplyConfig(ctx context.Context, metadata map[string]string) (err error) {
Expand All @@ -36,6 +41,17 @@ func (hw *HelloWorld) ApplyConfig(ctx context.Context, metadata map[string]strin
return nil
}

func (hw *HelloWorld) SetConfigStore(cs configstores.Store) (err error) {
//save for use
hw.config = cs
return nil
}
func (hw *HelloWorld) SetSecretStore(ss secretstores.SecretStore) (err error) {
//save for use
hw.secretStore = ss
return nil
}

var _ hello.HelloService = &HelloWorld{}

func NewHelloWorld() hello.HelloService {
Expand Down
8 changes: 8 additions & 0 deletions components/hello/helloworld/helloworld_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"

"mosn.io/layotto/components/pkg/common"

"mosn.io/layotto/components/hello"
Expand Down Expand Up @@ -59,4 +61,10 @@ func TestHelloWorld(t *testing.T) {
if resp.HelloString != "Bye, Layotto" {
t.Fatalf("hello output failed")
}

component := hs.(common.SetComponent)
err = component.SetConfigStore(nil)
assert.Nil(t, err)
err = component.SetSecretStore(nil)
assert.Nil(t, err)
}
3 changes: 3 additions & 0 deletions components/oss/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package oss
import (
"encoding/json"
"errors"

"mosn.io/layotto/components/ref"
)

const (
Expand All @@ -31,6 +33,7 @@ var (

// Config wraps configuration for a oss implementation
type Config struct {
ref.Config
Metadata map[string]json.RawMessage `json:"metadata"`
Type string `json:"type"`
}
28 changes: 28 additions & 0 deletions components/pkg/common/inject_component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2021 Layotto Authors
*
* 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 (
"mosn.io/layotto/components/configstores"

"github.com/dapr/components-contrib/secretstores"
)

type SetComponent interface {
SetConfigStore(cs configstores.Store) (err error)
SetSecretStore(ss secretstores.SecretStore) (err error)
}
10 changes: 8 additions & 2 deletions components/ref/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package ref

// Config is ref json config
type Config struct {
SecretRef []*Item `json:"secret_ref"`
SecretRef []*SecretRefConfig `json:"secret_ref"`
ComponentRef *ComponentRefConfig `json:"component_ref"`
}

type Item struct {
type SecretRefConfig struct {
//secret component name, such as : local.file
StoreName string `json:"store_name"`
// key in the secret component
Expand All @@ -31,3 +32,8 @@ type Item struct {
// key need to inject into metadata
InjectAs string `json:"inject_as"`
}

type ComponentRefConfig struct {
SecretStore string `json:"secret_store"`
ConfigStore string `json:"config_store"`
}
23 changes: 22 additions & 1 deletion configs/config_ref_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,28 @@
"hellos": {
"helloworld": {
"type": "helloworld",
"hello": "greeting"
"hello": "greeting",
"secret_ref": [
{
"store_name": "local.file",
"key": "db-user-pass:password",
"sub_key": "db-user-pass:password",
"inject_as": "redisPassword"
}
],
"component_ref": {
"config_store": "config_demo",
"secret_store": "local.file"
}
}
},
"config_store": {
"config_demo": {
"type": "etcd",
"address": [
"127.0.0.1:2379"
],
"timeout": "10"
}
},
"secret_store": {
Expand Down
5 changes: 3 additions & 2 deletions pkg/runtime/ref/contianer.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ type RefContainer struct {
ConfigRef map[string]configstores.Store
}

// NewRefContainer return a new container
func NewRefContainer() *RefContainer {
return &RefContainer{
SecretRef: make(map[string]secretstores.SecretStore),
ConfigRef: make(map[string]configstores.Store),
}
}

func (r *RefContainer) GetSecretStore(key string) secretstores.SecretStore {
func (r *RefContainer) getSecretStore(key string) secretstores.SecretStore {
return r.SecretRef[key]
}

func (r *RefContainer) GetConfigStore(key string) configstores.Store {
func (r *RefContainer) getConfigStore(key string) configstores.Store {
return r.ConfigRef[key]
}
4 changes: 2 additions & 2 deletions pkg/runtime/ref/contianer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRefContainer(t *testing.T) {
container.SecretRef["fake"] = ss
cf := &mock.MockStore{}
container.ConfigRef["mock"] = cf
assert.Equal(t, ss, container.GetSecretStore("fake"))
assert.Equal(t, cf, container.GetConfigStore("mock"))
assert.Equal(t, ss, container.getSecretStore("fake"))
assert.Equal(t, cf, container.getConfigStore("mock"))

}
44 changes: 41 additions & 3 deletions pkg/runtime/ref/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,34 @@
package ref

import (
"fmt"

"github.com/dapr/components-contrib/secretstores"

"mosn.io/layotto/components/configstores"
"mosn.io/layotto/components/ref"
)

type DefaultInjector struct {
Container RefContainer
//limit ref component
ComponentLimit map[string]struct{}
ZLBer marked this conversation as resolved.
Show resolved Hide resolved
Container RefContainer
}

// NewDefaultInjector return a single Inject
func NewDefaultInjector(secretStores map[string]secretstores.SecretStore, configStores map[string]configstores.Store) *DefaultInjector {
injector := &DefaultInjector{
Container: RefContainer{
SecretRef: secretStores,
ConfigRef: configStores,
},
}
return injector
}

// InjectSecretRef inject secret to metaData
// TODO: permission control
func (i *DefaultInjector) InjectSecretRef(items []*ref.Item, metaData map[string]string) (map[string]string, error) {
func (i *DefaultInjector) InjectSecretRef(items []*ref.SecretRefConfig, metaData map[string]string) (map[string]string, error) {
if metaData == nil {
metaData = make(map[string]string)
}
Expand All @@ -38,7 +54,7 @@ func (i *DefaultInjector) InjectSecretRef(items []*ref.Item, metaData map[string

meta := make(map[string]string)
for _, item := range items {
store := i.Container.GetSecretStore(item.StoreName)
store := i.Container.getSecretStore(item.StoreName)
secret, err := store.GetSecret(secretstores.GetSecretRequest{
Name: item.Key,
})
Expand All @@ -62,3 +78,25 @@ func (i *DefaultInjector) InjectSecretRef(items []*ref.Item, metaData map[string
}
return metaData, nil
}

func (i *DefaultInjector) InjectConfigStoreRef(cf *ref.ComponentRefConfig) (configstores.Store, error) {
ZLBer marked this conversation as resolved.
Show resolved Hide resolved
if cf == nil || cf.ConfigStore == "" {
return nil, nil
}
configStore := i.Container.getConfigStore(cf.ConfigStore)
if configStore == nil {
return nil, fmt.Errorf("fail to get configStore:%v", cf.ConfigStore)
}
return configStore, nil
}

func (i *DefaultInjector) InjectSecretStoreRef(cf *ref.ComponentRefConfig) (secretstores.SecretStore, error) {
ZLBer marked this conversation as resolved.
Show resolved Hide resolved
if cf == nil || cf.SecretStore == "" {
return nil, nil
}
secretStore := i.Container.getSecretStore(cf.SecretStore)
if secretStore == nil {
return nil, fmt.Errorf("fail to get secretStore:%v", cf.SecretStore)
}
return secretStore, nil
}
28 changes: 24 additions & 4 deletions pkg/runtime/ref/injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,49 @@ func TestInject(t *testing.T) {
cf := &mock.MockStore{}
container.ConfigRef["mock_config_store"] = cf

injector := DefaultInjector{Container: *container}
injector := NewDefaultInjector(container.SecretRef, container.ConfigRef)
meta := make(map[string]string)

var items []*ref.Item
var items []*ref.SecretRefConfig
secretRef, err := injector.InjectSecretRef(nil, meta)
assert.Nil(t, err)
assert.Equal(t, len(secretRef), 0)
secretRef, err = injector.InjectSecretRef(items, nil)
assert.Nil(t, err)
assert.Equal(t, len(secretRef), 0)
items = append(items, &ref.Item{
items = append(items, &ref.SecretRefConfig{
StoreName: "fake_secret_store",
Key: "good-key",
SubKey: "good-key",
InjectAs: "ref-key",
})
items = append(items, &ref.Item{
items = append(items, &ref.SecretRefConfig{
StoreName: "fake_secret_store",
Key: "good-key",
SubKey: "good-key",
})
injector.InjectSecretRef(items, meta)
assert.Equal(t, meta["ref-key"], "life is good")
assert.Equal(t, meta["good-key"], "life is good")
secretStoreRef, err := injector.InjectSecretStoreRef(&ref.ComponentRefConfig{
SecretStore: "fake_secret_store",
})
assert.Nil(t, err)
assert.Equal(t, secretStoreRef, ss)

_, err = injector.InjectSecretStoreRef(&ref.ComponentRefConfig{
SecretStore: "null",
})
assert.NotNil(t, err)

configStoreRef, err := injector.InjectConfigStoreRef(&ref.ComponentRefConfig{
ConfigStore: "mock_config_store",
})
assert.Nil(t, err)
assert.Equal(t, configStoreRef, cf)

_, err = injector.InjectConfigStoreRef(&ref.ComponentRefConfig{
ConfigStore: "null",
})
assert.NotNil(t, err)
}
Loading