Welcome to mirror list, hosted at ThFree Co, Russian Federation.

domain.go « domain « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9678f5fca53182c0a478e80dbb3ed73140b6599a (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
package domain

import (
	"crypto/tls"
	"errors"
	"net/http"
	"sync"
	"time"

	"gitlab.com/gitlab-org/gitlab-pages/internal/httperrors"
	"gitlab.com/gitlab-org/gitlab-pages/internal/serving"
)

// GroupConfig represents a per-request config for a group domain
type GroupConfig interface {
	IsHTTPSOnly(*http.Request) bool
	HasAccessControl(*http.Request) bool
	IsNamespaceProject(*http.Request) bool
	ProjectID(*http.Request) uint64
	ProjectExists(*http.Request) bool
}

// Domain is a domain that gitlab-pages can serve.
type Domain struct {
	Group   string
	Project string

	DomainName    string
	Certificate   string
	Key           string
	HTTPSOnly     bool
	ProjectID     uint64
	AccessControl bool

	GroupConfig GroupConfig // handles group domain config
	Serving     serving.Serving

	certificate      *tls.Certificate
	certificateError error
	certificateOnce  sync.Once
}

// String implements Stringer.
func (d *Domain) String() string {
	if d.Group != "" && d.Project != "" {
		return d.Group + "/" + d.Project
	}

	if d.Group != "" {
		return d.Group
	}

	return d.Project
}

func (d *Domain) isCustomDomain() bool {
	return d.GroupConfig == nil
}

// IsHTTPSOnly figures out if the request should be handled with HTTPS
// only by looking at group and project level config.
func (d *Domain) IsHTTPSOnly(r *http.Request) bool {
	if d == nil {
		return false
	}

	// Check custom domain config (e.g. http://example.com)
	if d.isCustomDomain() {
		return d.HTTPSOnly
	}

	// Check projects served under the group domain, including the default one
	return d.GroupConfig.IsHTTPSOnly(r)
}

// IsAccessControlEnabled figures out if the request is to a project that has access control enabled
func (d *Domain) IsAccessControlEnabled(r *http.Request) bool {
	if d == nil {
		return false
	}

	// Check custom domain config (e.g. http://example.com)
	if d.isCustomDomain() {
		return d.AccessControl
	}

	// Check projects served under the group domain, including the default one
	return d.GroupConfig.HasAccessControl(r)
}

// HasAcmeChallenge checks domain directory contains particular acme challenge
func (d *Domain) HasAcmeChallenge(token string) bool {
	if d == nil {
		return false
	}

	if !d.isCustomDomain() {
		return false
	}

	return d.Serving.HasAcmeChallenge(token)
}

// IsNamespaceProject figures out if the request is to a namespace project
func (d *Domain) IsNamespaceProject(r *http.Request) bool {
	if d == nil {
		return false
	}

	// If request is to a custom domain, we do not handle it as a namespace project
	// as there can't be multiple projects under the same custom domain
	if d.isCustomDomain() {
		return false
	}

	// Check projects served under the group domain, including the default one
	return d.GroupConfig.IsNamespaceProject(r)
}

// GetID figures out what is the ID of the project user tries to access
func (d *Domain) GetID(r *http.Request) uint64 {
	if d == nil {
		return 0
	}

	if d.isCustomDomain() {
		return d.ProjectID
	}

	return d.GroupConfig.ProjectID(r)
}

// HasProject figures out if the project exists that the user tries to access
func (d *Domain) HasProject(r *http.Request) bool {
	if d == nil {
		return false
	}

	if d.isCustomDomain() {
		return true
	}

	return d.GroupConfig.ProjectExists(r)
}

// EnsureCertificate parses the PEM-encoded certificate for the domain
func (d *Domain) EnsureCertificate() (*tls.Certificate, error) {
	if !d.isCustomDomain() {
		return nil, errors.New("tls certificates can be loaded only for pages with configuration")
	}

	d.certificateOnce.Do(func() {
		var cert tls.Certificate
		cert, d.certificateError = tls.X509KeyPair([]byte(d.Certificate), []byte(d.Key))
		if d.certificateError == nil {
			d.certificate = &cert
		}
	})

	return d.certificate, d.certificateError
}

// ServeFileHTTP returns true if something was served, false if not.
func (d *Domain) ServeFileHTTP(w http.ResponseWriter, r *http.Request) bool {
	if d == nil {
		httperrors.Serve404(w)
		return true
	}

	if !d.IsAccessControlEnabled(r) {
		// Set caching headers
		w.Header().Set("Cache-Control", "max-age=600")
		w.Header().Set("Expires", time.Now().Add(10*time.Minute).Format(time.RFC1123))
	}

	return d.Serving.ServeFileHTTP(w, r)
}

// ServeNotFoundHTTP serves the not found pages from the projects.
func (d *Domain) ServeNotFoundHTTP(w http.ResponseWriter, r *http.Request) {
	if d == nil {
		httperrors.Serve404(w)
		return
	}

	d.Serving.ServeNotFoundHTTP(w, r)
}