Skip to content

Commit

Permalink
Merge pull request #34 from arooshap/master
Browse files Browse the repository at this point in the history
Changes to limit debug functionality and add http -> https redirect..
  • Loading branch information
vkuznet authored Oct 15, 2024
2 parents 895ca34 + be296d0 commit 5d8c91e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ type Configuration struct {

ZapLogger string `json:"zap_logger"` // define zap logger usage

// debug server info
DebugAllowedIPs []string `json:"debug_allowed_ips"` // list of allowed IPs to view debug/profile info

// Monit pieces
MonitType string `json:"monit_type"` // monit record type
MonitProducer string `json:"monit_producer"` // monit record producer
Expand Down
3 changes: 3 additions & 0 deletions oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,9 @@ func oauthProxyServer() {

// the callback authentication handler
http.HandleFunc(fmt.Sprintf("%s/callback", Config.Base), oauthCallbackHandler)

// Only expose debug endpoints (pprof, expvar) if the client IP is allowed
http.HandleFunc("/debug/", debugHandler)

// the request handler
http.HandleFunc("/", oauthRequestHandler)
Expand Down
22 changes: 22 additions & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"crypto/tls"
"fmt"
"log"
"net/http"
"time"
Expand All @@ -26,6 +27,13 @@ var NumLogicalCores int
// CMSAuth structure to create CMS Auth headers
var CMSAuth cmsauth.CMSAuth

// redirectToHTTPS will redirect all HTTP requests to HTTPS
func redirectToHTTPS(w http.ResponseWriter, r *http.Request) {
httpsURL := fmt.Sprintf("https://%s%s", r.Host, r.URL.RequestURI())
log.Printf("redirect %s to https\n", r.URL.String())
http.Redirect(w, r, httpsURL, http.StatusMovedPermanently)
}

// Server starts APS server
func Server(config string, port, metricsPort int, logFile string, useX509, scitokens, rules bool) {
err := parseConfig(config)
Expand Down Expand Up @@ -124,6 +132,20 @@ func Server(config string, port, metricsPort int, logFile string, useX509, scito
Config.CollectorPassword,
httpClient)

// start HTTP server for redirecting http requests to https end-point
go func() {
httpServer := &http.Server{
Addr: ":80", // HTTP on port 80
Handler: http.HandlerFunc(redirectToHTTPS),
}

log.Println("HTTP to HTTPS redirect server is running on port 80...")
err := httpServer.ListenAndServe()
if err != nil {
log.Println("Error starting HTTP server:", err)
}
}()

// start our servers
if useX509 {
if Config.CricURL != "" || Config.CricFile != "" {
Expand Down
26 changes: 26 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -647,3 +648,28 @@ func SetReferrer(r *http.Request) {
r.Header.Set("Referer", ref)
r.Header.Set("Referrer", ref)
}

// Checks if the remote IP is in the allowed range
func isAllowedIP(r *http.Request) bool {
// Extract the remote IP from the request (format could be IP:port)
ip, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Printf("Error parsing RemoteAddr: %v\n", err)
return false
}

// check if IP is allowed to view debug info
return InList(ip, Config.DebugAllowedIPs)
}

// Middleware to restrict pprof and expvar to allowed IPs
func debugHandler(w http.ResponseWriter, r *http.Request) {
if !isAllowedIP(r) {
http.Error(w, "403 Forbidden", http.StatusForbidden)
return
}

// Serve the original debug endpoint if the IP is allowed
http.DefaultServeMux.ServeHTTP(w, r)
}

3 changes: 3 additions & 0 deletions x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ func x509ProxyServer() {
// the server settings handler
http.HandleFunc(fmt.Sprintf("%s/server", Config.Base), settingsHandler)

// Only expose debug endpoints (pprof, expvar) if the client IP is allowed
http.HandleFunc("/debug/", debugHandler)

// the request handler
http.HandleFunc("/", x509RequestHandler)

Expand Down

0 comments on commit 5d8c91e

Please sign in to comment.