aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/model.go
blob: bf97b0fe36ad7af1bcfc18a8a27f1c5337125c66 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Package admin manage repos
package admin

import (
	"errors"
	"fmt"
	"io"
	"io/fs"
	"log"
	"os"
	"path/filepath"

	"github.com/go-git/go-billy/v5/memfs"
	"github.com/go-git/go-billy/v5/osfs"
	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/storage/filesystem"
	"github.com/go-git/go-git/v5/storage/memory"
	"gopkg.in/ini.v1"
	"sigs.k8s.io/yaml"
)

const (
	// Read mode operations for repo
	Read Action = 0
	// Write mode operations for repo
	Write = 1
	// Admin mode operations for repo
	Admin = 2
	// GitExportMagic magic file name for daemon export
	GitExportMagic = "git-daemon-export-ok"
	// GitWebExportMagic magic filename for web repos
	GitWebExportMagic = "git-web-export-ok"
)

// Action composite type for modes
type Action int

// GitWeb git web configuration
type GitWeb struct {
	Owner       string `json:"owner"`
	Description string `json:"description"`
	Category    string `json:"category"`
	URL         string `json:"url"`
}

// Permission authorization controls
type Permission struct {
	Role string `json:"role"`
	Mode Action `json:"mode"`
}

// GitRepo git repository
type GitRepo struct {
	// Public "git-daemon-export-ok" magic file for git-http-backend
	Public bool `json:"public"`
	// Name game of repository
	Name string `json:"name"`
	// Web config settings
	GitWebConfig *GitWeb `json:"git_web_config"`
	// Permissions for authorization
	Permissions []*Permission `json:"permissions"`
}

// ServerRepos repos that are part of this server instance
type ServerRepos struct {
	Name     string     `json:"name"`
	Version  string     `json:"version"`
	Repos    []*GitRepo `json:"repos"`
	basePath string
}

func loadFromGit(gitURL, filePath string) ([]byte, error) {
	fs := memfs.New()
	storer := memory.NewStorage()
	_, err := git.Clone(storer, fs, &git.CloneOptions{
		URL: gitURL,
	})
	if err != nil {
		// log.error
		fmt.Printf("coudln't clone mgmt repo %s", err)
		return []byte(""), errors.New("coudln't clone mgmt repo")
	}
	file, err := fs.Open(filePath)
	if err != nil {
		fmt.Printf("Failed to open gitserver config %s", err)
		return []byte(""), errors.New("coudln't open git config file from mgmt repo")
	}
	defer file.Close()
	return io.ReadAll(file)
}

func loadLocalFile(path string) ([]byte, error) {
	file, err := os.Open(path)
	if err != nil {
		log.Printf("config file not opened %s", path)
		return []byte{}, err
	}
	defer file.Close()
	configBytes, err := io.ReadAll(file)
	if err != nil {
		log.Print("config file not read")
		return []byte{}, err
	}
	return configBytes, nil
}

// loadServerConfig configPath should be the absolutepath to the configmap if it's not in a repo
func loadServerConfig(mgmtRepo bool, baseDir, configPath string) (*ServerRepos, error) {
	var (
		configBytes []byte
		err         error
	)
	if mgmtRepo {
		repoURI := filepath.Join("file:///", baseDir, "mgmt.git")
		configBytes, err = loadFromGit(repoURI, configPath)
		if err != nil {
			// log.error
			log.Print("Failed to load config file from git")
			return &ServerRepos{}, err
		}
	} else {
		configBytes, err = loadLocalFile(configPath)
		if err != nil {
			// log.error
			log.Print("Failed to load config file from file system")
			return &ServerRepos{}, err
		}
	}
	config := &ServerRepos{}
	err = yaml.Unmarshal(configBytes, &config)
	if err != nil {
		return &ServerRepos{}, errors.New("could not parse gitserver config")
	}
	return config, nil
}

// ServerPolicies generate casbin policies
func (s *ServerRepos) ServerPolicies() [][]string {
	policies := [][]string{}
	for _, repo := range s.Repos {
		policies = append(policies, repo.CasbinPolicies()...)
	}
	return policies
}

// ConfigureRepos run reconciler for all repos
func (s *ServerRepos) ConfigureRepos() {
	for _, repo := range s.Repos {
		repo.ReconcileRepo(s.basePath)
	}
}

func readOnlyPaths(role, repoName string) [][]string {
	return [][]string{
		[]string{role, fmt.Sprintf("/%s/info/refs", repoName), "GET"},
		[]string{role, fmt.Sprintf("/%s/git-upload-pack", repoName), "POST"},
	}
}
func writePaths(role, repoName string) [][]string {
	return [][]string{[]string{role, fmt.Sprintf("/%s/git-receive-pack", repoName), "POST"}}
}

// Policy generate policy for repo base on mode
func (p *Permission) Policy(repoName string) [][]string {
	policies := [][]string{}
	// if read mode or greater e.g. write mode
	roleName := fmt.Sprintf("role:%s", p.Role)
	if p.Mode >= Read {
		policies = append(policies, readOnlyPaths(roleName, repoName)...)
	}
	// if write mode
	if p.Mode >= Write {
		policies = append(policies, writePaths(roleName, repoName)...)
	}
	return policies
}

// CasbinPolicies generate all policies
func (r *GitRepo) CasbinPolicies() [][]string {
	policies := [][]string{}
	for _, perm := range r.Permissions {
		policies = append(policies, perm.Policy(r.Name)...)
	}
	return policies
}

// ReconcileRepo update repo export settings, update web config
func (r *GitRepo) ReconcileRepo(basePath string) {
	// if exist -> continue
	repoBase := filepath.Join(basePath, fmt.Sprintf("%s.git", r.Name))
	_, err := os.Stat(repoBase)
	if errors.Is(err, fs.ErrNotExist) {
		// if no exist -> init bare
		repoFs := osfs.New(repoBase)
		strg := filesystem.NewStorage(repoFs, nil)
		_, _ = git.Init(strg, nil)
	}
	// set export file for git-http-backend
	okExport := filepath.Join(repoBase, GitExportMagic)
	_, err = os.Stat(okExport)
	if errors.Is(err, fs.ErrNotExist) {
		// Create web export
		f, err := os.Create(okExport)
		f.Close()
		if err != nil {
			log.Fatalf("%s coudln't be created %s", GitExportMagic, err)
		}
	}
	r.ConfigureExport(repoBase)
	if r.GitWebConfig == nil {
		r.GitWebConfig = &GitWeb{}
	}
	r.GitWebConfig.ReconcileGitConf(repoBase)
}

// ConfigureExport setup repo for sharing and configure web settings
func (r *GitRepo) ConfigureExport(repoBase string) {
	okExport := filepath.Join(repoBase, GitWebExportMagic)
	_, err := os.Create(okExport)
	if err != nil {
		log.Fatalf("%s coudln't be created %s", GitWebExportMagic, err)
	}
}

// ReconcileGitConf reconcile gitweb configuration section of gitconfig
func (r *GitWeb) ReconcileGitConf(repoBase string) {
	confPath := filepath.Join(repoBase, "config")
	cfg, err := ini.Load(confPath)
	if err != nil {
		log.Fatalf("Coudln't read gitconfig %s", err)
	}
	// check if empty, delete
	if (GitWeb{} == *r) {
		if cfg.HasSection("gitweb") {
			cfg.DeleteSection("gitweb")
			if err := cfg.SaveTo(confPath); err != nil {
				log.Fatalf("Coudln't save gitconfig %s", err)
			}
		}
		return
	}
	section := cfg.Section("gitweb")
	section.Key("description").SetValue(r.Description)
	section.Key("owner").SetValue(r.Owner)
	section.Key("url").SetValue(r.URL)
	section.Key("category").SetValue(r.Category)
	if err := cfg.SaveTo(confPath); err != nil {
		log.Fatalf("Coudln't save gitconfig %s", err)
	}
}