-
Notifications
You must be signed in to change notification settings - Fork 2
/
role.go
187 lines (158 loc) · 4.46 KB
/
role.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
177
178
179
180
181
182
183
184
185
186
187
package generator
import (
"fmt"
"path/filepath"
"text/template"
"github.com/hashicorp/go-hclog"
"github.com/sev-2/raiden"
"github.com/sev-2/raiden/pkg/logger"
"github.com/sev-2/raiden/pkg/supabase/objects"
"github.com/sev-2/raiden/pkg/utils"
)
var RoleLogger hclog.Logger = logger.HcLog().Named("generator.role")
// ----- Define type, variable and constant -----
type GenerateRoleData struct {
Imports []string
Package string
Name string
DefaultLimitConnection int
ConnectionLimit int
InheritRole bool
IsReplicationRole bool
IsSuperuser bool
CanBypassRls bool
CanCreateDB bool
CanCreateRole bool
CanLogin bool
ValidUntil string
}
const (
RoleDir = "internal/roles"
RoleTemplate = `package {{ .Package }}
{{- if gt (len .Imports) 0 }}
import (
{{- range .Imports}}
{{.}}
{{- end}}
)
{{- end }}
type {{ .Name | ToGoIdentifier }} struct {
raiden.RoleBase
}
func (r *{{ .Name | ToGoIdentifier }}) Name() string {
return "{{ .Name }}"
}
{{- if ne .ConnectionLimit .DefaultLimitConnection }}
func (r *{{ .Name | ToGoIdentifier }}) ConnectionLimit() int {
return {{ .ConnectionLimit }}
}
{{- end }}
{{- if not .InheritRole }}
func (r *{{ .Name | ToGoIdentifier }}) InheritRole() bool {
return {{ .InheritRole }}
}
{{- end }}
{{- if .IsReplicationRole }}
func (r *{{ .Name | ToGoIdentifier }}) IsReplicationRole() bool {
return {{ .IsReplicationRole }}
}
{{- end }}
{{- if .IsSuperuser }}
func (r *{{ .Name | ToGoIdentifier }}) IsSuperuser() bool {
return {{ .IsSuperuser }}
}
{{- end }}
{{- if .CanBypassRls }}
func (r *{{ .Name | ToGoIdentifier }}) CanBypassRls() bool {
return {{ .CanBypassRls }}
}
{{- end }}
{{- if .CanCreateDB }}
func (r *{{ .Name | ToGoIdentifier }}) CanCreateDB() bool {
return {{ .CanCreateDB }}
}
{{- end }}
{{- if .CanCreateRole }}
func (r *{{ .Name | ToGoIdentifier }}) CanCreateRole() bool {
return {{ .CanCreateRole }}
}
{{- end }}
{{- if .CanLogin }}
func (r *{{ .Name | ToGoIdentifier }}) CanLogin() bool {
return {{ .CanLogin }}
}
{{- end }}
{{- if ne .ValidUntil ""}}
func (r *{{ .Name | ToGoIdentifier }}) ValidUntil() *objects.SupabaseTime {
t, err := time.Parse(raiden.DefaultRoleValidUntilLayout, "{{ .ValidUntil }}")
if err != nil {
raiden.Error(err.Error())
return nil
}
return objects.NewSupabaseTime(t)
}
{{- end }}
`
)
func GenerateRoles(basePath string, roles []objects.Role, generateFn GenerateFn) (err error) {
folderPath := filepath.Join(basePath, RoleDir)
RoleLogger.Trace("create roles folder if not exist", folderPath)
if exist := utils.IsFolderExists(folderPath); !exist {
if err := utils.CreateFolder(folderPath); err != nil {
return err
}
}
for _, v := range roles {
if err := GenerateRole(folderPath, v, generateFn); err != nil {
return err
}
}
return nil
}
func GenerateRole(folderPath string, role objects.Role, generateFn GenerateFn) error {
// define binding func
funcMaps := []template.FuncMap{
{"ToGoIdentifier": utils.SnakeCaseToPascalCase},
}
// define file path
filePath := filepath.Join(folderPath, fmt.Sprintf("%s.%s", role.Name, "go"))
// set imports path
var imports []string
raidenPath := fmt.Sprintf("%q", "github.com/sev-2/raiden")
imports = append(imports, raidenPath)
var validUntil string
if role.ValidUntil != nil {
imports = append(
imports,
fmt.Sprintf("%q", "time"),
fmt.Sprintf("%q", "github.com/sev-2/raiden/pkg/supabase/objects"),
)
validUntil = role.ValidUntil.Format(raiden.DefaultRoleValidUntilLayout)
}
// execute the template and write to the file
data := GenerateRoleData{
Package: "roles",
Imports: imports,
Name: role.Name,
ConnectionLimit: role.ConnectionLimit,
DefaultLimitConnection: raiden.DefaultRoleConnectionLimit,
InheritRole: role.InheritRole,
IsReplicationRole: role.IsReplicationRole,
IsSuperuser: role.IsSuperuser,
CanBypassRls: role.CanBypassRLS,
CanCreateDB: role.CanCreateDB,
CanCreateRole: role.CanCreateRole,
CanLogin: role.CanLogin,
ValidUntil: validUntil,
}
// set input
input := GenerateInput{
BindData: data,
Template: RoleTemplate,
TemplateName: "roleTemplate",
OutputPath: filePath,
FuncMap: funcMaps,
}
RoleLogger.Debug("generate role", "path", input.OutputPath)
return generateFn(input, nil)
}