forked from raystack/frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources_repository.go
176 lines (148 loc) · 3.97 KB
/
resources_repository.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package blob
import (
"context"
"fmt"
"io"
"strings"
"sync"
"time"
"github.com/goto/shield/core/namespace"
"github.com/goto/shield/core/resource"
"github.com/ghodss/yaml"
"github.com/goto/salt/log"
"github.com/pkg/errors"
"github.com/robfig/cron/v3"
"gocloud.dev/blob"
)
type ResourceBackends struct {
Backends []ResourceBackend `json:"backends" yaml:"backends"`
}
type ResourceBackend struct {
Name string `json:"name" yaml:"name"`
ResourceTypes []ResourceType `json:"resource_types" yaml:"resource_types"`
}
type ResourceType struct {
Name string `json:"name" yaml:"name"`
Actions map[string][]string `json:"actions" yaml:"actions"`
}
type Resources struct {
Resources []Resource
}
type Resource struct {
Name string
Actions map[string][]string
}
type ResourcesRepository struct {
log log.Logger
mu *sync.Mutex
cron *cron.Cron
Bucket Bucket
cached []resource.YAML
}
func (repo *ResourcesRepository) GetAll(ctx context.Context) ([]resource.YAML, error) {
repo.mu.Lock()
currentCache := repo.cached
repo.mu.Unlock()
if repo.cron != nil {
// cache must have been refreshed automatically, just return
return currentCache, nil
}
err := repo.refresh(ctx)
return repo.cached, err
}
func (repo *ResourcesRepository) GetRelationsForNamespace(ctx context.Context, namespaceID string) (map[string]bool, error) {
resources, err := repo.GetAll(ctx)
if err != nil {
return nil, err
}
relationSet := map[string]bool{}
relationSet["organization"] = true
relationSet["project"] = true
relationSet["team"] = true
for _, resource := range resources {
if resource.Name == namespaceID {
for _, action := range resource.Actions {
for _, relation := range action {
relationSet[fmt.Sprintf("%s_%s", namespaceID, relation)] = true
}
}
break
}
}
if len(relationSet) == 0 {
return nil, fmt.Errorf("resource not found")
}
return relationSet, err
}
func (repo *ResourcesRepository) refresh(ctx context.Context) error {
var resources []resource.YAML
// get all items
it := repo.Bucket.List(&blob.ListOptions{})
for {
obj, err := it.Next(ctx)
if err != nil {
if err == io.EOF {
break
}
return err
}
if obj.IsDir {
continue
}
if !(strings.HasSuffix(obj.Key, ".yaml") || strings.HasSuffix(obj.Key, ".yml")) {
continue
}
fileBytes, err := repo.Bucket.ReadAll(ctx, obj.Key)
if err != nil {
return errors.Wrap(err, "bucket.ReadAll: "+obj.Key)
}
var resourceBackends ResourceBackends
if err := yaml.Unmarshal(fileBytes, &resourceBackends); err != nil {
return errors.Wrap(err, "yaml.Unmarshal: "+obj.Key)
}
if len(resourceBackends.Backends) == 0 {
continue
}
for _, resourceBackend := range resourceBackends.Backends {
for _, resourceType := range resourceBackend.ResourceTypes {
resources = append(resources, resource.YAML{
Name: namespace.CreateID(resourceBackend.Name, resourceType.Name),
Actions: resourceType.Actions,
Backend: resourceBackend.Name,
ResourceType: resourceType.Name,
})
}
}
}
repo.mu.Lock()
repo.cached = resources
repo.mu.Unlock()
repo.log.Debug("resource config cache refreshed", "resource_config_count", len(repo.cached))
return nil
}
func (repo *ResourcesRepository) InitCache(ctx context.Context, refreshDelay time.Duration) error {
repo.cron = cron.New(cron.WithChain(
cron.SkipIfStillRunning(cron.DefaultLogger),
))
if _, err := repo.cron.AddFunc("@every "+refreshDelay.String(), func() {
if err := repo.refresh(ctx); err != nil {
repo.log.Warn("failed to refresh resource config repository", "err", err)
}
}); err != nil {
return err
}
repo.cron.Start()
// do it once right now
return repo.refresh(ctx)
}
func (repo *ResourcesRepository) Close() error {
<-repo.cron.Stop().Done()
return repo.Bucket.Close()
}
func NewResourcesRepository(logger log.Logger, b Bucket) *ResourcesRepository {
return &ResourcesRepository{
log: logger,
Bucket: b,
mu: new(sync.Mutex),
}
}