-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
289 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"github.com/vs-uulm/ztsfc_http_pip/internal/app/router" | ||
"github.com/vs-uulm/ztsfc_http_pip/internal/app/config" | ||
"github.com/vs-uulm/ztsfc_http_pip/internal/app/device" | ||
yt "github.com/leobrada/yaml_tools" | ||
logger "github.com/vs-uulm/ztsfc_http_logger" | ||
confInit "github.com/vs-uulm/ztsfc_http_pip/internal/app/init" | ||
ti "github.com/vs-uulm/ztsfc_http_pip/internal/app/threat_intelligence" | ||
) | ||
|
||
//var ( | ||
// SysLogger *logger.Logger | ||
//) | ||
|
||
func init() { | ||
var confFilePath string | ||
|
||
flag.StringVar(&confFilePath, "c", "./config/conf.yml", "Path to user defined yaml config file") | ||
flag.Parse() | ||
|
||
err := yt.LoadYamlFile(confFilePath, &config.Config) | ||
if err != nil { | ||
log.Fatalf("main: init(): could not load yaml file: %v", err) | ||
} | ||
|
||
confInit.InitSysLoggerParams() | ||
config.SysLogger, err = logger.New(config.Config.SysLogger.LogFilePath, | ||
config.Config.SysLogger.LogLevel, | ||
config.Config.SysLogger.IfTextFormatter, | ||
logger.Fields{"type": "system"}, | ||
) | ||
if err != nil { | ||
log.Fatalf("main: init(): could not initialize logger: %v", err) | ||
} | ||
config.SysLogger.Debugf("loading logger configuration from %s - OK", confFilePath) | ||
|
||
if err = confInit.InitConfig(); err != nil { | ||
config.SysLogger.Fatalf("main: init(): could not initialize Environment params: %v", err) | ||
} | ||
|
||
// For testing | ||
device.LoadTestDevices() | ||
} | ||
|
||
func main() { | ||
go ti.RunThreatIntelligence() | ||
|
||
device.PrintDevices() | ||
|
||
pip := router.NewRouter() | ||
|
||
pip.ListenAndServeTLS() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package device | ||
|
||
import ( | ||
"github.com/vs-uulm/ztsfc_http_pip/internal/app/config" | ||
) | ||
|
||
var ( | ||
DevicesByID = make(map[string]*Device) | ||
DevicesByIP = make(map[string]*Device) | ||
) | ||
|
||
type Device struct { | ||
DeviceID string `json:"deviceID"` | ||
CurrentIP string `json:"currentIP"` | ||
Revoked bool `json:"revoked"` | ||
} | ||
|
||
func NewDevice(_deviceID, _currentIP string, _revoked bool) (*Device, error) { | ||
newDevice := new(Device) | ||
newDevice.DeviceID = _deviceID | ||
newDevice.CurrentIP = _currentIP | ||
newDevice.Revoked = _revoked | ||
return newDevice, nil | ||
} | ||
|
||
func PrintDevices() { | ||
for _, deviceObj := range DevicesByID { | ||
config.SysLogger.Infof("%v\n", deviceObj) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package device | ||
|
||
func LoadTestDevices() { | ||
m1MacMini, _ := NewDevice("M1 Mac Mini", "", false) | ||
DevicesByID[m1MacMini.DeviceID] = m1MacMini | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package init | ||
|
||
import ( | ||
"fmt" | ||
"crypto/x509" | ||
|
||
"github.com/vs-uulm/ztsfc_http_pip/internal/app/config" | ||
gct "github.com/leobrada/golang_convenience_tools" | ||
) | ||
|
||
func initPip() error { | ||
fields := "" | ||
var err error | ||
|
||
if config.Config.Pip.ListenAddr == "" { | ||
fields += "listen_addr" | ||
} | ||
|
||
if config.Config.Pip.CertsPipAcceptsWhenShownByPdp == nil { | ||
fields += "certs_pip_accepts_when_shown_by_pdp" | ||
} | ||
|
||
|
||
if config.Config.Pip.CertShownByPipToPdp == "" { | ||
fields += "cert_shown_by_pip_to_pdp" | ||
} | ||
|
||
if config.Config.Pip.PrivkeyForCertShownByPipToPdp == "" { | ||
fields += "privkey_for_certs_shown_by_pip_to_pdp" | ||
} | ||
|
||
// Read CA certs used for signing client certs and are accepted by the PEP | ||
config.Config.Pip.CaCertPoolPipAcceptsFromPdp = x509.NewCertPool() | ||
for _, acceptedPdpCert := range config.Config.Pip.CertsPipAcceptsWhenShownByPdp { | ||
if err = gct.LoadCACertificate(acceptedPdpCert, config.Config.Pip.CaCertPoolPipAcceptsFromPdp); err != nil { | ||
return fmt.Errorf("initPipParams(): error loading certificates PIP accepts from PDP: %w", err) | ||
} | ||
} | ||
|
||
config.Config.Pip.X509KeyPairShownByPipToPdp, err = gct.LoadX509KeyPair(config.Config.Pip.CertShownByPipToPdp, | ||
config.Config.Pip.PrivkeyForCertShownByPipToPdp) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package router | ||
|
||
import ( | ||
"crypto/tls" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/vs-uulm/ztsfc_http_pip/internal/app/config" | ||
"github.com/vs-uulm/ztsfc_http_pip/internal/app/device" | ||
) | ||
|
||
const ( | ||
// Request URIs for the API endpoint. | ||
getDeviceEndpoint = "/get-device-attributes" | ||
) | ||
|
||
type Router struct { | ||
frontend_tls_config *tls.Config | ||
frontend_server *http.Server | ||
} | ||
|
||
func NewRouter() *Router { | ||
|
||
// Create new Router | ||
router := new(Router) | ||
|
||
// Create TLS config for frontend server | ||
router.frontend_tls_config = &tls.Config{ | ||
Rand: nil, | ||
Time: nil, | ||
MinVersion: tls.VersionTLS13, | ||
MaxVersion: tls.VersionTLS13, | ||
SessionTicketsDisabled: true, | ||
Certificates: []tls.Certificate{config.Config.Pip.X509KeyPairShownByPipToPdp}, | ||
ClientAuth: tls.RequireAndVerifyClientCert, | ||
ClientCAs: config.Config.Pip.CaCertPoolPipAcceptsFromPdp, | ||
} | ||
|
||
// Create MUX server | ||
http.HandleFunc(getDeviceEndpoint, handleGetDeviceRequests) | ||
|
||
// Create HTTP frontend server | ||
router.frontend_server = &http.Server{ | ||
Addr: config.Config.Pip.ListenAddr, | ||
TLSConfig: router.frontend_tls_config, | ||
} | ||
|
||
return router | ||
} | ||
|
||
func handleGetDeviceRequests(w http.ResponseWriter, req *http.Request) { | ||
q := req.URL.Query() | ||
|
||
dev := q.Get("device"); | ||
if len(dev) == 0 { | ||
config.SysLogger.Infof("router: handleGetDeviceRequests(): get device request did not contain a device") | ||
w.WriteHeader(404) | ||
return | ||
} | ||
|
||
requestedDevice, ok := device.DevicesByID[dev] | ||
if !ok { | ||
config.SysLogger.Infof("router: handleGetDeviceRequests(): PDP requested a device that does not exist in the DB") | ||
w.WriteHeader(404) | ||
return | ||
} | ||
|
||
config.SysLogger.Infof("router: handleGetDeviceRequests(): PDP requested the following device: %v", requestedDevice) | ||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(requestedDevice) | ||
} | ||
|
||
func (router *Router) ListenAndServeTLS() error { | ||
return router.frontend_server.ListenAndServeTLS("", "") | ||
} |
Oops, something went wrong.