Skip to content

Commit e3e0e13

Browse files
committed
Use tagged switches more, rename ValidatePerm
1 parent 7b62e5c commit e3e0e13

File tree

6 files changed

+40
-36
lines changed

6 files changed

+40
-36
lines changed

auth/authenticator.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ type Authenticator interface {
2323
ValidateAndReject(w http.ResponseWriter, r *http.Request) string
2424
// HasPerm returns a boolean indicating whether or not the user has the requested permission.
2525
HasPerm(username string, permission string) (bool, error)
26-
// ValidatePermAndReject is called on an HTTP API request and returns the username if request is
26+
// ValidateWithPermAndReject is called on an HTTP API request and returns the username if request is
2727
// authenticated, and a boolean indicating whether or not the user has the requested permission.
2828
// If unauthenticated or forbidden, the request is rejected with a 401 or 403 error respectively.
29-
ValidatePermAndReject(w http.ResponseWriter, r *http.Request, permission string) (string, bool)
29+
ValidateWithPermAndReject(w http.ResponseWriter, r *http.Request, permission string) (string, bool)
3030
// CanManageAuth returns whether or not this authenticator can manage auth, i.e. users and tokens.
3131
CanManageAuth() bool
3232
// Login allows logging in a user and returning the token.
@@ -81,13 +81,13 @@ func (a *ReplaceableAuthenticator) HasPerm(username string, permission string) (
8181
return a.Engine.HasPerm(username, permission)
8282
}
8383

84-
// ValidatePermAndReject is called on an HTTP API request and returns the username if request is
84+
// ValidateWithPermAndReject is called on an HTTP API request and returns the username if request is
8585
// authenticated, and a boolean indicating whether or not the user has the requested permission.
8686
// If unauthenticated or forbidden, the request is rejected with a 401 or 403 error respectively.
87-
func (a *ReplaceableAuthenticator) ValidatePermAndReject(w http.ResponseWriter, r *http.Request, permission string) (string, bool) {
87+
func (a *ReplaceableAuthenticator) ValidateWithPermAndReject(w http.ResponseWriter, r *http.Request, permission string) (string, bool) {
8888
a.EngineMutex.RLock()
8989
defer a.EngineMutex.RUnlock()
90-
return a.Engine.ValidatePermAndReject(w, r, permission)
90+
return a.Engine.ValidateWithPermAndReject(w, r, permission)
9191
}
9292

9393
// CanManageAuth returns whether or not this authenticator can manage auth, i.e. users and tokens.

auth/memory.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ func (*MemoryAuthenticator) HasPerm(_ string, _ string) (bool, error) {
104104
return true, nil
105105
}
106106

107-
// ValidatePermAndReject is called on an HTTP API request and returns the username if request is
107+
// ValidateWithPermAndReject is called on an HTTP API request and returns the username if request is
108108
// authenticated, and a boolean indicating whether or not the user has the requested permission.
109109
// If unauthenticated or forbidden, the request is rejected with a 401 or 403 error respectively.
110-
func (a *MemoryAuthenticator) ValidatePermAndReject(w http.ResponseWriter, r *http.Request, permission string) (string, bool) {
110+
func (a *MemoryAuthenticator) ValidateWithPermAndReject(w http.ResponseWriter, r *http.Request, permission string) (string, bool) {
111111
username := a.ValidateAndReject(w, r)
112112
if username == "" {
113113
return "", false

auth/redis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ func (*RedisAuthenticator) HasPerm(_ string, _ string) (bool, error) {
171171
return true, nil
172172
}
173173

174-
// ValidatePermAndReject is called on an HTTP API request and returns the username if request is
174+
// ValidateWithPermAndReject is called on an HTTP API request and returns the username if request is
175175
// authenticated, and a boolean indicating whether or not the user has the requested permission.
176176
// If unauthenticated or forbidden, the request is rejected with a 401 or 403 error respectively.
177-
func (a *RedisAuthenticator) ValidatePermAndReject(w http.ResponseWriter, r *http.Request, permission string) (string, bool) {
177+
func (a *RedisAuthenticator) ValidateWithPermAndReject(w http.ResponseWriter, r *http.Request, permission string) (string, bool) {
178178
username := a.ValidateAndReject(w, r)
179179
if username == "" {
180180
return "", false

endpoints_auth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,17 +145,17 @@ type accountsRequestBody struct {
145145
}
146146

147147
func accountsEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request) {
148-
if !connector.Authenticator.CanManageAuth() {
148+
if r.Method != "GET" && r.Method != "POST" && r.Method != "PATCH" && r.Method != "DELETE" {
149+
httpError(w, "Only GET, POST, PATCH and DELETE are allowed!", http.StatusMethodNotAllowed)
150+
return
151+
} else if !connector.Authenticator.CanManageAuth() {
149152
httpError(w, "This node does not support managing users. "+
150153
"Perform user management on the primary node!", http.StatusForbidden)
151154
return
152155
}
153156
user := connector.ValidateAndReject(w, r)
154157
if user == "" {
155158
return
156-
} else if r.Method != "GET" && r.Method != "POST" && r.Method != "PATCH" && r.Method != "DELETE" {
157-
httpError(w, "Only GET, POST, PATCH and DELETE are allowed!", http.StatusMethodNotAllowed)
158-
return
159159
}
160160
var users map[string]string
161161
contents, err := os.ReadFile(UsersJsonPath)

endpoints_files.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ func filesEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request)
5656
httpError(w, "This server does not exist!", http.StatusNotFound)
5757
return
5858
}
59-
if r.Method == "GET" {
59+
switch r.Method {
60+
case "GET":
6061
filesEndpointGet(w, r, process)
61-
} else if r.Method == "PATCH" {
62+
case "PATCH":
6263
filesEndpointPatch(connector, w, r, process, id, user)
63-
} else {
64+
default:
6465
httpError(w, "Only GET and PATCH are allowed!", http.StatusMethodNotAllowed)
6566
}
6667
}
@@ -363,15 +364,16 @@ func fileEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request)
363364
httpError(w, "The file requested is outside the server!", http.StatusForbidden)
364365
return
365366
}
366-
if r.Method == "GET" {
367+
switch r.Method {
368+
case "GET":
367369
fileEndpointGet(connector, w, r, id, filePath, user)
368-
} else if r.Method == "DELETE" {
370+
case "DELETE":
369371
fileEndpointDelete(connector, w, r, id, filePath, user)
370-
} else if r.Method == "POST" {
372+
case "POST":
371373
fileEndpointPost(connector, w, r, id, filePath, user)
372-
} else if r.Method == "PATCH" {
374+
case "PATCH":
373375
fileEndpointPatch(connector, w, r, process, id, user)
374-
} else {
376+
default:
375377
httpError(w, "Only GET, POST, PATCH and DELETE are allowed!", http.StatusMethodNotAllowed)
376378
}
377379
}
@@ -733,15 +735,16 @@ func compressionEndpoint(connector *Connector, w http.ResponseWriter, r *http.Re
733735
}
734736
} else {
735737
var archive *tar.Writer
736-
if compression == "true" || compression == "gzip" || compression == "" {
738+
switch compression {
739+
case "true", "gzip", "":
737740
compressionWriter := gzip.NewWriter(archiveFile)
738741
defer compressionWriter.Close()
739742
archive = tar.NewWriter(compressionWriter)
740-
} else if compression == "xz" || compression == "zstd" {
743+
case "xz", "zstd":
741744
compressionWriter := system.NativeCompressionWriter(archiveFile, compression)
742745
defer compressionWriter.Close()
743746
archive = tar.NewWriter(compressionWriter)
744-
} else {
747+
default:
745748
archive = tar.NewWriter(archiveFile)
746749
}
747750
defer archive.Close()

endpoints_misc.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ func configEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request
2828
user := connector.ValidateAndReject(w, r)
2929
if user == "" {
3030
return
31-
} else if r.Method != "GET" && r.Method != "PATCH" {
32-
httpError(w, "Only GET and PATCH are allowed!", http.StatusMethodNotAllowed)
33-
return
3431
}
35-
if r.Method == "GET" {
32+
switch r.Method {
33+
case "GET":
3634
contents, err := os.ReadFile(ConfigJsonPath)
3735
if err != nil {
3836
log.Println("Error reading "+ConfigJsonPath+" when user accessed /config!", err)
@@ -42,7 +40,7 @@ func configEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request
4240
connector.Info("config.view", "ip", GetIP(r), "user", user)
4341
w.Header().Set("content-type", "application/json")
4442
_, _ = w.Write(contents)
45-
} else if r.Method == "PATCH" {
43+
case "PATCH":
4644
var buffer bytes.Buffer
4745
_, err := buffer.ReadFrom(r.Body)
4846
if err != nil {
@@ -77,6 +75,8 @@ func configEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request
7775
connector.Info("config.edit", "ip", GetIP(r), "user", user, "newConfig", config)
7876
writeJsonStringRes(w, "{\"success\":true}")
7977
info.Println("Config updated remotely by user over HTTP API (see action logs for info)!")
78+
default:
79+
httpError(w, "Only GET and PATCH are allowed!", http.StatusMethodNotAllowed)
8080
}
8181
}
8282

@@ -156,11 +156,12 @@ func serverEndpoint(connector *Connector, w http.ResponseWriter, r *http.Request
156156
httpError(w, "This server does not exist!", http.StatusNotFound)
157157
return
158158
}
159-
if r.Method == "GET" {
159+
switch r.Method {
160+
case "GET":
160161
serverEndpointGet(w, process)
161-
} else if r.Method == "POST" {
162+
case "POST":
162163
serverEndpointPost(connector, w, r, process, id, user)
163-
} else {
164+
default:
164165
httpError(w, "Only GET and POST is allowed!", http.StatusMethodNotAllowed)
165166
}
166167
}
@@ -211,7 +212,8 @@ func serverEndpointPost(connector *Connector, w http.ResponseWriter, r *http.Req
211212
}
212213
operation := strings.ToUpper(body.String())
213214
// Check whether the operation is correct or not.
214-
if operation == "START" {
215+
switch operation {
216+
case "START":
215217
// Start process if required.
216218
if process.Online.Load() != 1 {
217219
err = process.StartProcess(connector)
@@ -221,7 +223,7 @@ func serverEndpointPost(connector *Connector, w http.ResponseWriter, r *http.Req
221223
res := make(map[string]bool)
222224
res["success"] = err == nil
223225
writeJsonStructRes(w, res) // skipcq GSC-G104
224-
} else if operation == "STOP" || operation == "KILL" || operation == "TERM" {
226+
case "STOP", "KILL", "TERM":
225227
// Stop process if required.
226228
if process.Online.Load() == 1 {
227229
// Octyne 2.x should drop STOP or move it to SIGTERM.
@@ -237,9 +239,8 @@ func serverEndpointPost(connector *Connector, w http.ResponseWriter, r *http.Req
237239
res := make(map[string]bool)
238240
res["success"] = true
239241
writeJsonStructRes(w, res) // skipcq GSC-G104
240-
} else {
242+
default:
241243
httpError(w, "Invalid operation requested!", http.StatusBadRequest)
242-
return
243244
}
244245
}
245246

0 commit comments

Comments
 (0)