Skip to content

Commit

Permalink
Add a way for the grader to request problems (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhchavez authored Dec 14, 2021
1 parent 0053b78 commit d6235b1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
20 changes: 15 additions & 5 deletions cmd/omegaup-gitserver/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ func (a *omegaupAuthorization) parseUsernameAndPassword(
return
}

if username == "omegaup:system" {
// omegaup:system can only log in using the auth token or the secret token.
if strings.HasPrefix(username, "omegaup:") {
// omegaup:system and friends can only log in using the auth token or the secret token.
a.log.Error(
"user tried to login with restricted user",
map[string]interface{}{
Expand Down Expand Up @@ -231,13 +231,15 @@ func (a *omegaupAuthorization) parseAuthorizationHeader(
}
}

if a.config.Gitserver.AllowSecretTokenAuthentication && a.config.Gitserver.SecretToken != "" {
if strings.EqualFold(tokens[0], omegaUpSharedSecretAuthenticationScheme) {
if a.config.Gitserver.AllowSecretTokenAuthentication &&
strings.EqualFold(tokens[0], omegaUpSharedSecretAuthenticationScheme) {
if a.config.Gitserver.SecretToken != "" || a.config.Gitserver.GraderSecretToken != "" {
if len(tokens) != 3 {
return
}

if tokens[1] != a.config.Gitserver.SecretToken {
if (tokens[1] != a.config.Gitserver.SecretToken) &&
(tokens[1] != a.config.Gitserver.GraderSecretToken || tokens[2] != "omegaup:grader") {
return
}

Expand Down Expand Up @@ -388,18 +390,25 @@ func (a *omegaupAuthorization) authorize(
// This is a legit health check, so we grant privileges to the test problem.
requestContext.Request.CanView = true
requestContext.Request.CanEdit = true
requestContext.Request.CanViewAllRefs = true
} else if username == "omegaup:grader" || *insecureSkipAuthorization {
// This is the grader, it has read-only privileges for all problems.
requestContext.Request.CanView = true
requestContext.Request.CanViewAllRefs = true
} else if username == "omegaup:system" || *insecureSkipAuthorization {
// This is the frontend, and we trust it completely.
requestContext.Request.IsSystem = true
requestContext.Request.IsAdmin = true
requestContext.Request.CanView = true
requestContext.Request.CanEdit = true
requestContext.Request.CanViewAllRefs = true
} else if requestContext.Request.Create {
// This is a repository creation request. There is nothing in the database
// yet, so grant them all privileges.
requestContext.Request.IsAdmin = true
requestContext.Request.CanView = true
requestContext.Request.CanEdit = true
requestContext.Request.CanViewAllRefs = true
} else {
auth, err := a.getAuthorizationFromFrontend(
username,
Expand All @@ -421,6 +430,7 @@ func (a *omegaupAuthorization) authorize(
requestContext.Request.IsAdmin = auth.IsAdmin
requestContext.Request.CanView = auth.CanView
requestContext.Request.CanEdit = auth.CanEdit
requestContext.Request.CanViewAllRefs = auth.CanEdit
}
a.log.Info(
"Auth",
Expand Down
5 changes: 5 additions & 0 deletions cmd/omegaup-gitserver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ type GitserverConfig struct {
// authenticate instead of using PKI, in both directions.
SecretToken string

// GraderSecretToken is a shared secret with the grader that can be used to
// authenticate instead of using PKI, in both directions.
GraderSecretToken string

// AllowSecretTokenAuthentication controls whether the SecretToken can be
// used to authenticate incoming requests, instead of just being used for
// outgoing requests towards the frontend.
Expand Down Expand Up @@ -84,6 +88,7 @@ var defaultConfig = Config{
RootPath: "/var/lib/omegaup/problems.git",
PublicKey: "gKEg5JlIOA1BsIxETZYhjd+ZGchY/rZeQM0GheAWvXw=",
SecretToken: "",
GraderSecretToken: "",
Port: 33861,
PprofPort: 33862,
LibinteractivePath: "/usr/share/java/libinteractive.jar",
Expand Down
2 changes: 1 addition & 1 deletion cmd/omegaup-gitserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func referenceDiscovery(
referenceName string,
) bool {
requestContext := request.FromContext(ctx)
if requestContext.Request.CanEdit {
if requestContext.Request.CanViewAllRefs {
return true
}
if requestContext.Request.HasSolved {
Expand Down
19 changes: 10 additions & 9 deletions request/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ const requestContextKey key = 0
// Request stores the request-specific part of the Context, to make it easier
// to serialize.
type Request struct {
ProblemName string
Username string
Create bool
IsSystem bool
IsAdmin bool
CanView bool
CanEdit bool
HasSolved bool
ReviewRef string
ProblemName string
Username string
Create bool
IsSystem bool
IsAdmin bool
CanView bool
CanViewAllRefs bool
CanEdit bool
HasSolved bool
ReviewRef string
}

// Context stores a few variables that are request-specific.
Expand Down

0 comments on commit d6235b1

Please sign in to comment.