···
15
-
"github.com/bluesky-social/indigo/atproto/identity"
securejoin "github.com/cyphar/filepath-securejoin"
"github.com/urfave/cli/v3"
18
-
"tangled.org/core/idresolver"
19
-
"tangled.org/core/knotserver/config"
···
func Run(ctx context.Context, cmd *cli.Command) error {
l := log.FromContext(ctx)
61
-
c, err := config.Load(ctx)
63
-
return fmt.Errorf("failed to load config: %w", err)
incomingUser := cmd.String("user")
gitDir := cmd.String("git-dir")
logPath := cmd.String("log-path")
···
94
+
// TODO: greet user with their resolved handle instead of did
l.Info("access denied: no interactive shells", "user", incomingUser)
fmt.Fprintf(os.Stderr, "Hi @%s! You've successfully authenticated.\n", incomingUser)
···
gitCommand := cmdParts[0]
117
-
// did:foo/repo-name or
118
-
// handle/repo-name or
119
-
// any of the above with a leading slash (/)
121
-
components := strings.Split(strings.TrimPrefix(strings.Trim(cmdParts[1], "'"), "/"), "/")
122
-
l.Info("command components", "components", components)
124
-
if len(components) != 2 {
125
-
l.Error("invalid repo format", "components", components)
126
-
fmt.Fprintln(os.Stderr, "invalid repo format, needs <user>/<repo> or /<user>/<repo>")
130
-
didOrHandle := components[0]
131
-
identity := resolveIdentity(ctx, c, l, didOrHandle)
132
-
did := identity.DID.String()
133
-
repoName := components[1]
134
-
qualifiedRepoName, _ := securejoin.SecureJoin(did, repoName)
109
+
repoPath := cmdParts[1]
validCommands := map[string]bool{
"git-receive-pack": true,
···
fmt.Fprintln(os.Stderr, "access denied: invalid git command")
return fmt.Errorf("access denied: invalid git command")
147
-
if gitCommand != "git-upload-pack" {
148
-
if !isPushPermitted(l, incomingUser, qualifiedRepoName, endpoint) {
149
-
l.Error("access denied: user not allowed",
150
-
"did", incomingUser,
151
-
"reponame", qualifiedRepoName)
152
-
fmt.Fprintln(os.Stderr, "access denied: user not allowed")
122
+
// qualify repo path from internal server which holds the knot config
123
+
qualifiedRepoPath, err := guardAndQualifyRepo(l, endpoint, incomingUser, repoPath, gitCommand)
125
+
l.Error("failed to run guard", "err", err)
126
+
fmt.Fprintln(os.Stderr, err)
157
-
fullPath, _ := securejoin.SecureJoin(gitDir, qualifiedRepoName)
130
+
fullPath, _ := securejoin.SecureJoin(gitDir, qualifiedRepoPath)
l.Info("processing command",
···
gitCmd.Env = append(os.Environ(),
fmt.Sprintf("GIT_USER_DID=%s", incomingUser),
186
-
fmt.Sprintf("GIT_USER_PDS_ENDPOINT=%s", identity.PDSEndpoint()),
if err := gitCmd.Run(); err != nil {
···
l.Info("command completed",
204
-
func resolveIdentity(ctx context.Context, c *config.Config, l *slog.Logger, didOrHandle string) *identity.Identity {
205
-
resolver := idresolver.DefaultResolver(c.Server.PlcUrl)
206
-
ident, err := resolver.ResolveIdent(ctx, didOrHandle)
208
-
l.Error("Error resolving handle", "error", err, "handle", didOrHandle)
209
-
fmt.Fprintf(os.Stderr, "error resolving handle: %v\n", err)
212
-
if ident.Handle.IsInvalidHandle() {
213
-
l.Error("Error resolving handle", "invalid handle", didOrHandle)
214
-
fmt.Fprintf(os.Stderr, "error resolving handle: invalid handle\n")
220
-
func isPushPermitted(l *slog.Logger, user, qualifiedRepoName, endpoint string) bool {
221
-
u, _ := url.Parse(endpoint + "/push-allowed")
176
+
// runs guardAndQualifyRepo logic
177
+
func guardAndQualifyRepo(l *slog.Logger, endpoint, incomingUser, repo, gitCommand string) (string, error) {
178
+
u, _ := url.Parse(endpoint + "/guard")
223
-
q.Add("user", user)
224
-
q.Add("repo", qualifiedRepoName)
180
+
q.Add("user", incomingUser)
181
+
q.Add("repo", repo)
182
+
q.Add("gitCmd", gitCommand)
227
-
req, err := http.Get(u.String())
185
+
resp, err := http.Get(u.String())
229
-
l.Error("Error verifying permissions", "error", err)
230
-
fmt.Fprintf(os.Stderr, "error verifying permissions: %v\n", err)
189
+
defer resp.Body.Close()
191
+
l.Info("Running guard", "url", u.String(), "status", resp.Status)
234
-
l.Info("Checking push permission",
236
-
"status", req.Status)
193
+
body, err := io.ReadAll(resp.Body)
197
+
text := string(body)
238
-
return req.StatusCode == http.StatusNoContent
199
+
switch resp.StatusCode {
200
+
case http.StatusOK:
202
+
case http.StatusForbidden:
203
+
l.Error("access denied: user not allowed", "did", incomingUser, "reponame", text)
204
+
return text, errors.New("access denied: user not allowed")
206
+
return "", errors.New(text)