forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_config.go
148 lines (118 loc) · 3.63 KB
/
schema_config.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
package blob
import (
"context"
"fmt"
"io"
"strings"
"github.com/goto/shield/internal/schema"
"github.com/pkg/errors"
"gocloud.dev/blob"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"
)
type RoleConfig struct {
Name string `yaml:"name" json:"name"`
Principals []string `yaml:"principals" json:"principals"`
}
type PermissionsConfig struct {
Name string `yaml:"name" json:"name"`
Roles []string `yaml:"roles" json:"roles"`
}
type ResourceTypeConfig struct {
Name string `yaml:"name" json:"name"`
Roles []RoleConfig `yaml:"roles" json:"roles"`
Permissions []PermissionsConfig `yaml:"permissions" json:"permissions"`
}
type Config struct {
Type string `yaml:"type" json:"type"`
ResourceTypes []ResourceTypeConfig `yaml:"resource_types" json:"resource_types,omitempty"`
Roles []RoleConfig `yaml:"roles" json:"roles,omitempty"`
Permissions []PermissionsConfig `yaml:"permissions" json:"permissions,omitempty"`
}
type SchemaConfig struct {
bucket Bucket
config schema.NamespaceConfigMapType
}
func NewSchemaConfigRepository(b Bucket) *SchemaConfig {
return &SchemaConfig{bucket: b}
}
func (s *SchemaConfig) GetSchema(ctx context.Context) (schema.NamespaceConfigMapType, error) {
configMap := make(schema.NamespaceConfigMapType)
if s.config != nil {
return s.config, nil
}
configFromFiles, err := s.readYAMLFiles(ctx)
if err != nil {
return nil, err
}
for _, c := range configFromFiles {
for k, v := range c {
if v.Type == "resource_group" {
configMap = schema.MergeNamespaceConfigMap(configMap, getNamespacesForResourceGroup(k, v))
} else {
configMap = schema.MergeNamespaceConfigMap(getNamespaceFromConfig(k, v.Roles, v.Permissions), configMap)
}
}
}
s.config = configMap
return configMap, nil
}
func (s *SchemaConfig) readYAMLFiles(ctx context.Context) ([]map[string]Config, error) {
configYAMLs := make([]map[string]Config, 0)
// iterate over bucket files, only read .yml & .yaml files
it := s.bucket.List(&blob.ListOptions{})
for {
obj, err := it.Next(ctx)
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
if obj.IsDir {
continue
}
if !(strings.HasSuffix(obj.Key, ".yaml") || strings.HasSuffix(obj.Key, ".yml")) {
continue
}
fileBytes, err := s.bucket.ReadAll(ctx, obj.Key)
if err != nil {
return nil, fmt.Errorf("%s: %s", "error in reading bucket object", err.Error())
}
var configYAML map[string]Config
if err := yaml.Unmarshal(fileBytes, &configYAML); err != nil {
return nil, errors.Wrap(err, "yaml.Unmarshal: "+obj.Key)
}
if len(configYAML) == 0 {
continue
}
configYAMLs = append(configYAMLs, configYAML)
}
return configYAMLs, nil
}
func getNamespacesForResourceGroup(name string, c Config) schema.NamespaceConfigMapType {
namespaceConfig := schema.NamespaceConfigMapType{}
for _, v := range c.ResourceTypes {
maps.Copy(namespaceConfig, getNamespaceFromConfig(name, v.Roles, v.Permissions, v.Name))
}
return namespaceConfig
}
func getNamespaceFromConfig(name string, rolesConfigs []RoleConfig, permissionConfigs []PermissionsConfig, resourceType ...string) schema.NamespaceConfigMapType {
tnc := schema.NamespaceConfig{
Roles: make(map[string][]string),
Permissions: make(map[string][]string),
}
for _, v1 := range rolesConfigs {
tnc.Roles[v1.Name] = v1.Principals
}
for _, v2 := range permissionConfigs {
tnc.Permissions[v2.Name] = v2.Roles
}
if len(resourceType) == 0 {
tnc.Type = schema.SystemNamespace
} else {
tnc.Type = schema.ResourceGroupNamespace
name = fmt.Sprintf("%s/%s", name, resourceType[0])
}
return schema.NamespaceConfigMapType{name: tnc}
}