aboutsummaryrefslogtreecommitdiff
path: root/internal/admin/service_test.go
blob: 9af6bb9a83708bdacf39b6445d89156bd2e265b8 (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
package admin

import (
	"io"
	"log"
	"os"
	"path/filepath"
	"testing"
)

var (
	updatedServerConfig = []byte(`
---
name: "go-git-server"
version: "v1alpha1"
basepath: ./repos
repos:
  - name: mgmt
    permissions:
      - role: admin
        mode: 1
  - name: testmerepo
    git_web_config:
      owner: grumps
      description: >-
        A wrapper to git http-backend providing authentcation and authorization
        inspired by gitolite.
    permissions:
      - role: maintainers
        mode: 1
  - name: thisismynewrepo
    git_web_config:
      owner: grumps
      description: >-
        A wrapper to git http-backend providing authentcation and authorization
        inspired by gitolite.
    permissions:
      - role: maintainers
        mode: 1
`)
)

func tempModelPolicyConfig(t *testing.T, tempDir, tempRepoDir string) (string, string, string) {

	// auth model
	destModelFile := filepath.Join(tempDir, "auth_model.ini")
	srcModelFile := "../../auth_model.ini"
	copyFile(t, srcModelFile, destModelFile)
	// end auth model

	// policy
	destPolicyFile := filepath.Join(tempDir, "testpolicy.csv")
	srcPolicyFile := "../../tests/testpolicy.csv"
	copyFile(t, srcPolicyFile, destPolicyFile)
	// end policy

	// config
	destConfigFile := filepath.Join(tempRepoDir, "gitserver.yaml")
	srcConfigFile := "../../gitserver.yaml"
	copyFile(t, srcConfigFile, destConfigFile)
	// end config
	return destModelFile, destPolicyFile, destConfigFile
}

func copyFile(t *testing.T, srcFilePath, destPath string) {
	srcFile, err := os.Open(srcFilePath)
	if err != nil {
		t.Fatalf("Error opening base config %s", err)
	}
	defer srcFile.Close()

	// dest
	destFile, err := os.OpenFile(destPath, os.O_RDWR|os.O_CREATE, 0755)
	if err != nil {
		t.Fatalf("failed to open destination in git repo %s", err)
	}
	defer destFile.Close()

	// copy
	if _, err := io.Copy(destFile, srcFile); err != nil {
		t.Fatalf("Error copying file %s", err)
	}
}

func TestInitServer(t *testing.T) {
	tempDir := t.TempDir()
	tempRepoDir := t.TempDir()

	destModelFile, destPolicyFile, destConfigFile := tempModelPolicyConfig(t, tempDir, tempRepoDir)

	t.Run("test reload config success", func(t *testing.T) {
		svc, _ := NewService(destModelFile,
			destPolicyFile,
			filepath.Join(tempRepoDir, "gitserver.yaml"),
			tempRepoDir,
			false)
		//nolint:gosec
		err := os.WriteFile(destConfigFile, updatedServerConfig, 0500)
		if err != nil {
			t.Fatal(err)
		}
		// stuff
		svc.Reload()
		// check policy file to make sure it was saved
		expectedPolicies := [][]string{
			{"role:maintainers", "/thisismynewrepo/info/refs", "GET"},
			{"role:maintainers", "/thisismynewrepo/git-upload-pack", "POST"},
			{"role:maintainers", "/thisismynewrepo/git-receive-pack", "POST"},
		}
		for _, policy := range expectedPolicies {
			hasPolicy, err := svc.HasPolicy(policy[0], policy[1], policy[2])
			if err != nil {
				t.Log("error checking policies", err)
				t.Fail()
			}
			if !hasPolicy {
				t.Log("policy not found", policy)
				t.Fail()
			}
		}
	})
	t.Run("test reload config err", func(t *testing.T) {
		svc, _ := NewService(destModelFile,
			destPolicyFile,
			// TODO set abs path
			filepath.Join(tempRepoDir, "gitserver.yaml"),
			tempRepoDir,
			false)
		notAGoodConfig := []byte("this is not valid yaml")
		//nolint:gosec
		err := os.WriteFile(destConfigFile, notAGoodConfig, 0500)
		if err != nil {
			t.Fatal(err)
		}
		// stuff
		svc.Reload()
		// check policy file to make sure it wasn't saved
		expectedPolicies := [][]string{
			{"role:admin", "/mgmt/info/refs", "GET"},
			{"role:admin", "/mgmt/git-upload-pack", "POST"},
			{"role:admin", "/mgmt/git-receive-pack", "POST"},
		}
		for _, policy := range expectedPolicies {
			hasPolicy, err := svc.HasPolicy(policy[0], policy[1], policy[2])
			if err != nil {
				t.Log("error checking policies", err)
				t.Fail()
			}
			if !hasPolicy {
				t.Log("policy not found", policy)
				t.Fail()
			}

		}
	})
	t.Run("test an unitialized server config", func(t *testing.T) {
		tempRepoDir := t.TempDir()
		svc, _ := NewService(destModelFile,
			destPolicyFile,
			// TODO set abs path
			"gitserver.yaml",
			tempRepoDir,
			true)
		if svc.Conf.Name != "default config" {
			log.Fatalf("found %s expected 'default config'", svc.Conf.Name)
			t.Fail()
		}
	})
}