Skip to content

Commit

Permalink
fix check proxy auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Son Roy Almerol committed Nov 18, 2024
1 parent dee20d1 commit d856f17
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
3 changes: 2 additions & 1 deletion internal/proxy/controllers/agents/agents.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ func AgentLogHandler(storeInstance *store.Store) func(http.ResponseWriter, *http
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

syslogger, err := syslog.InitializeLogger()
Expand Down
9 changes: 6 additions & 3 deletions internal/proxy/controllers/exclusions/exclusions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func D2DExclusionHandler(storeInstance *store.Store) func(http.ResponseWriter, *
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

if r.Method == http.MethodGet {
Expand Down Expand Up @@ -58,7 +59,8 @@ func ExtJsExclusionHandler(storeInstance *store.Store) func(http.ResponseWriter,
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

w.Header().Set("Content-Type", "application/json")
Expand Down Expand Up @@ -97,7 +99,8 @@ func ExtJsExclusionSingleHandler(storeInstance *store.Store) func(http.ResponseW
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

w.Header().Set("Content-Type", "application/json")
Expand Down
12 changes: 8 additions & 4 deletions internal/proxy/controllers/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ func D2DJobHandler(storeInstance *store.Store) func(http.ResponseWriter, *http.R
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

allJobs, err := storeInstance.GetAllJobs()
Expand Down Expand Up @@ -57,7 +58,8 @@ func ExtJsJobRunHandler(storeInstance *store.Store) func(http.ResponseWriter, *h
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

job, err := storeInstance.GetJob(pathVar["job"])
Expand Down Expand Up @@ -92,7 +94,8 @@ func ExtJsJobHandler(storeInstance *store.Store) func(http.ResponseWriter, *http
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

w.Header().Set("Content-Type", "application/json")
Expand Down Expand Up @@ -136,7 +139,8 @@ func ExtJsJobSingleHandler(storeInstance *store.Store) func(http.ResponseWriter,
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

w.Header().Set("Content-Type", "application/json")
Expand Down
9 changes: 6 additions & 3 deletions internal/proxy/controllers/partial_files/partial_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func D2DPartialFileHandler(storeInstance *store.Store) func(http.ResponseWriter,
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

if r.Method == http.MethodGet {
Expand Down Expand Up @@ -58,7 +59,8 @@ func ExtJsPartialFileHandler(storeInstance *store.Store) func(http.ResponseWrite
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

w.Header().Set("Content-Type", "application/json")
Expand Down Expand Up @@ -96,7 +98,8 @@ func ExtJsPartialFileSingleHandler(storeInstance *store.Store) func(http.Respons
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

w.Header().Set("Content-Type", "application/json")
Expand Down
13 changes: 9 additions & 4 deletions internal/proxy/controllers/targets/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ func D2DTargetHandler(storeInstance *store.Store) func(http.ResponseWriter, *htt
return func(w http.ResponseWriter, r *http.Request, pathVar map[string]string) {
if r.Method != http.MethodGet && r.Method != http.MethodPost {
http.Error(w, "Invalid HTTP method", http.StatusBadRequest)
return
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

if r.Method == http.MethodGet {
Expand Down Expand Up @@ -109,7 +111,8 @@ func D2DTargetAgentHandler(storeInstance *store.Store) func(http.ResponseWriter,
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

var reqParsed NewAgentHostnameRequest
Expand Down Expand Up @@ -164,7 +167,8 @@ func ExtJsTargetHandler(storeInstance *store.Store) func(http.ResponseWriter, *h
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

w.Header().Set("Content-Type", "application/json")
Expand Down Expand Up @@ -207,7 +211,8 @@ func ExtJsTargetSingleHandler(storeInstance *store.Store) func(http.ResponseWrit
}

if err := storeInstance.CheckProxyAuth(r); err != nil {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
http.Error(w, "authentication failed - no authentication credentials provided", http.StatusUnauthorized)
return
}

w.Header().Set("Content-Type", "application/json")
Expand Down
4 changes: 4 additions & 0 deletions internal/store/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,9 @@ func (storeInstance *Store) CheckProxyAuth(r *http.Request) error {
resp.Body.Close()
}()

if resp.StatusCode > 299 || resp.StatusCode < 200 {
return fmt.Errorf("CheckProxyAuth: invalid auth -> %w", err)
}

return nil
}

0 comments on commit d856f17

Please sign in to comment.