Skip to content

Commit 210bbbb

Browse files
committed
Commit on '2024-05-22 17:09:08'
1 parent 858c00b commit 210bbbb

File tree

27 files changed

+724
-470
lines changed

27 files changed

+724
-470
lines changed

cmd/main.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"errors"
77
"gitbot/internal/config"
88
"gitbot/internal/event"
9-
109
"gitbot/internal/event/provider"
10+
"gitbot/internal/event/queue"
11+
"gitbot/internal/event/worker"
1112

1213
"log/slog"
1314
"net/http"
@@ -46,23 +47,32 @@ func NewKubernetes() *kubernetes.Clientset {
4647
return clientset
4748
}
4849

50+
type Server struct {
51+
}
52+
4953
func main() {
5054
// Configure log
5155
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
5256

57+
// Load config
58+
//config := config.Load(config.Get("CONFIG_FILE"))
59+
5360
// Configure kubernetes
54-
clientset := NewKubernetes()
61+
//clientset := NewKubernetes()
5562

5663
// Configure webhook queue
57-
queue := event.NewMemoryQueue()
64+
queue := queue.NewMemoryQueue[event.QueueItem]()
65+
worker := worker.NewWorker(queue)
5866

59-
// New Router for Http Server
67+
// Handlers
68+
bitbucketProvider := provider.NewBitbucketProvider(config.Get("BITBUCKET_BEARER_TOKEN"))
69+
bitbucketHandler := event.NewHandler(queue, bitbucketProvider)
70+
71+
// Routes
6072
router := http.NewServeMux()
61-
router.HandleFunc("GET /status", status)
6273

63-
// Setup Git Providers Routes
64-
bitbucket := provider.NewBitbucketProvider(config.Get("BITBUCKET_BEARER_TOKEN"))
65-
router.HandleFunc("POST /api/v1/webhook/bitbucket", event.Handle(queue, bitbucket))
74+
router.HandleFunc("GET /status", status)
75+
router.HandleFunc("POST /api/v1/webhook/bitbucket", bitbucketHandler.Handle())
6676

6777
// Starting Http Server
6878
srv := &http.Server{Addr: ":" + config.Get("HTTP_PORT"), Handler: router}
@@ -77,7 +87,7 @@ func main() {
7787
}()
7888

7989
// Start Event Queue Worker
80-
StopWorker := event.StartWorker(queue, clientset)
90+
go worker.Start()
8191

8292
// Wait for shutdown signal
8393
done := make(chan os.Signal, 1)
@@ -96,7 +106,7 @@ func main() {
96106

97107
// Stop event queue worker
98108
slog.Info("Shutdown event queue...")
99-
StopWorker(ctx)
109+
worker.Stop(ctx)
100110

101111
slog.Info("Server Stopped...")
102112
time.Sleep(3 * time.Second)

config.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
clusters:
3+
- name: prod
4+
inside: https://F014FC5DAE0A9C41F698C9375CC5DB7D.gr7.eu-south-2.eks.amazonaws.com
5+
auth_type: serviceaccount
6+
- name: demo
7+
inside: https://F014FC5DAE0A9C41F698C9375CC5DB7D.gr7.eu-south-2.eks.amazonaws.com
8+
auth_type: aws
9+
aws_role: arn:aws:iam::<account_id>:role/<role_name>
10+
security:
11+
groups:
12+
- name: testing
13+
users:
14+
15+
rules:
16+
- repository: https://bitbucket.org/firmapro/kubernetes.git
17+
filepattern: ["overlays/test/**", ""]
18+
action: ["lock", "unlock"]
19+
group: ["testing"]
20+
user:
21+

internal/config/config.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"os"
55

66
"github.com/joho/godotenv"
7+
//"gopkg.in/yaml.v3"
78
)
89

910
func init() {
@@ -13,6 +14,40 @@ func init() {
1314
}
1415
}
1516

17+
//func Get(key string, def ...string) string {
18+
// return os.Getenv(key)
19+
//}
20+
1621
func Get(key string) string {
1722
return os.Getenv(key)
1823
}
24+
25+
// func Load(filepath string) *Config {
26+
// //err := godotenv.Load("env.ini")
27+
// //if err != nil {
28+
// // panic("Error loading .env file")
29+
// //}
30+
//
31+
// data, err := os.ReadFile(filepath)
32+
// if err != nil {
33+
// panic(err)
34+
// }
35+
//
36+
// var configfile iYamlConfigFile
37+
// err = yaml.Unmarshal(data, &configfile)
38+
// if err != nil {
39+
// panic(err)
40+
// }
41+
//
42+
// // validate
43+
// if err := configfile.validate(); err != nil {
44+
// panic(err)
45+
// }
46+
//
47+
// // Get security rules from configfile
48+
// sg := configfile.seRules()
49+
//
50+
// return &Config{
51+
// SecurityRules: sg,
52+
// }
53+
// }

internal/config/file/manager.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package config
2+
3+
import (
4+
"os"
5+
6+
"gopkg.in/yaml.v3"
7+
)
8+
9+
type ConfigManager struct{}
10+
11+
func Load(filepath string) *Config {
12+
//err := godotenv.Load("env.ini")
13+
//if err != nil {
14+
// panic("Error loading .env file")
15+
//}
16+
17+
data, err := os.ReadFile(filepath)
18+
if err != nil {
19+
panic(err)
20+
}
21+
22+
var configfile iYamlConfigFile
23+
err = yaml.Unmarshal(data, &configfile)
24+
if err != nil {
25+
panic(err)
26+
}
27+
28+
// validate
29+
if err := configfile.validate(); err != nil {
30+
panic(err)
31+
}
32+
33+
// Get security rules from configfile
34+
sg := configfile.seRules()
35+
36+
return &Config{
37+
SecurityRules: sg,
38+
}
39+
}

internal/config/file/mapper.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package config
2+
3+
func (c iYamlConfigFile) seRules() []SecurityRule {
4+
var result []SecurityRule
5+
6+
for _, r := range c.Security.Rules {
7+
// Add users from group to userlist
8+
userList := r.UserList
9+
for _, group := range r.GroupList {
10+
for _, g := range c.Security.Groups {
11+
if group != g.Name {
12+
continue
13+
}
14+
userList = append(userList, g.Users...)
15+
}
16+
}
17+
18+
// create final securityrule
19+
result = append(result, SecurityRule{
20+
Repository: r.Respository,
21+
FilePatternList: r.FilePatternList,
22+
ActionList: r.ActionList,
23+
UserList: userList,
24+
})
25+
}
26+
27+
return result
28+
}

internal/config/file/types.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package config
2+
3+
type iYamlConfigFile struct {
4+
Security struct {
5+
Groups []struct {
6+
Name string `yaml:"name"`
7+
Users []string `yaml:"users"`
8+
} `yaml:"groups"`
9+
Rules []struct {
10+
Respository string `yaml:"repository"`
11+
FilePatternList []string `yaml:"filepattern"`
12+
ActionList []string `yaml:"action"`
13+
GroupList []string `yaml:"group"`
14+
UserList []string `yaml:"user"`
15+
} `yaml:"rules"`
16+
} `yaml:"security"`
17+
}

internal/config/file/validate.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package config
2+
3+
import "fmt"
4+
5+
func (c iYamlConfigFile) validate() error {
6+
7+
// Group has any user
8+
if err := c.hasEmptyGroup(); err != nil {
9+
return err
10+
}
11+
12+
// Group in rule exists
13+
if err := c.hasRuleGroupThatExists(); err != nil {
14+
return err
15+
}
16+
17+
// Check Actions lock, unlock
18+
if err := c.hasRuleCorrectActions(); err != nil {
19+
return err
20+
}
21+
22+
if err := c.hasRuleFieldEmpty(); err != nil {
23+
return err
24+
}
25+
26+
return nil
27+
}
28+
29+
func (c iYamlConfigFile) hasEmptyGroup() error {
30+
for _, g := range c.Security.Groups {
31+
if len(g.Users) == 0 {
32+
return fmt.Errorf("Group '%s' is empty", g.Name)
33+
}
34+
}
35+
36+
return nil
37+
}
38+
39+
func (c iYamlConfigFile) hasRuleGroupThatExists() error {
40+
for _, rule := range c.Security.Rules {
41+
for _, group := range rule.GroupList {
42+
exists := false
43+
for _, g := range c.Security.Groups {
44+
if g.Name == group {
45+
exists = true
46+
}
47+
}
48+
if !exists {
49+
return fmt.Errorf("The group '%s' do not exists in config", group)
50+
}
51+
}
52+
}
53+
return nil
54+
}
55+
56+
func (c iYamlConfigFile) hasRuleCorrectActions() error {
57+
for _, rule := range c.Security.Rules {
58+
for _, action := range rule.ActionList {
59+
if !(action == "lock" || action == "unlock") {
60+
return fmt.Errorf("The action '%s' is not valid", action)
61+
}
62+
}
63+
}
64+
return nil
65+
}
66+
67+
func (c iYamlConfigFile) hasRuleFieldEmpty() error {
68+
for _, rule := range c.Security.Rules {
69+
if len(rule.ActionList) == 0 {
70+
return fmt.Errorf("The field action is empty")
71+
}
72+
if len(rule.FilePatternList) == 0 {
73+
return fmt.Errorf("The field filepattern is empty")
74+
}
75+
if len(rule.UserList)+len(rule.GroupList) == 0 {
76+
return fmt.Errorf("The rule dont have any user associate")
77+
}
78+
}
79+
return nil
80+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package config
2+
3+
import "testing"
4+
5+
func TestConfigFile(t *testing.T) {
6+
Load("../../config.yaml")
7+
}

0 commit comments

Comments
 (0)